michael@0: /*- michael@0: * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. michael@0: * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are met: michael@0: * michael@0: * a) Redistributions of source code must retain the above copyright notice, michael@0: * this list of conditions and the following disclaimer. michael@0: * michael@0: * b) Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in michael@0: * the documentation and/or other materials provided with the distribution. michael@0: * michael@0: * c) Neither the name of Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef __FreeBSD__ michael@0: #include michael@0: __FBSDID("$FreeBSD: head/sys/netinet/sctputil.c 259943 2013-12-27 13:07:00Z tuexen $"); michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef INET6 michael@0: #if defined(__Userspace__) || defined(__FreeBSD__) michael@0: #include michael@0: #endif michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include /* for sctp_deliver_data() */ michael@0: #include michael@0: #include michael@0: #include michael@0: #if defined(__Userspace__) michael@0: #include michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: #define APPLE_FILE_NO 8 michael@0: #endif michael@0: michael@0: #if defined(__Windows__) michael@0: #if !defined(SCTP_LOCAL_TRACE_BUF) michael@0: #include "eventrace_netinet.h" michael@0: #include "sctputil.tmh" /* this is the file that will be auto generated */ michael@0: #endif michael@0: #else michael@0: #ifndef KTR_SCTP michael@0: #define KTR_SCTP KTR_SUBSYS michael@0: #endif michael@0: #endif michael@0: michael@0: extern struct sctp_cc_functions sctp_cc_functions[]; michael@0: extern struct sctp_ss_functions sctp_ss_functions[]; michael@0: michael@0: void michael@0: sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.sb.stcb = stcb; michael@0: sctp_clog.x.sb.so_sbcc = sb->sb_cc; michael@0: if (stcb) michael@0: sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc; michael@0: else michael@0: sctp_clog.x.sb.stcb_sbcc = 0; michael@0: sctp_clog.x.sb.incr = incr; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_SB, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.close.inp = (void *)inp; michael@0: sctp_clog.x.close.sctp_flags = inp->sctp_flags; michael@0: if (stcb) { michael@0: sctp_clog.x.close.stcb = (void *)stcb; michael@0: sctp_clog.x.close.state = (uint16_t)stcb->asoc.state; michael@0: } else { michael@0: sctp_clog.x.close.stcb = 0; michael@0: sctp_clog.x.close.state = 0; michael@0: } michael@0: sctp_clog.x.close.loc = loc; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_CLOSE, michael@0: 0, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: rto_logging(struct sctp_nets *net, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: memset(&sctp_clog, 0, sizeof(sctp_clog)); michael@0: sctp_clog.x.rto.net = (void *) net; michael@0: sctp_clog.x.rto.rtt = net->rtt / 1000; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_RTT, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.strlog.stcb = stcb; michael@0: sctp_clog.x.strlog.n_tsn = tsn; michael@0: sctp_clog.x.strlog.n_sseq = sseq; michael@0: sctp_clog.x.strlog.e_tsn = 0; michael@0: sctp_clog.x.strlog.e_sseq = 0; michael@0: sctp_clog.x.strlog.strm = stream; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_STRM, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_nagle_event(struct sctp_tcb *stcb, int action) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.nagle.stcb = (void *)stcb; michael@0: sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight; michael@0: sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size; michael@0: sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue; michael@0: sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_NAGLE, michael@0: action, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.sack.cumack = cumack; michael@0: sctp_clog.x.sack.oldcumack = old_cumack; michael@0: sctp_clog.x.sack.tsn = tsn; michael@0: sctp_clog.x.sack.numGaps = gaps; michael@0: sctp_clog.x.sack.numDups = dups; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_SACK, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: memset(&sctp_clog, 0, sizeof(sctp_clog)); michael@0: sctp_clog.x.map.base = map; michael@0: sctp_clog.x.map.cum = cum; michael@0: sctp_clog.x.map.high = high; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_MAP, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: memset(&sctp_clog, 0, sizeof(sctp_clog)); michael@0: sctp_clog.x.fr.largest_tsn = biggest_tsn; michael@0: sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn; michael@0: sctp_clog.x.fr.tsn = tsn; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_FR, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_mb(struct mbuf *m, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.mb.mp = m; michael@0: sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m)); michael@0: sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m)); michael@0: sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0); michael@0: if (SCTP_BUF_IS_EXTENDED(m)) { michael@0: sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m); michael@0: #if defined(__APPLE__) michael@0: /* APPLE does not use a ref_cnt, but a forward/backward ref queue */ michael@0: #else michael@0: sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m)); michael@0: #endif michael@0: } else { michael@0: sctp_clog.x.mb.ext = 0; michael@0: sctp_clog.x.mb.refcnt = 0; michael@0: } michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_MBUF, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: if (control == NULL) { michael@0: SCTP_PRINTF("Gak log of NULL?\n"); michael@0: return; michael@0: } michael@0: sctp_clog.x.strlog.stcb = control->stcb; michael@0: sctp_clog.x.strlog.n_tsn = control->sinfo_tsn; michael@0: sctp_clog.x.strlog.n_sseq = control->sinfo_ssn; michael@0: sctp_clog.x.strlog.strm = control->sinfo_stream; michael@0: if (poschk != NULL) { michael@0: sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn; michael@0: sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn; michael@0: } else { michael@0: sctp_clog.x.strlog.e_tsn = 0; michael@0: sctp_clog.x.strlog.e_sseq = 0; michael@0: } michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_STRM, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.cwnd.net = net; michael@0: if (stcb->asoc.send_queue_cnt > 255) michael@0: sctp_clog.x.cwnd.cnt_in_send = 255; michael@0: else michael@0: sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt; michael@0: if (stcb->asoc.stream_queue_cnt > 255) michael@0: sctp_clog.x.cwnd.cnt_in_str = 255; michael@0: else michael@0: sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt; michael@0: michael@0: if (net) { michael@0: sctp_clog.x.cwnd.cwnd_new_value = net->cwnd; michael@0: sctp_clog.x.cwnd.inflight = net->flight_size; michael@0: sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack; michael@0: sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack; michael@0: sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack; michael@0: } michael@0: if (SCTP_CWNDLOG_PRESEND == from) { michael@0: sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd; michael@0: } michael@0: sctp_clog.x.cwnd.cwnd_augment = augment; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_CWND, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: #ifndef __APPLE__ michael@0: void michael@0: sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: memset(&sctp_clog, 0, sizeof(sctp_clog)); michael@0: if (inp) { michael@0: sctp_clog.x.lock.sock = (void *) inp->sctp_socket; michael@0: michael@0: } else { michael@0: sctp_clog.x.lock.sock = (void *) NULL; michael@0: } michael@0: sctp_clog.x.lock.inp = (void *) inp; michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version >= 503000) || (defined(__APPLE__)) michael@0: if (stcb) { michael@0: sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx); michael@0: } else { michael@0: sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN; michael@0: } michael@0: if (inp) { michael@0: sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx); michael@0: sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx); michael@0: } else { michael@0: sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN; michael@0: sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN; michael@0: } michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version <= 602000) michael@0: sctp_clog.x.lock.info_lock = mtx_owned(&SCTP_BASE_INFO(ipi_ep_mtx)); michael@0: #else michael@0: sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx)); michael@0: #endif michael@0: if (inp && (inp->sctp_socket)) { michael@0: sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx)); michael@0: sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx)); michael@0: sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx)); michael@0: } else { michael@0: sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN; michael@0: sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN; michael@0: sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN; michael@0: } michael@0: #endif michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_LOCK_EVENT, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: memset(&sctp_clog, 0, sizeof(sctp_clog)); michael@0: sctp_clog.x.cwnd.net = net; michael@0: sctp_clog.x.cwnd.cwnd_new_value = error; michael@0: sctp_clog.x.cwnd.inflight = net->flight_size; michael@0: sctp_clog.x.cwnd.cwnd_augment = burst; michael@0: if (stcb->asoc.send_queue_cnt > 255) michael@0: sctp_clog.x.cwnd.cnt_in_send = 255; michael@0: else michael@0: sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt; michael@0: if (stcb->asoc.stream_queue_cnt > 255) michael@0: sctp_clog.x.cwnd.cnt_in_str = 255; michael@0: else michael@0: sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_MAXBURST, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.rwnd.rwnd = peers_rwnd; michael@0: sctp_clog.x.rwnd.send_size = snd_size; michael@0: sctp_clog.x.rwnd.overhead = overhead; michael@0: sctp_clog.x.rwnd.new_rwnd = 0; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_RWND, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.rwnd.rwnd = peers_rwnd; michael@0: sctp_clog.x.rwnd.send_size = flight_size; michael@0: sctp_clog.x.rwnd.overhead = overhead; michael@0: sctp_clog.x.rwnd.new_rwnd = a_rwndval; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_RWND, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.mbcnt.total_queue_size = total_oq; michael@0: sctp_clog.x.mbcnt.size_change = book; michael@0: sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q; michael@0: sctp_clog.x.mbcnt.mbcnt_change = mbcnt; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_MBCNT, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_MISC_EVENT, michael@0: from, michael@0: a, b, c, d); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.wake.stcb = (void *)stcb; michael@0: sctp_clog.x.wake.wake_cnt = wake_cnt; michael@0: sctp_clog.x.wake.flight = stcb->asoc.total_flight_count; michael@0: sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt; michael@0: sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt; michael@0: michael@0: if (stcb->asoc.stream_queue_cnt < 0xff) michael@0: sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt; michael@0: else michael@0: sctp_clog.x.wake.stream_qcnt = 0xff; michael@0: michael@0: if (stcb->asoc.chunks_on_out_queue < 0xff) michael@0: sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue; michael@0: else michael@0: sctp_clog.x.wake.chunks_on_oque = 0xff; michael@0: michael@0: sctp_clog.x.wake.sctpflags = 0; michael@0: /* set in the defered mode stuff */ michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) michael@0: sctp_clog.x.wake.sctpflags |= 1; michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) michael@0: sctp_clog.x.wake.sctpflags |= 2; michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) michael@0: sctp_clog.x.wake.sctpflags |= 4; michael@0: /* what about the sb */ michael@0: if (stcb->sctp_socket) { michael@0: struct socket *so = stcb->sctp_socket; michael@0: michael@0: sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff)); michael@0: } else { michael@0: sctp_clog.x.wake.sbflags = 0xff; michael@0: } michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_WAKE, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_log_block(uint8_t from, struct sctp_association *asoc, int sendlen) michael@0: { michael@0: #if defined(__FreeBSD__) || defined(SCTP_LOCAL_TRACE_BUF) michael@0: struct sctp_cwnd_log sctp_clog; michael@0: michael@0: sctp_clog.x.blk.onsb = asoc->total_output_queue_size; michael@0: sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt); michael@0: sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd; michael@0: sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt; michael@0: sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue; michael@0: sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight/1024); michael@0: sctp_clog.x.blk.sndlen = sendlen; michael@0: SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", michael@0: SCTP_LOG_EVENT_BLOCK, michael@0: from, michael@0: sctp_clog.x.misc.log1, michael@0: sctp_clog.x.misc.log2, michael@0: sctp_clog.x.misc.log3, michael@0: sctp_clog.x.misc.log4); michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED) michael@0: { michael@0: /* May need to fix this if ktrdump does not work */ michael@0: return (0); michael@0: } michael@0: michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2]; michael@0: static int sctp_audit_indx = 0; michael@0: michael@0: static michael@0: void michael@0: sctp_print_audit_report(void) michael@0: { michael@0: int i; michael@0: int cnt; michael@0: michael@0: cnt = 0; michael@0: for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) { michael@0: if ((sctp_audit_data[i][0] == 0xe0) && michael@0: (sctp_audit_data[i][1] == 0x01)) { michael@0: cnt = 0; michael@0: SCTP_PRINTF("\n"); michael@0: } else if (sctp_audit_data[i][0] == 0xf0) { michael@0: cnt = 0; michael@0: SCTP_PRINTF("\n"); michael@0: } else if ((sctp_audit_data[i][0] == 0xc0) && michael@0: (sctp_audit_data[i][1] == 0x01)) { michael@0: SCTP_PRINTF("\n"); michael@0: cnt = 0; michael@0: } michael@0: SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0], michael@0: (uint32_t) sctp_audit_data[i][1]); michael@0: cnt++; michael@0: if ((cnt % 14) == 0) michael@0: SCTP_PRINTF("\n"); michael@0: } michael@0: for (i = 0; i < sctp_audit_indx; i++) { michael@0: if ((sctp_audit_data[i][0] == 0xe0) && michael@0: (sctp_audit_data[i][1] == 0x01)) { michael@0: cnt = 0; michael@0: SCTP_PRINTF("\n"); michael@0: } else if (sctp_audit_data[i][0] == 0xf0) { michael@0: cnt = 0; michael@0: SCTP_PRINTF("\n"); michael@0: } else if ((sctp_audit_data[i][0] == 0xc0) && michael@0: (sctp_audit_data[i][1] == 0x01)) { michael@0: SCTP_PRINTF("\n"); michael@0: cnt = 0; michael@0: } michael@0: SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0], michael@0: (uint32_t) sctp_audit_data[i][1]); michael@0: cnt++; michael@0: if ((cnt % 14) == 0) michael@0: SCTP_PRINTF("\n"); michael@0: } michael@0: SCTP_PRINTF("\n"); michael@0: } michael@0: michael@0: void michael@0: sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: struct sctp_nets *net) michael@0: { michael@0: int resend_cnt, tot_out, rep, tot_book_cnt; michael@0: struct sctp_nets *lnet; michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xAA; michael@0: sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: if (inp == NULL) { michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xAF; michael@0: sctp_audit_data[sctp_audit_indx][1] = 0x01; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: return; michael@0: } michael@0: if (stcb == NULL) { michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xAF; michael@0: sctp_audit_data[sctp_audit_indx][1] = 0x02; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: return; michael@0: } michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xA1; michael@0: sctp_audit_data[sctp_audit_indx][1] = michael@0: (0x000000ff & stcb->asoc.sent_queue_retran_cnt); michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: rep = 0; michael@0: tot_book_cnt = 0; michael@0: resend_cnt = tot_out = 0; michael@0: TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { michael@0: if (chk->sent == SCTP_DATAGRAM_RESEND) { michael@0: resend_cnt++; michael@0: } else if (chk->sent < SCTP_DATAGRAM_RESEND) { michael@0: tot_out += chk->book_size; michael@0: tot_book_cnt++; michael@0: } michael@0: } michael@0: if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) { michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xAF; michael@0: sctp_audit_data[sctp_audit_indx][1] = 0xA1; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n", michael@0: resend_cnt, stcb->asoc.sent_queue_retran_cnt); michael@0: rep = 1; michael@0: stcb->asoc.sent_queue_retran_cnt = resend_cnt; michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xA2; michael@0: sctp_audit_data[sctp_audit_indx][1] = michael@0: (0x000000ff & stcb->asoc.sent_queue_retran_cnt); michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: } michael@0: if (tot_out != stcb->asoc.total_flight) { michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xAF; michael@0: sctp_audit_data[sctp_audit_indx][1] = 0xA2; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: rep = 1; michael@0: SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out, michael@0: (int)stcb->asoc.total_flight); michael@0: stcb->asoc.total_flight = tot_out; michael@0: } michael@0: if (tot_book_cnt != stcb->asoc.total_flight_count) { michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xAF; michael@0: sctp_audit_data[sctp_audit_indx][1] = 0xA5; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: rep = 1; michael@0: SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt); michael@0: michael@0: stcb->asoc.total_flight_count = tot_book_cnt; michael@0: } michael@0: tot_out = 0; michael@0: TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) { michael@0: tot_out += lnet->flight_size; michael@0: } michael@0: if (tot_out != stcb->asoc.total_flight) { michael@0: sctp_audit_data[sctp_audit_indx][0] = 0xAF; michael@0: sctp_audit_data[sctp_audit_indx][1] = 0xA3; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: rep = 1; michael@0: SCTP_PRINTF("real flight:%d net total was %d\n", michael@0: stcb->asoc.total_flight, tot_out); michael@0: /* now corrective action */ michael@0: TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) { michael@0: michael@0: tot_out = 0; michael@0: TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { michael@0: if ((chk->whoTo == lnet) && michael@0: (chk->sent < SCTP_DATAGRAM_RESEND)) { michael@0: tot_out += chk->book_size; michael@0: } michael@0: } michael@0: if (lnet->flight_size != tot_out) { michael@0: SCTP_PRINTF("net:%p flight was %d corrected to %d\n", michael@0: (void *)lnet, lnet->flight_size, michael@0: tot_out); michael@0: lnet->flight_size = tot_out; michael@0: } michael@0: } michael@0: } michael@0: if (rep) { michael@0: sctp_print_audit_report(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_audit_log(uint8_t ev, uint8_t fd) michael@0: { michael@0: michael@0: sctp_audit_data[sctp_audit_indx][0] = ev; michael@0: sctp_audit_data[sctp_audit_indx][1] = fd; michael@0: sctp_audit_indx++; michael@0: if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { michael@0: sctp_audit_indx = 0; michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /* michael@0: * sctp_stop_timers_for_shutdown() should be called michael@0: * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT michael@0: * state to make sure that all timers are stopped. michael@0: */ michael@0: void michael@0: sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_nets *net; michael@0: michael@0: asoc = &stcb->asoc; michael@0: michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); michael@0: TAILQ_FOREACH(net, &asoc->nets, sctp_next) { michael@0: (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); michael@0: (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * a list of sizes based on typical mtu's, used only if next hop size not michael@0: * returned. michael@0: */ michael@0: static uint32_t sctp_mtu_sizes[] = { michael@0: 68, michael@0: 296, michael@0: 508, michael@0: 512, michael@0: 544, michael@0: 576, michael@0: 1006, michael@0: 1492, michael@0: 1500, michael@0: 1536, michael@0: 2002, michael@0: 2048, michael@0: 4352, michael@0: 4464, michael@0: 8166, michael@0: 17914, michael@0: 32000, michael@0: 65535 michael@0: }; michael@0: michael@0: /* michael@0: * Return the largest MTU smaller than val. If there is no michael@0: * entry, just return val. michael@0: */ michael@0: uint32_t michael@0: sctp_get_prev_mtu(uint32_t val) michael@0: { michael@0: uint32_t i; michael@0: michael@0: if (val <= sctp_mtu_sizes[0]) { michael@0: return (val); michael@0: } michael@0: for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) { michael@0: if (val <= sctp_mtu_sizes[i]) { michael@0: break; michael@0: } michael@0: } michael@0: return (sctp_mtu_sizes[i - 1]); michael@0: } michael@0: michael@0: /* michael@0: * Return the smallest MTU larger than val. If there is no michael@0: * entry, just return val. michael@0: */ michael@0: uint32_t michael@0: sctp_get_next_mtu(uint32_t val) michael@0: { michael@0: /* select another MTU that is just bigger than this one */ michael@0: uint32_t i; michael@0: michael@0: for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) { michael@0: if (val < sctp_mtu_sizes[i]) { michael@0: return (sctp_mtu_sizes[i]); michael@0: } michael@0: } michael@0: return (val); michael@0: } michael@0: michael@0: void michael@0: sctp_fill_random_store(struct sctp_pcb *m) michael@0: { michael@0: /* michael@0: * Here we use the MD5/SHA-1 to hash with our good randomNumbers and michael@0: * our counter. The result becomes our good random numbers and we michael@0: * then setup to give these out. Note that we do no locking to michael@0: * protect this. This is ok, since if competing folks call this we michael@0: * will get more gobbled gook in the random store which is what we michael@0: * want. There is a danger that two guys will use the same random michael@0: * numbers, but thats ok too since that is random as well :-> michael@0: */ michael@0: m->store_at = 0; michael@0: (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers, michael@0: sizeof(m->random_numbers), (uint8_t *)&m->random_counter, michael@0: sizeof(m->random_counter), (uint8_t *)m->random_store); michael@0: m->random_counter++; michael@0: } michael@0: michael@0: uint32_t michael@0: sctp_select_initial_TSN(struct sctp_pcb *inp) michael@0: { michael@0: /* michael@0: * A true implementation should use random selection process to get michael@0: * the initial stream sequence number, using RFC1750 as a good michael@0: * guideline michael@0: */ michael@0: uint32_t x, *xp; michael@0: uint8_t *p; michael@0: int store_at, new_store; michael@0: michael@0: if (inp->initial_sequence_debug != 0) { michael@0: uint32_t ret; michael@0: michael@0: ret = inp->initial_sequence_debug; michael@0: inp->initial_sequence_debug++; michael@0: return (ret); michael@0: } michael@0: retry: michael@0: store_at = inp->store_at; michael@0: new_store = store_at + sizeof(uint32_t); michael@0: if (new_store >= (SCTP_SIGNATURE_SIZE-3)) { michael@0: new_store = 0; michael@0: } michael@0: if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) { michael@0: goto retry; michael@0: } michael@0: if (new_store == 0) { michael@0: /* Refill the random store */ michael@0: sctp_fill_random_store(inp); michael@0: } michael@0: p = &inp->random_store[store_at]; michael@0: xp = (uint32_t *)p; michael@0: x = *xp; michael@0: return (x); michael@0: } michael@0: michael@0: uint32_t michael@0: sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check) michael@0: { michael@0: uint32_t x; michael@0: struct timeval now; michael@0: michael@0: if (check) { michael@0: (void)SCTP_GETTIME_TIMEVAL(&now); michael@0: } michael@0: for (;;) { michael@0: x = sctp_select_initial_TSN(&inp->sctp_ep); michael@0: if (x == 0) { michael@0: /* we never use 0 */ michael@0: continue; michael@0: } michael@0: if (!check || sctp_is_vtag_good(x, lport, rport, &now)) { michael@0: break; michael@0: } michael@0: } michael@0: return (x); michael@0: } michael@0: michael@0: int michael@0: sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: uint32_t override_tag, uint32_t vrf_id) michael@0: { michael@0: struct sctp_association *asoc; michael@0: /* michael@0: * Anything set to zero is taken care of by the allocation routine's michael@0: * bzero michael@0: */ michael@0: michael@0: /* michael@0: * Up front select what scoping to apply on addresses I tell my peer michael@0: * Not sure what to do with these right now, we will need to come up michael@0: * with a way to set them. We may need to pass them through from the michael@0: * caller in the sctp_aloc_assoc() function. michael@0: */ michael@0: int i; michael@0: michael@0: asoc = &stcb->asoc; michael@0: /* init all variables to a known value. */ michael@0: SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE); michael@0: asoc->max_burst = inp->sctp_ep.max_burst; michael@0: asoc->fr_max_burst = inp->sctp_ep.fr_max_burst; michael@0: asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]); michael@0: asoc->cookie_life = inp->sctp_ep.def_cookie_life; michael@0: asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off; michael@0: asoc->ecn_allowed = inp->sctp_ecn_enable; michael@0: asoc->sctp_nr_sack_on_off = (uint8_t)SCTP_BASE_SYSCTL(sctp_nr_sack_on_off); michael@0: asoc->sctp_cmt_pf = (uint8_t)0; michael@0: asoc->sctp_frag_point = inp->sctp_frag_point; michael@0: asoc->sctp_features = inp->sctp_features; michael@0: asoc->default_dscp = inp->sctp_ep.default_dscp; michael@0: #ifdef INET6 michael@0: if (inp->sctp_ep.default_flowlabel) { michael@0: asoc->default_flowlabel = inp->sctp_ep.default_flowlabel; michael@0: } else { michael@0: if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) { michael@0: asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep); michael@0: asoc->default_flowlabel &= 0x000fffff; michael@0: asoc->default_flowlabel |= 0x80000000; michael@0: } else { michael@0: asoc->default_flowlabel = 0; michael@0: } michael@0: } michael@0: #endif michael@0: asoc->sb_send_resv = 0; michael@0: if (override_tag) { michael@0: asoc->my_vtag = override_tag; michael@0: } else { michael@0: asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1); michael@0: } michael@0: /* Get the nonce tags */ michael@0: asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0); michael@0: asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0); michael@0: asoc->vrf_id = vrf_id; michael@0: michael@0: #ifdef SCTP_ASOCLOG_OF_TSNS michael@0: asoc->tsn_in_at = 0; michael@0: asoc->tsn_out_at = 0; michael@0: asoc->tsn_in_wrapped = 0; michael@0: asoc->tsn_out_wrapped = 0; michael@0: asoc->cumack_log_at = 0; michael@0: asoc->cumack_log_atsnt = 0; michael@0: #endif michael@0: #ifdef SCTP_FS_SPEC_LOG michael@0: asoc->fs_index = 0; michael@0: #endif michael@0: asoc->refcnt = 0; michael@0: asoc->assoc_up_sent = 0; michael@0: asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq = michael@0: sctp_select_initial_TSN(&inp->sctp_ep); michael@0: asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; michael@0: /* we are optimisitic here */ michael@0: asoc->peer_supports_pktdrop = 1; michael@0: asoc->peer_supports_nat = 0; michael@0: asoc->sent_queue_retran_cnt = 0; michael@0: michael@0: /* for CMT */ michael@0: asoc->last_net_cmt_send_started = NULL; michael@0: michael@0: /* This will need to be adjusted */ michael@0: asoc->last_acked_seq = asoc->init_seq_number - 1; michael@0: asoc->advanced_peer_ack_point = asoc->last_acked_seq; michael@0: asoc->asconf_seq_in = asoc->last_acked_seq; michael@0: michael@0: /* here we are different, we hold the next one we expect */ michael@0: asoc->str_reset_seq_in = asoc->last_acked_seq + 1; michael@0: michael@0: asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max; michael@0: asoc->initial_rto = inp->sctp_ep.initial_rto; michael@0: michael@0: asoc->max_init_times = inp->sctp_ep.max_init_times; michael@0: asoc->max_send_times = inp->sctp_ep.max_send_times; michael@0: asoc->def_net_failure = inp->sctp_ep.def_net_failure; michael@0: asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold; michael@0: asoc->free_chunk_cnt = 0; michael@0: michael@0: asoc->iam_blocking = 0; michael@0: asoc->context = inp->sctp_context; michael@0: asoc->local_strreset_support = inp->local_strreset_support; michael@0: asoc->def_send = inp->def_send; michael@0: asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]); michael@0: asoc->sack_freq = inp->sctp_ep.sctp_sack_freq; michael@0: asoc->pr_sctp_cnt = 0; michael@0: asoc->total_output_queue_size = 0; michael@0: michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: asoc->scope.ipv6_addr_legal = 1; michael@0: if (SCTP_IPV6_V6ONLY(inp) == 0) { michael@0: asoc->scope.ipv4_addr_legal = 1; michael@0: } else { michael@0: asoc->scope.ipv4_addr_legal = 0; michael@0: } michael@0: #if defined(__Userspace__) michael@0: asoc->scope.conn_addr_legal = 0; michael@0: #endif michael@0: } else { michael@0: asoc->scope.ipv6_addr_legal = 0; michael@0: #if defined(__Userspace__) michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) { michael@0: asoc->scope.conn_addr_legal = 1; michael@0: asoc->scope.ipv4_addr_legal = 0; michael@0: } else { michael@0: asoc->scope.conn_addr_legal = 0; michael@0: asoc->scope.ipv4_addr_legal = 1; michael@0: } michael@0: #else michael@0: asoc->scope.ipv4_addr_legal = 1; michael@0: #endif michael@0: } michael@0: michael@0: asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND); michael@0: asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket); michael@0: michael@0: asoc->smallest_mtu = inp->sctp_frag_point; michael@0: asoc->minrto = inp->sctp_ep.sctp_minrto; michael@0: asoc->maxrto = inp->sctp_ep.sctp_maxrto; michael@0: michael@0: asoc->locked_on_sending = NULL; michael@0: asoc->stream_locked_on = 0; michael@0: asoc->ecn_echo_cnt_onq = 0; michael@0: asoc->stream_locked = 0; michael@0: michael@0: asoc->send_sack = 1; michael@0: michael@0: LIST_INIT(&asoc->sctp_restricted_addrs); michael@0: michael@0: TAILQ_INIT(&asoc->nets); michael@0: TAILQ_INIT(&asoc->pending_reply_queue); michael@0: TAILQ_INIT(&asoc->asconf_ack_sent); michael@0: /* Setup to fill the hb random cache at first HB */ michael@0: asoc->hb_random_idx = 4; michael@0: michael@0: asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time; michael@0: michael@0: stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module; michael@0: stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module]; michael@0: michael@0: stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module; michael@0: stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module]; michael@0: michael@0: /* michael@0: * Now the stream parameters, here we allocate space for all streams michael@0: * that we request by default. michael@0: */ michael@0: asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams = michael@0: inp->sctp_ep.pre_open_stream_count; michael@0: SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *, michael@0: asoc->streamoutcnt * sizeof(struct sctp_stream_out), michael@0: SCTP_M_STRMO); michael@0: if (asoc->strmout == NULL) { michael@0: /* big trouble no memory */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: for (i = 0; i < asoc->streamoutcnt; i++) { michael@0: /* michael@0: * inbound side must be set to 0xffff, also NOTE when we get michael@0: * the INIT-ACK back (for INIT sender) we MUST reduce the michael@0: * count (streamoutcnt) but first check if we sent to any of michael@0: * the upper streams that were dropped (if some were). Those michael@0: * that were dropped must be notified to the upper layer as michael@0: * failed to send. michael@0: */ michael@0: asoc->strmout[i].next_sequence_send = 0x0; michael@0: TAILQ_INIT(&asoc->strmout[i].outqueue); michael@0: asoc->strmout[i].chunks_on_queues = 0; michael@0: asoc->strmout[i].stream_no = i; michael@0: asoc->strmout[i].last_msg_incomplete = 0; michael@0: asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL); michael@0: } michael@0: asoc->ss_functions.sctp_ss_init(stcb, asoc, 0); michael@0: michael@0: /* Now the mapping array */ michael@0: asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY; michael@0: SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size, michael@0: SCTP_M_MAP); michael@0: if (asoc->mapping_array == NULL) { michael@0: SCTP_FREE(asoc->strmout, SCTP_M_STRMO); michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: memset(asoc->mapping_array, 0, asoc->mapping_array_size); michael@0: SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size, michael@0: SCTP_M_MAP); michael@0: if (asoc->nr_mapping_array == NULL) { michael@0: SCTP_FREE(asoc->strmout, SCTP_M_STRMO); michael@0: SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); michael@0: michael@0: /* Now the init of the other outqueues */ michael@0: TAILQ_INIT(&asoc->free_chunks); michael@0: TAILQ_INIT(&asoc->control_send_queue); michael@0: TAILQ_INIT(&asoc->asconf_send_queue); michael@0: TAILQ_INIT(&asoc->send_queue); michael@0: TAILQ_INIT(&asoc->sent_queue); michael@0: TAILQ_INIT(&asoc->reasmqueue); michael@0: TAILQ_INIT(&asoc->resetHead); michael@0: asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome; michael@0: TAILQ_INIT(&asoc->asconf_queue); michael@0: /* authentication fields */ michael@0: asoc->authinfo.random = NULL; michael@0: asoc->authinfo.active_keyid = 0; michael@0: asoc->authinfo.assoc_key = NULL; michael@0: asoc->authinfo.assoc_keyid = 0; michael@0: asoc->authinfo.recv_key = NULL; michael@0: asoc->authinfo.recv_keyid = 0; michael@0: LIST_INIT(&asoc->shared_keys); michael@0: asoc->marked_retrans = 0; michael@0: asoc->port = inp->sctp_ep.port; michael@0: asoc->timoinit = 0; michael@0: asoc->timodata = 0; michael@0: asoc->timosack = 0; michael@0: asoc->timoshutdown = 0; michael@0: asoc->timoheartbeat = 0; michael@0: asoc->timocookie = 0; michael@0: asoc->timoshutdownack = 0; michael@0: (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time); michael@0: asoc->discontinuity_time = asoc->start_time; michael@0: /* sa_ignore MEMLEAK {memory is put in the assoc mapping array and freed later when michael@0: * the association is freed. michael@0: */ michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: sctp_print_mapping_array(struct sctp_association *asoc) michael@0: { michael@0: unsigned int i, limit; michael@0: michael@0: SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n", michael@0: asoc->mapping_array_size, michael@0: asoc->mapping_array_base_tsn, michael@0: asoc->cumulative_tsn, michael@0: asoc->highest_tsn_inside_map, michael@0: asoc->highest_tsn_inside_nr_map); michael@0: for (limit = asoc->mapping_array_size; limit > 1; limit--) { michael@0: if (asoc->mapping_array[limit - 1] != 0) { michael@0: break; michael@0: } michael@0: } michael@0: SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit); michael@0: for (i = 0; i < limit; i++) { michael@0: SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n'); michael@0: } michael@0: if (limit % 16) michael@0: SCTP_PRINTF("\n"); michael@0: for (limit = asoc->mapping_array_size; limit > 1; limit--) { michael@0: if (asoc->nr_mapping_array[limit - 1]) { michael@0: break; michael@0: } michael@0: } michael@0: SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit); michael@0: for (i = 0; i < limit; i++) { michael@0: SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ': '\n'); michael@0: } michael@0: if (limit % 16) michael@0: SCTP_PRINTF("\n"); michael@0: } michael@0: michael@0: int michael@0: sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed) michael@0: { michael@0: /* mapping array needs to grow */ michael@0: uint8_t *new_array1, *new_array2; michael@0: uint32_t new_size; michael@0: michael@0: new_size = asoc->mapping_array_size + ((needed+7)/8 + SCTP_MAPPING_ARRAY_INCR); michael@0: SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP); michael@0: SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP); michael@0: if ((new_array1 == NULL) || (new_array2 == NULL)) { michael@0: /* can't get more, forget it */ michael@0: SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size); michael@0: if (new_array1) { michael@0: SCTP_FREE(new_array1, SCTP_M_MAP); michael@0: } michael@0: if (new_array2) { michael@0: SCTP_FREE(new_array2, SCTP_M_MAP); michael@0: } michael@0: return (-1); michael@0: } michael@0: memset(new_array1, 0, new_size); michael@0: memset(new_array2, 0, new_size); michael@0: memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size); michael@0: memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size); michael@0: SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); michael@0: SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); michael@0: asoc->mapping_array = new_array1; michael@0: asoc->nr_mapping_array = new_array2; michael@0: asoc->mapping_array_size = new_size; michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_iterator_work(struct sctp_iterator *it) michael@0: { michael@0: int iteration_count = 0; michael@0: int inp_skip = 0; michael@0: int first_in = 1; michael@0: struct sctp_inpcb *tinp; michael@0: michael@0: SCTP_INP_INFO_RLOCK(); michael@0: SCTP_ITERATOR_LOCK(); michael@0: if (it->inp) { michael@0: SCTP_INP_RLOCK(it->inp); michael@0: SCTP_INP_DECR_REF(it->inp); michael@0: } michael@0: if (it->inp == NULL) { michael@0: /* iterator is complete */ michael@0: done_with_iterator: michael@0: SCTP_ITERATOR_UNLOCK(); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: if (it->function_atend != NULL) { michael@0: (*it->function_atend) (it->pointer, it->val); michael@0: } michael@0: SCTP_FREE(it, SCTP_M_ITER); michael@0: return; michael@0: } michael@0: select_a_new_ep: michael@0: if (first_in) { michael@0: first_in = 0; michael@0: } else { michael@0: SCTP_INP_RLOCK(it->inp); michael@0: } michael@0: while (((it->pcb_flags) && michael@0: ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) || michael@0: ((it->pcb_features) && michael@0: ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) { michael@0: /* endpoint flags or features don't match, so keep looking */ michael@0: if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { michael@0: SCTP_INP_RUNLOCK(it->inp); michael@0: goto done_with_iterator; michael@0: } michael@0: tinp = it->inp; michael@0: it->inp = LIST_NEXT(it->inp, sctp_list); michael@0: SCTP_INP_RUNLOCK(tinp); michael@0: if (it->inp == NULL) { michael@0: goto done_with_iterator; michael@0: } michael@0: SCTP_INP_RLOCK(it->inp); michael@0: } michael@0: /* now go through each assoc which is in the desired state */ michael@0: if (it->done_current_ep == 0) { michael@0: if (it->function_inp != NULL) michael@0: inp_skip = (*it->function_inp)(it->inp, it->pointer, it->val); michael@0: it->done_current_ep = 1; michael@0: } michael@0: if (it->stcb == NULL) { michael@0: /* run the per instance function */ michael@0: it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list); michael@0: } michael@0: if ((inp_skip) || it->stcb == NULL) { michael@0: if (it->function_inp_end != NULL) { michael@0: inp_skip = (*it->function_inp_end)(it->inp, michael@0: it->pointer, michael@0: it->val); michael@0: } michael@0: SCTP_INP_RUNLOCK(it->inp); michael@0: goto no_stcb; michael@0: } michael@0: while (it->stcb) { michael@0: SCTP_TCB_LOCK(it->stcb); michael@0: if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) { michael@0: /* not in the right state... keep looking */ michael@0: SCTP_TCB_UNLOCK(it->stcb); michael@0: goto next_assoc; michael@0: } michael@0: /* see if we have limited out the iterator loop */ michael@0: iteration_count++; michael@0: if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) { michael@0: /* Pause to let others grab the lock */ michael@0: atomic_add_int(&it->stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(it->stcb); michael@0: SCTP_INP_INCR_REF(it->inp); michael@0: SCTP_INP_RUNLOCK(it->inp); michael@0: SCTP_ITERATOR_UNLOCK(); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: SCTP_INP_INFO_RLOCK(); michael@0: SCTP_ITERATOR_LOCK(); michael@0: if (sctp_it_ctl.iterator_flags) { michael@0: /* We won't be staying here */ michael@0: SCTP_INP_DECR_REF(it->inp); michael@0: atomic_add_int(&it->stcb->asoc.refcnt, -1); michael@0: #if !defined(__FreeBSD__) michael@0: if (sctp_it_ctl.iterator_flags & michael@0: SCTP_ITERATOR_MUST_EXIT) { michael@0: goto done_with_iterator; michael@0: } michael@0: #endif michael@0: if (sctp_it_ctl.iterator_flags & michael@0: SCTP_ITERATOR_STOP_CUR_IT) { michael@0: sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT; michael@0: goto done_with_iterator; michael@0: } michael@0: if (sctp_it_ctl.iterator_flags & michael@0: SCTP_ITERATOR_STOP_CUR_INP) { michael@0: sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP; michael@0: goto no_stcb; michael@0: } michael@0: /* If we reach here huh? */ michael@0: SCTP_PRINTF("Unknown it ctl flag %x\n", michael@0: sctp_it_ctl.iterator_flags); michael@0: sctp_it_ctl.iterator_flags = 0; michael@0: } michael@0: SCTP_INP_RLOCK(it->inp); michael@0: SCTP_INP_DECR_REF(it->inp); michael@0: SCTP_TCB_LOCK(it->stcb); michael@0: atomic_add_int(&it->stcb->asoc.refcnt, -1); michael@0: iteration_count = 0; michael@0: } michael@0: michael@0: /* run function on this one */ michael@0: (*it->function_assoc)(it->inp, it->stcb, it->pointer, it->val); michael@0: michael@0: /* michael@0: * we lie here, it really needs to have its own type but michael@0: * first I must verify that this won't effect things :-0 michael@0: */ michael@0: if (it->no_chunk_output == 0) michael@0: sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED); michael@0: michael@0: SCTP_TCB_UNLOCK(it->stcb); michael@0: next_assoc: michael@0: it->stcb = LIST_NEXT(it->stcb, sctp_tcblist); michael@0: if (it->stcb == NULL) { michael@0: /* Run last function */ michael@0: if (it->function_inp_end != NULL) { michael@0: inp_skip = (*it->function_inp_end)(it->inp, michael@0: it->pointer, michael@0: it->val); michael@0: } michael@0: } michael@0: } michael@0: SCTP_INP_RUNLOCK(it->inp); michael@0: no_stcb: michael@0: /* done with all assocs on this endpoint, move on to next endpoint */ michael@0: it->done_current_ep = 0; michael@0: if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { michael@0: it->inp = NULL; michael@0: } else { michael@0: it->inp = LIST_NEXT(it->inp, sctp_list); michael@0: } michael@0: if (it->inp == NULL) { michael@0: goto done_with_iterator; michael@0: } michael@0: goto select_a_new_ep; michael@0: } michael@0: michael@0: void michael@0: sctp_iterator_worker(void) michael@0: { michael@0: struct sctp_iterator *it, *nit; michael@0: michael@0: /* This function is called with the WQ lock in place */ michael@0: michael@0: sctp_it_ctl.iterator_running = 1; michael@0: TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { michael@0: sctp_it_ctl.cur_it = it; michael@0: /* now lets work on this one */ michael@0: TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); michael@0: SCTP_IPI_ITERATOR_WQ_UNLOCK(); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_SET(it->vn); michael@0: #endif michael@0: sctp_iterator_work(it); michael@0: sctp_it_ctl.cur_it = NULL; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: SCTP_IPI_ITERATOR_WQ_LOCK(); michael@0: #if !defined(__FreeBSD__) michael@0: if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) { michael@0: break; michael@0: } michael@0: #endif michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: sctp_it_ctl.iterator_running = 0; michael@0: return; michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_handle_addr_wq(void) michael@0: { michael@0: /* deal with the ADDR wq from the rtsock calls */ michael@0: struct sctp_laddr *wi, *nwi; michael@0: struct sctp_asconf_iterator *asc; michael@0: michael@0: SCTP_MALLOC(asc, struct sctp_asconf_iterator *, michael@0: sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT); michael@0: if (asc == NULL) { michael@0: /* Try later, no memory */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, michael@0: (struct sctp_inpcb *)NULL, michael@0: (struct sctp_tcb *)NULL, michael@0: (struct sctp_nets *)NULL); michael@0: return; michael@0: } michael@0: LIST_INIT(&asc->list_of_work); michael@0: asc->cnt = 0; michael@0: michael@0: SCTP_WQ_ADDR_LOCK(); michael@0: LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { michael@0: LIST_REMOVE(wi, sctp_nxt_addr); michael@0: LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr); michael@0: asc->cnt++; michael@0: } michael@0: SCTP_WQ_ADDR_UNLOCK(); michael@0: michael@0: if (asc->cnt == 0) { michael@0: SCTP_FREE(asc, SCTP_M_ASC_IT); michael@0: } else { michael@0: (void)sctp_initiate_iterator(sctp_asconf_iterator_ep, michael@0: sctp_asconf_iterator_stcb, michael@0: NULL, /* No ep end for boundall */ michael@0: SCTP_PCB_FLAGS_BOUNDALL, michael@0: SCTP_PCB_ANY_FEATURES, michael@0: SCTP_ASOC_ANY_STATE, michael@0: (void *)asc, 0, michael@0: sctp_asconf_iterator_end, NULL, 0); michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_timeout_handler(void *t) michael@0: { michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb; michael@0: struct sctp_nets *net; michael@0: struct sctp_timer *tmr; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: int did_output, type; michael@0: michael@0: tmr = (struct sctp_timer *)t; michael@0: inp = (struct sctp_inpcb *)tmr->ep; michael@0: stcb = (struct sctp_tcb *)tmr->tcb; michael@0: net = (struct sctp_nets *)tmr->net; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_SET((struct vnet *)tmr->vnet); michael@0: #endif michael@0: did_output = 1; michael@0: michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xF0, (uint8_t) tmr->type); michael@0: sctp_auditing(3, inp, stcb, net); michael@0: #endif michael@0: michael@0: /* sanity checks... */ michael@0: if (tmr->self != (void *)tmr) { michael@0: /* michael@0: * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n", michael@0: * (void *)tmr); michael@0: */ michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: return; michael@0: } michael@0: tmr->stopped_from = 0xa001; michael@0: if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) { michael@0: /* michael@0: * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n", michael@0: * tmr->type); michael@0: */ michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: return; michael@0: } michael@0: tmr->stopped_from = 0xa002; michael@0: if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) { michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: return; michael@0: } michael@0: /* if this is an iterator timeout, get the struct and clear inp */ michael@0: tmr->stopped_from = 0xa003; michael@0: type = tmr->type; michael@0: if (inp) { michael@0: SCTP_INP_INCR_REF(inp); michael@0: if ((inp->sctp_socket == NULL) && michael@0: ((tmr->type != SCTP_TIMER_TYPE_INPKILL) && michael@0: (tmr->type != SCTP_TIMER_TYPE_INIT) && michael@0: (tmr->type != SCTP_TIMER_TYPE_SEND) && michael@0: (tmr->type != SCTP_TIMER_TYPE_RECV) && michael@0: (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) && michael@0: (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) && michael@0: (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) && michael@0: (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) && michael@0: (tmr->type != SCTP_TIMER_TYPE_ASOCKILL)) michael@0: ) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: return; michael@0: } michael@0: } michael@0: tmr->stopped_from = 0xa004; michael@0: if (stcb) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->asoc.state == 0) { michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: if (inp) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: return; michael@0: } michael@0: } michael@0: tmr->stopped_from = 0xa005; michael@0: SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type); michael@0: if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) { michael@0: if (inp) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: if (stcb) { michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: return; michael@0: } michael@0: tmr->stopped_from = 0xa006; michael@0: michael@0: if (stcb) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) && michael@0: ((stcb->asoc.state == 0) || michael@0: (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: if (inp) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: return; michael@0: } michael@0: } michael@0: /* record in stopped what t-o occured */ michael@0: tmr->stopped_from = tmr->type; michael@0: michael@0: /* mark as being serviced now */ michael@0: if (SCTP_OS_TIMER_PENDING(&tmr->timer)) { michael@0: /* michael@0: * Callout has been rescheduled. michael@0: */ michael@0: goto get_out; michael@0: } michael@0: if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) { michael@0: /* michael@0: * Not active, so no action. michael@0: */ michael@0: goto get_out; michael@0: } michael@0: SCTP_OS_TIMER_DEACTIVATE(&tmr->timer); michael@0: michael@0: /* call the handler for the appropriate timer type */ michael@0: switch (tmr->type) { michael@0: case SCTP_TIMER_TYPE_ZERO_COPY: michael@0: if (inp == NULL) { michael@0: break; michael@0: } michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { michael@0: SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket); michael@0: } michael@0: break; michael@0: case SCTP_TIMER_TYPE_ZCOPY_SENDQ: michael@0: if (inp == NULL) { michael@0: break; michael@0: } michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { michael@0: SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket); michael@0: } michael@0: break; michael@0: case SCTP_TIMER_TYPE_ADDR_WQ: michael@0: sctp_handle_addr_wq(); michael@0: break; michael@0: case SCTP_TIMER_TYPE_SEND: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timodata); michael@0: stcb->asoc.timodata++; michael@0: stcb->asoc.num_send_timers_up--; michael@0: if (stcb->asoc.num_send_timers_up < 0) { michael@0: stcb->asoc.num_send_timers_up = 0; michael@0: } michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: if (sctp_t3rxt_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: michael@0: goto out_decr; michael@0: } michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(4, inp, stcb, net); michael@0: #endif michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED); michael@0: if ((stcb->asoc.num_send_timers_up == 0) && michael@0: (stcb->asoc.sent_queue_cnt > 0)) { michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: /* michael@0: * safeguard. If there on some on the sent queue michael@0: * somewhere but no timers running something is michael@0: * wrong... so we start a timer on the first chunk michael@0: * on the send queue on whatever net it is sent to. michael@0: */ michael@0: chk = TAILQ_FIRST(&stcb->asoc.sent_queue); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, michael@0: chk->whoTo); michael@0: } michael@0: break; michael@0: case SCTP_TIMER_TYPE_INIT: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoinit); michael@0: stcb->asoc.timoinit++; michael@0: if (sctp_t1init_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: } michael@0: /* We do output but not here */ michael@0: did_output = 0; michael@0: break; michael@0: case SCTP_TIMER_TYPE_RECV: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timosack); michael@0: stcb->asoc.timosack++; michael@0: sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(4, inp, stcb, net); michael@0: #endif michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED); michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWN: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: if (sctp_shutdown_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoshutdown); michael@0: stcb->asoc.timoshutdown++; michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(4, inp, stcb, net); michael@0: #endif michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED); michael@0: break; michael@0: case SCTP_TIMER_TYPE_HEARTBEAT: michael@0: if ((stcb == NULL) || (inp == NULL) || (net == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoheartbeat); michael@0: stcb->asoc.timoheartbeat++; michael@0: if (sctp_heartbeat_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(4, inp, stcb, net); michael@0: #endif michael@0: if (!(net->dest_state & SCTP_ADDR_NOHB)) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: break; michael@0: case SCTP_TIMER_TYPE_COOKIE: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: michael@0: if (sctp_cookie_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timocookie); michael@0: stcb->asoc.timocookie++; michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(4, inp, stcb, net); michael@0: #endif michael@0: /* michael@0: * We consider T3 and Cookie timer pretty much the same with michael@0: * respect to where from in chunk_output. michael@0: */ michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED); michael@0: break; michael@0: case SCTP_TIMER_TYPE_NEWCOOKIE: michael@0: { michael@0: struct timeval tv; michael@0: int i, secret; michael@0: if (inp == NULL) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timosecret); michael@0: (void)SCTP_GETTIME_TIMEVAL(&tv); michael@0: SCTP_INP_WLOCK(inp); michael@0: inp->sctp_ep.time_of_secret_change = tv.tv_sec; michael@0: inp->sctp_ep.last_secret_number = michael@0: inp->sctp_ep.current_secret_number; michael@0: inp->sctp_ep.current_secret_number++; michael@0: if (inp->sctp_ep.current_secret_number >= michael@0: SCTP_HOW_MANY_SECRETS) { michael@0: inp->sctp_ep.current_secret_number = 0; michael@0: } michael@0: secret = (int)inp->sctp_ep.current_secret_number; michael@0: for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { michael@0: inp->sctp_ep.secret_key[secret][i] = michael@0: sctp_select_initial_TSN(&inp->sctp_ep); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net); michael@0: } michael@0: did_output = 0; michael@0: break; michael@0: case SCTP_TIMER_TYPE_PATHMTURAISE: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timopathmtu); michael@0: sctp_pathmtu_timer(inp, stcb, net); michael@0: did_output = 0; michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWNACK: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: if (sctp_shutdownack_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoshutdownack); michael@0: stcb->asoc.timoshutdownack++; michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(4, inp, stcb, net); michael@0: #endif michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED); michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWNGUARD: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoshutdownguard); michael@0: sctp_abort_an_association(inp, stcb, NULL, SCTP_SO_NOT_LOCKED); michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: michael@0: case SCTP_TIMER_TYPE_STRRESET: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: if (sctp_strreset_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timostrmrst); michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED); michael@0: break; michael@0: case SCTP_TIMER_TYPE_ASCONF: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: if (sctp_asconf_timer(inp, stcb, net)) { michael@0: /* no need to unlock on tcb its gone */ michael@0: goto out_decr; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoasconf); michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_auditing(4, inp, stcb, net); michael@0: #endif michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED); michael@0: break; michael@0: case SCTP_TIMER_TYPE_PRIM_DELETED: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: sctp_delete_prim_timer(inp, stcb, net); michael@0: SCTP_STAT_INCR(sctps_timodelprim); michael@0: break; michael@0: michael@0: case SCTP_TIMER_TYPE_AUTOCLOSE: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoautoclose); michael@0: sctp_autoclose_timer(inp, stcb, net); michael@0: sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED); michael@0: did_output = 0; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ASOCKILL: michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: break; michael@0: } michael@0: SCTP_STAT_INCR(sctps_timoassockill); michael@0: /* Can we free it yet? */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL+SCTP_LOC_1); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(inp); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL+SCTP_LOC_2); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: /* michael@0: * free asoc, always unlocks (or destroy's) so prevent michael@0: * duplicate unlock or unlock of a free mtx :-0 michael@0: */ michael@0: stcb = NULL; michael@0: goto out_no_decr; michael@0: case SCTP_TIMER_TYPE_INPKILL: michael@0: SCTP_STAT_INCR(sctps_timoinpkill); michael@0: if (inp == NULL) { michael@0: break; michael@0: } michael@0: /* michael@0: * special case, take away our increment since WE are the michael@0: * killer michael@0: */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL+SCTP_LOC_3); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1); michael@0: #endif michael@0: sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, michael@0: SCTP_CALLED_FROM_INPKILL_TIMER); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1); michael@0: #endif michael@0: inp = NULL; michael@0: goto out_no_decr; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n", michael@0: tmr->type); michael@0: break; michael@0: } michael@0: #ifdef SCTP_AUDITING_ENABLED michael@0: sctp_audit_log(0xF1, (uint8_t) tmr->type); michael@0: if (inp) michael@0: sctp_auditing(5, inp, stcb, net); michael@0: #endif michael@0: if ((did_output) && stcb) { michael@0: /* michael@0: * Now we need to clean up the control chunk chain if an michael@0: * ECNE is on it. It must be marked as UNSENT again so next michael@0: * call will continue to send it until such time that we get michael@0: * a CWR, to remove it. It is, however, less likely that we michael@0: * will find a ecn echo on the chain though. michael@0: */ michael@0: sctp_fix_ecn_echo(&stcb->asoc); michael@0: } michael@0: get_out: michael@0: if (stcb) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: michael@0: out_decr: michael@0: if (inp) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: michael@0: out_no_decr: michael@0: SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n", michael@0: type); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: struct sctp_nets *net) michael@0: { michael@0: uint32_t to_ticks; michael@0: struct sctp_timer *tmr; michael@0: michael@0: if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) michael@0: return; michael@0: michael@0: tmr = NULL; michael@0: if (stcb) { michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: } michael@0: switch (t_type) { michael@0: case SCTP_TIMER_TYPE_ZERO_COPY: michael@0: tmr = &inp->sctp_ep.zero_copy_timer; michael@0: to_ticks = SCTP_ZERO_COPY_TICK_DELAY; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ZCOPY_SENDQ: michael@0: tmr = &inp->sctp_ep.zero_copy_sendq_timer; michael@0: to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ADDR_WQ: michael@0: /* Only 1 tick away :-) */ michael@0: tmr = &SCTP_BASE_INFO(addr_wq_timer); michael@0: to_ticks = SCTP_ADDRESS_TICK_DELAY; michael@0: break; michael@0: case SCTP_TIMER_TYPE_SEND: michael@0: /* Here we use the RTO timer */ michael@0: { michael@0: int rto_val; michael@0: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: if (net->RTO == 0) { michael@0: rto_val = stcb->asoc.initial_rto; michael@0: } else { michael@0: rto_val = net->RTO; michael@0: } michael@0: to_ticks = MSEC_TO_TICKS(rto_val); michael@0: } michael@0: break; michael@0: case SCTP_TIMER_TYPE_INIT: michael@0: /* michael@0: * Here we use the INIT timer default usually about 1 michael@0: * minute. michael@0: */ michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: if (net->RTO == 0) { michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); michael@0: } else { michael@0: to_ticks = MSEC_TO_TICKS(net->RTO); michael@0: } michael@0: break; michael@0: case SCTP_TIMER_TYPE_RECV: michael@0: /* michael@0: * Here we use the Delayed-Ack timer value from the inp michael@0: * ususually about 200ms. michael@0: */ michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.dack_timer; michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack); michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWN: michael@0: /* Here we use the RTO of the destination. */ michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: if (net->RTO == 0) { michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); michael@0: } else { michael@0: to_ticks = MSEC_TO_TICKS(net->RTO); michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_HEARTBEAT: michael@0: /* michael@0: * the net is used here so that we can add in the RTO. Even michael@0: * though we use a different timer. We also add the HB timer michael@0: * PLUS a random jitter. michael@0: */ michael@0: if ((inp == NULL) || (stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } else { michael@0: uint32_t rndval; michael@0: uint32_t jitter; michael@0: michael@0: if ((net->dest_state & SCTP_ADDR_NOHB) && michael@0: !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) { michael@0: return; michael@0: } michael@0: if (net->RTO == 0) { michael@0: to_ticks = stcb->asoc.initial_rto; michael@0: } else { michael@0: to_ticks = net->RTO; michael@0: } michael@0: rndval = sctp_select_initial_TSN(&inp->sctp_ep); michael@0: jitter = rndval % to_ticks; michael@0: if (jitter >= (to_ticks >> 1)) { michael@0: to_ticks = to_ticks + (jitter - (to_ticks >> 1)); michael@0: } else { michael@0: to_ticks = to_ticks - jitter; michael@0: } michael@0: if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) && michael@0: !(net->dest_state & SCTP_ADDR_PF)) { michael@0: to_ticks += net->heart_beat_delay; michael@0: } michael@0: /* michael@0: * Now we must convert the to_ticks that are now in michael@0: * ms to ticks. michael@0: */ michael@0: to_ticks = MSEC_TO_TICKS(to_ticks); michael@0: tmr = &net->hb_timer; michael@0: } michael@0: break; michael@0: case SCTP_TIMER_TYPE_COOKIE: michael@0: /* michael@0: * Here we can use the RTO timer from the network since one michael@0: * RTT was compelete. If a retran happened then we will be michael@0: * using the RTO initial value. michael@0: */ michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: if (net->RTO == 0) { michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); michael@0: } else { michael@0: to_ticks = MSEC_TO_TICKS(net->RTO); michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_NEWCOOKIE: michael@0: /* michael@0: * nothing needed but the endpoint here ususually about 60 michael@0: * minutes. michael@0: */ michael@0: if (inp == NULL) { michael@0: return; michael@0: } michael@0: tmr = &inp->sctp_ep.signature_change; michael@0: to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE]; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ASOCKILL: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.strreset_timer; michael@0: to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT); michael@0: break; michael@0: case SCTP_TIMER_TYPE_INPKILL: michael@0: /* michael@0: * The inp is setup to die. We re-use the signature_chage michael@0: * timer since that has stopped and we are in the GONE michael@0: * state. michael@0: */ michael@0: if (inp == NULL) { michael@0: return; michael@0: } michael@0: tmr = &inp->sctp_ep.signature_change; michael@0: to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT); michael@0: break; michael@0: case SCTP_TIMER_TYPE_PATHMTURAISE: michael@0: /* michael@0: * Here we use the value found in the EP for PMTU ususually michael@0: * about 10 minutes. michael@0: */ michael@0: if ((stcb == NULL) || (inp == NULL)) { michael@0: return; michael@0: } michael@0: if (net == NULL) { michael@0: return; michael@0: } michael@0: if (net->dest_state & SCTP_ADDR_NO_PMTUD) { michael@0: return; michael@0: } michael@0: to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU]; michael@0: tmr = &net->pmtu_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWNACK: michael@0: /* Here we use the RTO of the destination */ michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: if (net->RTO == 0) { michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); michael@0: } else { michael@0: to_ticks = MSEC_TO_TICKS(net->RTO); michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWNGUARD: michael@0: /* michael@0: * Here we use the endpoints shutdown guard timer usually michael@0: * about 3 minutes. michael@0: */ michael@0: if ((inp == NULL) || (stcb == NULL)) { michael@0: return; michael@0: } michael@0: to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN]; michael@0: tmr = &stcb->asoc.shut_guard_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_STRRESET: michael@0: /* michael@0: * Here the timer comes from the stcb but its value is from michael@0: * the net's RTO. michael@0: */ michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: if (net->RTO == 0) { michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); michael@0: } else { michael@0: to_ticks = MSEC_TO_TICKS(net->RTO); michael@0: } michael@0: tmr = &stcb->asoc.strreset_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ASCONF: michael@0: /* michael@0: * Here the timer comes from the stcb but its value is from michael@0: * the net's RTO. michael@0: */ michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: if (net->RTO == 0) { michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); michael@0: } else { michael@0: to_ticks = MSEC_TO_TICKS(net->RTO); michael@0: } michael@0: tmr = &stcb->asoc.asconf_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_PRIM_DELETED: michael@0: if ((stcb == NULL) || (net != NULL)) { michael@0: return; michael@0: } michael@0: to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); michael@0: tmr = &stcb->asoc.delete_prim_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_AUTOCLOSE: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: if (stcb->asoc.sctp_autoclose_ticks == 0) { michael@0: /* michael@0: * Really an error since stcb is NOT set to michael@0: * autoclose michael@0: */ michael@0: return; michael@0: } michael@0: to_ticks = stcb->asoc.sctp_autoclose_ticks; michael@0: tmr = &stcb->asoc.autoclose_timer; michael@0: break; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n", michael@0: __FUNCTION__, t_type); michael@0: return; michael@0: break; michael@0: } michael@0: if ((to_ticks <= 0) || (tmr == NULL)) { michael@0: SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n", michael@0: __FUNCTION__, t_type, to_ticks, (void *)tmr); michael@0: return; michael@0: } michael@0: if (SCTP_OS_TIMER_PENDING(&tmr->timer)) { michael@0: /* michael@0: * we do NOT allow you to have it already running. if it is michael@0: * we leave the current one up unchanged michael@0: */ michael@0: return; michael@0: } michael@0: /* At this point we can proceed */ michael@0: if (t_type == SCTP_TIMER_TYPE_SEND) { michael@0: stcb->asoc.num_send_timers_up++; michael@0: } michael@0: tmr->stopped_from = 0; michael@0: tmr->type = t_type; michael@0: tmr->ep = (void *)inp; michael@0: tmr->tcb = (void *)stcb; michael@0: tmr->net = (void *)net; michael@0: tmr->self = (void *)tmr; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: tmr->vnet = (void *)curvnet; michael@0: #endif michael@0: #ifndef __Panda__ michael@0: tmr->ticks = sctp_get_tick_count(); michael@0: #endif michael@0: (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: struct sctp_nets *net, uint32_t from) michael@0: { michael@0: struct sctp_timer *tmr; michael@0: michael@0: if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && michael@0: (inp == NULL)) michael@0: return; michael@0: michael@0: tmr = NULL; michael@0: if (stcb) { michael@0: SCTP_TCB_LOCK_ASSERT(stcb); michael@0: } michael@0: switch (t_type) { michael@0: case SCTP_TIMER_TYPE_ZERO_COPY: michael@0: tmr = &inp->sctp_ep.zero_copy_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ZCOPY_SENDQ: michael@0: tmr = &inp->sctp_ep.zero_copy_sendq_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ADDR_WQ: michael@0: tmr = &SCTP_BASE_INFO(addr_wq_timer); michael@0: break; michael@0: case SCTP_TIMER_TYPE_SEND: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_INIT: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_RECV: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.dack_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWN: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_HEARTBEAT: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->hb_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_COOKIE: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_NEWCOOKIE: michael@0: /* nothing needed but the endpoint here */ michael@0: tmr = &inp->sctp_ep.signature_change; michael@0: /* michael@0: * We re-use the newcookie timer for the INP kill timer. We michael@0: * must assure that we do not kill it by accident. michael@0: */ michael@0: break; michael@0: case SCTP_TIMER_TYPE_ASOCKILL: michael@0: /* michael@0: * Stop the asoc kill timer. michael@0: */ michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.strreset_timer; michael@0: break; michael@0: michael@0: case SCTP_TIMER_TYPE_INPKILL: michael@0: /* michael@0: * The inp is setup to die. We re-use the signature_chage michael@0: * timer since that has stopped and we are in the GONE michael@0: * state. michael@0: */ michael@0: tmr = &inp->sctp_ep.signature_change; michael@0: break; michael@0: case SCTP_TIMER_TYPE_PATHMTURAISE: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->pmtu_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWNACK: michael@0: if ((stcb == NULL) || (net == NULL)) { michael@0: return; michael@0: } michael@0: tmr = &net->rxt_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_SHUTDOWNGUARD: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.shut_guard_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_STRRESET: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.strreset_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_ASCONF: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.asconf_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_PRIM_DELETED: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.delete_prim_timer; michael@0: break; michael@0: case SCTP_TIMER_TYPE_AUTOCLOSE: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: tmr = &stcb->asoc.autoclose_timer; michael@0: break; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n", michael@0: __FUNCTION__, t_type); michael@0: break; michael@0: } michael@0: if (tmr == NULL) { michael@0: return; michael@0: } michael@0: if ((tmr->type != t_type) && tmr->type) { michael@0: /* michael@0: * Ok we have a timer that is under joint use. Cookie timer michael@0: * per chance with the SEND timer. We therefore are NOT michael@0: * running the timer that the caller wants stopped. So just michael@0: * return. michael@0: */ michael@0: return; michael@0: } michael@0: if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) { michael@0: stcb->asoc.num_send_timers_up--; michael@0: if (stcb->asoc.num_send_timers_up < 0) { michael@0: stcb->asoc.num_send_timers_up = 0; michael@0: } michael@0: } michael@0: tmr->self = NULL; michael@0: tmr->stopped_from = from; michael@0: (void)SCTP_OS_TIMER_STOP(&tmr->timer); michael@0: return; michael@0: } michael@0: michael@0: uint32_t michael@0: sctp_calculate_len(struct mbuf *m) michael@0: { michael@0: uint32_t tlen = 0; michael@0: struct mbuf *at; michael@0: michael@0: at = m; michael@0: while (at) { michael@0: tlen += SCTP_BUF_LEN(at); michael@0: at = SCTP_BUF_NEXT(at); michael@0: } michael@0: return (tlen); michael@0: } michael@0: michael@0: void michael@0: sctp_mtu_size_reset(struct sctp_inpcb *inp, michael@0: struct sctp_association *asoc, uint32_t mtu) michael@0: { michael@0: /* michael@0: * Reset the P-MTU size on this association, this involves changing michael@0: * the asoc MTU, going through ANY chunk+overhead larger than mtu to michael@0: * allow the DF flag to be cleared. michael@0: */ michael@0: struct sctp_tmit_chunk *chk; michael@0: unsigned int eff_mtu, ovh; michael@0: michael@0: asoc->smallest_mtu = mtu; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: ovh = SCTP_MIN_OVERHEAD; michael@0: } else { michael@0: ovh = SCTP_MIN_V4_OVERHEAD; michael@0: } michael@0: eff_mtu = mtu - ovh; michael@0: TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { michael@0: if (chk->send_size > eff_mtu) { michael@0: chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; michael@0: } michael@0: } michael@0: TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { michael@0: if (chk->send_size > eff_mtu) { michael@0: chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * given an association and starting time of the current RTT period return michael@0: * RTO in number of msecs net should point to the current network michael@0: */ michael@0: michael@0: uint32_t michael@0: sctp_calculate_rto(struct sctp_tcb *stcb, michael@0: struct sctp_association *asoc, michael@0: struct sctp_nets *net, michael@0: struct timeval *told, michael@0: int safe, int rtt_from_sack) michael@0: { michael@0: /*- michael@0: * given an association and the starting time of the current RTT michael@0: * period (in value1/value2) return RTO in number of msecs. michael@0: */ michael@0: int32_t rtt; /* RTT in ms */ michael@0: uint32_t new_rto; michael@0: int first_measure = 0; michael@0: struct timeval now, then, *old; michael@0: michael@0: /* Copy it out for sparc64 */ michael@0: if (safe == sctp_align_unsafe_makecopy) { michael@0: old = &then; michael@0: memcpy(&then, told, sizeof(struct timeval)); michael@0: } else if (safe == sctp_align_safe_nocopy) { michael@0: old = told; michael@0: } else { michael@0: /* error */ michael@0: SCTP_PRINTF("Huh, bad rto calc call\n"); michael@0: return (0); michael@0: } michael@0: /************************/ michael@0: /* 1. calculate new RTT */ michael@0: /************************/ michael@0: /* get the current time */ michael@0: if (stcb->asoc.use_precise_time) { michael@0: (void)SCTP_GETPTIME_TIMEVAL(&now); michael@0: } else { michael@0: (void)SCTP_GETTIME_TIMEVAL(&now); michael@0: } michael@0: timevalsub(&now, old); michael@0: /* store the current RTT in us */ michael@0: net->rtt = (uint64_t)1000000 * (uint64_t)now.tv_sec + michael@0: (uint64_t)now.tv_usec; michael@0: /* computer rtt in ms */ michael@0: rtt = net->rtt / 1000; michael@0: if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) { michael@0: /* Tell the CC module that a new update has just occurred from a sack */ michael@0: (*asoc->cc_functions.sctp_rtt_calculated)(stcb, net, &now); michael@0: } michael@0: /* Do we need to determine the lan? We do this only michael@0: * on sacks i.e. RTT being determined from data not michael@0: * non-data (HB/INIT->INITACK). michael@0: */ michael@0: if ((rtt_from_sack == SCTP_RTT_FROM_DATA) && michael@0: (net->lan_type == SCTP_LAN_UNKNOWN)) { michael@0: if (net->rtt > SCTP_LOCAL_LAN_RTT) { michael@0: net->lan_type = SCTP_LAN_INTERNET; michael@0: } else { michael@0: net->lan_type = SCTP_LAN_LOCAL; michael@0: } michael@0: } michael@0: michael@0: /***************************/ michael@0: /* 2. update RTTVAR & SRTT */ michael@0: /***************************/ michael@0: /*- michael@0: * Compute the scaled average lastsa and the michael@0: * scaled variance lastsv as described in van Jacobson michael@0: * Paper "Congestion Avoidance and Control", Annex A. michael@0: * michael@0: * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt michael@0: * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar michael@0: */ michael@0: if (net->RTO_measured) { michael@0: rtt -= (net->lastsa >> SCTP_RTT_SHIFT); michael@0: net->lastsa += rtt; michael@0: if (rtt < 0) { michael@0: rtt = -rtt; michael@0: } michael@0: rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT); michael@0: net->lastsv += rtt; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) { michael@0: rto_logging(net, SCTP_LOG_RTTVAR); michael@0: } michael@0: } else { michael@0: /* First RTO measurment */ michael@0: net->RTO_measured = 1; michael@0: first_measure = 1; michael@0: net->lastsa = rtt << SCTP_RTT_SHIFT; michael@0: net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) { michael@0: rto_logging(net, SCTP_LOG_INITIAL_RTT); michael@0: } michael@0: } michael@0: if (net->lastsv == 0) { michael@0: net->lastsv = SCTP_CLOCK_GRANULARITY; michael@0: } michael@0: new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv; michael@0: if ((new_rto > SCTP_SAT_NETWORK_MIN) && michael@0: (stcb->asoc.sat_network_lockout == 0)) { michael@0: stcb->asoc.sat_network = 1; michael@0: } else if ((!first_measure) && stcb->asoc.sat_network) { michael@0: stcb->asoc.sat_network = 0; michael@0: stcb->asoc.sat_network_lockout = 1; michael@0: } michael@0: /* bound it, per C6/C7 in Section 5.3.1 */ michael@0: if (new_rto < stcb->asoc.minrto) { michael@0: new_rto = stcb->asoc.minrto; michael@0: } michael@0: if (new_rto > stcb->asoc.maxrto) { michael@0: new_rto = stcb->asoc.maxrto; michael@0: } michael@0: /* we are now returning the RTO */ michael@0: return (new_rto); michael@0: } michael@0: michael@0: /* michael@0: * return a pointer to a contiguous piece of data from the given mbuf chain michael@0: * starting at 'off' for 'len' bytes. If the desired piece spans more than michael@0: * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size michael@0: * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain. michael@0: */ michael@0: caddr_t michael@0: sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr) michael@0: { michael@0: uint32_t count; michael@0: uint8_t *ptr; michael@0: michael@0: ptr = in_ptr; michael@0: if ((off < 0) || (len <= 0)) michael@0: return (NULL); michael@0: michael@0: /* find the desired start location */ michael@0: while ((m != NULL) && (off > 0)) { michael@0: if (off < SCTP_BUF_LEN(m)) michael@0: break; michael@0: off -= SCTP_BUF_LEN(m); michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: if (m == NULL) michael@0: return (NULL); michael@0: michael@0: /* is the current mbuf large enough (eg. contiguous)? */ michael@0: if ((SCTP_BUF_LEN(m) - off) >= len) { michael@0: return (mtod(m, caddr_t) + off); michael@0: } else { michael@0: /* else, it spans more than one mbuf, so save a temp copy... */ michael@0: while ((m != NULL) && (len > 0)) { michael@0: count = min(SCTP_BUF_LEN(m) - off, len); michael@0: bcopy(mtod(m, caddr_t) + off, ptr, count); michael@0: len -= count; michael@0: ptr += count; michael@0: off = 0; michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: if ((m == NULL) && (len > 0)) michael@0: return (NULL); michael@0: else michael@0: return ((caddr_t)in_ptr); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: struct sctp_paramhdr * michael@0: sctp_get_next_param(struct mbuf *m, michael@0: int offset, michael@0: struct sctp_paramhdr *pull, michael@0: int pull_limit) michael@0: { michael@0: /* This just provides a typed signature to Peter's Pull routine */ michael@0: return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit, michael@0: (uint8_t *) pull)); michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_add_pad_tombuf(struct mbuf *m, int padlen) michael@0: { michael@0: /* michael@0: * add padlen bytes of 0 filled padding to the end of the mbuf. If michael@0: * padlen is > 3 this routine will fail. michael@0: */ michael@0: uint8_t *dp; michael@0: int i; michael@0: michael@0: if (padlen > 3) { michael@0: SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); michael@0: return (ENOBUFS); michael@0: } michael@0: if (padlen <= M_TRAILINGSPACE(m)) { michael@0: /* michael@0: * The easy way. We hope the majority of the time we hit michael@0: * here :) michael@0: */ michael@0: dp = (uint8_t *) (mtod(m, caddr_t) + SCTP_BUF_LEN(m)); michael@0: SCTP_BUF_LEN(m) += padlen; michael@0: } else { michael@0: /* Hard way we must grow the mbuf */ michael@0: struct mbuf *tmp; michael@0: michael@0: tmp = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (tmp == NULL) { michael@0: /* Out of space GAK! we are in big trouble. */ michael@0: SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); michael@0: return (ENOBUFS); michael@0: } michael@0: /* setup and insert in middle */ michael@0: SCTP_BUF_LEN(tmp) = padlen; michael@0: SCTP_BUF_NEXT(tmp) = NULL; michael@0: SCTP_BUF_NEXT(m) = tmp; michael@0: dp = mtod(tmp, uint8_t *); michael@0: } michael@0: /* zero out the pad */ michael@0: for (i = 0; i < padlen; i++) { michael@0: *dp = 0; michael@0: dp++; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf) michael@0: { michael@0: /* find the last mbuf in chain and pad it */ michael@0: struct mbuf *m_at; michael@0: michael@0: if (last_mbuf) { michael@0: return (sctp_add_pad_tombuf(last_mbuf, padval)); michael@0: } else { michael@0: for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) { michael@0: if (SCTP_BUF_NEXT(m_at) == NULL) { michael@0: return (sctp_add_pad_tombuf(m_at, padval)); michael@0: } michael@0: } michael@0: } michael@0: SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT); michael@0: return (EFAULT); michael@0: } michael@0: michael@0: static void michael@0: sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb, michael@0: uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_assoc_change *sac; michael@0: struct sctp_queued_to_read *control; michael@0: size_t notif_len, abort_len; michael@0: unsigned int i; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) { michael@0: notif_len = sizeof(struct sctp_assoc_change); michael@0: if (abort != NULL) { michael@0: abort_len = ntohs(abort->ch.chunk_length); michael@0: } else { michael@0: abort_len = 0; michael@0: } michael@0: if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) { michael@0: notif_len += SCTP_ASSOC_SUPPORTS_MAX; michael@0: } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) { michael@0: notif_len += abort_len; michael@0: } michael@0: m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) { michael@0: /* Retry with smaller value. */ michael@0: notif_len = sizeof(struct sctp_assoc_change); michael@0: m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) { michael@0: goto set_error; michael@0: } michael@0: } michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: sac = mtod(m_notify, struct sctp_assoc_change *); michael@0: sac->sac_type = SCTP_ASSOC_CHANGE; michael@0: sac->sac_flags = 0; michael@0: sac->sac_length = sizeof(struct sctp_assoc_change); michael@0: sac->sac_state = state; michael@0: sac->sac_error = error; michael@0: /* XXX verify these stream counts */ michael@0: sac->sac_outbound_streams = stcb->asoc.streamoutcnt; michael@0: sac->sac_inbound_streams = stcb->asoc.streamincnt; michael@0: sac->sac_assoc_id = sctp_get_associd(stcb); michael@0: if (notif_len > sizeof(struct sctp_assoc_change)) { michael@0: if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) { michael@0: i = 0; michael@0: if (stcb->asoc.peer_supports_prsctp) { michael@0: sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR; michael@0: } michael@0: if (stcb->asoc.peer_supports_auth) { michael@0: sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH; michael@0: } michael@0: if (stcb->asoc.peer_supports_asconf) { michael@0: sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF; michael@0: } michael@0: sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF; michael@0: if (stcb->asoc.peer_supports_strreset) { michael@0: sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG; michael@0: } michael@0: sac->sac_length += i; michael@0: } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) { michael@0: memcpy(sac->sac_info, abort, abort_len); michael@0: sac->sac_length += abort_len; michael@0: } michael@0: } michael@0: SCTP_BUF_LEN(m_notify) = sac->sac_length; michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control != NULL) { michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: control->spec_flags = M_NOTIFICATION; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, michael@0: so_locked); michael@0: } else { michael@0: sctp_m_freem(m_notify); michael@0: } michael@0: } michael@0: /* michael@0: * For 1-to-1 style sockets, we send up and error when an ABORT michael@0: * comes in. michael@0: */ michael@0: set_error: michael@0: if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && michael@0: ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) { michael@0: SOCK_LOCK(stcb->sctp_socket); michael@0: if (from_peer) { michael@0: if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED); michael@0: stcb->sctp_socket->so_error = ECONNREFUSED; michael@0: } else { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET); michael@0: stcb->sctp_socket->so_error = ECONNRESET; michael@0: } michael@0: } else { michael@0: if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) || michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT); michael@0: stcb->sctp_socket->so_error = ETIMEDOUT; michael@0: } else { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED); michael@0: stcb->sctp_socket->so_error = ECONNABORTED; michael@0: } michael@0: } michael@0: } michael@0: /* Wake ANY sleepers */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: if (!so_locked) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && michael@0: ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) { michael@0: #if defined(__APPLE__) michael@0: socantrcvmore(stcb->sctp_socket); michael@0: #else michael@0: socantrcvmore_locked(stcb->sctp_socket); michael@0: #endif michael@0: } michael@0: sorwakeup(stcb->sctp_socket); michael@0: sowwakeup(stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if (!so_locked) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: static void michael@0: sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state, michael@0: struct sockaddr *sa, uint32_t error) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_paddr_change *spc; michael@0: struct sctp_queued_to_read *control; michael@0: michael@0: if ((stcb == NULL) || michael@0: sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: return; michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: spc = mtod(m_notify, struct sctp_paddr_change *); michael@0: spc->spc_type = SCTP_PEER_ADDR_CHANGE; michael@0: spc->spc_flags = 0; michael@0: spc->spc_length = sizeof(struct sctp_paddr_change); michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: struct sockaddr_in6 *sin6; michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6)); michael@0: michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr; michael@0: if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { michael@0: if (sin6->sin6_scope_id == 0) { michael@0: /* recover scope_id for user */ michael@0: #ifdef SCTP_KAME michael@0: (void)sa6_recoverscope(sin6); michael@0: #else michael@0: (void)in6_recoverscope(sin6, &sin6->sin6_addr, michael@0: NULL); michael@0: #endif michael@0: } else { michael@0: /* clear embedded scope_id for user */ michael@0: in6_clearscope(&sin6->sin6_addr); michael@0: } michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: break; michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_conn)); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: spc->spc_state = state; michael@0: spc->spc_error = error; michael@0: spc->spc_assoc_id = sctp_get_associd(stcb); michael@0: michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change); michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: control->spec_flags = M_NOTIFICATION; michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, michael@0: SCTP_READ_LOCK_NOT_HELD, michael@0: SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error, michael@0: struct sctp_tmit_chunk *chk, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_send_failed *ssf; michael@0: struct sctp_send_failed_event *ssfe; michael@0: struct sctp_queued_to_read *control; michael@0: int length; michael@0: michael@0: if ((stcb == NULL) || michael@0: (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) && michael@0: sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: michael@0: if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { michael@0: length = sizeof(struct sctp_send_failed_event); michael@0: } else { michael@0: length = sizeof(struct sctp_send_failed); michael@0: } michael@0: m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: /* no space left */ michael@0: return; michael@0: length += chk->send_size; michael@0: length -= sizeof(struct sctp_data_chunk); michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { michael@0: ssfe = mtod(m_notify, struct sctp_send_failed_event *); michael@0: ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT; michael@0: if (sent) { michael@0: ssfe->ssfe_flags = SCTP_DATA_SENT; michael@0: } else { michael@0: ssfe->ssfe_flags = SCTP_DATA_UNSENT; michael@0: } michael@0: ssfe->ssfe_length = length; michael@0: ssfe->ssfe_error = error; michael@0: /* not exactly what the user sent in, but should be close :) */ michael@0: bzero(&ssfe->ssfe_info, sizeof(ssfe->ssfe_info)); michael@0: ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number; michael@0: ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags; michael@0: ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype; michael@0: ssfe->ssfe_info.snd_context = chk->rec.data.context; michael@0: ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb); michael@0: ssfe->ssfe_assoc_id = sctp_get_associd(stcb); michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event); michael@0: } else { michael@0: ssf = mtod(m_notify, struct sctp_send_failed *); michael@0: ssf->ssf_type = SCTP_SEND_FAILED; michael@0: if (sent) { michael@0: ssf->ssf_flags = SCTP_DATA_SENT; michael@0: } else { michael@0: ssf->ssf_flags = SCTP_DATA_UNSENT; michael@0: } michael@0: ssf->ssf_length = length; michael@0: ssf->ssf_error = error; michael@0: /* not exactly what the user sent in, but should be close :) */ michael@0: bzero(&ssf->ssf_info, sizeof(ssf->ssf_info)); michael@0: ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number; michael@0: ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq; michael@0: ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags; michael@0: ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype; michael@0: ssf->ssf_info.sinfo_context = chk->rec.data.context; michael@0: ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb); michael@0: ssf->ssf_assoc_id = sctp_get_associd(stcb); michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed); michael@0: } michael@0: if (chk->data) { michael@0: /* michael@0: * trim off the sctp chunk header(it should michael@0: * be there) michael@0: */ michael@0: if (chk->send_size >= sizeof(struct sctp_data_chunk)) { michael@0: m_adj(chk->data, sizeof(struct sctp_data_chunk)); michael@0: sctp_mbuf_crush(chk->data); michael@0: chk->send_size -= sizeof(struct sctp_data_chunk); michael@0: } michael@0: } michael@0: SCTP_BUF_NEXT(m_notify) = chk->data; michael@0: /* Steal off the mbuf */ michael@0: chk->data = NULL; michael@0: /* michael@0: * For this case, we check the actual socket buffer, since the assoc michael@0: * is going away we don't want to overfill the socket buffer for a michael@0: * non-reader michael@0: */ michael@0: if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->spec_flags = M_NOTIFICATION; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, michael@0: SCTP_READ_LOCK_NOT_HELD, michael@0: so_locked); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error, michael@0: struct sctp_stream_queue_pending *sp, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_send_failed *ssf; michael@0: struct sctp_send_failed_event *ssfe; michael@0: struct sctp_queued_to_read *control; michael@0: int length; michael@0: michael@0: if ((stcb == NULL) || michael@0: (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) && michael@0: sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { michael@0: length = sizeof(struct sctp_send_failed_event); michael@0: } else { michael@0: length = sizeof(struct sctp_send_failed); michael@0: } michael@0: m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) { michael@0: /* no space left */ michael@0: return; michael@0: } michael@0: length += sp->length; michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { michael@0: ssfe = mtod(m_notify, struct sctp_send_failed_event *); michael@0: ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT; michael@0: ssfe->ssfe_flags = SCTP_DATA_UNSENT; michael@0: ssfe->ssfe_length = length; michael@0: ssfe->ssfe_error = error; michael@0: /* not exactly what the user sent in, but should be close :) */ michael@0: bzero(&ssfe->ssfe_info, sizeof(ssfe->ssfe_info)); michael@0: ssfe->ssfe_info.snd_sid = sp->stream; michael@0: if (sp->some_taken) { michael@0: ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG; michael@0: } else { michael@0: ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG; michael@0: } michael@0: ssfe->ssfe_info.snd_ppid = sp->ppid; michael@0: ssfe->ssfe_info.snd_context = sp->context; michael@0: ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb); michael@0: ssfe->ssfe_assoc_id = sctp_get_associd(stcb); michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event); michael@0: } else { michael@0: ssf = mtod(m_notify, struct sctp_send_failed *); michael@0: ssf->ssf_type = SCTP_SEND_FAILED; michael@0: ssf->ssf_flags = SCTP_DATA_UNSENT; michael@0: ssf->ssf_length = length; michael@0: ssf->ssf_error = error; michael@0: /* not exactly what the user sent in, but should be close :) */ michael@0: bzero(&ssf->ssf_info, sizeof(ssf->ssf_info)); michael@0: ssf->ssf_info.sinfo_stream = sp->stream; michael@0: ssf->ssf_info.sinfo_ssn = 0; michael@0: if (sp->some_taken) { michael@0: ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG; michael@0: } else { michael@0: ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG; michael@0: } michael@0: ssf->ssf_info.sinfo_ppid = sp->ppid; michael@0: ssf->ssf_info.sinfo_context = sp->context; michael@0: ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb); michael@0: ssf->ssf_assoc_id = sctp_get_associd(stcb); michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed); michael@0: } michael@0: SCTP_BUF_NEXT(m_notify) = sp->data; michael@0: michael@0: /* Steal off the mbuf */ michael@0: sp->data = NULL; michael@0: /* michael@0: * For this case, we check the actual socket buffer, since the assoc michael@0: * is going away we don't want to overfill the socket buffer for a michael@0: * non-reader michael@0: */ michael@0: if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->spec_flags = M_NOTIFICATION; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked); michael@0: } michael@0: michael@0: michael@0: michael@0: static void michael@0: sctp_notify_adaptation_layer(struct sctp_tcb *stcb) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_adaptation_event *sai; michael@0: struct sctp_queued_to_read *control; michael@0: michael@0: if ((stcb == NULL) || michael@0: sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: michael@0: m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: /* no space left */ michael@0: return; michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: sai = mtod(m_notify, struct sctp_adaptation_event *); michael@0: sai->sai_type = SCTP_ADAPTATION_INDICATION; michael@0: sai->sai_flags = 0; michael@0: sai->sai_length = sizeof(struct sctp_adaptation_event); michael@0: sai->sai_adaptation_ind = stcb->asoc.peers_adaptation; michael@0: sai->sai_assoc_id = sctp_get_associd(stcb); michael@0: michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event); michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: control->spec_flags = M_NOTIFICATION; michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: /* This always must be called with the read-queue LOCKED in the INP */ michael@0: static void michael@0: sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error, michael@0: uint32_t val, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_pdapi_event *pdapi; michael@0: struct sctp_queued_to_read *control; michael@0: struct sockbuf *sb; michael@0: michael@0: if ((stcb == NULL) || michael@0: sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) { michael@0: return; michael@0: } michael@0: michael@0: m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: /* no space left */ michael@0: return; michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: pdapi = mtod(m_notify, struct sctp_pdapi_event *); michael@0: pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT; michael@0: pdapi->pdapi_flags = 0; michael@0: pdapi->pdapi_length = sizeof(struct sctp_pdapi_event); michael@0: pdapi->pdapi_indication = error; michael@0: pdapi->pdapi_stream = (val >> 16); michael@0: pdapi->pdapi_seq = (val & 0x0000ffff); michael@0: pdapi->pdapi_assoc_id = sctp_get_associd(stcb); michael@0: michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event); michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->spec_flags = M_NOTIFICATION; michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: control->held_length = 0; michael@0: control->length = 0; michael@0: sb = &stcb->sctp_socket->so_rcv; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify)); michael@0: } michael@0: sctp_sballoc(stcb, sb, m_notify); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify)); michael@0: control->end_added = 1; michael@0: if (stcb->asoc.control_pdapi) michael@0: TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next); michael@0: else { michael@0: /* we really should not see this case */ michael@0: TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next); michael@0: } michael@0: if (stcb->sctp_ep && stcb->sctp_socket) { michael@0: /* This should always be the case */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: if (!so_locked) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if (!so_locked) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_notify_shutdown_event(struct sctp_tcb *stcb) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_shutdown_event *sse; michael@0: struct sctp_queued_to_read *control; michael@0: michael@0: /* michael@0: * For TCP model AND UDP connected sockets we will send an error up michael@0: * when an SHUTDOWN completes michael@0: */ michael@0: if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: /* mark socket closed for read/write and wakeup! */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return; michael@0: } michael@0: #endif michael@0: socantsendmore(stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: michael@0: m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: /* no space left */ michael@0: return; michael@0: sse = mtod(m_notify, struct sctp_shutdown_event *); michael@0: sse->sse_type = SCTP_SHUTDOWN_EVENT; michael@0: sse->sse_flags = 0; michael@0: sse->sse_length = sizeof(struct sctp_shutdown_event); michael@0: sse->sse_assoc_id = sctp_get_associd(stcb); michael@0: michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event); michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->spec_flags = M_NOTIFICATION; michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: static void michael@0: sctp_notify_sender_dry_event(struct sctp_tcb *stcb, michael@0: int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_sender_dry_event *event; michael@0: struct sctp_queued_to_read *control; michael@0: michael@0: if ((stcb == NULL) || michael@0: sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: michael@0: m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) { michael@0: /* no space left */ michael@0: return; michael@0: } michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: event = mtod(m_notify, struct sctp_sender_dry_event *); michael@0: event->sender_dry_type = SCTP_SENDER_DRY_EVENT; michael@0: event->sender_dry_flags = 0; michael@0: event->sender_dry_length = sizeof(struct sctp_sender_dry_event); michael@0: event->sender_dry_assoc_id = sctp_get_associd(stcb); michael@0: michael@0: SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event); michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: control->spec_flags = M_NOTIFICATION; michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_queued_to_read *control; michael@0: struct sctp_stream_change_event *stradd; michael@0: int len; michael@0: michael@0: if ((stcb == NULL) || michael@0: (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: if ((stcb->asoc.peer_req_out) && flag) { michael@0: /* Peer made the request, don't tell the local user */ michael@0: stcb->asoc.peer_req_out = 0; michael@0: return; michael@0: } michael@0: stcb->asoc.peer_req_out = 0; michael@0: m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: /* no space left */ michael@0: return; michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: len = sizeof(struct sctp_stream_change_event); michael@0: if (len > M_TRAILINGSPACE(m_notify)) { michael@0: /* never enough room */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: stradd = mtod(m_notify, struct sctp_stream_change_event *); michael@0: stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT; michael@0: stradd->strchange_flags = flag; michael@0: stradd->strchange_length = len; michael@0: stradd->strchange_assoc_id = sctp_get_associd(stcb); michael@0: stradd->strchange_instrms = numberin; michael@0: stradd->strchange_outstrms = numberout; michael@0: SCTP_BUF_LEN(m_notify) = len; michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { michael@0: /* no space */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->spec_flags = M_NOTIFICATION; michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: void michael@0: sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_queued_to_read *control; michael@0: struct sctp_assoc_reset_event *strasoc; michael@0: int len; michael@0: michael@0: if ((stcb == NULL) || michael@0: (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: /* no space left */ michael@0: return; michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: len = sizeof(struct sctp_assoc_reset_event); michael@0: if (len > M_TRAILINGSPACE(m_notify)) { michael@0: /* never enough room */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: strasoc = mtod(m_notify, struct sctp_assoc_reset_event *); michael@0: strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT; michael@0: strasoc->assocreset_flags = flag; michael@0: strasoc->assocreset_length = len; michael@0: strasoc->assocreset_assoc_id= sctp_get_associd(stcb); michael@0: strasoc->assocreset_local_tsn = sending_tsn; michael@0: strasoc->assocreset_remote_tsn = recv_tsn; michael@0: SCTP_BUF_LEN(m_notify) = len; michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { michael@0: /* no space */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->spec_flags = M_NOTIFICATION; michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: michael@0: michael@0: static void michael@0: sctp_notify_stream_reset(struct sctp_tcb *stcb, michael@0: int number_entries, uint16_t * list, int flag) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_queued_to_read *control; michael@0: struct sctp_stream_reset_event *strreset; michael@0: int len; michael@0: michael@0: if ((stcb == NULL) || michael@0: (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) { michael@0: /* event not enabled */ michael@0: return; michael@0: } michael@0: michael@0: m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) michael@0: /* no space left */ michael@0: return; michael@0: SCTP_BUF_LEN(m_notify) = 0; michael@0: len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t)); michael@0: if (len > M_TRAILINGSPACE(m_notify)) { michael@0: /* never enough room */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: strreset = mtod(m_notify, struct sctp_stream_reset_event *); michael@0: strreset->strreset_type = SCTP_STREAM_RESET_EVENT; michael@0: strreset->strreset_flags = flag; michael@0: strreset->strreset_length = len; michael@0: strreset->strreset_assoc_id = sctp_get_associd(stcb); michael@0: if (number_entries) { michael@0: int i; michael@0: michael@0: for (i = 0; i < number_entries; i++) { michael@0: strreset->strreset_stream_list[i] = ntohs(list[i]); michael@0: } michael@0: } michael@0: SCTP_BUF_LEN(m_notify) = len; michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { michael@0: /* no space */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: /* append to socket */ michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control == NULL) { michael@0: /* no memory */ michael@0: sctp_m_freem(m_notify); michael@0: return; michael@0: } michael@0: control->spec_flags = M_NOTIFICATION; michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk) michael@0: { michael@0: struct mbuf *m_notify; michael@0: struct sctp_remote_error *sre; michael@0: struct sctp_queued_to_read *control; michael@0: size_t notif_len, chunk_len; michael@0: michael@0: if ((stcb == NULL) || michael@0: sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) { michael@0: return; michael@0: } michael@0: if (chunk != NULL) { michael@0: chunk_len = ntohs(chunk->ch.chunk_length); michael@0: } else { michael@0: chunk_len = 0; michael@0: } michael@0: notif_len = sizeof(struct sctp_remote_error) + chunk_len; michael@0: m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) { michael@0: /* Retry with smaller value. */ michael@0: notif_len = sizeof(struct sctp_remote_error); michael@0: m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m_notify == NULL) { michael@0: return; michael@0: } michael@0: } michael@0: SCTP_BUF_NEXT(m_notify) = NULL; michael@0: sre = mtod(m_notify, struct sctp_remote_error *); michael@0: sre->sre_type = SCTP_REMOTE_ERROR; michael@0: sre->sre_flags = 0; michael@0: sre->sre_length = sizeof(struct sctp_remote_error); michael@0: sre->sre_error = error; michael@0: sre->sre_assoc_id = sctp_get_associd(stcb); michael@0: if (notif_len > sizeof(struct sctp_remote_error)) { michael@0: memcpy(sre->sre_data, chunk, chunk_len); michael@0: sre->sre_length += chunk_len; michael@0: } michael@0: SCTP_BUF_LEN(m_notify) = sre->sre_length; michael@0: control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, michael@0: 0, 0, stcb->asoc.context, 0, 0, 0, michael@0: m_notify); michael@0: if (control != NULL) { michael@0: control->length = SCTP_BUF_LEN(m_notify); michael@0: /* not that we need this */ michael@0: control->tail_mbuf = m_notify; michael@0: control->spec_flags = M_NOTIFICATION; michael@0: sctp_add_to_readq(stcb->sctp_ep, stcb, michael@0: control, michael@0: &stcb->sctp_socket->so_rcv, 1, michael@0: SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); michael@0: } else { michael@0: sctp_m_freem(m_notify); michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb, michael@0: uint32_t error, void *data, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: if ((stcb == NULL) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { michael@0: /* If the socket is gone we are out of here */ michael@0: return; michael@0: } michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__) michael@0: if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) { michael@0: #else michael@0: if (stcb->sctp_socket->so_state & SS_CANTRCVMORE) { michael@0: #endif michael@0: return; michael@0: } michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } michael@0: #endif michael@0: if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) || michael@0: (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) { michael@0: if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) || michael@0: (notification == SCTP_NOTIFY_INTERFACE_UP) || michael@0: (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) { michael@0: /* Don't report these in front states */ michael@0: return; michael@0: } michael@0: } michael@0: switch (notification) { michael@0: case SCTP_NOTIFY_ASSOC_UP: michael@0: if (stcb->asoc.assoc_up_sent == 0) { michael@0: sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked); michael@0: stcb->asoc.assoc_up_sent = 1; michael@0: } michael@0: if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) { michael@0: sctp_notify_adaptation_layer(stcb); michael@0: } michael@0: if (stcb->asoc.peer_supports_auth == 0) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0, michael@0: NULL, so_locked); michael@0: } michael@0: break; michael@0: case SCTP_NOTIFY_ASSOC_DOWN: michael@0: sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked); michael@0: #if defined(__Userspace__) michael@0: if (stcb->sctp_ep->recv_callback) { michael@0: if (stcb->sctp_socket) { michael@0: union sctp_sockstore addr; michael@0: struct sctp_rcvinfo rcv; michael@0: michael@0: memset(&addr, 0, sizeof(union sctp_sockstore)); michael@0: memset(&rcv, 0, sizeof(struct sctp_rcvinfo)); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: stcb->sctp_ep->recv_callback(stcb->sctp_socket, addr, NULL, 0, rcv, 0, stcb->sctp_ep->ulp_info); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: } michael@0: } michael@0: #endif michael@0: break; michael@0: case SCTP_NOTIFY_INTERFACE_DOWN: michael@0: { michael@0: struct sctp_nets *net; michael@0: michael@0: net = (struct sctp_nets *)data; michael@0: sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE, michael@0: (struct sockaddr *)&net->ro._l_addr, error); michael@0: break; michael@0: } michael@0: case SCTP_NOTIFY_INTERFACE_UP: michael@0: { michael@0: struct sctp_nets *net; michael@0: michael@0: net = (struct sctp_nets *)data; michael@0: sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE, michael@0: (struct sockaddr *)&net->ro._l_addr, error); michael@0: break; michael@0: } michael@0: case SCTP_NOTIFY_INTERFACE_CONFIRMED: michael@0: { michael@0: struct sctp_nets *net; michael@0: michael@0: net = (struct sctp_nets *)data; michael@0: sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED, michael@0: (struct sockaddr *)&net->ro._l_addr, error); michael@0: break; michael@0: } michael@0: case SCTP_NOTIFY_SPECIAL_SP_FAIL: michael@0: sctp_notify_send_failed2(stcb, error, michael@0: (struct sctp_stream_queue_pending *)data, so_locked); michael@0: break; michael@0: case SCTP_NOTIFY_SENT_DG_FAIL: michael@0: sctp_notify_send_failed(stcb, 1, error, michael@0: (struct sctp_tmit_chunk *)data, so_locked); michael@0: break; michael@0: case SCTP_NOTIFY_UNSENT_DG_FAIL: michael@0: sctp_notify_send_failed(stcb, 0, error, michael@0: (struct sctp_tmit_chunk *)data, so_locked); michael@0: break; michael@0: case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION: michael@0: { michael@0: uint32_t val; michael@0: val = *((uint32_t *)data); michael@0: michael@0: sctp_notify_partial_delivery_indication(stcb, error, val, so_locked); michael@0: break; michael@0: } michael@0: case SCTP_NOTIFY_ASSOC_LOC_ABORTED: michael@0: if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) || michael@0: ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked); michael@0: } else { michael@0: sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked); michael@0: } michael@0: break; michael@0: case SCTP_NOTIFY_ASSOC_REM_ABORTED: michael@0: if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) || michael@0: ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) { michael@0: sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked); michael@0: } else { michael@0: sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked); michael@0: } michael@0: break; michael@0: case SCTP_NOTIFY_ASSOC_RESTART: michael@0: sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked); michael@0: if (stcb->asoc.peer_supports_auth == 0) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0, michael@0: NULL, so_locked); michael@0: } michael@0: break; michael@0: case SCTP_NOTIFY_STR_RESET_SEND: michael@0: sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN); michael@0: break; michael@0: case SCTP_NOTIFY_STR_RESET_RECV: michael@0: sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING); michael@0: break; michael@0: case SCTP_NOTIFY_STR_RESET_FAILED_OUT: michael@0: sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), michael@0: (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_FAILED)); michael@0: break; michael@0: case SCTP_NOTIFY_STR_RESET_DENIED_OUT: michael@0: sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), michael@0: (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_DENIED)); michael@0: break; michael@0: case SCTP_NOTIFY_STR_RESET_FAILED_IN: michael@0: sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), michael@0: (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_FAILED)); michael@0: break; michael@0: case SCTP_NOTIFY_STR_RESET_DENIED_IN: michael@0: sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), michael@0: (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_DENIED)); michael@0: break; michael@0: case SCTP_NOTIFY_ASCONF_ADD_IP: michael@0: sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data, michael@0: error); michael@0: break; michael@0: case SCTP_NOTIFY_ASCONF_DELETE_IP: michael@0: sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data, michael@0: error); michael@0: break; michael@0: case SCTP_NOTIFY_ASCONF_SET_PRIMARY: michael@0: sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data, michael@0: error); michael@0: break; michael@0: case SCTP_NOTIFY_PEER_SHUTDOWN: michael@0: sctp_notify_shutdown_event(stcb); michael@0: break; michael@0: case SCTP_NOTIFY_AUTH_NEW_KEY: michael@0: sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error, michael@0: (uint16_t)(uintptr_t)data, michael@0: so_locked); michael@0: break; michael@0: case SCTP_NOTIFY_AUTH_FREE_KEY: michael@0: sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error, michael@0: (uint16_t)(uintptr_t)data, michael@0: so_locked); michael@0: break; michael@0: case SCTP_NOTIFY_NO_PEER_AUTH: michael@0: sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error, michael@0: (uint16_t)(uintptr_t)data, michael@0: so_locked); michael@0: break; michael@0: case SCTP_NOTIFY_SENDER_DRY: michael@0: sctp_notify_sender_dry_event(stcb, so_locked); michael@0: break; michael@0: case SCTP_NOTIFY_REMOTE_ERROR: michael@0: sctp_notify_remote_error(stcb, error, data); michael@0: break; michael@0: default: michael@0: SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n", michael@0: __FUNCTION__, notification, notification); michael@0: break; michael@0: } /* end switch */ michael@0: } michael@0: michael@0: void michael@0: sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct sctp_association *asoc; michael@0: struct sctp_stream_out *outs; michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: struct sctp_stream_queue_pending *sp, *nsp; michael@0: int i; michael@0: michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: asoc = &stcb->asoc; michael@0: if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: /* already being freed */ michael@0: return; michael@0: } michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } michael@0: #endif michael@0: if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (asoc->state & SCTP_STATE_CLOSED_SOCKET)) { michael@0: return; michael@0: } michael@0: /* now through all the gunk freeing chunks */ michael@0: if (holds_lock == 0) { michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: } michael@0: /* sent queue SHOULD be empty */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { michael@0: TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); michael@0: asoc->sent_queue_cnt--; michael@0: if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { michael@0: if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) { michael@0: asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--; michael@0: #ifdef INVARIANTS michael@0: } else { michael@0: panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number); michael@0: #endif michael@0: } michael@0: } michael@0: if (chk->data != NULL) { michael@0: sctp_free_bufspace(stcb, asoc, chk, 1); michael@0: sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, michael@0: error, chk, so_locked); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: } michael@0: sctp_free_a_chunk(stcb, chk, so_locked); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: /* pending send queue SHOULD be empty */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { michael@0: TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); michael@0: asoc->send_queue_cnt--; michael@0: if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) { michael@0: asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--; michael@0: #ifdef INVARIANTS michael@0: } else { michael@0: panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number); michael@0: #endif michael@0: } michael@0: if (chk->data != NULL) { michael@0: sctp_free_bufspace(stcb, asoc, chk, 1); michael@0: sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, michael@0: error, chk, so_locked); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: } michael@0: sctp_free_a_chunk(stcb, chk, so_locked); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: for (i = 0; i < asoc->streamoutcnt; i++) { michael@0: /* For each stream */ michael@0: outs = &asoc->strmout[i]; michael@0: /* clean up any sends there */ michael@0: asoc->locked_on_sending = NULL; michael@0: TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { michael@0: asoc->stream_queue_cnt--; michael@0: TAILQ_REMOVE(&outs->outqueue, sp, next); michael@0: sctp_free_spbufspace(stcb, asoc, sp); michael@0: if (sp->data) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, michael@0: error, (void *)sp, so_locked); michael@0: if (sp->data) { michael@0: sctp_m_freem(sp->data); michael@0: sp->data = NULL; michael@0: sp->tail_mbuf = NULL; michael@0: sp->length = 0; michael@0: } michael@0: } michael@0: if (sp->net) { michael@0: sctp_free_remote_addr(sp->net); michael@0: sp->net = NULL; michael@0: } michael@0: /* Free the chunk */ michael@0: sctp_free_a_strmoq(stcb, sp, so_locked); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: } michael@0: michael@0: if (holds_lock == 0) { michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error, michael@0: struct sctp_abort_chunk *abort, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: if (stcb == NULL) { michael@0: return; michael@0: } michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } michael@0: #endif michael@0: if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || michael@0: ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { michael@0: stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED; michael@0: } michael@0: if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { michael@0: return; michael@0: } michael@0: /* Tell them we lost the asoc */ michael@0: sctp_report_all_outbound(stcb, error, 1, so_locked); michael@0: if (from_peer) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked); michael@0: } else { michael@0: sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked); michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: struct mbuf *m, int iphlen, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct mbuf *op_err, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: uint32_t vtag; michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: vtag = 0; michael@0: if (stcb != NULL) { michael@0: /* We have a TCB to abort, send notification too */ michael@0: vtag = stcb->asoc.peer_vtag; michael@0: sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED); michael@0: /* get the assoc vrf id and table id */ michael@0: vrf_id = stcb->asoc.vrf_id; michael@0: stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; michael@0: } michael@0: sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: if (stcb != NULL) { michael@0: /* Ok, now lets free it */ michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(inp); michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: #endif michael@0: SCTP_STAT_INCR_COUNTER32(sctps_aborted); michael@0: if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL+SCTP_LOC_4); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: } michael@0: #ifdef SCTP_ASOCLOG_OF_TSNS michael@0: void michael@0: sctp_print_out_track_log(struct sctp_tcb *stcb) michael@0: { michael@0: #ifdef NOSIY_PRINTS michael@0: int i; michael@0: SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code); michael@0: SCTP_PRINTF("IN bound TSN log-aaa\n"); michael@0: if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) { michael@0: SCTP_PRINTF("None rcvd\n"); michael@0: goto none_in; michael@0: } michael@0: if (stcb->asoc.tsn_in_wrapped) { michael@0: for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) { michael@0: SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", michael@0: stcb->asoc.in_tsnlog[i].tsn, michael@0: stcb->asoc.in_tsnlog[i].strm, michael@0: stcb->asoc.in_tsnlog[i].seq, michael@0: stcb->asoc.in_tsnlog[i].flgs, michael@0: stcb->asoc.in_tsnlog[i].sz); michael@0: } michael@0: } michael@0: if (stcb->asoc.tsn_in_at) { michael@0: for (i = 0; i < stcb->asoc.tsn_in_at; i++) { michael@0: SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", michael@0: stcb->asoc.in_tsnlog[i].tsn, michael@0: stcb->asoc.in_tsnlog[i].strm, michael@0: stcb->asoc.in_tsnlog[i].seq, michael@0: stcb->asoc.in_tsnlog[i].flgs, michael@0: stcb->asoc.in_tsnlog[i].sz); michael@0: } michael@0: } michael@0: none_in: michael@0: SCTP_PRINTF("OUT bound TSN log-aaa\n"); michael@0: if ((stcb->asoc.tsn_out_at == 0) && michael@0: (stcb->asoc.tsn_out_wrapped == 0)) { michael@0: SCTP_PRINTF("None sent\n"); michael@0: } michael@0: if (stcb->asoc.tsn_out_wrapped) { michael@0: for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) { michael@0: SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", michael@0: stcb->asoc.out_tsnlog[i].tsn, michael@0: stcb->asoc.out_tsnlog[i].strm, michael@0: stcb->asoc.out_tsnlog[i].seq, michael@0: stcb->asoc.out_tsnlog[i].flgs, michael@0: stcb->asoc.out_tsnlog[i].sz); michael@0: } michael@0: } michael@0: if (stcb->asoc.tsn_out_at) { michael@0: for (i = 0; i < stcb->asoc.tsn_out_at; i++) { michael@0: SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", michael@0: stcb->asoc.out_tsnlog[i].tsn, michael@0: stcb->asoc.out_tsnlog[i].strm, michael@0: stcb->asoc.out_tsnlog[i].seq, michael@0: stcb->asoc.out_tsnlog[i].flgs, michael@0: stcb->asoc.out_tsnlog[i].sz); michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb, michael@0: struct mbuf *op_err, michael@0: int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: so = SCTP_INP_SO(inp); michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(inp)); michael@0: } michael@0: #endif michael@0: if (stcb == NULL) { michael@0: /* Got to have a TCB */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: if (LIST_EMPTY(&inp->sctp_asoc_list)) { michael@0: #if defined(__APPLE__) michael@0: if (!so_locked) { michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: } michael@0: #endif michael@0: sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, michael@0: SCTP_CALLED_DIRECTLY_NOCMPSET); michael@0: #if defined(__APPLE__) michael@0: if (!so_locked) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: return; michael@0: } else { michael@0: stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; michael@0: } michael@0: /* notify the ulp */ michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { michael@0: sctp_abort_notification(stcb, 0, 0, NULL, so_locked); michael@0: } michael@0: /* notify the peer */ michael@0: sctp_send_abort_tcb(stcb, op_err, so_locked); michael@0: SCTP_STAT_INCR_COUNTER32(sctps_aborted); michael@0: if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: /* now free the asoc */ michael@0: #ifdef SCTP_ASOCLOG_OF_TSNS michael@0: sctp_print_out_track_log(stcb); michael@0: #endif michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if (!so_locked) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: } michael@0: #endif michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL+SCTP_LOC_5); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if (!so_locked) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_inpcb *inp, michael@0: #if defined(__FreeBSD__) michael@0: uint8_t use_mflowid, uint32_t mflowid, michael@0: #endif michael@0: uint32_t vrf_id, uint16_t port) michael@0: { michael@0: struct sctp_chunkhdr *ch, chunk_buf; michael@0: unsigned int chk_length; michael@0: int contains_init_chunk; michael@0: michael@0: SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue); michael@0: /* Generate a TO address for future reference */ michael@0: if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { michael@0: if (LIST_EMPTY(&inp->sctp_asoc_list)) { michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1); michael@0: #endif michael@0: sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, michael@0: SCTP_CALLED_DIRECTLY_NOCMPSET); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1); michael@0: #endif michael@0: } michael@0: } michael@0: contains_init_chunk = 0; michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, michael@0: sizeof(*ch), (uint8_t *) & chunk_buf); michael@0: while (ch != NULL) { michael@0: chk_length = ntohs(ch->chunk_length); michael@0: if (chk_length < sizeof(*ch)) { michael@0: /* break to abort land */ michael@0: break; michael@0: } michael@0: switch (ch->chunk_type) { michael@0: case SCTP_INIT: michael@0: contains_init_chunk = 1; michael@0: break; michael@0: case SCTP_COOKIE_ECHO: michael@0: /* We hit here only if the assoc is being freed */ michael@0: return; michael@0: case SCTP_PACKET_DROPPED: michael@0: /* we don't respond to pkt-dropped */ michael@0: return; michael@0: case SCTP_ABORT_ASSOCIATION: michael@0: /* we don't respond with an ABORT to an ABORT */ michael@0: return; michael@0: case SCTP_SHUTDOWN_COMPLETE: michael@0: /* michael@0: * we ignore it since we are not waiting for it and michael@0: * peer is gone michael@0: */ michael@0: return; michael@0: case SCTP_SHUTDOWN_ACK: michael@0: sctp_send_shutdown_complete2(src, dst, sh, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: return; michael@0: default: michael@0: break; michael@0: } michael@0: offset += SCTP_SIZE32(chk_length); michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, michael@0: sizeof(*ch), (uint8_t *) & chunk_buf); michael@0: } michael@0: if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || michael@0: ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && michael@0: (contains_init_chunk == 0))) { michael@0: sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * check the inbound datagram to make sure there is not an abort inside it, michael@0: * if there is return 1, else return 0. michael@0: */ michael@0: int michael@0: sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill) michael@0: { michael@0: struct sctp_chunkhdr *ch; michael@0: struct sctp_init_chunk *init_chk, chunk_buf; michael@0: int offset; michael@0: unsigned int chk_length; michael@0: michael@0: offset = iphlen + sizeof(struct sctphdr); michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch), michael@0: (uint8_t *) & chunk_buf); michael@0: while (ch != NULL) { michael@0: chk_length = ntohs(ch->chunk_length); michael@0: if (chk_length < sizeof(*ch)) { michael@0: /* packet is probably corrupt */ michael@0: break; michael@0: } michael@0: /* we seem to be ok, is it an abort? */ michael@0: if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) { michael@0: /* yep, tell them */ michael@0: return (1); michael@0: } michael@0: if (ch->chunk_type == SCTP_INITIATION) { michael@0: /* need to update the Vtag */ michael@0: init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, michael@0: offset, sizeof(*init_chk), (uint8_t *) & chunk_buf); michael@0: if (init_chk != NULL) { michael@0: *vtagfill = ntohl(init_chk->init.initiate_tag); michael@0: } michael@0: } michael@0: /* Nope, move to the next chunk */ michael@0: offset += SCTP_SIZE32(chk_length); michael@0: ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, michael@0: sizeof(*ch), (uint8_t *) & chunk_buf); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: /* michael@0: * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id michael@0: * set (i.e. it's 0) so, create this function to compare link local scopes michael@0: */ michael@0: #ifdef INET6 michael@0: uint32_t michael@0: sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2) michael@0: { michael@0: #if defined(__Userspace__) michael@0: /*__Userspace__ Returning 1 here always */ michael@0: #endif michael@0: #if defined(SCTP_EMBEDDED_V6_SCOPE) michael@0: struct sockaddr_in6 a, b; michael@0: michael@0: /* save copies */ michael@0: a = *addr1; michael@0: b = *addr2; michael@0: michael@0: if (a.sin6_scope_id == 0) michael@0: #ifdef SCTP_KAME michael@0: if (sa6_recoverscope(&a)) { michael@0: #else michael@0: if (in6_recoverscope(&a, &a.sin6_addr, NULL)) { michael@0: #endif /* SCTP_KAME */ michael@0: /* can't get scope, so can't match */ michael@0: return (0); michael@0: } michael@0: if (b.sin6_scope_id == 0) michael@0: #ifdef SCTP_KAME michael@0: if (sa6_recoverscope(&b)) { michael@0: #else michael@0: if (in6_recoverscope(&b, &b.sin6_addr, NULL)) { michael@0: #endif /* SCTP_KAME */ michael@0: /* can't get scope, so can't match */ michael@0: return (0); michael@0: } michael@0: if (a.sin6_scope_id != b.sin6_scope_id) michael@0: return (0); michael@0: #else michael@0: if (addr1->sin6_scope_id != addr2->sin6_scope_id) michael@0: return (0); michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: michael@0: return (1); michael@0: } michael@0: michael@0: #if defined(SCTP_EMBEDDED_V6_SCOPE) michael@0: /* michael@0: * returns a sockaddr_in6 with embedded scope recovered and removed michael@0: */ michael@0: struct sockaddr_in6 * michael@0: sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store) michael@0: { michael@0: /* check and strip embedded scope junk */ michael@0: if (addr->sin6_family == AF_INET6) { michael@0: if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) { michael@0: if (addr->sin6_scope_id == 0) { michael@0: *store = *addr; michael@0: #ifdef SCTP_KAME michael@0: if (!sa6_recoverscope(store)) { michael@0: #else michael@0: if (!in6_recoverscope(store, &store->sin6_addr, michael@0: NULL)) { michael@0: #endif /* SCTP_KAME */ michael@0: /* use the recovered scope */ michael@0: addr = store; michael@0: } michael@0: } else { michael@0: /* else, return the original "to" addr */ michael@0: in6_clearscope(&addr->sin6_addr); michael@0: } michael@0: } michael@0: } michael@0: return (addr); michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #endif michael@0: michael@0: /* michael@0: * are the two addresses the same? currently a "scopeless" check returns: 1 michael@0: * if same, 0 if not michael@0: */ michael@0: int michael@0: sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2) michael@0: { michael@0: michael@0: /* must be valid */ michael@0: if (sa1 == NULL || sa2 == NULL) michael@0: return (0); michael@0: michael@0: /* must be the same family */ michael@0: if (sa1->sa_family != sa2->sa_family) michael@0: return (0); michael@0: michael@0: switch (sa1->sa_family) { michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: /* IPv6 addresses */ michael@0: struct sockaddr_in6 *sin6_1, *sin6_2; michael@0: michael@0: sin6_1 = (struct sockaddr_in6 *)sa1; michael@0: sin6_2 = (struct sockaddr_in6 *)sa2; michael@0: return (SCTP6_ARE_ADDR_EQUAL(sin6_1, michael@0: sin6_2)); michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: /* IPv4 addresses */ michael@0: struct sockaddr_in *sin_1, *sin_2; michael@0: michael@0: sin_1 = (struct sockaddr_in *)sa1; michael@0: sin_2 = (struct sockaddr_in *)sa2; michael@0: return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr); michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: { michael@0: struct sockaddr_conn *sconn_1, *sconn_2; michael@0: michael@0: sconn_1 = (struct sockaddr_conn *)sa1; michael@0: sconn_2 = (struct sockaddr_conn *)sa2; michael@0: return (sconn_1->sconn_addr == sconn_2->sconn_addr); michael@0: } michael@0: #endif michael@0: default: michael@0: /* we don't do these... */ michael@0: return (0); michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_print_address(struct sockaddr *sa) michael@0: { michael@0: #ifdef INET6 michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 700000 michael@0: char ip6buf[INET6_ADDRSTRLEN]; michael@0: #endif michael@0: #endif michael@0: michael@0: switch (sa->sa_family) { michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)sa; michael@0: #if defined(__Userspace__) michael@0: SCTP_PRINTF("IPv6 address: %x:%x:%x:%x:%x:%x:%x:%x:port:%d scope:%u\n", michael@0: ntohs(sin6->sin6_addr.s6_addr16[0]), michael@0: ntohs(sin6->sin6_addr.s6_addr16[1]), michael@0: ntohs(sin6->sin6_addr.s6_addr16[2]), michael@0: ntohs(sin6->sin6_addr.s6_addr16[3]), michael@0: ntohs(sin6->sin6_addr.s6_addr16[4]), michael@0: ntohs(sin6->sin6_addr.s6_addr16[5]), michael@0: ntohs(sin6->sin6_addr.s6_addr16[6]), michael@0: ntohs(sin6->sin6_addr.s6_addr16[7]), michael@0: ntohs(sin6->sin6_port), michael@0: sin6->sin6_scope_id); michael@0: #else michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 700000 michael@0: SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n", michael@0: ip6_sprintf(ip6buf, &sin6->sin6_addr), michael@0: ntohs(sin6->sin6_port), michael@0: sin6->sin6_scope_id); michael@0: #else michael@0: SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n", michael@0: ip6_sprintf(&sin6->sin6_addr), michael@0: ntohs(sin6->sin6_port), michael@0: sin6->sin6_scope_id); michael@0: #endif michael@0: #endif michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct sockaddr_in *sin; michael@0: unsigned char *p; michael@0: michael@0: sin = (struct sockaddr_in *)sa; michael@0: p = (unsigned char *)&sin->sin_addr; michael@0: SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n", michael@0: p[0], p[1], p[2], p[3], ntohs(sin->sin_port)); michael@0: break; michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: { michael@0: struct sockaddr_conn *sconn; michael@0: michael@0: sconn = (struct sockaddr_conn *)sa; michael@0: SCTP_PRINTF("AF_CONN address: %p\n", sconn->sconn_addr); michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: SCTP_PRINTF("?\n"); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: void michael@0: sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp, michael@0: struct sctp_inpcb *new_inp, michael@0: struct sctp_tcb *stcb, michael@0: int waitflags) michael@0: { michael@0: /* michael@0: * go through our old INP and pull off any control structures that michael@0: * belong to stcb and move then to the new inp. michael@0: */ michael@0: struct socket *old_so, *new_so; michael@0: struct sctp_queued_to_read *control, *nctl; michael@0: struct sctp_readhead tmp_queue; michael@0: struct mbuf *m; michael@0: int error = 0; michael@0: michael@0: old_so = old_inp->sctp_socket; michael@0: new_so = new_inp->sctp_socket; michael@0: TAILQ_INIT(&tmp_queue); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 700000 michael@0: SOCKBUF_LOCK(&(old_so->so_rcv)); michael@0: #endif michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) michael@0: error = sblock(&old_so->so_rcv, waitflags); michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 700000 michael@0: SOCKBUF_UNLOCK(&(old_so->so_rcv)); michael@0: #endif michael@0: if (error) { michael@0: /* Gak, can't get sblock, we have a problem. michael@0: * data will be left stranded.. and we michael@0: * don't dare look at it since the michael@0: * other thread may be reading something. michael@0: * Oh well, its a screwed up app that does michael@0: * a peeloff OR a accept while reading michael@0: * from the main socket... actually its michael@0: * only the peeloff() case, since I think michael@0: * read will fail on a listening socket.. michael@0: */ michael@0: return; michael@0: } michael@0: /* lock the socket buffers */ michael@0: SCTP_INP_READ_LOCK(old_inp); michael@0: TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) { michael@0: /* Pull off all for out target stcb */ michael@0: if (control->stcb == stcb) { michael@0: /* remove it we want it */ michael@0: TAILQ_REMOVE(&old_inp->read_queue, control, next); michael@0: TAILQ_INSERT_TAIL(&tmp_queue, control, next); michael@0: m = control->data; michael@0: while (m) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE,SCTP_BUF_LEN(m)); michael@0: } michael@0: sctp_sbfree(control, stcb, &old_so->so_rcv, m); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: } michael@0: } michael@0: SCTP_INP_READ_UNLOCK(old_inp); michael@0: /* Remove the sb-lock on the old socket */ michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 700000 michael@0: SOCKBUF_LOCK(&(old_so->so_rcv)); michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: sbunlock(&old_so->so_rcv, 1); michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) michael@0: sbunlock(&old_so->so_rcv); michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 700000 michael@0: SOCKBUF_UNLOCK(&(old_so->so_rcv)); michael@0: #endif michael@0: /* Now we move them over to the new socket buffer */ michael@0: SCTP_INP_READ_LOCK(new_inp); michael@0: TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) { michael@0: TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next); michael@0: m = control->data; michael@0: while (m) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m)); michael@0: } michael@0: sctp_sballoc(stcb, &new_so->so_rcv, m); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: } michael@0: SCTP_INP_READ_UNLOCK(new_inp); michael@0: } michael@0: michael@0: void michael@0: sctp_add_to_readq(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_queued_to_read *control, michael@0: struct sockbuf *sb, michael@0: int end, michael@0: int inp_read_lock_held, michael@0: int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: /* michael@0: * Here we must place the control on the end of the socket read michael@0: * queue AND increment sb_cc so that select will work properly on michael@0: * read. michael@0: */ michael@0: struct mbuf *m, *prev = NULL; michael@0: michael@0: if (inp == NULL) { michael@0: /* Gak, TSNH!! */ michael@0: #ifdef INVARIANTS michael@0: panic("Gak, inp NULL on add_to_readq"); michael@0: #endif michael@0: return; michael@0: } michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(inp)); michael@0: } michael@0: #endif michael@0: if (inp_read_lock_held == 0) michael@0: SCTP_INP_READ_LOCK(inp); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) { michael@0: sctp_free_remote_addr(control->whoFrom); michael@0: if (control->data) { michael@0: sctp_m_freem(control->data); michael@0: control->data = NULL; michael@0: } michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control); michael@0: if (inp_read_lock_held == 0) michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: return; michael@0: } michael@0: if (!(control->spec_flags & M_NOTIFICATION)) { michael@0: atomic_add_int(&inp->total_recvs, 1); michael@0: if (!control->do_not_ref_stcb) { michael@0: atomic_add_int(&stcb->total_recvs, 1); michael@0: } michael@0: } michael@0: m = control->data; michael@0: control->held_length = 0; michael@0: control->length = 0; michael@0: while (m) { michael@0: if (SCTP_BUF_LEN(m) == 0) { michael@0: /* Skip mbufs with NO length */ michael@0: if (prev == NULL) { michael@0: /* First one */ michael@0: control->data = sctp_m_free(m); michael@0: m = control->data; michael@0: } else { michael@0: SCTP_BUF_NEXT(prev) = sctp_m_free(m); michael@0: m = SCTP_BUF_NEXT(prev); michael@0: } michael@0: if (m == NULL) { michael@0: control->tail_mbuf = prev; michael@0: } michael@0: continue; michael@0: } michael@0: prev = m; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m)); michael@0: } michael@0: sctp_sballoc(stcb, sb, m); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: atomic_add_int(&control->length, SCTP_BUF_LEN(m)); michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: if (prev != NULL) { michael@0: control->tail_mbuf = prev; michael@0: } else { michael@0: /* Everything got collapsed out?? */ michael@0: sctp_free_remote_addr(control->whoFrom); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control); michael@0: if (inp_read_lock_held == 0) michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: return; michael@0: } michael@0: if (end) { michael@0: control->end_added = 1; michael@0: } michael@0: #if defined(__Userspace__) michael@0: if (inp->recv_callback) { michael@0: if (inp_read_lock_held == 0) michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: if (control->end_added == 1) { michael@0: struct socket *so; michael@0: struct mbuf *m; michael@0: char *buffer; michael@0: struct sctp_rcvinfo rcv; michael@0: union sctp_sockstore addr; michael@0: int flags; michael@0: michael@0: if ((buffer = malloc(control->length)) == NULL) { michael@0: return; michael@0: } michael@0: so = stcb->sctp_socket; michael@0: for (m = control->data; m; m = SCTP_BUF_NEXT(m)) { michael@0: sctp_sbfree(control, control->stcb, &so->so_rcv, m); michael@0: } michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: m_copydata(control->data, 0, control->length, buffer); michael@0: memset(&rcv, 0, sizeof(struct sctp_rcvinfo)); michael@0: rcv.rcv_sid = control->sinfo_stream; michael@0: rcv.rcv_ssn = control->sinfo_ssn; michael@0: rcv.rcv_flags = control->sinfo_flags; michael@0: rcv.rcv_ppid = control->sinfo_ppid; michael@0: rcv.rcv_tsn = control->sinfo_tsn; michael@0: rcv.rcv_cumtsn = control->sinfo_cumtsn; michael@0: rcv.rcv_context = control->sinfo_context; michael@0: rcv.rcv_assoc_id = control->sinfo_assoc_id; michael@0: memset(&addr, 0, sizeof(union sctp_sockstore)); michael@0: switch (control->whoFrom->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: addr.sin = control->whoFrom->ro._l_addr.sin; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: addr.sin6 = control->whoFrom->ro._l_addr.sin6; michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: addr.sconn = control->whoFrom->ro._l_addr.sconn; michael@0: break; michael@0: default: michael@0: addr.sa = control->whoFrom->ro._l_addr.sa; michael@0: break; michael@0: } michael@0: flags = MSG_EOR; michael@0: if (control->spec_flags & M_NOTIFICATION) { michael@0: flags |= MSG_NOTIFICATION; michael@0: } michael@0: inp->recv_callback(so, addr, buffer, control->length, rcv, flags, inp->ulp_info); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: sctp_free_remote_addr(control->whoFrom); michael@0: control->whoFrom = NULL; michael@0: sctp_m_freem(control->data); michael@0: control->data = NULL; michael@0: control->length = 0; michael@0: sctp_free_a_readq(stcb, control); michael@0: } michael@0: return; michael@0: } michael@0: #endif michael@0: TAILQ_INSERT_TAIL(&inp->read_queue, control, next); michael@0: if (inp_read_lock_held == 0) michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: if (inp && inp->sctp_socket) { michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { michael@0: SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket); michael@0: } else { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: so = SCTP_INP_SO(inp); michael@0: if (!so_locked) { michael@0: if (stcb) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: if (stcb) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: } michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: sctp_sorwakeup(inp, inp->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if (!so_locked) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_append_to_readq(struct sctp_inpcb *inp, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_queued_to_read *control, michael@0: struct mbuf *m, michael@0: int end, michael@0: int ctls_cumack, michael@0: struct sockbuf *sb) michael@0: { michael@0: /* michael@0: * A partial delivery API event is underway. OR we are appending on michael@0: * the reassembly queue. michael@0: * michael@0: * If PDAPI this means we need to add m to the end of the data. michael@0: * Increase the length in the control AND increment the sb_cc. michael@0: * Otherwise sb is NULL and all we need to do is put it at the end michael@0: * of the mbuf chain. michael@0: */ michael@0: int len = 0; michael@0: struct mbuf *mm, *tail = NULL, *prev = NULL; michael@0: michael@0: if (inp) { michael@0: SCTP_INP_READ_LOCK(inp); michael@0: } michael@0: if (control == NULL) { michael@0: get_out: michael@0: if (inp) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: } michael@0: return (-1); michael@0: } michael@0: if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: return (0); michael@0: } michael@0: if (control->end_added) { michael@0: /* huh this one is complete? */ michael@0: goto get_out; michael@0: } michael@0: mm = m; michael@0: if (mm == NULL) { michael@0: goto get_out; michael@0: } michael@0: michael@0: while (mm) { michael@0: if (SCTP_BUF_LEN(mm) == 0) { michael@0: /* Skip mbufs with NO lenght */ michael@0: if (prev == NULL) { michael@0: /* First one */ michael@0: m = sctp_m_free(mm); michael@0: mm = m; michael@0: } else { michael@0: SCTP_BUF_NEXT(prev) = sctp_m_free(mm); michael@0: mm = SCTP_BUF_NEXT(prev); michael@0: } michael@0: continue; michael@0: } michael@0: prev = mm; michael@0: len += SCTP_BUF_LEN(mm); michael@0: if (sb) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm)); michael@0: } michael@0: sctp_sballoc(stcb, sb, mm); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: } michael@0: mm = SCTP_BUF_NEXT(mm); michael@0: } michael@0: if (prev) { michael@0: tail = prev; michael@0: } else { michael@0: /* Really there should always be a prev */ michael@0: if (m == NULL) { michael@0: /* Huh nothing left? */ michael@0: #ifdef INVARIANTS michael@0: panic("Nothing left to add?"); michael@0: #else michael@0: goto get_out; michael@0: #endif michael@0: } michael@0: tail = m; michael@0: } michael@0: if (control->tail_mbuf) { michael@0: /* append */ michael@0: SCTP_BUF_NEXT(control->tail_mbuf) = m; michael@0: control->tail_mbuf = tail; michael@0: } else { michael@0: /* nothing there */ michael@0: #ifdef INVARIANTS michael@0: if (control->data != NULL) { michael@0: panic("This should NOT happen"); michael@0: } michael@0: #endif michael@0: control->data = m; michael@0: control->tail_mbuf = tail; michael@0: } michael@0: atomic_add_int(&control->length, len); michael@0: if (end) { michael@0: /* message is complete */ michael@0: if (stcb && (control == stcb->asoc.control_pdapi)) { michael@0: stcb->asoc.control_pdapi = NULL; michael@0: } michael@0: control->held_length = 0; michael@0: control->end_added = 1; michael@0: } michael@0: if (stcb == NULL) { michael@0: control->do_not_ref_stcb = 1; michael@0: } michael@0: /* michael@0: * When we are appending in partial delivery, the cum-ack is used michael@0: * for the actual pd-api highest tsn on this mbuf. The true cum-ack michael@0: * is populated in the outbound sinfo structure from the true cumack michael@0: * if the association exists... michael@0: */ michael@0: control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack; michael@0: #if defined(__Userspace__) michael@0: if (inp->recv_callback) { michael@0: uint32_t pd_point, length; michael@0: michael@0: length = control->length; michael@0: if (stcb != NULL && stcb->sctp_socket != NULL) { michael@0: pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT, michael@0: stcb->sctp_ep->partial_delivery_point); michael@0: } else { michael@0: pd_point = inp->partial_delivery_point; michael@0: } michael@0: if ((control->end_added == 1) || (length >= pd_point)) { michael@0: struct socket *so; michael@0: char *buffer; michael@0: struct sctp_rcvinfo rcv; michael@0: union sctp_sockstore addr; michael@0: int flags; michael@0: michael@0: if ((buffer = malloc(control->length)) == NULL) { michael@0: return (-1); michael@0: } michael@0: so = stcb->sctp_socket; michael@0: for (m = control->data; m; m = SCTP_BUF_NEXT(m)) { michael@0: sctp_sbfree(control, control->stcb, &so->so_rcv, m); michael@0: } michael@0: m_copydata(control->data, 0, control->length, buffer); michael@0: memset(&rcv, 0, sizeof(struct sctp_rcvinfo)); michael@0: rcv.rcv_sid = control->sinfo_stream; michael@0: rcv.rcv_ssn = control->sinfo_ssn; michael@0: rcv.rcv_flags = control->sinfo_flags; michael@0: rcv.rcv_ppid = control->sinfo_ppid; michael@0: rcv.rcv_tsn = control->sinfo_tsn; michael@0: rcv.rcv_cumtsn = control->sinfo_cumtsn; michael@0: rcv.rcv_context = control->sinfo_context; michael@0: rcv.rcv_assoc_id = control->sinfo_assoc_id; michael@0: memset(&addr, 0, sizeof(union sctp_sockstore)); michael@0: switch (control->whoFrom->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: addr.sin = control->whoFrom->ro._l_addr.sin; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: addr.sin6 = control->whoFrom->ro._l_addr.sin6; michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: addr.sconn = control->whoFrom->ro._l_addr.sconn; michael@0: break; michael@0: default: michael@0: addr.sa = control->whoFrom->ro._l_addr.sa; michael@0: break; michael@0: } michael@0: flags = 0; michael@0: if (control->end_added == 1) { michael@0: flags |= MSG_EOR; michael@0: } michael@0: if (control->spec_flags & M_NOTIFICATION) { michael@0: flags |= MSG_NOTIFICATION; michael@0: } michael@0: sctp_m_freem(control->data); michael@0: control->data = NULL; michael@0: control->tail_mbuf = NULL; michael@0: control->length = 0; michael@0: if (control->end_added) { michael@0: sctp_free_remote_addr(control->whoFrom); michael@0: control->whoFrom = NULL; michael@0: sctp_free_a_readq(stcb, control); michael@0: } else { michael@0: control->some_taken = 1; michael@0: } michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: inp->recv_callback(so, addr, buffer, length, rcv, flags, inp->ulp_info); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: } michael@0: if (inp) michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: return (0); michael@0: } michael@0: #endif michael@0: if (inp) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: } michael@0: if (inp && inp->sctp_socket) { michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { michael@0: SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket); michael@0: } else { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: so = SCTP_INP_SO(inp); michael@0: if (stcb) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: if (stcb) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: } michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return (0); michael@0: } michael@0: #endif michael@0: sctp_sorwakeup(inp, inp->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: michael@0: /*************HOLD THIS COMMENT FOR PATCH FILE OF michael@0: *************ALTERNATE ROUTING CODE michael@0: */ michael@0: michael@0: /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF michael@0: *************ALTERNATE ROUTING CODE michael@0: */ michael@0: michael@0: struct mbuf * michael@0: sctp_generate_invmanparam(int err) michael@0: { michael@0: /* Return a MBUF with a invalid mandatory parameter */ michael@0: struct mbuf *m; michael@0: michael@0: m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA); michael@0: if (m) { michael@0: struct sctp_paramhdr *ph; michael@0: michael@0: SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr); michael@0: ph = mtod(m, struct sctp_paramhdr *); michael@0: ph->param_length = htons(sizeof(struct sctp_paramhdr)); michael@0: ph->param_type = htons(err); michael@0: } michael@0: return (m); michael@0: } michael@0: michael@0: #ifdef SCTP_MBCNT_LOGGING michael@0: void michael@0: sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc, michael@0: struct sctp_tmit_chunk *tp1, int chk_cnt) michael@0: { michael@0: if (tp1->data == NULL) { michael@0: return; michael@0: } michael@0: asoc->chunks_on_out_queue -= chk_cnt; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) { michael@0: sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE, michael@0: asoc->total_output_queue_size, michael@0: tp1->book_size, michael@0: 0, michael@0: tp1->mbcnt); michael@0: } michael@0: if (asoc->total_output_queue_size >= tp1->book_size) { michael@0: atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size); michael@0: } else { michael@0: asoc->total_output_queue_size = 0; michael@0: } michael@0: michael@0: if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) || michael@0: ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) { michael@0: if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) { michael@0: stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size; michael@0: } else { michael@0: stcb->sctp_socket->so_snd.sb_cc = 0; michael@0: michael@0: } michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: michael@0: int michael@0: sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1, michael@0: uint8_t sent, int so_locked michael@0: #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_UNUSED michael@0: #endif michael@0: ) michael@0: { michael@0: struct sctp_stream_out *strq; michael@0: struct sctp_tmit_chunk *chk = NULL, *tp2; michael@0: struct sctp_stream_queue_pending *sp; michael@0: uint16_t stream = 0, seq = 0; michael@0: uint8_t foundeom = 0; michael@0: int ret_sz = 0; michael@0: int notdone; michael@0: int do_wakeup_routine = 0; michael@0: #if defined(__APPLE__) michael@0: if (so_locked) { michael@0: sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } else { michael@0: sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep)); michael@0: } michael@0: #endif michael@0: stream = tp1->rec.data.stream_number; michael@0: seq = tp1->rec.data.stream_seq; michael@0: do { michael@0: ret_sz += tp1->book_size; michael@0: if (tp1->data != NULL) { michael@0: if (tp1->sent < SCTP_DATAGRAM_RESEND) { michael@0: sctp_flight_size_decrease(tp1); michael@0: sctp_total_flight_decrease(stcb, tp1); michael@0: } michael@0: sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1); michael@0: stcb->asoc.peers_rwnd += tp1->send_size; michael@0: stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh); michael@0: if (sent) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked); michael@0: } else { michael@0: sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked); michael@0: } michael@0: if (tp1->data) { michael@0: sctp_m_freem(tp1->data); michael@0: tp1->data = NULL; michael@0: } michael@0: do_wakeup_routine = 1; michael@0: if (PR_SCTP_BUF_ENABLED(tp1->flags)) { michael@0: stcb->asoc.sent_queue_cnt_removeable--; michael@0: } michael@0: } michael@0: tp1->sent = SCTP_FORWARD_TSN_SKIP; michael@0: if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) == michael@0: SCTP_DATA_NOT_FRAG) { michael@0: /* not frag'ed we ae done */ michael@0: notdone = 0; michael@0: foundeom = 1; michael@0: } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) { michael@0: /* end of frag, we are done */ michael@0: notdone = 0; michael@0: foundeom = 1; michael@0: } else { michael@0: /* michael@0: * Its a begin or middle piece, we must mark all of michael@0: * it michael@0: */ michael@0: notdone = 1; michael@0: tp1 = TAILQ_NEXT(tp1, sctp_next); michael@0: } michael@0: } while (tp1 && notdone); michael@0: if (foundeom == 0) { michael@0: /* michael@0: * The multi-part message was scattered across the send and michael@0: * sent queue. michael@0: */ michael@0: TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) { michael@0: if ((tp1->rec.data.stream_number != stream) || michael@0: (tp1->rec.data.stream_seq != seq)) { michael@0: break; michael@0: } michael@0: /* save to chk in case we have some on stream out michael@0: * queue. If so and we have an un-transmitted one michael@0: * we don't have to fudge the TSN. michael@0: */ michael@0: chk = tp1; michael@0: ret_sz += tp1->book_size; michael@0: sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1); michael@0: if (sent) { michael@0: sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked); michael@0: } else { michael@0: sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked); michael@0: } michael@0: if (tp1->data) { michael@0: sctp_m_freem(tp1->data); michael@0: tp1->data = NULL; michael@0: } michael@0: /* No flight involved here book the size to 0 */ michael@0: tp1->book_size = 0; michael@0: if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) { michael@0: foundeom = 1; michael@0: } michael@0: do_wakeup_routine = 1; michael@0: tp1->sent = SCTP_FORWARD_TSN_SKIP; michael@0: TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next); michael@0: /* on to the sent queue so we can wait for it to be passed by. */ michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1, michael@0: sctp_next); michael@0: stcb->asoc.send_queue_cnt--; michael@0: stcb->asoc.sent_queue_cnt++; michael@0: } michael@0: } michael@0: if (foundeom == 0) { michael@0: /* michael@0: * Still no eom found. That means there michael@0: * is stuff left on the stream out queue.. yuck. michael@0: */ michael@0: SCTP_TCB_SEND_LOCK(stcb); michael@0: strq = &stcb->asoc.strmout[stream]; michael@0: sp = TAILQ_FIRST(&strq->outqueue); michael@0: if (sp != NULL) { michael@0: sp->discard_rest = 1; michael@0: /* michael@0: * We may need to put a chunk on the michael@0: * queue that holds the TSN that michael@0: * would have been sent with the LAST michael@0: * bit. michael@0: */ michael@0: if (chk == NULL) { michael@0: /* Yep, we have to */ michael@0: sctp_alloc_a_chunk(stcb, chk); michael@0: if (chk == NULL) { michael@0: /* we are hosed. All we can michael@0: * do is nothing.. which will michael@0: * cause an abort if the peer is michael@0: * paying attention. michael@0: */ michael@0: goto oh_well; michael@0: } michael@0: memset(chk, 0, sizeof(*chk)); michael@0: chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG; michael@0: chk->sent = SCTP_FORWARD_TSN_SKIP; michael@0: chk->asoc = &stcb->asoc; michael@0: chk->rec.data.stream_seq = strq->next_sequence_send; michael@0: chk->rec.data.stream_number = sp->stream; michael@0: chk->rec.data.payloadtype = sp->ppid; michael@0: chk->rec.data.context = sp->context; michael@0: chk->flags = sp->act_flags; michael@0: if (sp->net) michael@0: chk->whoTo = sp->net; michael@0: else michael@0: chk->whoTo = stcb->asoc.primary_destination; michael@0: atomic_add_int(&chk->whoTo->ref_count, 1); michael@0: #if defined(__FreeBSD__) || defined(__Panda__) michael@0: chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1); michael@0: #else michael@0: chk->rec.data.TSN_seq = stcb->asoc.sending_seq++; michael@0: #endif michael@0: stcb->asoc.pr_sctp_cnt++; michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next); michael@0: stcb->asoc.sent_queue_cnt++; michael@0: stcb->asoc.pr_sctp_cnt++; michael@0: } else { michael@0: chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG; michael@0: } michael@0: strq->next_sequence_send++; michael@0: oh_well: michael@0: if (sp->data) { michael@0: /* Pull any data to free up the SB and michael@0: * allow sender to "add more" while we michael@0: * will throw away :-) michael@0: */ michael@0: sctp_free_spbufspace(stcb, &stcb->asoc, sp); michael@0: ret_sz += sp->length; michael@0: do_wakeup_routine = 1; michael@0: sp->some_taken = 1; michael@0: sctp_m_freem(sp->data); michael@0: sp->data = NULL; michael@0: sp->tail_mbuf = NULL; michael@0: sp->length = 0; michael@0: } michael@0: } michael@0: SCTP_TCB_SEND_UNLOCK(stcb); michael@0: } michael@0: if (do_wakeup_routine) { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: so = SCTP_INP_SO(stcb->sctp_ep); michael@0: if (!so_locked) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { michael@0: /* assoc was freed while we were unlocked */ michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: return (ret_sz); michael@0: } michael@0: } michael@0: #endif michael@0: sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: if (!so_locked) { michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: } michael@0: #endif michael@0: } michael@0: return (ret_sz); michael@0: } michael@0: michael@0: /* michael@0: * checks to see if the given address, sa, is one that is currently known by michael@0: * the kernel note: can't distinguish the same address on multiple interfaces michael@0: * and doesn't handle multiple addresses with different zone/scope id's note: michael@0: * ifa_ifwithaddr() compares the entire sockaddr struct michael@0: */ michael@0: struct sctp_ifa * michael@0: sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr, michael@0: int holds_lock) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: michael@0: if (holds_lock == 0) { michael@0: SCTP_INP_RLOCK(inp); michael@0: } michael@0: michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == NULL) michael@0: continue; michael@0: if (addr->sa_family != laddr->ifa->address.sa.sa_family) michael@0: continue; michael@0: #ifdef INET michael@0: if (addr->sa_family == AF_INET) { michael@0: if (((struct sockaddr_in *)addr)->sin_addr.s_addr == michael@0: laddr->ifa->address.sin.sin_addr.s_addr) { michael@0: /* found him. */ michael@0: if (holds_lock == 0) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: return (laddr->ifa); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (addr->sa_family == AF_INET6) { michael@0: if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr, michael@0: &laddr->ifa->address.sin6)) { michael@0: /* found him. */ michael@0: if (holds_lock == 0) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: return (laddr->ifa); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: if (addr->sa_family == AF_CONN) { michael@0: if (((struct sockaddr_conn *)addr)->sconn_addr == laddr->ifa->address.sconn.sconn_addr) { michael@0: /* found him. */ michael@0: if (holds_lock == 0) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: return (laddr->ifa); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: if (holds_lock == 0) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: uint32_t michael@0: sctp_get_ifa_hash_val(struct sockaddr *addr) michael@0: { michael@0: switch (addr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct sockaddr_in *sin; michael@0: michael@0: sin = (struct sockaddr_in *)addr; michael@0: return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16)); michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sockaddr_in6 *sin6; michael@0: uint32_t hash_of_addr; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)addr; michael@0: #if !defined(__Windows__) && !defined(__Userspace_os_FreeBSD) && !defined(__Userspace_os_Darwin) && !defined(__Userspace_os_Windows) michael@0: hash_of_addr = (sin6->sin6_addr.s6_addr32[0] + michael@0: sin6->sin6_addr.s6_addr32[1] + michael@0: sin6->sin6_addr.s6_addr32[2] + michael@0: sin6->sin6_addr.s6_addr32[3]); michael@0: #else michael@0: hash_of_addr = (((uint32_t *)&sin6->sin6_addr)[0] + michael@0: ((uint32_t *)&sin6->sin6_addr)[1] + michael@0: ((uint32_t *)&sin6->sin6_addr)[2] + michael@0: ((uint32_t *)&sin6->sin6_addr)[3]); michael@0: #endif michael@0: hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16)); michael@0: return (hash_of_addr); michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: { michael@0: struct sockaddr_conn *sconn; michael@0: uintptr_t temp; michael@0: michael@0: sconn = (struct sockaddr_conn *)addr; michael@0: temp = (uintptr_t)sconn->sconn_addr; michael@0: return ((uint32_t)(temp ^ (temp >> 16))); michael@0: } michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: struct sctp_ifa * michael@0: sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock) michael@0: { michael@0: struct sctp_ifa *sctp_ifap; michael@0: struct sctp_vrf *vrf; michael@0: struct sctp_ifalist *hash_head; michael@0: uint32_t hash_of_addr; michael@0: michael@0: if (holds_lock == 0) michael@0: SCTP_IPI_ADDR_RLOCK(); michael@0: michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) { michael@0: stage_right: michael@0: if (holds_lock == 0) michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (NULL); michael@0: } michael@0: michael@0: hash_of_addr = sctp_get_ifa_hash_val(addr); michael@0: michael@0: hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; michael@0: if (hash_head == NULL) { michael@0: SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ", michael@0: hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark, michael@0: (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark)); michael@0: sctp_print_address(addr); michael@0: SCTP_PRINTF("No such bucket for address\n"); michael@0: if (holds_lock == 0) michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: michael@0: return (NULL); michael@0: } michael@0: LIST_FOREACH(sctp_ifap, hash_head, next_bucket) { michael@0: if (sctp_ifap == NULL) { michael@0: #ifdef INVARIANTS michael@0: panic("Huh LIST_FOREACH corrupt"); michael@0: goto stage_right; michael@0: #else michael@0: SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n"); michael@0: goto stage_right; michael@0: #endif michael@0: } michael@0: if (addr->sa_family != sctp_ifap->address.sa.sa_family) michael@0: continue; michael@0: #ifdef INET michael@0: if (addr->sa_family == AF_INET) { michael@0: if (((struct sockaddr_in *)addr)->sin_addr.s_addr == michael@0: sctp_ifap->address.sin.sin_addr.s_addr) { michael@0: /* found him. */ michael@0: if (holds_lock == 0) michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (sctp_ifap); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (addr->sa_family == AF_INET6) { michael@0: if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr, michael@0: &sctp_ifap->address.sin6)) { michael@0: /* found him. */ michael@0: if (holds_lock == 0) michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (sctp_ifap); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: if (addr->sa_family == AF_CONN) { michael@0: if (((struct sockaddr_conn *)addr)->sconn_addr == sctp_ifap->address.sconn.sconn_addr) { michael@0: /* found him. */ michael@0: if (holds_lock == 0) michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (sctp_ifap); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: if (holds_lock == 0) michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (NULL); michael@0: } michael@0: michael@0: static void michael@0: sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock, michael@0: uint32_t rwnd_req) michael@0: { michael@0: /* User pulled some data, do we need a rwnd update? */ michael@0: int r_unlocked = 0; michael@0: uint32_t dif, rwnd; michael@0: struct socket *so = NULL; michael@0: michael@0: if (stcb == NULL) michael@0: return; michael@0: michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: michael@0: if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | michael@0: SCTP_STATE_SHUTDOWN_RECEIVED | michael@0: SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: /* Pre-check If we are freeing no update */ michael@0: goto no_lock; michael@0: } michael@0: SCTP_INP_INCR_REF(stcb->sctp_ep); michael@0: if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { michael@0: goto out; michael@0: } michael@0: so = stcb->sctp_socket; michael@0: if (so == NULL) { michael@0: goto out; michael@0: } michael@0: atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far); michael@0: /* Have you have freed enough to look */ michael@0: *freed_so_far = 0; michael@0: /* Yep, its worth a look and the lock overhead */ michael@0: michael@0: /* Figure out what the rwnd would be */ michael@0: rwnd = sctp_calc_rwnd(stcb, &stcb->asoc); michael@0: if (rwnd >= stcb->asoc.my_last_reported_rwnd) { michael@0: dif = rwnd - stcb->asoc.my_last_reported_rwnd; michael@0: } else { michael@0: dif = 0; michael@0: } michael@0: if (dif >= rwnd_req) { michael@0: if (hold_rlock) { michael@0: SCTP_INP_READ_UNLOCK(stcb->sctp_ep); michael@0: r_unlocked = 1; michael@0: } michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: /* michael@0: * One last check before we allow the guy possibly michael@0: * to get in. There is a race, where the guy has not michael@0: * reached the gate. In that case michael@0: */ michael@0: goto out; michael@0: } michael@0: SCTP_TCB_LOCK(stcb); michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: /* No reports here */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: goto out; michael@0: } michael@0: SCTP_STAT_INCR(sctps_wu_sacks_sent); michael@0: sctp_send_sack(stcb, SCTP_SO_LOCKED); michael@0: michael@0: sctp_chunk_output(stcb->sctp_ep, stcb, michael@0: SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED); michael@0: /* make sure no timer is running */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL+SCTP_LOC_6); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } else { michael@0: /* Update how much we have pending */ michael@0: stcb->freed_by_sorcv_sincelast = dif; michael@0: } michael@0: out: michael@0: if (so && r_unlocked && hold_rlock) { michael@0: SCTP_INP_READ_LOCK(stcb->sctp_ep); michael@0: } michael@0: michael@0: SCTP_INP_DECR_REF(stcb->sctp_ep); michael@0: no_lock: michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: return; michael@0: } michael@0: michael@0: int michael@0: sctp_sorecvmsg(struct socket *so, michael@0: struct uio *uio, michael@0: struct mbuf **mp, michael@0: struct sockaddr *from, michael@0: int fromlen, michael@0: int *msg_flags, michael@0: struct sctp_sndrcvinfo *sinfo, michael@0: int filling_sinfo) michael@0: { michael@0: /* michael@0: * MSG flags we will look at MSG_DONTWAIT - non-blocking IO. michael@0: * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy michael@0: * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ?? michael@0: * On the way out we may send out any combination of: michael@0: * MSG_NOTIFICATION MSG_EOR michael@0: * michael@0: */ michael@0: struct sctp_inpcb *inp = NULL; michael@0: int my_len = 0; michael@0: int cp_len = 0, error = 0; michael@0: struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL; michael@0: struct mbuf *m = NULL; michael@0: struct sctp_tcb *stcb = NULL; michael@0: int wakeup_read_socket = 0; michael@0: int freecnt_applied = 0; michael@0: int out_flags = 0, in_flags = 0; michael@0: int block_allowed = 1; michael@0: uint32_t freed_so_far = 0; michael@0: uint32_t copied_so_far = 0; michael@0: int in_eeor_mode = 0; michael@0: int no_rcv_needed = 0; michael@0: uint32_t rwnd_req = 0; michael@0: int hold_sblock = 0; michael@0: int hold_rlock = 0; michael@0: int slen = 0; michael@0: uint32_t held_length = 0; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 700000 michael@0: int sockbuf_lock = 0; michael@0: #endif michael@0: michael@0: if (uio == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: michael@0: if (msg_flags) { michael@0: in_flags = *msg_flags; michael@0: if (in_flags & MSG_PEEK) michael@0: SCTP_STAT_INCR(sctps_read_peeks); michael@0: } else { michael@0: in_flags = 0; michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: slen = uio->uio_resid; michael@0: #else michael@0: slen = uio_resid(uio); michael@0: #endif michael@0: #else michael@0: slen = uio->uio_resid; michael@0: #endif michael@0: michael@0: /* Pull in and set up our int flags */ michael@0: if (in_flags & MSG_OOB) { michael@0: /* Out of band's NOT supported */ michael@0: return (EOPNOTSUPP); michael@0: } michael@0: if ((in_flags & MSG_PEEK) && (mp != NULL)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: if ((in_flags & (MSG_DONTWAIT michael@0: #if defined(__FreeBSD__) && __FreeBSD_version > 500000 michael@0: | MSG_NBIO michael@0: #endif michael@0: )) || michael@0: SCTP_SO_IS_NBIO(so)) { michael@0: block_allowed = 0; michael@0: } michael@0: /* setup the endpoint */ michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT); michael@0: return (EFAULT); michael@0: } michael@0: rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT); michael@0: /* Must be at least a MTU's worth */ michael@0: if (rwnd_req < SCTP_MIN_RWND) michael@0: rwnd_req = SCTP_MIN_RWND; michael@0: in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) { michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: sctp_misc_ints(SCTP_SORECV_ENTER, michael@0: rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid); michael@0: #else michael@0: sctp_misc_ints(SCTP_SORECV_ENTER, michael@0: rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio_resid(uio)); michael@0: #endif michael@0: #else michael@0: sctp_misc_ints(SCTP_SORECV_ENTER, michael@0: rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid); michael@0: #endif michael@0: } michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) || defined(__Userspace__) michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: hold_sblock = 1; michael@0: #endif michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) { michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: sctp_misc_ints(SCTP_SORECV_ENTERPL, michael@0: rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid); michael@0: #else michael@0: sctp_misc_ints(SCTP_SORECV_ENTERPL, michael@0: rwnd_req, block_allowed, so->so_rcv.sb_cc, uio_resid(uio)); michael@0: #endif michael@0: #else michael@0: sctp_misc_ints(SCTP_SORECV_ENTERPL, michael@0: rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid); michael@0: #endif michael@0: } michael@0: michael@0: #if defined(__APPLE__) michael@0: error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags)); michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) michael@0: error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0)); michael@0: #endif michael@0: if (error) { michael@0: goto release_unlocked; michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 700000 michael@0: sockbuf_lock = 1; michael@0: #endif michael@0: restart: michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) || defined(__Userspace__) michael@0: if (hold_sblock == 0) { michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: hold_sblock = 1; michael@0: } michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: sbunlock(&so->so_rcv, 1); michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 700000 michael@0: sbunlock(&so->so_rcv); michael@0: #endif michael@0: michael@0: restart_nosblocks: michael@0: if (hold_sblock == 0) { michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: hold_sblock = 1; michael@0: } michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { michael@0: goto out; michael@0: } michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__) michael@0: if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) { michael@0: #else michael@0: if ((so->so_state & SS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) { michael@0: #endif michael@0: if (so->so_error) { michael@0: error = so->so_error; michael@0: if ((in_flags & MSG_PEEK) == 0) michael@0: so->so_error = 0; michael@0: goto out; michael@0: } else { michael@0: if (so->so_rcv.sb_cc == 0) { michael@0: /* indicate EOF */ michael@0: error = 0; michael@0: goto out; michael@0: } michael@0: } michael@0: } michael@0: if ((so->so_rcv.sb_cc <= held_length) && block_allowed) { michael@0: /* we need to wait for data */ michael@0: if ((so->so_rcv.sb_cc == 0) && michael@0: ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { michael@0: /* For active open side clear flags for re-use michael@0: * passive open is blocked by connect. michael@0: */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) { michael@0: /* You were aborted, passive side always hits here */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET); michael@0: error = ECONNRESET; michael@0: } michael@0: so->so_state &= ~(SS_ISCONNECTING | michael@0: SS_ISDISCONNECTING | michael@0: SS_ISCONFIRMING | michael@0: SS_ISCONNECTED); michael@0: if (error == 0) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN); michael@0: error = ENOTCONN; michael@0: } michael@0: } michael@0: goto out; michael@0: } michael@0: } michael@0: error = sbwait(&so->so_rcv); michael@0: if (error) { michael@0: goto out; michael@0: } michael@0: held_length = 0; michael@0: goto restart_nosblocks; michael@0: } else if (so->so_rcv.sb_cc == 0) { michael@0: if (so->so_error) { michael@0: error = so->so_error; michael@0: if ((in_flags & MSG_PEEK) == 0) michael@0: so->so_error = 0; michael@0: } else { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { michael@0: /* For active open side clear flags for re-use michael@0: * passive open is blocked by connect. michael@0: */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) { michael@0: /* You were aborted, passive side always hits here */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET); michael@0: error = ECONNRESET; michael@0: } michael@0: so->so_state &= ~(SS_ISCONNECTING | michael@0: SS_ISDISCONNECTING | michael@0: SS_ISCONFIRMING | michael@0: SS_ISCONNECTED); michael@0: if (error == 0) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN); michael@0: error = ENOTCONN; michael@0: } michael@0: } michael@0: goto out; michael@0: } michael@0: } michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK); michael@0: error = EWOULDBLOCK; michael@0: } michael@0: goto out; michael@0: } michael@0: if (hold_sblock == 1) { michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: hold_sblock = 0; michael@0: } michael@0: #if defined(__APPLE__) michael@0: error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags)); michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 700000 michael@0: error = sblock(&so->so_rcv, (block_allowed ? M_WAITOK : 0)); michael@0: #endif michael@0: /* we possibly have data we can read */ michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: control = TAILQ_FIRST(&inp->read_queue); michael@0: if (control == NULL) { michael@0: /* This could be happening since michael@0: * the appender did the increment but as not michael@0: * yet did the tailq insert onto the read_queue michael@0: */ michael@0: if (hold_rlock == 0) { michael@0: SCTP_INP_READ_LOCK(inp); michael@0: } michael@0: control = TAILQ_FIRST(&inp->read_queue); michael@0: if ((control == NULL) && (so->so_rcv.sb_cc != 0)) { michael@0: #ifdef INVARIANTS michael@0: panic("Huh, its non zero and nothing on control?"); michael@0: #endif michael@0: so->so_rcv.sb_cc = 0; michael@0: } michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: hold_rlock = 0; michael@0: goto restart; michael@0: } michael@0: michael@0: if ((control->length == 0) && michael@0: (control->do_not_ref_stcb)) { michael@0: /* Clean up code for freeing assoc that left behind a pdapi.. michael@0: * maybe a peer in EEOR that just closed after sending and michael@0: * never indicated a EOR. michael@0: */ michael@0: if (hold_rlock == 0) { michael@0: hold_rlock = 1; michael@0: SCTP_INP_READ_LOCK(inp); michael@0: } michael@0: control->held_length = 0; michael@0: if (control->data) { michael@0: /* Hmm there is data here .. fix */ michael@0: struct mbuf *m_tmp; michael@0: int cnt = 0; michael@0: m_tmp = control->data; michael@0: while (m_tmp) { michael@0: cnt += SCTP_BUF_LEN(m_tmp); michael@0: if (SCTP_BUF_NEXT(m_tmp) == NULL) { michael@0: control->tail_mbuf = m_tmp; michael@0: control->end_added = 1; michael@0: } michael@0: m_tmp = SCTP_BUF_NEXT(m_tmp); michael@0: } michael@0: control->length = cnt; michael@0: } else { michael@0: /* remove it */ michael@0: TAILQ_REMOVE(&inp->read_queue, control, next); michael@0: /* Add back any hiddend data */ michael@0: sctp_free_remote_addr(control->whoFrom); michael@0: sctp_free_a_readq(stcb, control); michael@0: } michael@0: if (hold_rlock) { michael@0: hold_rlock = 0; michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: } michael@0: goto restart; michael@0: } michael@0: if ((control->length == 0) && michael@0: (control->end_added == 1)) { michael@0: /* Do we also need to check for (control->pdapi_aborted == 1)? */ michael@0: if (hold_rlock == 0) { michael@0: hold_rlock = 1; michael@0: SCTP_INP_READ_LOCK(inp); michael@0: } michael@0: TAILQ_REMOVE(&inp->read_queue, control, next); michael@0: if (control->data) { michael@0: #ifdef INVARIANTS michael@0: panic("control->data not null but control->length == 0"); michael@0: #else michael@0: SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n"); michael@0: sctp_m_freem(control->data); michael@0: control->data = NULL; michael@0: #endif michael@0: } michael@0: if (control->aux_data) { michael@0: sctp_m_free (control->aux_data); michael@0: control->aux_data = NULL; michael@0: } michael@0: sctp_free_remote_addr(control->whoFrom); michael@0: sctp_free_a_readq(stcb, control); michael@0: if (hold_rlock) { michael@0: hold_rlock = 0; michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: } michael@0: goto restart; michael@0: } michael@0: if (control->length == 0) { michael@0: if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) && michael@0: (filling_sinfo)) { michael@0: /* find a more suitable one then this */ michael@0: ctl = TAILQ_NEXT(control, next); michael@0: while (ctl) { michael@0: if ((ctl->stcb != control->stcb) && (ctl->length) && michael@0: (ctl->some_taken || michael@0: (ctl->spec_flags & M_NOTIFICATION) || michael@0: ((ctl->do_not_ref_stcb == 0) && michael@0: (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0))) michael@0: ) { michael@0: /*- michael@0: * If we have a different TCB next, and there is data michael@0: * present. If we have already taken some (pdapi), OR we can michael@0: * ref the tcb and no delivery as started on this stream, we michael@0: * take it. Note we allow a notification on a different michael@0: * assoc to be delivered.. michael@0: */ michael@0: control = ctl; michael@0: goto found_one; michael@0: } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) && michael@0: (ctl->length) && michael@0: ((ctl->some_taken) || michael@0: ((ctl->do_not_ref_stcb == 0) && michael@0: ((ctl->spec_flags & M_NOTIFICATION) == 0) && michael@0: (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) { michael@0: /*- michael@0: * If we have the same tcb, and there is data present, and we michael@0: * have the strm interleave feature present. Then if we have michael@0: * taken some (pdapi) or we can refer to tht tcb AND we have michael@0: * not started a delivery for this stream, we can take it. michael@0: * Note we do NOT allow a notificaiton on the same assoc to michael@0: * be delivered. michael@0: */ michael@0: control = ctl; michael@0: goto found_one; michael@0: } michael@0: ctl = TAILQ_NEXT(ctl, next); michael@0: } michael@0: } michael@0: /* michael@0: * if we reach here, not suitable replacement is available michael@0: * fragment interleave is NOT on. So stuff the sb_cc michael@0: * into the our held count, and its time to sleep again. michael@0: */ michael@0: held_length = so->so_rcv.sb_cc; michael@0: control->held_length = so->so_rcv.sb_cc; michael@0: goto restart; michael@0: } michael@0: /* Clear the held length since there is something to read */ michael@0: control->held_length = 0; michael@0: if (hold_rlock) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: hold_rlock = 0; michael@0: } michael@0: found_one: michael@0: /* michael@0: * If we reach here, control has a some data for us to read off. michael@0: * Note that stcb COULD be NULL. michael@0: */ michael@0: control->some_taken++; michael@0: if (hold_sblock) { michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: hold_sblock = 0; michael@0: } michael@0: stcb = control->stcb; michael@0: if (stcb) { michael@0: if ((control->do_not_ref_stcb == 0) && michael@0: (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { michael@0: if (freecnt_applied == 0) michael@0: stcb = NULL; michael@0: } else if (control->do_not_ref_stcb == 0) { michael@0: /* you can't free it on me please */ michael@0: /* michael@0: * The lock on the socket buffer protects us so the michael@0: * free code will stop. But since we used the socketbuf michael@0: * lock and the sender uses the tcb_lock to increment, michael@0: * we need to use the atomic add to the refcnt michael@0: */ michael@0: if (freecnt_applied) { michael@0: #ifdef INVARIANTS michael@0: panic("refcnt already incremented"); michael@0: #else michael@0: SCTP_PRINTF("refcnt already incremented?\n"); michael@0: #endif michael@0: } else { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: freecnt_applied = 1; michael@0: } michael@0: /* michael@0: * Setup to remember how much we have not yet told michael@0: * the peer our rwnd has opened up. Note we grab michael@0: * the value from the tcb from last time. michael@0: * Note too that sack sending clears this when a sack michael@0: * is sent, which is fine. Once we hit the rwnd_req, michael@0: * we then will go to the sctp_user_rcvd() that will michael@0: * not lock until it KNOWs it MUST send a WUP-SACK. michael@0: */ michael@0: freed_so_far = stcb->freed_by_sorcv_sincelast; michael@0: stcb->freed_by_sorcv_sincelast = 0; michael@0: } michael@0: } michael@0: if (stcb && michael@0: ((control->spec_flags & M_NOTIFICATION) == 0) && michael@0: control->do_not_ref_stcb == 0) { michael@0: stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1; michael@0: } michael@0: michael@0: /* First lets get off the sinfo and sockaddr info */ michael@0: if ((sinfo) && filling_sinfo) { michael@0: memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo)); michael@0: nxt = TAILQ_NEXT(control, next); michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) || michael@0: sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) { michael@0: struct sctp_extrcvinfo *s_extra; michael@0: s_extra = (struct sctp_extrcvinfo *)sinfo; michael@0: if ((nxt) && michael@0: (nxt->length)) { michael@0: s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL; michael@0: if (nxt->sinfo_flags & SCTP_UNORDERED) { michael@0: s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED; michael@0: } michael@0: if (nxt->spec_flags & M_NOTIFICATION) { michael@0: s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION; michael@0: } michael@0: s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id; michael@0: s_extra->sreinfo_next_length = nxt->length; michael@0: s_extra->sreinfo_next_ppid = nxt->sinfo_ppid; michael@0: s_extra->sreinfo_next_stream = nxt->sinfo_stream; michael@0: if (nxt->tail_mbuf != NULL) { michael@0: if (nxt->end_added) { michael@0: s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE; michael@0: } michael@0: } michael@0: } else { michael@0: /* we explicitly 0 this, since the memcpy got michael@0: * some other things beyond the older sinfo_ michael@0: * that is on the control's structure :-D michael@0: */ michael@0: nxt = NULL; michael@0: s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG; michael@0: s_extra->sreinfo_next_aid = 0; michael@0: s_extra->sreinfo_next_length = 0; michael@0: s_extra->sreinfo_next_ppid = 0; michael@0: s_extra->sreinfo_next_stream = 0; michael@0: } michael@0: } michael@0: /* michael@0: * update off the real current cum-ack, if we have an stcb. michael@0: */ michael@0: if ((control->do_not_ref_stcb == 0) && stcb) michael@0: sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn; michael@0: /* michael@0: * mask off the high bits, we keep the actual chunk bits in michael@0: * there. michael@0: */ michael@0: sinfo->sinfo_flags &= 0x00ff; michael@0: if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) { michael@0: sinfo->sinfo_flags |= SCTP_UNORDERED; michael@0: } michael@0: } michael@0: #ifdef SCTP_ASOCLOG_OF_TSNS michael@0: { michael@0: int index, newindex; michael@0: struct sctp_pcbtsn_rlog *entry; michael@0: do { michael@0: index = inp->readlog_index; michael@0: newindex = index + 1; michael@0: if (newindex >= SCTP_READ_LOG_SIZE) { michael@0: newindex = 0; michael@0: } michael@0: } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0); michael@0: entry = &inp->readlog[index]; michael@0: entry->vtag = control->sinfo_assoc_id; michael@0: entry->strm = control->sinfo_stream; michael@0: entry->seq = control->sinfo_ssn; michael@0: entry->sz = control->length; michael@0: entry->flgs = control->sinfo_flags; michael@0: } michael@0: #endif michael@0: if (fromlen && from) { michael@0: #ifdef HAVE_SA_LEN michael@0: cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sa.sa_len); michael@0: #endif michael@0: switch (control->whoFrom->ro._l_addr.sa.sa_family) { michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #ifndef HAVE_SA_LEN michael@0: cp_len = min((size_t)fromlen, sizeof(struct sockaddr_in6)); michael@0: #endif michael@0: ((struct sockaddr_in6 *)from)->sin6_port = control->port_from; michael@0: break; michael@0: #endif michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #ifndef HAVE_SA_LEN michael@0: cp_len = min((size_t)fromlen, sizeof(struct sockaddr_in)); michael@0: #endif michael@0: ((struct sockaddr_in *)from)->sin_port = control->port_from; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: #ifndef HAVE_SA_LEN michael@0: cp_len = min((size_t)fromlen, sizeof(struct sockaddr_conn)); michael@0: #endif michael@0: ((struct sockaddr_conn *)from)->sconn_port = control->port_from; michael@0: break; michael@0: #endif michael@0: default: michael@0: #ifndef HAVE_SA_LEN michael@0: cp_len = min((size_t)fromlen, sizeof(struct sockaddr)); michael@0: #endif michael@0: break; michael@0: } michael@0: memcpy(from, &control->whoFrom->ro._l_addr, cp_len); michael@0: michael@0: #if defined(INET) && defined(INET6) michael@0: if ((sctp_is_feature_on(inp,SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) && michael@0: (from->sa_family == AF_INET) && michael@0: ((size_t)fromlen >= sizeof(struct sockaddr_in6))) { michael@0: struct sockaddr_in *sin; michael@0: struct sockaddr_in6 sin6; michael@0: michael@0: sin = (struct sockaddr_in *)from; michael@0: bzero(&sin6, sizeof(sin6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: #if defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_Darwin) || defined(__Userspace_os_Windows) michael@0: ((uint32_t *)&sin6.sin6_addr)[2] = htonl(0xffff); michael@0: bcopy(&sin->sin_addr, michael@0: &(((uint32_t *)&sin6.sin6_addr)[3]), michael@0: sizeof(uint32_t)); michael@0: #elif defined(__Windows__) michael@0: ((uint32_t *)&sin6.sin6_addr)[2] = htonl(0xffff); michael@0: bcopy(&sin->sin_addr, michael@0: &((uint32_t *)&sin6.sin6_addr)[3], michael@0: sizeof(uint32_t)); michael@0: #else michael@0: sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); michael@0: bcopy(&sin->sin_addr, michael@0: &sin6.sin6_addr.s6_addr32[3], michael@0: sizeof(sin6.sin6_addr.s6_addr32[3])); michael@0: #endif michael@0: sin6.sin6_port = sin->sin_port; michael@0: memcpy(from, &sin6, sizeof(struct sockaddr_in6)); michael@0: } michael@0: #endif michael@0: #if defined(SCTP_EMBEDDED_V6_SCOPE) michael@0: #ifdef INET6 michael@0: { michael@0: struct sockaddr_in6 lsa6, *from6; michael@0: michael@0: from6 = (struct sockaddr_in6 *)from; michael@0: sctp_recover_scope_mac(from6, (&lsa6)); michael@0: } michael@0: #endif michael@0: #endif michael@0: } michael@0: /* now copy out what data we can */ michael@0: if (mp == NULL) { michael@0: /* copy out each mbuf in the chain up to length */ michael@0: get_more_data: michael@0: m = control->data; michael@0: while (m) { michael@0: /* Move out all we can */ michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: cp_len = (int)uio->uio_resid; michael@0: #else michael@0: cp_len = (int)uio_resid(uio); michael@0: #endif michael@0: #else michael@0: cp_len = (int)uio->uio_resid; michael@0: #endif michael@0: my_len = (int)SCTP_BUF_LEN(m); michael@0: if (cp_len > my_len) { michael@0: /* not enough in this buf */ michael@0: cp_len = my_len; michael@0: } michael@0: if (hold_rlock) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: hold_rlock = 0; michael@0: } michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(so, 0); michael@0: #endif michael@0: if (cp_len > 0) michael@0: error = uiomove(mtod(m, char *), cp_len, uio); michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(so, 0); michael@0: #endif michael@0: /* re-read */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: goto release; michael@0: } michael@0: michael@0: if ((control->do_not_ref_stcb == 0) && stcb && michael@0: stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: no_rcv_needed = 1; michael@0: } michael@0: if (error) { michael@0: /* error we are out of here */ michael@0: goto release; michael@0: } michael@0: if ((SCTP_BUF_NEXT(m) == NULL) && michael@0: (cp_len >= SCTP_BUF_LEN(m)) && michael@0: ((control->end_added == 0) || michael@0: (control->end_added && michael@0: (TAILQ_NEXT(control, next) == NULL))) michael@0: ) { michael@0: SCTP_INP_READ_LOCK(inp); michael@0: hold_rlock = 1; michael@0: } michael@0: if (cp_len == SCTP_BUF_LEN(m)) { michael@0: if ((SCTP_BUF_NEXT(m)== NULL) && michael@0: (control->end_added)) { michael@0: out_flags |= MSG_EOR; michael@0: if ((control->do_not_ref_stcb == 0) && michael@0: (control->stcb != NULL) && michael@0: ((control->spec_flags & M_NOTIFICATION) == 0)) michael@0: control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; michael@0: } michael@0: if (control->spec_flags & M_NOTIFICATION) { michael@0: out_flags |= MSG_NOTIFICATION; michael@0: } michael@0: /* we ate up the mbuf */ michael@0: if (in_flags & MSG_PEEK) { michael@0: /* just looking */ michael@0: m = SCTP_BUF_NEXT(m); michael@0: copied_so_far += cp_len; michael@0: } else { michael@0: /* dispose of the mbuf */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&so->so_rcv, michael@0: control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m)); michael@0: } michael@0: sctp_sbfree(control, stcb, &so->so_rcv, m); michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&so->so_rcv, michael@0: control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: copied_so_far += cp_len; michael@0: freed_so_far += cp_len; michael@0: freed_so_far += MSIZE; michael@0: atomic_subtract_int(&control->length, cp_len); michael@0: control->data = sctp_m_free(m); michael@0: m = control->data; michael@0: /* been through it all, must hold sb lock ok to null tail */ michael@0: if (control->data == NULL) { michael@0: #ifdef INVARIANTS michael@0: #if !defined(__APPLE__) michael@0: if ((control->end_added == 0) || michael@0: (TAILQ_NEXT(control, next) == NULL)) { michael@0: /* If the end is not added, OR the michael@0: * next is NOT null we MUST have the lock. michael@0: */ michael@0: if (mtx_owned(&inp->inp_rdata_mtx) == 0) { michael@0: panic("Hmm we don't own the lock?"); michael@0: } michael@0: } michael@0: #endif michael@0: #endif michael@0: control->tail_mbuf = NULL; michael@0: #ifdef INVARIANTS michael@0: if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) { michael@0: panic("end_added, nothing left and no MSG_EOR"); michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: } else { michael@0: /* Do we need to trim the mbuf? */ michael@0: if (control->spec_flags & M_NOTIFICATION) { michael@0: out_flags |= MSG_NOTIFICATION; michael@0: } michael@0: if ((in_flags & MSG_PEEK) == 0) { michael@0: SCTP_BUF_RESV_UF(m, cp_len); michael@0: SCTP_BUF_LEN(m) -= cp_len; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, cp_len); michael@0: } michael@0: atomic_subtract_int(&so->so_rcv.sb_cc, cp_len); michael@0: if ((control->do_not_ref_stcb == 0) && michael@0: stcb) { michael@0: atomic_subtract_int(&stcb->asoc.sb_cc, cp_len); michael@0: } michael@0: copied_so_far += cp_len; michael@0: freed_so_far += cp_len; michael@0: freed_so_far += MSIZE; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb, michael@0: SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: atomic_subtract_int(&control->length, cp_len); michael@0: } else { michael@0: copied_so_far += cp_len; michael@0: } michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) { michael@0: #else michael@0: if ((out_flags & MSG_EOR) || (uio_resid(uio) == 0)) { michael@0: #endif michael@0: #else michael@0: if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) { michael@0: #endif michael@0: break; michael@0: } michael@0: if (((stcb) && (in_flags & MSG_PEEK) == 0) && michael@0: (control->do_not_ref_stcb == 0) && michael@0: (freed_so_far >= rwnd_req)) { michael@0: sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); michael@0: } michael@0: } /* end while(m) */ michael@0: /* michael@0: * At this point we have looked at it all and we either have michael@0: * a MSG_EOR/or read all the user wants... michael@0: * control->length == 0. michael@0: */ michael@0: if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) { michael@0: /* we are done with this control */ michael@0: if (control->length == 0) { michael@0: if (control->data) { michael@0: #ifdef INVARIANTS michael@0: panic("control->data not null at read eor?"); michael@0: #else michael@0: SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n"); michael@0: sctp_m_freem(control->data); michael@0: control->data = NULL; michael@0: #endif michael@0: } michael@0: done_with_control: michael@0: if (TAILQ_NEXT(control, next) == NULL) { michael@0: /* If we don't have a next we need a michael@0: * lock, if there is a next interrupt michael@0: * is filling ahead of us and we don't michael@0: * need a lock to remove this guy michael@0: * (which is the head of the queue). michael@0: */ michael@0: if (hold_rlock == 0) { michael@0: SCTP_INP_READ_LOCK(inp); michael@0: hold_rlock = 1; michael@0: } michael@0: } michael@0: TAILQ_REMOVE(&inp->read_queue, control, next); michael@0: /* Add back any hiddend data */ michael@0: if (control->held_length) { michael@0: held_length = 0; michael@0: control->held_length = 0; michael@0: wakeup_read_socket = 1; michael@0: } michael@0: if (control->aux_data) { michael@0: sctp_m_free (control->aux_data); michael@0: control->aux_data = NULL; michael@0: } michael@0: no_rcv_needed = control->do_not_ref_stcb; michael@0: sctp_free_remote_addr(control->whoFrom); michael@0: control->data = NULL; michael@0: sctp_free_a_readq(stcb, control); michael@0: control = NULL; michael@0: if ((freed_so_far >= rwnd_req) && michael@0: (no_rcv_needed == 0)) michael@0: sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); michael@0: michael@0: } else { michael@0: /* michael@0: * The user did not read all of this michael@0: * message, turn off the returned MSG_EOR michael@0: * since we are leaving more behind on the michael@0: * control to read. michael@0: */ michael@0: #ifdef INVARIANTS michael@0: if (control->end_added && michael@0: (control->data == NULL) && michael@0: (control->tail_mbuf == NULL)) { michael@0: panic("Gak, control->length is corrupt?"); michael@0: } michael@0: #endif michael@0: no_rcv_needed = control->do_not_ref_stcb; michael@0: out_flags &= ~MSG_EOR; michael@0: } michael@0: } michael@0: if (out_flags & MSG_EOR) { michael@0: goto release; michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: if ((uio->uio_resid == 0) || michael@0: #else michael@0: if ((uio_resid(uio) == 0) || michael@0: #endif michael@0: #else michael@0: if ((uio->uio_resid == 0) || michael@0: #endif michael@0: ((in_eeor_mode) && michael@0: (copied_so_far >= (uint32_t)max(so->so_rcv.sb_lowat, 1)))) { michael@0: goto release; michael@0: } michael@0: /* michael@0: * If I hit here the receiver wants more and this message is michael@0: * NOT done (pd-api). So two questions. Can we block? if not michael@0: * we are done. Did the user NOT set MSG_WAITALL? michael@0: */ michael@0: if (block_allowed == 0) { michael@0: goto release; michael@0: } michael@0: /* michael@0: * We need to wait for more data a few things: - We don't michael@0: * sbunlock() so we don't get someone else reading. - We michael@0: * must be sure to account for the case where what is added michael@0: * is NOT to our control when we wakeup. michael@0: */ michael@0: michael@0: /* Do we need to tell the transport a rwnd update might be michael@0: * needed before we go to sleep? michael@0: */ michael@0: if (((stcb) && (in_flags & MSG_PEEK) == 0) && michael@0: ((freed_so_far >= rwnd_req) && michael@0: (control->do_not_ref_stcb == 0) && michael@0: (no_rcv_needed == 0))) { michael@0: sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); michael@0: } michael@0: wait_some_more: michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__) michael@0: if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { michael@0: goto release; michael@0: } michael@0: #else michael@0: if (so->so_state & SS_CANTRCVMORE) { michael@0: goto release; michael@0: } michael@0: #endif michael@0: michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) michael@0: goto release; michael@0: michael@0: if (hold_rlock == 1) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: hold_rlock = 0; michael@0: } michael@0: if (hold_sblock == 0) { michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: hold_sblock = 1; michael@0: } michael@0: #if defined(__APPLE__) michael@0: sbunlock(&so->so_rcv, 1); michael@0: #endif michael@0: if ((copied_so_far) && (control->length == 0) && michael@0: (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) { michael@0: goto release; michael@0: } michael@0: if (so->so_rcv.sb_cc <= control->held_length) { michael@0: error = sbwait(&so->so_rcv); michael@0: if (error) { michael@0: #if defined(__FreeBSD__) michael@0: goto release; michael@0: #else michael@0: goto release_unlocked; michael@0: #endif michael@0: } michael@0: control->held_length = 0; michael@0: } michael@0: #if defined(__APPLE__) michael@0: error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags)); michael@0: #endif michael@0: if (hold_sblock) { michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: hold_sblock = 0; michael@0: } michael@0: if (control->length == 0) { michael@0: /* still nothing here */ michael@0: if (control->end_added == 1) { michael@0: /* he aborted, or is done i.e.did a shutdown */ michael@0: out_flags |= MSG_EOR; michael@0: if (control->pdapi_aborted) { michael@0: if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0)) michael@0: control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; michael@0: michael@0: out_flags |= MSG_TRUNC; michael@0: } else { michael@0: if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0)) michael@0: control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; michael@0: } michael@0: goto done_with_control; michael@0: } michael@0: if (so->so_rcv.sb_cc > held_length) { michael@0: control->held_length = so->so_rcv.sb_cc; michael@0: held_length = 0; michael@0: } michael@0: goto wait_some_more; michael@0: } else if (control->data == NULL) { michael@0: /* we must re-sync since data michael@0: * is probably being added michael@0: */ michael@0: SCTP_INP_READ_LOCK(inp); michael@0: if ((control->length > 0) && (control->data == NULL)) { michael@0: /* big trouble.. we have the lock and its corrupt? */ michael@0: #ifdef INVARIANTS michael@0: panic ("Impossible data==NULL length !=0"); michael@0: #endif michael@0: out_flags |= MSG_EOR; michael@0: out_flags |= MSG_TRUNC; michael@0: control->length = 0; michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: goto done_with_control; michael@0: } michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: /* We will fall around to get more data */ michael@0: } michael@0: goto get_more_data; michael@0: } else { michael@0: /*- michael@0: * Give caller back the mbuf chain, michael@0: * store in uio_resid the length michael@0: */ michael@0: wakeup_read_socket = 0; michael@0: if ((control->end_added == 0) || michael@0: (TAILQ_NEXT(control, next) == NULL)) { michael@0: /* Need to get rlock */ michael@0: if (hold_rlock == 0) { michael@0: SCTP_INP_READ_LOCK(inp); michael@0: hold_rlock = 1; michael@0: } michael@0: } michael@0: if (control->end_added) { michael@0: out_flags |= MSG_EOR; michael@0: if ((control->do_not_ref_stcb == 0) && michael@0: (control->stcb != NULL) && michael@0: ((control->spec_flags & M_NOTIFICATION) == 0)) michael@0: control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; michael@0: } michael@0: if (control->spec_flags & M_NOTIFICATION) { michael@0: out_flags |= MSG_NOTIFICATION; michael@0: } michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: uio->uio_resid = control->length; michael@0: #else michael@0: uio_setresid(uio, control->length); michael@0: #endif michael@0: #else michael@0: uio->uio_resid = control->length; michael@0: #endif michael@0: *mp = control->data; michael@0: m = control->data; michael@0: while (m) { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&so->so_rcv, michael@0: control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m)); michael@0: } michael@0: sctp_sbfree(control, stcb, &so->so_rcv, m); michael@0: freed_so_far += SCTP_BUF_LEN(m); michael@0: freed_so_far += MSIZE; michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { michael@0: sctp_sblog(&so->so_rcv, michael@0: control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0); michael@0: } michael@0: m = SCTP_BUF_NEXT(m); michael@0: } michael@0: control->data = control->tail_mbuf = NULL; michael@0: control->length = 0; michael@0: if (out_flags & MSG_EOR) { michael@0: /* Done with this control */ michael@0: goto done_with_control; michael@0: } michael@0: } michael@0: release: michael@0: if (hold_rlock == 1) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: hold_rlock = 0; michael@0: } michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version < 700000) || defined(__Userspace__) michael@0: if (hold_sblock == 0) { michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: hold_sblock = 1; michael@0: } michael@0: #else michael@0: if (hold_sblock == 1) { michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: hold_sblock = 0; michael@0: } michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: sbunlock(&so->so_rcv, 1); michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) michael@0: sbunlock(&so->so_rcv); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 700000 michael@0: sockbuf_lock = 0; michael@0: #endif michael@0: #endif michael@0: michael@0: release_unlocked: michael@0: if (hold_sblock) { michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: hold_sblock = 0; michael@0: } michael@0: if ((stcb) && (in_flags & MSG_PEEK) == 0) { michael@0: if ((freed_so_far >= rwnd_req) && michael@0: (control && (control->do_not_ref_stcb == 0)) && michael@0: (no_rcv_needed == 0)) michael@0: sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); michael@0: } michael@0: out: michael@0: if (msg_flags) { michael@0: *msg_flags = out_flags; michael@0: } michael@0: if (((out_flags & MSG_EOR) == 0) && michael@0: ((in_flags & MSG_PEEK) == 0) && michael@0: (sinfo) && michael@0: (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) || michael@0: sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) { michael@0: struct sctp_extrcvinfo *s_extra; michael@0: s_extra = (struct sctp_extrcvinfo *)sinfo; michael@0: s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG; michael@0: } michael@0: if (hold_rlock == 1) { michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: } michael@0: if (hold_sblock) { michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 700000 michael@0: if (sockbuf_lock) { michael@0: sbunlock(&so->so_rcv); michael@0: } michael@0: #endif michael@0: michael@0: if (freecnt_applied) { michael@0: /* michael@0: * The lock on the socket buffer protects us so the free michael@0: * code will stop. But since we used the socketbuf lock and michael@0: * the sender uses the tcb_lock to increment, we need to use michael@0: * the atomic add to the refcnt. michael@0: */ michael@0: if (stcb == NULL) { michael@0: #ifdef INVARIANTS michael@0: panic("stcb for refcnt has gone NULL?"); michael@0: goto stage_left; michael@0: #else michael@0: goto stage_left; michael@0: #endif michael@0: } michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: /* Save the value back for next time */ michael@0: stcb->freed_by_sorcv_sincelast = freed_so_far; michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) { michael@0: if (stcb) { michael@0: sctp_misc_ints(SCTP_SORECV_DONE, michael@0: freed_so_far, michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: ((uio) ? (slen - uio->uio_resid) : slen), michael@0: #else michael@0: ((uio) ? (slen - uio_resid(uio)) : slen), michael@0: #endif michael@0: #else michael@0: ((uio) ? (slen - uio->uio_resid) : slen), michael@0: #endif michael@0: stcb->asoc.my_rwnd, michael@0: so->so_rcv.sb_cc); michael@0: } else { michael@0: sctp_misc_ints(SCTP_SORECV_DONE, michael@0: freed_so_far, michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) michael@0: ((uio) ? (slen - uio->uio_resid) : slen), michael@0: #else michael@0: ((uio) ? (slen - uio_resid(uio)) : slen), michael@0: #endif michael@0: #else michael@0: ((uio) ? (slen - uio->uio_resid) : slen), michael@0: #endif michael@0: 0, michael@0: so->so_rcv.sb_cc); michael@0: } michael@0: } michael@0: stage_left: michael@0: if (wakeup_read_socket) { michael@0: sctp_sorwakeup(inp, so); michael@0: } michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: struct mbuf * michael@0: sctp_m_free(struct mbuf *m) michael@0: { michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: if (SCTP_BUF_IS_EXTENDED(m)) { michael@0: sctp_log_mb(m, SCTP_MBUF_IFREE); michael@0: } michael@0: } michael@0: return (m_free(m)); michael@0: } michael@0: michael@0: void sctp_m_freem(struct mbuf *mb) michael@0: { michael@0: while (mb != NULL) michael@0: mb = sctp_m_free(mb); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: int michael@0: sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id) michael@0: { michael@0: /* Given a local address. For all associations michael@0: * that holds the address, request a peer-set-primary. michael@0: */ michael@0: struct sctp_ifa *ifa; michael@0: struct sctp_laddr *wi; michael@0: michael@0: ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0); michael@0: if (ifa == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL); michael@0: return (EADDRNOTAVAIL); michael@0: } michael@0: /* Now that we have the ifa we must awaken the michael@0: * iterator with this message. michael@0: */ michael@0: wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); michael@0: if (wi == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: /* Now incr the count and int wi structure */ michael@0: SCTP_INCR_LADDR_COUNT(); michael@0: bzero(wi, sizeof(*wi)); michael@0: (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); michael@0: wi->ifa = ifa; michael@0: wi->action = SCTP_SET_PRIM_ADDR; michael@0: atomic_add_int(&ifa->refcount, 1); michael@0: michael@0: /* Now add it to the work queue */ michael@0: SCTP_WQ_ADDR_LOCK(); michael@0: /* michael@0: * Should this really be a tailq? As it is we will process the michael@0: * newest first :-0 michael@0: */ michael@0: LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); michael@0: SCTP_WQ_ADDR_UNLOCK(); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, michael@0: (struct sctp_inpcb *)NULL, michael@0: (struct sctp_tcb *)NULL, michael@0: (struct sctp_nets *)NULL); michael@0: return (0); michael@0: } michael@0: michael@0: #if defined(__Userspace__) michael@0: /* no sctp_soreceive for __Userspace__ now */ michael@0: #endif michael@0: michael@0: #if !defined(__Userspace__) michael@0: int michael@0: sctp_soreceive( struct socket *so, michael@0: struct sockaddr **psa, michael@0: struct uio *uio, michael@0: struct mbuf **mp0, michael@0: struct mbuf **controlp, michael@0: int *flagsp) michael@0: { michael@0: int error, fromlen; michael@0: uint8_t sockbuf[256]; michael@0: struct sockaddr *from; michael@0: struct sctp_extrcvinfo sinfo; michael@0: int filling_sinfo = 1; michael@0: struct sctp_inpcb *inp; michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: /* pickup the assoc we are reading from */ michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) && michael@0: sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) && michael@0: sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) || michael@0: (controlp == NULL)) { michael@0: /* user does not want the sndrcv ctl */ michael@0: filling_sinfo = 0; michael@0: } michael@0: if (psa) { michael@0: from = (struct sockaddr *)sockbuf; michael@0: fromlen = sizeof(sockbuf); michael@0: #ifdef HAVE_SA_LEN michael@0: from->sa_len = 0; michael@0: #endif michael@0: } else { michael@0: from = NULL; michael@0: fromlen = 0; michael@0: } michael@0: michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_LOCK(so, 1); michael@0: #endif michael@0: error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp, michael@0: (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo); michael@0: if ((controlp) && (filling_sinfo)) { michael@0: /* copy back the sinfo in a CMSG format */ michael@0: if (filling_sinfo) michael@0: *controlp = sctp_build_ctl_nchunk(inp, michael@0: (struct sctp_sndrcvinfo *)&sinfo); michael@0: else michael@0: *controlp = NULL; michael@0: } michael@0: if (psa) { michael@0: /* copy back the address info */ michael@0: #ifdef HAVE_SA_LEN michael@0: if (from && from->sa_len) { michael@0: #else michael@0: if (from) { michael@0: #endif michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__) michael@0: *psa = sodupsockaddr(from, M_NOWAIT); michael@0: #else michael@0: *psa = dup_sockaddr(from, mp0 == 0); michael@0: #endif michael@0: } else { michael@0: *psa = NULL; michael@0: } michael@0: } michael@0: #if defined(__APPLE__) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: #endif michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version < 603000) || defined(__Windows__) michael@0: /* michael@0: * General routine to allocate a hash table with control of memory flags. michael@0: * is in 7.0 and beyond for sure :-) michael@0: */ michael@0: void * michael@0: sctp_hashinit_flags(int elements, struct malloc_type *type, michael@0: u_long *hashmask, int flags) michael@0: { michael@0: long hashsize; michael@0: LIST_HEAD(generic, generic) *hashtbl; michael@0: int i; michael@0: michael@0: michael@0: if (elements <= 0) { michael@0: #ifdef INVARIANTS michael@0: panic("hashinit: bad elements"); michael@0: #else michael@0: SCTP_PRINTF("hashinit: bad elements?"); michael@0: elements = 1; michael@0: #endif michael@0: } michael@0: for (hashsize = 1; hashsize <= elements; hashsize <<= 1) michael@0: continue; michael@0: hashsize >>= 1; michael@0: if (flags & HASH_WAITOK) michael@0: hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); michael@0: else if (flags & HASH_NOWAIT) michael@0: hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_NOWAIT); michael@0: else { michael@0: #ifdef INVARIANTS michael@0: panic("flag incorrect in hashinit_flags"); michael@0: #else michael@0: return (NULL); michael@0: #endif michael@0: } michael@0: michael@0: /* no memory? */ michael@0: if (hashtbl == NULL) michael@0: return (NULL); michael@0: michael@0: for (i = 0; i < hashsize; i++) michael@0: LIST_INIT(&hashtbl[i]); michael@0: *hashmask = hashsize - 1; michael@0: return (hashtbl); michael@0: } michael@0: #endif michael@0: michael@0: #else /* __Userspace__ ifdef above sctp_soreceive */ michael@0: /* michael@0: * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland. michael@0: * NOTE: We don't want multiple definitions here. So sctp_hashinit_flags() above for michael@0: *__FreeBSD__ must be excluded. michael@0: * michael@0: */ michael@0: michael@0: void * michael@0: sctp_hashinit_flags(int elements, struct malloc_type *type, michael@0: u_long *hashmask, int flags) michael@0: { michael@0: long hashsize; michael@0: LIST_HEAD(generic, generic) *hashtbl; michael@0: int i; michael@0: michael@0: if (elements <= 0) { michael@0: SCTP_PRINTF("hashinit: bad elements?"); michael@0: #ifdef INVARIANTS michael@0: return (NULL); michael@0: #else michael@0: elements = 1; michael@0: #endif michael@0: } michael@0: for (hashsize = 1; hashsize <= elements; hashsize <<= 1) michael@0: continue; michael@0: hashsize >>= 1; michael@0: /*cannot use MALLOC here because it has to be declared or defined michael@0: using MALLOC_DECLARE or MALLOC_DEFINE first. */ michael@0: if (flags & HASH_WAITOK) michael@0: hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl)); michael@0: else if (flags & HASH_NOWAIT) michael@0: hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl)); michael@0: else { michael@0: #ifdef INVARIANTS michael@0: SCTP_PRINTF("flag incorrect in hashinit_flags.\n"); michael@0: #endif michael@0: return (NULL); michael@0: } michael@0: michael@0: /* no memory? */ michael@0: if (hashtbl == NULL) michael@0: return (NULL); michael@0: michael@0: for (i = 0; i < hashsize; i++) michael@0: LIST_INIT(&hashtbl[i]); michael@0: *hashmask = hashsize - 1; michael@0: return (hashtbl); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask) michael@0: { michael@0: LIST_HEAD(generic, generic) *hashtbl, *hp; michael@0: michael@0: hashtbl = vhashtbl; michael@0: for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++) michael@0: if (!LIST_EMPTY(hp)) { michael@0: SCTP_PRINTF("hashdestroy: hash not empty.\n"); michael@0: return; michael@0: } michael@0: FREE(hashtbl, type); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask) michael@0: { michael@0: LIST_HEAD(generic, generic) *hashtbl/*, *hp*/; michael@0: /* michael@0: LIST_ENTRY(type) *start, *temp; michael@0: */ michael@0: hashtbl = vhashtbl; michael@0: /* Apparently temp is not dynamically allocated, so attempts to michael@0: free it results in error. michael@0: for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++) michael@0: if (!LIST_EMPTY(hp)) { michael@0: start = LIST_FIRST(hp); michael@0: while (start != NULL) { michael@0: temp = start; michael@0: start = start->le_next; michael@0: SCTP_PRINTF("%s: %p \n", __func__, (void *)temp); michael@0: FREE(temp, type); michael@0: } michael@0: } michael@0: */ michael@0: FREE(hashtbl, type); michael@0: } michael@0: michael@0: michael@0: #endif michael@0: michael@0: michael@0: int michael@0: sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr, michael@0: int totaddr, int *error) michael@0: { michael@0: int added = 0; michael@0: int i; michael@0: struct sctp_inpcb *inp; michael@0: struct sockaddr *sa; michael@0: size_t incr = 0; michael@0: #ifdef INET michael@0: struct sockaddr_in *sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 *sin6; michael@0: #endif michael@0: michael@0: sa = addr; michael@0: inp = stcb->sctp_ep; michael@0: *error = 0; michael@0: for (i = 0; i < totaddr; i++) { michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: incr = sizeof(struct sockaddr_in); michael@0: sin = (struct sockaddr_in *)sa; michael@0: if ((sin->sin_addr.s_addr == INADDR_ANY) || michael@0: (sin->sin_addr.s_addr == INADDR_BROADCAST) || michael@0: IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_7); michael@0: *error = EINVAL; michael@0: goto out_now; michael@0: } michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { michael@0: /* assoc gone no un-lock */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_7); michael@0: *error = ENOBUFS; michael@0: goto out_now; michael@0: } michael@0: added++; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: incr = sizeof(struct sockaddr_in6); michael@0: sin6 = (struct sockaddr_in6 *)sa; michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || michael@0: IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_8); michael@0: *error = EINVAL; michael@0: goto out_now; michael@0: } michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { michael@0: /* assoc gone no un-lock */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_8); michael@0: *error = ENOBUFS; michael@0: goto out_now; michael@0: } michael@0: added++; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: incr = sizeof(struct sockaddr_in6); michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { michael@0: /* assoc gone no un-lock */ michael@0: SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); michael@0: (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_8); michael@0: *error = ENOBUFS; michael@0: goto out_now; michael@0: } michael@0: added++; michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: sa = (struct sockaddr *)((caddr_t)sa + incr); michael@0: } michael@0: out_now: michael@0: return (added); michael@0: } michael@0: michael@0: struct sctp_tcb * michael@0: sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr, michael@0: int *totaddr, int *num_v4, int *num_v6, int *error, michael@0: int limit, int *bad_addr) michael@0: { michael@0: struct sockaddr *sa; michael@0: struct sctp_tcb *stcb = NULL; michael@0: size_t incr, at, i; michael@0: at = incr = 0; michael@0: sa = addr; michael@0: michael@0: *error = *num_v6 = *num_v4 = 0; michael@0: /* account and validate addresses */ michael@0: for (i = 0; i < (size_t)*totaddr; i++) { michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: (*num_v4) += 1; michael@0: incr = sizeof(struct sockaddr_in); michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != incr) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: *bad_addr = 1; michael@0: return (NULL); michael@0: } michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)sa; michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: /* Must be non-mapped for connectx */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: *bad_addr = 1; michael@0: return (NULL); michael@0: } michael@0: (*num_v6) += 1; michael@0: incr = sizeof(struct sockaddr_in6); michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != incr) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: *bad_addr = 1; michael@0: return (NULL); michael@0: } michael@0: #endif michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: *totaddr = i; michael@0: /* we are done */ michael@0: break; michael@0: } michael@0: if (i == (size_t)*totaddr) { michael@0: break; michael@0: } michael@0: SCTP_INP_INCR_REF(inp); michael@0: stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL); michael@0: if (stcb != NULL) { michael@0: /* Already have or am bring up an association */ michael@0: return (stcb); michael@0: } else { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: if ((at + incr) > (size_t)limit) { michael@0: *totaddr = i; michael@0: break; michael@0: } michael@0: sa = (struct sockaddr *)((caddr_t)sa + incr); michael@0: } michael@0: return ((struct sctp_tcb *)NULL); michael@0: } michael@0: michael@0: /* michael@0: * sctp_bindx(ADD) for one address. michael@0: * assumes all arguments are valid/checked by caller. michael@0: */ michael@0: void michael@0: sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp, michael@0: struct sockaddr *sa, sctp_assoc_t assoc_id, michael@0: uint32_t vrf_id, int *error, void *p) michael@0: { michael@0: struct sockaddr *addr_touse; michael@0: #ifdef INET6 michael@0: struct sockaddr_in sin; michael@0: #endif michael@0: #ifdef SCTP_MVRF michael@0: int i, fnd = 0; michael@0: #endif michael@0: michael@0: /* see if we're bound all already! */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #ifdef SCTP_MVRF michael@0: /* Is the VRF one we have */ michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: if (vrf_id == inp->m_vrf_ids[i]) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (!fnd) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #endif michael@0: addr_touse = sa; michael@0: #ifdef INET6 michael@0: if (sa->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *sin6; michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != sizeof(struct sockaddr_in6)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #endif michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { michael@0: /* can only bind v6 on PF_INET6 sockets */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: sin6 = (struct sockaddr_in6 *)addr_touse; michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && michael@0: SCTP_IPV6_V6ONLY(inp)) { michael@0: /* can't bind v4-mapped on PF_INET sockets */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: in6_sin6_2_sin(&sin, sin6); michael@0: addr_touse = (struct sockaddr *)&sin; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: if (sa->sa_family == AF_INET) { michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != sizeof(struct sockaddr_in)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #endif michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && michael@0: SCTP_IPV6_V6ONLY(inp)) { michael@0: /* can't bind v4 on PF_INET sockets */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { michael@0: #if !(defined(__Panda__) || defined(__Windows__)) michael@0: if (p == NULL) { michael@0: /* Can't get proc for Net/Open BSD */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #endif michael@0: *error = sctp_inpcb_bind(so, addr_touse, NULL, p); michael@0: return; michael@0: } michael@0: /* michael@0: * No locks required here since bind and mgmt_ep_sa michael@0: * all do their own locking. If we do something for michael@0: * the FIX: below we may need to lock in that case. michael@0: */ michael@0: if (assoc_id == 0) { michael@0: /* add the address */ michael@0: struct sctp_inpcb *lep; michael@0: struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse; michael@0: michael@0: /* validate the incoming port */ michael@0: if ((lsin->sin_port != 0) && michael@0: (lsin->sin_port != inp->sctp_lport)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } else { michael@0: /* user specified 0 port, set it to existing port */ michael@0: lsin->sin_port = inp->sctp_lport; michael@0: } michael@0: michael@0: lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id); michael@0: if (lep != NULL) { michael@0: /* michael@0: * We must decrement the refcount michael@0: * since we have the ep already and michael@0: * are binding. No remove going on michael@0: * here. michael@0: */ michael@0: SCTP_INP_DECR_REF(lep); michael@0: } michael@0: if (lep == inp) { michael@0: /* already bound to it.. ok */ michael@0: return; michael@0: } else if (lep == NULL) { michael@0: ((struct sockaddr_in *)addr_touse)->sin_port = 0; michael@0: *error = sctp_addr_mgmt_ep_sa(inp, addr_touse, michael@0: SCTP_ADD_IP_ADDRESS, michael@0: vrf_id, NULL); michael@0: } else { michael@0: *error = EADDRINUSE; michael@0: } michael@0: if (*error) michael@0: return; michael@0: } else { michael@0: /* michael@0: * FIX: decide whether we allow assoc based michael@0: * bindx michael@0: */ michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * sctp_bindx(DELETE) for one address. michael@0: * assumes all arguments are valid/checked by caller. michael@0: */ michael@0: void michael@0: sctp_bindx_delete_address(struct sctp_inpcb *inp, michael@0: struct sockaddr *sa, sctp_assoc_t assoc_id, michael@0: uint32_t vrf_id, int *error) michael@0: { michael@0: struct sockaddr *addr_touse; michael@0: #ifdef INET6 michael@0: struct sockaddr_in sin; michael@0: #endif michael@0: #ifdef SCTP_MVRF michael@0: int i, fnd = 0; michael@0: #endif michael@0: michael@0: /* see if we're bound all already! */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #ifdef SCTP_MVRF michael@0: /* Is the VRF one we have */ michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: if (vrf_id == inp->m_vrf_ids[i]) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (!fnd) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #endif michael@0: addr_touse = sa; michael@0: #ifdef INET6 michael@0: if (sa->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *sin6; michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != sizeof(struct sockaddr_in6)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #endif michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { michael@0: /* can only bind v6 on PF_INET6 sockets */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: sin6 = (struct sockaddr_in6 *)addr_touse; michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && michael@0: SCTP_IPV6_V6ONLY(inp)) { michael@0: /* can't bind mapped-v4 on PF_INET sockets */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: in6_sin6_2_sin(&sin, sin6); michael@0: addr_touse = (struct sockaddr *)&sin; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: if (sa->sa_family == AF_INET) { michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != sizeof(struct sockaddr_in)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: #endif michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && michael@0: SCTP_IPV6_V6ONLY(inp)) { michael@0: /* can't bind v4 on PF_INET sockets */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); michael@0: *error = EINVAL; michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: /* michael@0: * No lock required mgmt_ep_sa does its own locking. michael@0: * If the FIX: below is ever changed we may need to michael@0: * lock before calling association level binding. michael@0: */ michael@0: if (assoc_id == 0) { michael@0: /* delete the address */ michael@0: *error = sctp_addr_mgmt_ep_sa(inp, addr_touse, michael@0: SCTP_DEL_IP_ADDRESS, michael@0: vrf_id, NULL); michael@0: } else { michael@0: /* michael@0: * FIX: decide whether we allow assoc based michael@0: * bindx michael@0: */ michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * returns the valid local address count for an assoc, taking into account michael@0: * all scoping rules michael@0: */ michael@0: int michael@0: sctp_local_addr_count(struct sctp_tcb *stcb) michael@0: { michael@0: int loopback_scope; michael@0: #if defined(INET) michael@0: int ipv4_local_scope, ipv4_addr_legal; michael@0: #endif michael@0: #if defined (INET6) michael@0: int local_scope, site_scope, ipv6_addr_legal; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: int conn_addr_legal; michael@0: #endif michael@0: struct sctp_vrf *vrf; michael@0: struct sctp_ifn *sctp_ifn; michael@0: struct sctp_ifa *sctp_ifa; michael@0: int count = 0; michael@0: michael@0: /* Turn on all the appropriate scopes */ michael@0: loopback_scope = stcb->asoc.scope.loopback_scope; michael@0: #if defined(INET) michael@0: ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope; michael@0: ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal; michael@0: #endif michael@0: #if defined(INET6) michael@0: local_scope = stcb->asoc.scope.local_scope; michael@0: site_scope = stcb->asoc.scope.site_scope; michael@0: ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: conn_addr_legal = stcb->asoc.scope.conn_addr_legal; michael@0: #endif michael@0: SCTP_IPI_ADDR_RLOCK(); michael@0: vrf = sctp_find_vrf(stcb->asoc.vrf_id); michael@0: if (vrf == NULL) { michael@0: /* no vrf, no addresses */ michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (0); michael@0: } michael@0: michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: /* michael@0: * bound all case: go through all ifns on the vrf michael@0: */ michael@0: LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { michael@0: if ((loopback_scope == 0) && michael@0: SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { michael@0: continue; michael@0: } michael@0: LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { michael@0: if (sctp_is_addr_restricted(stcb, sctp_ifa)) michael@0: continue; michael@0: switch (sctp_ifa->address.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (ipv4_addr_legal) { michael@0: struct sockaddr_in *sin; michael@0: michael@0: sin = (struct sockaddr_in *)&sctp_ifa->address.sa; michael@0: if (sin->sin_addr.s_addr == 0) { michael@0: /* skip unspecified addrs */ michael@0: continue; michael@0: } michael@0: if ((ipv4_local_scope == 0) && michael@0: (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { michael@0: continue; michael@0: } michael@0: /* count this one */ michael@0: count++; michael@0: } else { michael@0: continue; michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (ipv6_addr_legal) { michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME) michael@0: struct sockaddr_in6 lsa6; michael@0: #endif michael@0: sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa; michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { michael@0: continue; michael@0: } michael@0: if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { michael@0: if (local_scope == 0) michael@0: continue; michael@0: #if defined(SCTP_EMBEDDED_V6_SCOPE) michael@0: if (sin6->sin6_scope_id == 0) { michael@0: #ifdef SCTP_KAME michael@0: if (sa6_recoverscope(sin6) != 0) michael@0: /* michael@0: * bad link michael@0: * local michael@0: * address michael@0: */ michael@0: continue; michael@0: #else michael@0: lsa6 = *sin6; michael@0: if (in6_recoverscope(&lsa6, michael@0: &lsa6.sin6_addr, michael@0: NULL)) michael@0: /* michael@0: * bad link michael@0: * local michael@0: * address michael@0: */ michael@0: continue; michael@0: sin6 = &lsa6; michael@0: #endif /* SCTP_KAME */ michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: } michael@0: if ((site_scope == 0) && michael@0: (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { michael@0: continue; michael@0: } michael@0: /* count this one */ michael@0: count++; michael@0: } michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (conn_addr_legal) { michael@0: count++; michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: /* michael@0: * subset bound case michael@0: */ michael@0: struct sctp_laddr *laddr; michael@0: LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, michael@0: sctp_nxt_addr) { michael@0: if (sctp_is_addr_restricted(stcb, laddr->ifa)) { michael@0: continue; michael@0: } michael@0: /* count this one */ michael@0: count++; michael@0: } michael@0: } michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (count); michael@0: } michael@0: michael@0: #if defined(SCTP_LOCAL_TRACE_BUF) michael@0: michael@0: void michael@0: 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) michael@0: { michael@0: uint32_t saveindex, newindex; michael@0: michael@0: #if defined(__Windows__) michael@0: if (SCTP_BASE_SYSCTL(sctp_log) == NULL) { michael@0: return; michael@0: } michael@0: do { michael@0: saveindex = SCTP_BASE_SYSCTL(sctp_log)->index; michael@0: if (saveindex >= SCTP_MAX_LOGGING_SIZE) { michael@0: newindex = 1; michael@0: } else { michael@0: newindex = saveindex + 1; michael@0: } michael@0: } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log)->index, saveindex, newindex) == 0); michael@0: if (saveindex >= SCTP_MAX_LOGGING_SIZE) { michael@0: saveindex = 0; michael@0: } michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT; michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].subsys = subsys; michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[0] = a; michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[1] = b; michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[2] = c; michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[3] = d; michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[4] = e; michael@0: SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[5] = f; michael@0: #else michael@0: do { michael@0: saveindex = SCTP_BASE_SYSCTL(sctp_log).index; michael@0: if (saveindex >= SCTP_MAX_LOGGING_SIZE) { michael@0: newindex = 1; michael@0: } else { michael@0: newindex = saveindex + 1; michael@0: } michael@0: } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0); michael@0: if (saveindex >= SCTP_MAX_LOGGING_SIZE) { michael@0: saveindex = 0; michael@0: } michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT; michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys; michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a; michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b; michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c; michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d; michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e; michael@0: SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f; michael@0: #endif michael@0: } michael@0: michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #if __FreeBSD_version >= 800044 michael@0: static void michael@0: sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored) michael@0: { michael@0: struct ip *iph; michael@0: #ifdef INET6 michael@0: struct ip6_hdr *ip6; michael@0: #endif michael@0: struct mbuf *sp, *last; michael@0: struct udphdr *uhdr; michael@0: uint16_t port; michael@0: michael@0: if ((m->m_flags & M_PKTHDR) == 0) { michael@0: /* Can't handle one that is not a pkt hdr */ michael@0: goto out; michael@0: } michael@0: /* Pull the src port */ michael@0: iph = mtod(m, struct ip *); michael@0: uhdr = (struct udphdr *)((caddr_t)iph + off); michael@0: port = uhdr->uh_sport; michael@0: /* Split out the mbuf chain. Leave the michael@0: * IP header in m, place the michael@0: * rest in the sp. michael@0: */ michael@0: sp = m_split(m, off, M_NOWAIT); michael@0: if (sp == NULL) { michael@0: /* Gak, drop packet, we can't do a split */ michael@0: goto out; michael@0: } michael@0: if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) { michael@0: /* Gak, packet can't have an SCTP header in it - too small */ michael@0: m_freem(sp); michael@0: goto out; michael@0: } michael@0: /* Now pull up the UDP header and SCTP header together */ michael@0: sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr)); michael@0: if (sp == NULL) { michael@0: /* Gak pullup failed */ michael@0: goto out; michael@0: } michael@0: /* Trim out the UDP header */ michael@0: m_adj(sp, sizeof(struct udphdr)); michael@0: michael@0: /* Now reconstruct the mbuf chain */ michael@0: for (last = m; last->m_next; last = last->m_next); michael@0: last->m_next = sp; michael@0: m->m_pkthdr.len += sp->m_pkthdr.len; michael@0: iph = mtod(m, struct ip *); michael@0: switch (iph->ip_v) { michael@0: #ifdef INET michael@0: case IPVERSION: michael@0: #if __FreeBSD_version >= 1000000 michael@0: iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr)); michael@0: #else michael@0: iph->ip_len -= sizeof(struct udphdr); michael@0: #endif michael@0: sctp_input_with_port(m, off, port); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case IPV6_VERSION >> 4: michael@0: ip6 = mtod(m, struct ip6_hdr *); michael@0: ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr)); michael@0: sctp6_input_with_port(&m, &off, port); michael@0: break; michael@0: #endif michael@0: default: michael@0: goto out; michael@0: break; michael@0: } michael@0: return; michael@0: out: michael@0: m_freem(m); michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: sctp_over_udp_stop(void) michael@0: { michael@0: /* michael@0: * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writting! michael@0: */ michael@0: #ifdef INET michael@0: if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) { michael@0: soclose(SCTP_BASE_INFO(udp4_tun_socket)); michael@0: SCTP_BASE_INFO(udp4_tun_socket) = NULL; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) { michael@0: soclose(SCTP_BASE_INFO(udp6_tun_socket)); michael@0: SCTP_BASE_INFO(udp6_tun_socket) = NULL; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: sctp_over_udp_start(void) michael@0: { michael@0: #if __FreeBSD_version >= 800044 michael@0: uint16_t port; michael@0: int ret; michael@0: #ifdef INET michael@0: struct sockaddr_in sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 sin6; michael@0: #endif michael@0: /* michael@0: * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writting! michael@0: */ michael@0: port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port); michael@0: if (ntohs(port) == 0) { michael@0: /* Must have a port set */ michael@0: return (EINVAL); michael@0: } michael@0: #ifdef INET michael@0: if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) { michael@0: /* Already running -- must stop first */ michael@0: return (EALREADY); michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) { michael@0: /* Already running -- must stop first */ michael@0: return (EALREADY); michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket), michael@0: SOCK_DGRAM, IPPROTO_UDP, michael@0: curthread->td_ucred, curthread))) { michael@0: sctp_over_udp_stop(); michael@0: return (ret); michael@0: } michael@0: /* Call the special UDP hook. */ michael@0: if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket), michael@0: sctp_recv_udp_tunneled_packet))) { michael@0: sctp_over_udp_stop(); michael@0: return (ret); michael@0: } michael@0: /* Ok, we have a socket, bind it to the port. */ michael@0: memset(&sin, 0, sizeof(struct sockaddr_in)); michael@0: sin.sin_len = sizeof(struct sockaddr_in); michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_port = htons(port); michael@0: if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket), michael@0: (struct sockaddr *)&sin, curthread))) { michael@0: sctp_over_udp_stop(); michael@0: return (ret); michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket), michael@0: SOCK_DGRAM, IPPROTO_UDP, michael@0: curthread->td_ucred, curthread))) { michael@0: sctp_over_udp_stop(); michael@0: return (ret); michael@0: } michael@0: /* Call the special UDP hook. */ michael@0: if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket), michael@0: sctp_recv_udp_tunneled_packet))) { michael@0: sctp_over_udp_stop(); michael@0: return (ret); michael@0: } michael@0: /* Ok, we have a socket, bind it to the port. */ michael@0: memset(&sin6, 0, sizeof(struct sockaddr_in6)); michael@0: sin6.sin6_len = sizeof(struct sockaddr_in6); michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = htons(port); michael@0: if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket), michael@0: (struct sockaddr *)&sin6, curthread))) { michael@0: sctp_over_udp_stop(); michael@0: return (ret); michael@0: } michael@0: #endif michael@0: return (0); michael@0: #else michael@0: return (ENOTSUP); michael@0: #endif michael@0: } michael@0: #endif