michael@0: /*- michael@0: * Copyright (c) 2001-2007, 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/netinet6/sctp6_usrreq.c 257555 2013-11-02 20:12:19Z tuexen $"); michael@0: #endif michael@0: michael@0: #include michael@0: #ifdef INET6 michael@0: #ifdef __FreeBSD__ michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef INET6 michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #if !defined(__Userspace_os_Windows) michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: #define APPLE_FILE_NO 9 michael@0: #endif michael@0: #ifdef IPSEC michael@0: #include michael@0: #ifdef INET6 michael@0: #include michael@0: #endif /* INET6 */ michael@0: #endif /* IPSEC */ michael@0: michael@0: #if !defined(__Userspace__) michael@0: extern struct protosw inetsw[]; michael@0: #endif michael@0: #if defined(__Panda__) || defined(__Userspace__) michael@0: int ip6_v6only=0; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: void michael@0: in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) michael@0: { michael@0: #if defined(__Userspace_os_Windows) michael@0: uint32_t temp; michael@0: #endif michael@0: bzero(sin, sizeof(*sin)); michael@0: #ifdef HAVE_SIN_LEN michael@0: sin->sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: sin->sin_family = AF_INET; michael@0: sin->sin_port = sin6->sin6_port; michael@0: #if defined(__Userspace_os_Windows) michael@0: temp = sin6->sin6_addr.s6_addr16[7]; michael@0: temp = temp << 16; michael@0: temp = temp | sin6->sin6_addr.s6_addr16[6]; michael@0: sin->sin_addr.s_addr = temp; michael@0: sctp_print_address((struct sockaddr*)sin); michael@0: #else michael@0: sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: in6_sin6_2_sin_in_sock(struct sockaddr *nam) michael@0: { michael@0: struct sockaddr_in *sin_p; michael@0: struct sockaddr_in6 sin6; michael@0: michael@0: /* save original sockaddr_in6 addr and convert it to sockaddr_in */ michael@0: sin6 = *(struct sockaddr_in6 *)nam; michael@0: sin_p = (struct sockaddr_in *)nam; michael@0: in6_sin6_2_sin(sin_p, &sin6); michael@0: } michael@0: #endif michael@0: michael@0: #if !defined(__Userspace__) michael@0: int michael@0: #if defined(__APPLE__) || defined(__FreeBSD__) michael@0: sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port) michael@0: #elif defined( __Panda__) michael@0: sctp6_input(pakhandle_type *i_pak) michael@0: #else michael@0: sctp6_input(struct mbuf **i_pak, int *offp, int proto) michael@0: #endif michael@0: { michael@0: struct mbuf *m; michael@0: int iphlen; michael@0: uint32_t vrf_id; michael@0: uint8_t ecn_bits; michael@0: struct sockaddr_in6 src, dst; michael@0: struct ip6_hdr *ip6; michael@0: struct sctphdr *sh; michael@0: struct sctp_chunkhdr *ch; michael@0: int length, offset; michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: uint8_t compute_crc; michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: uint32_t mflowid; michael@0: uint8_t use_mflowid; michael@0: #endif michael@0: #if !(defined(__APPLE__) || defined (__FreeBSD__)) michael@0: uint16_t port = 0; michael@0: #endif michael@0: michael@0: #if defined(__Panda__) michael@0: /* This is Evil, but its the only way to make panda work right. */ michael@0: iphlen = sizeof(struct ip6_hdr); michael@0: #else michael@0: iphlen = *offp; michael@0: #endif michael@0: if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) { michael@0: SCTP_RELEASE_PKT(*i_pak); michael@0: return (IPPROTO_DONE); michael@0: } michael@0: m = SCTP_HEADER_TO_CHAIN(*i_pak); michael@0: #ifdef __Panda__ michael@0: SCTP_DETACH_HEADER_FROM_CHAIN(*i_pak); michael@0: (void)SCTP_RELEASE_HEADER(*i_pak); michael@0: #endif michael@0: #ifdef SCTP_MBUF_LOGGING michael@0: /* Log in any input mbufs */ michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { michael@0: struct mbuf *mat; michael@0: michael@0: for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) { michael@0: if (SCTP_BUF_IS_EXTENDED(mat)) { michael@0: sctp_log_mb(mat, SCTP_MBUF_INPUT); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { michael@0: sctp_packet_log(m); michael@0: } michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #if __FreeBSD_version > 1000049 michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n", michael@0: m->m_pkthdr.len, michael@0: if_name(m->m_pkthdr.rcvif), michael@0: (int)m->m_pkthdr.csum_flags, CSUM_BITS); michael@0: #elif __FreeBSD_version >= 800000 michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: if_name(m->m_pkthdr.rcvif), michael@0: m->m_pkthdr.csum_flags); michael@0: #else michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: m->m_pkthdr.rcvif->if_xname, michael@0: m->m_pkthdr.csum_flags); michael@0: #endif michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp6_input(): Packet of length %d received on %s%d with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: m->m_pkthdr.rcvif->if_name, michael@0: m->m_pkthdr.rcvif->if_unit, michael@0: m->m_pkthdr.csum_flags); michael@0: #endif michael@0: #if defined(__Windows__) michael@0: SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, michael@0: "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", michael@0: m->m_pkthdr.len, michael@0: m->m_pkthdr.rcvif->if_xname, michael@0: m->m_pkthdr.csum_flags); michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: if (m->m_flags & M_FLOWID) { michael@0: mflowid = m->m_pkthdr.flowid; michael@0: use_mflowid = 1; michael@0: } else { michael@0: mflowid = 0; michael@0: use_mflowid = 0; michael@0: } michael@0: #endif michael@0: SCTP_STAT_INCR(sctps_recvpackets); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_inpackets); michael@0: /* Get IP, SCTP, and first chunk header together in the first mbuf. */ michael@0: offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); michael@0: ip6 = mtod(m, struct ip6_hdr *); michael@0: IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen, michael@0: (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))); michael@0: if (sh == NULL) { michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: return (IPPROTO_DONE); michael@0: } michael@0: ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); michael@0: offset -= sizeof(struct sctp_chunkhdr); michael@0: memset(&src, 0, sizeof(struct sockaddr_in6)); michael@0: src.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: src.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: src.sin6_port = sh->src_port; michael@0: src.sin6_addr = ip6->ip6_src; michael@0: #if defined(__FreeBSD__) michael@0: #if defined(__APPLE__) michael@0: /* XXX: This code should also be used on Apple */ michael@0: #endif michael@0: if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) { michael@0: goto out; michael@0: } michael@0: #endif michael@0: memset(&dst, 0, sizeof(struct sockaddr_in6)); michael@0: dst.sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: dst.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: dst.sin6_port = sh->dest_port; michael@0: dst.sin6_addr = ip6->ip6_dst; michael@0: #if defined(__FreeBSD__) michael@0: #if defined(__APPLE__) michael@0: /* XXX: This code should also be used on Apple */ michael@0: #endif michael@0: if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) { michael@0: goto out; michael@0: } michael@0: #endif michael@0: #ifdef __FreeBSD__ michael@0: if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) { michael@0: /* XXX send icmp6 host/port unreach? */ michael@0: goto out; michael@0: } michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: #if defined(NFAITH) && 0 < NFAITH michael@0: if (faithprefix(&dst.sin6_addr)) { michael@0: goto out; michael@0: } michael@0: #endif michael@0: #endif michael@0: length = ntohs(ip6->ip6_plen) + iphlen; michael@0: /* Validate mbuf chain length with IP payload length. */ michael@0: if (SCTP_HEADER_LEN(m) != length) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT1, michael@0: "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m)); michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: goto out; michael@0: } michael@0: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { michael@0: goto out; michael@0: } michael@0: ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff); michael@0: #if defined(SCTP_WITH_NO_CSUM) michael@0: SCTP_STAT_INCR(sctps_recvnocrc); michael@0: #else michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { michael@0: SCTP_STAT_INCR(sctps_recvhwcrc); michael@0: compute_crc = 0; michael@0: } else { michael@0: #else michael@0: if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && michael@0: (IN6_ARE_ADDR_EQUAL(&src.sin6_addr, &dst.sin6_addr))) { michael@0: SCTP_STAT_INCR(sctps_recvnocrc); michael@0: compute_crc = 0; michael@0: } else { michael@0: #endif michael@0: SCTP_STAT_INCR(sctps_recvswcrc); michael@0: compute_crc = 1; michael@0: } michael@0: #endif michael@0: sctp_common_input_processing(&m, iphlen, offset, length, michael@0: (struct sockaddr *)&src, michael@0: (struct sockaddr *)&dst, michael@0: sh, ch, michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: compute_crc, michael@0: #endif michael@0: ecn_bits, michael@0: #if defined(__FreeBSD__) michael@0: use_mflowid, mflowid, michael@0: #endif michael@0: vrf_id, port); michael@0: out: michael@0: if (m) { michael@0: sctp_m_freem(m); michael@0: } michael@0: return (IPPROTO_DONE); michael@0: } michael@0: michael@0: #if defined(__APPLE__) michael@0: int michael@0: sctp6_input(struct mbuf **i_pak, int *offp) michael@0: { michael@0: return (sctp6_input_with_port(i_pak, offp, 0)); michael@0: } michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) michael@0: int michael@0: sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED) michael@0: { michael@0: return (sctp6_input_with_port(i_pak, offp, 0)); michael@0: } michael@0: #endif michael@0: michael@0: #if defined(__Panda__) michael@0: void michael@0: #else michael@0: static void michael@0: #endif michael@0: sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6, michael@0: struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: uint32_t nxtsz; michael@0: michael@0: if ((inp == NULL) || (stcb == NULL) || (net == NULL) || michael@0: (icmp6 == NULL) || (sh == NULL)) { michael@0: goto out; michael@0: } michael@0: /* First do we even look at it? */ michael@0: if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) michael@0: goto out; michael@0: michael@0: if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) { michael@0: /* not PACKET TO BIG */ michael@0: goto out; michael@0: } michael@0: /* michael@0: * ok we need to look closely. We could even get smarter and look at michael@0: * anyone that we sent to in case we get a different ICMP that tells michael@0: * us there is no way to reach a host, but for this impl, all we michael@0: * care about is MTU discovery. michael@0: */ michael@0: nxtsz = ntohl(icmp6->icmp6_mtu); michael@0: /* Stop any PMTU timer */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ+SCTP_LOC_1); michael@0: michael@0: /* Adjust destination size limit */ michael@0: if (net->mtu > nxtsz) { michael@0: net->mtu = nxtsz; michael@0: if (net->port) { michael@0: net->mtu -= sizeof(struct udphdr); michael@0: } michael@0: } michael@0: /* now what about the ep? */ michael@0: if (stcb->asoc.smallest_mtu > nxtsz) { michael@0: struct sctp_tmit_chunk *chk; michael@0: michael@0: /* Adjust that too */ michael@0: stcb->asoc.smallest_mtu = nxtsz; michael@0: /* now off to subtract IP_DF flag if needed */ michael@0: michael@0: TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { michael@0: if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { michael@0: chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; michael@0: } michael@0: } michael@0: TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { michael@0: if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { michael@0: /* michael@0: * For this guy we also mark for immediate michael@0: * resend since we sent to big of chunk michael@0: */ michael@0: chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; michael@0: if (chk->sent != SCTP_DATAGRAM_RESEND) michael@0: stcb->asoc.sent_queue_retran_cnt++; michael@0: chk->sent = SCTP_DATAGRAM_RESEND; michael@0: chk->rec.data.doing_fast_retransmit = 0; michael@0: michael@0: chk->sent = SCTP_DATAGRAM_RESEND; michael@0: /* Clear any time so NO RTT is being done */ michael@0: chk->sent_rcv_time.tv_sec = 0; michael@0: chk->sent_rcv_time.tv_usec = 0; michael@0: stcb->asoc.total_flight -= chk->send_size; michael@0: net->flight_size -= chk->send_size; michael@0: } michael@0: } michael@0: } michael@0: sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); michael@0: out: michael@0: if (stcb) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: michael@0: void michael@0: sctp6_notify(struct sctp_inpcb *inp, michael@0: struct icmp6_hdr *icmph, michael@0: struct sctphdr *sh, michael@0: struct sockaddr *to, michael@0: struct sctp_tcb *stcb, michael@0: struct sctp_nets *net) michael@0: { michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: struct socket *so; michael@0: michael@0: #endif michael@0: michael@0: /* protection */ michael@0: if ((inp == NULL) || (stcb == NULL) || (net == NULL) || michael@0: (sh == NULL) || (to == NULL)) { michael@0: if (stcb) michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return; michael@0: } michael@0: /* First job is to verify the vtag matches what I would send */ michael@0: if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return; michael@0: } michael@0: if (icmph->icmp6_type != ICMP_UNREACH) { michael@0: /* We only care about unreachable */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return; michael@0: } michael@0: if ((icmph->icmp6_code == ICMP_UNREACH_NET) || michael@0: (icmph->icmp6_code == ICMP_UNREACH_HOST) || michael@0: (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) || michael@0: (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) || michael@0: (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) || michael@0: (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) || michael@0: (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) || michael@0: #if defined(__Panda__) michael@0: (icmph->icmp6_code == ICMP_UNREACH_ADMIN)) { michael@0: #elif defined(__Userspace_os_NetBSD) michael@0: (icmph->icmp6_code == ICMP_UNREACH_ADMIN_PROHIBIT)) { michael@0: #else michael@0: (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) { michael@0: #endif michael@0: michael@0: /* michael@0: * Hmm reachablity problems we must examine closely. If its michael@0: * not reachable, we may have lost a network. Or if there is michael@0: * NO protocol at the other end named SCTP. well we consider michael@0: * it a OOTB abort. michael@0: */ michael@0: if (net->dest_state & SCTP_ADDR_REACHABLE) { michael@0: /* Ok that destination is NOT reachable */ michael@0: net->dest_state &= ~SCTP_ADDR_REACHABLE; michael@0: net->dest_state &= ~SCTP_ADDR_PF; michael@0: sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, michael@0: stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) || michael@0: (icmph->icmp6_code == ICMP_UNREACH_PORT)) { michael@0: /* michael@0: * Here the peer is either playing tricks on us, michael@0: * including an address that belongs to someone who michael@0: * does not support SCTP OR was a userland michael@0: * implementation that shutdown and now is dead. In michael@0: * either case treat it like a OOTB abort with no michael@0: * TCB michael@0: */ michael@0: sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED); 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_SCTP_USRREQ+SCTP_LOC_2); michael@0: #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) michael@0: SCTP_SOCKET_UNLOCK(so, 1); michael@0: /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed.*/ michael@0: #endif michael@0: /* no need to unlock here, since the TCB is gone */ michael@0: } else { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: #if !defined(__Panda__) && !defined(__Userspace__) michael@0: void michael@0: sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d) michael@0: { michael@0: struct sctphdr sh; michael@0: struct ip6ctlparam *ip6cp = NULL; michael@0: uint32_t vrf_id; michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: vrf_id = SCTP_DEFAULT_VRFID; michael@0: #endif michael@0: michael@0: #ifdef HAVE_SA_LEN michael@0: if (pktdst->sa_family != AF_INET6 || michael@0: pktdst->sa_len != sizeof(struct sockaddr_in6)) michael@0: #else michael@0: if (pktdst->sa_family != AF_INET6) michael@0: #endif michael@0: return; michael@0: michael@0: if ((unsigned)cmd >= PRC_NCMDS) michael@0: return; michael@0: if (PRC_IS_REDIRECT(cmd)) { michael@0: d = NULL; michael@0: } else if (inet6ctlerrmap[cmd] == 0) { michael@0: return; michael@0: } michael@0: /* if the parameter is from icmp6, decode it. */ michael@0: if (d != NULL) { michael@0: ip6cp = (struct ip6ctlparam *)d; michael@0: } else { michael@0: ip6cp = (struct ip6ctlparam *)NULL; michael@0: } michael@0: michael@0: if (ip6cp) { michael@0: /* michael@0: * XXX: We assume that when IPV6 is non NULL, M and OFF are michael@0: * valid. michael@0: */ michael@0: /* check if we can safely examine src and dst ports */ michael@0: struct sctp_inpcb *inp = NULL; michael@0: struct sctp_tcb *stcb = NULL; michael@0: struct sctp_nets *net = NULL; michael@0: struct sockaddr_in6 final; michael@0: michael@0: if (ip6cp->ip6c_m == NULL) michael@0: return; michael@0: michael@0: bzero(&sh, sizeof(sh)); michael@0: bzero(&final, sizeof(final)); michael@0: inp = NULL; michael@0: net = NULL; michael@0: m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh), michael@0: (caddr_t)&sh); michael@0: ip6cp->ip6c_src->sin6_port = sh.src_port; michael@0: #ifdef HAVE_SIN6_LEN michael@0: final.sin6_len = sizeof(final); michael@0: #endif michael@0: final.sin6_family = AF_INET6; michael@0: #if defined(__FreeBSD__) && __FreeBSD_cc_version < 440000 michael@0: final.sin6_addr = *ip6cp->ip6c_finaldst; michael@0: #else michael@0: final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr; michael@0: #endif /* __FreeBSD_cc_version */ michael@0: final.sin6_port = sh.dest_port; michael@0: stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final, michael@0: (struct sockaddr *)ip6cp->ip6c_src, michael@0: &inp, &net, 1, vrf_id); michael@0: /* inp's ref-count increased && stcb locked */ michael@0: if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { michael@0: if (cmd == PRC_MSGSIZE) { michael@0: sctp6_notify_mbuf(inp, michael@0: ip6cp->ip6c_icmp6, michael@0: &sh, michael@0: stcb, michael@0: net); michael@0: /* inp's ref-count reduced && stcb unlocked */ michael@0: } else { michael@0: sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh, michael@0: (struct sockaddr *)&final, michael@0: stcb, net); michael@0: /* inp's ref-count reduced && stcb unlocked */ michael@0: } michael@0: } else { michael@0: #if !defined(__Windows__) michael@0: if (PRC_IS_REDIRECT(cmd) && inp) { michael@0: in6_rtchange((struct in6pcb *)inp, michael@0: inet6ctlerrmap[cmd]); michael@0: } michael@0: #endif michael@0: if (inp) { michael@0: /* reduce inp's ref-count */ michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: } michael@0: if (stcb) michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * this routine can probably be collasped into the one in sctp_userreq.c michael@0: * since they do the same thing and now we lookup with a sockaddr michael@0: */ michael@0: #ifdef __FreeBSD__ michael@0: static int michael@0: sctp6_getcred(SYSCTL_HANDLER_ARGS) michael@0: { michael@0: struct xucred xuc; michael@0: struct sockaddr_in6 addrs[2]; michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_nets *net; michael@0: struct sctp_tcb *stcb; michael@0: int error; michael@0: uint32_t vrf_id; michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) michael@0: vrf_id = SCTP_DEFAULT_VRFID; michael@0: #else michael@0: vrf_id = panda_get_vrf_from_call(); /* from connectx call? */ michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version > 602000 michael@0: error = priv_check(req->td, PRIV_NETINET_GETCRED); michael@0: #elif defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: error = suser(req->td); michael@0: #else michael@0: error = suser(req->p); michael@0: #endif michael@0: if (error) michael@0: return (error); michael@0: michael@0: if (req->newlen != sizeof(addrs)) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: if (req->oldlen != sizeof(struct ucred)) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: error = SYSCTL_IN(req, addrs, sizeof(addrs)); michael@0: if (error) michael@0: return (error); michael@0: michael@0: stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]), michael@0: sin6tosa(&addrs[0]), michael@0: &inp, &net, 1, vrf_id); michael@0: if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { michael@0: if ((inp != NULL) && (stcb == NULL)) { michael@0: /* reduce ref-count */ michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_DECR_REF(inp); michael@0: goto cred_can_cont; michael@0: } michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); michael@0: error = ENOENT; michael@0: goto out; michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: /* We use the write lock here, only michael@0: * since in the error leg we need it. michael@0: * If we used RLOCK, then we would have michael@0: * to wlock/decr/unlock/rlock. Which michael@0: * in theory could create a hole. Better michael@0: * to use higher wlock. michael@0: */ michael@0: SCTP_INP_WLOCK(inp); michael@0: cred_can_cont: michael@0: error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); michael@0: if (error) { michael@0: SCTP_INP_WUNLOCK(inp); michael@0: goto out; michael@0: } michael@0: cru2x(inp->sctp_socket->so_cred, &xuc); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); michael@0: out: michael@0: return (error); michael@0: } michael@0: michael@0: SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, michael@0: 0, 0, michael@0: sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); michael@0: michael@0: #endif michael@0: michael@0: /* This is the same as the sctp_abort() could be made common */ michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__) michael@0: static void michael@0: #elif defined(__Panda__) || defined(__Userspace__) michael@0: int michael@0: #else michael@0: static int michael@0: #endif michael@0: sctp6_abort(struct socket *so) michael@0: { michael@0: struct sctp_inpcb *inp; michael@0: uint32_t flags; michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__) michael@0: return; michael@0: #else michael@0: return (EINVAL); michael@0: #endif michael@0: } michael@0: sctp_must_try_again: michael@0: flags = inp->sctp_flags; michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 17); michael@0: #endif michael@0: if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && michael@0: (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 16); michael@0: #endif michael@0: sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, michael@0: SCTP_CALLED_AFTER_CMPSET_OFCLOSE); michael@0: SOCK_LOCK(so); michael@0: SCTP_SB_CLEAR(so->so_snd); michael@0: /* same for the rcv ones, they are only michael@0: * here for the accounting/select. michael@0: */ michael@0: SCTP_SB_CLEAR(so->so_rcv); michael@0: #if defined(__APPLE__) michael@0: so->so_usecount--; michael@0: #else michael@0: /* Now null out the reference, we are completely detached. */ michael@0: so->so_pcb = NULL; michael@0: #endif michael@0: SOCK_UNLOCK(so); michael@0: } else { michael@0: flags = inp->sctp_flags; michael@0: if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { michael@0: goto sctp_must_try_again; michael@0: } michael@0: } michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__) michael@0: return; michael@0: #else michael@0: return (0); michael@0: #endif michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: static int michael@0: sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED) michael@0: #elif defined(__Panda__) || defined(__Userspace__) michael@0: int michael@0: sctp6_attach(struct socket *so, int proto SCTP_UNUSED, uint32_t vrf_id) michael@0: #elif defined(__Windows__) michael@0: static int michael@0: sctp6_attach(struct socket *so, int proto SCTP_UNUSED, PKTHREAD p SCTP_UNUSED) michael@0: #else michael@0: static int michael@0: sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct proc *p SCTP_UNUSED) michael@0: #endif michael@0: { michael@0: struct in6pcb *inp6; michael@0: int error; michael@0: struct sctp_inpcb *inp; michael@0: #if !defined(__Panda__) && !defined(__Userspace__) michael@0: uint32_t vrf_id = SCTP_DEFAULT_VRFID; michael@0: #endif michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp != NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: michael@0: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { michael@0: error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); michael@0: if (error) michael@0: return (error); michael@0: } michael@0: error = sctp_inpcb_alloc(so, vrf_id); michael@0: if (error) michael@0: return (error); michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: SCTP_INP_WLOCK(inp); michael@0: inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ michael@0: inp6 = (struct in6pcb *)inp; michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__) michael@0: inp6->inp_vflag |= INP_IPV6; michael@0: #else michael@0: inp->inp_vflag |= INP_IPV6; michael@0: #endif michael@0: #if !defined(__Panda__) michael@0: inp6->in6p_hops = -1; /* use kernel default */ michael@0: inp6->in6p_cksum = -1; /* just to be sure */ michael@0: #endif michael@0: #ifdef INET michael@0: /* michael@0: * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 michael@0: * socket as well, because the socket may be bound to an IPv6 michael@0: * wildcard address, which may match an IPv4-mapped IPv6 address. michael@0: */ michael@0: inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl); michael@0: #endif michael@0: /* michael@0: * Hmm what about the IPSEC stuff that is missing here but in michael@0: * sctp_attach()? michael@0: */ michael@0: SCTP_INP_WUNLOCK(inp); michael@0: return (0); michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: static int michael@0: sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) michael@0: { michael@0: #elif defined(__FreeBSD__) || defined(__APPLE__) michael@0: static int michael@0: sctp6_bind(struct socket *so, struct sockaddr *addr, struct proc *p) michael@0: { michael@0: #elif defined(__Panda__) || defined(__Userspace__) michael@0: int michael@0: sctp6_bind(struct socket *so, struct sockaddr *addr, void * p) michael@0: { michael@0: #elif defined(__Windows__) michael@0: static int michael@0: sctp6_bind(struct socket *so, struct sockaddr *addr, PKTHREAD p) michael@0: { michael@0: #else michael@0: static int michael@0: sctp6_bind(struct socket *so, struct mbuf *nam, struct proc *p) michael@0: { michael@0: struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL; michael@0: michael@0: #endif michael@0: struct sctp_inpcb *inp; michael@0: struct in6pcb *inp6; michael@0: int error; michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: michael@0: #if !defined(__Windows__) michael@0: if (addr) { michael@0: switch (addr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #ifdef HAVE_SA_LEN michael@0: if (addr->sa_len != sizeof(struct sockaddr_in)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #ifdef HAVE_SA_LEN michael@0: if (addr->sa_len != sizeof(struct sockaddr_in6)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: break; michael@0: #endif michael@0: default: michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: } michael@0: #endif michael@0: inp6 = (struct in6pcb *)inp; michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__) michael@0: inp6->inp_vflag &= ~INP_IPV4; michael@0: inp6->inp_vflag |= INP_IPV6; michael@0: #else michael@0: inp->inp_vflag &= ~INP_IPV4; michael@0: inp->inp_vflag |= INP_IPV6; michael@0: #endif michael@0: if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) { michael@0: switch (addr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: /* binding v4 addr to v6 socket, so reset flags */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__) michael@0: inp6->inp_vflag |= INP_IPV4; michael@0: inp6->inp_vflag &= ~INP_IPV6; michael@0: #else michael@0: inp->inp_vflag |= INP_IPV4; michael@0: inp->inp_vflag &= ~INP_IPV6; 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_p; michael@0: michael@0: sin6_p = (struct sockaddr_in6 *)addr; michael@0: michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__) michael@0: inp6->inp_vflag |= INP_IPV4; michael@0: #else michael@0: inp->inp_vflag |= INP_IPV4; michael@0: #endif michael@0: } michael@0: #ifdef INET michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { michael@0: struct sockaddr_in sin; michael@0: michael@0: in6_sin6_2_sin(&sin, sin6_p); michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__) michael@0: inp6->inp_vflag |= INP_IPV4; michael@0: inp6->inp_vflag &= ~INP_IPV6; michael@0: #else michael@0: inp->inp_vflag |= INP_IPV4; michael@0: inp->inp_vflag &= ~INP_IPV6; michael@0: #endif michael@0: error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p); michael@0: return (error); michael@0: } michael@0: #endif michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } else if (addr != NULL) { michael@0: struct sockaddr_in6 *sin6_p; michael@0: michael@0: /* IPV6_V6ONLY socket */ michael@0: #ifdef INET michael@0: if (addr->sa_family == AF_INET) { michael@0: /* can't bind v4 addr to v6 only socket! */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: sin6_p = (struct sockaddr_in6 *)addr; michael@0: michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { michael@0: /* can't bind v4-mapped addrs either! */ michael@0: /* NOTE: we don't support SIIT */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: } michael@0: error = sctp_inpcb_bind(so, addr, NULL, p); michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__) || defined(__Userspace__) michael@0: #if !defined(__Userspace__) michael@0: static void michael@0: #else michael@0: void michael@0: #endif michael@0: sctp6_close(struct socket *so) michael@0: { michael@0: sctp_close(so); michael@0: } michael@0: michael@0: /* This could be made common with sctp_detach() since they are identical */ michael@0: #else michael@0: michael@0: #if !defined(__Panda__) michael@0: static michael@0: #endif michael@0: int michael@0: sctp6_detach(struct socket *so) michael@0: { michael@0: #if defined(__Userspace__) michael@0: sctp_close(so); michael@0: return (0); michael@0: #else michael@0: return (sctp_detach(so)); michael@0: #endif michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #if !defined(__Panda__) && !defined(__Userspace__) michael@0: static michael@0: #endif michael@0: int michael@0: sctp6_disconnect(struct socket *so) michael@0: { michael@0: return (sctp_disconnect(so)); michael@0: } michael@0: michael@0: michael@0: int michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, michael@0: struct mbuf *control, struct thread *p); michael@0: michael@0: #else michael@0: sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, michael@0: struct mbuf *control, struct proc *p); michael@0: michael@0: #endif michael@0: michael@0: #if !defined(__Panda__) && !defined(__Windows__) && !defined(__Userspace__) michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: static int michael@0: sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, michael@0: struct mbuf *control, struct thread *p) michael@0: { michael@0: #elif defined(__FreeBSD__) || defined(__APPLE__) michael@0: static int michael@0: sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, michael@0: struct mbuf *control, struct proc *p) michael@0: { michael@0: #else michael@0: static int michael@0: sctp6_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam, michael@0: struct mbuf *control, struct proc *p) michael@0: { michael@0: struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL; michael@0: #endif michael@0: struct sctp_inpcb *inp; michael@0: struct in6pcb *inp6; michael@0: michael@0: #ifdef INET michael@0: struct sockaddr_in6 *sin6; michael@0: #endif /* INET */ michael@0: /* No SPL needed since sctp_output does this */ michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp == NULL) { michael@0: if (control) { michael@0: SCTP_RELEASE_PKT(control); michael@0: control = NULL; michael@0: } michael@0: SCTP_RELEASE_PKT(m); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: inp6 = (struct in6pcb *)inp; michael@0: /* michael@0: * For the TCP model we may get a NULL addr, if we are a connected michael@0: * socket thats ok. michael@0: */ michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && michael@0: (addr == NULL)) { michael@0: goto connected_type; michael@0: } michael@0: if (addr == NULL) { michael@0: SCTP_RELEASE_PKT(m); michael@0: if (control) { michael@0: SCTP_RELEASE_PKT(control); michael@0: control = NULL; michael@0: } michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ); michael@0: return (EDESTADDRREQ); michael@0: } michael@0: #ifdef INET michael@0: sin6 = (struct sockaddr_in6 *)addr; michael@0: if (SCTP_IPV6_V6ONLY(inp6)) { michael@0: /* michael@0: * if IPV6_V6ONLY flag, we discard datagrams destined to a michael@0: * v4 addr or v4-mapped addr michael@0: */ michael@0: if (addr->sa_family == AF_INET) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: } michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: struct sockaddr_in sin; michael@0: michael@0: /* convert v4-mapped into v4 addr and send */ michael@0: in6_sin6_2_sin(&sin, sin6); michael@0: return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p)); michael@0: } michael@0: #endif /* INET */ michael@0: connected_type: michael@0: /* now what about control */ michael@0: if (control) { michael@0: if (inp->control) { michael@0: SCTP_PRINTF("huh? control set?\n"); michael@0: SCTP_RELEASE_PKT(inp->control); michael@0: inp->control = NULL; michael@0: } michael@0: inp->control = control; michael@0: } michael@0: /* Place the data */ michael@0: if (inp->pkt) { michael@0: SCTP_BUF_NEXT(inp->pkt_last) = m; michael@0: inp->pkt_last = m; michael@0: } else { michael@0: inp->pkt_last = inp->pkt = m; michael@0: } michael@0: if ( michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) michael@0: /* FreeBSD and MacOSX uses a flag passed */ michael@0: ((flags & PRUS_MORETOCOME) == 0) michael@0: #else michael@0: 1 /* Open BSD does not have any "more to come" michael@0: * indication */ michael@0: #endif michael@0: ) { michael@0: /* michael@0: * note with the current version this code will only be used michael@0: * by OpenBSD, NetBSD and FreeBSD have methods for michael@0: * re-defining sosend() to use sctp_sosend(). One can michael@0: * optionaly switch back to this code (by changing back the michael@0: * defininitions but this is not advisable. michael@0: */ michael@0: int ret; michael@0: michael@0: ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); michael@0: inp->pkt = NULL; michael@0: inp->control = NULL; michael@0: return (ret); michael@0: } else { michael@0: return (0); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: static int michael@0: sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) michael@0: { michael@0: #elif defined(__FreeBSD__) || defined(__APPLE__) michael@0: static int michael@0: sctp6_connect(struct socket *so, struct sockaddr *addr, struct proc *p) michael@0: { michael@0: #elif defined(__Panda__) michael@0: int michael@0: sctp6_connect(struct socket *so, struct sockaddr *addr, void *p) michael@0: { michael@0: #elif defined(__Windows__) michael@0: static int michael@0: sctp6_connect(struct socket *so, struct sockaddr *addr, PKTHREAD p) michael@0: { michael@0: #elif defined(__Userspace__) michael@0: int michael@0: sctp6_connect(struct socket *so, struct sockaddr *addr) michael@0: { michael@0: void *p = NULL; michael@0: #else michael@0: static int michael@0: sctp6_connect(struct socket *so, struct mbuf *nam, struct proc *p) michael@0: { michael@0: struct sockaddr *addr = mtod(nam, struct sockaddr *); michael@0: #endif michael@0: uint32_t vrf_id; michael@0: int error = 0; michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb; michael@0: #ifdef INET michael@0: struct in6pcb *inp6; michael@0: struct sockaddr_in6 *sin6; michael@0: struct sockaddr_storage ss; michael@0: #endif michael@0: michael@0: #ifdef INET michael@0: inp6 = (struct in6pcb *)so->so_pcb; michael@0: #endif michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); michael@0: return (ECONNRESET); /* I made the same as TCP since we are michael@0: * not setup? */ michael@0: } michael@0: if (addr == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #if !defined(__Windows__) michael@0: switch (addr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #ifdef HAVE_SA_LEN michael@0: if (addr->sa_len != sizeof(struct sockaddr_in)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #ifdef HAVE_SA_LEN michael@0: if (addr->sa_len != sizeof(struct sockaddr_in6)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: break; michael@0: #endif michael@0: default: michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: michael@0: vrf_id = inp->def_vrf_id; michael@0: SCTP_ASOC_CREATE_LOCK(inp); michael@0: SCTP_INP_RLOCK(inp); michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == michael@0: SCTP_PCB_FLAGS_UNBOUND) { michael@0: /* Bind a ephemeral port */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: error = sctp6_bind(so, NULL, p); michael@0: if (error) { michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: michael@0: return (error); michael@0: } michael@0: SCTP_INP_RLOCK(inp); michael@0: } michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { michael@0: /* We are already connected AND the TCP model */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE); michael@0: return (EADDRINUSE); michael@0: } michael@0: #ifdef INET michael@0: sin6 = (struct sockaddr_in6 *)addr; michael@0: if (SCTP_IPV6_V6ONLY(inp6)) { michael@0: /* michael@0: * if IPV6_V6ONLY flag, ignore connections destined to a v4 michael@0: * addr or v4-mapped addr michael@0: */ michael@0: if (addr->sa_family == AF_INET) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: } michael@0: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { michael@0: /* convert v4-mapped into v4 addr */ michael@0: in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6); michael@0: addr = (struct sockaddr *)&ss; michael@0: } michael@0: #endif /* INET */ michael@0: /* Now do we connect? */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { michael@0: stcb = LIST_FIRST(&inp->sctp_asoc_list); michael@0: if (stcb) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } else { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_INCR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); michael@0: if (stcb == NULL) { michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: } michael@0: } michael@0: michael@0: if (stcb != NULL) { michael@0: /* Already have or am bring up an association */ michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY); michael@0: return (EALREADY); michael@0: } michael@0: /* We are GOOD to go */ michael@0: stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: if (stcb == NULL) { michael@0: /* Gak! no memory */ michael@0: return (error); michael@0: } michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { michael@0: stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; michael@0: /* Set the connected flag so we can queue data */ michael@0: soisconnecting(so); michael@0: } michael@0: stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; michael@0: (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); michael@0: michael@0: /* initialize authentication parameters for the assoc */ michael@0: sctp_initialize_auth_params(inp, stcb); michael@0: michael@0: sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return (error); michael@0: } michael@0: michael@0: static int michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: sctp6_getaddr(struct socket *so, struct sockaddr **addr) michael@0: { michael@0: struct sockaddr_in6 *sin6; michael@0: #elif defined(__Panda__) michael@0: sctp6_getaddr(struct socket *so, struct sockaddr *addr) michael@0: { michael@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; michael@0: #else michael@0: sctp6_getaddr(struct socket *so, struct mbuf *nam) michael@0: { michael@0: struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *); michael@0: #endif michael@0: struct sctp_inpcb *inp; michael@0: uint32_t vrf_id; michael@0: struct sctp_ifa *sctp_ifa; michael@0: michael@0: #ifdef SCTP_KAME michael@0: int error; michael@0: #endif /* SCTP_KAME */ michael@0: michael@0: /* michael@0: * Do the malloc first in case it blocks. michael@0: */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6)); michael@0: if (sin6 == NULL) michael@0: return (ENOMEM); michael@0: #elif defined(__Panda__) michael@0: bzero(sin6, sizeof(*sin6)); michael@0: #else michael@0: SCTP_BUF_LEN(nam) = sizeof(*sin6); michael@0: bzero(sin6, sizeof(*sin6)); michael@0: #endif michael@0: sin6->sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6->sin6_len = sizeof(*sin6); michael@0: #endif michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp == NULL) { michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: SCTP_FREE_SONAME(sin6); michael@0: #endif michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); michael@0: return (ECONNRESET); michael@0: } michael@0: SCTP_INP_RLOCK(inp); michael@0: sin6->sin6_port = inp->sctp_lport; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: /* For the bound all case you get back 0 */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { michael@0: struct sctp_tcb *stcb; michael@0: struct sockaddr_in6 *sin_a6; michael@0: struct sctp_nets *net; michael@0: int fnd; michael@0: stcb = LIST_FIRST(&inp->sctp_asoc_list); michael@0: if (stcb == NULL) { michael@0: goto notConn6; michael@0: } michael@0: fnd = 0; michael@0: sin_a6 = NULL; michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: if (sin_a6 == NULL) michael@0: /* this will make coverity happy */ michael@0: continue; michael@0: michael@0: if (sin_a6->sin6_family == AF_INET6) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: if ((!fnd) || (sin_a6 == NULL)) { michael@0: /* punt */ michael@0: goto notConn6; michael@0: } michael@0: vrf_id = inp->def_vrf_id; michael@0: sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id); michael@0: if (sctp_ifa) { michael@0: sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; michael@0: } michael@0: } else { michael@0: /* For the bound all case you get back 0 */ michael@0: notConn6: michael@0: memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); michael@0: } michael@0: } else { michael@0: /* Take the first IPv6 address in the list */ michael@0: struct sctp_laddr *laddr; michael@0: int fnd = 0; michael@0: michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa->address.sa.sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *sin_a; michael@0: michael@0: sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; michael@0: sin6->sin6_addr = sin_a->sin6_addr; michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (!fnd) { michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: SCTP_FREE_SONAME(sin6); michael@0: #endif michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); michael@0: return (ENOENT); michael@0: } michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: /* Scoping things for v6 */ michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: #ifdef SCTP_KAME michael@0: if ((error = sa6_recoverscope(sin6)) != 0) { michael@0: SCTP_FREE_SONAME(sin6); michael@0: return (error); michael@0: } michael@0: #else michael@0: if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) michael@0: /* skip ifp check below */ michael@0: in6_recoverscope(sin6, &sin6->sin6_addr, NULL); michael@0: else michael@0: sin6->sin6_scope_id = 0; /* XXX */ michael@0: #endif /* SCTP_KAME */ michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: (*addr) = (struct sockaddr *)sin6; michael@0: #endif michael@0: return (0); michael@0: } michael@0: michael@0: static int michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: sctp6_peeraddr(struct socket *so, struct sockaddr **addr) michael@0: { michael@0: struct sockaddr_in6 *sin6; michael@0: #elif defined(__Panda__) michael@0: sctp6_peeraddr(struct socket *so, struct sockaddr *addr) michael@0: { michael@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; michael@0: #else michael@0: sctp6_peeraddr(struct socket *so, struct mbuf *nam) michael@0: { michael@0: struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *); michael@0: #endif michael@0: int fnd; michael@0: struct sockaddr_in6 *sin_a6; michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb; michael@0: struct sctp_nets *net; michael@0: #ifdef SCTP_KAME michael@0: int error; michael@0: #endif michael@0: michael@0: /* Do the malloc first in case it blocks. */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); michael@0: if (sin6 == NULL) michael@0: return (ENOMEM); michael@0: #elif defined(__Panda__) michael@0: memset(sin6, 0, sizeof(*sin6)); michael@0: #else michael@0: SCTP_BUF_LEN(nam) = sizeof(*sin6); michael@0: memset(sin6, 0, sizeof(*sin6)); michael@0: #endif michael@0: sin6->sin6_family = AF_INET6; michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6->sin6_len = sizeof(*sin6); michael@0: #endif michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if ((inp == NULL) || michael@0: ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { michael@0: /* UDP type and listeners will drop out here */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: SCTP_FREE_SONAME(sin6); michael@0: #endif michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN); michael@0: return (ENOTCONN); michael@0: } michael@0: SCTP_INP_RLOCK(inp); michael@0: stcb = LIST_FIRST(&inp->sctp_asoc_list); michael@0: if (stcb) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: if (stcb == NULL) { michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: SCTP_FREE_SONAME(sin6); michael@0: #endif michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); michael@0: return (ECONNRESET); michael@0: } michael@0: fnd = 0; michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: if (sin_a6->sin6_family == AF_INET6) { michael@0: fnd = 1; michael@0: sin6->sin6_port = stcb->rport; michael@0: sin6->sin6_addr = sin_a6->sin6_addr; michael@0: break; michael@0: } michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: if (!fnd) { michael@0: /* No IPv4 address */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: SCTP_FREE_SONAME(sin6); michael@0: #endif michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); michael@0: return (ENOENT); michael@0: } michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: #ifdef SCTP_KAME michael@0: if ((error = sa6_recoverscope(sin6)) != 0) michael@0: return (error); michael@0: #else michael@0: in6_recoverscope(sin6, &sin6->sin6_addr, NULL); michael@0: #endif /* SCTP_KAME */ michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: *addr = (struct sockaddr *)sin6; michael@0: #endif michael@0: return (0); michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: static int michael@0: sctp6_in6getaddr(struct socket *so, struct sockaddr **nam) michael@0: { michael@0: #ifdef INET michael@0: struct sockaddr *addr; michael@0: #endif michael@0: #elif defined(__Panda__) michael@0: int michael@0: sctp6_in6getaddr(struct socket *so, struct sockaddr *nam, uint32_t *namelen) michael@0: { michael@0: struct sockaddr *addr = nam; michael@0: #elif defined(__Userspace__) michael@0: int michael@0: sctp6_in6getaddr(struct socket *so, struct mbuf *nam) michael@0: { michael@0: #ifdef INET michael@0: struct sockaddr *addr = mtod(nam, struct sockaddr *); michael@0: #endif michael@0: #else michael@0: static int michael@0: sctp6_in6getaddr(struct socket *so, struct mbuf *nam) michael@0: { michael@0: #ifdef INET michael@0: struct sockaddr *addr = mtod(nam, struct sockaddr *); michael@0: #endif michael@0: #endif michael@0: struct in6pcb *inp6 = sotoin6pcb(so); michael@0: int error; michael@0: michael@0: if (inp6 == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: michael@0: /* allow v6 addresses precedence */ michael@0: error = sctp6_getaddr(so, nam); michael@0: #ifdef INET michael@0: if (error) { michael@0: /* try v4 next if v6 failed */ michael@0: error = sctp_ingetaddr(so, nam); michael@0: if (error) { michael@0: return (error); michael@0: } michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: addr = *nam; michael@0: #endif michael@0: /* if I'm V6ONLY, convert it to v4-mapped */ michael@0: if (SCTP_IPV6_V6ONLY(inp6)) { michael@0: struct sockaddr_in6 sin6; michael@0: michael@0: in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); michael@0: memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(__Panda__) michael@0: *namelen = nam->sa_len; michael@0: #endif michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: static int michael@0: sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) michael@0: { michael@0: #ifdef INET michael@0: struct sockaddr *addr; michael@0: #endif michael@0: #elif defined(__Panda__) michael@0: int michael@0: sctp6_getpeeraddr(struct socket *so, struct sockaddr *nam, uint32_t *namelen) michael@0: { michael@0: struct sockaddr *addr = (struct sockaddr *)nam; michael@0: #elif defined(__Userspace__) michael@0: int michael@0: sctp6_getpeeraddr(struct socket *so, struct mbuf *nam) michael@0: { michael@0: #ifdef INET michael@0: struct sockaddr *addr = mtod(nam, struct sockaddr *); michael@0: #endif michael@0: #else michael@0: static michael@0: int michael@0: sctp6_getpeeraddr(struct socket *so, struct mbuf *nam) michael@0: { michael@0: #ifdef INET michael@0: struct sockaddr *addr = mtod(nam, struct sockaddr *); michael@0: #endif michael@0: michael@0: #endif michael@0: struct in6pcb *inp6 = sotoin6pcb(so); michael@0: int error; michael@0: michael@0: if (inp6 == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: michael@0: /* allow v6 addresses precedence */ michael@0: error = sctp6_peeraddr(so, nam); michael@0: #ifdef INET michael@0: if (error) { michael@0: /* try v4 next if v6 failed */ michael@0: error = sctp_peeraddr(so, nam); michael@0: if (error) { michael@0: return (error); michael@0: } michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: addr = *nam; michael@0: #endif michael@0: /* if I'm V6ONLY, convert it to v4-mapped */ michael@0: if (SCTP_IPV6_V6ONLY(inp6)) { michael@0: struct sockaddr_in6 sin6; michael@0: michael@0: in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); michael@0: memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(__Panda__) michael@0: *namelen = nam->sa_len; michael@0: #endif michael@0: return (error); michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) michael@0: struct pr_usrreqs sctp6_usrreqs = { michael@0: #if defined(__FreeBSD__) michael@0: .pru_abort = sctp6_abort, michael@0: .pru_accept = sctp_accept, michael@0: .pru_attach = sctp6_attach, michael@0: .pru_bind = sctp6_bind, michael@0: .pru_connect = sctp6_connect, michael@0: .pru_control = in6_control, michael@0: #if __FreeBSD_version >= 690000 michael@0: .pru_close = sctp6_close, michael@0: .pru_detach = sctp6_close, michael@0: .pru_sopoll = sopoll_generic, michael@0: .pru_flush = sctp_flush, michael@0: #else michael@0: .pru_detach = sctp6_detach, michael@0: .pru_sopoll = sopoll, michael@0: #endif michael@0: .pru_disconnect = sctp6_disconnect, michael@0: .pru_listen = sctp_listen, michael@0: .pru_peeraddr = sctp6_getpeeraddr, michael@0: .pru_send = sctp6_send, michael@0: .pru_shutdown = sctp_shutdown, michael@0: .pru_sockaddr = sctp6_in6getaddr, michael@0: .pru_sosend = sctp_sosend, michael@0: .pru_soreceive = sctp_soreceive michael@0: #elif defined(__APPLE__) michael@0: .pru_abort = sctp6_abort, michael@0: .pru_accept = sctp_accept, michael@0: .pru_attach = sctp6_attach, michael@0: .pru_bind = sctp6_bind, michael@0: .pru_connect = sctp6_connect, michael@0: .pru_connect2 = pru_connect2_notsupp, michael@0: .pru_control = in6_control, michael@0: .pru_detach = sctp6_detach, michael@0: .pru_disconnect = sctp6_disconnect, michael@0: .pru_listen = sctp_listen, michael@0: .pru_peeraddr = sctp6_getpeeraddr, michael@0: .pru_rcvd = NULL, michael@0: .pru_rcvoob = pru_rcvoob_notsupp, michael@0: .pru_send = sctp6_send, michael@0: .pru_sense = pru_sense_null, michael@0: .pru_shutdown = sctp_shutdown, michael@0: .pru_sockaddr = sctp6_in6getaddr, michael@0: .pru_sosend = sctp_sosend, michael@0: .pru_soreceive = sctp_soreceive, michael@0: .pru_sopoll = sopoll michael@0: #elif defined(__Windows__) michael@0: sctp6_abort, michael@0: sctp_accept, michael@0: sctp6_attach, michael@0: sctp6_bind, michael@0: sctp6_connect, michael@0: pru_connect2_notsupp, michael@0: NULL, michael@0: NULL, michael@0: sctp6_disconnect, michael@0: sctp_listen, michael@0: sctp6_getpeeraddr, michael@0: NULL, michael@0: pru_rcvoob_notsupp, michael@0: NULL, michael@0: pru_sense_null, michael@0: sctp_shutdown, michael@0: sctp_flush, michael@0: sctp6_in6getaddr, michael@0: sctp_sosend, michael@0: sctp_soreceive, michael@0: sopoll_generic, michael@0: NULL, michael@0: sctp6_close michael@0: #endif michael@0: }; michael@0: michael@0: #elif !defined(__Panda__) && !defined(__Userspace__) michael@0: int michael@0: sctp6_usrreq(so, req, m, nam, control, p) michael@0: struct socket *so; michael@0: int req; michael@0: struct mbuf *m, *nam, *control; michael@0: struct proc *p; michael@0: { michael@0: int s; michael@0: int error = 0; michael@0: int family; michael@0: uint32_t vrf_id; michael@0: family = so->so_proto->pr_domain->dom_family; michael@0: michael@0: if (req == PRU_CONTROL) { michael@0: switch (family) { michael@0: case PF_INET: michael@0: error = in_control(so, (long)m, (caddr_t)nam, michael@0: (struct ifnet *)control michael@0: ); michael@0: #ifdef INET6 michael@0: case PF_INET6: michael@0: error = in6_control(so, (long)m, (caddr_t)nam, michael@0: (struct ifnet *)control, p); michael@0: #endif michael@0: default: michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EAFNOSUPPORT); michael@0: error = EAFNOSUPPORT; michael@0: } michael@0: return (error); michael@0: } michael@0: switch (req) { michael@0: case PRU_ATTACH: michael@0: error = sctp6_attach(so, family, p); michael@0: break; michael@0: case PRU_DETACH: michael@0: error = sctp6_detach(so); michael@0: break; michael@0: case PRU_BIND: michael@0: if (nam == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: error = sctp6_bind(so, nam, p); michael@0: break; michael@0: case PRU_LISTEN: michael@0: error = sctp_listen(so, p); michael@0: break; michael@0: case PRU_CONNECT: michael@0: if (nam == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: error = sctp6_connect(so, nam, p); michael@0: break; michael@0: case PRU_DISCONNECT: michael@0: error = sctp6_disconnect(so); michael@0: break; michael@0: case PRU_ACCEPT: michael@0: if (nam == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: error = sctp_accept(so, nam); michael@0: break; michael@0: case PRU_SHUTDOWN: michael@0: error = sctp_shutdown(so); michael@0: break; michael@0: michael@0: case PRU_RCVD: michael@0: /* michael@0: * For OpenBSD and NetBSD, this is real ugly. The (mbuf *) michael@0: * nam that is passed (by soreceive()) is the int flags cast michael@0: * as a (mbuf *) yuck! michael@0: */ michael@0: error = sctp_usr_recvd(so, (int)((long)nam)); michael@0: break; michael@0: michael@0: case PRU_SEND: michael@0: /* Flags are ignored */ michael@0: error = sctp6_send(so, 0, m, nam, control, p); michael@0: break; michael@0: case PRU_ABORT: michael@0: error = sctp6_abort(so); michael@0: break; michael@0: michael@0: case PRU_SENSE: michael@0: error = 0; michael@0: break; michael@0: case PRU_RCVOOB: michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EAFNOSUPPORT); michael@0: error = EAFNOSUPPORT; michael@0: break; michael@0: case PRU_SENDOOB: michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EAFNOSUPPORT); michael@0: error = EAFNOSUPPORT; michael@0: break; michael@0: case PRU_PEERADDR: michael@0: error = sctp6_getpeeraddr(so, nam); michael@0: break; michael@0: case PRU_SOCKADDR: michael@0: error = sctp6_in6getaddr(so, nam); michael@0: break; michael@0: case PRU_SLOWTIMO: michael@0: error = 0; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: return (error); michael@0: } michael@0: #endif michael@0: #endif