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/sctp_pcb.c 258765 2013-11-30 12:51:19Z tuexen $"); michael@0: #endif michael@0: michael@0: #include michael@0: #ifdef __FreeBSD__ 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: #if defined(__FreeBSD__) && __FreeBSD_version >= 803000 michael@0: #include michael@0: #endif michael@0: #if !defined(__Userspace_os_Windows) michael@0: #include michael@0: #endif michael@0: #ifdef INET6 michael@0: #if defined(__Userspace__) michael@0: #include "user_ip6_var.h" michael@0: #else michael@0: #include michael@0: #endif michael@0: #endif michael@0: #if defined(__FreeBSD__) michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: #define APPLE_FILE_NO 4 michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: VNET_DEFINE(struct sctp_base_info, system_base_info); michael@0: #else michael@0: struct sctp_base_info system_base_info; michael@0: #endif michael@0: michael@0: #if defined(__Userspace__) michael@0: #if defined(INET) || defined(INET6) michael@0: struct ifaddrs *g_interfaces; michael@0: #endif michael@0: #endif michael@0: /* FIX: we don't handle multiple link local scopes */ michael@0: /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */ michael@0: #ifdef INET6 michael@0: int michael@0: SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b) michael@0: { michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: #if defined(__APPLE__) michael@0: struct in6_addr tmp_a, tmp_b; michael@0: michael@0: tmp_a = a->sin6_addr; michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) { michael@0: #else michael@0: if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) { michael@0: #endif michael@0: return (0); michael@0: } michael@0: tmp_b = b->sin6_addr; michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) { michael@0: #else michael@0: if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) { michael@0: #endif michael@0: return (0); michael@0: } michael@0: return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); michael@0: #elif defined(SCTP_KAME) michael@0: struct sockaddr_in6 tmp_a, tmp_b; michael@0: michael@0: memcpy(&tmp_a, a, sizeof(struct sockaddr_in6)); michael@0: if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) { michael@0: return (0); michael@0: } michael@0: memcpy(&tmp_b, b, sizeof(struct sockaddr_in6)); michael@0: if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) { michael@0: return (0); michael@0: } michael@0: return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr)); michael@0: #else michael@0: struct in6_addr tmp_a, tmp_b; michael@0: michael@0: tmp_a = a->sin6_addr; michael@0: if (in6_embedscope(&tmp_a, a) != 0) { michael@0: return (0); michael@0: } michael@0: tmp_b = b->sin6_addr; michael@0: if (in6_embedscope(&tmp_b, b) != 0) { michael@0: return (0); michael@0: } michael@0: return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); michael@0: #endif michael@0: #else michael@0: return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr))); michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb) michael@0: { michael@0: /* michael@0: * We really don't need to lock this, but I will just because it michael@0: * does not hurt. michael@0: */ michael@0: SCTP_INP_INFO_RLOCK(); michael@0: spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep); michael@0: spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc); michael@0: spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr); michael@0: spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr); michael@0: spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk); michael@0: spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq); michael@0: spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq); michael@0: spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: } michael@0: michael@0: /*- michael@0: * Addresses are added to VRF's (Virtual Router's). For BSD we michael@0: * have only the default VRF 0. We maintain a hash list of michael@0: * VRF's. Each VRF has its own list of sctp_ifn's. Each of michael@0: * these has a list of addresses. When we add a new address michael@0: * to a VRF we lookup the ifn/ifn_index, if the ifn does michael@0: * not exist we create it and add it to the list of IFN's michael@0: * within the VRF. Once we have the sctp_ifn, we add the michael@0: * address to the list. So we look something like: michael@0: * michael@0: * hash-vrf-table michael@0: * vrf-> ifn-> ifn -> ifn michael@0: * vrf | michael@0: * ... +--ifa-> ifa -> ifa michael@0: * vrf michael@0: * michael@0: * We keep these separate lists since the SCTP subsystem will michael@0: * point to these from its source address selection nets structure. michael@0: * When an address is deleted it does not happen right away on michael@0: * the SCTP side, it gets scheduled. What we do when a michael@0: * delete happens is immediately remove the address from michael@0: * the master list and decrement the refcount. As our michael@0: * addip iterator works through and frees the src address michael@0: * selection pointing to the sctp_ifa, eventually the refcount michael@0: * will reach 0 and we will delete it. Note that it is assumed michael@0: * that any locking on system level ifn/ifa is done at the michael@0: * caller of these functions and these routines will only michael@0: * lock the SCTP structures as they add or delete things. michael@0: * michael@0: * Other notes on VRF concepts. michael@0: * - An endpoint can be in multiple VRF's michael@0: * - An association lives within a VRF and only one VRF. michael@0: * - Any incoming packet we can deduce the VRF for by michael@0: * looking at the mbuf/pak inbound (for BSD its VRF=0 :D) michael@0: * - Any downward send call or connect call must supply the michael@0: * VRF via ancillary data or via some sort of set default michael@0: * VRF socket option call (again for BSD no brainer since michael@0: * the VRF is always 0). michael@0: * - An endpoint may add multiple VRF's to it. michael@0: * - Listening sockets can accept associations in any michael@0: * of the VRF's they are in but the assoc will end up michael@0: * in only one VRF (gotten from the packet or connect/send). michael@0: * michael@0: */ michael@0: michael@0: struct sctp_vrf * michael@0: sctp_allocate_vrf(int vrf_id) michael@0: { michael@0: struct sctp_vrf *vrf = NULL; michael@0: struct sctp_vrflist *bucket; michael@0: michael@0: /* First allocate the VRF structure */ michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf) { michael@0: /* Already allocated */ michael@0: return (vrf); michael@0: } michael@0: SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf), michael@0: SCTP_M_VRF); michael@0: if (vrf == NULL) { michael@0: /* No memory */ michael@0: #ifdef INVARIANTS michael@0: panic("No memory for VRF:%d", vrf_id); michael@0: #endif michael@0: return (NULL); michael@0: } michael@0: /* setup the VRF */ michael@0: memset(vrf, 0, sizeof(struct sctp_vrf)); michael@0: vrf->vrf_id = vrf_id; michael@0: LIST_INIT(&vrf->ifnlist); michael@0: vrf->total_ifa_count = 0; michael@0: vrf->refcount = 0; michael@0: /* now also setup table ids */ michael@0: SCTP_INIT_VRF_TABLEID(vrf); michael@0: /* Init the HASH of addresses */ michael@0: vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE, michael@0: &vrf->vrf_addr_hashmark); michael@0: if (vrf->vrf_addr_hash == NULL) { michael@0: /* No memory */ michael@0: #ifdef INVARIANTS michael@0: panic("No memory for VRF:%d", vrf_id); michael@0: #endif michael@0: SCTP_FREE(vrf, SCTP_M_VRF); michael@0: return (NULL); michael@0: } michael@0: michael@0: /* Add it to the hash table */ michael@0: bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; michael@0: LIST_INSERT_HEAD(bucket, vrf, next_vrf); michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); michael@0: return (vrf); michael@0: } michael@0: michael@0: michael@0: struct sctp_ifn * michael@0: sctp_find_ifn(void *ifn, uint32_t ifn_index) michael@0: { michael@0: struct sctp_ifn *sctp_ifnp; michael@0: struct sctp_ifnlist *hash_ifn_head; michael@0: michael@0: /* We assume the lock is held for the addresses michael@0: * if that's wrong problems could occur :-) michael@0: */ michael@0: hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; michael@0: LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) { michael@0: if (sctp_ifnp->ifn_index == ifn_index) { michael@0: return (sctp_ifnp); michael@0: } michael@0: if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) { michael@0: return (sctp_ifnp); michael@0: } michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: struct sctp_vrf * michael@0: sctp_find_vrf(uint32_t vrf_id) michael@0: { michael@0: struct sctp_vrflist *bucket; michael@0: struct sctp_vrf *liste; michael@0: michael@0: bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; michael@0: LIST_FOREACH(liste, bucket, next_vrf) { michael@0: if (vrf_id == liste->vrf_id) { michael@0: return (liste); michael@0: } michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_free_vrf(struct sctp_vrf *vrf) michael@0: { michael@0: if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) { michael@0: if (vrf->vrf_addr_hash) { michael@0: SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); michael@0: vrf->vrf_addr_hash = NULL; michael@0: } michael@0: /* We zero'd the count */ michael@0: LIST_REMOVE(vrf, next_vrf); michael@0: SCTP_FREE(vrf, SCTP_M_VRF); michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_free_ifn(struct sctp_ifn *sctp_ifnp) michael@0: { michael@0: if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) { michael@0: /* We zero'd the count */ michael@0: if (sctp_ifnp->vrf) { michael@0: sctp_free_vrf(sctp_ifnp->vrf); michael@0: } michael@0: SCTP_FREE(sctp_ifnp, SCTP_M_IFN); michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu) michael@0: { michael@0: struct sctp_ifn *sctp_ifnp; michael@0: michael@0: sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index); michael@0: if (sctp_ifnp != NULL) { michael@0: sctp_ifnp->ifn_mtu = mtu; michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_free_ifa(struct sctp_ifa *sctp_ifap) michael@0: { michael@0: if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) { michael@0: /* We zero'd the count */ michael@0: if (sctp_ifap->ifn_p) { michael@0: sctp_free_ifn(sctp_ifap->ifn_p); michael@0: } michael@0: SCTP_FREE(sctp_ifap, SCTP_M_IFA); michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock) michael@0: { michael@0: struct sctp_ifn *found; michael@0: michael@0: found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index); michael@0: if (found == NULL) { michael@0: /* Not in the list.. sorry */ michael@0: return; michael@0: } michael@0: if (hold_addr_lock == 0) michael@0: SCTP_IPI_ADDR_WLOCK(); michael@0: LIST_REMOVE(sctp_ifnp, next_bucket); michael@0: LIST_REMOVE(sctp_ifnp, next_ifn); michael@0: SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index, michael@0: sctp_ifnp->registered_af); michael@0: if (hold_addr_lock == 0) michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: /* Take away the reference, and possibly free it */ michael@0: sctp_free_ifn(sctp_ifnp); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr, michael@0: const char *if_name, uint32_t ifn_index) michael@0: { michael@0: struct sctp_vrf *vrf; michael@0: struct sctp_ifa *sctp_ifap; michael@0: michael@0: SCTP_IPI_ADDR_RLOCK(); michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); michael@0: goto out; michael@0: michael@0: } michael@0: sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); michael@0: if (sctp_ifap == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); michael@0: goto out; michael@0: } michael@0: if (sctp_ifap->ifn_p == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n"); michael@0: goto out; michael@0: } michael@0: if (if_name) { michael@0: if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", michael@0: sctp_ifap->ifn_p->ifn_name, if_name); michael@0: goto out; michael@0: } michael@0: } else { michael@0: if (sctp_ifap->ifn_p->ifn_index != ifn_index) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", michael@0: sctp_ifap->ifn_p->ifn_index, ifn_index); michael@0: goto out; michael@0: } michael@0: } michael@0: michael@0: sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID); michael@0: sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; michael@0: out: michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr, michael@0: const char *if_name, uint32_t ifn_index) michael@0: { michael@0: struct sctp_vrf *vrf; michael@0: struct sctp_ifa *sctp_ifap; michael@0: michael@0: SCTP_IPI_ADDR_RLOCK(); michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); michael@0: goto out; michael@0: michael@0: } michael@0: sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); michael@0: if (sctp_ifap == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); michael@0: goto out; michael@0: } michael@0: if (sctp_ifap->ifn_p == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n"); michael@0: goto out; michael@0: } michael@0: if (if_name) { michael@0: if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", michael@0: sctp_ifap->ifn_p->ifn_name, if_name); michael@0: goto out; michael@0: } michael@0: } else { michael@0: if (sctp_ifap->ifn_p->ifn_index != ifn_index) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", michael@0: sctp_ifap->ifn_p->ifn_index, ifn_index); michael@0: goto out; michael@0: } michael@0: } michael@0: michael@0: sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE); michael@0: sctp_ifap->localifa_flags |= SCTP_ADDR_VALID; michael@0: out: michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: } michael@0: michael@0: michael@0: /*- michael@0: * Add an ifa to an ifn. michael@0: * Register the interface as necessary. michael@0: * NOTE: ADDR write lock MUST be held. michael@0: */ michael@0: static void michael@0: sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap) michael@0: { michael@0: int ifa_af; michael@0: michael@0: LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); michael@0: sctp_ifap->ifn_p = sctp_ifnp; michael@0: atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); michael@0: /* update address counts */ michael@0: sctp_ifnp->ifa_count++; michael@0: ifa_af = sctp_ifap->address.sa.sa_family; michael@0: switch (ifa_af) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sctp_ifnp->num_v4++; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sctp_ifnp->num_v6++; michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: if (sctp_ifnp->ifa_count == 1) { michael@0: /* register the new interface */ michael@0: SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af); michael@0: sctp_ifnp->registered_af = ifa_af; michael@0: } michael@0: } michael@0: michael@0: michael@0: /*- michael@0: * Remove an ifa from its ifn. michael@0: * If no more addresses exist, remove the ifn too. Otherwise, re-register michael@0: * the interface based on the remaining address families left. michael@0: * NOTE: ADDR write lock MUST be held. michael@0: */ michael@0: static void michael@0: sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap) michael@0: { michael@0: LIST_REMOVE(sctp_ifap, next_ifa); michael@0: if (sctp_ifap->ifn_p) { michael@0: /* update address counts */ michael@0: sctp_ifap->ifn_p->ifa_count--; michael@0: switch (sctp_ifap->address.sa.sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sctp_ifap->ifn_p->num_v4--; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sctp_ifap->ifn_p->num_v6--; michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) { michael@0: /* remove the ifn, possibly freeing it */ michael@0: sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED); michael@0: } else { michael@0: /* re-register address family type, if needed */ michael@0: if ((sctp_ifap->ifn_p->num_v6 == 0) && michael@0: (sctp_ifap->ifn_p->registered_af == AF_INET6)) { michael@0: SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6); michael@0: SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET); michael@0: sctp_ifap->ifn_p->registered_af = AF_INET; michael@0: } else if ((sctp_ifap->ifn_p->num_v4 == 0) && michael@0: (sctp_ifap->ifn_p->registered_af == AF_INET)) { michael@0: SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET); michael@0: SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6); michael@0: sctp_ifap->ifn_p->registered_af = AF_INET6; michael@0: } michael@0: /* free the ifn refcount */ michael@0: sctp_free_ifn(sctp_ifap->ifn_p); michael@0: } michael@0: sctp_ifap->ifn_p = NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: struct sctp_ifa * michael@0: sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index, michael@0: uint32_t ifn_type, const char *if_name, void *ifa, michael@0: struct sockaddr *addr, uint32_t ifa_flags, michael@0: int dynamic_add) michael@0: { michael@0: struct sctp_vrf *vrf; michael@0: struct sctp_ifn *sctp_ifnp = NULL; michael@0: struct sctp_ifa *sctp_ifap = NULL; michael@0: struct sctp_ifalist *hash_addr_head; michael@0: struct sctp_ifnlist *hash_ifn_head; michael@0: uint32_t hash_of_addr; michael@0: int new_ifn_af = 0; michael@0: michael@0: #ifdef SCTP_DEBUG michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); michael@0: #endif michael@0: SCTP_IPI_ADDR_WLOCK(); michael@0: sctp_ifnp = sctp_find_ifn(ifn, ifn_index); michael@0: if (sctp_ifnp) { michael@0: vrf = sctp_ifnp->vrf; michael@0: } else { michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) { michael@0: vrf = sctp_allocate_vrf(vrf_id); michael@0: if (vrf == NULL) { michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: return (NULL); michael@0: } michael@0: } michael@0: } michael@0: if (sctp_ifnp == NULL) { michael@0: /* build one and add it, can't hold lock michael@0: * until after malloc done though. michael@0: */ michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, michael@0: sizeof(struct sctp_ifn), SCTP_M_IFN); michael@0: if (sctp_ifnp == NULL) { michael@0: #ifdef INVARIANTS michael@0: panic("No memory for IFN"); michael@0: #endif michael@0: return (NULL); michael@0: } michael@0: memset(sctp_ifnp, 0, sizeof(struct sctp_ifn)); michael@0: sctp_ifnp->ifn_index = ifn_index; michael@0: sctp_ifnp->ifn_p = ifn; michael@0: sctp_ifnp->ifn_type = ifn_type; michael@0: sctp_ifnp->refcount = 0; michael@0: sctp_ifnp->vrf = vrf; michael@0: atomic_add_int(&vrf->refcount, 1); michael@0: sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family); michael@0: if (if_name != NULL) { michael@0: snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name); michael@0: } else { michael@0: snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown"); michael@0: } michael@0: hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; michael@0: LIST_INIT(&sctp_ifnp->ifalist); michael@0: SCTP_IPI_ADDR_WLOCK(); michael@0: LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket); michael@0: LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn); michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); michael@0: new_ifn_af = 1; michael@0: } michael@0: sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); michael@0: if (sctp_ifap) { michael@0: /* Hmm, it already exists? */ michael@0: if ((sctp_ifap->ifn_p) && michael@0: (sctp_ifap->ifn_p->ifn_index == ifn_index)) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n", michael@0: sctp_ifap->ifn_p->ifn_name, ifn_index, michael@0: (void *)sctp_ifap); michael@0: if (new_ifn_af) { michael@0: /* Remove the created one that we don't want */ michael@0: sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED); michael@0: } michael@0: if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) { michael@0: /* easy to solve, just switch back to active */ michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n"); michael@0: sctp_ifap->localifa_flags = SCTP_ADDR_VALID; michael@0: sctp_ifap->ifn_p = sctp_ifnp; michael@0: atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); michael@0: } michael@0: exit_stage_left: michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: return (sctp_ifap); michael@0: } else { michael@0: if (sctp_ifap->ifn_p) { michael@0: /* michael@0: * The last IFN gets the address, remove the michael@0: * old one michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n", michael@0: (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name, michael@0: sctp_ifap->ifn_p->ifn_index, if_name, michael@0: ifn_index); michael@0: /* remove the address from the old ifn */ michael@0: sctp_remove_ifa_from_ifn(sctp_ifap); michael@0: /* move the address over to the new ifn */ michael@0: sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); michael@0: goto exit_stage_left; michael@0: } else { michael@0: /* repair ifnp which was NULL ? */ michael@0: sctp_ifap->localifa_flags = SCTP_ADDR_VALID; michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n", michael@0: (void *)sctp_ifnp, (void *)sctp_ifap); michael@0: sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); michael@0: } michael@0: goto exit_stage_left; michael@0: } michael@0: } michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA); michael@0: if (sctp_ifap == NULL) { michael@0: #ifdef INVARIANTS michael@0: panic("No memory for IFA"); michael@0: #endif michael@0: return (NULL); michael@0: } michael@0: memset(sctp_ifap, 0, sizeof(struct sctp_ifa)); michael@0: sctp_ifap->ifn_p = sctp_ifnp; michael@0: atomic_add_int(&sctp_ifnp->refcount, 1); michael@0: sctp_ifap->vrf_id = vrf_id; michael@0: sctp_ifap->ifa = ifa; michael@0: #ifdef HAVE_SA_LEN michael@0: memcpy(&sctp_ifap->address, addr, addr->sa_len); michael@0: #else michael@0: switch (addr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6)); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn)); michael@0: break; michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: #endif michael@0: sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE; michael@0: sctp_ifap->flags = ifa_flags; michael@0: /* Set scope */ michael@0: switch (sctp_ifap->address.sa.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 *)&sctp_ifap->address.sin; michael@0: if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || michael@0: (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) { michael@0: sctp_ifap->src_is_loop = 1; michael@0: } michael@0: if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { michael@0: sctp_ifap->src_is_priv = 1; michael@0: } michael@0: sctp_ifnp->num_v4++; michael@0: if (new_ifn_af) michael@0: new_ifn_af = AF_INET; michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: /* ok to use deprecated addresses? */ michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6; michael@0: if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || michael@0: (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) { michael@0: sctp_ifap->src_is_loop = 1; michael@0: } michael@0: if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { michael@0: sctp_ifap->src_is_priv = 1; michael@0: } michael@0: sctp_ifnp->num_v6++; michael@0: if (new_ifn_af) michael@0: new_ifn_af = AF_INET6; michael@0: break; michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (new_ifn_af) michael@0: new_ifn_af = AF_CONN; michael@0: break; michael@0: #endif michael@0: default: michael@0: new_ifn_af = 0; michael@0: break; michael@0: } michael@0: hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa); michael@0: michael@0: if ((sctp_ifap->src_is_priv == 0) && michael@0: (sctp_ifap->src_is_loop == 0)) { michael@0: sctp_ifap->src_is_glob = 1; michael@0: } michael@0: SCTP_IPI_ADDR_WLOCK(); michael@0: hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; michael@0: LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket); michael@0: sctp_ifap->refcount = 1; michael@0: LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); michael@0: sctp_ifnp->ifa_count++; michael@0: vrf->total_ifa_count++; michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); michael@0: if (new_ifn_af) { michael@0: SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af); michael@0: sctp_ifnp->registered_af = new_ifn_af; michael@0: } michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: if (dynamic_add) { michael@0: /* Bump up the refcount so that when the timer michael@0: * completes it will drop back down. michael@0: */ michael@0: struct sctp_laddr *wi; michael@0: michael@0: atomic_add_int(&sctp_ifap->refcount, 1); michael@0: wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); michael@0: if (wi == NULL) { michael@0: /* michael@0: * Gak, what can we do? We have lost an address michael@0: * change can you say HOSED? michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); michael@0: /* Opps, must decrement the count */ michael@0: sctp_del_addr_from_vrf(vrf_id, addr, ifn_index, michael@0: if_name); michael@0: return (NULL); michael@0: } 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 = sctp_ifap; michael@0: wi->action = SCTP_ADD_IP_ADDRESS; michael@0: michael@0: SCTP_WQ_ADDR_LOCK(); michael@0: LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); michael@0: SCTP_WQ_ADDR_UNLOCK(); michael@0: 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: } else { michael@0: /* it's ready for use */ michael@0: sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE; michael@0: } michael@0: return (sctp_ifap); michael@0: } michael@0: michael@0: void michael@0: sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr, michael@0: uint32_t ifn_index, const char *if_name) michael@0: { michael@0: struct sctp_vrf *vrf; michael@0: struct sctp_ifa *sctp_ifap = NULL; michael@0: michael@0: SCTP_IPI_ADDR_WLOCK(); michael@0: vrf = sctp_find_vrf(vrf_id); michael@0: if (vrf == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); michael@0: goto out_now; michael@0: } michael@0: michael@0: #ifdef SCTP_DEBUG michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); michael@0: #endif michael@0: sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); michael@0: if (sctp_ifap) { michael@0: /* Validate the delete */ michael@0: if (sctp_ifap->ifn_p) { michael@0: int valid = 0; michael@0: /*- michael@0: * The name has priority over the ifn_index michael@0: * if its given. We do this especially for michael@0: * panda who might recycle indexes fast. michael@0: */ michael@0: if (if_name) { michael@0: if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) { michael@0: /* They match its a correct delete */ michael@0: valid = 1; michael@0: } michael@0: } michael@0: if (!valid) { michael@0: /* last ditch check ifn_index */ michael@0: if (ifn_index == sctp_ifap->ifn_p->ifn_index) { michael@0: valid = 1; michael@0: } michael@0: } michael@0: if (!valid) { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n", michael@0: ifn_index, ((if_name == NULL) ? "NULL" : if_name)); michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n", michael@0: sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name); michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: return; michael@0: } michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap); michael@0: sctp_ifap->localifa_flags &= SCTP_ADDR_VALID; michael@0: sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; michael@0: vrf->total_ifa_count--; michael@0: LIST_REMOVE(sctp_ifap, next_bucket); michael@0: sctp_remove_ifa_from_ifn(sctp_ifap); michael@0: } michael@0: #ifdef SCTP_DEBUG michael@0: else { michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:", michael@0: ifn_index); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); michael@0: } michael@0: #endif michael@0: michael@0: out_now: michael@0: SCTP_IPI_ADDR_WUNLOCK(); michael@0: if (sctp_ifap) { michael@0: struct sctp_laddr *wi; michael@0: michael@0: wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); michael@0: if (wi == NULL) { michael@0: /* michael@0: * Gak, what can we do? We have lost an address michael@0: * change can you say HOSED? michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); michael@0: michael@0: /* Oops, must decrement the count */ michael@0: sctp_free_ifa(sctp_ifap); michael@0: return; michael@0: } 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 = sctp_ifap; michael@0: wi->action = SCTP_DEL_IP_ADDRESS; 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: 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: } michael@0: return; michael@0: } michael@0: michael@0: michael@0: static int michael@0: sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to) 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: 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: 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: 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: (!sctp_is_addr_pending(stcb, sctp_ifa))) { michael@0: /* We allow pending addresses, where we michael@0: * have sent an asconf-add to be considered michael@0: * valid. michael@0: */ michael@0: continue; michael@0: } michael@0: if (sctp_ifa->address.sa.sa_family != to->sa_family) { michael@0: continue; michael@0: } 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, *rsin; michael@0: michael@0: sin = &sctp_ifa->address.sin; michael@0: rsin = (struct sockaddr_in *)to; michael@0: if ((ipv4_local_scope == 0) && michael@0: IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { michael@0: continue; michael@0: } michael@0: if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (1); michael@0: } 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, *rsin6; michael@0: #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME) michael@0: struct sockaddr_in6 lsa6; michael@0: #endif michael@0: sin6 = &sctp_ifa->address.sin6; michael@0: rsin6 = (struct sockaddr_in6 *)to; 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: continue; michael@0: #else michael@0: lsa6 = *sin6; michael@0: if (in6_recoverscope(&lsa6, michael@0: &lsa6.sin6_addr, michael@0: NULL)) 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: if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (1); michael@0: } 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: struct sockaddr_conn *sconn, *rsconn; michael@0: michael@0: sconn = &sctp_ifa->address.sconn; michael@0: rsconn = (struct sockaddr_conn *)to; michael@0: if (sconn->sconn_addr == rsconn->sconn_addr) { michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (1); michael@0: } 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: struct sctp_laddr *laddr; michael@0: michael@0: LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); michael@0: continue; michael@0: } michael@0: if (sctp_is_addr_restricted(stcb, laddr->ifa) && michael@0: (!sctp_is_addr_pending(stcb, laddr->ifa))) { michael@0: /* We allow pending addresses, where we michael@0: * have sent an asconf-add to be considered michael@0: * valid. michael@0: */ michael@0: continue; michael@0: } michael@0: if (laddr->ifa->address.sa.sa_family != to->sa_family) { michael@0: continue; michael@0: } michael@0: switch (to->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct sockaddr_in *sin, *rsin; michael@0: michael@0: sin = (struct sockaddr_in *)&laddr->ifa->address.sin; michael@0: rsin = (struct sockaddr_in *)to; michael@0: if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (1); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sockaddr_in6 *sin6, *rsin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; michael@0: rsin6 = (struct sockaddr_in6 *)to; michael@0: if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (1); michael@0: } michael@0: break; michael@0: } michael@0: michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: { michael@0: struct sockaddr_conn *sconn, *rsconn; michael@0: michael@0: sconn = (struct sockaddr_conn *)&laddr->ifa->address.sconn; michael@0: rsconn = (struct sockaddr_conn *)to; michael@0: if (sconn->sconn_addr == rsconn->sconn_addr) { michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (1); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: michael@0: } michael@0: } michael@0: SCTP_IPI_ADDR_RUNLOCK(); michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: static struct sctp_tcb * michael@0: sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from, michael@0: struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id) michael@0: { michael@0: /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */ michael@0: /* michael@0: * If we support the TCP model, then we must now dig through to see michael@0: * if we can find our endpoint in the list of tcp ep's. michael@0: */ michael@0: uint16_t lport, rport; michael@0: struct sctppcbhead *ephead; michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_laddr *laddr; michael@0: struct sctp_tcb *stcb; michael@0: struct sctp_nets *net; michael@0: #ifdef SCTP_MVRF michael@0: int fnd, i; michael@0: #endif michael@0: michael@0: if ((to == NULL) || (from == NULL)) { michael@0: return (NULL); michael@0: } michael@0: michael@0: switch (to->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (from->sa_family == AF_INET) { michael@0: lport = ((struct sockaddr_in *)to)->sin_port; michael@0: rport = ((struct sockaddr_in *)from)->sin_port; michael@0: } else { michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (from->sa_family == AF_INET6) { michael@0: lport = ((struct sockaddr_in6 *)to)->sin6_port; michael@0: rport = ((struct sockaddr_in6 *)from)->sin6_port; michael@0: } else { michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (from->sa_family == AF_CONN) { michael@0: lport = ((struct sockaddr_conn *)to)->sconn_port; michael@0: rport = ((struct sockaddr_conn *)from)->sconn_port; michael@0: } else { michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: return (NULL); michael@0: } michael@0: ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; michael@0: /* michael@0: * Ok now for each of the guys in this bucket we must look and see: michael@0: * - Does the remote port match. - Does there single association's michael@0: * addresses match this address (to). If so we update p_ep to point michael@0: * to this ep and return the tcb from it. michael@0: */ michael@0: LIST_FOREACH(inp, ephead, sctp_hash) { michael@0: SCTP_INP_RLOCK(inp); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: if (lport != inp->sctp_lport) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: #ifdef SCTP_MVRF michael@0: fnd = 0; michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: if (inp->m_vrf_ids[i] == vrf_id) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (fnd == 0) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: #else michael@0: if (inp->def_vrf_id != vrf_id) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: #endif michael@0: /* check to see if the ep has one of the addresses */ michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { michael@0: /* We are NOT bound all, so look further */ michael@0: int match = 0; michael@0: michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: michael@0: if (laddr->ifa == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__); michael@0: continue; michael@0: } michael@0: if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); michael@0: continue; michael@0: } michael@0: if (laddr->ifa->address.sa.sa_family == michael@0: to->sa_family) { michael@0: /* see if it matches */ michael@0: #ifdef INET michael@0: if (from->sa_family == AF_INET) { michael@0: struct sockaddr_in *intf_addr, *sin; michael@0: michael@0: intf_addr = &laddr->ifa->address.sin; michael@0: sin = (struct sockaddr_in *)to; michael@0: if (sin->sin_addr.s_addr == michael@0: intf_addr->sin_addr.s_addr) { michael@0: match = 1; michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (from->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *intf_addr6; michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *) michael@0: to; michael@0: intf_addr6 = &laddr->ifa->address.sin6; michael@0: michael@0: if (SCTP6_ARE_ADDR_EQUAL(sin6, michael@0: intf_addr6)) { michael@0: match = 1; michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: if (from->sa_family == AF_CONN) { michael@0: struct sockaddr_conn *intf_addr, *sconn; michael@0: michael@0: intf_addr = &laddr->ifa->address.sconn; michael@0: sconn = (struct sockaddr_conn *)to; michael@0: if (sconn->sconn_addr == michael@0: intf_addr->sconn_addr) { michael@0: match = 1; michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: if (match == 0) { michael@0: /* This endpoint does not have this address */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: } michael@0: /* michael@0: * Ok if we hit here the ep has the address, does it hold michael@0: * the tcb? michael@0: */ michael@0: /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */ michael@0: stcb = LIST_FIRST(&inp->sctp_asoc_list); michael@0: if (stcb == NULL) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: SCTP_TCB_LOCK(stcb); michael@0: if (!sctp_does_stcb_own_this_addr(stcb, to)) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: if (stcb->rport != rport) { michael@0: /* remote port does not match. */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: if (!sctp_does_stcb_own_this_addr(stcb, to)) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: /* Does this TCB have a matching address? */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: michael@0: if (net->ro._l_addr.sa.sa_family != from->sa_family) { michael@0: /* not the same family, can't be a match */ michael@0: continue; michael@0: } michael@0: switch (from->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct sockaddr_in *sin, *rsin; michael@0: michael@0: sin = (struct sockaddr_in *)&net->ro._l_addr; michael@0: rsin = (struct sockaddr_in *)from; michael@0: if (sin->sin_addr.s_addr == michael@0: rsin->sin_addr.s_addr) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: /* Update the endpoint pointer */ michael@0: *inp_p = inp; michael@0: SCTP_INP_RUNLOCK(inp); michael@0: return (stcb); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sockaddr_in6 *sin6, *rsin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: rsin6 = (struct sockaddr_in6 *)from; michael@0: if (SCTP6_ARE_ADDR_EQUAL(sin6, michael@0: rsin6)) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: /* Update the endpoint pointer */ michael@0: *inp_p = inp; michael@0: SCTP_INP_RUNLOCK(inp); michael@0: return (stcb); michael@0: } 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, *rsconn; michael@0: michael@0: sconn = (struct sockaddr_conn *)&net->ro._l_addr; michael@0: rsconn = (struct sockaddr_conn *)from; michael@0: if (sconn->sconn_addr == rsconn->sconn_addr) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: /* Update the endpoint pointer */ michael@0: *inp_p = inp; michael@0: SCTP_INP_RUNLOCK(inp); michael@0: return (stcb); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * rules for use michael@0: * michael@0: * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an michael@0: * stcb, both will be locked (locked_tcb and stcb) but decrement will be done michael@0: * (if locked == NULL). 3) Decrement happens on return ONLY if locked == michael@0: * NULL. michael@0: */ michael@0: michael@0: struct sctp_tcb * michael@0: sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote, michael@0: struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb) michael@0: { michael@0: struct sctpasochead *head; michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb = NULL; michael@0: struct sctp_nets *net; michael@0: uint16_t rport; michael@0: michael@0: inp = *inp_p; michael@0: switch (remote->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: rport = (((struct sockaddr_in *)remote)->sin_port); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: rport = (((struct sockaddr_in6 *)remote)->sin6_port); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: rport = (((struct sockaddr_in6 *)remote)->sin6_port); michael@0: break; michael@0: #endif michael@0: default: michael@0: return (NULL); michael@0: } michael@0: if (locked_tcb) { michael@0: /* michael@0: * UN-lock so we can do proper locking here this occurs when michael@0: * called from load_addresses_from_init. michael@0: */ michael@0: atomic_add_int(&locked_tcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(locked_tcb); michael@0: } michael@0: SCTP_INP_INFO_RLOCK(); michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: /*- michael@0: * Now either this guy is our listener or it's the michael@0: * connector. If it is the one that issued the connect, then michael@0: * it's only chance is to be the first TCB in the list. If michael@0: * it is the acceptor, then do the special_lookup to hash michael@0: * and find the real inp. michael@0: */ michael@0: if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) { michael@0: /* to is peer addr, from is my addr */ michael@0: #ifndef SCTP_MVRF michael@0: stcb = sctp_tcb_special_locate(inp_p, remote, local, michael@0: netp, inp->def_vrf_id); michael@0: if ((stcb != NULL) && (locked_tcb == NULL)) { michael@0: /* we have a locked tcb, lower refcount */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: if ((locked_tcb != NULL) && (locked_tcb != stcb)) { michael@0: SCTP_INP_RLOCK(locked_tcb->sctp_ep); michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); michael@0: } michael@0: #else michael@0: /*- michael@0: * MVRF is tricky, we must look in every VRF michael@0: * the endpoint has. michael@0: */ michael@0: int i; michael@0: michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: stcb = sctp_tcb_special_locate(inp_p, remote, local, michael@0: netp, inp->m_vrf_ids[i]); michael@0: if ((stcb != NULL) && (locked_tcb == NULL)) { michael@0: /* we have a locked tcb, lower refcount */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: break; michael@0: } michael@0: if ((locked_tcb != NULL) && (locked_tcb != stcb)) { michael@0: SCTP_INP_RLOCK(locked_tcb->sctp_ep); michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } else { michael@0: SCTP_INP_WLOCK(inp); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: goto null_return; michael@0: } michael@0: stcb = LIST_FIRST(&inp->sctp_asoc_list); michael@0: if (stcb == NULL) { michael@0: goto null_return; michael@0: } michael@0: SCTP_TCB_LOCK(stcb); michael@0: michael@0: if (stcb->rport != rport) { michael@0: /* remote port does not match. */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: goto null_return; michael@0: } michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: goto null_return; michael@0: } michael@0: if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: goto null_return; michael@0: } michael@0: /* now look at the list of remote addresses */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: #ifdef INVARIANTS michael@0: if (net == (TAILQ_NEXT(net, sctp_next))) { michael@0: panic("Corrupt net list"); michael@0: } michael@0: #endif michael@0: if (net->ro._l_addr.sa.sa_family != michael@0: remote->sa_family) { michael@0: /* not the same family */ michael@0: continue; michael@0: } michael@0: switch (remote->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct sockaddr_in *sin, *rsin; michael@0: michael@0: sin = (struct sockaddr_in *) michael@0: &net->ro._l_addr; michael@0: rsin = (struct sockaddr_in *)remote; michael@0: if (sin->sin_addr.s_addr == michael@0: rsin->sin_addr.s_addr) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: if (locked_tcb == NULL) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } else if (locked_tcb != stcb) { michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: } michael@0: if (locked_tcb) { michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: } michael@0: michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sockaddr_in6 *sin6, *rsin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: rsin6 = (struct sockaddr_in6 *)remote; michael@0: if (SCTP6_ARE_ADDR_EQUAL(sin6, michael@0: rsin6)) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: if (locked_tcb == NULL) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } else if (locked_tcb != stcb) { michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: } michael@0: if (locked_tcb) { michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } 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, *rsconn; michael@0: michael@0: sconn = (struct sockaddr_conn *)&net->ro._l_addr; michael@0: rsconn = (struct sockaddr_conn *)remote; michael@0: if (sconn->sconn_addr == rsconn->sconn_addr) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: if (locked_tcb == NULL) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } else if (locked_tcb != stcb) { michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: } michael@0: if (locked_tcb) { michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: } else { michael@0: SCTP_INP_WLOCK(inp); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: goto null_return; michael@0: } michael@0: head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport, michael@0: inp->sctp_hashmark)]; michael@0: if (head == NULL) { michael@0: goto null_return; michael@0: } michael@0: LIST_FOREACH(stcb, head, sctp_tcbhash) { michael@0: if (stcb->rport != rport) { michael@0: /* remote port does not match */ michael@0: continue; michael@0: } michael@0: SCTP_TCB_LOCK(stcb); michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: continue; michael@0: } michael@0: if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: continue; michael@0: } michael@0: /* now look at the list of remote addresses */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: #ifdef INVARIANTS michael@0: if (net == (TAILQ_NEXT(net, sctp_next))) { michael@0: panic("Corrupt net list"); michael@0: } michael@0: #endif michael@0: if (net->ro._l_addr.sa.sa_family != michael@0: remote->sa_family) { michael@0: /* not the same family */ michael@0: continue; michael@0: } michael@0: switch (remote->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: { michael@0: struct sockaddr_in *sin, *rsin; michael@0: michael@0: sin = (struct sockaddr_in *) michael@0: &net->ro._l_addr; michael@0: rsin = (struct sockaddr_in *)remote; michael@0: if (sin->sin_addr.s_addr == michael@0: rsin->sin_addr.s_addr) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: if (locked_tcb == NULL) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } else if (locked_tcb != stcb) { michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: } michael@0: if (locked_tcb) { michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: struct sockaddr_in6 *sin6, *rsin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *) michael@0: &net->ro._l_addr; michael@0: rsin6 = (struct sockaddr_in6 *)remote; michael@0: if (SCTP6_ARE_ADDR_EQUAL(sin6, michael@0: rsin6)) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: if (locked_tcb == NULL) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } else if (locked_tcb != stcb) { michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: } michael@0: if (locked_tcb) { michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } 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, *rsconn; michael@0: michael@0: sconn = (struct sockaddr_conn *)&net->ro._l_addr; michael@0: rsconn = (struct sockaddr_conn *)remote; michael@0: if (sconn->sconn_addr == rsconn->sconn_addr) { michael@0: /* found it */ michael@0: if (netp != NULL) { michael@0: *netp = net; michael@0: } michael@0: if (locked_tcb == NULL) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: } else if (locked_tcb != stcb) { michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: } michael@0: if (locked_tcb) { michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: } michael@0: null_return: michael@0: /* clean up for returning null */ michael@0: if (locked_tcb) { michael@0: SCTP_TCB_LOCK(locked_tcb); michael@0: atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: /* not found */ michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Find an association for a specific endpoint using the association id given michael@0: * out in the COMM_UP notification michael@0: */ michael@0: struct sctp_tcb * michael@0: sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) michael@0: { michael@0: /* michael@0: * Use my the assoc_id to find a endpoint michael@0: */ michael@0: struct sctpasochead *head; michael@0: struct sctp_tcb *stcb; michael@0: uint32_t id; michael@0: michael@0: if (inp == NULL) { michael@0: SCTP_PRINTF("TSNH ep_associd\n"); michael@0: return (NULL); michael@0: } michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: SCTP_PRINTF("TSNH ep_associd0\n"); michael@0: return (NULL); michael@0: } michael@0: id = (uint32_t)asoc_id; michael@0: head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; michael@0: if (head == NULL) { michael@0: /* invalid id TSNH */ michael@0: SCTP_PRINTF("TSNH ep_associd1\n"); michael@0: return (NULL); michael@0: } michael@0: LIST_FOREACH(stcb, head, sctp_tcbasocidhash) { michael@0: if (stcb->asoc.assoc_id == id) { michael@0: if (inp != stcb->sctp_ep) { michael@0: /* michael@0: * some other guy has the same id active (id michael@0: * collision ??). michael@0: */ michael@0: SCTP_PRINTF("TSNH ep_associd2\n"); michael@0: continue; michael@0: } michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: continue; michael@0: } michael@0: if (want_lock) { michael@0: SCTP_TCB_LOCK(stcb); michael@0: } michael@0: return (stcb); michael@0: } michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: struct sctp_tcb * michael@0: sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) michael@0: { michael@0: struct sctp_tcb *stcb; michael@0: michael@0: SCTP_INP_RLOCK(inp); michael@0: stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock); michael@0: SCTP_INP_RUNLOCK(inp); michael@0: return (stcb); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Endpoint probe expects that the INP_INFO is locked. michael@0: */ michael@0: static struct sctp_inpcb * michael@0: sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head, michael@0: uint16_t lport, uint32_t vrf_id) michael@0: { michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_laddr *laddr; 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: struct sockaddr_in6 *intf_addr6; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: struct sockaddr_conn *sconn; michael@0: #endif michael@0: #ifdef SCTP_MVRF michael@0: int i; michael@0: #endif michael@0: int fnd; michael@0: michael@0: #ifdef INET michael@0: sin = NULL; michael@0: #endif michael@0: #ifdef INET6 michael@0: sin6 = NULL; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: sconn = NULL; michael@0: #endif michael@0: switch (nam->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sin = (struct sockaddr_in *)nam; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sin6 = (struct sockaddr_in6 *)nam; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: sconn = (struct sockaddr_conn *)nam; michael@0: break; michael@0: #endif michael@0: default: michael@0: /* unsupported family */ michael@0: return (NULL); michael@0: } michael@0: michael@0: if (head == NULL) michael@0: return (NULL); michael@0: michael@0: LIST_FOREACH(inp, head, sctp_hash) { michael@0: SCTP_INP_RLOCK(inp); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) && michael@0: (inp->sctp_lport == lport)) { michael@0: /* got it */ michael@0: #ifdef INET michael@0: if ((nam->sa_family == AF_INET) && michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && michael@0: SCTP_IPV6_V6ONLY(inp)) { michael@0: /* IPv4 on a IPv6 socket with ONLY IPv6 set */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: /* A V6 address and the endpoint is NOT bound V6 */ michael@0: if (nam->sa_family == AF_INET6 && michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: #endif michael@0: /* does a VRF id match? */ michael@0: fnd = 0; michael@0: #ifdef SCTP_MVRF michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: if (inp->m_vrf_ids[i] == vrf_id) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: #else michael@0: if (inp->def_vrf_id == vrf_id) michael@0: fnd = 1; michael@0: #endif michael@0: michael@0: SCTP_INP_RUNLOCK(inp); michael@0: if (!fnd) michael@0: continue; michael@0: return (inp); michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: switch (nam->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (sin->sin_addr.s_addr == INADDR_ANY) { michael@0: /* Can't hunt for one that has no address specified */ michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { michael@0: /* Can't hunt for one that has no address specified */ michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (sconn->sconn_addr == NULL) { michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: /* michael@0: * ok, not bound to all so see if we can find a EP bound to this michael@0: * address. michael@0: */ michael@0: LIST_FOREACH(inp, head, sctp_hash) { michael@0: SCTP_INP_RLOCK(inp); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: /* michael@0: * Ok this could be a likely candidate, look at all of its michael@0: * addresses michael@0: */ michael@0: if (inp->sctp_lport != lport) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: /* does a VRF id match? */ michael@0: fnd = 0; michael@0: #ifdef SCTP_MVRF michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: if (inp->m_vrf_ids[i] == vrf_id) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: #else michael@0: if (inp->def_vrf_id == vrf_id) michael@0: fnd = 1; michael@0: michael@0: #endif michael@0: if (!fnd) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", michael@0: __FUNCTION__); michael@0: continue; michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ", michael@0: (void *)laddr->ifa); michael@0: if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n"); michael@0: continue; michael@0: } michael@0: if (laddr->ifa->address.sa.sa_family == nam->sa_family) { michael@0: /* possible, see if it matches */ michael@0: switch (nam->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #if defined(__APPLE__) michael@0: if (sin == NULL) { michael@0: /* TSNH */ michael@0: break; michael@0: } michael@0: #endif michael@0: if (sin->sin_addr.s_addr == michael@0: laddr->ifa->address.sin.sin_addr.s_addr) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: return (inp); michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: intf_addr6 = &laddr->ifa->address.sin6; michael@0: if (SCTP6_ARE_ADDR_EQUAL(sin6, michael@0: intf_addr6)) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: return (inp); michael@0: } michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: return (inp); michael@0: } michael@0: break; michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: static struct sctp_inpcb * michael@0: sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id) michael@0: { michael@0: struct sctppcbhead *head; michael@0: struct sctp_inpcb *t_inp; michael@0: #ifdef SCTP_MVRF michael@0: int i; michael@0: #endif michael@0: int fnd; michael@0: michael@0: head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, michael@0: SCTP_BASE_INFO(hashmark))]; michael@0: LIST_FOREACH(t_inp, head, sctp_hash) { michael@0: if (t_inp->sctp_lport != lport) { michael@0: continue; michael@0: } michael@0: /* is it in the VRF in question */ michael@0: fnd = 0; michael@0: #ifdef SCTP_MVRF michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: if (t_inp->m_vrf_ids[i] == vrf_id) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: #else michael@0: if (t_inp->def_vrf_id == vrf_id) michael@0: fnd = 1; michael@0: #endif michael@0: if (!fnd) michael@0: continue; michael@0: michael@0: /* This one is in use. */ michael@0: /* check the v6/v4 binding issue */ michael@0: if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && michael@0: SCTP_IPV6_V6ONLY(t_inp)) { michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: /* collision in V6 space */ michael@0: return (t_inp); michael@0: } else { michael@0: /* inp is BOUND_V4 no conflict */ michael@0: continue; michael@0: } michael@0: } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: /* t_inp is bound v4 and v6, conflict always */ michael@0: return (t_inp); michael@0: } else { michael@0: /* t_inp is bound only V4 */ michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && michael@0: SCTP_IPV6_V6ONLY(inp)) { michael@0: /* no conflict */ michael@0: continue; michael@0: } michael@0: /* else fall through to conflict */ michael@0: } michael@0: return (t_inp); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp) michael@0: { michael@0: /* For 1-2-1 with port reuse */ michael@0: struct sctppcbhead *head; michael@0: struct sctp_inpcb *tinp; michael@0: michael@0: if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) { michael@0: /* only works with port reuse on */ michael@0: return (-1); michael@0: } michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) { michael@0: return (0); michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, michael@0: SCTP_BASE_INFO(hashmark))]; michael@0: /* Kick out all non-listeners to the TCP hash */ michael@0: LIST_FOREACH(tinp, head, sctp_hash) { michael@0: if (tinp->sctp_lport != inp->sctp_lport) { michael@0: continue; michael@0: } michael@0: if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: continue; michael@0: } michael@0: if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: continue; michael@0: } michael@0: if (tinp->sctp_socket->so_qlimit) { michael@0: continue; michael@0: } michael@0: SCTP_INP_WLOCK(tinp); michael@0: LIST_REMOVE(tinp, sctp_hash); michael@0: head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))]; michael@0: tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; michael@0: LIST_INSERT_HEAD(head, tinp, sctp_hash); michael@0: SCTP_INP_WUNLOCK(tinp); michael@0: } michael@0: SCTP_INP_WLOCK(inp); michael@0: /* Pull from where he was */ michael@0: LIST_REMOVE(inp, sctp_hash); michael@0: inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL; michael@0: head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))]; michael@0: LIST_INSERT_HEAD(head, inp, sctp_hash); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_RLOCK(inp); michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: struct sctp_inpcb * michael@0: sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, michael@0: uint32_t vrf_id) michael@0: { michael@0: /* michael@0: * First we check the hash table to see if someone has this port michael@0: * bound with just the port. michael@0: */ michael@0: struct sctp_inpcb *inp; michael@0: struct sctppcbhead *head; michael@0: int lport; michael@0: unsigned int i; 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: #if defined(__Userspace__) michael@0: struct sockaddr_conn *sconn; michael@0: #endif michael@0: michael@0: switch (nam->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sin = (struct sockaddr_in *)nam; michael@0: lport = sin->sin_port; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sin6 = (struct sockaddr_in6 *)nam; michael@0: lport = sin6->sin6_port; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: sconn = (struct sockaddr_conn *)nam; michael@0: lport = sconn->sconn_port; michael@0: break; michael@0: #endif michael@0: default: michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * I could cheat here and just cast to one of the types but we will michael@0: * do it right. It also provides the check against an Unsupported michael@0: * type too. michael@0: */ michael@0: /* Find the head of the ALLADDR chain */ michael@0: if (have_lock == 0) { michael@0: SCTP_INP_INFO_RLOCK(); michael@0: } michael@0: head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, michael@0: SCTP_BASE_INFO(hashmark))]; michael@0: inp = sctp_endpoint_probe(nam, head, lport, vrf_id); michael@0: michael@0: /* michael@0: * If the TCP model exists it could be that the main listening michael@0: * endpoint is gone but there still exists a connected socket for this michael@0: * guy. If so we can return the first one that we find. This may NOT michael@0: * be the correct one so the caller should be wary on the returned INP. michael@0: * Currently the only caller that sets find_tcp_pool is in bindx where michael@0: * we are verifying that a user CAN bind the address. He either michael@0: * has bound it already, or someone else has, or its open to bind, michael@0: * so this is good enough. michael@0: */ michael@0: if (inp == NULL && find_tcp_pool) { michael@0: for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) { michael@0: head = &SCTP_BASE_INFO(sctp_tcpephash)[i]; michael@0: inp = sctp_endpoint_probe(nam, head, lport, vrf_id); michael@0: if (inp) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (inp) { michael@0: SCTP_INP_INCR_REF(inp); michael@0: } michael@0: if (have_lock == 0) { michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: } michael@0: return (inp); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Find an association for an endpoint with the pointer to whom you want to michael@0: * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may michael@0: * need to change the *to to some other struct like a mbuf... michael@0: */ michael@0: struct sctp_tcb * michael@0: sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to, michael@0: struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, michael@0: uint32_t vrf_id) michael@0: { michael@0: struct sctp_inpcb *inp = NULL; michael@0: struct sctp_tcb *stcb; michael@0: michael@0: SCTP_INP_INFO_RLOCK(); michael@0: if (find_tcp_pool) { michael@0: if (inp_p != NULL) { michael@0: stcb = sctp_tcb_special_locate(inp_p, from, to, netp, michael@0: vrf_id); michael@0: } else { michael@0: stcb = sctp_tcb_special_locate(&inp, from, to, netp, michael@0: vrf_id); michael@0: } michael@0: if (stcb != NULL) { michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } michael@0: } michael@0: inp = sctp_pcb_findep(to, 0, 1, vrf_id); michael@0: if (inp_p != NULL) { michael@0: *inp_p = inp; michael@0: } michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: if (inp == NULL) { michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * ok, we have an endpoint, now lets find the assoc for it (if any) michael@0: * we now place the source address or from in the to of the find michael@0: * endpoint call. Since in reality this chain is used from the michael@0: * inbound packet side. michael@0: */ michael@0: if (inp_p != NULL) { michael@0: stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to, michael@0: NULL); michael@0: } else { michael@0: stcb = sctp_findassociation_ep_addr(&inp, from, netp, to, michael@0: NULL); michael@0: } michael@0: return (stcb); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * This routine will grub through the mbuf that is a INIT or INIT-ACK and michael@0: * find all addresses that the sender has specified in any address list. Each michael@0: * address will be used to lookup the TCB and see if one exits. michael@0: */ michael@0: static struct sctp_tcb * michael@0: sctp_findassociation_special_addr(struct mbuf *m, int offset, michael@0: struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, michael@0: struct sockaddr *dst) michael@0: { michael@0: struct sctp_paramhdr *phdr, parm_buf; michael@0: #if defined(INET) || defined(INET6) michael@0: struct sctp_tcb *stcb; michael@0: uint16_t ptype; michael@0: #endif michael@0: uint16_t plen; michael@0: #ifdef INET michael@0: struct sockaddr_in sin4; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 sin6; michael@0: #endif michael@0: michael@0: #ifdef INET michael@0: memset(&sin4, 0, sizeof(sin4)); michael@0: #ifdef HAVE_SIN_LEN michael@0: sin4.sin_len = sizeof(sin4); michael@0: #endif michael@0: sin4.sin_family = AF_INET; michael@0: sin4.sin_port = sh->src_port; michael@0: #endif michael@0: #ifdef INET6 michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6.sin6_len = sizeof(sin6); michael@0: #endif michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = sh->src_port; michael@0: #endif michael@0: michael@0: offset += sizeof(struct sctp_init_chunk); michael@0: michael@0: phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); michael@0: while (phdr != NULL) { michael@0: /* now we must see if we want the parameter */ michael@0: #if defined(INET) || defined(INET6) michael@0: ptype = ntohs(phdr->param_type); michael@0: #endif michael@0: plen = ntohs(phdr->param_length); michael@0: if (plen == 0) { michael@0: break; michael@0: } michael@0: #ifdef INET michael@0: if (ptype == SCTP_IPV4_ADDRESS && michael@0: plen == sizeof(struct sctp_ipv4addr_param)) { michael@0: /* Get the rest of the address */ michael@0: struct sctp_ipv4addr_param ip4_parm, *p4; michael@0: michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm))); michael@0: if (phdr == NULL) { michael@0: return (NULL); michael@0: } michael@0: p4 = (struct sctp_ipv4addr_param *)phdr; michael@0: memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr)); michael@0: /* look it up */ michael@0: stcb = sctp_findassociation_ep_addr(inp_p, michael@0: (struct sockaddr *)&sin4, netp, dst, NULL); michael@0: if (stcb != NULL) { michael@0: return (stcb); michael@0: } michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: if (ptype == SCTP_IPV6_ADDRESS && michael@0: plen == sizeof(struct sctp_ipv6addr_param)) { michael@0: /* Get the rest of the address */ michael@0: struct sctp_ipv6addr_param ip6_parm, *p6; michael@0: michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)&ip6_parm, min(plen,sizeof(ip6_parm))); michael@0: if (phdr == NULL) { michael@0: return (NULL); michael@0: } michael@0: p6 = (struct sctp_ipv6addr_param *)phdr; michael@0: memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr)); michael@0: /* look it up */ michael@0: stcb = sctp_findassociation_ep_addr(inp_p, michael@0: (struct sockaddr *)&sin6, netp, dst, NULL); michael@0: if (stcb != NULL) { michael@0: return (stcb); michael@0: } michael@0: } michael@0: #endif michael@0: offset += SCTP_SIZE32(plen); michael@0: phdr = sctp_get_next_param(m, offset, &parm_buf, michael@0: sizeof(parm_buf)); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: static struct sctp_tcb * michael@0: sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag, michael@0: struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport, michael@0: uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag) michael@0: { michael@0: /* michael@0: * Use my vtag to hash. If we find it we then verify the source addr michael@0: * is in the assoc. If all goes well we save a bit on rec of a michael@0: * packet. michael@0: */ michael@0: struct sctpasochead *head; michael@0: struct sctp_nets *net; michael@0: struct sctp_tcb *stcb; michael@0: #ifdef SCTP_MVRF michael@0: unsigned int i; michael@0: #endif michael@0: michael@0: SCTP_INP_INFO_RLOCK(); michael@0: head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag, michael@0: SCTP_BASE_INFO(hashasocmark))]; michael@0: if (head == NULL) { michael@0: /* invalid vtag */ michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (NULL); michael@0: } michael@0: LIST_FOREACH(stcb, head, sctp_asocs) { michael@0: SCTP_INP_RLOCK(stcb->sctp_ep); michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: SCTP_INP_RUNLOCK(stcb->sctp_ep); michael@0: continue; michael@0: } michael@0: #ifdef SCTP_MVRF michael@0: for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) { michael@0: if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) { michael@0: break; michael@0: } michael@0: } michael@0: if (i == stcb->sctp_ep->num_vrfs) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: continue; michael@0: } michael@0: #else michael@0: if (stcb->sctp_ep->def_vrf_id != vrf_id) { michael@0: SCTP_INP_RUNLOCK(stcb->sctp_ep); michael@0: continue; michael@0: } michael@0: #endif michael@0: SCTP_TCB_LOCK(stcb); michael@0: SCTP_INP_RUNLOCK(stcb->sctp_ep); michael@0: if (stcb->asoc.my_vtag == vtag) { michael@0: /* candidate */ michael@0: if (stcb->rport != rport) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: continue; michael@0: } michael@0: if (stcb->sctp_ep->sctp_lport != lport) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: continue; michael@0: } michael@0: if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: continue; michael@0: } michael@0: /* RRS:Need toaddr check here */ michael@0: if (sctp_does_stcb_own_this_addr(stcb, to) == 0) { michael@0: /* Endpoint does not own this address */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: continue; michael@0: } michael@0: if (remote_tag) { michael@0: /* If we have both vtags that's all we match on */ michael@0: if (stcb->asoc.peer_vtag == remote_tag) { michael@0: /* If both tags match we consider it conclusive michael@0: * and check NO source/destination addresses michael@0: */ michael@0: goto conclusive; michael@0: } michael@0: } michael@0: if (skip_src_check) { michael@0: conclusive: michael@0: if (from) { michael@0: *netp = sctp_findnet(stcb, from); michael@0: } else { michael@0: *netp = NULL; /* unknown */ michael@0: } michael@0: if (inp_p) michael@0: *inp_p = stcb->sctp_ep; michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } michael@0: net = sctp_findnet(stcb, from); michael@0: if (net) { michael@0: /* yep its him. */ michael@0: *netp = net; michael@0: SCTP_STAT_INCR(sctps_vtagexpress); michael@0: *inp_p = stcb->sctp_ep; michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (stcb); michael@0: } else { michael@0: /* michael@0: * not him, this should only happen in rare michael@0: * cases so I peg it. michael@0: */ michael@0: SCTP_STAT_INCR(sctps_vtagbogus); michael@0: } michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Find an association with the pointer to the inbound IP packet. This can be michael@0: * a IPv4 or IPv6 packet. michael@0: */ michael@0: struct sctp_tcb * michael@0: sctp_findassociation_addr(struct mbuf *m, int offset, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sctphdr *sh, struct sctp_chunkhdr *ch, michael@0: struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) michael@0: { michael@0: int find_tcp_pool; michael@0: struct sctp_tcb *stcb; michael@0: struct sctp_inpcb *inp; michael@0: michael@0: if (sh->v_tag) { michael@0: /* we only go down this path if vtag is non-zero */ michael@0: stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag), michael@0: inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0); michael@0: if (stcb) { michael@0: return (stcb); michael@0: } michael@0: } michael@0: michael@0: find_tcp_pool = 0; michael@0: if ((ch->chunk_type != SCTP_INITIATION) && michael@0: (ch->chunk_type != SCTP_INITIATION_ACK) && michael@0: (ch->chunk_type != SCTP_COOKIE_ACK) && michael@0: (ch->chunk_type != SCTP_COOKIE_ECHO)) { michael@0: /* Other chunk types go to the tcp pool. */ michael@0: find_tcp_pool = 1; michael@0: } michael@0: if (inp_p) { michael@0: stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp, michael@0: find_tcp_pool, vrf_id); michael@0: inp = *inp_p; michael@0: } else { michael@0: stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp, michael@0: find_tcp_pool, vrf_id); michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp); michael@0: if (stcb == NULL && inp) { michael@0: /* Found a EP but not this address */ michael@0: if ((ch->chunk_type == SCTP_INITIATION) || michael@0: (ch->chunk_type == SCTP_INITIATION_ACK)) { michael@0: /*- michael@0: * special hook, we do NOT return linp or an michael@0: * association that is linked to an existing michael@0: * association that is under the TCP pool (i.e. no michael@0: * listener exists). The endpoint finding routine michael@0: * will always find a listener before examining the michael@0: * TCP pool. michael@0: */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { michael@0: if (inp_p) { michael@0: *inp_p = NULL; michael@0: } michael@0: return (NULL); michael@0: } michael@0: stcb = sctp_findassociation_special_addr(m, michael@0: offset, sh, &inp, netp, dst); michael@0: if (inp_p != NULL) { michael@0: *inp_p = inp; michael@0: } michael@0: } michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb); michael@0: return (stcb); michael@0: } michael@0: michael@0: /* michael@0: * lookup an association by an ASCONF lookup address. michael@0: * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup michael@0: */ michael@0: struct sctp_tcb * michael@0: sctp_findassociation_ep_asconf(struct mbuf *m, int offset, michael@0: struct sockaddr *dst, struct sctphdr *sh, michael@0: struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) michael@0: { michael@0: struct sctp_tcb *stcb; michael@0: struct sockaddr_storage remote_store; michael@0: struct sctp_paramhdr parm_buf, *phdr; michael@0: int ptype; michael@0: int zero_address = 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: memset(&remote_store, 0, sizeof(remote_store)); michael@0: phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk), michael@0: &parm_buf, sizeof(struct sctp_paramhdr)); michael@0: if (phdr == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n", michael@0: __FUNCTION__); michael@0: return NULL; michael@0: } michael@0: ptype = (int)((uint32_t) ntohs(phdr->param_type)); michael@0: /* get the correlation address */ michael@0: switch (ptype) { michael@0: #ifdef INET6 michael@0: case SCTP_IPV6_ADDRESS: michael@0: { michael@0: /* ipv6 address param */ michael@0: struct sctp_ipv6addr_param *p6, p6_buf; michael@0: michael@0: if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) { michael@0: return NULL; michael@0: } michael@0: p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m, michael@0: offset + sizeof(struct sctp_asconf_chunk), michael@0: &p6_buf.ph, sizeof(*p6)); michael@0: if (p6 == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n", michael@0: __FUNCTION__); michael@0: return (NULL); michael@0: } michael@0: sin6 = (struct sockaddr_in6 *)&remote_store; 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: sin6->sin6_port = sh->src_port; michael@0: memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr)); michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) michael@0: zero_address = 1; michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET michael@0: case SCTP_IPV4_ADDRESS: michael@0: { michael@0: /* ipv4 address param */ michael@0: struct sctp_ipv4addr_param *p4, p4_buf; michael@0: michael@0: if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) { michael@0: return NULL; michael@0: } michael@0: p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m, michael@0: offset + sizeof(struct sctp_asconf_chunk), michael@0: &p4_buf.ph, sizeof(*p4)); michael@0: if (p4 == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n", michael@0: __FUNCTION__); michael@0: return (NULL); michael@0: } michael@0: sin = (struct sockaddr_in *)&remote_store; michael@0: sin->sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin->sin_len = sizeof(*sin); michael@0: #endif michael@0: sin->sin_port = sh->src_port; michael@0: memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr)); michael@0: if (sin->sin_addr.s_addr == INADDR_ANY) michael@0: zero_address = 1; michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* invalid address param type */ michael@0: return NULL; michael@0: } michael@0: michael@0: if (zero_address) { michael@0: stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p, michael@0: netp, sh->src_port, sh->dest_port, 1, vrf_id, 0); michael@0: if (stcb != NULL) { michael@0: SCTP_INP_DECR_REF(*inp_p); michael@0: } michael@0: } else { michael@0: stcb = sctp_findassociation_ep_addr(inp_p, michael@0: (struct sockaddr *)&remote_store, netp, michael@0: dst, NULL); michael@0: } michael@0: return (stcb); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * allocate a sctp_inpcb and setup a temporary binding to a port/all michael@0: * addresses. This way if we don't get a bind we by default pick a ephemeral michael@0: * port with all addresses bound. michael@0: */ michael@0: int michael@0: sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id) michael@0: { michael@0: /* michael@0: * we get called when a new endpoint starts up. We need to allocate michael@0: * the sctp_inpcb structure from the zone and init it. Mark it as michael@0: * unbound and find a port that we can use as an ephemeral with michael@0: * INADDR_ANY. If the user binds later no problem we can then add in michael@0: * the specific addresses. And setup the default parameters for the michael@0: * EP. michael@0: */ michael@0: int i, error; michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_pcb *m; michael@0: struct timeval time; michael@0: sctp_sharedkey_t *null_key; michael@0: michael@0: error = 0; michael@0: michael@0: SCTP_INP_INFO_WLOCK(); michael@0: inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb); michael@0: if (inp == NULL) { michael@0: SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n"); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); michael@0: return (ENOBUFS); michael@0: } michael@0: /* zap it */ michael@0: bzero(inp, sizeof(*inp)); michael@0: michael@0: /* bump generations */ michael@0: #if defined(__APPLE__) michael@0: inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE; michael@0: #endif michael@0: /* setup socket pointers */ michael@0: inp->sctp_socket = so; michael@0: inp->ip_inp.inp.inp_socket = so; michael@0: #ifdef INET6 michael@0: #if !defined(__Userspace__) && !defined(__Windows__) michael@0: if (INP_SOCKAF(so) == AF_INET6) { michael@0: if (MODULE_GLOBAL(ip6_auto_flowlabel)) { michael@0: inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL; michael@0: } michael@0: if (MODULE_GLOBAL(ip6_v6only)) { michael@0: inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY; michael@0: } michael@0: } michael@0: #endif michael@0: #endif michael@0: inp->sctp_associd_counter = 1; michael@0: inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT; michael@0: inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; michael@0: inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off); michael@0: inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable); michael@0: #if defined(__Userspace__) michael@0: inp->ulp_info = NULL; michael@0: inp->recv_callback = NULL; michael@0: inp->send_callback = NULL; michael@0: inp->send_sb_threshold = 0; michael@0: #endif michael@0: /* init the small hash table we use to track asocid <-> tcb */ michael@0: inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark); michael@0: if (inp->sctp_asocidhash == NULL) { michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return (ENOBUFS); michael@0: } michael@0: #ifdef IPSEC michael@0: #if !(defined(__APPLE__)) michael@0: { michael@0: struct inpcbpolicy *pcb_sp = NULL; michael@0: michael@0: error = ipsec_init_policy(so, &pcb_sp); michael@0: /* Arrange to share the policy */ michael@0: inp->ip_inp.inp.inp_sp = pcb_sp; michael@0: ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp; michael@0: } michael@0: #else michael@0: /* not sure what to do for openbsd here */ michael@0: error = 0; michael@0: #endif michael@0: if (error != 0) { michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return error; michael@0: } michael@0: #endif /* IPSEC */ michael@0: SCTP_INCR_EP_COUNT(); michael@0: inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: michael@0: so->so_pcb = (caddr_t)inp; michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 803000 michael@0: if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) || michael@0: (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) { michael@0: #else michael@0: if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) { michael@0: #endif michael@0: /* UDP style socket */ michael@0: inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | michael@0: SCTP_PCB_FLAGS_UNBOUND); michael@0: /* Be sure it is NON-BLOCKING IO for UDP */ michael@0: /* SCTP_SET_SO_NBIO(so); */ michael@0: } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) { michael@0: /* TCP style socket */ michael@0: inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | michael@0: SCTP_PCB_FLAGS_UNBOUND); michael@0: /* Be sure we have blocking IO by default */ michael@0: SCTP_CLEAR_SO_NBIO(so); michael@0: #if defined(__Panda__) michael@0: } else if (SCTP_SO_TYPE(so) == SOCK_FASTSEQPACKET) { michael@0: inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | michael@0: SCTP_PCB_FLAGS_UNBOUND); michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE); michael@0: } else if (SCTP_SO_TYPE(so) == SOCK_FASTSTREAM) { michael@0: inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | michael@0: SCTP_PCB_FLAGS_UNBOUND); michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE); michael@0: #endif michael@0: } else { michael@0: /* michael@0: * unsupported socket type (RAW, etc)- in case we missed it michael@0: * in protosw michael@0: */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP); michael@0: so->so_pcb = NULL; michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); michael@0: return (EOPNOTSUPP); michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) { michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); michael@0: } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) { michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); michael@0: } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) { michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); michael@0: } michael@0: inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize), michael@0: &inp->sctp_hashmark); michael@0: if (inp->sctp_tcbhash == NULL) { michael@0: SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n"); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); michael@0: so->so_pcb = NULL; michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); michael@0: return (ENOBUFS); michael@0: } michael@0: #ifdef SCTP_MVRF michael@0: inp->vrf_size = SCTP_DEFAULT_VRF_SIZE; michael@0: SCTP_MALLOC(inp->m_vrf_ids, uint32_t *, michael@0: (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF); michael@0: if (inp->m_vrf_ids == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); michael@0: so->so_pcb = NULL; michael@0: SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); michael@0: return (ENOBUFS); michael@0: } michael@0: inp->m_vrf_ids[0] = vrf_id; michael@0: inp->num_vrfs = 1; michael@0: #endif michael@0: inp->def_vrf_id = vrf_id; michael@0: michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr); michael@0: if (inp->ip_inp.inp.inpcb_mtx == NULL) { michael@0: SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so); michael@0: #ifdef SCTP_MVRF michael@0: SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); michael@0: #endif michael@0: SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); michael@0: so->so_pcb = NULL; michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); michael@0: SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: #elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) michael@0: lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr); michael@0: #else michael@0: lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).ipi_lock_grp, SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); michael@0: #endif michael@0: #endif michael@0: SCTP_INP_INFO_WLOCK(); michael@0: SCTP_INP_LOCK_INIT(inp); michael@0: #if defined(__FreeBSD__) michael@0: INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp"); michael@0: #endif michael@0: SCTP_INP_READ_INIT(inp); michael@0: SCTP_ASOC_CREATE_LOCK_INIT(inp); michael@0: /* lock the new ep */ michael@0: SCTP_INP_WLOCK(inp); michael@0: michael@0: /* add it to the info area */ michael@0: LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list); michael@0: #if defined(__APPLE__) michael@0: inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo); michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) michael@0: LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list); michael@0: #else michael@0: LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list); michael@0: #endif michael@0: #endif michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: michael@0: TAILQ_INIT(&inp->read_queue); michael@0: LIST_INIT(&inp->sctp_addr_list); michael@0: michael@0: LIST_INIT(&inp->sctp_asoc_list); michael@0: michael@0: #ifdef SCTP_TRACK_FREED_ASOCS michael@0: /* TEMP CODE */ michael@0: LIST_INIT(&inp->sctp_asoc_free_list); michael@0: #endif michael@0: /* Init the timer structure for signature change */ michael@0: SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer); michael@0: inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE; michael@0: michael@0: /* now init the actual endpoint default data */ michael@0: m = &inp->sctp_ep; michael@0: michael@0: /* setup the base timeout information */ michael@0: m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */ michael@0: m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */ michael@0: m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default)); michael@0: m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default)); michael@0: m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default)); michael@0: m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default)); michael@0: m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default)); michael@0: /* all max/min max are in ms */ michael@0: m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default); michael@0: m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default); michael@0: m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default); michael@0: m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default); michael@0: m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default); michael@0: m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default); michael@0: m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default); michael@0: m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default); michael@0: m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold); michael@0: m->sctp_sws_sender = SCTP_SWS_SENDER_DEF; michael@0: m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF; michael@0: m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default); michael@0: m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default); michael@0: michael@0: m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module); michael@0: m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module); michael@0: m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default); michael@0: /* number of streams to pre-open on a association */ michael@0: m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default); michael@0: michael@0: /* Add adaptation cookie */ michael@0: m->adaptation_layer_indicator = 0; michael@0: m->adaptation_layer_indicator_provided = 0; michael@0: michael@0: /* seed random number generator */ michael@0: m->random_counter = 1; michael@0: m->store_at = SCTP_SIGNATURE_SIZE; michael@0: SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers)); michael@0: sctp_fill_random_store(m); michael@0: michael@0: /* Minimum cookie size */ michael@0: m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) + michael@0: sizeof(struct sctp_state_cookie); michael@0: m->size_of_a_cookie += SCTP_SIGNATURE_SIZE; michael@0: michael@0: /* Setup the initial secret */ michael@0: (void)SCTP_GETTIME_TIMEVAL(&time); michael@0: m->time_of_secret_change = time.tv_sec; michael@0: michael@0: for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { michael@0: m->secret_key[0][i] = sctp_select_initial_TSN(m); michael@0: } michael@0: sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL); michael@0: michael@0: /* How long is a cookie good for ? */ michael@0: m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default)); michael@0: /* michael@0: * Initialize authentication parameters michael@0: */ michael@0: m->local_hmacs = sctp_default_supported_hmaclist(); michael@0: m->local_auth_chunks = sctp_alloc_chunklist(); michael@0: m->default_dscp = 0; michael@0: #ifdef INET6 michael@0: m->default_flowlabel = 0; michael@0: #endif michael@0: m->port = 0; /* encapsulation disabled by default */ michael@0: sctp_auth_set_default_chunks(m->local_auth_chunks); michael@0: LIST_INIT(&m->shared_keys); michael@0: /* add default NULL key as key id 0 */ michael@0: null_key = sctp_alloc_sharedkey(); michael@0: sctp_insert_sharedkey(&m->shared_keys, null_key); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 12); michael@0: #endif michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp, michael@0: struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_nets *net; michael@0: uint16_t lport, rport; michael@0: struct sctppcbhead *head; michael@0: struct sctp_laddr *laddr, *oladdr; michael@0: michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_INP_INFO_WLOCK(); michael@0: SCTP_INP_WLOCK(old_inp); michael@0: SCTP_INP_WLOCK(new_inp); michael@0: SCTP_TCB_LOCK(stcb); michael@0: atomic_subtract_int(&stcb->asoc.refcnt, 1); michael@0: michael@0: new_inp->sctp_ep.time_of_secret_change = michael@0: old_inp->sctp_ep.time_of_secret_change; michael@0: memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key, michael@0: sizeof(old_inp->sctp_ep.secret_key)); michael@0: new_inp->sctp_ep.current_secret_number = michael@0: old_inp->sctp_ep.current_secret_number; michael@0: new_inp->sctp_ep.last_secret_number = michael@0: old_inp->sctp_ep.last_secret_number; michael@0: new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie; michael@0: michael@0: /* make it so new data pours into the new socket */ michael@0: stcb->sctp_socket = new_inp->sctp_socket; michael@0: stcb->sctp_ep = new_inp; michael@0: michael@0: /* Copy the port across */ michael@0: lport = new_inp->sctp_lport = old_inp->sctp_lport; michael@0: rport = stcb->rport; michael@0: /* Pull the tcb from the old association */ michael@0: LIST_REMOVE(stcb, sctp_tcbhash); michael@0: LIST_REMOVE(stcb, sctp_tcblist); michael@0: if (stcb->asoc.in_asocid_hash) { michael@0: LIST_REMOVE(stcb, sctp_tcbasocidhash); michael@0: } michael@0: /* Now insert the new_inp into the TCP connected hash */ michael@0: head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; michael@0: michael@0: LIST_INSERT_HEAD(head, new_inp, sctp_hash); michael@0: /* Its safe to access */ michael@0: new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; michael@0: michael@0: /* Now move the tcb into the endpoint list */ michael@0: LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist); michael@0: /* michael@0: * Question, do we even need to worry about the ep-hash since we michael@0: * only have one connection? Probably not :> so lets get rid of it michael@0: * and not suck up any kernel memory in that. michael@0: */ michael@0: if (stcb->asoc.in_asocid_hash) { michael@0: struct sctpasochead *lhd; michael@0: lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, michael@0: new_inp->hashasocidmark)]; michael@0: LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash); michael@0: } michael@0: /* Ok. Let's restart timer. */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp, michael@0: stcb, net); michael@0: } michael@0: michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: if (new_inp->sctp_tcbhash != NULL) { michael@0: SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark); michael@0: new_inp->sctp_tcbhash = NULL; michael@0: } michael@0: if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { michael@0: /* Subset bound, so copy in the laddr list from the old_inp */ michael@0: LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) { michael@0: laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); michael@0: if (laddr == NULL) { michael@0: /* michael@0: * Gak, what can we do? This assoc is really michael@0: * HOSED. We probably should send an abort michael@0: * here. michael@0: */ michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n"); michael@0: continue; michael@0: } michael@0: SCTP_INCR_LADDR_COUNT(); michael@0: bzero(laddr, sizeof(*laddr)); michael@0: (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); michael@0: laddr->ifa = oladdr->ifa; michael@0: atomic_add_int(&laddr->ifa->refcount, 1); michael@0: LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr, michael@0: sctp_nxt_addr); michael@0: new_inp->laddr_count++; michael@0: if (oladdr == stcb->asoc.last_used_address) { michael@0: stcb->asoc.last_used_address = laddr; michael@0: } michael@0: } michael@0: } michael@0: /* Now any running timers need to be adjusted michael@0: * since we really don't care if they are running michael@0: * or not just blast in the new_inp into all of michael@0: * them. michael@0: */ michael@0: michael@0: stcb->asoc.dack_timer.ep = (void *)new_inp; michael@0: stcb->asoc.asconf_timer.ep = (void *)new_inp; michael@0: stcb->asoc.strreset_timer.ep = (void *)new_inp; michael@0: stcb->asoc.shut_guard_timer.ep = (void *)new_inp; michael@0: stcb->asoc.autoclose_timer.ep = (void *)new_inp; michael@0: stcb->asoc.delayed_event_timer.ep = (void *)new_inp; michael@0: stcb->asoc.delete_prim_timer.ep = (void *)new_inp; michael@0: /* now what about the nets? */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: net->pmtu_timer.ep = (void *)new_inp; michael@0: net->hb_timer.ep = (void *)new_inp; michael@0: net->rxt_timer.ep = (void *)new_inp; michael@0: } michael@0: SCTP_INP_WUNLOCK(new_inp); michael@0: SCTP_INP_WUNLOCK(old_inp); michael@0: } michael@0: michael@0: michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)) michael@0: /* michael@0: * Don't know why, but without this there is an unknown reference when michael@0: * compiling NetBSD... hmm michael@0: */ michael@0: extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6); michael@0: #endif michael@0: michael@0: michael@0: /* sctp_ifap is used to bypass normal local address validation checks */ michael@0: int michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, michael@0: struct sctp_ifa *sctp_ifap, struct thread *p) michael@0: #elif defined(__Windows__) michael@0: sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, michael@0: struct sctp_ifa *sctp_ifap, PKTHREAD p) michael@0: #else michael@0: sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, michael@0: struct sctp_ifa *sctp_ifap, struct proc *p) michael@0: #endif michael@0: { michael@0: /* bind a ep to a socket address */ michael@0: struct sctppcbhead *head; michael@0: struct sctp_inpcb *inp, *inp_tmp; michael@0: #if defined(INET) || (defined(INET6) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__APPLE__) michael@0: struct inpcb *ip_inp; michael@0: #endif michael@0: int port_reuse_active = 0; michael@0: int bindall; michael@0: #ifdef SCTP_MVRF michael@0: int i; michael@0: #endif michael@0: uint16_t lport; michael@0: int error; michael@0: uint32_t vrf_id; michael@0: michael@0: lport = 0; michael@0: error = 0; michael@0: bindall = 1; michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: #if defined(INET) || (defined(INET6) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__APPLE__) michael@0: ip_inp = (struct inpcb *)so->so_pcb; michael@0: #endif michael@0: #ifdef SCTP_DEBUG michael@0: if (addr) { michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n", michael@0: ntohs(((struct sockaddr_in *)addr)->sin_port)); michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Addr: "); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); michael@0: } michael@0: #endif michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { michael@0: /* already did a bind, subsequent binds NOT allowed ! */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: #ifdef INVARIANTS michael@0: if (p == NULL) michael@0: panic("null proc/thread"); michael@0: #endif michael@0: #endif michael@0: if (addr != NULL) { 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: /* IPV6_V6ONLY socket? */ michael@0: if (SCTP_IPV6_V6ONLY(ip_inp)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #ifdef HAVE_SA_LEN michael@0: if (addr->sa_len != sizeof(*sin)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: michael@0: sin = (struct sockaddr_in *)addr; michael@0: lport = sin->sin_port; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: /* michael@0: * For LOOPBACK the prison_local_ip4() call will transmute the ip address michael@0: * to the proper value. michael@0: */ michael@0: if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); michael@0: return (error); michael@0: } michael@0: #endif michael@0: if (sin->sin_addr.s_addr != INADDR_ANY) { michael@0: bindall = 0; michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: { michael@0: /* Only for pure IPv6 Address. (No IPv4 Mapped!) */ michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)addr; michael@0: michael@0: #ifdef HAVE_SA_LEN michael@0: if (addr->sa_len != sizeof(*sin6)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: lport = sin6->sin6_port; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 michael@0: /* michael@0: * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address michael@0: * to the proper value. michael@0: */ michael@0: if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr, michael@0: (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); michael@0: return (error); michael@0: } michael@0: #endif michael@0: if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { michael@0: bindall = 0; michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: /* KAME hack: embed scopeid */ michael@0: #if defined(SCTP_KAME) michael@0: if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #elif defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) { michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) { michael@0: #endif michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #elif defined(__FreeBSD__) michael@0: error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone)); michael@0: if (error != 0) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); michael@0: return (error); michael@0: } michael@0: #else michael@0: if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: } michael@0: #ifndef SCOPEDROUTING michael@0: /* this must be cleared for ifa_ifwithaddr() */ michael@0: sin6->sin6_scope_id = 0; michael@0: #endif /* SCOPEDROUTING */ 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: #ifdef HAVE_SA_LEN michael@0: if (addr->sa_len != sizeof(struct sockaddr_conn)) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: #endif michael@0: sconn = (struct sockaddr_conn *)addr; michael@0: lport = sconn->sconn_port; michael@0: if (sconn->sconn_addr != NULL) { michael@0: bindall = 0; michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT); michael@0: return (EAFNOSUPPORT); michael@0: } michael@0: } michael@0: SCTP_INP_INFO_WLOCK(); michael@0: SCTP_INP_WLOCK(inp); michael@0: /* Setup a vrf_id to be the default for the non-bind-all case. */ michael@0: vrf_id = inp->def_vrf_id; michael@0: michael@0: /* increase our count due to the unlock we do */ michael@0: SCTP_INP_INCR_REF(inp); michael@0: if (lport) { michael@0: /* michael@0: * Did the caller specify a port? if so we must see if an ep michael@0: * already has this one bound. michael@0: */ michael@0: /* got to be root to get at low ports */ michael@0: #if !defined(__Windows__) michael@0: if (ntohs(lport) < IPPORT_RESERVED) { michael@0: if (p && (error = michael@0: #ifdef __FreeBSD__ michael@0: #if __FreeBSD_version > 602000 michael@0: priv_check(p, PRIV_NETINET_RESERVEDPORT) michael@0: #elif __FreeBSD_version >= 500000 michael@0: suser_cred(p->td_ucred, 0) michael@0: #else michael@0: suser(p) michael@0: #endif michael@0: #elif defined(__APPLE__) michael@0: suser(p->p_ucred, &p->p_acflag) michael@0: #elif defined(__Userspace__) /* must be true to use raw socket */ michael@0: 1 michael@0: #else michael@0: suser(p, 0) michael@0: #endif michael@0: )) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return (error); michael@0: } michael@0: #if defined(__Panda__) michael@0: if (!SCTP_IS_PRIVILEDGED(so)) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EACCES); michael@0: return (EACCES); michael@0: } michael@0: #endif michael@0: } michael@0: #if !defined(__Panda__) && !defined(__Userspace__) michael@0: if (p == NULL) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); michael@0: return (error); michael@0: } michael@0: #endif michael@0: #endif /* __Windows__ */ michael@0: SCTP_INP_WUNLOCK(inp); michael@0: if (bindall) { michael@0: #ifdef SCTP_MVRF michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: vrf_id = inp->m_vrf_ids[i]; michael@0: #else michael@0: vrf_id = inp->def_vrf_id; michael@0: #endif michael@0: inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); michael@0: if (inp_tmp != NULL) { michael@0: /* michael@0: * lock guy returned and lower count michael@0: * note that we are not bound so michael@0: * inp_tmp should NEVER be inp. And michael@0: * it is this inp (inp_tmp) that gets michael@0: * the reference bump, so we must michael@0: * lower it. michael@0: */ michael@0: SCTP_INP_DECR_REF(inp_tmp); michael@0: /* unlock info */ michael@0: if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && michael@0: (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { michael@0: /* Ok, must be one-2-one and allowing port re-use */ michael@0: port_reuse_active = 1; michael@0: goto continue_anyway; michael@0: } michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); michael@0: return (EADDRINUSE); michael@0: } michael@0: #ifdef SCTP_MVRF michael@0: } michael@0: #endif michael@0: } else { michael@0: inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); michael@0: if (inp_tmp != NULL) { michael@0: /* michael@0: * lock guy returned and lower count note michael@0: * that we are not bound so inp_tmp should michael@0: * NEVER be inp. And it is this inp (inp_tmp) michael@0: * that gets the reference bump, so we must michael@0: * lower it. michael@0: */ michael@0: SCTP_INP_DECR_REF(inp_tmp); michael@0: /* unlock info */ michael@0: if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && michael@0: (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { michael@0: /* Ok, must be one-2-one and allowing port re-use */ michael@0: port_reuse_active = 1; michael@0: goto continue_anyway; michael@0: } michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); michael@0: return (EADDRINUSE); michael@0: } michael@0: } michael@0: continue_anyway: michael@0: SCTP_INP_WLOCK(inp); michael@0: if (bindall) { michael@0: /* verify that no lport is not used by a singleton */ michael@0: if ((port_reuse_active == 0) && michael@0: (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) { michael@0: /* Sorry someone already has this one bound */ michael@0: if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && michael@0: (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { michael@0: port_reuse_active = 1; michael@0: } else { michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); michael@0: return (EADDRINUSE); michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: uint16_t first, last, candidate; michael@0: uint16_t count; michael@0: int done; michael@0: michael@0: #if defined(__Windows__) michael@0: first = 1; michael@0: last = 0xffff; michael@0: #else michael@0: #if defined(__Userspace__) michael@0: /* TODO ensure uid is 0, etc... */ michael@0: #elif defined(__FreeBSD__) || defined(__APPLE__) michael@0: if (ip_inp->inp_flags & INP_HIGHPORT) { michael@0: first = MODULE_GLOBAL(ipport_hifirstauto); michael@0: last = MODULE_GLOBAL(ipport_hilastauto); michael@0: } else if (ip_inp->inp_flags & INP_LOWPORT) { michael@0: if (p && (error = michael@0: #ifdef __FreeBSD__ michael@0: #if __FreeBSD_version > 602000 michael@0: priv_check(p, PRIV_NETINET_RESERVEDPORT) michael@0: #elif __FreeBSD_version >= 500000 michael@0: suser_cred(p->td_ucred, 0) michael@0: #else michael@0: suser(p) michael@0: #endif michael@0: #elif defined(__APPLE__) michael@0: suser(p->p_ucred, &p->p_acflag) michael@0: #else michael@0: suser(p, 0) michael@0: #endif michael@0: )) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); michael@0: return (error); michael@0: } michael@0: first = MODULE_GLOBAL(ipport_lowfirstauto); michael@0: last = MODULE_GLOBAL(ipport_lowlastauto); michael@0: } else { michael@0: #endif michael@0: first = MODULE_GLOBAL(ipport_firstauto); michael@0: last = MODULE_GLOBAL(ipport_lastauto); michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) michael@0: } michael@0: #endif michael@0: #endif /* __Windows__ */ michael@0: if (first > last) { michael@0: uint16_t temp; michael@0: michael@0: temp = first; michael@0: first = last; michael@0: last = temp; michael@0: } michael@0: count = last - first + 1; /* number of candidates */ michael@0: candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count); michael@0: michael@0: done = 0; michael@0: while (!done) { michael@0: #ifdef SCTP_MVRF michael@0: for (i = 0; i < inp->num_vrfs; i++) { michael@0: if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) { michael@0: break; michael@0: } michael@0: } michael@0: if (i == inp->num_vrfs) { michael@0: done = 1; michael@0: } michael@0: #else michael@0: if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) { michael@0: done = 1; michael@0: } michael@0: #endif michael@0: if (!done) { michael@0: if (--count == 0) { michael@0: SCTP_INP_DECR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); michael@0: return (EADDRINUSE); michael@0: } michael@0: if (candidate == last) michael@0: candidate = first; michael@0: else michael@0: candidate = candidate + 1; michael@0: } michael@0: } michael@0: lport = htons(candidate); michael@0: } michael@0: SCTP_INP_DECR_REF(inp); michael@0: if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | michael@0: SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { michael@0: /* michael@0: * this really should not happen. The guy did a non-blocking michael@0: * bind and then did a close at the same time. michael@0: */ michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: /* ok we look clear to give out this port, so lets setup the binding */ michael@0: if (bindall) { michael@0: /* binding to all addresses, so just set in the proper flags */ michael@0: inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL; michael@0: /* set the automatic addr changes from kernel flag */ michael@0: if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) { michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF); michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); michael@0: } else { michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); michael@0: } michael@0: if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) { michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); michael@0: } else { michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); michael@0: } michael@0: /* set the automatic mobility_base from kernel michael@0: flag (by micchie) michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) { michael@0: sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE); michael@0: sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); michael@0: } else { michael@0: sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE); michael@0: sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); michael@0: } michael@0: /* set the automatic mobility_fasthandoff from kernel michael@0: flag (by micchie) michael@0: */ michael@0: if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) { michael@0: sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF); michael@0: sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); michael@0: } else { michael@0: sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF); michael@0: sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); michael@0: } michael@0: } else { michael@0: /* michael@0: * bind specific, make sure flags is off and add a new michael@0: * address structure to the sctp_addr_list inside the ep michael@0: * structure. michael@0: * michael@0: * We will need to allocate one and insert it at the head. The michael@0: * socketopt call can just insert new addresses in there as michael@0: * well. It will also have to do the embed scope kame hack michael@0: * too (before adding). michael@0: */ michael@0: struct sctp_ifa *ifa; michael@0: struct sockaddr_storage store_sa; michael@0: michael@0: memset(&store_sa, 0, sizeof(store_sa)); 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 *)&store_sa; michael@0: memcpy(sin, addr, sizeof(struct sockaddr_in)); michael@0: sin->sin_port = 0; michael@0: break; michael@0: } 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 *)&store_sa; michael@0: memcpy(sin6, addr, sizeof(struct sockaddr_in6)); michael@0: sin6->sin6_port = 0; 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 *)&store_sa; michael@0: memcpy(sconn, addr, sizeof(struct sockaddr_conn)); michael@0: sconn->sconn_port = 0; michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: /* michael@0: * first find the interface with the bound address need to michael@0: * zero out the port to find the address! yuck! can't do michael@0: * this earlier since need port for sctp_pcb_findep() michael@0: */ michael@0: if (sctp_ifap != NULL) { michael@0: ifa = sctp_ifap; michael@0: } else { michael@0: /* Note for BSD we hit here always other michael@0: * O/S's will pass things in via the michael@0: * sctp_ifap argument (Panda). michael@0: */ michael@0: ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa, michael@0: vrf_id, SCTP_ADDR_NOT_LOCKED); michael@0: } michael@0: if (ifa == NULL) { michael@0: /* Can't find an interface with that address */ michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL); michael@0: return (EADDRNOTAVAIL); michael@0: } michael@0: #ifdef INET6 michael@0: if (addr->sa_family == AF_INET6) { michael@0: /* GAK, more FIXME IFA lock? */ michael@0: if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { michael@0: /* Can't bind a non-existent addr. */ michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: } michael@0: #endif michael@0: /* we're not bound all */ michael@0: inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL; michael@0: /* allow bindx() to send ASCONF's for binding changes */ michael@0: sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); michael@0: /* clear automatic addr changes from kernel flag */ michael@0: sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); michael@0: michael@0: /* add this address to the endpoint list */ michael@0: error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0); michael@0: if (error != 0) { michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return (error); michael@0: } michael@0: inp->laddr_count++; michael@0: } michael@0: /* find the bucket */ michael@0: if (port_reuse_active) { michael@0: /* Put it into tcp 1-2-1 hash */ michael@0: head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))]; michael@0: inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; michael@0: } else { michael@0: head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))]; michael@0: } michael@0: /* put it in the bucket */ michael@0: LIST_INSERT_HEAD(head, inp, sctp_hash); michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n", michael@0: (void *)head, ntohs(lport), port_reuse_active); michael@0: /* set in the port */ michael@0: inp->sctp_lport = lport; michael@0: michael@0: /* turn off just the unbound flag */ michael@0: inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: static void michael@0: sctp_iterator_inp_being_freed(struct sctp_inpcb *inp) michael@0: { michael@0: struct sctp_iterator *it, *nit; michael@0: michael@0: /* michael@0: * We enter with the only the ITERATOR_LOCK in place and a write michael@0: * lock on the inp_info stuff. michael@0: */ michael@0: it = sctp_it_ctl.cur_it; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: if (it && (it->vn != curvnet)) { michael@0: /* Its not looking at our VNET */ michael@0: return; michael@0: } michael@0: #endif michael@0: if (it && (it->inp == inp)) { michael@0: /* michael@0: * This is tricky and we hold the iterator lock, michael@0: * but when it returns and gets the lock (when we michael@0: * release it) the iterator will try to operate on michael@0: * inp. We need to stop that from happening. But michael@0: * of course the iterator has a reference on the michael@0: * stcb and inp. We can mark it and it will stop. michael@0: * michael@0: * If its a single iterator situation, we michael@0: * set the end iterator flag. Otherwise michael@0: * we set the iterator to go to the next inp. michael@0: * michael@0: */ michael@0: if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { michael@0: sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; michael@0: } else { michael@0: sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP; michael@0: } michael@0: } michael@0: /* Now go through and remove any single reference to michael@0: * our inp that may be still pending on the list michael@0: */ michael@0: SCTP_IPI_ITERATOR_WQ_LOCK(); michael@0: TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: if (it->vn != curvnet) { michael@0: continue; michael@0: } michael@0: #endif michael@0: if (it->inp == inp) { michael@0: /* This one points to me is it inp specific? */ michael@0: if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { michael@0: /* Remove and free this one */ michael@0: TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, michael@0: it, sctp_nxt_itr); 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: } else { michael@0: it->inp = LIST_NEXT(it->inp, sctp_list); michael@0: if (it->inp) { michael@0: SCTP_INP_INCR_REF(it->inp); michael@0: } michael@0: } michael@0: /* When its put in the refcnt is incremented so decr it */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: } michael@0: SCTP_IPI_ITERATOR_WQ_UNLOCK(); michael@0: } michael@0: michael@0: /* release sctp_inpcb unbind the port */ michael@0: void michael@0: sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from) michael@0: { michael@0: /* michael@0: * Here we free a endpoint. We must find it (if it is in the Hash michael@0: * table) and remove it from there. Then we must also find it in the michael@0: * overall list and remove it from there. After all removals are michael@0: * complete then any timer has to be stopped. Then start the actual michael@0: * freeing. a) Any local lists. b) Any associations. c) The hash of michael@0: * all associations. d) finally the ep itself. michael@0: */ michael@0: struct sctp_tcb *asoc, *nasoc; michael@0: struct sctp_laddr *laddr, *nladdr; michael@0: struct inpcb *ip_pcb; michael@0: struct socket *so; michael@0: int being_refed = 0; michael@0: struct sctp_queued_to_read *sq, *nsq; michael@0: #if !defined(__Panda__) && !defined(__Userspace__) michael@0: #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 michael@0: sctp_rtentry_t *rt; michael@0: #endif michael@0: #endif michael@0: int cnt; michael@0: sctp_sharedkey_t *shared_key, *nshared_key; michael@0: michael@0: michael@0: #if defined(__APPLE__) michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: #endif michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 0); michael@0: #endif michael@0: SCTP_ITERATOR_LOCK(); michael@0: /* mark any iterators on the list or being processed */ michael@0: sctp_iterator_inp_being_freed(inp); michael@0: SCTP_ITERATOR_UNLOCK(); michael@0: so = inp->sctp_socket; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: /* been here before.. eeks.. get out of here */ michael@0: SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate); michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 1); michael@0: #endif michael@0: return; michael@0: } michael@0: SCTP_ASOC_CREATE_LOCK(inp); michael@0: SCTP_INP_INFO_WLOCK(); michael@0: michael@0: SCTP_INP_WLOCK(inp); michael@0: if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) { michael@0: inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP; michael@0: /* socket is gone, so no more wakeups allowed */ michael@0: inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE; michael@0: inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; michael@0: inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; michael@0: michael@0: } michael@0: /* First time through we have the socket lock, after that no more. */ michael@0: sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL, michael@0: SCTP_FROM_SCTP_PCB+SCTP_LOC_1); michael@0: michael@0: if (inp->control) { michael@0: sctp_m_freem(inp->control); michael@0: inp->control = NULL; michael@0: } michael@0: if (inp->pkt) { michael@0: sctp_m_freem(inp->pkt); michael@0: inp->pkt = NULL; michael@0: } michael@0: ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer michael@0: * here but I will be nice :> (i.e. michael@0: * ip_pcb = ep;) */ michael@0: if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { michael@0: int cnt_in_sd; michael@0: michael@0: cnt_in_sd = 0; michael@0: LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { michael@0: SCTP_TCB_LOCK(asoc); michael@0: if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: /* Skip guys being freed */ michael@0: cnt_in_sd++; michael@0: if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { michael@0: /* michael@0: * Special case - we did not start a kill michael@0: * timer on the asoc due to it was not michael@0: * closed. So go ahead and start it now. michael@0: */ michael@0: asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); michael@0: } michael@0: SCTP_TCB_UNLOCK(asoc); michael@0: continue; michael@0: } michael@0: if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) || michael@0: (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) && michael@0: (asoc->asoc.total_output_queue_size == 0)) { michael@0: /* If we have data in queue, we don't want to just michael@0: * free since the app may have done, send()/close michael@0: * or connect/send/close. And it wants the data michael@0: * to get across first. michael@0: */ michael@0: /* Just abandon things in the front states */ michael@0: if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, michael@0: SCTP_FROM_SCTP_PCB+SCTP_LOC_2) == 0) { michael@0: cnt_in_sd++; michael@0: } michael@0: continue; michael@0: } michael@0: /* Disconnect the socket please */ michael@0: asoc->sctp_socket = NULL; michael@0: asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET; michael@0: if ((asoc->asoc.size_on_reasm_queue > 0) || michael@0: (asoc->asoc.control_pdapi) || michael@0: (asoc->asoc.size_on_all_streams > 0) || michael@0: (so && (so->so_rcv.sb_cc > 0))) { michael@0: /* Left with Data unread */ michael@0: struct mbuf *op_err; michael@0: michael@0: op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err) { michael@0: /* Fill in the user initiated abort */ michael@0: struct sctp_paramhdr *ph; michael@0: michael@0: SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); michael@0: ph = mtod(op_err, struct sctp_paramhdr *); michael@0: ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); michael@0: ph->param_length = htons(SCTP_BUF_LEN(op_err)); michael@0: } michael@0: asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_3; michael@0: sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); michael@0: SCTP_STAT_INCR_COUNTER32(sctps_aborted); michael@0: if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: if (sctp_free_assoc(inp, asoc, michael@0: SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_4) == 0) { michael@0: cnt_in_sd++; michael@0: } michael@0: continue; michael@0: } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) && michael@0: TAILQ_EMPTY(&asoc->asoc.sent_queue) && michael@0: (asoc->asoc.stream_queue_cnt == 0)) { michael@0: if (asoc->asoc.locked_on_sending) { michael@0: goto abort_anyway; michael@0: } michael@0: if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) && michael@0: (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { michael@0: struct sctp_nets *netp; michael@0: michael@0: /* michael@0: * there is nothing queued to send, michael@0: * so I send shutdown michael@0: */ michael@0: if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT); michael@0: SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING); michael@0: sctp_stop_timers_for_shutdown(asoc); michael@0: if (asoc->asoc.alternate) { michael@0: netp = asoc->asoc.alternate; michael@0: } else { michael@0: netp = asoc->asoc.primary_destination; michael@0: } michael@0: sctp_send_shutdown(asoc, netp); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc, michael@0: netp); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, michael@0: asoc->asoc.primary_destination); michael@0: sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED); michael@0: } michael@0: } else { michael@0: /* mark into shutdown pending */ michael@0: struct sctp_stream_queue_pending *sp; michael@0: michael@0: asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING; michael@0: sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, michael@0: asoc->asoc.primary_destination); michael@0: if (asoc->asoc.locked_on_sending) { michael@0: sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue), michael@0: sctp_streamhead); michael@0: if (sp == NULL) { michael@0: SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n", michael@0: (void *)asoc->asoc.locked_on_sending, michael@0: asoc->asoc.locked_on_sending->stream_no); michael@0: } else { michael@0: if ((sp->length == 0) && (sp->msg_is_complete == 0)) michael@0: asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT; michael@0: } michael@0: } michael@0: if (TAILQ_EMPTY(&asoc->asoc.send_queue) && michael@0: TAILQ_EMPTY(&asoc->asoc.sent_queue) && michael@0: (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) { michael@0: struct mbuf *op_err; michael@0: abort_anyway: michael@0: op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err) { michael@0: /* Fill in the user initiated abort */ michael@0: struct sctp_paramhdr *ph; michael@0: michael@0: SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); michael@0: ph = mtod(op_err, struct sctp_paramhdr *); michael@0: ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); michael@0: ph->param_length = htons(SCTP_BUF_LEN(op_err)); michael@0: } michael@0: asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_5; michael@0: sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); michael@0: SCTP_STAT_INCR_COUNTER32(sctps_aborted); michael@0: if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: if (sctp_free_assoc(inp, asoc, michael@0: SCTP_PCBFREE_NOFORCE, michael@0: SCTP_FROM_SCTP_PCB+SCTP_LOC_6) == 0) { michael@0: cnt_in_sd++; michael@0: } michael@0: continue; michael@0: } else { michael@0: sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); michael@0: } michael@0: } michael@0: cnt_in_sd++; michael@0: SCTP_TCB_UNLOCK(asoc); michael@0: } michael@0: /* now is there some left in our SHUTDOWN state? */ michael@0: if (cnt_in_sd) { michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 2); michael@0: #endif michael@0: inp->sctp_socket = NULL; michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return; michael@0: } michael@0: } michael@0: inp->sctp_socket = NULL; michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != michael@0: SCTP_PCB_FLAGS_UNBOUND) { michael@0: /* michael@0: * ok, this guy has been bound. It's port is michael@0: * somewhere in the SCTP_BASE_INFO(hash table). Remove michael@0: * it! michael@0: */ michael@0: LIST_REMOVE(inp, sctp_hash); michael@0: inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND; michael@0: } michael@0: michael@0: /* If there is a timer running to kill us, michael@0: * forget it, since it may have a contest michael@0: * on the INP lock.. which would cause us michael@0: * to die ... michael@0: */ michael@0: cnt = 0; michael@0: LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { michael@0: SCTP_TCB_LOCK(asoc); michael@0: if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { michael@0: asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); michael@0: } michael@0: cnt++; michael@0: SCTP_TCB_UNLOCK(asoc); michael@0: continue; michael@0: } michael@0: /* Free associations that are NOT killing us */ michael@0: if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) && michael@0: ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) { michael@0: struct mbuf *op_err; michael@0: michael@0: op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), michael@0: 0, M_NOWAIT, 1, MT_DATA); michael@0: if (op_err) { michael@0: /* Fill in the user initiated abort */ michael@0: struct sctp_paramhdr *ph; michael@0: michael@0: SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); michael@0: ph = mtod(op_err, struct sctp_paramhdr *); michael@0: ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); michael@0: ph->param_length = htons(SCTP_BUF_LEN(op_err)); michael@0: } michael@0: asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_7; michael@0: sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); michael@0: SCTP_STAT_INCR_COUNTER32(sctps_aborted); michael@0: } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { michael@0: cnt++; michael@0: SCTP_TCB_UNLOCK(asoc); michael@0: continue; michael@0: } michael@0: if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || michael@0: (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { michael@0: SCTP_STAT_DECR_GAUGE32(sctps_currestab); michael@0: } michael@0: if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_8) == 0) { michael@0: cnt++; michael@0: } michael@0: } michael@0: if (cnt) { michael@0: /* Ok we have someone out there that will kill us */ michael@0: (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 3); michael@0: #endif michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return; michael@0: } michael@0: if (SCTP_INP_LOCK_CONTENDED(inp)) michael@0: being_refed++; michael@0: if (SCTP_INP_READ_CONTENDED(inp)) michael@0: being_refed++; michael@0: if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp)) michael@0: being_refed++; michael@0: michael@0: if ((inp->refcount) || michael@0: (being_refed) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) { michael@0: (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 4); michael@0: #endif michael@0: sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return; michael@0: } michael@0: inp->sctp_ep.signature_change.type = 0; michael@0: inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE; michael@0: /* Remove it from the list .. last thing we need a michael@0: * lock for. michael@0: */ michael@0: LIST_REMOVE(inp, sctp_list); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_ASOC_CREATE_UNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: /* Now we release all locks. Since this INP michael@0: * cannot be found anymore except possibly by the michael@0: * kill timer that might be running. We call michael@0: * the drain function here. It should hit the case michael@0: * were it sees the ACTIVE flag cleared and exit michael@0: * out freeing us to proceed and destroy everything. michael@0: */ michael@0: if (from != SCTP_CALLED_FROM_INPKILL_TIMER) { michael@0: (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer); michael@0: } else { michael@0: /* Probably un-needed */ michael@0: (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); michael@0: } michael@0: michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 5); michael@0: #endif michael@0: michael@0: #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) michael@0: #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 michael@0: rt = ip_pcb->inp_route.ro_rt; michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(__Panda__) michael@0: if (inp->pak_to_read) { michael@0: (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_timer.timer); michael@0: SCTP_RELEASE_PKT(inp->pak_to_read); michael@0: inp->pak_to_read = NULL; michael@0: } michael@0: if (inp->pak_to_read_sendq) { michael@0: (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_sendq_timer.timer); michael@0: SCTP_RELEASE_PKT(inp->pak_to_read_sendq); michael@0: inp->pak_to_read_sendq = NULL; michael@0: } michael@0: #endif michael@0: if ((inp->sctp_asocidhash) != NULL) { michael@0: SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark); michael@0: inp->sctp_asocidhash = NULL; michael@0: } michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) { michael@0: /* Its only abandoned if it had data left */ michael@0: if (sq->length) michael@0: SCTP_STAT_INCR(sctps_left_abandon); michael@0: michael@0: TAILQ_REMOVE(&inp->read_queue, sq, next); michael@0: sctp_free_remote_addr(sq->whoFrom); michael@0: if (so) michael@0: so->so_rcv.sb_cc -= sq->length; michael@0: if (sq->data) { michael@0: sctp_m_freem(sq->data); michael@0: sq->data = NULL; michael@0: } michael@0: /* michael@0: * no need to free the net count, since at this point all michael@0: * assoc's are gone. michael@0: */ michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq); michael@0: SCTP_DECR_READQ_COUNT(); michael@0: } michael@0: /* Now the sctp_pcb things */ michael@0: /* michael@0: * free each asoc if it is not already closed/free. we can't use the michael@0: * macro here since le_next will get freed as part of the michael@0: * sctp_free_assoc() call. michael@0: */ michael@0: if (so) { michael@0: #ifdef IPSEC michael@0: ipsec_delete_pcbpolicy(ip_pcb); michael@0: #endif /* IPSEC */ michael@0: michael@0: /* Unlocks not needed since the socket is gone now */ michael@0: } michael@0: #ifndef __Panda__ michael@0: if (ip_pcb->inp_options) { michael@0: (void)sctp_m_free(ip_pcb->inp_options); michael@0: ip_pcb->inp_options = 0; 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: if (rt) { michael@0: RTFREE(rt); michael@0: ip_pcb->inp_route.ro_rt = 0; michael@0: } michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version < 803000 michael@0: #ifdef INET michael@0: if (ip_pcb->inp_moptions) { michael@0: inp_freemoptions(ip_pcb->inp_moptions); michael@0: ip_pcb->inp_moptions = 0; michael@0: } michael@0: #endif michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef INET6 michael@0: #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) michael@0: #if defined(__FreeBSD__) || defined(__APPLE__) michael@0: if (ip_pcb->inp_vflag & INP_IPV6) { michael@0: #else michael@0: if (inp->inp_vflag & INP_IPV6) { michael@0: #endif michael@0: struct in6pcb *in6p; michael@0: michael@0: in6p = (struct in6pcb *)inp; michael@0: ip6_freepcbopts(in6p->in6p_outputopts); michael@0: } michael@0: #endif michael@0: #endif /* INET6 */ michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: inp->inp_vflag = 0; michael@0: #else michael@0: ip_pcb->inp_vflag = 0; michael@0: #endif michael@0: /* free up authentication fields */ michael@0: if (inp->sctp_ep.local_auth_chunks != NULL) michael@0: sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); michael@0: if (inp->sctp_ep.local_hmacs != NULL) michael@0: sctp_free_hmaclist(inp->sctp_ep.local_hmacs); michael@0: michael@0: LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) { michael@0: LIST_REMOVE(shared_key, next); michael@0: sctp_free_sharedkey(shared_key); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: michael@0: #if defined(__APPLE__) michael@0: inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD; michael@0: if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) { michael@0: #ifdef INVARIANTS michael@0: panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp); michael@0: #else michael@0: SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp); michael@0: #endif michael@0: } michael@0: inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING; michael@0: #endif michael@0: /* michael@0: * if we have an address list the following will free the list of michael@0: * ifaddr's that are set into this ep. Again macro limitations here, michael@0: * since the LIST_FOREACH could be a bad idea. michael@0: */ michael@0: LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) { michael@0: sctp_remove_laddr(laddr); michael@0: } michael@0: michael@0: #ifdef SCTP_TRACK_FREED_ASOCS michael@0: /* TEMP CODE */ michael@0: LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) { michael@0: LIST_REMOVE(asoc, sctp_tcblist); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc); michael@0: SCTP_DECR_ASOC_COUNT(); michael@0: } michael@0: /* *** END TEMP CODE ****/ michael@0: #endif michael@0: #ifdef SCTP_MVRF michael@0: SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); michael@0: #endif michael@0: /* Now lets see about freeing the EP hash table. */ michael@0: if (inp->sctp_tcbhash != NULL) { michael@0: SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); michael@0: inp->sctp_tcbhash = NULL; michael@0: } michael@0: /* Now we must put the ep memory back into the zone pool */ michael@0: #if defined(__FreeBSD__) michael@0: INP_LOCK_DESTROY(&inp->ip_inp.inp); michael@0: #endif michael@0: SCTP_INP_LOCK_DESTROY(inp); michael@0: SCTP_INP_READ_DESTROY(inp); michael@0: SCTP_ASOC_CREATE_LOCK_DESTROY(inp); michael@0: #if !defined(__APPLE__) michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); michael@0: SCTP_DECR_EP_COUNT(); michael@0: #else michael@0: /* For Tiger, we will do this later... */ michael@0: #endif michael@0: } michael@0: michael@0: michael@0: struct sctp_nets * michael@0: sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) michael@0: { michael@0: struct sctp_nets *net; michael@0: /* locate the address */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) michael@0: return (net); michael@0: } michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) michael@0: { michael@0: #ifdef __Panda__ michael@0: return (0); michael@0: #else michael@0: struct sctp_ifa *sctp_ifa; michael@0: sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED); michael@0: if (sctp_ifa) { michael@0: return (1); michael@0: } else { michael@0: return (0); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: /* michael@0: * add's a remote endpoint address, done with the INIT/INIT-ACK as well as michael@0: * when a ASCONF arrives that adds it. It will also initialize all the cwnd michael@0: * stats of stuff. michael@0: */ michael@0: int michael@0: sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, michael@0: struct sctp_nets **netp, int set_scope, int from) michael@0: { michael@0: /* michael@0: * The following is redundant to the same lines in the michael@0: * sctp_aloc_assoc() but is needed since others call the add michael@0: * address function michael@0: */ michael@0: struct sctp_nets *net, *netfirst; michael@0: int addr_inscope; michael@0: michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ", michael@0: from); michael@0: SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr); michael@0: michael@0: netfirst = sctp_findnet(stcb, newaddr); michael@0: if (netfirst) { michael@0: /* michael@0: * Lie and return ok, we don't want to make the association michael@0: * go away for this behavior. It will happen in the TCP michael@0: * model in a connected socket. It does not reach the hash michael@0: * table until after the association is built so it can't be michael@0: * found. Mark as reachable, since the initial creation will michael@0: * have been cleared and the NOT_IN_ASSOC flag will have michael@0: * been added... and we don't want to end up removing it michael@0: * back out. michael@0: */ michael@0: if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { michael@0: netfirst->dest_state = (SCTP_ADDR_REACHABLE | michael@0: SCTP_ADDR_UNCONFIRMED); michael@0: } else { michael@0: netfirst->dest_state = SCTP_ADDR_REACHABLE; michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: addr_inscope = 1; michael@0: switch (newaddr->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 *)newaddr; michael@0: if (sin->sin_addr.s_addr == 0) { michael@0: /* Invalid address */ michael@0: return (-1); michael@0: } michael@0: /* zero out the bzero area */ michael@0: memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); michael@0: michael@0: /* assure len is set */ michael@0: #ifdef HAVE_SIN_LEN michael@0: sin->sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: if (set_scope) { michael@0: #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE michael@0: stcb->asoc.scope.ipv4_local_scope = 1; michael@0: #else michael@0: if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { michael@0: stcb->asoc.scope.ipv4_local_scope = 1; michael@0: } michael@0: #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */ michael@0: } else { michael@0: /* Validate the address is in scope */ michael@0: if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && michael@0: (stcb->asoc.scope.ipv4_local_scope == 0)) { michael@0: addr_inscope = 0; michael@0: } michael@0: } michael@0: break; michael@0: } 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 *)newaddr; michael@0: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { michael@0: /* Invalid address */ michael@0: return (-1); michael@0: } michael@0: /* assure len is set */ michael@0: #ifdef HAVE_SIN6_LEN michael@0: sin6->sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: if (set_scope) { michael@0: if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { michael@0: stcb->asoc.scope.loopback_scope = 1; michael@0: stcb->asoc.scope.local_scope = 0; michael@0: stcb->asoc.scope.ipv4_local_scope = 1; michael@0: stcb->asoc.scope.site_scope = 1; michael@0: } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { michael@0: /* michael@0: * If the new destination is a LINK_LOCAL we michael@0: * must have common site scope. Don't set michael@0: * the local scope since we may not share michael@0: * all links, only loopback can do this. michael@0: * Links on the local network would also be michael@0: * on our private network for v4 too. michael@0: */ michael@0: stcb->asoc.scope.ipv4_local_scope = 1; michael@0: stcb->asoc.scope.site_scope = 1; michael@0: } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { michael@0: /* michael@0: * If the new destination is SITE_LOCAL then michael@0: * we must have site scope in common. michael@0: */ michael@0: stcb->asoc.scope.site_scope = 1; michael@0: } michael@0: } else { michael@0: /* Validate the address is in scope */ michael@0: if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && michael@0: (stcb->asoc.scope.loopback_scope == 0)) { michael@0: addr_inscope = 0; michael@0: } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && michael@0: (stcb->asoc.scope.local_scope == 0)) { michael@0: addr_inscope = 0; michael@0: } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && michael@0: (stcb->asoc.scope.site_scope == 0)) { michael@0: addr_inscope = 0; michael@0: } michael@0: } 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 *)newaddr; michael@0: if (sconn->sconn_addr == NULL) { michael@0: /* Invalid address */ michael@0: return (-1); michael@0: } michael@0: #ifdef HAVE_SCONN_LEN michael@0: sconn->sconn_len = sizeof(struct sockaddr_conn); michael@0: #endif michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* not supported family type */ michael@0: return (-1); michael@0: } michael@0: net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets); michael@0: if (net == NULL) { michael@0: return (-1); michael@0: } michael@0: SCTP_INCR_RADDR_COUNT(); michael@0: bzero(net, sizeof(struct sctp_nets)); michael@0: (void)SCTP_GETTIME_TIMEVAL(&net->start_time); michael@0: #ifdef HAVE_SA_LEN michael@0: memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); michael@0: #endif michael@0: switch (newaddr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #ifndef HAVE_SA_LEN michael@0: memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in)); michael@0: #endif michael@0: ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #ifndef HAVE_SA_LEN michael@0: memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6)); michael@0: #endif michael@0: ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: #ifndef HAVE_SA_LEN michael@0: memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn)); michael@0: #endif michael@0: ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport; michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); michael@0: if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { michael@0: stcb->asoc.scope.loopback_scope = 1; michael@0: stcb->asoc.scope.ipv4_local_scope = 1; michael@0: stcb->asoc.scope.local_scope = 0; michael@0: stcb->asoc.scope.site_scope = 1; michael@0: addr_inscope = 1; michael@0: } michael@0: net->failure_threshold = stcb->asoc.def_net_failure; michael@0: net->pf_threshold = stcb->asoc.def_net_pf_threshold; michael@0: if (addr_inscope == 0) { michael@0: net->dest_state = (SCTP_ADDR_REACHABLE | michael@0: SCTP_ADDR_OUT_OF_SCOPE); michael@0: } else { michael@0: if (from == SCTP_ADDR_IS_CONFIRMED) michael@0: /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ michael@0: net->dest_state = SCTP_ADDR_REACHABLE; michael@0: else michael@0: net->dest_state = SCTP_ADDR_REACHABLE | michael@0: SCTP_ADDR_UNCONFIRMED; michael@0: } michael@0: /* We set this to 0, the timer code knows that michael@0: * this means its an initial value michael@0: */ michael@0: net->rto_needed = 1; michael@0: net->RTO = 0; michael@0: net->RTO_measured = 0; michael@0: stcb->asoc.numnets++; michael@0: net->ref_count = 1; michael@0: net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1; michael@0: net->port = stcb->asoc.port; michael@0: net->dscp = stcb->asoc.default_dscp; michael@0: #ifdef INET6 michael@0: net->flowlabel = stcb->asoc.default_flowlabel; michael@0: #endif michael@0: if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { michael@0: net->dest_state |= SCTP_ADDR_NOHB; michael@0: } else { michael@0: net->dest_state &= ~SCTP_ADDR_NOHB; michael@0: } michael@0: if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { michael@0: net->dest_state |= SCTP_ADDR_NO_PMTUD; michael@0: } else { michael@0: net->dest_state &= ~SCTP_ADDR_NO_PMTUD; michael@0: } michael@0: net->heart_beat_delay = stcb->asoc.heart_beat_delay; michael@0: /* Init the timer structure */ michael@0: SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&net->hb_timer.timer); michael@0: michael@0: /* Now generate a route for this guy */ michael@0: #ifdef INET6 michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: /* KAME hack: embed scopeid */ michael@0: if (newaddr->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) michael@0: (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL); michael@0: #else michael@0: (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL); michael@0: #endif michael@0: #elif defined(SCTP_KAME) michael@0: (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); michael@0: #else michael@0: (void)in6_embedscope(&sin6->sin6_addr, sin6); michael@0: #endif michael@0: #ifndef SCOPEDROUTING michael@0: sin6->sin6_scope_id = 0; michael@0: #endif michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #endif michael@0: SCTP_RTALLOC((sctp_route_t *)&net->ro, stcb->asoc.vrf_id); michael@0: michael@0: #if !defined(__Userspace__) michael@0: if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) { michael@0: /* Get source address */ michael@0: net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep, michael@0: stcb, michael@0: (sctp_route_t *)&net->ro, michael@0: net, michael@0: 0, michael@0: stcb->asoc.vrf_id); michael@0: /* Now get the interface MTU */ michael@0: if (net->ro._s_addr && net->ro._s_addr->ifn_p) { michael@0: net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p); michael@0: } michael@0: if (net->mtu > 0) { michael@0: uint32_t rmtu; michael@0: michael@0: rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt); michael@0: if (rmtu == 0) { michael@0: /* Start things off to match mtu of interface please. */ michael@0: SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa, michael@0: net->ro.ro_rt, net->mtu); michael@0: } else { michael@0: /* we take the route mtu over the interface, since michael@0: * the route may be leading out the loopback, or michael@0: * a different interface. michael@0: */ michael@0: net->mtu = rmtu; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: if (net->mtu == 0) { michael@0: switch (newaddr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: net->mtu = SCTP_DEFAULT_MTU; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: net->mtu = 1280; michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: net->mtu = 1280; michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: if (net->port) { michael@0: net->mtu -= (uint32_t)sizeof(struct udphdr); michael@0: } michael@0: if (from == SCTP_ALLOC_ASOC) { michael@0: stcb->asoc.smallest_mtu = net->mtu; michael@0: } michael@0: if (stcb->asoc.smallest_mtu > net->mtu) { michael@0: stcb->asoc.smallest_mtu = net->mtu; michael@0: } michael@0: #ifdef INET6 michael@0: #ifdef SCTP_EMBEDDED_V6_SCOPE michael@0: if (newaddr->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *sin6; michael@0: michael@0: sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; michael@0: #ifdef SCTP_KAME michael@0: (void)sa6_recoverscope(sin6); michael@0: #else michael@0: (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); michael@0: #endif /* SCTP_KAME */ michael@0: } michael@0: #endif /* SCTP_EMBEDDED_V6_SCOPE */ michael@0: #endif michael@0: michael@0: /* JRS - Use the congestion control given in the CC module */ michael@0: if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) michael@0: (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net); michael@0: michael@0: /* michael@0: * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning michael@0: * of assoc (2005/06/27, iyengar@cis.udel.edu) michael@0: */ michael@0: net->find_pseudo_cumack = 1; michael@0: net->find_rtx_pseudo_cumack = 1; michael@0: net->src_addr_selected = 0; michael@0: #if defined(__FreeBSD__) michael@0: /* Choose an initial flowid. */ michael@0: net->flowid = stcb->asoc.my_vtag ^ michael@0: ntohs(stcb->rport) ^ michael@0: ntohs(stcb->sctp_ep->sctp_lport); michael@0: #ifdef INVARIANTS michael@0: net->flowidset = 1; michael@0: #endif michael@0: #endif michael@0: if (netp) { michael@0: *netp = net; michael@0: } michael@0: netfirst = TAILQ_FIRST(&stcb->asoc.nets); michael@0: if (net->ro.ro_rt == NULL) { michael@0: /* Since we have no route put it at the back */ michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); michael@0: } else if (netfirst == NULL) { michael@0: /* We are the first one in the pool. */ michael@0: TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); michael@0: } else if (netfirst->ro.ro_rt == NULL) { michael@0: /* michael@0: * First one has NO route. Place this one ahead of the first michael@0: * one. michael@0: */ michael@0: TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); michael@0: #ifndef __Panda__ michael@0: } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) { michael@0: /* michael@0: * This one has a different interface than the one at the michael@0: * top of the list. Place it ahead. michael@0: */ michael@0: TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); michael@0: #endif michael@0: } else { michael@0: /* michael@0: * Ok we have the same interface as the first one. Move michael@0: * forward until we find either a) one with a NULL route... michael@0: * insert ahead of that b) one with a different ifp.. insert michael@0: * after that. c) end of the list.. insert at the tail. michael@0: */ michael@0: struct sctp_nets *netlook; michael@0: michael@0: do { michael@0: netlook = TAILQ_NEXT(netfirst, sctp_next); michael@0: if (netlook == NULL) { michael@0: /* End of the list */ michael@0: TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); michael@0: break; michael@0: } else if (netlook->ro.ro_rt == NULL) { michael@0: /* next one has NO route */ michael@0: TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); michael@0: break; michael@0: } michael@0: #ifndef __Panda__ michael@0: else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) michael@0: #else michael@0: else michael@0: #endif michael@0: { michael@0: TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, michael@0: net, sctp_next); michael@0: break; michael@0: } michael@0: #ifndef __Panda__ michael@0: /* Shift forward */ michael@0: netfirst = netlook; michael@0: #endif michael@0: } while (netlook != NULL); michael@0: } michael@0: michael@0: /* got to have a primary set */ michael@0: if (stcb->asoc.primary_destination == 0) { michael@0: stcb->asoc.primary_destination = net; michael@0: } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) && michael@0: (net->ro.ro_rt) && michael@0: ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { michael@0: /* No route to current primary adopt new primary */ michael@0: stcb->asoc.primary_destination = net; michael@0: } michael@0: /* Validate primary is first */ michael@0: net = TAILQ_FIRST(&stcb->asoc.nets); michael@0: if ((net != stcb->asoc.primary_destination) && michael@0: (stcb->asoc.primary_destination)) { michael@0: /* first one on the list is NOT the primary michael@0: * sctp_cmpaddr() is much more efficient if michael@0: * the primary is the first on the list, make it michael@0: * so. michael@0: */ michael@0: TAILQ_REMOVE(&stcb->asoc.nets, michael@0: stcb->asoc.primary_destination, sctp_next); michael@0: TAILQ_INSERT_HEAD(&stcb->asoc.nets, michael@0: stcb->asoc.primary_destination, sctp_next); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: static uint32_t michael@0: sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb) michael@0: { michael@0: uint32_t id; michael@0: struct sctpasochead *head; michael@0: struct sctp_tcb *lstcb; michael@0: michael@0: SCTP_INP_WLOCK(inp); michael@0: try_again: michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: /* TSNH */ michael@0: SCTP_INP_WUNLOCK(inp); michael@0: return (0); michael@0: } michael@0: /* michael@0: * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC, michael@0: * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC. michael@0: */ michael@0: if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) { michael@0: inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1; michael@0: } michael@0: id = inp->sctp_associd_counter; michael@0: inp->sctp_associd_counter++; michael@0: lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0); michael@0: if (lstcb) { michael@0: goto try_again; michael@0: } michael@0: head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; michael@0: LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash); michael@0: stcb->asoc.in_asocid_hash = 1; michael@0: SCTP_INP_WUNLOCK(inp); michael@0: return id; michael@0: } michael@0: michael@0: /* michael@0: * allocate an association and add it to the endpoint. The caller must be michael@0: * careful to add all additional addresses once they are know right away or michael@0: * else the assoc will be may experience a blackout scenario. michael@0: */ michael@0: struct sctp_tcb * michael@0: sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, michael@0: int *error, uint32_t override_tag, uint32_t vrf_id, michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 michael@0: struct thread *p michael@0: #elif defined(__Windows__) michael@0: PKTHREAD p michael@0: #else michael@0: #if defined(__Userspace__) michael@0: /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */ michael@0: #endif michael@0: struct proc *p michael@0: #endif michael@0: ) michael@0: { michael@0: /* note the p argument is only valid in unbound sockets */ michael@0: michael@0: struct sctp_tcb *stcb; michael@0: struct sctp_association *asoc; michael@0: struct sctpasochead *head; michael@0: uint16_t rport; michael@0: int err; michael@0: michael@0: /* michael@0: * Assumption made here: Caller has done a michael@0: * sctp_findassociation_ep_addr(ep, addr's); to make sure the michael@0: * address does not exist already. michael@0: */ michael@0: if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) { michael@0: /* Hit max assoc, sorry no more */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); michael@0: *error = ENOBUFS; michael@0: return (NULL); michael@0: } michael@0: if (firstaddr == NULL) { michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: SCTP_INP_RLOCK(inp); michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && michael@0: ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { michael@0: /* michael@0: * If its in the TCP pool, its NOT allowed to create an michael@0: * association. The parent listener needs to call michael@0: * sctp_aloc_assoc.. or the one-2-many socket. If a peeled michael@0: * off, or connected one does this.. its an error. michael@0: */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: } michael@0: SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:"); michael@0: #ifdef SCTP_DEBUG michael@0: if (firstaddr) { michael@0: SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr); michael@0: switch (firstaddr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", michael@0: ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", michael@0: ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port)); michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", michael@0: ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port)); michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } else { michael@0: SCTPDBG(SCTP_DEBUG_PCB3,"None\n"); michael@0: } michael@0: #endif /* SCTP_DEBUG */ michael@0: switch (firstaddr->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 *)firstaddr; michael@0: if ((ntohs(sin->sin_port) == 0) || michael@0: (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: /* Invalid address */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: rport = sin->sin_port; michael@0: break; michael@0: } 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 *)firstaddr; michael@0: if ((ntohs(sin6->sin6_port) == 0) || michael@0: IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || michael@0: IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { michael@0: /* Invalid address */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: rport = sin6->sin6_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 *)firstaddr; michael@0: if ((ntohs(sconn->sconn_port) == 0) || michael@0: (sconn->sconn_addr == NULL)) { michael@0: /* Invalid address */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: rport = sconn->sconn_port; michael@0: break; michael@0: } michael@0: #endif michael@0: default: michael@0: /* not supported family type */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { michael@0: /* michael@0: * If you have not performed a bind, then we need to do the michael@0: * ephemeral bind for you. michael@0: */ michael@0: if ((err = sctp_inpcb_bind(inp->sctp_socket, michael@0: (struct sockaddr *)NULL, michael@0: (struct sctp_ifa *)NULL, michael@0: #ifndef __Panda__ michael@0: p michael@0: #else michael@0: (struct proc *)NULL michael@0: #endif michael@0: ))) { michael@0: /* bind error, probably perm */ michael@0: *error = err; michael@0: return (NULL); michael@0: } michael@0: } michael@0: stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb); michael@0: if (stcb == NULL) { michael@0: /* out of memory? */ michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); michael@0: *error = ENOMEM; michael@0: return (NULL); michael@0: } michael@0: SCTP_INCR_ASOC_COUNT(); michael@0: michael@0: bzero(stcb, sizeof(*stcb)); michael@0: asoc = &stcb->asoc; michael@0: michael@0: asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb); michael@0: SCTP_TCB_LOCK_INIT(stcb); michael@0: SCTP_TCB_SEND_LOCK_INIT(stcb); michael@0: stcb->rport = rport; michael@0: /* setup back pointer's */ michael@0: stcb->sctp_ep = inp; michael@0: stcb->sctp_socket = inp->sctp_socket; michael@0: if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) { michael@0: /* failed */ michael@0: SCTP_TCB_LOCK_DESTROY(stcb); michael@0: SCTP_TCB_SEND_LOCK_DESTROY(stcb); michael@0: LIST_REMOVE(stcb, sctp_tcbasocidhash); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); michael@0: SCTP_DECR_ASOC_COUNT(); michael@0: *error = err; michael@0: return (NULL); michael@0: } michael@0: /* and the port */ michael@0: SCTP_INP_INFO_WLOCK(); michael@0: SCTP_INP_WLOCK(inp); michael@0: if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { michael@0: /* inpcb freed while alloc going on */ michael@0: SCTP_TCB_LOCK_DESTROY(stcb); michael@0: SCTP_TCB_SEND_LOCK_DESTROY(stcb); michael@0: LIST_REMOVE(stcb, sctp_tcbasocidhash); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_DECR_ASOC_COUNT(); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: *error = EINVAL; michael@0: return (NULL); michael@0: } michael@0: SCTP_TCB_LOCK(stcb); michael@0: michael@0: /* now that my_vtag is set, add it to the hash */ michael@0: head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; michael@0: /* put it in the bucket in the vtag hash of assoc's for the system */ michael@0: LIST_INSERT_HEAD(head, stcb, sctp_asocs); michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: michael@0: if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) { michael@0: /* failure.. memory error? */ michael@0: if (asoc->strmout) { michael@0: SCTP_FREE(asoc->strmout, SCTP_M_STRMO); michael@0: asoc->strmout = NULL; michael@0: } michael@0: if (asoc->mapping_array) { michael@0: SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); michael@0: asoc->mapping_array = NULL; michael@0: } michael@0: if (asoc->nr_mapping_array) { michael@0: SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); michael@0: asoc->nr_mapping_array = NULL; michael@0: } michael@0: SCTP_DECR_ASOC_COUNT(); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_TCB_LOCK_DESTROY(stcb); michael@0: SCTP_TCB_SEND_LOCK_DESTROY(stcb); michael@0: LIST_REMOVE(stcb, sctp_tcbasocidhash); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); michael@0: *error = ENOBUFS; michael@0: return (NULL); michael@0: } michael@0: /* Init all the timers */ michael@0: SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer); michael@0: SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); michael@0: michael@0: LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); michael@0: /* now file the port under the hash as well */ michael@0: if (inp->sctp_tcbhash != NULL) { michael@0: head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, michael@0: inp->sctp_hashmark)]; michael@0: LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); michael@0: } michael@0: SCTP_INP_WUNLOCK(inp); michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb); michael@0: return (stcb); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) michael@0: { michael@0: struct sctp_association *asoc; michael@0: michael@0: asoc = &stcb->asoc; michael@0: asoc->numnets--; michael@0: TAILQ_REMOVE(&asoc->nets, net, sctp_next); michael@0: if (net == asoc->primary_destination) { michael@0: /* Reset primary */ michael@0: struct sctp_nets *lnet; michael@0: michael@0: lnet = TAILQ_FIRST(&asoc->nets); michael@0: /* Mobility adaptation michael@0: Ideally, if deleted destination is the primary, it becomes michael@0: a fast retransmission trigger by the subsequent SET PRIMARY. michael@0: (by micchie) michael@0: */ michael@0: if (sctp_is_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_BASE) || michael@0: sctp_is_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_FASTHANDOFF)) { michael@0: SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); michael@0: if (asoc->deleted_primary != NULL) { michael@0: SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); michael@0: goto out; michael@0: } michael@0: asoc->deleted_primary = net; michael@0: atomic_add_int(&net->ref_count, 1); michael@0: memset(&net->lastsa, 0, sizeof(net->lastsa)); michael@0: memset(&net->lastsv, 0, sizeof(net->lastsv)); michael@0: sctp_mobility_feature_on(stcb->sctp_ep, michael@0: SCTP_MOBILITY_PRIM_DELETED); michael@0: sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, michael@0: stcb->sctp_ep, stcb, NULL); michael@0: } michael@0: out: michael@0: /* Try to find a confirmed primary */ michael@0: asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); michael@0: } michael@0: if (net == asoc->last_data_chunk_from) { michael@0: /* Reset primary */ michael@0: asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); michael@0: } michael@0: if (net == asoc->last_control_chunk_from) { michael@0: /* Clear net */ michael@0: asoc->last_control_chunk_from = NULL; michael@0: } michael@0: if (net == stcb->asoc.alternate) { michael@0: sctp_free_remote_addr(stcb->asoc.alternate); michael@0: stcb->asoc.alternate = NULL; michael@0: } michael@0: sctp_free_remote_addr(net); michael@0: } michael@0: michael@0: /* michael@0: * remove a remote endpoint address from an association, it will fail if the michael@0: * address does not exist. michael@0: */ michael@0: int michael@0: sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) michael@0: { michael@0: /* michael@0: * Here we need to remove a remote address. This is quite simple, we michael@0: * first find it in the list of address for the association michael@0: * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE michael@0: * on that item. Note we do not allow it to be removed if there are michael@0: * no other addresses. michael@0: */ michael@0: struct sctp_association *asoc; michael@0: struct sctp_nets *net, *nnet; michael@0: michael@0: asoc = &stcb->asoc; michael@0: michael@0: /* locate the address */ michael@0: TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { michael@0: if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { michael@0: continue; michael@0: } michael@0: if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, michael@0: remaddr)) { michael@0: /* we found the guy */ michael@0: if (asoc->numnets < 2) { michael@0: /* Must have at LEAST two remote addresses */ michael@0: return (-1); michael@0: } else { michael@0: sctp_remove_net(stcb, net); michael@0: return (0); michael@0: } michael@0: } michael@0: } michael@0: /* not found. */ michael@0: return (-2); michael@0: } michael@0: michael@0: void michael@0: sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport) michael@0: { michael@0: struct sctpvtaghead *chain; michael@0: struct sctp_tagblock *twait_block; michael@0: int found = 0; michael@0: int i; michael@0: michael@0: chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; michael@0: LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { michael@0: for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { michael@0: if ((twait_block->vtag_block[i].v_tag == tag) && michael@0: (twait_block->vtag_block[i].lport == lport) && michael@0: (twait_block->vtag_block[i].rport == rport)) { michael@0: twait_block->vtag_block[i].tv_sec_at_expire = 0; michael@0: twait_block->vtag_block[i].v_tag = 0; michael@0: twait_block->vtag_block[i].lport = 0; michael@0: twait_block->vtag_block[i].rport = 0; michael@0: found = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (found) michael@0: break; michael@0: } michael@0: } michael@0: michael@0: int michael@0: sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport) michael@0: { michael@0: struct sctpvtaghead *chain; michael@0: struct sctp_tagblock *twait_block; michael@0: int found = 0; michael@0: int i; michael@0: michael@0: SCTP_INP_INFO_WLOCK(); michael@0: chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; michael@0: LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { michael@0: for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { michael@0: if ((twait_block->vtag_block[i].v_tag == tag) && michael@0: (twait_block->vtag_block[i].lport == lport) && michael@0: (twait_block->vtag_block[i].rport == rport)) { michael@0: found = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (found) michael@0: break; michael@0: } michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: return (found); michael@0: } michael@0: michael@0: michael@0: void michael@0: sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport) michael@0: { michael@0: struct sctpvtaghead *chain; michael@0: struct sctp_tagblock *twait_block; michael@0: struct timeval now; michael@0: int set, i; michael@0: michael@0: if (time == 0) { michael@0: /* Its disabled */ michael@0: return; michael@0: } michael@0: (void)SCTP_GETTIME_TIMEVAL(&now); michael@0: chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; michael@0: set = 0; michael@0: LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { michael@0: /* Block(s) present, lets find space, and expire on the fly */ michael@0: for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { michael@0: if ((twait_block->vtag_block[i].v_tag == 0) && michael@0: !set) { michael@0: twait_block->vtag_block[i].tv_sec_at_expire = michael@0: now.tv_sec + time; michael@0: twait_block->vtag_block[i].v_tag = tag; michael@0: twait_block->vtag_block[i].lport = lport; michael@0: twait_block->vtag_block[i].rport = rport; michael@0: set = 1; michael@0: } else if ((twait_block->vtag_block[i].v_tag) && michael@0: ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) { michael@0: /* Audit expires this guy */ michael@0: twait_block->vtag_block[i].tv_sec_at_expire = 0; michael@0: twait_block->vtag_block[i].v_tag = 0; michael@0: twait_block->vtag_block[i].lport = 0; michael@0: twait_block->vtag_block[i].rport = 0; michael@0: if (set == 0) { michael@0: /* Reuse it for my new tag */ michael@0: twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time; michael@0: twait_block->vtag_block[i].v_tag = tag; michael@0: twait_block->vtag_block[i].lport = lport; michael@0: twait_block->vtag_block[i].rport = rport; michael@0: set = 1; michael@0: } michael@0: } michael@0: } michael@0: if (set) { michael@0: /* michael@0: * We only do up to the block where we can michael@0: * place our tag for audits michael@0: */ michael@0: break; michael@0: } michael@0: } michael@0: /* Need to add a new block to chain */ michael@0: if (!set) { michael@0: SCTP_MALLOC(twait_block, struct sctp_tagblock *, michael@0: sizeof(struct sctp_tagblock), SCTP_M_TIMW); michael@0: if (twait_block == NULL) { michael@0: #ifdef INVARIANTS michael@0: panic("Can not alloc tagblock"); michael@0: #endif michael@0: return; michael@0: } michael@0: memset(twait_block, 0, sizeof(struct sctp_tagblock)); michael@0: LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); michael@0: twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time; michael@0: twait_block->vtag_block[0].v_tag = tag; michael@0: twait_block->vtag_block[0].lport = lport; michael@0: twait_block->vtag_block[0].rport = rport; michael@0: } michael@0: } michael@0: michael@0: michael@0: #ifdef __Panda__ michael@0: void panda_wakeup_socket(struct socket *so); michael@0: #endif michael@0: michael@0: /*- michael@0: * Free the association after un-hashing the remote port. This michael@0: * function ALWAYS returns holding NO LOCK on the stcb. It DOES michael@0: * expect that the input to this function IS a locked TCB. michael@0: * It will return 0, if it did NOT destroy the association (instead michael@0: * it unlocks it. It will return NON-zero if it either destroyed the michael@0: * association OR the association is already destroyed. michael@0: */ michael@0: int michael@0: sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) michael@0: { michael@0: int i; michael@0: struct sctp_association *asoc; michael@0: struct sctp_nets *net, *nnet; michael@0: struct sctp_laddr *laddr, *naddr; michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: struct sctp_asconf_addr *aparam, *naparam; michael@0: struct sctp_asconf_ack *aack, *naack; michael@0: struct sctp_stream_reset_list *strrst, *nstrrst; michael@0: struct sctp_queued_to_read *sq, *nsq; michael@0: struct sctp_stream_queue_pending *sp, *nsp; michael@0: sctp_sharedkey_t *shared_key, *nshared_key; michael@0: struct socket *so; michael@0: michael@0: /* first, lets purge the entry from the hash table. */ michael@0: #if defined(__APPLE__) michael@0: sctp_lock_assert(SCTP_INP_SO(inp)); michael@0: #endif michael@0: michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, stcb, 6); michael@0: #endif michael@0: if (stcb->asoc.state == 0) { michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 7); michael@0: #endif michael@0: /* there is no asoc, really TSNH :-0 */ michael@0: return (1); michael@0: } michael@0: if (stcb->asoc.alternate) { michael@0: sctp_free_remote_addr(stcb->asoc.alternate); michael@0: stcb->asoc.alternate = NULL; michael@0: } michael@0: #if !defined(__APPLE__) /* TEMP: moved to below */ michael@0: /* TEMP CODE */ michael@0: if (stcb->freed_from_where == 0) { michael@0: /* Only record the first place free happened from */ michael@0: stcb->freed_from_where = from_location; michael@0: } michael@0: /* TEMP CODE */ michael@0: #endif michael@0: michael@0: asoc = &stcb->asoc; michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) michael@0: /* nothing around */ michael@0: so = NULL; michael@0: else michael@0: so = inp->sctp_socket; michael@0: michael@0: /* michael@0: * We used timer based freeing if a reader or writer is in the way. michael@0: * So we first check if we are actually being called from a timer, michael@0: * if so we abort early if a reader or writer is still in the way. michael@0: */ michael@0: if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && michael@0: (from_inpcbfree == SCTP_NORMAL_PROC)) { michael@0: /* michael@0: * is it the timer driving us? if so are the reader/writers michael@0: * gone? michael@0: */ michael@0: if (stcb->asoc.refcnt) { michael@0: /* nope, reader or writer in the way */ michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); michael@0: /* no asoc destroyed */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, stcb, 8); michael@0: #endif michael@0: return (0); michael@0: } michael@0: } michael@0: /* now clean up any other timers */ michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); michael@0: asoc->dack_timer.self = NULL; michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); michael@0: /*- michael@0: * For stream reset we don't blast this unless michael@0: * it is a str-reset timer, it might be the michael@0: * free-asoc timer which we DON'T want to michael@0: * disturb. michael@0: */ michael@0: if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET) michael@0: asoc->strreset_timer.self = NULL; michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); michael@0: asoc->asconf_timer.self = NULL; michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); michael@0: asoc->autoclose_timer.self = NULL; michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer); michael@0: asoc->shut_guard_timer.self = NULL; michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); michael@0: asoc->delayed_event_timer.self = NULL; michael@0: /* Mobility adaptation */ michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer); michael@0: asoc->delete_prim_timer.self = NULL; michael@0: TAILQ_FOREACH(net, &asoc->nets, sctp_next) { michael@0: (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer); michael@0: net->rxt_timer.self = NULL; michael@0: (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); michael@0: net->pmtu_timer.self = NULL; michael@0: (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer); michael@0: net->hb_timer.self = NULL; michael@0: } michael@0: /* Now the read queue needs to be cleaned up (only once) */ michael@0: if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { michael@0: stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED; michael@0: SCTP_INP_READ_LOCK(inp); michael@0: TAILQ_FOREACH(sq, &inp->read_queue, next) { michael@0: if (sq->stcb == stcb) { michael@0: sq->do_not_ref_stcb = 1; michael@0: sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; michael@0: /* If there is no end, there never michael@0: * will be now. michael@0: */ michael@0: if (sq->end_added == 0) { michael@0: /* Held for PD-API clear that. */ michael@0: sq->pdapi_aborted = 1; michael@0: sq->held_length = 0; michael@0: if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { michael@0: /* michael@0: * Need to add a PD-API aborted indication. michael@0: * Setting the control_pdapi assures that it will michael@0: * be added right after this msg. michael@0: */ michael@0: uint32_t strseq; michael@0: stcb->asoc.control_pdapi = sq; michael@0: strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn; michael@0: sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, michael@0: stcb, michael@0: SCTP_PARTIAL_DELIVERY_ABORTED, michael@0: (void *)&strseq, michael@0: SCTP_SO_LOCKED); michael@0: stcb->asoc.control_pdapi = NULL; michael@0: } michael@0: } michael@0: /* Add an end to wake them */ michael@0: sq->end_added = 1; michael@0: } michael@0: } michael@0: SCTP_INP_READ_UNLOCK(inp); michael@0: if (stcb->block_entry) { michael@0: SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); michael@0: stcb->block_entry->error = ECONNRESET; michael@0: stcb->block_entry = NULL; michael@0: } michael@0: } michael@0: if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) { michael@0: /* Someone holds a reference OR the socket is unaccepted yet. michael@0: */ michael@0: if ((stcb->asoc.refcnt) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { michael@0: stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) michael@0: /* nothing around */ michael@0: so = NULL; michael@0: if (so) { michael@0: /* Wake any reader/writers */ michael@0: sctp_sorwakeup(inp, so); michael@0: sctp_sowwakeup(inp, so); michael@0: } michael@0: michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, stcb, 9); michael@0: #endif michael@0: /* no asoc destroyed */ michael@0: return (0); michael@0: } michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, stcb, 10); michael@0: #endif michael@0: /* When I reach here, no others want michael@0: * to kill the assoc yet.. and I own michael@0: * the lock. Now its possible an abort michael@0: * comes in when I do the lock exchange michael@0: * below to grab all the locks to do michael@0: * the final take out. to prevent this michael@0: * we increment the count, which will michael@0: * start a timer and blow out above thus michael@0: * assuring us that we hold exclusive michael@0: * killing of the asoc. Note that michael@0: * after getting back the TCB lock michael@0: * we will go ahead and increment the michael@0: * counter back up and stop any timer michael@0: * a passing stranger may have started :-S michael@0: */ michael@0: if (from_inpcbfree == SCTP_NORMAL_PROC) { michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_INP_INFO_WLOCK(); michael@0: SCTP_INP_WLOCK(inp); michael@0: SCTP_TCB_LOCK(stcb); michael@0: } michael@0: /* Double check the GONE flag */ michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) michael@0: /* nothing around */ michael@0: so = NULL; michael@0: michael@0: if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || michael@0: (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { michael@0: /* michael@0: * For TCP type we need special handling when we are michael@0: * connected. We also include the peel'ed off ones to. michael@0: */ michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { michael@0: inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; michael@0: inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; michael@0: if (so) { michael@0: SOCK_LOCK(so); michael@0: if (so->so_rcv.sb_cc == 0) { michael@0: so->so_state &= ~(SS_ISCONNECTING | michael@0: SS_ISDISCONNECTING | michael@0: SS_ISCONFIRMING | michael@0: SS_ISCONNECTED); michael@0: } michael@0: #if defined(__APPLE__) michael@0: socantrcvmore(so); michael@0: #else michael@0: socantrcvmore_locked(so); michael@0: #endif michael@0: sctp_sowwakeup(inp, so); michael@0: sctp_sorwakeup(inp, so); michael@0: SCTP_SOWAKEUP(so); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Make it invalid too, that way if its michael@0: * about to run it will abort and return. michael@0: */ michael@0: /* re-increment the lock */ michael@0: if (from_inpcbfree == SCTP_NORMAL_PROC) { michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: } michael@0: if (stcb->asoc.refcnt) { michael@0: stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; michael@0: sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); michael@0: if (from_inpcbfree == SCTP_NORMAL_PROC) { michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: return (0); michael@0: } michael@0: asoc->state = 0; michael@0: if (inp->sctp_tcbhash) { michael@0: LIST_REMOVE(stcb, sctp_tcbhash); michael@0: } michael@0: if (stcb->asoc.in_asocid_hash) { michael@0: LIST_REMOVE(stcb, sctp_tcbasocidhash); michael@0: } michael@0: /* Now lets remove it from the list of ALL associations in the EP */ michael@0: LIST_REMOVE(stcb, sctp_tcblist); michael@0: if (from_inpcbfree == SCTP_NORMAL_PROC) { michael@0: SCTP_INP_INCR_REF(inp); michael@0: SCTP_INP_WUNLOCK(inp); michael@0: } michael@0: /* pull from vtag hash */ michael@0: LIST_REMOVE(stcb, sctp_asocs); michael@0: sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait), michael@0: inp->sctp_lport, stcb->rport); michael@0: michael@0: /* Now restop the timers to be sure michael@0: * this is paranoia at is finest! michael@0: */ michael@0: (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 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->shut_guard_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->rxt_timer.timer); 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: asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE; michael@0: /* michael@0: * The chunk lists and such SHOULD be empty but we check them just michael@0: * in case. michael@0: */ michael@0: /* anything on the wheel needs to be removed */ michael@0: for (i = 0; i < asoc->streamoutcnt; i++) { michael@0: struct sctp_stream_out *outs; michael@0: michael@0: outs = &asoc->strmout[i]; michael@0: /* now clean up any chunks here */ michael@0: TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { michael@0: TAILQ_REMOVE(&outs->outqueue, sp, next); michael@0: sctp_free_spbufspace(stcb, asoc, sp); michael@0: if (sp->data) { michael@0: if (so) { michael@0: /* Still an open socket - report */ michael@0: sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, michael@0: 0, (void *)sp, SCTP_SO_LOCKED); michael@0: } 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: sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); michael@0: } michael@0: } michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { michael@0: TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); michael@0: SCTP_FREE(strrst, SCTP_M_STRESET); michael@0: } michael@0: TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { michael@0: TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); michael@0: if (sq->data) { michael@0: sctp_m_freem(sq->data); michael@0: sq->data = NULL; michael@0: } michael@0: sctp_free_remote_addr(sq->whoFrom); michael@0: sq->whoFrom = NULL; michael@0: sq->stcb = NULL; michael@0: /* Free the ctl entry */ michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq); michael@0: SCTP_DECR_READQ_COUNT(); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) { michael@0: TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: if (chk->holds_key_ref) michael@0: sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); michael@0: SCTP_DECR_CHK_COUNT(); michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); michael@0: asoc->free_chunk_cnt--; 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: 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: TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); michael@0: if (chk->data) { michael@0: if (so) { michael@0: /* Still a socket? */ michael@0: sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, michael@0: 0, chk, SCTP_SO_LOCKED); michael@0: } michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: } michael@0: if (chk->holds_key_ref) michael@0: sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); michael@0: if (chk->whoTo) { michael@0: sctp_free_remote_addr(chk->whoTo); michael@0: chk->whoTo = NULL; michael@0: } michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); michael@0: SCTP_DECR_CHK_COUNT(); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: /* sent queue SHOULD be empty */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { 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: TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); michael@0: if (chk->data) { michael@0: if (so) { michael@0: /* Still a socket? */ michael@0: sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, michael@0: 0, chk, SCTP_SO_LOCKED); michael@0: } michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: } michael@0: if (chk->holds_key_ref) michael@0: sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); michael@0: sctp_free_remote_addr(chk->whoTo); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); michael@0: SCTP_DECR_CHK_COUNT(); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: #ifdef INVARIANTS michael@0: for (i = 0; i < stcb->asoc.streamoutcnt; i++) { michael@0: if (stcb->asoc.strmout[i].chunks_on_queues > 0) { michael@0: panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i); michael@0: } michael@0: } michael@0: #endif michael@0: /* control queue MAY not be empty */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { michael@0: TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: if (chk->holds_key_ref) michael@0: sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); michael@0: sctp_free_remote_addr(chk->whoTo); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); michael@0: SCTP_DECR_CHK_COUNT(); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: /* ASCONF queue MAY not be empty */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { michael@0: TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: if (chk->holds_key_ref) michael@0: sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); michael@0: sctp_free_remote_addr(chk->whoTo); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); michael@0: SCTP_DECR_CHK_COUNT(); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { michael@0: TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: if (chk->holds_key_ref) michael@0: sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); michael@0: sctp_free_remote_addr(chk->whoTo); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); michael@0: SCTP_DECR_CHK_COUNT(); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: michael@0: if (asoc->mapping_array) { michael@0: SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); michael@0: asoc->mapping_array = NULL; michael@0: } michael@0: if (asoc->nr_mapping_array) { michael@0: SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); michael@0: asoc->nr_mapping_array = NULL; michael@0: } michael@0: /* the stream outs */ michael@0: if (asoc->strmout) { michael@0: SCTP_FREE(asoc->strmout, SCTP_M_STRMO); michael@0: asoc->strmout = NULL; michael@0: } michael@0: asoc->strm_realoutsize = asoc->streamoutcnt = 0; michael@0: if (asoc->strmin) { michael@0: struct sctp_queued_to_read *ctl, *nctl; michael@0: michael@0: for (i = 0; i < asoc->streamincnt; i++) { michael@0: TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { michael@0: TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); michael@0: sctp_free_remote_addr(ctl->whoFrom); michael@0: if (ctl->data) { michael@0: sctp_m_freem(ctl->data); michael@0: ctl->data = NULL; michael@0: } michael@0: /* michael@0: * We don't free the address here michael@0: * since all the net's were freed michael@0: * above. michael@0: */ michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl); michael@0: SCTP_DECR_READQ_COUNT(); michael@0: } michael@0: } michael@0: SCTP_FREE(asoc->strmin, SCTP_M_STRMI); michael@0: asoc->strmin = NULL; michael@0: } michael@0: asoc->streamincnt = 0; michael@0: TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { michael@0: #ifdef INVARIANTS michael@0: if (SCTP_BASE_INFO(ipi_count_raddr) == 0) { michael@0: panic("no net's left alloc'ed, or list points to itself"); michael@0: } michael@0: #endif michael@0: TAILQ_REMOVE(&asoc->nets, net, sctp_next); michael@0: sctp_free_remote_addr(net); michael@0: } michael@0: LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) { michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: sctp_remove_laddr(laddr); michael@0: } michael@0: michael@0: /* pending asconf (address) parameters */ michael@0: TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); michael@0: SCTP_FREE(aparam,SCTP_M_ASC_ADDR); michael@0: } michael@0: TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); michael@0: if (aack->data != NULL) { michael@0: sctp_m_freem(aack->data); michael@0: } michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); michael@0: } michael@0: /* clean up auth stuff */ michael@0: if (asoc->local_hmacs) michael@0: sctp_free_hmaclist(asoc->local_hmacs); michael@0: if (asoc->peer_hmacs) michael@0: sctp_free_hmaclist(asoc->peer_hmacs); michael@0: michael@0: if (asoc->local_auth_chunks) michael@0: sctp_free_chunklist(asoc->local_auth_chunks); michael@0: if (asoc->peer_auth_chunks) michael@0: sctp_free_chunklist(asoc->peer_auth_chunks); michael@0: michael@0: sctp_free_authinfo(&asoc->authinfo); michael@0: michael@0: LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) { michael@0: LIST_REMOVE(shared_key, next); michael@0: sctp_free_sharedkey(shared_key); michael@0: /*sa_ignore FREED_MEMORY*/ michael@0: } michael@0: michael@0: /* Insert new items here :> */ michael@0: michael@0: /* Get rid of LOCK */ michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: SCTP_TCB_LOCK_DESTROY(stcb); michael@0: SCTP_TCB_SEND_LOCK_DESTROY(stcb); michael@0: if (from_inpcbfree == SCTP_NORMAL_PROC) { michael@0: SCTP_INP_INFO_WUNLOCK(); michael@0: SCTP_INP_RLOCK(inp); michael@0: } michael@0: #if defined(__APPLE__) /* TEMP CODE */ michael@0: stcb->freed_from_where = from_location; michael@0: #endif michael@0: #ifdef SCTP_TRACK_FREED_ASOCS michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: /* now clean up the tasoc itself */ michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); michael@0: SCTP_DECR_ASOC_COUNT(); michael@0: } else { michael@0: LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); michael@0: } michael@0: #else michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); michael@0: SCTP_DECR_ASOC_COUNT(); michael@0: #endif michael@0: if (from_inpcbfree == SCTP_NORMAL_PROC) { michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { michael@0: /* If its NOT the inp_free calling us AND michael@0: * sctp_close as been called, we michael@0: * call back... michael@0: */ michael@0: SCTP_INP_RUNLOCK(inp); michael@0: /* This will start the kill timer (if we are michael@0: * the last one) since we hold an increment yet. But michael@0: * this is the only safe way to do this michael@0: * since otherwise if the socket closes michael@0: * at the same time we are here we might michael@0: * collide in the cleanup. michael@0: */ michael@0: sctp_inpcb_free(inp, michael@0: SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, michael@0: SCTP_CALLED_DIRECTLY_NOCMPSET); michael@0: SCTP_INP_DECR_REF(inp); michael@0: goto out_of; michael@0: } else { michael@0: /* The socket is still open. */ michael@0: SCTP_INP_DECR_REF(inp); michael@0: } michael@0: } michael@0: if (from_inpcbfree == SCTP_NORMAL_PROC) { michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: out_of: michael@0: /* destroyed the asoc */ michael@0: #ifdef SCTP_LOG_CLOSING michael@0: sctp_log_closing(inp, NULL, 11); michael@0: #endif michael@0: return (1); michael@0: } michael@0: michael@0: michael@0: michael@0: /* michael@0: * determine if a destination is "reachable" based upon the addresses bound michael@0: * to the current endpoint (e.g. only v4 or v6 currently bound) michael@0: */ michael@0: /* michael@0: * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use michael@0: * assoc level v4/v6 flags, as the assoc *may* not have the same address michael@0: * types bound as its endpoint michael@0: */ michael@0: int michael@0: sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) michael@0: { michael@0: struct sctp_inpcb *inp; michael@0: int answer; michael@0: michael@0: /* michael@0: * No locks here, the TCB, in all cases is already locked and an michael@0: * assoc is up. There is either a INP lock by the caller applied (in michael@0: * asconf case when deleting an address) or NOT in the HB case, michael@0: * however if HB then the INP increment is up and the INP will not michael@0: * be removed (on top of the fact that we have a TCB lock). So we michael@0: * only want to read the sctp_flags, which is either bound-all or michael@0: * not.. no protection needed since once an assoc is up you can't be michael@0: * changing your binding. michael@0: */ michael@0: inp = stcb->sctp_ep; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: /* if bound all, destination is not restricted */ michael@0: /* michael@0: * RRS: Question during lock work: Is this correct? If you michael@0: * are bound-all you still might need to obey the V4--V6 michael@0: * flags??? IMO this bound-all stuff needs to be removed! michael@0: */ michael@0: return (1); michael@0: } michael@0: /* NOTE: all "scope" checks are done when local addresses are added */ michael@0: switch (destaddr->sa_family) { michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: answer = inp->inp_vflag & INP_IPV6; michael@0: #else michael@0: answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: answer = inp->inp_vflag & INP_IPV4; michael@0: #else michael@0: answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; michael@0: #endif michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: answer = inp->ip_inp.inp.inp_vflag & INP_CONN; michael@0: break; michael@0: #endif michael@0: default: michael@0: /* invalid family, so it's unreachable */ michael@0: answer = 0; michael@0: break; michael@0: } michael@0: return (answer); michael@0: } michael@0: michael@0: /* michael@0: * update the inp_vflags on an endpoint michael@0: */ michael@0: static void michael@0: sctp_update_ep_vflag(struct sctp_inpcb *inp) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: michael@0: /* first clear the flag */ michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: inp->inp_vflag = 0; michael@0: #else michael@0: inp->ip_inp.inp.inp_vflag = 0; michael@0: #endif michael@0: /* set the flag based on addresses on the ep list */ michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", michael@0: __FUNCTION__); michael@0: continue; michael@0: } michael@0: michael@0: if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { michael@0: continue; michael@0: } michael@0: switch (laddr->ifa->address.sa.sa_family) { michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: inp->inp_vflag |= INP_IPV6; michael@0: #else michael@0: inp->ip_inp.inp.inp_vflag |= INP_IPV6; michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: inp->inp_vflag |= INP_IPV4; michael@0: #else michael@0: inp->ip_inp.inp.inp_vflag |= INP_IPV4; michael@0: #endif michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: inp->ip_inp.inp.inp_vflag |= INP_CONN; michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Add the address to the endpoint local address list There is nothing to be michael@0: * done if we are bound to all addresses michael@0: */ michael@0: void michael@0: sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: int fnd, error = 0; michael@0: michael@0: fnd = 0; michael@0: michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: /* You are already bound to all. You have it already */ michael@0: return; michael@0: } michael@0: #ifdef INET6 michael@0: if (ifa->address.sa.sa_family == AF_INET6) { michael@0: if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { michael@0: /* Can't bind a non-useable addr. */ michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: /* first, is it already present? */ michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == ifa) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (fnd == 0) { michael@0: /* Not in the ep list */ michael@0: error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); michael@0: if (error != 0) michael@0: return; michael@0: inp->laddr_count++; michael@0: /* update inp_vflag flags */ michael@0: switch (ifa->address.sa.sa_family) { michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: inp->inp_vflag |= INP_IPV6; michael@0: #else michael@0: inp->ip_inp.inp.inp_vflag |= INP_IPV6; michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) michael@0: inp->inp_vflag |= INP_IPV4; michael@0: #else michael@0: inp->ip_inp.inp.inp_vflag |= INP_IPV4; michael@0: #endif michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: inp->ip_inp.inp.inp_vflag |= INP_CONN; michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: return; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * select a new (hopefully reachable) destination net (should only be used michael@0: * when we deleted an ep addr that is the only usable source address to reach michael@0: * the destination net) michael@0: */ michael@0: static void michael@0: sctp_select_primary_destination(struct sctp_tcb *stcb) michael@0: { michael@0: struct sctp_nets *net; michael@0: michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: /* for now, we'll just pick the first reachable one we find */ michael@0: if (net->dest_state & SCTP_ADDR_UNCONFIRMED) michael@0: continue; michael@0: if (sctp_destination_is_reachable(stcb, michael@0: (struct sockaddr *)&net->ro._l_addr)) { michael@0: /* found a reachable destination */ michael@0: stcb->asoc.primary_destination = net; michael@0: } michael@0: } michael@0: /* I can't there from here! ...we're gonna die shortly... */ michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Delete the address from the endpoint local address list There is nothing michael@0: * to be done if we are bound to all addresses michael@0: */ michael@0: void michael@0: sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: int fnd; michael@0: michael@0: fnd = 0; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { michael@0: /* You are already bound to all. You have it already */ michael@0: return; michael@0: } michael@0: LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { michael@0: if (laddr->ifa == ifa) { michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (fnd && (inp->laddr_count < 2)) { michael@0: /* can't delete unless there are at LEAST 2 addresses */ michael@0: return; michael@0: } michael@0: if (fnd) { michael@0: /* michael@0: * clean up any use of this address go through our michael@0: * associations and clear any last_used_address that match michael@0: * this one for each assoc, see if a new primary_destination michael@0: * is needed michael@0: */ michael@0: struct sctp_tcb *stcb; michael@0: michael@0: /* clean up "next_addr_touse" */ michael@0: if (inp->next_addr_touse == laddr) michael@0: /* delete this address */ michael@0: inp->next_addr_touse = NULL; michael@0: michael@0: /* clean up "last_used_address" */ michael@0: LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { michael@0: struct sctp_nets *net; michael@0: SCTP_TCB_LOCK(stcb); michael@0: if (stcb->asoc.last_used_address == laddr) michael@0: /* delete this address */ michael@0: stcb->asoc.last_used_address = NULL; michael@0: /* Now spin through all the nets and purge any ref to laddr */ michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: if (net->ro._s_addr && michael@0: (net->ro._s_addr->ifa == laddr->ifa)) { michael@0: /* Yep, purge src address selected */ michael@0: sctp_rtentry_t *rt; michael@0: michael@0: /* delete this address if cached */ michael@0: rt = net->ro.ro_rt; michael@0: if (rt != NULL) { michael@0: RTFREE(rt); michael@0: net->ro.ro_rt = NULL; michael@0: } michael@0: sctp_free_ifa(net->ro._s_addr); michael@0: net->ro._s_addr = NULL; michael@0: net->src_addr_selected = 0; michael@0: } michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } /* for each tcb */ michael@0: /* remove it from the ep list */ michael@0: sctp_remove_laddr(laddr); michael@0: inp->laddr_count--; michael@0: /* update inp_vflag flags */ michael@0: sctp_update_ep_vflag(inp); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * Add the address to the TCB local address restricted list. michael@0: * This is a "pending" address list (eg. addresses waiting for an michael@0: * ASCONF-ACK response) and cannot be used as a valid source address. michael@0: */ michael@0: void michael@0: sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: struct sctpladdr *list; michael@0: michael@0: /* michael@0: * Assumes TCB is locked.. and possibly the INP. May need to michael@0: * confirm/fix that if we need it and is not the case. michael@0: */ michael@0: list = &stcb->asoc.sctp_restricted_addrs; michael@0: michael@0: #ifdef INET6 michael@0: if (ifa->address.sa.sa_family == AF_INET6) { michael@0: if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { michael@0: /* Can't bind a non-existent addr. */ michael@0: return; michael@0: } michael@0: } michael@0: #endif michael@0: /* does the address already exist? */ michael@0: LIST_FOREACH(laddr, list, sctp_nxt_addr) { michael@0: if (laddr->ifa == ifa) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: /* add to the list */ michael@0: (void)sctp_insert_laddr(list, ifa, 0); michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * insert an laddr entry with the given ifa for the desired list michael@0: */ michael@0: int michael@0: sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) michael@0: { michael@0: struct sctp_laddr *laddr; michael@0: michael@0: laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); michael@0: if (laddr == NULL) { michael@0: /* out of memory? */ michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); michael@0: return (EINVAL); michael@0: } michael@0: SCTP_INCR_LADDR_COUNT(); michael@0: bzero(laddr, sizeof(*laddr)); michael@0: (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); michael@0: laddr->ifa = ifa; michael@0: laddr->action = act; michael@0: atomic_add_int(&ifa->refcount, 1); michael@0: /* insert it */ michael@0: LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: /* michael@0: * Remove an laddr entry from the local address list (on an assoc) michael@0: */ michael@0: void michael@0: sctp_remove_laddr(struct sctp_laddr *laddr) michael@0: { michael@0: michael@0: /* remove from the list */ michael@0: LIST_REMOVE(laddr, sctp_nxt_addr); michael@0: sctp_free_ifa(laddr->ifa); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr); michael@0: SCTP_DECR_LADDR_COUNT(); michael@0: } michael@0: michael@0: /* michael@0: * Remove a local address from the TCB local address restricted list michael@0: */ michael@0: void michael@0: sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) michael@0: { michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_laddr *laddr; michael@0: michael@0: /* michael@0: * This is called by asconf work. It is assumed that a) The TCB is michael@0: * locked and b) The INP is locked. This is true in as much as I can michael@0: * trace through the entry asconf code where I did these locks. michael@0: * Again, the ASCONF code is a bit different in that it does lock michael@0: * the INP during its work often times. This must be since we don't michael@0: * want other proc's looking up things while what they are looking michael@0: * up is changing :-D michael@0: */ michael@0: michael@0: inp = stcb->sctp_ep; michael@0: /* if subset bound and don't allow ASCONF's, can't delete last */ michael@0: if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && michael@0: sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { michael@0: if (stcb->sctp_ep->laddr_count < 2) { michael@0: /* can't delete last address */ michael@0: return; michael@0: } michael@0: } michael@0: LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { michael@0: /* remove the address if it exists */ michael@0: if (laddr->ifa == NULL) michael@0: continue; michael@0: if (laddr->ifa == ifa) { michael@0: sctp_remove_laddr(laddr); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: /* address not found! */ michael@0: return; michael@0: } michael@0: michael@0: #if defined(__FreeBSD__) michael@0: /* michael@0: * Temporarily remove for __APPLE__ until we use the Tiger equivalents michael@0: */ michael@0: /* sysctl */ michael@0: static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; michael@0: static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; michael@0: #endif /* FreeBSD || APPLE */ michael@0: michael@0: michael@0: michael@0: #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) michael@0: struct sctp_mcore_ctrl *sctp_mcore_workers = NULL; michael@0: int *sctp_cpuarry = NULL; michael@0: void michael@0: sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use) michael@0: { michael@0: /* Queue a packet to a processor for the specified core */ michael@0: struct sctp_mcore_queue *qent; michael@0: struct sctp_mcore_ctrl *wkq; michael@0: int need_wake = 0; michael@0: if (sctp_mcore_workers == NULL) { michael@0: /* Something went way bad during setup */ michael@0: sctp_input_with_port(m, off, 0); michael@0: return; michael@0: } michael@0: SCTP_MALLOC(qent, struct sctp_mcore_queue *, michael@0: (sizeof(struct sctp_mcore_queue)), michael@0: SCTP_M_MCORE); michael@0: if (qent == NULL) { michael@0: /* This is trouble */ michael@0: sctp_input_with_port(m, off, 0); michael@0: return; michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: qent->vn = curvnet; michael@0: #endif michael@0: qent->m = m; michael@0: qent->off = off; michael@0: qent->v6 = 0; michael@0: wkq = &sctp_mcore_workers[cpu_to_use]; michael@0: SCTP_MCORE_QLOCK(wkq); michael@0: michael@0: TAILQ_INSERT_TAIL(&wkq->que, qent, next); michael@0: if (wkq->running == 0) { michael@0: need_wake = 1; michael@0: } michael@0: SCTP_MCORE_QUNLOCK(wkq); michael@0: if (need_wake) { michael@0: wakeup(&wkq->running); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_mcore_thread(void *arg) michael@0: { michael@0: michael@0: struct sctp_mcore_ctrl *wkq; michael@0: struct sctp_mcore_queue *qent; michael@0: michael@0: wkq = (struct sctp_mcore_ctrl *)arg; michael@0: struct mbuf *m; michael@0: int off, v6; michael@0: michael@0: /* Wait for first tickle */ michael@0: SCTP_MCORE_LOCK(wkq); michael@0: wkq->running = 0; michael@0: msleep(&wkq->running, michael@0: &wkq->core_mtx, michael@0: 0, "wait for pkt", 0); michael@0: SCTP_MCORE_UNLOCK(wkq); michael@0: michael@0: /* Bind to our cpu */ michael@0: thread_lock(curthread); michael@0: sched_bind(curthread, wkq->cpuid); michael@0: thread_unlock(curthread); michael@0: michael@0: /* Now lets start working */ michael@0: SCTP_MCORE_LOCK(wkq); michael@0: /* Now grab lock and go */ michael@0: for (;;) { michael@0: SCTP_MCORE_QLOCK(wkq); michael@0: skip_sleep: michael@0: wkq->running = 1; michael@0: qent = TAILQ_FIRST(&wkq->que); michael@0: if (qent) { michael@0: TAILQ_REMOVE(&wkq->que, qent, next); michael@0: SCTP_MCORE_QUNLOCK(wkq); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_SET(qent->vn); michael@0: #endif michael@0: m = qent->m; michael@0: off = qent->off; michael@0: v6 = qent->v6; michael@0: SCTP_FREE(qent, SCTP_M_MCORE); michael@0: if (v6 == 0) { michael@0: sctp_input_with_port(m, off, 0); michael@0: } else { michael@0: SCTP_PRINTF("V6 not yet supported\n"); michael@0: sctp_m_freem(m); michael@0: } michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: #endif michael@0: SCTP_MCORE_QLOCK(wkq); michael@0: } michael@0: wkq->running = 0; michael@0: if (!TAILQ_EMPTY(&wkq->que)) { michael@0: goto skip_sleep; michael@0: } michael@0: SCTP_MCORE_QUNLOCK(wkq); michael@0: msleep(&wkq->running, michael@0: &wkq->core_mtx, michael@0: 0, "wait for pkt", 0); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: sctp_startup_mcore_threads(void) michael@0: { michael@0: int i, cpu; michael@0: michael@0: if (mp_ncpus == 1) michael@0: return; michael@0: michael@0: if (sctp_mcore_workers != NULL) { michael@0: /* Already been here in some previous michael@0: * vnet? michael@0: */ michael@0: return; michael@0: } michael@0: SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *, michael@0: ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)), michael@0: SCTP_M_MCORE); michael@0: if (sctp_mcore_workers == NULL) { michael@0: /* TSNH I hope */ michael@0: return; michael@0: } michael@0: memset(sctp_mcore_workers, 0 , ((mp_maxid+1) * michael@0: sizeof(struct sctp_mcore_ctrl))); michael@0: /* Init the structures */ michael@0: for (i = 0; i<=mp_maxid; i++) { michael@0: TAILQ_INIT(&sctp_mcore_workers[i].que); michael@0: SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]); michael@0: SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]); michael@0: sctp_mcore_workers[i].cpuid = i; michael@0: } michael@0: if (sctp_cpuarry == NULL) { michael@0: SCTP_MALLOC(sctp_cpuarry, int *, michael@0: (mp_ncpus * sizeof(int)), michael@0: SCTP_M_MCORE); michael@0: i = 0; michael@0: CPU_FOREACH(cpu) { michael@0: sctp_cpuarry[i] = cpu; michael@0: i++; michael@0: } michael@0: } michael@0: michael@0: /* Now start them all */ michael@0: CPU_FOREACH(cpu) { michael@0: #if __FreeBSD_version <= 701000 michael@0: (void)kthread_create(sctp_mcore_thread, michael@0: (void *)&sctp_mcore_workers[cpu], michael@0: &sctp_mcore_workers[cpu].thread_proc, michael@0: RFPROC, michael@0: SCTP_KTHREAD_PAGES, michael@0: SCTP_MCORE_NAME); michael@0: michael@0: #else michael@0: (void)kproc_create(sctp_mcore_thread, michael@0: (void *)&sctp_mcore_workers[cpu], michael@0: &sctp_mcore_workers[cpu].thread_proc, michael@0: RFPROC, michael@0: SCTP_KTHREAD_PAGES, michael@0: SCTP_MCORE_NAME); michael@0: #endif michael@0: michael@0: } michael@0: } michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000 michael@0: static struct mbuf * michael@0: sctp_netisr_hdlr(struct mbuf *m, uintptr_t source) michael@0: { michael@0: struct ip *ip; michael@0: struct sctphdr *sh; michael@0: int offset; michael@0: uint32_t flowid, tag; michael@0: michael@0: /* michael@0: * No flow id built by lower layers fix it so we michael@0: * create one. michael@0: */ michael@0: ip = mtod(m, struct ip *); michael@0: offset = (ip->ip_hl << 2) + sizeof(struct sctphdr); michael@0: if (SCTP_BUF_LEN(m) < offset) { michael@0: if ((m = m_pullup(m, offset)) == NULL) { michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: return (NULL); michael@0: } michael@0: ip = mtod(m, struct ip *); michael@0: } michael@0: sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2)); michael@0: tag = htonl(sh->v_tag); michael@0: flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); michael@0: m->m_pkthdr.flowid = flowid; michael@0: m->m_flags |= M_FLOWID; michael@0: return (m); michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: sctp_pcb_init() michael@0: { michael@0: /* michael@0: * SCTP initialization for the PCB structures should be called by michael@0: * the sctp_init() funciton. michael@0: */ michael@0: int i; michael@0: struct timeval tv; michael@0: michael@0: if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) { michael@0: /* error I was called twice */ michael@0: return; michael@0: } michael@0: SCTP_BASE_VAR(sctp_pcb_initialized) = 1; michael@0: michael@0: #if defined(SCTP_LOCAL_TRACE_BUF) michael@0: #if defined(__Windows__) michael@0: if (SCTP_BASE_SYSCTL(sctp_log) != NULL) { michael@0: bzero(SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log)); michael@0: } michael@0: #else michael@0: bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log)); michael@0: #endif michael@0: #endif michael@0: #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) michael@0: SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *, michael@0: ((mp_maxid+1) * sizeof(struct sctpstat)), michael@0: SCTP_M_MCORE); michael@0: #endif michael@0: (void)SCTP_GETTIME_TIMEVAL(&tv); michael@0: #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) michael@0: bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid+1))); michael@0: SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec; michael@0: SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec; michael@0: #else michael@0: bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat)); michael@0: SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; michael@0: SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; michael@0: #endif michael@0: /* init the empty list of (All) Endpoints */ michael@0: LIST_INIT(&SCTP_BASE_INFO(listhead)); michael@0: #if defined(__APPLE__) michael@0: LIST_INIT(&SCTP_BASE_INFO(inplisthead)); michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) michael@0: SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead); michael@0: SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init(); michael@0: lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); michael@0: SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); michael@0: SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init(); michael@0: lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr); michael@0: #else michael@0: SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead); michael@0: SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init(); michael@0: lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); michael@0: SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); michael@0: SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init(); michael@0: lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); michael@0: #endif michael@0: #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) michael@0: SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc; michael@0: in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo)); michael@0: #endif michael@0: #endif michael@0: michael@0: michael@0: /* init the hash table of endpoints */ michael@0: #if defined(__FreeBSD__) michael@0: #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000 michael@0: TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize)); michael@0: TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize)); michael@0: TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale)); michael@0: #else michael@0: TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE, michael@0: SCTP_BASE_SYSCTL(sctp_hashtblsize)); michael@0: TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE, michael@0: SCTP_BASE_SYSCTL(sctp_pcbtblsize)); michael@0: TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE, michael@0: SCTP_BASE_SYSCTL(sctp_chunkscale)); michael@0: #endif michael@0: #endif michael@0: SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31), michael@0: &SCTP_BASE_INFO(hashasocmark)); michael@0: SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), michael@0: &SCTP_BASE_INFO(hashmark)); michael@0: SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), michael@0: &SCTP_BASE_INFO(hashtcpmark)); michael@0: SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize); michael@0: michael@0: michael@0: SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, michael@0: &SCTP_BASE_INFO(hashvrfmark)); michael@0: michael@0: SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, michael@0: &SCTP_BASE_INFO(vrf_ifn_hashmark)); michael@0: /* init the zones */ michael@0: /* michael@0: * FIX ME: Should check for NULL returns, but if it does fail we are michael@0: * doomed to panic anyways... add later maybe. michael@0: */ michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep", michael@0: sizeof(struct sctp_inpcb), maxsockets); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc", michael@0: sizeof(struct sctp_tcb), sctp_max_number_of_assoc); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr", michael@0: sizeof(struct sctp_laddr), michael@0: (sctp_max_number_of_assoc * sctp_scale_up_for_address)); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr", michael@0: sizeof(struct sctp_nets), michael@0: (sctp_max_number_of_assoc * sctp_scale_up_for_address)); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk", michael@0: sizeof(struct sctp_tmit_chunk), michael@0: (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq", michael@0: sizeof(struct sctp_queued_to_read), michael@0: (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out", michael@0: sizeof(struct sctp_stream_queue_pending), michael@0: (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf", michael@0: sizeof(struct sctp_asconf), michael@0: (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); michael@0: michael@0: SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack", michael@0: sizeof(struct sctp_asconf_ack), michael@0: (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); michael@0: michael@0: michael@0: /* Master Lock INIT for info structure */ michael@0: SCTP_INP_INFO_LOCK_INIT(); michael@0: SCTP_STATLOG_INIT_LOCK(); michael@0: michael@0: SCTP_IPI_COUNT_INIT(); michael@0: SCTP_IPI_ADDR_INIT(); michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: SCTP_IP_PKTLOG_INIT(); michael@0: #endif michael@0: LIST_INIT(&SCTP_BASE_INFO(addr_wq)); michael@0: michael@0: SCTP_WQ_ADDR_INIT(); michael@0: /* not sure if we need all the counts */ michael@0: SCTP_BASE_INFO(ipi_count_ep) = 0; michael@0: /* assoc/tcb zone info */ michael@0: SCTP_BASE_INFO(ipi_count_asoc) = 0; michael@0: /* local addrlist zone info */ michael@0: SCTP_BASE_INFO(ipi_count_laddr) = 0; michael@0: /* remote addrlist zone info */ michael@0: SCTP_BASE_INFO(ipi_count_raddr) = 0; michael@0: /* chunk info */ michael@0: SCTP_BASE_INFO(ipi_count_chunk) = 0; michael@0: michael@0: /* socket queue zone info */ michael@0: SCTP_BASE_INFO(ipi_count_readq) = 0; michael@0: michael@0: /* stream out queue cont */ michael@0: SCTP_BASE_INFO(ipi_count_strmoq) = 0; michael@0: michael@0: SCTP_BASE_INFO(ipi_free_strmoq) = 0; michael@0: SCTP_BASE_INFO(ipi_free_chunks) = 0; michael@0: michael@0: SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer)); michael@0: michael@0: /* Init the TIMEWAIT list */ michael@0: for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { michael@0: LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]); michael@0: } michael@0: #if defined(SCTP_PROCESS_LEVEL_LOCKS) michael@0: #if defined(__Userspace_os_Windows) michael@0: InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup); michael@0: #else michael@0: (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL); michael@0: #endif michael@0: #endif michael@0: sctp_startup_iterator(); michael@0: michael@0: #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) michael@0: sctp_startup_mcore_threads(); michael@0: #endif michael@0: michael@0: #ifndef __Panda__ michael@0: /* michael@0: * INIT the default VRF which for BSD is the only one, other O/S's michael@0: * may have more. But initially they must start with one and then michael@0: * add the VRF's as addresses are added. michael@0: */ michael@0: sctp_init_vrf_list(SCTP_DEFAULT_VRF); michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000 michael@0: if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) { michael@0: SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n"); michael@0: } michael@0: #endif michael@0: #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_) michael@0: /* allocate the lock for the callout/timer queue */ michael@0: SCTP_TIMERQ_LOCK_INIT(); michael@0: TAILQ_INIT(&SCTP_BASE_INFO(callqueue)); michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: mbuf_init(NULL); michael@0: atomic_init(); michael@0: #if defined(INET) || defined(INET6) michael@0: recv_thread_init(); michael@0: #endif michael@0: #endif michael@0: } michael@0: michael@0: /* michael@0: * Assumes that the SCTP_BASE_INFO() lock is NOT held. michael@0: */ michael@0: void michael@0: sctp_pcb_finish(void) michael@0: { michael@0: struct sctp_vrflist *vrf_bucket; michael@0: struct sctp_vrf *vrf, *nvrf; michael@0: struct sctp_ifn *ifn, *nifn; michael@0: struct sctp_ifa *ifa, *nifa; michael@0: struct sctpvtaghead *chain; michael@0: struct sctp_tagblock *twait_block, *prev_twait_block; michael@0: struct sctp_laddr *wi, *nwi; michael@0: int i; michael@0: struct sctp_iterator *it, *nit; michael@0: michael@0: #if !defined(__FreeBSD__) michael@0: /* Notify the iterator to exit. */ michael@0: SCTP_IPI_ITERATOR_WQ_LOCK(); michael@0: sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT; michael@0: sctp_wakeup_iterator(); michael@0: SCTP_IPI_ITERATOR_WQ_UNLOCK(); michael@0: #endif michael@0: #if defined(__APPLE__) michael@0: #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) michael@0: in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo)); michael@0: #endif michael@0: SCTP_IPI_ITERATOR_WQ_LOCK(); michael@0: do { michael@0: msleep(&sctp_it_ctl.iterator_flags, michael@0: sctp_it_ctl.ipi_iterator_wq_mtx, michael@0: 0, "waiting_for_work", 0); michael@0: } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0); michael@0: thread_deallocate(sctp_it_ctl.thread_proc); michael@0: SCTP_IPI_ITERATOR_WQ_UNLOCK(); michael@0: #endif michael@0: #if defined(__Windows__) michael@0: if (sctp_it_ctl.iterator_thread_obj != NULL) { michael@0: NTSTATUS status = STATUS_SUCCESS; michael@0: michael@0: KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE); michael@0: status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj, michael@0: Executive, michael@0: KernelMode, michael@0: FALSE, michael@0: NULL); michael@0: ObDereferenceObject(sctp_it_ctl.iterator_thread_obj); michael@0: } michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: if (sctp_it_ctl.thread_proc) { michael@0: #if defined(__Userspace_os_Windows) michael@0: WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE); michael@0: CloseHandle(sctp_it_ctl.thread_proc); michael@0: sctp_it_ctl.thread_proc = NULL; michael@0: #else michael@0: pthread_join(sctp_it_ctl.thread_proc, NULL); michael@0: sctp_it_ctl.thread_proc = 0; michael@0: #endif michael@0: } michael@0: #endif michael@0: #if defined(SCTP_PROCESS_LEVEL_LOCKS) michael@0: #if defined(__Userspace_os_Windows) michael@0: DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup); michael@0: #else michael@0: pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup); michael@0: #endif michael@0: #endif michael@0: /* In FreeBSD the iterator thread never exits michael@0: * but we do clean up. michael@0: * The only way FreeBSD reaches here is if we have VRF's michael@0: * but we still add the ifdef to make it compile on old versions. michael@0: */ michael@0: SCTP_IPI_ITERATOR_WQ_LOCK(); michael@0: TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: if (it->vn != curvnet) { michael@0: continue; michael@0: } michael@0: #endif michael@0: TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); 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: } michael@0: SCTP_IPI_ITERATOR_WQ_UNLOCK(); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: SCTP_ITERATOR_LOCK(); michael@0: if ((sctp_it_ctl.cur_it) && michael@0: (sctp_it_ctl.cur_it->vn == curvnet)) { michael@0: sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; michael@0: } michael@0: SCTP_ITERATOR_UNLOCK(); michael@0: #endif michael@0: #if !defined(__FreeBSD__) michael@0: SCTP_IPI_ITERATOR_WQ_DESTROY(); michael@0: SCTP_ITERATOR_LOCK_DESTROY(); michael@0: #endif michael@0: SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer)); 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: SCTP_DECR_LADDR_COUNT(); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi); michael@0: } michael@0: SCTP_WQ_ADDR_UNLOCK(); michael@0: michael@0: /* michael@0: * free the vrf/ifn/ifa lists and hashes (be sure address monitor michael@0: * is destroyed first). michael@0: */ michael@0: vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))]; michael@0: LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) { michael@0: LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) { michael@0: LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) { michael@0: /* free the ifa */ michael@0: LIST_REMOVE(ifa, next_bucket); michael@0: LIST_REMOVE(ifa, next_ifa); michael@0: SCTP_FREE(ifa, SCTP_M_IFA); michael@0: } michael@0: /* free the ifn */ michael@0: LIST_REMOVE(ifn, next_bucket); michael@0: LIST_REMOVE(ifn, next_ifn); michael@0: SCTP_FREE(ifn, SCTP_M_IFN); michael@0: } michael@0: SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); michael@0: /* free the vrf */ michael@0: LIST_REMOVE(vrf, next_vrf); michael@0: SCTP_FREE(vrf, SCTP_M_VRF); michael@0: } michael@0: /* free the vrf hashes */ michael@0: SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark)); michael@0: SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark)); michael@0: #if defined(__Userspace__) && !defined(__Userspace_os_Windows) michael@0: /* free memory allocated by getifaddrs call */ michael@0: #if defined(INET) || defined(INET6) michael@0: freeifaddrs(g_interfaces); michael@0: #endif michael@0: #endif michael@0: michael@0: /* free the TIMEWAIT list elements malloc'd in the function michael@0: * sctp_add_vtag_to_timewait()... michael@0: */ michael@0: for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { michael@0: chain = &SCTP_BASE_INFO(vtag_timewait)[i]; michael@0: if (!LIST_EMPTY(chain)) { michael@0: prev_twait_block = NULL; michael@0: LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { michael@0: if (prev_twait_block) { michael@0: SCTP_FREE(prev_twait_block, SCTP_M_TIMW); michael@0: } michael@0: prev_twait_block = twait_block; michael@0: } michael@0: SCTP_FREE(prev_twait_block, SCTP_M_TIMW); michael@0: } michael@0: } michael@0: michael@0: /* free the locks and mutexes */ michael@0: #if defined(__APPLE__) michael@0: SCTP_TIMERQ_LOCK_DESTROY(); michael@0: #endif michael@0: #ifdef SCTP_PACKET_LOGGING michael@0: SCTP_IP_PKTLOG_DESTROY(); michael@0: #endif michael@0: SCTP_IPI_ADDR_DESTROY(); michael@0: #if defined(__APPLE__) michael@0: SCTP_IPI_COUNT_DESTROY(); michael@0: #endif michael@0: SCTP_STATLOG_DESTROY(); michael@0: SCTP_INP_INFO_LOCK_DESTROY(); michael@0: michael@0: SCTP_WQ_ADDR_DESTROY(); michael@0: michael@0: #if defined(__APPLE__) michael@0: #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) michael@0: lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); michael@0: lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp); michael@0: lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr); michael@0: #else michael@0: lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); michael@0: lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp); michael@0: lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); michael@0: #endif michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: SCTP_TIMERQ_LOCK_DESTROY(); michael@0: SCTP_ZONE_DESTROY(zone_mbuf); michael@0: SCTP_ZONE_DESTROY(zone_clust); michael@0: SCTP_ZONE_DESTROY(zone_ext_refcnt); michael@0: #endif michael@0: #if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__) michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf)); michael@0: SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack)); michael@0: #endif michael@0: /* Get rid of other stuff to */ michael@0: if (SCTP_BASE_INFO(sctp_asochash) != NULL) michael@0: SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark)); michael@0: if (SCTP_BASE_INFO(sctp_ephash) != NULL) michael@0: SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark)); michael@0: if (SCTP_BASE_INFO(sctp_tcpephash) != NULL) michael@0: SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark)); michael@0: #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) michael@0: SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: int michael@0: sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, michael@0: int offset, int limit, michael@0: struct sockaddr *src, struct sockaddr *dst, michael@0: struct sockaddr *altsa) michael@0: { michael@0: /* michael@0: * grub through the INIT pulling addresses and loading them to the michael@0: * nets structure in the asoc. The from address in the mbuf should michael@0: * also be loaded (if it is not already). This routine can be called michael@0: * with either INIT or INIT-ACK's as long as the m points to the IP michael@0: * packet and the offset points to the beginning of the parameters. michael@0: */ michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_nets *net, *nnet, *net_tmp; michael@0: struct sctp_paramhdr *phdr, parm_buf; michael@0: struct sctp_tcb *stcb_tmp; michael@0: uint16_t ptype, plen; michael@0: struct sockaddr *sa; michael@0: uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; michael@0: struct sctp_auth_random *p_random = NULL; michael@0: uint16_t random_len = 0; michael@0: uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; michael@0: struct sctp_auth_hmac_algo *hmacs = NULL; michael@0: uint16_t hmacs_len = 0; michael@0: uint8_t saw_asconf = 0; michael@0: uint8_t saw_asconf_ack = 0; michael@0: uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; michael@0: struct sctp_auth_chunk_list *chunks = NULL; michael@0: uint16_t num_chunks = 0; michael@0: sctp_key_t *new_key; michael@0: uint32_t keylen; michael@0: int got_random = 0, got_hmacs = 0, got_chklist = 0; michael@0: uint8_t ecn_allowed; 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: /* First get the destination address setup too. */ michael@0: #ifdef INET michael@0: memset(&sin, 0, sizeof(sin)); michael@0: sin.sin_family = AF_INET; michael@0: #ifdef HAVE_SIN_LEN michael@0: sin.sin_len = sizeof(sin); michael@0: #endif michael@0: sin.sin_port = stcb->rport; michael@0: #endif michael@0: #ifdef INET6 michael@0: memset(&sin6, 0, 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: sin6.sin6_port = stcb->rport; michael@0: #endif michael@0: if (altsa) { michael@0: sa = altsa; michael@0: } else { michael@0: sa = src; michael@0: } michael@0: /* Turn off ECN until we get through all params */ michael@0: ecn_allowed = 0; michael@0: TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { michael@0: /* mark all addresses that we have currently on the list */ michael@0: net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; michael@0: } michael@0: /* does the source address already exist? if so skip it */ michael@0: inp = stcb->sctp_ep; michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: michael@0: if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { michael@0: /* we must add the source address */ michael@0: /* no scope set here since we have a tcb already. */ michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (stcb->asoc.scope.ipv4_addr_legal) { michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) { michael@0: return (-1); michael@0: } michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (stcb->asoc.scope.ipv6_addr_legal) { michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { michael@0: return (-2); michael@0: } michael@0: } michael@0: break; michael@0: #endif michael@0: #if defined(__Userspace__) michael@0: case AF_CONN: michael@0: if (stcb->asoc.scope.conn_addr_legal) { michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { michael@0: return (-2); michael@0: } michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: } else { michael@0: if (net_tmp != NULL && stcb_tmp == stcb) { michael@0: net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; michael@0: } else if (stcb_tmp != stcb) { michael@0: /* It belongs to another association? */ michael@0: if (stcb_tmp) michael@0: SCTP_TCB_UNLOCK(stcb_tmp); michael@0: return (-3); michael@0: } michael@0: } michael@0: if (stcb->asoc.state == 0) { michael@0: /* the assoc was freed? */ michael@0: return (-4); michael@0: } michael@0: /* michael@0: * peer must explicitly turn this on. This may have been initialized michael@0: * to be "on" in order to allow local addr changes while INIT's are michael@0: * in flight. michael@0: */ michael@0: stcb->asoc.peer_supports_asconf = 0; michael@0: /* now we must go through each of the params. */ michael@0: phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); michael@0: while (phdr) { michael@0: ptype = ntohs(phdr->param_type); michael@0: plen = ntohs(phdr->param_length); michael@0: /* michael@0: * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype, michael@0: * (int)plen); michael@0: */ michael@0: if (offset + plen > limit) { michael@0: break; michael@0: } michael@0: if (plen == 0) { michael@0: break; michael@0: } michael@0: #ifdef INET michael@0: if (ptype == SCTP_IPV4_ADDRESS) { michael@0: if (stcb->asoc.scope.ipv4_addr_legal) { michael@0: struct sctp_ipv4addr_param *p4, p4_buf; michael@0: michael@0: /* ok get the v4 address and check/add */ michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)&p4_buf, michael@0: sizeof(p4_buf)); michael@0: if (plen != sizeof(struct sctp_ipv4addr_param) || michael@0: phdr == NULL) { michael@0: return (-5); michael@0: } michael@0: p4 = (struct sctp_ipv4addr_param *)phdr; michael@0: sin.sin_addr.s_addr = p4->addr; michael@0: if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { michael@0: /* Skip multi-cast addresses */ michael@0: goto next_param; michael@0: } michael@0: if ((sin.sin_addr.s_addr == INADDR_BROADCAST) || michael@0: (sin.sin_addr.s_addr == INADDR_ANY)) { michael@0: goto next_param; michael@0: } michael@0: sa = (struct sockaddr *)&sin; michael@0: inp = stcb->sctp_ep; michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, michael@0: dst, stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: michael@0: if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || michael@0: inp == NULL) { michael@0: /* we must add the source address */ michael@0: /* michael@0: * no scope set since we have a tcb michael@0: * already michael@0: */ michael@0: michael@0: /* michael@0: * we must validate the state again michael@0: * here michael@0: */ michael@0: add_it_now: michael@0: if (stcb->asoc.state == 0) { michael@0: /* the assoc was freed? */ michael@0: return (-7); michael@0: } michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) { michael@0: return (-8); michael@0: } michael@0: } else if (stcb_tmp == stcb) { michael@0: if (stcb->asoc.state == 0) { michael@0: /* the assoc was freed? */ michael@0: return (-10); michael@0: } michael@0: if (net != NULL) { michael@0: /* clear flag */ michael@0: net->dest_state &= michael@0: ~SCTP_ADDR_NOT_IN_ASSOC; michael@0: } michael@0: } else { michael@0: /* michael@0: * strange, address is in another michael@0: * assoc? straighten out locks. michael@0: */ michael@0: if (stcb_tmp) { michael@0: if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) { michael@0: /* in setup state we abort this guy */ michael@0: sctp_abort_an_association(stcb_tmp->sctp_ep, michael@0: stcb_tmp, NULL, SCTP_SO_NOT_LOCKED); michael@0: goto add_it_now; michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb_tmp); michael@0: } michael@0: michael@0: if (stcb->asoc.state == 0) { michael@0: /* the assoc was freed? */ michael@0: return (-12); michael@0: } michael@0: return (-13); michael@0: } michael@0: } michael@0: } else michael@0: #endif michael@0: #ifdef INET6 michael@0: if (ptype == SCTP_IPV6_ADDRESS) { michael@0: if (stcb->asoc.scope.ipv6_addr_legal) { michael@0: /* ok get the v6 address and check/add */ michael@0: struct sctp_ipv6addr_param *p6, p6_buf; michael@0: michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)&p6_buf, michael@0: sizeof(p6_buf)); michael@0: if (plen != sizeof(struct sctp_ipv6addr_param) || michael@0: phdr == NULL) { michael@0: return (-14); michael@0: } michael@0: p6 = (struct sctp_ipv6addr_param *)phdr; michael@0: memcpy((caddr_t)&sin6.sin6_addr, p6->addr, michael@0: sizeof(p6->addr)); michael@0: if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { michael@0: /* Skip multi-cast addresses */ michael@0: goto next_param; michael@0: } michael@0: if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { michael@0: /* Link local make no sense without scope */ michael@0: goto next_param; michael@0: } michael@0: sa = (struct sockaddr *)&sin6; michael@0: inp = stcb->sctp_ep; michael@0: atomic_add_int(&stcb->asoc.refcnt, 1); michael@0: stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, michael@0: dst, stcb); michael@0: atomic_add_int(&stcb->asoc.refcnt, -1); michael@0: if (stcb_tmp == NULL && michael@0: (inp == stcb->sctp_ep || inp == NULL)) { michael@0: /* michael@0: * we must validate the state again michael@0: * here michael@0: */ michael@0: add_it_now6: michael@0: if (stcb->asoc.state == 0) { michael@0: /* the assoc was freed? */ michael@0: return (-16); michael@0: } michael@0: /* michael@0: * we must add the address, no scope michael@0: * set michael@0: */ michael@0: if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) { michael@0: return (-17); michael@0: } michael@0: } else if (stcb_tmp == stcb) { michael@0: /* michael@0: * we must validate the state again michael@0: * here michael@0: */ michael@0: if (stcb->asoc.state == 0) { michael@0: /* the assoc was freed? */ michael@0: return (-19); michael@0: } michael@0: if (net != NULL) { michael@0: /* clear flag */ michael@0: net->dest_state &= michael@0: ~SCTP_ADDR_NOT_IN_ASSOC; michael@0: } michael@0: } else { michael@0: /* michael@0: * strange, address is in another michael@0: * assoc? straighten out locks. michael@0: */ michael@0: if (stcb_tmp) michael@0: if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) { michael@0: /* in setup state we abort this guy */ michael@0: sctp_abort_an_association(stcb_tmp->sctp_ep, michael@0: stcb_tmp, NULL, SCTP_SO_NOT_LOCKED); michael@0: goto add_it_now6; michael@0: } michael@0: SCTP_TCB_UNLOCK(stcb_tmp); michael@0: michael@0: if (stcb->asoc.state == 0) { michael@0: /* the assoc was freed? */ michael@0: return (-21); michael@0: } michael@0: return (-22); michael@0: } michael@0: } michael@0: } else michael@0: #endif michael@0: if (ptype == SCTP_ECN_CAPABLE) { michael@0: ecn_allowed = 1; michael@0: } else if (ptype == SCTP_ULP_ADAPTATION) { michael@0: if (stcb->asoc.state != SCTP_STATE_OPEN) { michael@0: struct sctp_adaptation_layer_indication ai, *aip; michael@0: michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)&ai, sizeof(ai)); michael@0: aip = (struct sctp_adaptation_layer_indication *)phdr; michael@0: if (aip) { michael@0: stcb->asoc.peers_adaptation = ntohl(aip->indication); michael@0: stcb->asoc.adaptation_needed = 1; michael@0: } michael@0: } michael@0: } else if (ptype == SCTP_SET_PRIM_ADDR) { michael@0: struct sctp_asconf_addr_param lstore, *fee; michael@0: int lptype; michael@0: struct sockaddr *lsa = NULL; michael@0: #ifdef INET michael@0: struct sctp_asconf_addrv4_param *fii; michael@0: #endif michael@0: michael@0: stcb->asoc.peer_supports_asconf = 1; michael@0: if (plen > sizeof(lstore)) { michael@0: return (-23); michael@0: } michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)&lstore, michael@0: min(plen,sizeof(lstore))); michael@0: if (phdr == NULL) { michael@0: return (-24); michael@0: } michael@0: fee = (struct sctp_asconf_addr_param *)phdr; michael@0: lptype = ntohs(fee->addrp.ph.param_type); michael@0: switch (lptype) { michael@0: #ifdef INET michael@0: case SCTP_IPV4_ADDRESS: michael@0: if (plen != michael@0: sizeof(struct sctp_asconf_addrv4_param)) { michael@0: SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", michael@0: (int)sizeof(struct sctp_asconf_addrv4_param), michael@0: plen); michael@0: } else { michael@0: fii = (struct sctp_asconf_addrv4_param *)fee; michael@0: sin.sin_addr.s_addr = fii->addrp.addr; michael@0: lsa = (struct sockaddr *)&sin; michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case SCTP_IPV6_ADDRESS: michael@0: if (plen != michael@0: sizeof(struct sctp_asconf_addr_param)) { michael@0: SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", michael@0: (int)sizeof(struct sctp_asconf_addr_param), michael@0: plen); michael@0: } else { michael@0: memcpy(sin6.sin6_addr.s6_addr, michael@0: fee->addrp.addr, michael@0: sizeof(fee->addrp.addr)); michael@0: lsa = (struct sockaddr *)&sin6; michael@0: } michael@0: break; michael@0: #endif michael@0: default: michael@0: break; michael@0: } michael@0: if (lsa) { michael@0: (void)sctp_set_primary_addr(stcb, sa, NULL); michael@0: } michael@0: } else if (ptype == SCTP_HAS_NAT_SUPPORT) { michael@0: stcb->asoc.peer_supports_nat = 1; michael@0: } else if (ptype == SCTP_PRSCTP_SUPPORTED) { michael@0: /* Peer supports pr-sctp */ michael@0: stcb->asoc.peer_supports_prsctp = 1; michael@0: } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { michael@0: /* A supported extension chunk */ michael@0: struct sctp_supported_chunk_types_param *pr_supported; michael@0: uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; michael@0: int num_ent, i; michael@0: michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)&local_store, min(sizeof(local_store),plen)); michael@0: if (phdr == NULL) { michael@0: return (-25); michael@0: } michael@0: stcb->asoc.peer_supports_asconf = 0; michael@0: stcb->asoc.peer_supports_prsctp = 0; michael@0: stcb->asoc.peer_supports_pktdrop = 0; michael@0: stcb->asoc.peer_supports_strreset = 0; michael@0: stcb->asoc.peer_supports_nr_sack = 0; michael@0: stcb->asoc.peer_supports_auth = 0; michael@0: pr_supported = (struct sctp_supported_chunk_types_param *)phdr; michael@0: num_ent = plen - sizeof(struct sctp_paramhdr); michael@0: for (i = 0; i < num_ent; i++) { michael@0: switch (pr_supported->chunk_types[i]) { michael@0: case SCTP_ASCONF: michael@0: case SCTP_ASCONF_ACK: michael@0: stcb->asoc.peer_supports_asconf = 1; michael@0: break; michael@0: case SCTP_FORWARD_CUM_TSN: michael@0: stcb->asoc.peer_supports_prsctp = 1; michael@0: break; michael@0: case SCTP_PACKET_DROPPED: michael@0: stcb->asoc.peer_supports_pktdrop = 1; michael@0: break; michael@0: case SCTP_NR_SELECTIVE_ACK: michael@0: stcb->asoc.peer_supports_nr_sack = 1; michael@0: break; michael@0: case SCTP_STREAM_RESET: michael@0: stcb->asoc.peer_supports_strreset = 1; michael@0: break; michael@0: case SCTP_AUTHENTICATION: michael@0: stcb->asoc.peer_supports_auth = 1; michael@0: break; michael@0: default: michael@0: /* one I have not learned yet */ michael@0: break; michael@0: michael@0: } michael@0: } michael@0: } else if (ptype == SCTP_RANDOM) { michael@0: if (plen > sizeof(random_store)) michael@0: break; michael@0: if (got_random) { michael@0: /* already processed a RANDOM */ michael@0: goto next_param; michael@0: } michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)random_store, michael@0: min(sizeof(random_store),plen)); michael@0: if (phdr == NULL) michael@0: return (-26); michael@0: p_random = (struct sctp_auth_random *)phdr; michael@0: random_len = plen - sizeof(*p_random); michael@0: /* enforce the random length */ michael@0: if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { michael@0: SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); michael@0: return (-27); michael@0: } michael@0: got_random = 1; michael@0: } else if (ptype == SCTP_HMAC_LIST) { michael@0: int num_hmacs; michael@0: int i; michael@0: michael@0: if (plen > sizeof(hmacs_store)) michael@0: break; michael@0: if (got_hmacs) { michael@0: /* already processed a HMAC list */ michael@0: goto next_param; michael@0: } michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)hmacs_store, michael@0: min(plen,sizeof(hmacs_store))); michael@0: if (phdr == NULL) michael@0: return (-28); michael@0: hmacs = (struct sctp_auth_hmac_algo *)phdr; michael@0: hmacs_len = plen - sizeof(*hmacs); michael@0: num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); michael@0: /* validate the hmac list */ michael@0: if (sctp_verify_hmac_param(hmacs, num_hmacs)) { michael@0: return (-29); michael@0: } michael@0: if (stcb->asoc.peer_hmacs != NULL) michael@0: sctp_free_hmaclist(stcb->asoc.peer_hmacs); michael@0: stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); michael@0: if (stcb->asoc.peer_hmacs != NULL) { michael@0: for (i = 0; i < num_hmacs; i++) { michael@0: (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, michael@0: ntohs(hmacs->hmac_ids[i])); michael@0: } michael@0: } michael@0: got_hmacs = 1; michael@0: } else if (ptype == SCTP_CHUNK_LIST) { michael@0: int i; michael@0: michael@0: if (plen > sizeof(chunks_store)) michael@0: break; michael@0: if (got_chklist) { michael@0: /* already processed a Chunks list */ michael@0: goto next_param; michael@0: } michael@0: phdr = sctp_get_next_param(m, offset, michael@0: (struct sctp_paramhdr *)chunks_store, michael@0: min(plen,sizeof(chunks_store))); michael@0: if (phdr == NULL) michael@0: return (-30); michael@0: chunks = (struct sctp_auth_chunk_list *)phdr; michael@0: num_chunks = plen - sizeof(*chunks); michael@0: if (stcb->asoc.peer_auth_chunks != NULL) michael@0: sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); michael@0: else michael@0: stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); michael@0: for (i = 0; i < num_chunks; i++) { michael@0: (void)sctp_auth_add_chunk(chunks->chunk_types[i], michael@0: stcb->asoc.peer_auth_chunks); michael@0: /* record asconf/asconf-ack if listed */ michael@0: if (chunks->chunk_types[i] == SCTP_ASCONF) michael@0: saw_asconf = 1; michael@0: if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) michael@0: saw_asconf_ack = 1; michael@0: michael@0: } michael@0: got_chklist = 1; michael@0: } else if ((ptype == SCTP_HEARTBEAT_INFO) || michael@0: (ptype == SCTP_STATE_COOKIE) || michael@0: (ptype == SCTP_UNRECOG_PARAM) || michael@0: (ptype == SCTP_COOKIE_PRESERVE) || michael@0: (ptype == SCTP_SUPPORTED_ADDRTYPE) || michael@0: (ptype == SCTP_ADD_IP_ADDRESS) || michael@0: (ptype == SCTP_DEL_IP_ADDRESS) || michael@0: (ptype == SCTP_ERROR_CAUSE_IND) || michael@0: (ptype == SCTP_SUCCESS_REPORT)) { michael@0: /* don't care */ ; michael@0: } else { michael@0: if ((ptype & 0x8000) == 0x0000) { michael@0: /* michael@0: * must stop processing the rest of the michael@0: * param's. Any report bits were handled michael@0: * with the call to michael@0: * sctp_arethere_unrecognized_parameters() michael@0: * when the INIT or INIT-ACK was first seen. michael@0: */ michael@0: break; michael@0: } michael@0: } michael@0: michael@0: next_param: michael@0: offset += SCTP_SIZE32(plen); michael@0: if (offset >= limit) { michael@0: break; michael@0: } michael@0: phdr = sctp_get_next_param(m, offset, &parm_buf, michael@0: sizeof(parm_buf)); michael@0: } michael@0: /* Now check to see if we need to purge any addresses */ michael@0: TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) { michael@0: if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == michael@0: SCTP_ADDR_NOT_IN_ASSOC) { michael@0: /* This address has been removed from the asoc */ michael@0: /* remove and free it */ michael@0: stcb->asoc.numnets--; michael@0: TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); michael@0: sctp_free_remote_addr(net); michael@0: if (net == stcb->asoc.primary_destination) { michael@0: stcb->asoc.primary_destination = NULL; michael@0: sctp_select_primary_destination(stcb); michael@0: } michael@0: } michael@0: } michael@0: if (ecn_allowed == 0) { michael@0: stcb->asoc.ecn_allowed = 0; michael@0: } michael@0: /* validate authentication required parameters */ michael@0: if (got_random && got_hmacs) { michael@0: stcb->asoc.peer_supports_auth = 1; michael@0: } else { michael@0: stcb->asoc.peer_supports_auth = 0; michael@0: } michael@0: if (!stcb->asoc.peer_supports_auth && got_chklist) { michael@0: /* peer does not support auth but sent a chunks list? */ michael@0: return (-31); michael@0: } michael@0: if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf && michael@0: !stcb->asoc.peer_supports_auth) { michael@0: /* peer supports asconf but not auth? */ michael@0: return (-32); michael@0: } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) && michael@0: ((saw_asconf == 0) || (saw_asconf_ack == 0))) { michael@0: return (-33); michael@0: } michael@0: /* concatenate the full random key */ michael@0: keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; michael@0: if (chunks != NULL) { michael@0: keylen += sizeof(*chunks) + num_chunks; michael@0: } michael@0: new_key = sctp_alloc_key(keylen); michael@0: if (new_key != NULL) { michael@0: /* copy in the RANDOM */ michael@0: if (p_random != NULL) { michael@0: keylen = sizeof(*p_random) + random_len; michael@0: bcopy(p_random, new_key->key, keylen); michael@0: } michael@0: /* append in the AUTH chunks */ michael@0: if (chunks != NULL) { michael@0: bcopy(chunks, new_key->key + keylen, michael@0: sizeof(*chunks) + num_chunks); michael@0: keylen += sizeof(*chunks) + num_chunks; michael@0: } michael@0: /* append in the HMACs */ michael@0: if (hmacs != NULL) { michael@0: bcopy(hmacs, new_key->key + keylen, michael@0: sizeof(*hmacs) + hmacs_len); michael@0: } michael@0: } else { michael@0: /* failed to get memory for the key */ michael@0: return (-34); michael@0: } michael@0: if (stcb->asoc.authinfo.peer_random != NULL) michael@0: sctp_free_key(stcb->asoc.authinfo.peer_random); michael@0: stcb->asoc.authinfo.peer_random = new_key; michael@0: sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); michael@0: sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, michael@0: struct sctp_nets *net) michael@0: { michael@0: /* make sure the requested primary address exists in the assoc */ michael@0: if (net == NULL && sa) michael@0: net = sctp_findnet(stcb, sa); michael@0: michael@0: if (net == NULL) { michael@0: /* didn't find the requested primary address! */ michael@0: return (-1); michael@0: } else { michael@0: /* set the primary address */ michael@0: if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { michael@0: /* Must be confirmed, so queue to set */ michael@0: net->dest_state |= SCTP_ADDR_REQ_PRIMARY; michael@0: return (0); michael@0: } michael@0: stcb->asoc.primary_destination = net; michael@0: if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) { michael@0: sctp_free_remote_addr(stcb->asoc.alternate); michael@0: stcb->asoc.alternate = NULL; michael@0: } michael@0: net = TAILQ_FIRST(&stcb->asoc.nets); michael@0: if (net != stcb->asoc.primary_destination) { michael@0: /* first one on the list is NOT the primary michael@0: * sctp_cmpaddr() is much more efficient if michael@0: * the primary is the first on the list, make it michael@0: * so. michael@0: */ michael@0: TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); michael@0: TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); michael@0: } michael@0: return (0); michael@0: } michael@0: } michael@0: michael@0: int michael@0: sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now) michael@0: { michael@0: /* michael@0: * This function serves two purposes. It will see if a TAG can be michael@0: * re-used and return 1 for yes it is ok and 0 for don't use that michael@0: * tag. A secondary function it will do is purge out old tags that michael@0: * can be removed. michael@0: */ michael@0: struct sctpvtaghead *chain; michael@0: struct sctp_tagblock *twait_block; michael@0: struct sctpasochead *head; michael@0: struct sctp_tcb *stcb; michael@0: int i; michael@0: michael@0: SCTP_INP_INFO_RLOCK(); michael@0: head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, michael@0: SCTP_BASE_INFO(hashasocmark))]; michael@0: if (head == NULL) { michael@0: /* invalid vtag */ michael@0: goto skip_vtag_check; michael@0: } michael@0: LIST_FOREACH(stcb, head, sctp_asocs) { michael@0: /* We choose not to lock anything here. TCB's can't be michael@0: * removed since we have the read lock, so they can't michael@0: * be freed on us, same thing for the INP. I may michael@0: * be wrong with this assumption, but we will go michael@0: * with it for now :-) michael@0: */ michael@0: if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { michael@0: continue; michael@0: } michael@0: if (stcb->asoc.my_vtag == tag) { michael@0: /* candidate */ michael@0: if (stcb->rport != rport) { michael@0: continue; michael@0: } michael@0: if (stcb->sctp_ep->sctp_lport != lport) { michael@0: continue; michael@0: } michael@0: /* Its a used tag set */ michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (0); michael@0: } michael@0: } michael@0: skip_vtag_check: michael@0: michael@0: chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; michael@0: /* Now what about timed wait ? */ michael@0: LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { michael@0: /* michael@0: * Block(s) are present, lets see if we have this tag in the michael@0: * list michael@0: */ michael@0: for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { michael@0: if (twait_block->vtag_block[i].v_tag == 0) { michael@0: /* not used */ michael@0: continue; michael@0: } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire < michael@0: now->tv_sec) { michael@0: /* Audit expires this guy */ michael@0: twait_block->vtag_block[i].tv_sec_at_expire = 0; michael@0: twait_block->vtag_block[i].v_tag = 0; michael@0: twait_block->vtag_block[i].lport = 0; michael@0: twait_block->vtag_block[i].rport = 0; michael@0: } else if ((twait_block->vtag_block[i].v_tag == tag) && michael@0: (twait_block->vtag_block[i].lport == lport) && michael@0: (twait_block->vtag_block[i].rport == rport)) { michael@0: /* Bad tag, sorry :< */ michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (0); michael@0: } michael@0: } michael@0: } michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (1); michael@0: } michael@0: michael@0: static void michael@0: sctp_drain_mbufs(struct sctp_tcb *stcb) michael@0: { michael@0: /* michael@0: * We must hunt this association for MBUF's past the cumack (i.e. michael@0: * out of order data that we can renege on). michael@0: */ michael@0: struct sctp_association *asoc; michael@0: struct sctp_tmit_chunk *chk, *nchk; michael@0: uint32_t cumulative_tsn_p1; michael@0: struct sctp_queued_to_read *ctl, *nctl; michael@0: int cnt, strmat; michael@0: uint32_t gap, i; michael@0: int fnd = 0; michael@0: michael@0: /* We look for anything larger than the cum-ack + 1 */ michael@0: michael@0: asoc = &stcb->asoc; michael@0: if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { michael@0: /* none we can reneg on. */ michael@0: return; michael@0: } michael@0: SCTP_STAT_INCR(sctps_protocol_drains_done); michael@0: cumulative_tsn_p1 = asoc->cumulative_tsn + 1; michael@0: cnt = 0; michael@0: /* First look in the re-assembly queue */ michael@0: TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { michael@0: if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) { michael@0: /* Yep it is above cum-ack */ michael@0: cnt++; michael@0: SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn); michael@0: asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size); michael@0: sctp_ucount_decr(asoc->cnt_on_reasm_queue); michael@0: SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); michael@0: TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); michael@0: if (chk->data) { michael@0: sctp_m_freem(chk->data); michael@0: chk->data = NULL; michael@0: } michael@0: sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: } michael@0: /* Ok that was fun, now we will drain all the inbound streams? */ michael@0: for (strmat = 0; strmat < asoc->streamincnt; strmat++) { michael@0: TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) { michael@0: if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) { michael@0: /* Yep it is above cum-ack */ michael@0: cnt++; michael@0: SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn); michael@0: asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length); michael@0: sctp_ucount_decr(asoc->cnt_on_all_streams); michael@0: SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); michael@0: TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next); michael@0: if (ctl->data) { michael@0: sctp_m_freem(ctl->data); michael@0: ctl->data = NULL; michael@0: } michael@0: sctp_free_remote_addr(ctl->whoFrom); michael@0: SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl); michael@0: SCTP_DECR_READQ_COUNT(); michael@0: } michael@0: } michael@0: } michael@0: if (cnt) { michael@0: /* We must back down to see what the new highest is */ michael@0: for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { michael@0: SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); michael@0: if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { michael@0: asoc->highest_tsn_inside_map = i; michael@0: fnd = 1; michael@0: break; michael@0: } michael@0: } michael@0: if (!fnd) { michael@0: asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; michael@0: } michael@0: michael@0: /* michael@0: * Question, should we go through the delivery queue? The only michael@0: * reason things are on here is the app not reading OR a p-d-api up. michael@0: * An attacker COULD send enough in to initiate the PD-API and then michael@0: * send a bunch of stuff to other streams... these would wind up on michael@0: * the delivery queue.. and then we would not get to them. But in michael@0: * order to do this I then have to back-track and un-deliver michael@0: * sequence numbers in streams.. el-yucko. I think for now we will michael@0: * NOT look at the delivery queue and leave it to be something to michael@0: * consider later. An alternative would be to abort the P-D-API with michael@0: * a notification and then deliver the data.... Or another method michael@0: * might be to keep track of how many times the situation occurs and michael@0: * if we see a possible attack underway just abort the association. michael@0: */ michael@0: #ifdef SCTP_DEBUG michael@0: SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); michael@0: #endif michael@0: /* michael@0: * Now do we need to find a new michael@0: * asoc->highest_tsn_inside_map? michael@0: */ michael@0: asoc->last_revoke_count = cnt; michael@0: (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); michael@0: /*sa_ignore NO_NULL_CHK*/ michael@0: sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); michael@0: sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED); michael@0: } michael@0: /* michael@0: * Another issue, in un-setting the TSN's in the mapping array we michael@0: * DID NOT adjust the highest_tsn marker. This will cause one of two michael@0: * things to occur. It may cause us to do extra work in checking for michael@0: * our mapping array movement. More importantly it may cause us to michael@0: * SACK every datagram. This may not be a bad thing though since we michael@0: * will recover once we get our cum-ack above and all this stuff we michael@0: * dumped recovered. michael@0: */ michael@0: } michael@0: michael@0: void michael@0: sctp_drain() michael@0: { michael@0: /* michael@0: * We must walk the PCB lists for ALL associations here. The system michael@0: * is LOW on MBUF's and needs help. This is where reneging will michael@0: * occur. We really hope this does NOT happen! michael@0: */ michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: VNET_ITERATOR_DECL(vnet_iter); michael@0: #else michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb; michael@0: michael@0: SCTP_STAT_INCR(sctps_protocol_drain_calls); michael@0: if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { michael@0: return; michael@0: } michael@0: #endif michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: VNET_LIST_RLOCK_NOSLEEP(); michael@0: VNET_FOREACH(vnet_iter) { michael@0: CURVNET_SET(vnet_iter); michael@0: struct sctp_inpcb *inp; michael@0: struct sctp_tcb *stcb; michael@0: #endif michael@0: michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: SCTP_STAT_INCR(sctps_protocol_drain_calls); michael@0: if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { michael@0: #ifdef VIMAGE michael@0: continue; michael@0: #else michael@0: return; michael@0: #endif michael@0: } michael@0: #endif michael@0: SCTP_INP_INFO_RLOCK(); michael@0: LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) { michael@0: /* For each endpoint */ michael@0: SCTP_INP_RLOCK(inp); michael@0: LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { michael@0: /* For each association */ michael@0: SCTP_TCB_LOCK(stcb); michael@0: sctp_drain_mbufs(stcb); michael@0: SCTP_TCB_UNLOCK(stcb); michael@0: } michael@0: SCTP_INP_RUNLOCK(inp); michael@0: } michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: CURVNET_RESTORE(); michael@0: } michael@0: VNET_LIST_RUNLOCK_NOSLEEP(); michael@0: #endif michael@0: } michael@0: michael@0: /* michael@0: * start a new iterator michael@0: * iterates through all endpoints and associations based on the pcb_state michael@0: * flags and asoc_state. "af" (mandatory) is executed for all matching michael@0: * assocs and "ef" (optional) is executed when the iterator completes. michael@0: * "inpf" (optional) is executed for each new endpoint as it is being michael@0: * iterated through. inpe (optional) is called when the inp completes michael@0: * its way through all the stcbs. michael@0: */ michael@0: int michael@0: sctp_initiate_iterator(inp_func inpf, michael@0: asoc_func af, michael@0: inp_func inpe, michael@0: uint32_t pcb_state, michael@0: uint32_t pcb_features, michael@0: uint32_t asoc_state, michael@0: void *argp, michael@0: uint32_t argi, michael@0: end_func ef, michael@0: struct sctp_inpcb *s_inp, michael@0: uint8_t chunk_output_off) michael@0: { michael@0: struct sctp_iterator *it = NULL; michael@0: michael@0: if (af == NULL) { michael@0: return (-1); michael@0: } michael@0: SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), michael@0: SCTP_M_ITER); michael@0: if (it == NULL) { michael@0: SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); michael@0: return (ENOMEM); michael@0: } michael@0: memset(it, 0, sizeof(*it)); michael@0: it->function_assoc = af; michael@0: it->function_inp = inpf; michael@0: if (inpf) michael@0: it->done_current_ep = 0; michael@0: else michael@0: it->done_current_ep = 1; michael@0: it->function_atend = ef; michael@0: it->pointer = argp; michael@0: it->val = argi; michael@0: it->pcb_flags = pcb_state; michael@0: it->pcb_features = pcb_features; michael@0: it->asoc_state = asoc_state; michael@0: it->function_inp_end = inpe; michael@0: it->no_chunk_output = chunk_output_off; michael@0: #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 michael@0: it->vn = curvnet; michael@0: #endif michael@0: if (s_inp) { michael@0: /* Assume lock is held here */ michael@0: it->inp = s_inp; michael@0: SCTP_INP_INCR_REF(it->inp); michael@0: it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; michael@0: } else { michael@0: SCTP_INP_INFO_RLOCK(); michael@0: it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead)); michael@0: if (it->inp) { michael@0: SCTP_INP_INCR_REF(it->inp); michael@0: } michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; michael@0: michael@0: } michael@0: SCTP_IPI_ITERATOR_WQ_LOCK(); michael@0: michael@0: TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); michael@0: if (sctp_it_ctl.iterator_running == 0) { michael@0: sctp_wakeup_iterator(); michael@0: } michael@0: SCTP_IPI_ITERATOR_WQ_UNLOCK(); michael@0: /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ michael@0: return (0); michael@0: }