1f8829a4aSRandall Stewart /*- 2b1006367SRandall Stewart * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3f8829a4aSRandall Stewart * 4f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without 5f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met: 6f8829a4aSRandall Stewart * 7f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice, 8f8829a4aSRandall Stewart * this list of conditions and the following disclaimer. 9f8829a4aSRandall Stewart * 10f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright 11f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in 12f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution. 13f8829a4aSRandall Stewart * 14f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its 15f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived 16f8829a4aSRandall Stewart * from this software without specific prior written permission. 17f8829a4aSRandall Stewart * 18f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE. 29f8829a4aSRandall Stewart */ 30f8829a4aSRandall Stewart 31f8829a4aSRandall Stewart /* $KAME: sctp_pcb.c,v 1.38 2005/03/06 16:04:18 itojun Exp $ */ 32f8829a4aSRandall Stewart 33f8829a4aSRandall Stewart #include <sys/cdefs.h> 34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$"); 35f8829a4aSRandall Stewart 36f8829a4aSRandall Stewart #include <netinet/sctp_os.h> 3793164cf9SRandall Stewart #include <sys/proc.h> 38f8829a4aSRandall Stewart #include <netinet/sctp_var.h> 3942551e99SRandall Stewart #include <netinet/sctp_sysctl.h> 40f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h> 41f8829a4aSRandall Stewart #include <netinet/sctputil.h> 42f8829a4aSRandall Stewart #include <netinet/sctp.h> 43f8829a4aSRandall Stewart #include <netinet/sctp_header.h> 44f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h> 45f8829a4aSRandall Stewart #include <netinet/sctp_output.h> 46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h> 4742551e99SRandall Stewart #include <netinet/sctp_bsd_addr.h> 48f8829a4aSRandall Stewart 49f8829a4aSRandall Stewart 50f8829a4aSRandall Stewart struct sctp_epinfo sctppcbinfo; 51f8829a4aSRandall Stewart 52f8829a4aSRandall Stewart /* FIX: we don't handle multiple link local scopes */ 53f8829a4aSRandall Stewart /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */ 54f8829a4aSRandall Stewart int 55f8829a4aSRandall Stewart SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b) 56f8829a4aSRandall Stewart { 57f8829a4aSRandall Stewart struct in6_addr tmp_a, tmp_b; 58f8829a4aSRandall Stewart 59f8829a4aSRandall Stewart /* use a copy of a and b */ 60f8829a4aSRandall Stewart tmp_a = *a; 61f8829a4aSRandall Stewart tmp_b = *b; 62f8829a4aSRandall Stewart in6_clearscope(&tmp_a); 63f8829a4aSRandall Stewart in6_clearscope(&tmp_b); 64f8829a4aSRandall Stewart return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); 65f8829a4aSRandall Stewart } 66f8829a4aSRandall Stewart 67f8829a4aSRandall Stewart void 68f8829a4aSRandall Stewart sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb) 69f8829a4aSRandall Stewart { 70f8829a4aSRandall Stewart /* 71f8829a4aSRandall Stewart * We really don't need to lock this, but I will just because it 72f8829a4aSRandall Stewart * does not hurt. 73f8829a4aSRandall Stewart */ 74f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 75f8829a4aSRandall Stewart spcb->ep_count = sctppcbinfo.ipi_count_ep; 76f8829a4aSRandall Stewart spcb->asoc_count = sctppcbinfo.ipi_count_asoc; 77f8829a4aSRandall Stewart spcb->laddr_count = sctppcbinfo.ipi_count_laddr; 78f8829a4aSRandall Stewart spcb->raddr_count = sctppcbinfo.ipi_count_raddr; 79f8829a4aSRandall Stewart spcb->chk_count = sctppcbinfo.ipi_count_chunk; 80f8829a4aSRandall Stewart spcb->readq_count = sctppcbinfo.ipi_count_readq; 81f8829a4aSRandall Stewart spcb->stream_oque = sctppcbinfo.ipi_count_strmoq; 82f8829a4aSRandall Stewart spcb->free_chunks = sctppcbinfo.ipi_free_chunks; 83f8829a4aSRandall Stewart 84f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 85f8829a4aSRandall Stewart } 86f8829a4aSRandall Stewart 8742551e99SRandall Stewart /* 8842551e99SRandall Stewart * Addresses are added to VRF's (Virtual Router's). For BSD we 8942551e99SRandall Stewart * have only the default VRF 0. We maintain a hash list of 9042551e99SRandall Stewart * VRF's. Each VRF has its own list of sctp_ifn's. Each of 9142551e99SRandall Stewart * these has a list of addresses. When we add a new address 9242551e99SRandall Stewart * to a VRF we lookup the ifn/ifn_index, if the ifn does 9342551e99SRandall Stewart * not exist we create it and add it to the list of IFN's 9442551e99SRandall Stewart * within the VRF. Once we have the sctp_ifn, we add the 9542551e99SRandall Stewart * address to the list. So we look something like: 9642551e99SRandall Stewart * 9742551e99SRandall Stewart * hash-vrf-table 9842551e99SRandall Stewart * vrf-> ifn-> ifn -> ifn 9942551e99SRandall Stewart * vrf | 10042551e99SRandall Stewart * ... +--ifa-> ifa -> ifa 10142551e99SRandall Stewart * vrf 10242551e99SRandall Stewart * 10342551e99SRandall Stewart * We keep these seperate lists since the SCTP subsystem will 10442551e99SRandall Stewart * point to these from its source address selection nets structure. 10542551e99SRandall Stewart * When an address is deleted it does not happen right away on 10642551e99SRandall Stewart * the SCTP side, it gets scheduled. What we do when a 10742551e99SRandall Stewart * delete happens is immediately remove the address from 10842551e99SRandall Stewart * the master list and decrement the refcount. As our 10942551e99SRandall Stewart * addip iterator works through and frees the src address 11042551e99SRandall Stewart * selection pointing to the sctp_ifa, eventually the refcount 11142551e99SRandall Stewart * will reach 0 and we will delete it. Note that it is assumed 11242551e99SRandall Stewart * that any locking on system level ifn/ifa is done at the 11342551e99SRandall Stewart * caller of these functions and these routines will only 11442551e99SRandall Stewart * lock the SCTP structures as they add or delete things. 11542551e99SRandall Stewart * 11642551e99SRandall Stewart * Other notes on VRF concepts. 11742551e99SRandall Stewart * - An endpoint can be in multiple VRF's 11842551e99SRandall Stewart * - An association lives within a VRF and only one VRF. 11942551e99SRandall Stewart * - Any incoming packet we can deduce the VRF for by 12042551e99SRandall Stewart * looking at the mbuf/pak inbound (for BSD its VRF=0 :D) 12142551e99SRandall Stewart * - Any downward send call or connect call must supply the 12242551e99SRandall Stewart * VRF via ancillary data or via some sort of set default 12342551e99SRandall Stewart * VRF socket option call (again for BSD no brainer since 12442551e99SRandall Stewart * the VRF is always 0). 12542551e99SRandall Stewart * - An endpoint may add multiple VRF's to it. 12642551e99SRandall Stewart * - Listening sockets can accept associations in any 12742551e99SRandall Stewart * of the VRF's they are in but the assoc will end up 12842551e99SRandall Stewart * in only one VRF (gotten from the packet or connect/send). 12942551e99SRandall Stewart * 13042551e99SRandall Stewart */ 13142551e99SRandall Stewart 13242551e99SRandall Stewart struct sctp_vrf * 13317205eccSRandall Stewart sctp_allocate_vrf(int vrf_id) 13442551e99SRandall Stewart { 13542551e99SRandall Stewart struct sctp_vrf *vrf = NULL; 13642551e99SRandall Stewart struct sctp_vrflist *bucket; 13742551e99SRandall Stewart 13842551e99SRandall Stewart /* First allocate the VRF structure */ 13917205eccSRandall Stewart vrf = sctp_find_vrf(vrf_id); 14042551e99SRandall Stewart if (vrf) { 14142551e99SRandall Stewart /* Already allocated */ 14242551e99SRandall Stewart return (vrf); 14342551e99SRandall Stewart } 14442551e99SRandall Stewart SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf), 145207304d4SRandall Stewart SCTP_M_VRF); 14642551e99SRandall Stewart if (vrf == NULL) { 14742551e99SRandall Stewart /* No memory */ 14842551e99SRandall Stewart #ifdef INVARIANTS 14917205eccSRandall Stewart panic("No memory for VRF:%d", vrf_id); 15042551e99SRandall Stewart #endif 15142551e99SRandall Stewart return (NULL); 15242551e99SRandall Stewart } 15342551e99SRandall Stewart /* setup the VRF */ 15442551e99SRandall Stewart memset(vrf, 0, sizeof(struct sctp_vrf)); 15517205eccSRandall Stewart vrf->vrf_id = vrf_id; 15642551e99SRandall Stewart LIST_INIT(&vrf->ifnlist); 15742551e99SRandall Stewart vrf->total_ifa_count = 0; 15880fefe0aSRandall Stewart vrf->refcount = 0; 159f4c93d24SRandall Stewart /* now also setup table ids */ 160f4c93d24SRandall Stewart SCTP_INIT_VRF_TABLEID(vrf); 1616a27c376SRandall Stewart /* Init the HASH of addresses */ 16217205eccSRandall Stewart vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE, 16317205eccSRandall Stewart &vrf->vrf_addr_hashmark); 1646a27c376SRandall Stewart if (vrf->vrf_addr_hash == NULL) { 1656a27c376SRandall Stewart /* No memory */ 1666a27c376SRandall Stewart #ifdef INVARIANTS 16717205eccSRandall Stewart panic("No memory for VRF:%d", vrf_id); 16817205eccSRandall Stewart #endif 169207304d4SRandall Stewart SCTP_FREE(vrf, SCTP_M_VRF); 17017205eccSRandall Stewart return (NULL); 17117205eccSRandall Stewart } 17242551e99SRandall Stewart /* Add it to the hash table */ 17317205eccSRandall Stewart bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)]; 17442551e99SRandall Stewart LIST_INSERT_HEAD(bucket, vrf, next_vrf); 17517205eccSRandall Stewart atomic_add_int(&sctppcbinfo.ipi_count_vrfs, 1); 17642551e99SRandall Stewart return (vrf); 17742551e99SRandall Stewart } 17842551e99SRandall Stewart 17942551e99SRandall Stewart 18042551e99SRandall Stewart struct sctp_ifn * 181f4c93d24SRandall Stewart sctp_find_ifn(void *ifn, uint32_t ifn_index) 18242551e99SRandall Stewart { 18342551e99SRandall Stewart struct sctp_ifn *sctp_ifnp; 18417205eccSRandall Stewart struct sctp_ifnlist *hash_ifn_head; 18542551e99SRandall Stewart 18642551e99SRandall Stewart /* 18742551e99SRandall Stewart * We assume the lock is held for the addresses if thats wrong 18842551e99SRandall Stewart * problems could occur :-) 18942551e99SRandall Stewart */ 190f4c93d24SRandall Stewart hash_ifn_head = &sctppcbinfo.vrf_ifn_hash[(ifn_index & sctppcbinfo.vrf_ifn_hashmark)]; 19117205eccSRandall Stewart LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) { 19242551e99SRandall Stewart if (sctp_ifnp->ifn_index == ifn_index) { 19342551e99SRandall Stewart return (sctp_ifnp); 19442551e99SRandall Stewart } 19542551e99SRandall Stewart if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) { 19642551e99SRandall Stewart return (sctp_ifnp); 19742551e99SRandall Stewart } 19842551e99SRandall Stewart } 19942551e99SRandall Stewart return (NULL); 20042551e99SRandall Stewart } 20142551e99SRandall Stewart 20217205eccSRandall Stewart 20317205eccSRandall Stewart 20442551e99SRandall Stewart struct sctp_vrf * 20517205eccSRandall Stewart sctp_find_vrf(uint32_t vrf_id) 20642551e99SRandall Stewart { 20742551e99SRandall Stewart struct sctp_vrflist *bucket; 20842551e99SRandall Stewart struct sctp_vrf *liste; 20942551e99SRandall Stewart 21017205eccSRandall Stewart bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)]; 21142551e99SRandall Stewart LIST_FOREACH(liste, bucket, next_vrf) { 21217205eccSRandall Stewart if (vrf_id == liste->vrf_id) { 21342551e99SRandall Stewart return (liste); 21442551e99SRandall Stewart } 21542551e99SRandall Stewart } 21642551e99SRandall Stewart return (NULL); 21742551e99SRandall Stewart } 21842551e99SRandall Stewart 21980fefe0aSRandall Stewart void 22080fefe0aSRandall Stewart sctp_free_vrf(struct sctp_vrf *vrf) 22180fefe0aSRandall Stewart { 22280fefe0aSRandall Stewart int ret; 22380fefe0aSRandall Stewart 22480fefe0aSRandall Stewart ret = atomic_fetchadd_int(&vrf->refcount, -1); 22580fefe0aSRandall Stewart if (ret == 1) { 22680fefe0aSRandall Stewart /* We zero'd the count */ 22780fefe0aSRandall Stewart LIST_REMOVE(vrf, next_vrf); 22880fefe0aSRandall Stewart SCTP_FREE(vrf, SCTP_M_VRF); 22980fefe0aSRandall Stewart atomic_subtract_int(&sctppcbinfo.ipi_count_vrfs, 1); 23080fefe0aSRandall Stewart } 23180fefe0aSRandall Stewart } 232bff64a4dSRandall Stewart 23342551e99SRandall Stewart void 23417205eccSRandall Stewart sctp_free_ifn(struct sctp_ifn *sctp_ifnp) 23517205eccSRandall Stewart { 23617205eccSRandall Stewart int ret; 23717205eccSRandall Stewart 23817205eccSRandall Stewart ret = atomic_fetchadd_int(&sctp_ifnp->refcount, -1); 23917205eccSRandall Stewart if (ret == 1) { 24017205eccSRandall Stewart /* We zero'd the count */ 24180fefe0aSRandall Stewart if (sctp_ifnp->vrf) { 24280fefe0aSRandall Stewart sctp_free_vrf(sctp_ifnp->vrf); 24380fefe0aSRandall Stewart } 244207304d4SRandall Stewart SCTP_FREE(sctp_ifnp, SCTP_M_IFN); 24517205eccSRandall Stewart atomic_subtract_int(&sctppcbinfo.ipi_count_ifns, 1); 24617205eccSRandall Stewart } 24717205eccSRandall Stewart } 24817205eccSRandall Stewart 24917205eccSRandall Stewart void 250f4c93d24SRandall Stewart sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu) 25117205eccSRandall Stewart { 25217205eccSRandall Stewart struct sctp_ifn *sctp_ifnp; 25317205eccSRandall Stewart 254f4c93d24SRandall Stewart sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index); 25517205eccSRandall Stewart if (sctp_ifnp != NULL) { 25617205eccSRandall Stewart sctp_ifnp->ifn_mtu = mtu; 25717205eccSRandall Stewart } 25817205eccSRandall Stewart } 25917205eccSRandall Stewart 26017205eccSRandall Stewart 26117205eccSRandall Stewart void 26242551e99SRandall Stewart sctp_free_ifa(struct sctp_ifa *sctp_ifap) 26342551e99SRandall Stewart { 26442551e99SRandall Stewart int ret; 26542551e99SRandall Stewart 26642551e99SRandall Stewart ret = atomic_fetchadd_int(&sctp_ifap->refcount, -1); 26742551e99SRandall Stewart if (ret == 1) { 26842551e99SRandall Stewart /* We zero'd the count */ 26980fefe0aSRandall Stewart if (sctp_ifap->ifn_p) { 27080fefe0aSRandall Stewart sctp_free_ifn(sctp_ifap->ifn_p); 27180fefe0aSRandall Stewart } 272207304d4SRandall Stewart SCTP_FREE(sctp_ifap, SCTP_M_IFA); 27317205eccSRandall Stewart atomic_subtract_int(&sctppcbinfo.ipi_count_ifas, 1); 27442551e99SRandall Stewart } 27542551e99SRandall Stewart } 27642551e99SRandall Stewart 27717205eccSRandall Stewart static void 27817205eccSRandall Stewart sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock) 27917205eccSRandall Stewart { 28017205eccSRandall Stewart struct sctp_ifn *found; 28117205eccSRandall Stewart 282f4c93d24SRandall Stewart found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index); 28317205eccSRandall Stewart if (found == NULL) { 28417205eccSRandall Stewart /* Not in the list.. sorry */ 28517205eccSRandall Stewart return; 28617205eccSRandall Stewart } 28717205eccSRandall Stewart if (hold_addr_lock == 0) 288c99efcf6SRandall Stewart SCTP_IPI_ADDR_WLOCK(); 28917205eccSRandall Stewart LIST_REMOVE(sctp_ifnp, next_bucket); 29017205eccSRandall Stewart LIST_REMOVE(sctp_ifnp, next_ifn); 291ad21a364SRandall Stewart SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index, 292ad21a364SRandall Stewart sctp_ifnp->registered_af); 29317205eccSRandall Stewart if (hold_addr_lock == 0) 294c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 29517205eccSRandall Stewart /* Take away the reference, and possibly free it */ 29617205eccSRandall Stewart sctp_free_ifn(sctp_ifnp); 29717205eccSRandall Stewart } 29817205eccSRandall Stewart 299851b7298SRandall Stewart void 300baf3da66SRandall Stewart sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr, 301baf3da66SRandall Stewart const char *if_name, uint32_t ifn_index) 302851b7298SRandall Stewart { 303851b7298SRandall Stewart struct sctp_vrf *vrf; 304851b7298SRandall Stewart struct sctp_ifa *sctp_ifap = NULL; 305851b7298SRandall Stewart 306c99efcf6SRandall Stewart SCTP_IPI_ADDR_RLOCK(); 307851b7298SRandall Stewart vrf = sctp_find_vrf(vrf_id); 308851b7298SRandall Stewart if (vrf == NULL) { 309baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); 310851b7298SRandall Stewart goto out; 311851b7298SRandall Stewart 312851b7298SRandall Stewart } 313851b7298SRandall Stewart sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); 314851b7298SRandall Stewart if (sctp_ifap == NULL) { 315baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); 316851b7298SRandall Stewart goto out; 317851b7298SRandall Stewart } 318851b7298SRandall Stewart if (sctp_ifap->ifn_p == NULL) { 319baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n"); 320851b7298SRandall Stewart goto out; 321851b7298SRandall Stewart } 322851b7298SRandall Stewart if (if_name) { 323851b7298SRandall Stewart int len1, len2; 324851b7298SRandall Stewart 325851b7298SRandall Stewart len1 = strlen(if_name); 326851b7298SRandall Stewart len2 = strlen(sctp_ifap->ifn_p->ifn_name); 327851b7298SRandall Stewart if (len1 != len2) { 328baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different lenght %d vs %d - ignored\n", 329851b7298SRandall Stewart len1, len2); 330851b7298SRandall Stewart goto out; 331851b7298SRandall Stewart } 332851b7298SRandall Stewart if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) { 333baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", 334851b7298SRandall Stewart sctp_ifap->ifn_p->ifn_name, 335851b7298SRandall Stewart if_name); 336851b7298SRandall Stewart goto out; 337851b7298SRandall Stewart } 338851b7298SRandall Stewart } else { 339851b7298SRandall Stewart if (sctp_ifap->ifn_p->ifn_index != ifn_index) { 340baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", 341851b7298SRandall Stewart sctp_ifap->ifn_p->ifn_index, ifn_index); 342851b7298SRandall Stewart goto out; 343851b7298SRandall Stewart } 344851b7298SRandall Stewart } 345851b7298SRandall Stewart 346851b7298SRandall Stewart sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID); 347851b7298SRandall Stewart sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; 348851b7298SRandall Stewart out: 349c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 350851b7298SRandall Stewart } 351851b7298SRandall Stewart 352851b7298SRandall Stewart void 353baf3da66SRandall Stewart sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr, 354baf3da66SRandall Stewart const char *if_name, uint32_t ifn_index) 355851b7298SRandall Stewart { 356851b7298SRandall Stewart struct sctp_vrf *vrf; 357851b7298SRandall Stewart struct sctp_ifa *sctp_ifap = NULL; 358851b7298SRandall Stewart 359c99efcf6SRandall Stewart SCTP_IPI_ADDR_RLOCK(); 360851b7298SRandall Stewart vrf = sctp_find_vrf(vrf_id); 361851b7298SRandall Stewart if (vrf == NULL) { 362baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); 363851b7298SRandall Stewart goto out; 364851b7298SRandall Stewart 365851b7298SRandall Stewart } 366851b7298SRandall Stewart sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); 367851b7298SRandall Stewart if (sctp_ifap == NULL) { 368baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); 369851b7298SRandall Stewart goto out; 370851b7298SRandall Stewart } 371851b7298SRandall Stewart if (sctp_ifap->ifn_p == NULL) { 372baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n"); 373851b7298SRandall Stewart goto out; 374851b7298SRandall Stewart } 375851b7298SRandall Stewart if (if_name) { 376851b7298SRandall Stewart int len1, len2; 377851b7298SRandall Stewart 378851b7298SRandall Stewart len1 = strlen(if_name); 379851b7298SRandall Stewart len2 = strlen(sctp_ifap->ifn_p->ifn_name); 380851b7298SRandall Stewart if (len1 != len2) { 381baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different lenght %d vs %d - ignored\n", 382851b7298SRandall Stewart len1, len2); 383851b7298SRandall Stewart goto out; 384851b7298SRandall Stewart } 385851b7298SRandall Stewart if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) { 386baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", 387851b7298SRandall Stewart sctp_ifap->ifn_p->ifn_name, 388851b7298SRandall Stewart if_name); 389851b7298SRandall Stewart goto out; 390851b7298SRandall Stewart } 391851b7298SRandall Stewart } else { 392851b7298SRandall Stewart if (sctp_ifap->ifn_p->ifn_index != ifn_index) { 393baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", 394851b7298SRandall Stewart sctp_ifap->ifn_p->ifn_index, ifn_index); 395851b7298SRandall Stewart goto out; 396851b7298SRandall Stewart } 397851b7298SRandall Stewart } 398851b7298SRandall Stewart 399851b7298SRandall Stewart sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE); 400851b7298SRandall Stewart sctp_ifap->localifa_flags |= SCTP_ADDR_VALID; 401851b7298SRandall Stewart out: 402c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 403851b7298SRandall Stewart } 404851b7298SRandall Stewart 405baf3da66SRandall Stewart /*- 406baf3da66SRandall Stewart * Add an ifa to an ifn. 407baf3da66SRandall Stewart * Register the interface as necessary. 408baf3da66SRandall Stewart * NOTE: ADDR write lock MUST be held. 409baf3da66SRandall Stewart */ 410baf3da66SRandall Stewart static void 411baf3da66SRandall Stewart sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap) 412baf3da66SRandall Stewart { 413baf3da66SRandall Stewart int ifa_af; 414baf3da66SRandall Stewart 415baf3da66SRandall Stewart LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); 416baf3da66SRandall Stewart sctp_ifap->ifn_p = sctp_ifnp; 417baf3da66SRandall Stewart atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); 418baf3da66SRandall Stewart /* update address counts */ 419baf3da66SRandall Stewart sctp_ifnp->ifa_count++; 420baf3da66SRandall Stewart ifa_af = sctp_ifap->address.sa.sa_family; 421baf3da66SRandall Stewart if (ifa_af == AF_INET) 422baf3da66SRandall Stewart sctp_ifnp->num_v4++; 423baf3da66SRandall Stewart else 424baf3da66SRandall Stewart sctp_ifnp->num_v6++; 425baf3da66SRandall Stewart if (sctp_ifnp->ifa_count == 1) { 426baf3da66SRandall Stewart /* register the new interface */ 427baf3da66SRandall Stewart SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af); 428baf3da66SRandall Stewart sctp_ifnp->registered_af = ifa_af; 429baf3da66SRandall Stewart } 430baf3da66SRandall Stewart } 431baf3da66SRandall Stewart 432baf3da66SRandall Stewart /*- 433baf3da66SRandall Stewart * Remove an ifa from its ifn. 434baf3da66SRandall Stewart * If no more addresses exist, remove the ifn too. Otherwise, re-register 435baf3da66SRandall Stewart * the interface based on the remaining address families left. 436baf3da66SRandall Stewart * NOTE: ADDR write lock MUST be held. 437baf3da66SRandall Stewart */ 438baf3da66SRandall Stewart static void 439baf3da66SRandall Stewart sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap) 440baf3da66SRandall Stewart { 441baf3da66SRandall Stewart uint32_t ifn_index; 442baf3da66SRandall Stewart 443baf3da66SRandall Stewart LIST_REMOVE(sctp_ifap, next_ifa); 444baf3da66SRandall Stewart if (sctp_ifap->ifn_p) { 445baf3da66SRandall Stewart /* update address counts */ 446baf3da66SRandall Stewart sctp_ifap->ifn_p->ifa_count--; 447baf3da66SRandall Stewart if (sctp_ifap->address.sa.sa_family == AF_INET6) 448baf3da66SRandall Stewart sctp_ifap->ifn_p->num_v6--; 449baf3da66SRandall Stewart else if (sctp_ifap->address.sa.sa_family == AF_INET) 450baf3da66SRandall Stewart sctp_ifap->ifn_p->num_v4--; 451baf3da66SRandall Stewart 452baf3da66SRandall Stewart ifn_index = sctp_ifap->ifn_p->ifn_index; 453baf3da66SRandall Stewart if (SCTP_LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) { 454baf3da66SRandall Stewart /* remove the ifn, possibly freeing it */ 455baf3da66SRandall Stewart sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED); 456baf3da66SRandall Stewart } else { 457baf3da66SRandall Stewart /* re-register address family type, if needed */ 458baf3da66SRandall Stewart if ((sctp_ifap->ifn_p->num_v6 == 0) && 459baf3da66SRandall Stewart (sctp_ifap->ifn_p->registered_af == AF_INET6)) { 460baf3da66SRandall Stewart SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET6); 461baf3da66SRandall Stewart SCTP_REGISTER_INTERFACE(ifn_index, AF_INET); 462baf3da66SRandall Stewart sctp_ifap->ifn_p->registered_af = AF_INET; 463baf3da66SRandall Stewart } else if ((sctp_ifap->ifn_p->num_v4 == 0) && 464baf3da66SRandall Stewart (sctp_ifap->ifn_p->registered_af == AF_INET)) { 465baf3da66SRandall Stewart SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET); 466baf3da66SRandall Stewart SCTP_REGISTER_INTERFACE(ifn_index, AF_INET6); 467baf3da66SRandall Stewart sctp_ifap->ifn_p->registered_af = AF_INET6; 468baf3da66SRandall Stewart } 469baf3da66SRandall Stewart /* free the ifn refcount */ 470baf3da66SRandall Stewart sctp_free_ifn(sctp_ifap->ifn_p); 471baf3da66SRandall Stewart } 472baf3da66SRandall Stewart sctp_ifap->ifn_p = NULL; 473baf3da66SRandall Stewart } 474baf3da66SRandall Stewart } 47517205eccSRandall Stewart 47642551e99SRandall Stewart struct sctp_ifa * 47717205eccSRandall Stewart sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index, 478baf3da66SRandall Stewart uint32_t ifn_type, const char *if_name, void *ifa, 479baf3da66SRandall Stewart struct sockaddr *addr, uint32_t ifa_flags, 48017205eccSRandall Stewart int dynamic_add) 48142551e99SRandall Stewart { 48242551e99SRandall Stewart struct sctp_vrf *vrf; 48342551e99SRandall Stewart struct sctp_ifn *sctp_ifnp = NULL; 48442551e99SRandall Stewart struct sctp_ifa *sctp_ifap = NULL; 48517205eccSRandall Stewart struct sctp_ifalist *hash_addr_head; 48617205eccSRandall Stewart struct sctp_ifnlist *hash_ifn_head; 4876a27c376SRandall Stewart uint32_t hash_of_addr; 488ad21a364SRandall Stewart int new_ifn_af = 0; 48942551e99SRandall Stewart 490baf3da66SRandall Stewart #ifdef SCTP_DEBUG 491baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id); 492baf3da66SRandall Stewart SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); 493baf3da66SRandall Stewart #endif 494c99efcf6SRandall Stewart SCTP_IPI_ADDR_WLOCK(); 49580fefe0aSRandall Stewart sctp_ifnp = sctp_find_ifn(ifn, ifn_index); 49680fefe0aSRandall Stewart if (sctp_ifnp) { 49780fefe0aSRandall Stewart vrf = sctp_ifnp->vrf; 49880fefe0aSRandall Stewart } else { 49917205eccSRandall Stewart vrf = sctp_find_vrf(vrf_id); 50042551e99SRandall Stewart if (vrf == NULL) { 50117205eccSRandall Stewart vrf = sctp_allocate_vrf(vrf_id); 50242551e99SRandall Stewart if (vrf == NULL) { 503c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 50442551e99SRandall Stewart return (NULL); 50542551e99SRandall Stewart } 50642551e99SRandall Stewart } 50780fefe0aSRandall Stewart } 50842551e99SRandall Stewart if (sctp_ifnp == NULL) { 50942551e99SRandall Stewart /* 51042551e99SRandall Stewart * build one and add it, can't hold lock until after malloc 51142551e99SRandall Stewart * done though. 51242551e99SRandall Stewart */ 513c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 514baf3da66SRandall Stewart SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, 515baf3da66SRandall Stewart sizeof(struct sctp_ifn), SCTP_M_IFN); 51642551e99SRandall Stewart if (sctp_ifnp == NULL) { 51742551e99SRandall Stewart #ifdef INVARIANTS 51842551e99SRandall Stewart panic("No memory for IFN:%u", sctp_ifnp->ifn_index); 51942551e99SRandall Stewart #endif 52042551e99SRandall Stewart return (NULL); 52142551e99SRandall Stewart } 522eacc51c5SRandall Stewart memset(sctp_ifnp, 0, sizeof(struct sctp_ifn)); 52342551e99SRandall Stewart sctp_ifnp->ifn_index = ifn_index; 52442551e99SRandall Stewart sctp_ifnp->ifn_p = ifn; 52542551e99SRandall Stewart sctp_ifnp->ifn_type = ifn_type; 52617205eccSRandall Stewart sctp_ifnp->refcount = 1; 52742551e99SRandall Stewart sctp_ifnp->vrf = vrf; 528eacc51c5SRandall Stewart 52980fefe0aSRandall Stewart atomic_add_int(&vrf->refcount, 1); 530ad21a364SRandall Stewart sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family); 53117205eccSRandall Stewart if (if_name != NULL) { 53242551e99SRandall Stewart memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ); 53317205eccSRandall Stewart } else { 53417205eccSRandall Stewart memcpy(sctp_ifnp->ifn_name, "unknown", min(7, SCTP_IFNAMSIZ)); 53517205eccSRandall Stewart } 536f4c93d24SRandall Stewart hash_ifn_head = &sctppcbinfo.vrf_ifn_hash[(ifn_index & sctppcbinfo.vrf_ifn_hashmark)]; 53742551e99SRandall Stewart LIST_INIT(&sctp_ifnp->ifalist); 538c99efcf6SRandall Stewart SCTP_IPI_ADDR_WLOCK(); 53917205eccSRandall Stewart LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket); 54042551e99SRandall Stewart LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn); 54117205eccSRandall Stewart atomic_add_int(&sctppcbinfo.ipi_count_ifns, 1); 542ad21a364SRandall Stewart new_ifn_af = 1; 54342551e99SRandall Stewart } 544851b7298SRandall Stewart sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); 54542551e99SRandall Stewart if (sctp_ifap) { 54642551e99SRandall Stewart /* Hmm, it already exists? */ 54742551e99SRandall Stewart if ((sctp_ifap->ifn_p) && 54842551e99SRandall Stewart (sctp_ifap->ifn_p->ifn_index == ifn_index)) { 549baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n", 550baf3da66SRandall Stewart sctp_ifap->ifn_p->ifn_name, ifn_index, 551baf3da66SRandall Stewart sctp_ifap); 55280fefe0aSRandall Stewart if (new_ifn_af) { 55380fefe0aSRandall Stewart /* Remove the created one that we don't want */ 554baf3da66SRandall Stewart sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED); 55580fefe0aSRandall Stewart } 55642551e99SRandall Stewart if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) { 55742551e99SRandall Stewart /* easy to solve, just switch back to active */ 558baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n"); 55942551e99SRandall Stewart sctp_ifap->localifa_flags = SCTP_ADDR_VALID; 56042551e99SRandall Stewart sctp_ifap->ifn_p = sctp_ifnp; 56180fefe0aSRandall Stewart atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); 562baf3da66SRandall Stewart } 56342551e99SRandall Stewart exit_stage_left: 564c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 56542551e99SRandall Stewart return (sctp_ifap); 56642551e99SRandall Stewart } else { 56742551e99SRandall Stewart if (sctp_ifap->ifn_p) { 56842551e99SRandall Stewart /* 569baf3da66SRandall Stewart * The last IFN gets the address, removee 570baf3da66SRandall Stewart * the old one 57142551e99SRandall Stewart */ 572baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n", 573baf3da66SRandall Stewart sctp_ifap, sctp_ifap->ifn_p->ifn_name, 574baf3da66SRandall Stewart sctp_ifap->ifn_p->ifn_index, if_name, 575baf3da66SRandall Stewart ifn_index); 576baf3da66SRandall Stewart /* remove the address from the old ifn */ 577baf3da66SRandall Stewart sctp_remove_ifa_from_ifn(sctp_ifap); 578baf3da66SRandall Stewart /* move the address over to the new ifn */ 579baf3da66SRandall Stewart sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); 58042551e99SRandall Stewart goto exit_stage_left; 58142551e99SRandall Stewart } else { 58242551e99SRandall Stewart /* repair ifnp which was NULL ? */ 58342551e99SRandall Stewart sctp_ifap->localifa_flags = SCTP_ADDR_VALID; 584baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n", 585baf3da66SRandall Stewart sctp_ifnp, sctp_ifap); 586baf3da66SRandall Stewart sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); 58742551e99SRandall Stewart } 58842551e99SRandall Stewart goto exit_stage_left; 58942551e99SRandall Stewart } 59042551e99SRandall Stewart } 591c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 592207304d4SRandall Stewart SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA); 59342551e99SRandall Stewart if (sctp_ifap == NULL) { 59442551e99SRandall Stewart #ifdef INVARIANTS 59542551e99SRandall Stewart panic("No memory for IFA"); 59642551e99SRandall Stewart #endif 59742551e99SRandall Stewart return (NULL); 59842551e99SRandall Stewart } 599132dea7dSRandall Stewart memset(sctp_ifap, 0, sizeof(struct sctp_ifa)); 60042551e99SRandall Stewart sctp_ifap->ifn_p = sctp_ifnp; 60142551e99SRandall Stewart atomic_add_int(&sctp_ifnp->refcount, 1); 60222a67197SRandall Stewart sctp_ifap->vrf_id = vrf_id; 60342551e99SRandall Stewart sctp_ifap->ifa = ifa; 60442551e99SRandall Stewart memcpy(&sctp_ifap->address, addr, addr->sa_len); 60542551e99SRandall Stewart sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE; 60642551e99SRandall Stewart sctp_ifap->flags = ifa_flags; 60742551e99SRandall Stewart /* Set scope */ 60842551e99SRandall Stewart if (sctp_ifap->address.sa.sa_family == AF_INET) { 60942551e99SRandall Stewart struct sockaddr_in *sin; 61042551e99SRandall Stewart 61142551e99SRandall Stewart sin = (struct sockaddr_in *)&sctp_ifap->address.sin; 61242551e99SRandall Stewart if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || 61342551e99SRandall Stewart (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) { 61442551e99SRandall Stewart sctp_ifap->src_is_loop = 1; 61542551e99SRandall Stewart } 61642551e99SRandall Stewart if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { 61742551e99SRandall Stewart sctp_ifap->src_is_priv = 1; 61842551e99SRandall Stewart } 619ad21a364SRandall Stewart sctp_ifnp->num_v4++; 620ad21a364SRandall Stewart if (new_ifn_af) 621ad21a364SRandall Stewart new_ifn_af = AF_INET; 62242551e99SRandall Stewart } else if (sctp_ifap->address.sa.sa_family == AF_INET6) { 62342551e99SRandall Stewart /* ok to use deprecated addresses? */ 62442551e99SRandall Stewart struct sockaddr_in6 *sin6; 62542551e99SRandall Stewart 62642551e99SRandall Stewart sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6; 62742551e99SRandall Stewart if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || 62842551e99SRandall Stewart (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) { 62942551e99SRandall Stewart sctp_ifap->src_is_loop = 1; 63042551e99SRandall Stewart } 63142551e99SRandall Stewart if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 63242551e99SRandall Stewart sctp_ifap->src_is_priv = 1; 63342551e99SRandall Stewart } 634ad21a364SRandall Stewart sctp_ifnp->num_v6++; 635ad21a364SRandall Stewart if (new_ifn_af) 636ad21a364SRandall Stewart new_ifn_af = AF_INET6; 637ad21a364SRandall Stewart } else { 638ad21a364SRandall Stewart new_ifn_af = 0; 63942551e99SRandall Stewart } 6406a27c376SRandall Stewart hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa); 6416a27c376SRandall Stewart 64242551e99SRandall Stewart if ((sctp_ifap->src_is_priv == 0) && 64342551e99SRandall Stewart (sctp_ifap->src_is_loop == 0)) { 64442551e99SRandall Stewart sctp_ifap->src_is_glob = 1; 64542551e99SRandall Stewart } 646c99efcf6SRandall Stewart SCTP_IPI_ADDR_WLOCK(); 64717205eccSRandall Stewart hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; 64817205eccSRandall Stewart LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket); 64942551e99SRandall Stewart sctp_ifap->refcount = 1; 65042551e99SRandall Stewart LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); 65142551e99SRandall Stewart sctp_ifnp->ifa_count++; 65242551e99SRandall Stewart vrf->total_ifa_count++; 65317205eccSRandall Stewart atomic_add_int(&sctppcbinfo.ipi_count_ifas, 1); 654ad21a364SRandall Stewart if (new_ifn_af) { 655ad21a364SRandall Stewart SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af); 656ad21a364SRandall Stewart sctp_ifnp->registered_af = new_ifn_af; 657ad21a364SRandall Stewart } 658c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 659d06c82f1SRandall Stewart if (dynamic_add) { 660d06c82f1SRandall Stewart /* 661d06c82f1SRandall Stewart * Bump up the refcount so that when the timer completes it 662d06c82f1SRandall Stewart * will drop back down. 663d06c82f1SRandall Stewart */ 664d06c82f1SRandall Stewart struct sctp_laddr *wi; 665d06c82f1SRandall Stewart 666d06c82f1SRandall Stewart atomic_add_int(&sctp_ifap->refcount, 1); 667d06c82f1SRandall Stewart wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr); 668d06c82f1SRandall Stewart if (wi == NULL) { 669d06c82f1SRandall Stewart /* 670d06c82f1SRandall Stewart * Gak, what can we do? We have lost an address 671d06c82f1SRandall Stewart * change can you say HOSED? 672d06c82f1SRandall Stewart */ 673baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); 674d06c82f1SRandall Stewart /* Opps, must decrement the count */ 675baf3da66SRandall Stewart sctp_del_addr_from_vrf(vrf_id, addr, ifn_index, 676baf3da66SRandall Stewart if_name); 677d06c82f1SRandall Stewart return (NULL); 678d06c82f1SRandall Stewart } 679d06c82f1SRandall Stewart SCTP_INCR_LADDR_COUNT(); 680d06c82f1SRandall Stewart bzero(wi, sizeof(*wi)); 681d61a0ae0SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); 682d06c82f1SRandall Stewart wi->ifa = sctp_ifap; 683d06c82f1SRandall Stewart wi->action = SCTP_ADD_IP_ADDRESS; 684d06c82f1SRandall Stewart SCTP_IPI_ITERATOR_WQ_LOCK(); 685d06c82f1SRandall Stewart /* 686d06c82f1SRandall Stewart * Should this really be a tailq? As it is we will process 687d06c82f1SRandall Stewart * the newest first :-0 688d06c82f1SRandall Stewart */ 689d06c82f1SRandall Stewart LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr); 690207304d4SRandall Stewart SCTP_IPI_ITERATOR_WQ_UNLOCK(); 691d06c82f1SRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, 692d06c82f1SRandall Stewart (struct sctp_inpcb *)NULL, 693d06c82f1SRandall Stewart (struct sctp_tcb *)NULL, 694d06c82f1SRandall Stewart (struct sctp_nets *)NULL); 69517205eccSRandall Stewart } else { 69617205eccSRandall Stewart /* it's ready for use */ 69717205eccSRandall Stewart sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE; 698d06c82f1SRandall Stewart } 69942551e99SRandall Stewart return (sctp_ifap); 70042551e99SRandall Stewart } 70142551e99SRandall Stewart 702d06c82f1SRandall Stewart void 70317205eccSRandall Stewart sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr, 704851b7298SRandall Stewart uint32_t ifn_index, const char *if_name) 70542551e99SRandall Stewart { 70642551e99SRandall Stewart struct sctp_vrf *vrf; 70742551e99SRandall Stewart struct sctp_ifa *sctp_ifap = NULL; 70842551e99SRandall Stewart 709c99efcf6SRandall Stewart SCTP_IPI_ADDR_WLOCK(); 71017205eccSRandall Stewart vrf = sctp_find_vrf(vrf_id); 71142551e99SRandall Stewart if (vrf == NULL) { 712baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); 71342551e99SRandall Stewart goto out_now; 71442551e99SRandall Stewart } 715baf3da66SRandall Stewart #ifdef SCTP_DEBUG 716baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id); 717baf3da66SRandall Stewart SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); 718baf3da66SRandall Stewart #endif 719851b7298SRandall Stewart sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); 72042551e99SRandall Stewart if (sctp_ifap) { 721851b7298SRandall Stewart /* Validate the delete */ 722851b7298SRandall Stewart if (sctp_ifap->ifn_p) { 723851b7298SRandall Stewart int valid = 0; 724851b7298SRandall Stewart 725851b7298SRandall Stewart /*- 726851b7298SRandall Stewart * The name has priority over the ifn_index 727851b7298SRandall Stewart * if its given. We do this especially for 728851b7298SRandall Stewart * panda who might recycle indexes fast. 729851b7298SRandall Stewart */ 730851b7298SRandall Stewart if (if_name) { 731851b7298SRandall Stewart int len1, len2; 732851b7298SRandall Stewart 733851b7298SRandall Stewart len1 = min(SCTP_IFNAMSIZ, strlen(if_name)); 734851b7298SRandall Stewart len2 = min(SCTP_IFNAMSIZ, strlen(sctp_ifap->ifn_p->ifn_name)); 735851b7298SRandall Stewart if (len1 && len2 && (len1 == len2)) { 736851b7298SRandall Stewart /* we can compare them */ 737851b7298SRandall Stewart if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) == 0) { 738851b7298SRandall Stewart /* 739851b7298SRandall Stewart * They match its a correct 740851b7298SRandall Stewart * delete 741851b7298SRandall Stewart */ 742851b7298SRandall Stewart valid = 1; 743851b7298SRandall Stewart } 744851b7298SRandall Stewart } 745851b7298SRandall Stewart } 746851b7298SRandall Stewart if (!valid) { 747851b7298SRandall Stewart /* last ditch check ifn_index */ 748851b7298SRandall Stewart if (ifn_index == sctp_ifap->ifn_p->ifn_index) { 749851b7298SRandall Stewart valid = 1; 750851b7298SRandall Stewart } 751851b7298SRandall Stewart } 752851b7298SRandall Stewart if (!valid) { 753baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n", 754851b7298SRandall Stewart ifn_index, ((if_name == NULL) ? "NULL" : if_name)); 755baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n", 756851b7298SRandall Stewart sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name); 757c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 758851b7298SRandall Stewart return; 759851b7298SRandall Stewart } 760851b7298SRandall Stewart } 761baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", sctp_ifap); 76242551e99SRandall Stewart sctp_ifap->localifa_flags &= SCTP_ADDR_VALID; 76342551e99SRandall Stewart sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; 76442551e99SRandall Stewart vrf->total_ifa_count--; 7656a27c376SRandall Stewart LIST_REMOVE(sctp_ifap, next_bucket); 766baf3da66SRandall Stewart sctp_remove_ifa_from_ifn(sctp_ifap); 76717205eccSRandall Stewart } 76817205eccSRandall Stewart #ifdef SCTP_DEBUG 76917205eccSRandall Stewart else { 770baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:", 77142551e99SRandall Stewart ifn_index); 772ad81507eSRandall Stewart SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); 77342551e99SRandall Stewart } 77417205eccSRandall Stewart #endif 775ad81507eSRandall Stewart 77642551e99SRandall Stewart out_now: 777c99efcf6SRandall Stewart SCTP_IPI_ADDR_WUNLOCK(); 778d06c82f1SRandall Stewart if (sctp_ifap) { 779d06c82f1SRandall Stewart struct sctp_laddr *wi; 780d06c82f1SRandall Stewart 781d06c82f1SRandall Stewart wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr); 782d06c82f1SRandall Stewart if (wi == NULL) { 783d06c82f1SRandall Stewart /* 784d06c82f1SRandall Stewart * Gak, what can we do? We have lost an address 785d06c82f1SRandall Stewart * change can you say HOSED? 786d06c82f1SRandall Stewart */ 787baf3da66SRandall Stewart SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); 788d06c82f1SRandall Stewart 789c99efcf6SRandall Stewart /* Oops, must decrement the count */ 790d06c82f1SRandall Stewart sctp_free_ifa(sctp_ifap); 791d06c82f1SRandall Stewart return; 792d06c82f1SRandall Stewart } 793d06c82f1SRandall Stewart SCTP_INCR_LADDR_COUNT(); 794d06c82f1SRandall Stewart bzero(wi, sizeof(*wi)); 795d61a0ae0SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); 796d06c82f1SRandall Stewart wi->ifa = sctp_ifap; 797d06c82f1SRandall Stewart wi->action = SCTP_DEL_IP_ADDRESS; 798d06c82f1SRandall Stewart SCTP_IPI_ITERATOR_WQ_LOCK(); 799d06c82f1SRandall Stewart /* 800d06c82f1SRandall Stewart * Should this really be a tailq? As it is we will process 801d06c82f1SRandall Stewart * the newest first :-0 802d06c82f1SRandall Stewart */ 803d06c82f1SRandall Stewart LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr); 804207304d4SRandall Stewart SCTP_IPI_ITERATOR_WQ_UNLOCK(); 805207304d4SRandall Stewart 806d06c82f1SRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, 807d06c82f1SRandall Stewart (struct sctp_inpcb *)NULL, 808d06c82f1SRandall Stewart (struct sctp_tcb *)NULL, 809d06c82f1SRandall Stewart (struct sctp_nets *)NULL); 810d06c82f1SRandall Stewart } 811d06c82f1SRandall Stewart return; 81242551e99SRandall Stewart } 813f8829a4aSRandall Stewart 814f8829a4aSRandall Stewart 815f8829a4aSRandall Stewart static struct sctp_tcb * 816f8829a4aSRandall Stewart sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from, 817bff64a4dSRandall Stewart struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id) 818f8829a4aSRandall Stewart { 819baf3da66SRandall Stewart /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */ 820f8829a4aSRandall Stewart /* 821f8829a4aSRandall Stewart * If we support the TCP model, then we must now dig through to see 822f8829a4aSRandall Stewart * if we can find our endpoint in the list of tcp ep's. 823f8829a4aSRandall Stewart */ 824f8829a4aSRandall Stewart uint16_t lport, rport; 825f8829a4aSRandall Stewart struct sctppcbhead *ephead; 826f8829a4aSRandall Stewart struct sctp_inpcb *inp; 827f8829a4aSRandall Stewart struct sctp_laddr *laddr; 828f8829a4aSRandall Stewart struct sctp_tcb *stcb; 829f8829a4aSRandall Stewart struct sctp_nets *net; 830f8829a4aSRandall Stewart 831f8829a4aSRandall Stewart if ((to == NULL) || (from == NULL)) { 832f8829a4aSRandall Stewart return (NULL); 833f8829a4aSRandall Stewart } 834f8829a4aSRandall Stewart if (to->sa_family == AF_INET && from->sa_family == AF_INET) { 835f8829a4aSRandall Stewart lport = ((struct sockaddr_in *)to)->sin_port; 836f8829a4aSRandall Stewart rport = ((struct sockaddr_in *)from)->sin_port; 837f8829a4aSRandall Stewart } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) { 838f8829a4aSRandall Stewart lport = ((struct sockaddr_in6 *)to)->sin6_port; 839f8829a4aSRandall Stewart rport = ((struct sockaddr_in6 *)from)->sin6_port; 840f8829a4aSRandall Stewart } else { 841f8829a4aSRandall Stewart return NULL; 842f8829a4aSRandall Stewart } 843f8829a4aSRandall Stewart ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR( 844f8829a4aSRandall Stewart (lport + rport), sctppcbinfo.hashtcpmark)]; 845f8829a4aSRandall Stewart /* 846f8829a4aSRandall Stewart * Ok now for each of the guys in this bucket we must look and see: 847f8829a4aSRandall Stewart * - Does the remote port match. - Does there single association's 848f8829a4aSRandall Stewart * addresses match this address (to). If so we update p_ep to point 849f8829a4aSRandall Stewart * to this ep and return the tcb from it. 850f8829a4aSRandall Stewart */ 851f8829a4aSRandall Stewart LIST_FOREACH(inp, ephead, sctp_hash) { 852f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp); 853f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 854f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 855f8829a4aSRandall Stewart continue; 856f8829a4aSRandall Stewart } 857d06c82f1SRandall Stewart if (lport != inp->sctp_lport) { 8581bb552e8SRandall Stewart SCTP_INP_RUNLOCK(inp); 859d06c82f1SRandall Stewart continue; 860d06c82f1SRandall Stewart } 861c105859eSRandall Stewart if (inp->def_vrf_id != vrf_id) { 862bff64a4dSRandall Stewart SCTP_INP_RUNLOCK(inp); 863bff64a4dSRandall Stewart continue; 864bff64a4dSRandall Stewart } 865f8829a4aSRandall Stewart /* check to see if the ep has one of the addresses */ 866f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { 867f8829a4aSRandall Stewart /* We are NOT bound all, so look further */ 868f8829a4aSRandall Stewart int match = 0; 869f8829a4aSRandall Stewart 870f8829a4aSRandall Stewart LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 871f8829a4aSRandall Stewart 872f8829a4aSRandall Stewart if (laddr->ifa == NULL) { 873ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__); 874f8829a4aSRandall Stewart continue; 875f8829a4aSRandall Stewart } 87642551e99SRandall Stewart if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 877ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); 878f8829a4aSRandall Stewart continue; 879f8829a4aSRandall Stewart } 88042551e99SRandall Stewart if (laddr->ifa->address.sa.sa_family == 881f8829a4aSRandall Stewart to->sa_family) { 882f8829a4aSRandall Stewart /* see if it matches */ 883f8829a4aSRandall Stewart struct sockaddr_in *intf_addr, *sin; 884f8829a4aSRandall Stewart 88542551e99SRandall Stewart intf_addr = &laddr->ifa->address.sin; 886f8829a4aSRandall Stewart sin = (struct sockaddr_in *)to; 887f8829a4aSRandall Stewart if (from->sa_family == AF_INET) { 888f8829a4aSRandall Stewart if (sin->sin_addr.s_addr == 889f8829a4aSRandall Stewart intf_addr->sin_addr.s_addr) { 890f8829a4aSRandall Stewart match = 1; 891f8829a4aSRandall Stewart break; 892f8829a4aSRandall Stewart } 893f8829a4aSRandall Stewart } else { 894f8829a4aSRandall Stewart struct sockaddr_in6 *intf_addr6; 895f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 896f8829a4aSRandall Stewart 897f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *) 898f8829a4aSRandall Stewart to; 89942551e99SRandall Stewart intf_addr6 = &laddr->ifa->address.sin6; 900f8829a4aSRandall Stewart 901f8829a4aSRandall Stewart if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 902f8829a4aSRandall Stewart &intf_addr6->sin6_addr)) { 903f8829a4aSRandall Stewart match = 1; 904f8829a4aSRandall Stewart break; 905f8829a4aSRandall Stewart } 906f8829a4aSRandall Stewart } 907f8829a4aSRandall Stewart } 908f8829a4aSRandall Stewart } 909f8829a4aSRandall Stewart if (match == 0) { 910f8829a4aSRandall Stewart /* This endpoint does not have this address */ 911f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 912f8829a4aSRandall Stewart continue; 913f8829a4aSRandall Stewart } 914f8829a4aSRandall Stewart } 915f8829a4aSRandall Stewart /* 916f8829a4aSRandall Stewart * Ok if we hit here the ep has the address, does it hold 917f8829a4aSRandall Stewart * the tcb? 918f8829a4aSRandall Stewart */ 919f8829a4aSRandall Stewart 920f8829a4aSRandall Stewart stcb = LIST_FIRST(&inp->sctp_asoc_list); 921f8829a4aSRandall Stewart if (stcb == NULL) { 922f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 923f8829a4aSRandall Stewart continue; 924f8829a4aSRandall Stewart } 925f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 926f8829a4aSRandall Stewart if (stcb->rport != rport) { 927f8829a4aSRandall Stewart /* remote port does not match. */ 928f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 929f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 930f8829a4aSRandall Stewart continue; 931f8829a4aSRandall Stewart } 932f8829a4aSRandall Stewart /* Does this TCB have a matching address? */ 933f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 934f8829a4aSRandall Stewart 935f8829a4aSRandall Stewart if (net->ro._l_addr.sa.sa_family != from->sa_family) { 936f8829a4aSRandall Stewart /* not the same family, can't be a match */ 937f8829a4aSRandall Stewart continue; 938f8829a4aSRandall Stewart } 939f8829a4aSRandall Stewart if (from->sa_family == AF_INET) { 940f8829a4aSRandall Stewart struct sockaddr_in *sin, *rsin; 941f8829a4aSRandall Stewart 942f8829a4aSRandall Stewart sin = (struct sockaddr_in *)&net->ro._l_addr; 943f8829a4aSRandall Stewart rsin = (struct sockaddr_in *)from; 944f8829a4aSRandall Stewart if (sin->sin_addr.s_addr == 945f8829a4aSRandall Stewart rsin->sin_addr.s_addr) { 946f8829a4aSRandall Stewart /* found it */ 947f8829a4aSRandall Stewart if (netp != NULL) { 948f8829a4aSRandall Stewart *netp = net; 949f8829a4aSRandall Stewart } 950f8829a4aSRandall Stewart /* Update the endpoint pointer */ 951f8829a4aSRandall Stewart *inp_p = inp; 952f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 953f8829a4aSRandall Stewart return (stcb); 954f8829a4aSRandall Stewart } 955f8829a4aSRandall Stewart } else { 956f8829a4aSRandall Stewart struct sockaddr_in6 *sin6, *rsin6; 957f8829a4aSRandall Stewart 958f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 959f8829a4aSRandall Stewart rsin6 = (struct sockaddr_in6 *)from; 960f8829a4aSRandall Stewart if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 961f8829a4aSRandall Stewart &rsin6->sin6_addr)) { 962f8829a4aSRandall Stewart /* found it */ 963f8829a4aSRandall Stewart if (netp != NULL) { 964f8829a4aSRandall Stewart *netp = net; 965f8829a4aSRandall Stewart } 966f8829a4aSRandall Stewart /* Update the endpoint pointer */ 967f8829a4aSRandall Stewart *inp_p = inp; 968f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 969f8829a4aSRandall Stewart return (stcb); 970f8829a4aSRandall Stewart } 971f8829a4aSRandall Stewart } 972f8829a4aSRandall Stewart } 973f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 974f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 975f8829a4aSRandall Stewart } 976f8829a4aSRandall Stewart return (NULL); 977f8829a4aSRandall Stewart } 978f8829a4aSRandall Stewart 979f8829a4aSRandall Stewart /* 980f8829a4aSRandall Stewart * rules for use 981f8829a4aSRandall Stewart * 982f8829a4aSRandall Stewart * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an 983f8829a4aSRandall Stewart * stcb, both will be locked (locked_tcb and stcb) but decrement will be done 984f8829a4aSRandall Stewart * (if locked == NULL). 3) Decrement happens on return ONLY if locked == 985f8829a4aSRandall Stewart * NULL. 986f8829a4aSRandall Stewart */ 987f8829a4aSRandall Stewart 988f8829a4aSRandall Stewart struct sctp_tcb * 989f8829a4aSRandall Stewart sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote, 990f8829a4aSRandall Stewart struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb) 991f8829a4aSRandall Stewart { 992f8829a4aSRandall Stewart struct sctpasochead *head; 993f8829a4aSRandall Stewart struct sctp_inpcb *inp; 994bff64a4dSRandall Stewart struct sctp_tcb *stcb = NULL; 995f8829a4aSRandall Stewart struct sctp_nets *net; 996f8829a4aSRandall Stewart uint16_t rport; 997f8829a4aSRandall Stewart 998f8829a4aSRandall Stewart inp = *inp_p; 999f8829a4aSRandall Stewart if (remote->sa_family == AF_INET) { 1000f8829a4aSRandall Stewart rport = (((struct sockaddr_in *)remote)->sin_port); 1001f8829a4aSRandall Stewart } else if (remote->sa_family == AF_INET6) { 1002f8829a4aSRandall Stewart rport = (((struct sockaddr_in6 *)remote)->sin6_port); 1003f8829a4aSRandall Stewart } else { 1004f8829a4aSRandall Stewart return (NULL); 1005f8829a4aSRandall Stewart } 1006f8829a4aSRandall Stewart if (locked_tcb) { 1007f8829a4aSRandall Stewart /* 1008f8829a4aSRandall Stewart * UN-lock so we can do proper locking here this occurs when 1009f8829a4aSRandall Stewart * called from load_addresses_from_init. 1010f8829a4aSRandall Stewart */ 1011c4739e2fSRandall Stewart atomic_add_int(&locked_tcb->asoc.refcnt, 1); 1012f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(locked_tcb); 1013f8829a4aSRandall Stewart } 1014f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 1015f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1016bff64a4dSRandall Stewart /*- 1017f8829a4aSRandall Stewart * Now either this guy is our listener or it's the 1018f8829a4aSRandall Stewart * connector. If it is the one that issued the connect, then 1019f8829a4aSRandall Stewart * it's only chance is to be the first TCB in the list. If 1020f8829a4aSRandall Stewart * it is the acceptor, then do the special_lookup to hash 1021f8829a4aSRandall Stewart * and find the real inp. 1022f8829a4aSRandall Stewart */ 1023f8829a4aSRandall Stewart if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) { 1024f8829a4aSRandall Stewart /* to is peer addr, from is my addr */ 1025f8829a4aSRandall Stewart stcb = sctp_tcb_special_locate(inp_p, remote, local, 1026bff64a4dSRandall Stewart netp, inp->def_vrf_id); 1027f8829a4aSRandall Stewart if ((stcb != NULL) && (locked_tcb == NULL)) { 1028f8829a4aSRandall Stewart /* we have a locked tcb, lower refcount */ 1029f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1030f8829a4aSRandall Stewart } 1031f8829a4aSRandall Stewart if ((locked_tcb != NULL) && (locked_tcb != stcb)) { 1032f8829a4aSRandall Stewart SCTP_INP_RLOCK(locked_tcb->sctp_ep); 1033f8829a4aSRandall Stewart SCTP_TCB_LOCK(locked_tcb); 1034c4739e2fSRandall Stewart atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1035f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); 1036f8829a4aSRandall Stewart } 1037f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1038f8829a4aSRandall Stewart return (stcb); 1039f8829a4aSRandall Stewart } else { 1040f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 1041f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1042f8829a4aSRandall Stewart goto null_return; 1043f8829a4aSRandall Stewart } 1044f8829a4aSRandall Stewart stcb = LIST_FIRST(&inp->sctp_asoc_list); 1045f8829a4aSRandall Stewart if (stcb == NULL) { 1046f8829a4aSRandall Stewart goto null_return; 1047f8829a4aSRandall Stewart } 1048f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 1049f8829a4aSRandall Stewart if (stcb->rport != rport) { 1050f8829a4aSRandall Stewart /* remote port does not match. */ 1051f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1052f8829a4aSRandall Stewart goto null_return; 1053f8829a4aSRandall Stewart } 1054f8829a4aSRandall Stewart /* now look at the list of remote addresses */ 1055f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1056a5d547adSRandall Stewart #ifdef INVARIANTS 1057f8829a4aSRandall Stewart if (net == (TAILQ_NEXT(net, sctp_next))) { 1058f8829a4aSRandall Stewart panic("Corrupt net list"); 1059f8829a4aSRandall Stewart } 1060f8829a4aSRandall Stewart #endif 1061f8829a4aSRandall Stewart if (net->ro._l_addr.sa.sa_family != 1062f8829a4aSRandall Stewart remote->sa_family) { 1063f8829a4aSRandall Stewart /* not the same family */ 1064f8829a4aSRandall Stewart continue; 1065f8829a4aSRandall Stewart } 1066f8829a4aSRandall Stewart if (remote->sa_family == AF_INET) { 1067f8829a4aSRandall Stewart struct sockaddr_in *sin, *rsin; 1068f8829a4aSRandall Stewart 1069f8829a4aSRandall Stewart sin = (struct sockaddr_in *) 1070f8829a4aSRandall Stewart &net->ro._l_addr; 1071f8829a4aSRandall Stewart rsin = (struct sockaddr_in *)remote; 1072f8829a4aSRandall Stewart if (sin->sin_addr.s_addr == 1073f8829a4aSRandall Stewart rsin->sin_addr.s_addr) { 1074f8829a4aSRandall Stewart /* found it */ 1075f8829a4aSRandall Stewart if (netp != NULL) { 1076f8829a4aSRandall Stewart *netp = net; 1077f8829a4aSRandall Stewart } 1078f8829a4aSRandall Stewart if (locked_tcb == NULL) { 1079f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1080f8829a4aSRandall Stewart } else if (locked_tcb != stcb) { 1081f8829a4aSRandall Stewart SCTP_TCB_LOCK(locked_tcb); 1082f8829a4aSRandall Stewart } 1083c4739e2fSRandall Stewart if (locked_tcb) { 1084c4739e2fSRandall Stewart atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1085c4739e2fSRandall Stewart } 1086f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 1087f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1088f8829a4aSRandall Stewart return (stcb); 1089f8829a4aSRandall Stewart } 1090f8829a4aSRandall Stewart } else if (remote->sa_family == AF_INET6) { 1091f8829a4aSRandall Stewart struct sockaddr_in6 *sin6, *rsin6; 1092f8829a4aSRandall Stewart 1093f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1094f8829a4aSRandall Stewart rsin6 = (struct sockaddr_in6 *)remote; 1095f8829a4aSRandall Stewart if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 1096f8829a4aSRandall Stewart &rsin6->sin6_addr)) { 1097f8829a4aSRandall Stewart /* found it */ 1098f8829a4aSRandall Stewart if (netp != NULL) { 1099f8829a4aSRandall Stewart *netp = net; 1100f8829a4aSRandall Stewart } 1101f8829a4aSRandall Stewart if (locked_tcb == NULL) { 1102f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1103f8829a4aSRandall Stewart } else if (locked_tcb != stcb) { 1104f8829a4aSRandall Stewart SCTP_TCB_LOCK(locked_tcb); 1105f8829a4aSRandall Stewart } 1106c4739e2fSRandall Stewart if (locked_tcb) { 1107c4739e2fSRandall Stewart atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1108c4739e2fSRandall Stewart } 1109f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 1110f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1111f8829a4aSRandall Stewart return (stcb); 1112f8829a4aSRandall Stewart } 1113f8829a4aSRandall Stewart } 1114f8829a4aSRandall Stewart } 1115f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1116f8829a4aSRandall Stewart } 1117f8829a4aSRandall Stewart } else { 1118f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 1119f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1120f8829a4aSRandall Stewart goto null_return; 1121f8829a4aSRandall Stewart } 1122f8829a4aSRandall Stewart head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport, 1123f8829a4aSRandall Stewart inp->sctp_hashmark)]; 1124f8829a4aSRandall Stewart if (head == NULL) { 1125f8829a4aSRandall Stewart goto null_return; 1126f8829a4aSRandall Stewart } 1127f8829a4aSRandall Stewart LIST_FOREACH(stcb, head, sctp_tcbhash) { 1128f8829a4aSRandall Stewart if (stcb->rport != rport) { 1129f8829a4aSRandall Stewart /* remote port does not match */ 1130f8829a4aSRandall Stewart continue; 1131f8829a4aSRandall Stewart } 1132f8829a4aSRandall Stewart /* now look at the list of remote addresses */ 1133f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 1134f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1135a5d547adSRandall Stewart #ifdef INVARIANTS 1136f8829a4aSRandall Stewart if (net == (TAILQ_NEXT(net, sctp_next))) { 1137f8829a4aSRandall Stewart panic("Corrupt net list"); 1138f8829a4aSRandall Stewart } 1139f8829a4aSRandall Stewart #endif 1140f8829a4aSRandall Stewart if (net->ro._l_addr.sa.sa_family != 1141f8829a4aSRandall Stewart remote->sa_family) { 1142f8829a4aSRandall Stewart /* not the same family */ 1143f8829a4aSRandall Stewart continue; 1144f8829a4aSRandall Stewart } 1145f8829a4aSRandall Stewart if (remote->sa_family == AF_INET) { 1146f8829a4aSRandall Stewart struct sockaddr_in *sin, *rsin; 1147f8829a4aSRandall Stewart 1148f8829a4aSRandall Stewart sin = (struct sockaddr_in *) 1149f8829a4aSRandall Stewart &net->ro._l_addr; 1150f8829a4aSRandall Stewart rsin = (struct sockaddr_in *)remote; 1151f8829a4aSRandall Stewart if (sin->sin_addr.s_addr == 1152f8829a4aSRandall Stewart rsin->sin_addr.s_addr) { 1153f8829a4aSRandall Stewart /* found it */ 1154f8829a4aSRandall Stewart if (netp != NULL) { 1155f8829a4aSRandall Stewart *netp = net; 1156f8829a4aSRandall Stewart } 1157f8829a4aSRandall Stewart if (locked_tcb == NULL) { 1158f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1159f8829a4aSRandall Stewart } else if (locked_tcb != stcb) { 1160f8829a4aSRandall Stewart SCTP_TCB_LOCK(locked_tcb); 1161f8829a4aSRandall Stewart } 1162c4739e2fSRandall Stewart if (locked_tcb) { 1163c4739e2fSRandall Stewart atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1164c4739e2fSRandall Stewart } 1165f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 1166f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1167f8829a4aSRandall Stewart return (stcb); 1168f8829a4aSRandall Stewart } 1169f8829a4aSRandall Stewart } else if (remote->sa_family == AF_INET6) { 1170f8829a4aSRandall Stewart struct sockaddr_in6 *sin6, *rsin6; 1171f8829a4aSRandall Stewart 1172f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *) 1173f8829a4aSRandall Stewart &net->ro._l_addr; 1174f8829a4aSRandall Stewart rsin6 = (struct sockaddr_in6 *)remote; 1175f8829a4aSRandall Stewart if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 1176f8829a4aSRandall Stewart &rsin6->sin6_addr)) { 1177f8829a4aSRandall Stewart /* found it */ 1178f8829a4aSRandall Stewart if (netp != NULL) { 1179f8829a4aSRandall Stewart *netp = net; 1180f8829a4aSRandall Stewart } 1181f8829a4aSRandall Stewart if (locked_tcb == NULL) { 1182f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1183f8829a4aSRandall Stewart } else if (locked_tcb != stcb) { 1184f8829a4aSRandall Stewart SCTP_TCB_LOCK(locked_tcb); 1185f8829a4aSRandall Stewart } 1186c4739e2fSRandall Stewart if (locked_tcb) { 1187c4739e2fSRandall Stewart atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1188c4739e2fSRandall Stewart } 1189f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 1190f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1191f8829a4aSRandall Stewart return (stcb); 1192f8829a4aSRandall Stewart } 1193f8829a4aSRandall Stewart } 1194f8829a4aSRandall Stewart } 1195f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1196f8829a4aSRandall Stewart } 1197f8829a4aSRandall Stewart } 1198f8829a4aSRandall Stewart null_return: 1199f8829a4aSRandall Stewart /* clean up for returning null */ 1200f8829a4aSRandall Stewart if (locked_tcb) { 1201f8829a4aSRandall Stewart SCTP_TCB_LOCK(locked_tcb); 1202c4739e2fSRandall Stewart atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); 1203f8829a4aSRandall Stewart } 1204f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 1205f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1206f8829a4aSRandall Stewart /* not found */ 1207f8829a4aSRandall Stewart return (NULL); 1208f8829a4aSRandall Stewart } 1209f8829a4aSRandall Stewart 1210f8829a4aSRandall Stewart /* 1211f8829a4aSRandall Stewart * Find an association for a specific endpoint using the association id given 1212f8829a4aSRandall Stewart * out in the COMM_UP notification 1213f8829a4aSRandall Stewart */ 1214f8829a4aSRandall Stewart 1215f8829a4aSRandall Stewart struct sctp_tcb * 1216f8829a4aSRandall Stewart sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) 1217f8829a4aSRandall Stewart { 1218f8829a4aSRandall Stewart /* 1219f8829a4aSRandall Stewart * Use my the assoc_id to find a endpoint 1220f8829a4aSRandall Stewart */ 1221f8829a4aSRandall Stewart struct sctpasochead *head; 1222f8829a4aSRandall Stewart struct sctp_tcb *stcb; 1223f8829a4aSRandall Stewart uint32_t id; 1224f8829a4aSRandall Stewart 1225f8829a4aSRandall Stewart if (asoc_id == 0 || inp == NULL) { 1226f8829a4aSRandall Stewart return (NULL); 1227f8829a4aSRandall Stewart } 1228f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 1229f8829a4aSRandall Stewart id = (uint32_t) asoc_id; 1230f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id, 1231f8829a4aSRandall Stewart sctppcbinfo.hashasocmark)]; 1232f8829a4aSRandall Stewart if (head == NULL) { 1233f8829a4aSRandall Stewart /* invalid id TSNH */ 1234f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1235f8829a4aSRandall Stewart return (NULL); 1236f8829a4aSRandall Stewart } 1237f8829a4aSRandall Stewart LIST_FOREACH(stcb, head, sctp_asocs) { 1238f8829a4aSRandall Stewart SCTP_INP_RLOCK(stcb->sctp_ep); 1239f8829a4aSRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1240f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1241f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1242f8829a4aSRandall Stewart return (NULL); 1243f8829a4aSRandall Stewart } 1244f8829a4aSRandall Stewart if (stcb->asoc.assoc_id == id) { 1245f8829a4aSRandall Stewart /* candidate */ 1246f8829a4aSRandall Stewart if (inp != stcb->sctp_ep) { 1247f8829a4aSRandall Stewart /* 1248f8829a4aSRandall Stewart * some other guy has the same id active (id 1249f8829a4aSRandall Stewart * collision ??). 1250f8829a4aSRandall Stewart */ 1251f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1252f8829a4aSRandall Stewart continue; 1253f8829a4aSRandall Stewart } 1254f8829a4aSRandall Stewart if (want_lock) { 1255f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 1256f8829a4aSRandall Stewart } 1257f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1258f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1259f8829a4aSRandall Stewart return (stcb); 1260f8829a4aSRandall Stewart } 1261f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1262f8829a4aSRandall Stewart } 1263f8829a4aSRandall Stewart /* Ok if we missed here, lets try the restart hash */ 1264a5d547adSRandall Stewart head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)]; 1265f8829a4aSRandall Stewart if (head == NULL) { 1266f8829a4aSRandall Stewart /* invalid id TSNH */ 1267f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1268f8829a4aSRandall Stewart return (NULL); 1269f8829a4aSRandall Stewart } 1270f8829a4aSRandall Stewart LIST_FOREACH(stcb, head, sctp_tcbrestarhash) { 1271f8829a4aSRandall Stewart SCTP_INP_RLOCK(stcb->sctp_ep); 1272f8829a4aSRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1273f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1274d61a0ae0SRandall Stewart continue; 1275f8829a4aSRandall Stewart } 1276d61a0ae0SRandall Stewart if (want_lock) { 1277f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 1278d61a0ae0SRandall Stewart } 1279f8829a4aSRandall Stewart if (stcb->asoc.assoc_id == id) { 1280f8829a4aSRandall Stewart /* candidate */ 1281d61a0ae0SRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1282f8829a4aSRandall Stewart if (inp != stcb->sctp_ep) { 1283f8829a4aSRandall Stewart /* 1284f8829a4aSRandall Stewart * some other guy has the same id active (id 1285f8829a4aSRandall Stewart * collision ??). 1286f8829a4aSRandall Stewart */ 1287d61a0ae0SRandall Stewart if (want_lock) { 1288f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1289d61a0ae0SRandall Stewart } 1290f8829a4aSRandall Stewart continue; 1291f8829a4aSRandall Stewart } 1292f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1293f8829a4aSRandall Stewart return (stcb); 1294d61a0ae0SRandall Stewart } else { 1295d61a0ae0SRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1296f8829a4aSRandall Stewart } 1297d61a0ae0SRandall Stewart if (want_lock) { 1298f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1299f8829a4aSRandall Stewart } 1300d61a0ae0SRandall Stewart } 1301f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1302f8829a4aSRandall Stewart return (NULL); 1303f8829a4aSRandall Stewart } 1304f8829a4aSRandall Stewart 1305f8829a4aSRandall Stewart 1306f8829a4aSRandall Stewart static struct sctp_inpcb * 1307f8829a4aSRandall Stewart sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head, 130842551e99SRandall Stewart uint16_t lport, uint32_t vrf_id) 1309f8829a4aSRandall Stewart { 1310f8829a4aSRandall Stewart struct sctp_inpcb *inp; 1311f8829a4aSRandall Stewart struct sockaddr_in *sin; 1312f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 1313f8829a4aSRandall Stewart struct sctp_laddr *laddr; 131442551e99SRandall Stewart int fnd; 1315f8829a4aSRandall Stewart 1316f8829a4aSRandall Stewart /* 1317f8829a4aSRandall Stewart * Endpoing probe expects that the INP_INFO is locked. 1318f8829a4aSRandall Stewart */ 1319f8829a4aSRandall Stewart if (nam->sa_family == AF_INET) { 1320f8829a4aSRandall Stewart sin = (struct sockaddr_in *)nam; 1321f8829a4aSRandall Stewart sin6 = NULL; 1322f8829a4aSRandall Stewart } else if (nam->sa_family == AF_INET6) { 1323f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)nam; 1324f8829a4aSRandall Stewart sin = NULL; 1325f8829a4aSRandall Stewart } else { 1326f8829a4aSRandall Stewart /* unsupported family */ 1327f8829a4aSRandall Stewart return (NULL); 1328f8829a4aSRandall Stewart } 1329f8829a4aSRandall Stewart if (head == NULL) 1330f8829a4aSRandall Stewart return (NULL); 1331f8829a4aSRandall Stewart LIST_FOREACH(inp, head, sctp_hash) { 1332f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp); 1333f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1334f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1335f8829a4aSRandall Stewart continue; 1336f8829a4aSRandall Stewart } 1337f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) && 1338f8829a4aSRandall Stewart (inp->sctp_lport == lport)) { 1339f8829a4aSRandall Stewart /* got it */ 1340f8829a4aSRandall Stewart if ((nam->sa_family == AF_INET) && 1341f8829a4aSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 134244b7479bSRandall Stewart SCTP_IPV6_V6ONLY(inp)) { 1343f8829a4aSRandall Stewart /* IPv4 on a IPv6 socket with ONLY IPv6 set */ 1344f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1345f8829a4aSRandall Stewart continue; 1346f8829a4aSRandall Stewart } 1347f8829a4aSRandall Stewart /* A V6 address and the endpoint is NOT bound V6 */ 1348f8829a4aSRandall Stewart if (nam->sa_family == AF_INET6 && 1349f8829a4aSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { 1350f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1351f8829a4aSRandall Stewart continue; 1352f8829a4aSRandall Stewart } 135342551e99SRandall Stewart /* does a VRF id match? */ 135442551e99SRandall Stewart fnd = 0; 135542551e99SRandall Stewart if (inp->def_vrf_id == vrf_id) 135642551e99SRandall Stewart fnd = 1; 135742551e99SRandall Stewart 1358f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 135942551e99SRandall Stewart if (!fnd) 136042551e99SRandall Stewart continue; 1361f8829a4aSRandall Stewart return (inp); 1362f8829a4aSRandall Stewart } 1363f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1364f8829a4aSRandall Stewart } 1365f8829a4aSRandall Stewart 1366f8829a4aSRandall Stewart if ((nam->sa_family == AF_INET) && 1367f8829a4aSRandall Stewart (sin->sin_addr.s_addr == INADDR_ANY)) { 1368f8829a4aSRandall Stewart /* Can't hunt for one that has no address specified */ 1369f8829a4aSRandall Stewart return (NULL); 1370f8829a4aSRandall Stewart } else if ((nam->sa_family == AF_INET6) && 1371f8829a4aSRandall Stewart (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) { 1372f8829a4aSRandall Stewart /* Can't hunt for one that has no address specified */ 1373f8829a4aSRandall Stewart return (NULL); 1374f8829a4aSRandall Stewart } 1375f8829a4aSRandall Stewart /* 1376f8829a4aSRandall Stewart * ok, not bound to all so see if we can find a EP bound to this 1377f8829a4aSRandall Stewart * address. 1378f8829a4aSRandall Stewart */ 1379f8829a4aSRandall Stewart LIST_FOREACH(inp, head, sctp_hash) { 1380f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp); 1381f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1382f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1383f8829a4aSRandall Stewart continue; 1384f8829a4aSRandall Stewart } 1385f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) { 1386f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1387f8829a4aSRandall Stewart continue; 1388f8829a4aSRandall Stewart } 1389f8829a4aSRandall Stewart /* 1390f8829a4aSRandall Stewart * Ok this could be a likely candidate, look at all of its 1391f8829a4aSRandall Stewart * addresses 1392f8829a4aSRandall Stewart */ 1393f8829a4aSRandall Stewart if (inp->sctp_lport != lport) { 1394f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1395f8829a4aSRandall Stewart continue; 1396f8829a4aSRandall Stewart } 139742551e99SRandall Stewart /* does a VRF id match? */ 139842551e99SRandall Stewart fnd = 0; 139942551e99SRandall Stewart if (inp->def_vrf_id == vrf_id) 140042551e99SRandall Stewart fnd = 1; 140142551e99SRandall Stewart 140242551e99SRandall Stewart if (!fnd) { 140342551e99SRandall Stewart SCTP_INP_RUNLOCK(inp); 140442551e99SRandall Stewart continue; 140542551e99SRandall Stewart } 1406f8829a4aSRandall Stewart LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1407f8829a4aSRandall Stewart if (laddr->ifa == NULL) { 1408ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", 1409ad81507eSRandall Stewart __FUNCTION__); 1410f8829a4aSRandall Stewart continue; 1411f8829a4aSRandall Stewart } 1412ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ", 1413f8829a4aSRandall Stewart laddr->ifa); 141442551e99SRandall Stewart if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 1415ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n"); 1416f8829a4aSRandall Stewart continue; 1417f8829a4aSRandall Stewart } 141842551e99SRandall Stewart if (laddr->ifa->address.sa.sa_family == nam->sa_family) { 1419f8829a4aSRandall Stewart /* possible, see if it matches */ 1420f8829a4aSRandall Stewart struct sockaddr_in *intf_addr; 1421f8829a4aSRandall Stewart 142242551e99SRandall Stewart intf_addr = &laddr->ifa->address.sin; 1423f8829a4aSRandall Stewart if (nam->sa_family == AF_INET) { 1424f8829a4aSRandall Stewart if (sin->sin_addr.s_addr == 1425f8829a4aSRandall Stewart intf_addr->sin_addr.s_addr) { 1426f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1427f8829a4aSRandall Stewart return (inp); 1428f8829a4aSRandall Stewart } 1429f8829a4aSRandall Stewart } else if (nam->sa_family == AF_INET6) { 1430f8829a4aSRandall Stewart struct sockaddr_in6 *intf_addr6; 1431f8829a4aSRandall Stewart 143242551e99SRandall Stewart intf_addr6 = &laddr->ifa->address.sin6; 1433f8829a4aSRandall Stewart if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 1434f8829a4aSRandall Stewart &intf_addr6->sin6_addr)) { 1435f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1436f8829a4aSRandall Stewart return (inp); 1437f8829a4aSRandall Stewart } 1438f8829a4aSRandall Stewart } 1439f8829a4aSRandall Stewart } 1440f8829a4aSRandall Stewart } 1441f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 1442f8829a4aSRandall Stewart } 1443f8829a4aSRandall Stewart return (NULL); 1444f8829a4aSRandall Stewart } 1445f8829a4aSRandall Stewart 1446f8829a4aSRandall Stewart struct sctp_inpcb * 1447ad81507eSRandall Stewart sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, 1448ad81507eSRandall Stewart uint32_t vrf_id) 1449f8829a4aSRandall Stewart { 1450f8829a4aSRandall Stewart /* 1451f8829a4aSRandall Stewart * First we check the hash table to see if someone has this port 1452f8829a4aSRandall Stewart * bound with just the port. 1453f8829a4aSRandall Stewart */ 1454f8829a4aSRandall Stewart struct sctp_inpcb *inp; 1455f8829a4aSRandall Stewart struct sctppcbhead *head; 1456f8829a4aSRandall Stewart struct sockaddr_in *sin; 1457f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 1458f8829a4aSRandall Stewart int lport; 1459f8829a4aSRandall Stewart 1460f8829a4aSRandall Stewart if (nam->sa_family == AF_INET) { 1461f8829a4aSRandall Stewart sin = (struct sockaddr_in *)nam; 1462f8829a4aSRandall Stewart lport = ((struct sockaddr_in *)nam)->sin_port; 1463f8829a4aSRandall Stewart } else if (nam->sa_family == AF_INET6) { 1464f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)nam; 1465f8829a4aSRandall Stewart lport = ((struct sockaddr_in6 *)nam)->sin6_port; 1466f8829a4aSRandall Stewart } else { 1467f8829a4aSRandall Stewart /* unsupported family */ 1468f8829a4aSRandall Stewart return (NULL); 1469f8829a4aSRandall Stewart } 1470f8829a4aSRandall Stewart /* 1471f8829a4aSRandall Stewart * I could cheat here and just cast to one of the types but we will 1472f8829a4aSRandall Stewart * do it right. It also provides the check against an Unsupported 1473f8829a4aSRandall Stewart * type too. 1474f8829a4aSRandall Stewart */ 1475f8829a4aSRandall Stewart /* Find the head of the ALLADDR chain */ 1476f8829a4aSRandall Stewart if (have_lock == 0) { 1477f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 1478f8829a4aSRandall Stewart } 1479f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport, 1480f8829a4aSRandall Stewart sctppcbinfo.hashmark)]; 148142551e99SRandall Stewart inp = sctp_endpoint_probe(nam, head, lport, vrf_id); 1482f8829a4aSRandall Stewart 1483f8829a4aSRandall Stewart /* 1484f8829a4aSRandall Stewart * If the TCP model exists it could be that the main listening 1485f8829a4aSRandall Stewart * endpoint is gone but there exists a connected socket for this guy 1486f8829a4aSRandall Stewart * yet. If so we can return the first one that we find. This may NOT 1487f8829a4aSRandall Stewart * be the correct one but the sctp_findassociation_ep_addr has 1488f8829a4aSRandall Stewart * further code to look at all TCP models. 1489f8829a4aSRandall Stewart */ 1490f8829a4aSRandall Stewart if (inp == NULL && find_tcp_pool) { 1491f8829a4aSRandall Stewart unsigned int i; 1492f8829a4aSRandall Stewart 1493f8829a4aSRandall Stewart for (i = 0; i < sctppcbinfo.hashtblsize; i++) { 1494f8829a4aSRandall Stewart /* 1495f8829a4aSRandall Stewart * This is real gross, but we do NOT have a remote 1496f8829a4aSRandall Stewart * port at this point depending on who is calling. 1497f8829a4aSRandall Stewart * We must therefore look for ANY one that matches 1498f8829a4aSRandall Stewart * our local port :/ 1499f8829a4aSRandall Stewart */ 1500f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_tcpephash[i]; 1501f8829a4aSRandall Stewart if (LIST_FIRST(head)) { 150242551e99SRandall Stewart inp = sctp_endpoint_probe(nam, head, lport, vrf_id); 1503f8829a4aSRandall Stewart if (inp) { 1504f8829a4aSRandall Stewart /* Found one */ 1505f8829a4aSRandall Stewart break; 1506f8829a4aSRandall Stewart } 1507f8829a4aSRandall Stewart } 1508f8829a4aSRandall Stewart } 1509f8829a4aSRandall Stewart } 1510f8829a4aSRandall Stewart if (inp) { 1511f8829a4aSRandall Stewart SCTP_INP_INCR_REF(inp); 1512f8829a4aSRandall Stewart } 1513f8829a4aSRandall Stewart if (have_lock == 0) { 1514f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1515f8829a4aSRandall Stewart } 1516f8829a4aSRandall Stewart return (inp); 1517f8829a4aSRandall Stewart } 1518f8829a4aSRandall Stewart 1519f8829a4aSRandall Stewart /* 1520f8829a4aSRandall Stewart * Find an association for an endpoint with the pointer to whom you want to 1521f8829a4aSRandall Stewart * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may 1522f8829a4aSRandall Stewart * need to change the *to to some other struct like a mbuf... 1523f8829a4aSRandall Stewart */ 1524f8829a4aSRandall Stewart struct sctp_tcb * 1525f8829a4aSRandall Stewart sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from, 15269a972525SRandall Stewart struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, 15279a972525SRandall Stewart uint32_t vrf_id) 1528f8829a4aSRandall Stewart { 1529c105859eSRandall Stewart struct sctp_inpcb *inp = NULL; 1530f8829a4aSRandall Stewart struct sctp_tcb *retval; 1531f8829a4aSRandall Stewart 1532f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 1533f8829a4aSRandall Stewart if (find_tcp_pool) { 1534f8829a4aSRandall Stewart if (inp_p != NULL) { 15359a972525SRandall Stewart retval = sctp_tcb_special_locate(inp_p, from, to, netp, 15369a972525SRandall Stewart vrf_id); 1537f8829a4aSRandall Stewart } else { 15389a972525SRandall Stewart retval = sctp_tcb_special_locate(&inp, from, to, netp, 15399a972525SRandall Stewart vrf_id); 1540f8829a4aSRandall Stewart } 1541f8829a4aSRandall Stewart if (retval != NULL) { 1542f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1543f8829a4aSRandall Stewart return (retval); 1544f8829a4aSRandall Stewart } 1545f8829a4aSRandall Stewart } 154642551e99SRandall Stewart inp = sctp_pcb_findep(to, 0, 1, vrf_id); 1547f8829a4aSRandall Stewart if (inp_p != NULL) { 1548f8829a4aSRandall Stewart *inp_p = inp; 1549f8829a4aSRandall Stewart } 1550f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1551f8829a4aSRandall Stewart 1552f8829a4aSRandall Stewart if (inp == NULL) { 1553f8829a4aSRandall Stewart return (NULL); 1554f8829a4aSRandall Stewart } 1555f8829a4aSRandall Stewart /* 1556f8829a4aSRandall Stewart * ok, we have an endpoint, now lets find the assoc for it (if any) 1557f8829a4aSRandall Stewart * we now place the source address or from in the to of the find 1558f8829a4aSRandall Stewart * endpoint call. Since in reality this chain is used from the 1559f8829a4aSRandall Stewart * inbound packet side. 1560f8829a4aSRandall Stewart */ 1561f8829a4aSRandall Stewart if (inp_p != NULL) { 15629a972525SRandall Stewart retval = sctp_findassociation_ep_addr(inp_p, from, netp, to, 15639a972525SRandall Stewart NULL); 1564f8829a4aSRandall Stewart } else { 15659a972525SRandall Stewart retval = sctp_findassociation_ep_addr(&inp, from, netp, to, 15669a972525SRandall Stewart NULL); 1567f8829a4aSRandall Stewart } 1568f8829a4aSRandall Stewart return retval; 1569f8829a4aSRandall Stewart } 1570f8829a4aSRandall Stewart 1571f8829a4aSRandall Stewart 1572f8829a4aSRandall Stewart /* 1573f8829a4aSRandall Stewart * This routine will grub through the mbuf that is a INIT or INIT-ACK and 1574f8829a4aSRandall Stewart * find all addresses that the sender has specified in any address list. Each 1575f8829a4aSRandall Stewart * address will be used to lookup the TCB and see if one exits. 1576f8829a4aSRandall Stewart */ 1577f8829a4aSRandall Stewart static struct sctp_tcb * 1578f8829a4aSRandall Stewart sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset, 1579f8829a4aSRandall Stewart struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, 1580f8829a4aSRandall Stewart struct sockaddr *dest) 1581f8829a4aSRandall Stewart { 1582f8829a4aSRandall Stewart struct sockaddr_in sin4; 1583f8829a4aSRandall Stewart struct sockaddr_in6 sin6; 1584f8829a4aSRandall Stewart struct sctp_paramhdr *phdr, parm_buf; 1585f8829a4aSRandall Stewart struct sctp_tcb *retval; 1586f8829a4aSRandall Stewart uint32_t ptype, plen; 1587f8829a4aSRandall Stewart 1588f8829a4aSRandall Stewart memset(&sin4, 0, sizeof(sin4)); 1589f8829a4aSRandall Stewart memset(&sin6, 0, sizeof(sin6)); 1590f8829a4aSRandall Stewart sin4.sin_len = sizeof(sin4); 1591f8829a4aSRandall Stewart sin4.sin_family = AF_INET; 1592f8829a4aSRandall Stewart sin4.sin_port = sh->src_port; 1593f8829a4aSRandall Stewart sin6.sin6_len = sizeof(sin6); 1594f8829a4aSRandall Stewart sin6.sin6_family = AF_INET6; 1595f8829a4aSRandall Stewart sin6.sin6_port = sh->src_port; 1596f8829a4aSRandall Stewart 1597f8829a4aSRandall Stewart retval = NULL; 1598f8829a4aSRandall Stewart offset += sizeof(struct sctp_init_chunk); 1599f8829a4aSRandall Stewart 1600f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); 1601f8829a4aSRandall Stewart while (phdr != NULL) { 1602f8829a4aSRandall Stewart /* now we must see if we want the parameter */ 1603f8829a4aSRandall Stewart ptype = ntohs(phdr->param_type); 1604f8829a4aSRandall Stewart plen = ntohs(phdr->param_length); 1605f8829a4aSRandall Stewart if (plen == 0) { 1606f8829a4aSRandall Stewart break; 1607f8829a4aSRandall Stewart } 1608f8829a4aSRandall Stewart if (ptype == SCTP_IPV4_ADDRESS && 1609f8829a4aSRandall Stewart plen == sizeof(struct sctp_ipv4addr_param)) { 1610f8829a4aSRandall Stewart /* Get the rest of the address */ 1611f8829a4aSRandall Stewart struct sctp_ipv4addr_param ip4_parm, *p4; 1612f8829a4aSRandall Stewart 1613f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 1614d06c82f1SRandall Stewart (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm))); 1615f8829a4aSRandall Stewart if (phdr == NULL) { 1616f8829a4aSRandall Stewart return (NULL); 1617f8829a4aSRandall Stewart } 1618f8829a4aSRandall Stewart p4 = (struct sctp_ipv4addr_param *)phdr; 1619f8829a4aSRandall Stewart memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr)); 1620f8829a4aSRandall Stewart /* look it up */ 1621f8829a4aSRandall Stewart retval = sctp_findassociation_ep_addr(inp_p, 1622f8829a4aSRandall Stewart (struct sockaddr *)&sin4, netp, dest, NULL); 1623f8829a4aSRandall Stewart if (retval != NULL) { 1624f8829a4aSRandall Stewart return (retval); 1625f8829a4aSRandall Stewart } 1626f8829a4aSRandall Stewart } else if (ptype == SCTP_IPV6_ADDRESS && 1627f8829a4aSRandall Stewart plen == sizeof(struct sctp_ipv6addr_param)) { 1628f8829a4aSRandall Stewart /* Get the rest of the address */ 1629f8829a4aSRandall Stewart struct sctp_ipv6addr_param ip6_parm, *p6; 1630f8829a4aSRandall Stewart 1631f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 1632d06c82f1SRandall Stewart (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm))); 1633f8829a4aSRandall Stewart if (phdr == NULL) { 1634f8829a4aSRandall Stewart return (NULL); 1635f8829a4aSRandall Stewart } 1636f8829a4aSRandall Stewart p6 = (struct sctp_ipv6addr_param *)phdr; 1637f8829a4aSRandall Stewart memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr)); 1638f8829a4aSRandall Stewart /* look it up */ 1639f8829a4aSRandall Stewart retval = sctp_findassociation_ep_addr(inp_p, 1640f8829a4aSRandall Stewart (struct sockaddr *)&sin6, netp, dest, NULL); 1641f8829a4aSRandall Stewart if (retval != NULL) { 1642f8829a4aSRandall Stewart return (retval); 1643f8829a4aSRandall Stewart } 1644f8829a4aSRandall Stewart } 1645f8829a4aSRandall Stewart offset += SCTP_SIZE32(plen); 1646f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, &parm_buf, 1647f8829a4aSRandall Stewart sizeof(parm_buf)); 1648f8829a4aSRandall Stewart } 1649f8829a4aSRandall Stewart return (NULL); 1650f8829a4aSRandall Stewart } 1651f8829a4aSRandall Stewart 1652f8829a4aSRandall Stewart 1653f8829a4aSRandall Stewart static struct sctp_tcb * 1654f8829a4aSRandall Stewart sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag, 1655f8829a4aSRandall Stewart struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport, 1656f8829a4aSRandall Stewart uint16_t lport, int skip_src_check) 1657f8829a4aSRandall Stewart { 1658f8829a4aSRandall Stewart /* 1659f8829a4aSRandall Stewart * Use my vtag to hash. If we find it we then verify the source addr 1660f8829a4aSRandall Stewart * is in the assoc. If all goes well we save a bit on rec of a 1661f8829a4aSRandall Stewart * packet. 1662f8829a4aSRandall Stewart */ 1663f8829a4aSRandall Stewart struct sctpasochead *head; 1664f8829a4aSRandall Stewart struct sctp_nets *net; 1665f8829a4aSRandall Stewart struct sctp_tcb *stcb; 1666f8829a4aSRandall Stewart 1667f8829a4aSRandall Stewart *netp = NULL; 1668f8829a4aSRandall Stewart *inp_p = NULL; 1669f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 1670f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag, 1671f8829a4aSRandall Stewart sctppcbinfo.hashasocmark)]; 1672f8829a4aSRandall Stewart if (head == NULL) { 1673f8829a4aSRandall Stewart /* invalid vtag */ 1674f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1675f8829a4aSRandall Stewart return (NULL); 1676f8829a4aSRandall Stewart } 1677f8829a4aSRandall Stewart LIST_FOREACH(stcb, head, sctp_asocs) { 1678f8829a4aSRandall Stewart SCTP_INP_RLOCK(stcb->sctp_ep); 1679f8829a4aSRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 1680f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1681d06c82f1SRandall Stewart continue; 1682f8829a4aSRandall Stewart } 1683f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 1684f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(stcb->sctp_ep); 1685f8829a4aSRandall Stewart if (stcb->asoc.my_vtag == vtag) { 1686f8829a4aSRandall Stewart /* candidate */ 1687f8829a4aSRandall Stewart if (stcb->rport != rport) { 1688f8829a4aSRandall Stewart /* 1689f8829a4aSRandall Stewart * we could remove this if vtags are unique 1690f8829a4aSRandall Stewart * across the system. 1691f8829a4aSRandall Stewart */ 1692f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1693f8829a4aSRandall Stewart continue; 1694f8829a4aSRandall Stewart } 1695f8829a4aSRandall Stewart if (stcb->sctp_ep->sctp_lport != lport) { 1696f8829a4aSRandall Stewart /* 1697f8829a4aSRandall Stewart * we could remove this if vtags are unique 1698f8829a4aSRandall Stewart * across the system. 1699f8829a4aSRandall Stewart */ 1700f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1701f8829a4aSRandall Stewart continue; 1702f8829a4aSRandall Stewart } 1703f8829a4aSRandall Stewart if (skip_src_check) { 1704f8829a4aSRandall Stewart *netp = NULL; /* unknown */ 1705ad81507eSRandall Stewart if (inp_p) 1706f8829a4aSRandall Stewart *inp_p = stcb->sctp_ep; 1707f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1708f8829a4aSRandall Stewart return (stcb); 1709f8829a4aSRandall Stewart } 1710f8829a4aSRandall Stewart net = sctp_findnet(stcb, from); 1711f8829a4aSRandall Stewart if (net) { 1712f8829a4aSRandall Stewart /* yep its him. */ 1713f8829a4aSRandall Stewart *netp = net; 1714f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_vtagexpress); 1715f8829a4aSRandall Stewart *inp_p = stcb->sctp_ep; 1716f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1717f8829a4aSRandall Stewart return (stcb); 1718f8829a4aSRandall Stewart } else { 1719f8829a4aSRandall Stewart /* 1720f8829a4aSRandall Stewart * not him, this should only happen in rare 1721f8829a4aSRandall Stewart * cases so I peg it. 1722f8829a4aSRandall Stewart */ 1723f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_vtagbogus); 1724f8829a4aSRandall Stewart } 1725f8829a4aSRandall Stewart } 1726f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1727f8829a4aSRandall Stewart } 1728f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1729f8829a4aSRandall Stewart return (NULL); 1730f8829a4aSRandall Stewart } 1731f8829a4aSRandall Stewart 1732f8829a4aSRandall Stewart /* 1733f8829a4aSRandall Stewart * Find an association with the pointer to the inbound IP packet. This can be 1734f8829a4aSRandall Stewart * a IPv4 or IPv6 packet. 1735f8829a4aSRandall Stewart */ 1736f8829a4aSRandall Stewart struct sctp_tcb * 1737f8829a4aSRandall Stewart sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset, 1738f8829a4aSRandall Stewart struct sctphdr *sh, struct sctp_chunkhdr *ch, 1739bff64a4dSRandall Stewart struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) 1740f8829a4aSRandall Stewart { 1741f8829a4aSRandall Stewart int find_tcp_pool; 1742f8829a4aSRandall Stewart struct ip *iph; 1743f8829a4aSRandall Stewart struct sctp_tcb *retval; 1744f8829a4aSRandall Stewart struct sockaddr_storage to_store, from_store; 1745f8829a4aSRandall Stewart struct sockaddr *to = (struct sockaddr *)&to_store; 1746f8829a4aSRandall Stewart struct sockaddr *from = (struct sockaddr *)&from_store; 1747f8829a4aSRandall Stewart struct sctp_inpcb *inp; 1748f8829a4aSRandall Stewart 1749f8829a4aSRandall Stewart iph = mtod(m, struct ip *); 1750f8829a4aSRandall Stewart if (iph->ip_v == IPVERSION) { 1751f8829a4aSRandall Stewart /* its IPv4 */ 1752f8829a4aSRandall Stewart struct sockaddr_in *from4; 1753f8829a4aSRandall Stewart 1754f8829a4aSRandall Stewart from4 = (struct sockaddr_in *)&from_store; 1755f8829a4aSRandall Stewart bzero(from4, sizeof(*from4)); 1756f8829a4aSRandall Stewart from4->sin_family = AF_INET; 1757f8829a4aSRandall Stewart from4->sin_len = sizeof(struct sockaddr_in); 1758f8829a4aSRandall Stewart from4->sin_addr.s_addr = iph->ip_src.s_addr; 1759f8829a4aSRandall Stewart from4->sin_port = sh->src_port; 1760f8829a4aSRandall Stewart } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1761f8829a4aSRandall Stewart /* its IPv6 */ 1762f8829a4aSRandall Stewart struct ip6_hdr *ip6; 1763f8829a4aSRandall Stewart struct sockaddr_in6 *from6; 1764f8829a4aSRandall Stewart 1765f8829a4aSRandall Stewart ip6 = mtod(m, struct ip6_hdr *); 1766f8829a4aSRandall Stewart from6 = (struct sockaddr_in6 *)&from_store; 1767f8829a4aSRandall Stewart bzero(from6, sizeof(*from6)); 1768f8829a4aSRandall Stewart from6->sin6_family = AF_INET6; 1769f8829a4aSRandall Stewart from6->sin6_len = sizeof(struct sockaddr_in6); 1770f8829a4aSRandall Stewart from6->sin6_addr = ip6->ip6_src; 1771f8829a4aSRandall Stewart from6->sin6_port = sh->src_port; 1772f8829a4aSRandall Stewart /* Get the scopes in properly to the sin6 addr's */ 1773f8829a4aSRandall Stewart /* we probably don't need these operations */ 1774f8829a4aSRandall Stewart (void)sa6_recoverscope(from6); 1775f8829a4aSRandall Stewart sa6_embedscope(from6, ip6_use_defzone); 1776f8829a4aSRandall Stewart } else { 1777f8829a4aSRandall Stewart /* Currently not supported. */ 1778f8829a4aSRandall Stewart return (NULL); 1779f8829a4aSRandall Stewart } 1780f8829a4aSRandall Stewart if (sh->v_tag) { 1781f8829a4aSRandall Stewart /* we only go down this path if vtag is non-zero */ 1782f8829a4aSRandall Stewart retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag), 1783f8829a4aSRandall Stewart inp_p, netp, sh->src_port, sh->dest_port, 0); 1784f8829a4aSRandall Stewart if (retval) { 1785f8829a4aSRandall Stewart return (retval); 1786f8829a4aSRandall Stewart } 1787f8829a4aSRandall Stewart } 1788f8829a4aSRandall Stewart if (iph->ip_v == IPVERSION) { 1789f8829a4aSRandall Stewart /* its IPv4 */ 1790f8829a4aSRandall Stewart struct sockaddr_in *to4; 1791f8829a4aSRandall Stewart 1792f8829a4aSRandall Stewart to4 = (struct sockaddr_in *)&to_store; 1793f8829a4aSRandall Stewart bzero(to4, sizeof(*to4)); 1794f8829a4aSRandall Stewart to4->sin_family = AF_INET; 1795f8829a4aSRandall Stewart to4->sin_len = sizeof(struct sockaddr_in); 1796f8829a4aSRandall Stewart to4->sin_addr.s_addr = iph->ip_dst.s_addr; 1797f8829a4aSRandall Stewart to4->sin_port = sh->dest_port; 1798f8829a4aSRandall Stewart } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1799f8829a4aSRandall Stewart /* its IPv6 */ 1800f8829a4aSRandall Stewart struct ip6_hdr *ip6; 1801f8829a4aSRandall Stewart struct sockaddr_in6 *to6; 1802f8829a4aSRandall Stewart 1803f8829a4aSRandall Stewart ip6 = mtod(m, struct ip6_hdr *); 1804f8829a4aSRandall Stewart to6 = (struct sockaddr_in6 *)&to_store; 1805f8829a4aSRandall Stewart bzero(to6, sizeof(*to6)); 1806f8829a4aSRandall Stewart to6->sin6_family = AF_INET6; 1807f8829a4aSRandall Stewart to6->sin6_len = sizeof(struct sockaddr_in6); 1808f8829a4aSRandall Stewart to6->sin6_addr = ip6->ip6_dst; 1809f8829a4aSRandall Stewart to6->sin6_port = sh->dest_port; 1810f8829a4aSRandall Stewart /* Get the scopes in properly to the sin6 addr's */ 1811f8829a4aSRandall Stewart /* we probably don't need these operations */ 1812f8829a4aSRandall Stewart (void)sa6_recoverscope(to6); 1813f8829a4aSRandall Stewart sa6_embedscope(to6, ip6_use_defzone); 1814f8829a4aSRandall Stewart } 1815f8829a4aSRandall Stewart find_tcp_pool = 0; 1816f8829a4aSRandall Stewart if ((ch->chunk_type != SCTP_INITIATION) && 1817f8829a4aSRandall Stewart (ch->chunk_type != SCTP_INITIATION_ACK) && 1818f8829a4aSRandall Stewart (ch->chunk_type != SCTP_COOKIE_ACK) && 1819f8829a4aSRandall Stewart (ch->chunk_type != SCTP_COOKIE_ECHO)) { 1820f8829a4aSRandall Stewart /* Other chunk types go to the tcp pool. */ 1821f8829a4aSRandall Stewart find_tcp_pool = 1; 1822f8829a4aSRandall Stewart } 1823f8829a4aSRandall Stewart if (inp_p) { 1824f8829a4aSRandall Stewart retval = sctp_findassociation_addr_sa(to, from, inp_p, netp, 182542551e99SRandall Stewart find_tcp_pool, vrf_id); 1826f8829a4aSRandall Stewart inp = *inp_p; 1827f8829a4aSRandall Stewart } else { 1828f8829a4aSRandall Stewart retval = sctp_findassociation_addr_sa(to, from, &inp, netp, 182942551e99SRandall Stewart find_tcp_pool, vrf_id); 1830f8829a4aSRandall Stewart } 1831ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp); 1832f8829a4aSRandall Stewart if (retval == NULL && inp) { 1833f8829a4aSRandall Stewart /* Found a EP but not this address */ 1834f8829a4aSRandall Stewart if ((ch->chunk_type == SCTP_INITIATION) || 1835f8829a4aSRandall Stewart (ch->chunk_type == SCTP_INITIATION_ACK)) { 1836d06c82f1SRandall Stewart /*- 1837f8829a4aSRandall Stewart * special hook, we do NOT return linp or an 1838f8829a4aSRandall Stewart * association that is linked to an existing 1839f8829a4aSRandall Stewart * association that is under the TCP pool (i.e. no 1840f8829a4aSRandall Stewart * listener exists). The endpoint finding routine 1841f8829a4aSRandall Stewart * will always find a listner before examining the 1842f8829a4aSRandall Stewart * TCP pool. 1843f8829a4aSRandall Stewart */ 1844f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { 1845f8829a4aSRandall Stewart if (inp_p) { 1846f8829a4aSRandall Stewart *inp_p = NULL; 1847f8829a4aSRandall Stewart } 1848f8829a4aSRandall Stewart return (NULL); 1849f8829a4aSRandall Stewart } 1850f8829a4aSRandall Stewart retval = sctp_findassociation_special_addr(m, iphlen, 1851f8829a4aSRandall Stewart offset, sh, &inp, netp, to); 1852f8829a4aSRandall Stewart if (inp_p != NULL) { 1853f8829a4aSRandall Stewart *inp_p = inp; 1854f8829a4aSRandall Stewart } 1855f8829a4aSRandall Stewart } 1856f8829a4aSRandall Stewart } 1857ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval); 1858f8829a4aSRandall Stewart return (retval); 1859f8829a4aSRandall Stewart } 1860f8829a4aSRandall Stewart 1861f8829a4aSRandall Stewart /* 1862f8829a4aSRandall Stewart * lookup an association by an ASCONF lookup address. 1863f8829a4aSRandall Stewart * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup 1864f8829a4aSRandall Stewart */ 1865f8829a4aSRandall Stewart struct sctp_tcb * 1866f8829a4aSRandall Stewart sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset, 1867f8829a4aSRandall Stewart struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp) 1868f8829a4aSRandall Stewart { 1869f8829a4aSRandall Stewart struct sctp_tcb *stcb; 1870f8829a4aSRandall Stewart struct sockaddr_in *sin; 1871f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 1872f8829a4aSRandall Stewart struct sockaddr_storage local_store, remote_store; 1873f8829a4aSRandall Stewart struct ip *iph; 1874f8829a4aSRandall Stewart struct sctp_paramhdr parm_buf, *phdr; 1875f8829a4aSRandall Stewart int ptype; 1876f8829a4aSRandall Stewart int zero_address = 0; 1877f8829a4aSRandall Stewart 1878f8829a4aSRandall Stewart 1879f8829a4aSRandall Stewart memset(&local_store, 0, sizeof(local_store)); 1880f8829a4aSRandall Stewart memset(&remote_store, 0, sizeof(remote_store)); 1881f8829a4aSRandall Stewart 1882f8829a4aSRandall Stewart /* First get the destination address setup too. */ 1883f8829a4aSRandall Stewart iph = mtod(m, struct ip *); 1884f8829a4aSRandall Stewart if (iph->ip_v == IPVERSION) { 1885f8829a4aSRandall Stewart /* its IPv4 */ 1886f8829a4aSRandall Stewart sin = (struct sockaddr_in *)&local_store; 1887f8829a4aSRandall Stewart sin->sin_family = AF_INET; 1888f8829a4aSRandall Stewart sin->sin_len = sizeof(*sin); 1889f8829a4aSRandall Stewart sin->sin_port = sh->dest_port; 1890f8829a4aSRandall Stewart sin->sin_addr.s_addr = iph->ip_dst.s_addr; 1891f8829a4aSRandall Stewart } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 1892f8829a4aSRandall Stewart /* its IPv6 */ 1893f8829a4aSRandall Stewart struct ip6_hdr *ip6; 1894f8829a4aSRandall Stewart 1895f8829a4aSRandall Stewart ip6 = mtod(m, struct ip6_hdr *); 1896f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)&local_store; 1897f8829a4aSRandall Stewart sin6->sin6_family = AF_INET6; 1898f8829a4aSRandall Stewart sin6->sin6_len = sizeof(*sin6); 1899f8829a4aSRandall Stewart sin6->sin6_port = sh->dest_port; 1900f8829a4aSRandall Stewart sin6->sin6_addr = ip6->ip6_dst; 1901f8829a4aSRandall Stewart } else { 1902f8829a4aSRandall Stewart return NULL; 1903f8829a4aSRandall Stewart } 1904f8829a4aSRandall Stewart 1905f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk), 1906f8829a4aSRandall Stewart &parm_buf, sizeof(struct sctp_paramhdr)); 1907f8829a4aSRandall Stewart if (phdr == NULL) { 1908ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n", 1909ad81507eSRandall Stewart __FUNCTION__); 1910f8829a4aSRandall Stewart return NULL; 1911f8829a4aSRandall Stewart } 1912f8829a4aSRandall Stewart ptype = (int)((uint32_t) ntohs(phdr->param_type)); 1913f8829a4aSRandall Stewart /* get the correlation address */ 1914f8829a4aSRandall Stewart if (ptype == SCTP_IPV6_ADDRESS) { 1915f8829a4aSRandall Stewart /* ipv6 address param */ 1916f8829a4aSRandall Stewart struct sctp_ipv6addr_param *p6, p6_buf; 1917f8829a4aSRandall Stewart 1918f8829a4aSRandall Stewart if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) { 1919f8829a4aSRandall Stewart return NULL; 1920f8829a4aSRandall Stewart } 1921f8829a4aSRandall Stewart p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m, 1922f8829a4aSRandall Stewart offset + sizeof(struct sctp_asconf_chunk), 1923f8829a4aSRandall Stewart &p6_buf.ph, sizeof(*p6)); 1924f8829a4aSRandall Stewart if (p6 == NULL) { 1925ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n", 1926ad81507eSRandall Stewart __FUNCTION__); 1927f8829a4aSRandall Stewart return (NULL); 1928f8829a4aSRandall Stewart } 1929f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)&remote_store; 1930f8829a4aSRandall Stewart sin6->sin6_family = AF_INET6; 1931f8829a4aSRandall Stewart sin6->sin6_len = sizeof(*sin6); 1932f8829a4aSRandall Stewart sin6->sin6_port = sh->src_port; 1933f8829a4aSRandall Stewart memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr)); 1934f8829a4aSRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 1935f8829a4aSRandall Stewart zero_address = 1; 1936f8829a4aSRandall Stewart } else if (ptype == SCTP_IPV4_ADDRESS) { 1937f8829a4aSRandall Stewart /* ipv4 address param */ 1938f8829a4aSRandall Stewart struct sctp_ipv4addr_param *p4, p4_buf; 1939f8829a4aSRandall Stewart 1940f8829a4aSRandall Stewart if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) { 1941f8829a4aSRandall Stewart return NULL; 1942f8829a4aSRandall Stewart } 1943f8829a4aSRandall Stewart p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m, 1944f8829a4aSRandall Stewart offset + sizeof(struct sctp_asconf_chunk), 1945f8829a4aSRandall Stewart &p4_buf.ph, sizeof(*p4)); 1946f8829a4aSRandall Stewart if (p4 == NULL) { 1947ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n", 1948ad81507eSRandall Stewart __FUNCTION__); 1949f8829a4aSRandall Stewart return (NULL); 1950f8829a4aSRandall Stewart } 1951f8829a4aSRandall Stewart sin = (struct sockaddr_in *)&remote_store; 1952f8829a4aSRandall Stewart sin->sin_family = AF_INET; 1953f8829a4aSRandall Stewart sin->sin_len = sizeof(*sin); 1954f8829a4aSRandall Stewart sin->sin_port = sh->src_port; 1955f8829a4aSRandall Stewart memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr)); 1956f8829a4aSRandall Stewart if (sin->sin_addr.s_addr == INADDR_ANY) 1957f8829a4aSRandall Stewart zero_address = 1; 1958f8829a4aSRandall Stewart } else { 1959f8829a4aSRandall Stewart /* invalid address param type */ 1960f8829a4aSRandall Stewart return NULL; 1961f8829a4aSRandall Stewart } 1962f8829a4aSRandall Stewart 1963f8829a4aSRandall Stewart if (zero_address) { 1964f8829a4aSRandall Stewart stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p, 1965f8829a4aSRandall Stewart netp, sh->src_port, sh->dest_port, 1); 1966f8829a4aSRandall Stewart /* 1967f8829a4aSRandall Stewart * printf("findassociation_ep_asconf: zero lookup address 1968f8829a4aSRandall Stewart * finds stcb 0x%x\n", (uint32_t)stcb); 1969f8829a4aSRandall Stewart */ 1970f8829a4aSRandall Stewart } else { 1971f8829a4aSRandall Stewart stcb = sctp_findassociation_ep_addr(inp_p, 1972f8829a4aSRandall Stewart (struct sockaddr *)&remote_store, netp, 1973f8829a4aSRandall Stewart (struct sockaddr *)&local_store, NULL); 1974f8829a4aSRandall Stewart } 1975f8829a4aSRandall Stewart return (stcb); 1976f8829a4aSRandall Stewart } 1977f8829a4aSRandall Stewart 1978f8829a4aSRandall Stewart 1979f8829a4aSRandall Stewart /* 1980f8829a4aSRandall Stewart * allocate a sctp_inpcb and setup a temporary binding to a port/all 1981f8829a4aSRandall Stewart * addresses. This way if we don't get a bind we by default pick a ephemeral 1982f8829a4aSRandall Stewart * port with all addresses bound. 1983f8829a4aSRandall Stewart */ 1984f8829a4aSRandall Stewart int 1985f4c93d24SRandall Stewart sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id) 1986f8829a4aSRandall Stewart { 1987f8829a4aSRandall Stewart /* 1988f8829a4aSRandall Stewart * we get called when a new endpoint starts up. We need to allocate 1989f8829a4aSRandall Stewart * the sctp_inpcb structure from the zone and init it. Mark it as 1990f8829a4aSRandall Stewart * unbound and find a port that we can use as an ephemeral with 1991f8829a4aSRandall Stewart * INADDR_ANY. If the user binds later no problem we can then add in 1992f8829a4aSRandall Stewart * the specific addresses. And setup the default parameters for the 1993f8829a4aSRandall Stewart * EP. 1994f8829a4aSRandall Stewart */ 1995f8829a4aSRandall Stewart int i, error; 1996f8829a4aSRandall Stewart struct sctp_inpcb *inp; 1997f8829a4aSRandall Stewart struct sctp_pcb *m; 1998f8829a4aSRandall Stewart struct timeval time; 1999f8829a4aSRandall Stewart sctp_sharedkey_t *null_key; 2000f8829a4aSRandall Stewart 2001f8829a4aSRandall Stewart error = 0; 2002f8829a4aSRandall Stewart 2003f8829a4aSRandall Stewart SCTP_INP_INFO_WLOCK(); 2004f42a358aSRandall Stewart inp = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep, struct sctp_inpcb); 2005f8829a4aSRandall Stewart if (inp == NULL) { 2006ad81507eSRandall Stewart SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n"); 2007f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2008c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 2009f8829a4aSRandall Stewart return (ENOBUFS); 2010f8829a4aSRandall Stewart } 2011f8829a4aSRandall Stewart /* zap it */ 2012f8829a4aSRandall Stewart bzero(inp, sizeof(*inp)); 2013f8829a4aSRandall Stewart 2014f8829a4aSRandall Stewart /* bump generations */ 2015f8829a4aSRandall Stewart /* setup socket pointers */ 2016f8829a4aSRandall Stewart inp->sctp_socket = so; 2017f8829a4aSRandall Stewart inp->ip_inp.inp.inp_socket = so; 2018f8829a4aSRandall Stewart 201962c1ff9cSRandall Stewart inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT; 2020f8829a4aSRandall Stewart inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; 2021f8829a4aSRandall Stewart 2022b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 2023f8829a4aSRandall Stewart { 2024f8829a4aSRandall Stewart struct inpcbpolicy *pcb_sp = NULL; 2025f8829a4aSRandall Stewart 20262cb64cb2SGeorge V. Neville-Neil error = ipsec_init_policy(so, &pcb_sp); 2027f8829a4aSRandall Stewart /* Arrange to share the policy */ 2028f8829a4aSRandall Stewart inp->ip_inp.inp.inp_sp = pcb_sp; 2029f8829a4aSRandall Stewart ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp; 2030f8829a4aSRandall Stewart } 2031f8829a4aSRandall Stewart if (error != 0) { 2032f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp); 2033f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2034f8829a4aSRandall Stewart return error; 2035f8829a4aSRandall Stewart } 2036b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 2037f8829a4aSRandall Stewart SCTP_INCR_EP_COUNT(); 2038f8829a4aSRandall Stewart inp->ip_inp.inp.inp_ip_ttl = ip_defttl; 2039f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2040f8829a4aSRandall Stewart 2041f8829a4aSRandall Stewart so->so_pcb = (caddr_t)inp; 2042f8829a4aSRandall Stewart 204342551e99SRandall Stewart if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) || 204442551e99SRandall Stewart (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) { 2045f8829a4aSRandall Stewart /* UDP style socket */ 2046f8829a4aSRandall Stewart inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | 2047f8829a4aSRandall Stewart SCTP_PCB_FLAGS_UNBOUND); 2048f8829a4aSRandall Stewart /* Be sure it is NON-BLOCKING IO for UDP */ 204942551e99SRandall Stewart /* SCTP_SET_SO_NBIO(so); */ 205042551e99SRandall Stewart } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) { 2051f8829a4aSRandall Stewart /* TCP style socket */ 2052f8829a4aSRandall Stewart inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | 2053f8829a4aSRandall Stewart SCTP_PCB_FLAGS_UNBOUND); 2054f8829a4aSRandall Stewart /* Be sure we have blocking IO by default */ 205542551e99SRandall Stewart SCTP_CLEAR_SO_NBIO(so); 2056f8829a4aSRandall Stewart } else { 2057f8829a4aSRandall Stewart /* 2058f8829a4aSRandall Stewart * unsupported socket type (RAW, etc)- in case we missed it 2059f8829a4aSRandall Stewart * in protosw 2060f8829a4aSRandall Stewart */ 2061c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP); 2062851b7298SRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp); 2063f8829a4aSRandall Stewart return (EOPNOTSUPP); 2064f8829a4aSRandall Stewart } 2065c4739e2fSRandall Stewart if (sctp_default_frag_interleave == SCTP_FRAG_LEVEL_1) { 2066d61a0ae0SRandall Stewart sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2067c4739e2fSRandall Stewart sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2068c4739e2fSRandall Stewart } else if (sctp_default_frag_interleave == SCTP_FRAG_LEVEL_2) { 2069c4739e2fSRandall Stewart sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2070c4739e2fSRandall Stewart sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2071c4739e2fSRandall Stewart } else if (sctp_default_frag_interleave == SCTP_FRAG_LEVEL_0) { 2072c4739e2fSRandall Stewart sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2073c4739e2fSRandall Stewart sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2074c4739e2fSRandall Stewart } 207544b7479bSRandall Stewart inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize, 2076f8829a4aSRandall Stewart &inp->sctp_hashmark); 2077f8829a4aSRandall Stewart if (inp->sctp_tcbhash == NULL) { 2078ad81507eSRandall Stewart SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n"); 2079c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 2080851b7298SRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp); 2081f8829a4aSRandall Stewart return (ENOBUFS); 2082f8829a4aSRandall Stewart } 2083f4c93d24SRandall Stewart inp->def_vrf_id = vrf_id; 208442551e99SRandall Stewart 2085f8829a4aSRandall Stewart SCTP_INP_INFO_WLOCK(); 2086f8829a4aSRandall Stewart SCTP_INP_LOCK_INIT(inp); 2087e1461651SRandall Stewart INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp"); 2088f8829a4aSRandall Stewart SCTP_INP_READ_INIT(inp); 2089f8829a4aSRandall Stewart SCTP_ASOC_CREATE_LOCK_INIT(inp); 2090f8829a4aSRandall Stewart /* lock the new ep */ 2091f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 2092f8829a4aSRandall Stewart 2093f8829a4aSRandall Stewart /* add it to the info area */ 2094f8829a4aSRandall Stewart LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list); 2095f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2096f8829a4aSRandall Stewart 2097f8829a4aSRandall Stewart TAILQ_INIT(&inp->read_queue); 2098f8829a4aSRandall Stewart LIST_INIT(&inp->sctp_addr_list); 209942551e99SRandall Stewart 2100f8829a4aSRandall Stewart LIST_INIT(&inp->sctp_asoc_list); 2101f8829a4aSRandall Stewart 2102a5d547adSRandall Stewart #ifdef SCTP_TRACK_FREED_ASOCS 2103a5d547adSRandall Stewart /* TEMP CODE */ 2104a5d547adSRandall Stewart LIST_INIT(&inp->sctp_asoc_free_list); 2105a5d547adSRandall Stewart #endif 2106f8829a4aSRandall Stewart /* Init the timer structure for signature change */ 2107139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer); 2108f8829a4aSRandall Stewart inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE; 2109f8829a4aSRandall Stewart 2110f8829a4aSRandall Stewart /* now init the actual endpoint default data */ 2111f8829a4aSRandall Stewart m = &inp->sctp_ep; 2112f8829a4aSRandall Stewart 2113f8829a4aSRandall Stewart /* setup the base timeout information */ 2114f8829a4aSRandall Stewart m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */ 2115f8829a4aSRandall Stewart m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */ 2116f8829a4aSRandall Stewart m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default); 2117f8829a4aSRandall Stewart m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default); 2118f8829a4aSRandall Stewart m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default); 2119f8829a4aSRandall Stewart m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default); 2120f8829a4aSRandall Stewart m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default); 2121f8829a4aSRandall Stewart /* all max/min max are in ms */ 2122f8829a4aSRandall Stewart m->sctp_maxrto = sctp_rto_max_default; 2123f8829a4aSRandall Stewart m->sctp_minrto = sctp_rto_min_default; 2124f8829a4aSRandall Stewart m->initial_rto = sctp_rto_initial_default; 2125f8829a4aSRandall Stewart m->initial_init_rto_max = sctp_init_rto_max_default; 212642551e99SRandall Stewart m->sctp_sack_freq = sctp_sack_freq_default; 2127f8829a4aSRandall Stewart 2128f8829a4aSRandall Stewart m->max_open_streams_intome = MAX_SCTP_STREAMS; 2129f8829a4aSRandall Stewart 2130f8829a4aSRandall Stewart m->max_init_times = sctp_init_rtx_max_default; 2131f8829a4aSRandall Stewart m->max_send_times = sctp_assoc_rtx_max_default; 2132f8829a4aSRandall Stewart m->def_net_failure = sctp_path_rtx_max_default; 2133f8829a4aSRandall Stewart m->sctp_sws_sender = SCTP_SWS_SENDER_DEF; 2134f8829a4aSRandall Stewart m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF; 2135f8829a4aSRandall Stewart m->max_burst = sctp_max_burst_default; 2136b54d3a6cSRandall Stewart if ((sctp_default_cc_module >= SCTP_CC_RFC2581) && 2137b54d3a6cSRandall Stewart (sctp_default_cc_module <= SCTP_CC_HTCP)) { 2138b54d3a6cSRandall Stewart m->sctp_default_cc_module = sctp_default_cc_module; 2139b54d3a6cSRandall Stewart } else { 2140b54d3a6cSRandall Stewart /* sysctl done with invalid value, set to 2581 */ 2141b54d3a6cSRandall Stewart m->sctp_default_cc_module = SCTP_CC_RFC2581; 2142b54d3a6cSRandall Stewart } 2143f8829a4aSRandall Stewart /* number of streams to pre-open on a association */ 2144f8829a4aSRandall Stewart m->pre_open_stream_count = sctp_nr_outgoing_streams_default; 2145f8829a4aSRandall Stewart 2146f8829a4aSRandall Stewart /* Add adaptation cookie */ 2147f8829a4aSRandall Stewart m->adaptation_layer_indicator = 0x504C5253; 2148f8829a4aSRandall Stewart 2149f8829a4aSRandall Stewart /* seed random number generator */ 2150f8829a4aSRandall Stewart m->random_counter = 1; 2151f8829a4aSRandall Stewart m->store_at = SCTP_SIGNATURE_SIZE; 2152139bc87fSRandall Stewart SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers)); 2153f8829a4aSRandall Stewart sctp_fill_random_store(m); 2154f8829a4aSRandall Stewart 2155f8829a4aSRandall Stewart /* Minimum cookie size */ 2156f8829a4aSRandall Stewart m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) + 2157f8829a4aSRandall Stewart sizeof(struct sctp_state_cookie); 2158f8829a4aSRandall Stewart m->size_of_a_cookie += SCTP_SIGNATURE_SIZE; 2159f8829a4aSRandall Stewart 2160f8829a4aSRandall Stewart /* Setup the initial secret */ 21616e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&time); 2162f8829a4aSRandall Stewart m->time_of_secret_change = time.tv_sec; 2163f8829a4aSRandall Stewart 2164f8829a4aSRandall Stewart for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { 2165f8829a4aSRandall Stewart m->secret_key[0][i] = sctp_select_initial_TSN(m); 2166f8829a4aSRandall Stewart } 2167f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL); 2168f8829a4aSRandall Stewart 2169f8829a4aSRandall Stewart /* How long is a cookie good for ? */ 21703c503c28SRandall Stewart m->def_cookie_life = MSEC_TO_TICKS(sctp_valid_cookie_life_default); 2171f8829a4aSRandall Stewart /* 2172f8829a4aSRandall Stewart * Initialize authentication parameters 2173f8829a4aSRandall Stewart */ 2174f8829a4aSRandall Stewart m->local_hmacs = sctp_default_supported_hmaclist(); 2175f8829a4aSRandall Stewart m->local_auth_chunks = sctp_alloc_chunklist(); 2176f8829a4aSRandall Stewart sctp_auth_set_default_chunks(m->local_auth_chunks); 2177f8829a4aSRandall Stewart LIST_INIT(&m->shared_keys); 2178f8829a4aSRandall Stewart /* add default NULL key as key id 0 */ 2179f8829a4aSRandall Stewart null_key = sctp_alloc_sharedkey(); 2180f8829a4aSRandall Stewart sctp_insert_sharedkey(&m->shared_keys, null_key); 2181f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2182f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 2183f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 12); 2184f8829a4aSRandall Stewart #endif 2185f8829a4aSRandall Stewart return (error); 2186f8829a4aSRandall Stewart } 2187f8829a4aSRandall Stewart 2188f8829a4aSRandall Stewart 2189f8829a4aSRandall Stewart void 2190f8829a4aSRandall Stewart sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp, 2191f8829a4aSRandall Stewart struct sctp_tcb *stcb) 2192f8829a4aSRandall Stewart { 2193f8829a4aSRandall Stewart struct sctp_nets *net; 2194f8829a4aSRandall Stewart uint16_t lport, rport; 2195f8829a4aSRandall Stewart struct sctppcbhead *head; 2196f8829a4aSRandall Stewart struct sctp_laddr *laddr, *oladdr; 2197f8829a4aSRandall Stewart 21982afb3e84SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 2199f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 2200f8829a4aSRandall Stewart SCTP_INP_INFO_WLOCK(); 2201f8829a4aSRandall Stewart SCTP_INP_WLOCK(old_inp); 2202f8829a4aSRandall Stewart SCTP_INP_WLOCK(new_inp); 2203f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 22042afb3e84SRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 2205f8829a4aSRandall Stewart 2206f8829a4aSRandall Stewart new_inp->sctp_ep.time_of_secret_change = 2207f8829a4aSRandall Stewart old_inp->sctp_ep.time_of_secret_change; 2208f8829a4aSRandall Stewart memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key, 2209f8829a4aSRandall Stewart sizeof(old_inp->sctp_ep.secret_key)); 2210f8829a4aSRandall Stewart new_inp->sctp_ep.current_secret_number = 2211f8829a4aSRandall Stewart old_inp->sctp_ep.current_secret_number; 2212f8829a4aSRandall Stewart new_inp->sctp_ep.last_secret_number = 2213f8829a4aSRandall Stewart old_inp->sctp_ep.last_secret_number; 2214f8829a4aSRandall Stewart new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie; 2215f8829a4aSRandall Stewart 2216f8829a4aSRandall Stewart /* make it so new data pours into the new socket */ 2217f8829a4aSRandall Stewart stcb->sctp_socket = new_inp->sctp_socket; 2218f8829a4aSRandall Stewart stcb->sctp_ep = new_inp; 2219f8829a4aSRandall Stewart 2220f8829a4aSRandall Stewart /* Copy the port across */ 2221f8829a4aSRandall Stewart lport = new_inp->sctp_lport = old_inp->sctp_lport; 2222f8829a4aSRandall Stewart rport = stcb->rport; 2223f8829a4aSRandall Stewart /* Pull the tcb from the old association */ 2224f8829a4aSRandall Stewart LIST_REMOVE(stcb, sctp_tcbhash); 2225f8829a4aSRandall Stewart LIST_REMOVE(stcb, sctp_tcblist); 2226f8829a4aSRandall Stewart 2227f8829a4aSRandall Stewart /* Now insert the new_inp into the TCP connected hash */ 2228f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport), 2229f8829a4aSRandall Stewart sctppcbinfo.hashtcpmark)]; 2230f8829a4aSRandall Stewart 2231f8829a4aSRandall Stewart LIST_INSERT_HEAD(head, new_inp, sctp_hash); 223293164cf9SRandall Stewart /* Its safe to access */ 223393164cf9SRandall Stewart new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; 2234f8829a4aSRandall Stewart 2235f8829a4aSRandall Stewart /* Now move the tcb into the endpoint list */ 2236f8829a4aSRandall Stewart LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist); 2237f8829a4aSRandall Stewart /* 2238f8829a4aSRandall Stewart * Question, do we even need to worry about the ep-hash since we 2239f8829a4aSRandall Stewart * only have one connection? Probably not :> so lets get rid of it 2240f8829a4aSRandall Stewart * and not suck up any kernel memory in that. 2241f8829a4aSRandall Stewart */ 2242f8829a4aSRandall Stewart 2243f8829a4aSRandall Stewart /* Ok. Let's restart timer. */ 2244f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2245f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp, 2246f8829a4aSRandall Stewart stcb, net); 2247f8829a4aSRandall Stewart } 2248f8829a4aSRandall Stewart 2249f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2250f8829a4aSRandall Stewart if (new_inp->sctp_tcbhash != NULL) { 225144b7479bSRandall Stewart SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark); 2252f8829a4aSRandall Stewart new_inp->sctp_tcbhash = NULL; 2253f8829a4aSRandall Stewart } 2254f8829a4aSRandall Stewart if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { 2255f8829a4aSRandall Stewart /* Subset bound, so copy in the laddr list from the old_inp */ 2256f8829a4aSRandall Stewart LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) { 2257f42a358aSRandall Stewart laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr); 2258f8829a4aSRandall Stewart if (laddr == NULL) { 2259f8829a4aSRandall Stewart /* 2260f8829a4aSRandall Stewart * Gak, what can we do? This assoc is really 2261f8829a4aSRandall Stewart * HOSED. We probably should send an abort 2262f8829a4aSRandall Stewart * here. 2263f8829a4aSRandall Stewart */ 2264ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n"); 2265f8829a4aSRandall Stewart continue; 2266f8829a4aSRandall Stewart } 2267f8829a4aSRandall Stewart SCTP_INCR_LADDR_COUNT(); 2268f8829a4aSRandall Stewart bzero(laddr, sizeof(*laddr)); 2269d61a0ae0SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); 2270f8829a4aSRandall Stewart laddr->ifa = oladdr->ifa; 2271bff64a4dSRandall Stewart atomic_add_int(&laddr->ifa->refcount, 1); 2272f8829a4aSRandall Stewart LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr, 2273f8829a4aSRandall Stewart sctp_nxt_addr); 2274f8829a4aSRandall Stewart new_inp->laddr_count++; 2275f8829a4aSRandall Stewart } 2276f8829a4aSRandall Stewart } 2277f8829a4aSRandall Stewart /* 2278f8829a4aSRandall Stewart * Now any running timers need to be adjusted since we really don't 2279f8829a4aSRandall Stewart * care if they are running or not just blast in the new_inp into 2280f8829a4aSRandall Stewart * all of them. 2281f8829a4aSRandall Stewart */ 2282f8829a4aSRandall Stewart 2283f8829a4aSRandall Stewart stcb->asoc.hb_timer.ep = (void *)new_inp; 2284f8829a4aSRandall Stewart stcb->asoc.dack_timer.ep = (void *)new_inp; 2285f8829a4aSRandall Stewart stcb->asoc.asconf_timer.ep = (void *)new_inp; 2286f8829a4aSRandall Stewart stcb->asoc.strreset_timer.ep = (void *)new_inp; 2287f8829a4aSRandall Stewart stcb->asoc.shut_guard_timer.ep = (void *)new_inp; 2288f8829a4aSRandall Stewart stcb->asoc.autoclose_timer.ep = (void *)new_inp; 2289f8829a4aSRandall Stewart stcb->asoc.delayed_event_timer.ep = (void *)new_inp; 2290851b7298SRandall Stewart stcb->asoc.delete_prim_timer.ep = (void *)new_inp; 2291f8829a4aSRandall Stewart /* now what about the nets? */ 2292f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2293f8829a4aSRandall Stewart net->pmtu_timer.ep = (void *)new_inp; 2294f8829a4aSRandall Stewart net->rxt_timer.ep = (void *)new_inp; 2295f8829a4aSRandall Stewart net->fr_timer.ep = (void *)new_inp; 2296f8829a4aSRandall Stewart } 2297f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(new_inp); 2298f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(old_inp); 2299f8829a4aSRandall Stewart } 2300f8829a4aSRandall Stewart 2301f8829a4aSRandall Stewart static int 230242551e99SRandall Stewart sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id) 2303f8829a4aSRandall Stewart { 2304f8829a4aSRandall Stewart struct sctppcbhead *head; 2305f8829a4aSRandall Stewart struct sctp_inpcb *t_inp; 230642551e99SRandall Stewart int fnd; 2307f8829a4aSRandall Stewart 2308f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport, 2309f8829a4aSRandall Stewart sctppcbinfo.hashmark)]; 2310f8829a4aSRandall Stewart LIST_FOREACH(t_inp, head, sctp_hash) { 2311f8829a4aSRandall Stewart if (t_inp->sctp_lport != lport) { 2312f8829a4aSRandall Stewart continue; 2313f8829a4aSRandall Stewart } 231442551e99SRandall Stewart /* is it in the VRF in question */ 231542551e99SRandall Stewart fnd = 0; 231642551e99SRandall Stewart if (t_inp->def_vrf_id == vrf_id) 231742551e99SRandall Stewart fnd = 1; 231842551e99SRandall Stewart if (!fnd) 231942551e99SRandall Stewart continue; 232042551e99SRandall Stewart 2321f8829a4aSRandall Stewart /* This one is in use. */ 2322f8829a4aSRandall Stewart /* check the v6/v4 binding issue */ 2323f8829a4aSRandall Stewart if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 232444b7479bSRandall Stewart SCTP_IPV6_V6ONLY(t_inp)) { 2325f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2326f8829a4aSRandall Stewart /* collision in V6 space */ 2327f8829a4aSRandall Stewart return (1); 2328f8829a4aSRandall Stewart } else { 2329f8829a4aSRandall Stewart /* inp is BOUND_V4 no conflict */ 2330f8829a4aSRandall Stewart continue; 2331f8829a4aSRandall Stewart } 2332f8829a4aSRandall Stewart } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2333f8829a4aSRandall Stewart /* t_inp is bound v4 and v6, conflict always */ 2334f8829a4aSRandall Stewart return (1); 2335f8829a4aSRandall Stewart } else { 2336f8829a4aSRandall Stewart /* t_inp is bound only V4 */ 2337f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 2338c105859eSRandall Stewart SCTP_IPV6_V6ONLY(inp)) { 2339f8829a4aSRandall Stewart /* no conflict */ 2340f8829a4aSRandall Stewart continue; 2341f8829a4aSRandall Stewart } 2342f8829a4aSRandall Stewart /* else fall through to conflict */ 2343f8829a4aSRandall Stewart } 2344f8829a4aSRandall Stewart return (1); 2345f8829a4aSRandall Stewart } 2346f8829a4aSRandall Stewart return (0); 2347f8829a4aSRandall Stewart } 2348f8829a4aSRandall Stewart 2349f8829a4aSRandall Stewart 2350f8829a4aSRandall Stewart 23511b649582SRandall Stewart /* sctp_ifap is used to bypass normal local address validation checks */ 2352f8829a4aSRandall Stewart int 235380fefe0aSRandall Stewart sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, 23541b649582SRandall Stewart struct sctp_ifa *sctp_ifap, struct thread *p) 2355f8829a4aSRandall Stewart { 2356f8829a4aSRandall Stewart /* bind a ep to a socket address */ 2357f8829a4aSRandall Stewart struct sctppcbhead *head; 2358f8829a4aSRandall Stewart struct sctp_inpcb *inp, *inp_tmp; 2359f8829a4aSRandall Stewart struct inpcb *ip_inp; 2360f8829a4aSRandall Stewart int bindall; 236118e198d3SRandall Stewart int prison = 0; 2362f8829a4aSRandall Stewart uint16_t lport; 2363f8829a4aSRandall Stewart int error; 236442551e99SRandall Stewart uint32_t vrf_id; 2365f8829a4aSRandall Stewart 2366f8829a4aSRandall Stewart lport = 0; 2367f8829a4aSRandall Stewart error = 0; 2368f8829a4aSRandall Stewart bindall = 1; 2369f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb; 2370f8829a4aSRandall Stewart ip_inp = (struct inpcb *)so->so_pcb; 2371f8829a4aSRandall Stewart #ifdef SCTP_DEBUG 2372f8829a4aSRandall Stewart if (addr) { 2373ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n", 2374f8829a4aSRandall Stewart ntohs(((struct sockaddr_in *)addr)->sin_port)); 2375ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Addr :"); 2376ad81507eSRandall Stewart SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); 2377f8829a4aSRandall Stewart } 2378ad81507eSRandall Stewart #endif 2379f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { 2380f8829a4aSRandall Stewart /* already did a bind, subsequent binds NOT allowed ! */ 2381c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2382f8829a4aSRandall Stewart return (EINVAL); 2383f8829a4aSRandall Stewart } 238452be287eSRandall Stewart #ifdef INVARIANTS 238552be287eSRandall Stewart if (p == NULL) 238652be287eSRandall Stewart panic("null proc/thread"); 238752be287eSRandall Stewart #endif 238852be287eSRandall Stewart if (p && jailed(p->td_ucred)) { 238918e198d3SRandall Stewart prison = 1; 239018e198d3SRandall Stewart } 2391f8829a4aSRandall Stewart if (addr != NULL) { 2392f8829a4aSRandall Stewart if (addr->sa_family == AF_INET) { 2393f8829a4aSRandall Stewart struct sockaddr_in *sin; 2394f8829a4aSRandall Stewart 2395f8829a4aSRandall Stewart /* IPV6_V6ONLY socket? */ 239644b7479bSRandall Stewart if (SCTP_IPV6_V6ONLY(ip_inp)) { 2397c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2398f8829a4aSRandall Stewart return (EINVAL); 2399f8829a4aSRandall Stewart } 2400c4739e2fSRandall Stewart if (addr->sa_len != sizeof(*sin)) { 2401c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2402f8829a4aSRandall Stewart return (EINVAL); 2403c4739e2fSRandall Stewart } 2404f8829a4aSRandall Stewart sin = (struct sockaddr_in *)addr; 2405f8829a4aSRandall Stewart lport = sin->sin_port; 240618e198d3SRandall Stewart if (prison) { 240718e198d3SRandall Stewart /* 240818e198d3SRandall Stewart * For INADDR_ANY and LOOPBACK the 240918e198d3SRandall Stewart * prison_ip() call will tranmute the ip 241018e198d3SRandall Stewart * address to the proper valie. 241118e198d3SRandall Stewart */ 2412c4739e2fSRandall Stewart if (prison_ip(p->td_ucred, 0, &sin->sin_addr.s_addr)) { 2413c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 241418e198d3SRandall Stewart return (EINVAL); 241518e198d3SRandall Stewart } 2416c4739e2fSRandall Stewart } 2417f8829a4aSRandall Stewart if (sin->sin_addr.s_addr != INADDR_ANY) { 2418f8829a4aSRandall Stewart bindall = 0; 2419f8829a4aSRandall Stewart } 2420f8829a4aSRandall Stewart } else if (addr->sa_family == AF_INET6) { 2421f8829a4aSRandall Stewart /* Only for pure IPv6 Address. (No IPv4 Mapped!) */ 2422f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 2423f8829a4aSRandall Stewart 2424f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)addr; 2425f8829a4aSRandall Stewart 2426c4739e2fSRandall Stewart if (addr->sa_len != sizeof(*sin6)) { 2427c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2428f8829a4aSRandall Stewart return (EINVAL); 2429c4739e2fSRandall Stewart } 2430f8829a4aSRandall Stewart lport = sin6->sin6_port; 243118e198d3SRandall Stewart /* 243218e198d3SRandall Stewart * Jail checks for IPv6 should go HERE! i.e. add the 243318e198d3SRandall Stewart * prison_ip() equivilant in this postion to 243418e198d3SRandall Stewart * transmute the addresses to the proper one jailed. 243518e198d3SRandall Stewart */ 2436f8829a4aSRandall Stewart if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 2437f8829a4aSRandall Stewart bindall = 0; 2438f8829a4aSRandall Stewart /* KAME hack: embed scopeid */ 2439c4739e2fSRandall Stewart if (sa6_embedscope(sin6, ip6_use_defzone) != 0) { 2440c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2441f8829a4aSRandall Stewart return (EINVAL); 2442f8829a4aSRandall Stewart } 2443c4739e2fSRandall Stewart } 2444f8829a4aSRandall Stewart /* this must be cleared for ifa_ifwithaddr() */ 2445f8829a4aSRandall Stewart sin6->sin6_scope_id = 0; 2446f8829a4aSRandall Stewart } else { 2447c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT); 2448f8829a4aSRandall Stewart return (EAFNOSUPPORT); 2449f8829a4aSRandall Stewart } 2450f8829a4aSRandall Stewart } 24513c503c28SRandall Stewart /* Setup a vrf_id to be the default for the non-bind-all case. */ 245242551e99SRandall Stewart vrf_id = inp->def_vrf_id; 245342551e99SRandall Stewart 2454f8829a4aSRandall Stewart SCTP_INP_INFO_WLOCK(); 2455f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 2456f8829a4aSRandall Stewart /* increase our count due to the unlock we do */ 2457f8829a4aSRandall Stewart SCTP_INP_INCR_REF(inp); 2458f8829a4aSRandall Stewart if (lport) { 2459f8829a4aSRandall Stewart /* 2460f8829a4aSRandall Stewart * Did the caller specify a port? if so we must see if a ep 2461f8829a4aSRandall Stewart * already has this one bound. 2462f8829a4aSRandall Stewart */ 2463f8829a4aSRandall Stewart /* got to be root to get at low ports */ 2464f8829a4aSRandall Stewart if (ntohs(lport) < IPPORT_RESERVED) { 246503b0b021SRandall Stewart if (p && (error = 246632f9753cSRobert Watson priv_check(p, PRIV_NETINET_RESERVEDPORT) 246703b0b021SRandall Stewart )) { 2468f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 2469f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2470f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2471f8829a4aSRandall Stewart return (error); 2472f8829a4aSRandall Stewart } 2473f8829a4aSRandall Stewart } 2474f8829a4aSRandall Stewart if (p == NULL) { 2475f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 2476f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2477f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2478c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2479f8829a4aSRandall Stewart return (error); 2480f8829a4aSRandall Stewart } 2481f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 248242551e99SRandall Stewart if (bindall) { 248342551e99SRandall Stewart vrf_id = inp->def_vrf_id; 248442551e99SRandall Stewart inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); 2485f8829a4aSRandall Stewart if (inp_tmp != NULL) { 2486f8829a4aSRandall Stewart /* 248742551e99SRandall Stewart * lock guy returned and lower count note 248842551e99SRandall Stewart * that we are not bound so inp_tmp should 248942551e99SRandall Stewart * NEVER be inp. And it is this inp 249042551e99SRandall Stewart * (inp_tmp) that gets the reference bump, 249142551e99SRandall Stewart * so we must lower it. 2492f8829a4aSRandall Stewart */ 2493f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp_tmp); 2494f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 2495f8829a4aSRandall Stewart /* unlock info */ 2496f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2497c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); 2498207304d4SRandall Stewart return (EADDRINUSE); 2499f8829a4aSRandall Stewart } 250042551e99SRandall Stewart } else { 250142551e99SRandall Stewart inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); 250242551e99SRandall Stewart if (inp_tmp != NULL) { 250342551e99SRandall Stewart /* 250442551e99SRandall Stewart * lock guy returned and lower count note 250542551e99SRandall Stewart * that we are not bound so inp_tmp should 250642551e99SRandall Stewart * NEVER be inp. And it is this inp 250742551e99SRandall Stewart * (inp_tmp) that gets the reference bump, 250842551e99SRandall Stewart * so we must lower it. 250942551e99SRandall Stewart */ 251042551e99SRandall Stewart SCTP_INP_DECR_REF(inp_tmp); 251142551e99SRandall Stewart SCTP_INP_DECR_REF(inp); 251242551e99SRandall Stewart /* unlock info */ 251342551e99SRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2514c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); 2515207304d4SRandall Stewart return (EADDRINUSE); 251642551e99SRandall Stewart } 251742551e99SRandall Stewart } 2518f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 2519f8829a4aSRandall Stewart if (bindall) { 2520f8829a4aSRandall Stewart /* verify that no lport is not used by a singleton */ 252142551e99SRandall Stewart if (sctp_isport_inuse(inp, lport, vrf_id)) { 2522f8829a4aSRandall Stewart /* Sorry someone already has this one bound */ 2523f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 2524f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2525f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2526c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); 2527207304d4SRandall Stewart return (EADDRINUSE); 2528f8829a4aSRandall Stewart } 2529f8829a4aSRandall Stewart } 2530f8829a4aSRandall Stewart } else { 25319a972525SRandall Stewart uint16_t first, last, candidate; 2532d61a0ae0SRandall Stewart uint16_t count; 2533d61a0ae0SRandall Stewart int done; 2534f8829a4aSRandall Stewart 2535d61a0ae0SRandall Stewart if (ip_inp->inp_flags & INP_HIGHPORT) { 2536d61a0ae0SRandall Stewart first = ipport_hifirstauto; 2537d61a0ae0SRandall Stewart last = ipport_hilastauto; 2538d61a0ae0SRandall Stewart } else if (ip_inp->inp_flags & INP_LOWPORT) { 2539d61a0ae0SRandall Stewart if (p && (error = 254032f9753cSRobert Watson priv_check(p, PRIV_NETINET_RESERVEDPORT) 2541d61a0ae0SRandall Stewart )) { 2542d61a0ae0SRandall Stewart SCTP_INP_DECR_REF(inp); 2543d61a0ae0SRandall Stewart SCTP_INP_WUNLOCK(inp); 2544d61a0ae0SRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2545c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); 2546d61a0ae0SRandall Stewart return (error); 2547d61a0ae0SRandall Stewart } 2548d61a0ae0SRandall Stewart first = ipport_lowfirstauto; 2549d61a0ae0SRandall Stewart last = ipport_lowlastauto; 2550d61a0ae0SRandall Stewart } else { 2551d61a0ae0SRandall Stewart first = ipport_firstauto; 2552d61a0ae0SRandall Stewart last = ipport_lastauto; 2553d61a0ae0SRandall Stewart } 2554d61a0ae0SRandall Stewart if (first > last) { 2555d61a0ae0SRandall Stewart uint16_t temp; 2556f8829a4aSRandall Stewart 2557d61a0ae0SRandall Stewart temp = first; 2558d61a0ae0SRandall Stewart first = last; 2559d61a0ae0SRandall Stewart last = temp; 2560f8829a4aSRandall Stewart } 2561d61a0ae0SRandall Stewart count = last - first + 1; /* number of candidates */ 25629a972525SRandall Stewart candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count); 25633c503c28SRandall Stewart 2564d61a0ae0SRandall Stewart done = 0; 2565d61a0ae0SRandall Stewart while (!done) { 25669a972525SRandall Stewart if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == 0) { 2567d61a0ae0SRandall Stewart done = 1; 2568f8829a4aSRandall Stewart } 2569d61a0ae0SRandall Stewart if (!done) { 2570d61a0ae0SRandall Stewart if (--count == 0) { 2571d61a0ae0SRandall Stewart SCTP_INP_DECR_REF(inp); 2572d61a0ae0SRandall Stewart SCTP_INP_WUNLOCK(inp); 2573d61a0ae0SRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2574c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); 2575207304d4SRandall Stewart return (EADDRINUSE); 2576f8829a4aSRandall Stewart } 25779a972525SRandall Stewart if (candidate == last) 25789a972525SRandall Stewart candidate = first; 2579d61a0ae0SRandall Stewart else 25809a972525SRandall Stewart candidate = candidate + 1; 2581f8829a4aSRandall Stewart } 2582f8829a4aSRandall Stewart } 25839a972525SRandall Stewart lport = htons(candidate); 2584f8829a4aSRandall Stewart } 2585f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 2586f8829a4aSRandall Stewart if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | 2587f8829a4aSRandall Stewart SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 2588f8829a4aSRandall Stewart /* 2589f8829a4aSRandall Stewart * this really should not happen. The guy did a non-blocking 2590f8829a4aSRandall Stewart * bind and then did a close at the same time. 2591f8829a4aSRandall Stewart */ 2592f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2593f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2594c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2595f8829a4aSRandall Stewart return (EINVAL); 2596f8829a4aSRandall Stewart } 2597f8829a4aSRandall Stewart /* ok we look clear to give out this port, so lets setup the binding */ 2598f8829a4aSRandall Stewart if (bindall) { 2599f8829a4aSRandall Stewart /* binding to all addresses, so just set in the proper flags */ 2600f8829a4aSRandall Stewart inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL; 2601f8829a4aSRandall Stewart /* set the automatic addr changes from kernel flag */ 2602f8829a4aSRandall Stewart if (sctp_auto_asconf == 0) { 2603c4739e2fSRandall Stewart sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF); 2604f8829a4aSRandall Stewart sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 2605f8829a4aSRandall Stewart } else { 2606c4739e2fSRandall Stewart sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); 2607f8829a4aSRandall Stewart sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 2608f8829a4aSRandall Stewart } 26092afb3e84SRandall Stewart /* 26102afb3e84SRandall Stewart * set the automatic mobility_base from kernel flag (by 26112afb3e84SRandall Stewart * micchie) 26122afb3e84SRandall Stewart */ 26132afb3e84SRandall Stewart if (sctp_mobility_base == 0) { 26142afb3e84SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE); 2615851b7298SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 26162afb3e84SRandall Stewart } else { 26172afb3e84SRandall Stewart sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE); 2618851b7298SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 26192afb3e84SRandall Stewart } 26202afb3e84SRandall Stewart /* 26212afb3e84SRandall Stewart * set the automatic mobility_fasthandoff from kernel flag 26222afb3e84SRandall Stewart * (by micchie) 26232afb3e84SRandall Stewart */ 26242afb3e84SRandall Stewart if (sctp_mobility_fasthandoff == 0) { 26252afb3e84SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF); 2626851b7298SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 26272afb3e84SRandall Stewart } else { 26282afb3e84SRandall Stewart sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF); 2629851b7298SRandall Stewart sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); 26302afb3e84SRandall Stewart } 2631f8829a4aSRandall Stewart } else { 2632f8829a4aSRandall Stewart /* 2633f8829a4aSRandall Stewart * bind specific, make sure flags is off and add a new 2634f8829a4aSRandall Stewart * address structure to the sctp_addr_list inside the ep 2635f8829a4aSRandall Stewart * structure. 2636f8829a4aSRandall Stewart * 2637f8829a4aSRandall Stewart * We will need to allocate one and insert it at the head. The 2638f8829a4aSRandall Stewart * socketopt call can just insert new addresses in there as 2639f8829a4aSRandall Stewart * well. It will also have to do the embed scope kame hack 2640f8829a4aSRandall Stewart * too (before adding). 2641f8829a4aSRandall Stewart */ 264242551e99SRandall Stewart struct sctp_ifa *ifa; 2643f8829a4aSRandall Stewart struct sockaddr_storage store_sa; 2644f8829a4aSRandall Stewart 2645f8829a4aSRandall Stewart memset(&store_sa, 0, sizeof(store_sa)); 2646f8829a4aSRandall Stewart if (addr->sa_family == AF_INET) { 2647f8829a4aSRandall Stewart struct sockaddr_in *sin; 2648f8829a4aSRandall Stewart 2649f8829a4aSRandall Stewart sin = (struct sockaddr_in *)&store_sa; 2650f8829a4aSRandall Stewart memcpy(sin, addr, sizeof(struct sockaddr_in)); 2651f8829a4aSRandall Stewart sin->sin_port = 0; 2652f8829a4aSRandall Stewart } else if (addr->sa_family == AF_INET6) { 2653f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 2654f8829a4aSRandall Stewart 2655f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)&store_sa; 2656f8829a4aSRandall Stewart memcpy(sin6, addr, sizeof(struct sockaddr_in6)); 2657f8829a4aSRandall Stewart sin6->sin6_port = 0; 2658f8829a4aSRandall Stewart } 2659f8829a4aSRandall Stewart /* 2660f8829a4aSRandall Stewart * first find the interface with the bound address need to 2661f8829a4aSRandall Stewart * zero out the port to find the address! yuck! can't do 2662f8829a4aSRandall Stewart * this earlier since need port for sctp_pcb_findep() 2663f8829a4aSRandall Stewart */ 26641b649582SRandall Stewart if (sctp_ifap != NULL) 26651b649582SRandall Stewart ifa = sctp_ifap; 26661b649582SRandall Stewart else { 26671b649582SRandall Stewart /* 26681b649582SRandall Stewart * Note for BSD we hit here always other O/S's will 26691b649582SRandall Stewart * pass things in via the sctp_ifap argument 26701b649582SRandall Stewart * (Panda). 26711b649582SRandall Stewart */ 26723c503c28SRandall Stewart ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa, 2673851b7298SRandall Stewart vrf_id, SCTP_ADDR_NOT_LOCKED); 26741b649582SRandall Stewart } 2675f8829a4aSRandall Stewart if (ifa == NULL) { 2676f8829a4aSRandall Stewart /* Can't find an interface with that address */ 2677f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2678f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2679c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL); 2680f8829a4aSRandall Stewart return (EADDRNOTAVAIL); 2681f8829a4aSRandall Stewart } 2682f8829a4aSRandall Stewart if (addr->sa_family == AF_INET6) { 268342551e99SRandall Stewart /* GAK, more FIXME IFA lock? */ 268442551e99SRandall Stewart if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 2685f8829a4aSRandall Stewart /* Can't bind a non-existent addr. */ 2686f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2687f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2688c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 2689f8829a4aSRandall Stewart return (EINVAL); 2690f8829a4aSRandall Stewart } 2691f8829a4aSRandall Stewart } 2692f8829a4aSRandall Stewart /* we're not bound all */ 2693f8829a4aSRandall Stewart inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL; 26943c503c28SRandall Stewart /* allow bindx() to send ASCONF's for binding changes */ 269542551e99SRandall Stewart sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); 269680fefe0aSRandall Stewart /* clear automatic addr changes from kernel flag */ 2697f8829a4aSRandall Stewart sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 269842551e99SRandall Stewart 2699f8829a4aSRandall Stewart /* add this address to the endpoint list */ 270042551e99SRandall Stewart error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0); 2701f8829a4aSRandall Stewart if (error != 0) { 2702f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2703f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2704f8829a4aSRandall Stewart return (error); 2705f8829a4aSRandall Stewart } 2706f8829a4aSRandall Stewart inp->laddr_count++; 2707f8829a4aSRandall Stewart } 2708f8829a4aSRandall Stewart /* find the bucket */ 2709f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport, 2710f8829a4aSRandall Stewart sctppcbinfo.hashmark)]; 2711f8829a4aSRandall Stewart /* put it in the bucket */ 2712f8829a4aSRandall Stewart LIST_INSERT_HEAD(head, inp, sctp_hash); 2713ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d\n", 2714ad81507eSRandall Stewart head, ntohs(lport)); 2715f8829a4aSRandall Stewart /* set in the port */ 2716f8829a4aSRandall Stewart inp->sctp_lport = lport; 2717f8829a4aSRandall Stewart 2718f8829a4aSRandall Stewart /* turn off just the unbound flag */ 2719f8829a4aSRandall Stewart inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; 2720f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 2721f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 2722f8829a4aSRandall Stewart return (0); 2723f8829a4aSRandall Stewart } 2724f8829a4aSRandall Stewart 2725f8829a4aSRandall Stewart 2726f8829a4aSRandall Stewart static void 2727f8829a4aSRandall Stewart sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next) 2728f8829a4aSRandall Stewart { 2729f8829a4aSRandall Stewart struct sctp_iterator *it; 2730f8829a4aSRandall Stewart 2731f8829a4aSRandall Stewart /* 2732f8829a4aSRandall Stewart * We enter with the only the ITERATOR_LOCK in place and a write 2733f8829a4aSRandall Stewart * lock on the inp_info stuff. 2734f8829a4aSRandall Stewart */ 2735f8829a4aSRandall Stewart 2736f8829a4aSRandall Stewart /* 2737f8829a4aSRandall Stewart * Go through all iterators, we must do this since it is possible 2738f8829a4aSRandall Stewart * that some iterator does NOT have the lock, but is waiting for it. 2739f8829a4aSRandall Stewart * And the one that had the lock has either moved in the last 2740f8829a4aSRandall Stewart * iteration or we just cleared it above. We need to find all of 2741f8829a4aSRandall Stewart * those guys. The list of iterators should never be very big 2742f8829a4aSRandall Stewart * though. 2743f8829a4aSRandall Stewart */ 274442551e99SRandall Stewart TAILQ_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) { 2745f8829a4aSRandall Stewart if (it == inp->inp_starting_point_for_iterator) 2746f8829a4aSRandall Stewart /* skip this guy, he's special */ 2747f8829a4aSRandall Stewart continue; 2748f8829a4aSRandall Stewart if (it->inp == inp) { 2749f8829a4aSRandall Stewart /* 2750f8829a4aSRandall Stewart * This is tricky and we DON'T lock the iterator. 2751f8829a4aSRandall Stewart * Reason is he's running but waiting for me since 2752f8829a4aSRandall Stewart * inp->inp_starting_point_for_iterator has the lock 2753f8829a4aSRandall Stewart * on me (the guy above we skipped). This tells us 2754f8829a4aSRandall Stewart * its is not running but waiting for 2755f8829a4aSRandall Stewart * inp->inp_starting_point_for_iterator to be 2756f8829a4aSRandall Stewart * released by the guy that does have our INP in a 2757f8829a4aSRandall Stewart * lock. 2758f8829a4aSRandall Stewart */ 2759f8829a4aSRandall Stewart if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 2760f8829a4aSRandall Stewart it->inp = NULL; 2761f8829a4aSRandall Stewart it->stcb = NULL; 2762f8829a4aSRandall Stewart } else { 2763f8829a4aSRandall Stewart /* set him up to do the next guy not me */ 2764f8829a4aSRandall Stewart it->inp = inp_next; 2765f8829a4aSRandall Stewart it->stcb = NULL; 2766f8829a4aSRandall Stewart } 2767f8829a4aSRandall Stewart } 2768f8829a4aSRandall Stewart } 2769f8829a4aSRandall Stewart it = inp->inp_starting_point_for_iterator; 2770f8829a4aSRandall Stewart if (it) { 2771f8829a4aSRandall Stewart if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 2772f8829a4aSRandall Stewart it->inp = NULL; 2773f8829a4aSRandall Stewart } else { 2774f8829a4aSRandall Stewart it->inp = inp_next; 2775f8829a4aSRandall Stewart } 2776f8829a4aSRandall Stewart it->stcb = NULL; 2777f8829a4aSRandall Stewart } 2778f8829a4aSRandall Stewart } 2779f8829a4aSRandall Stewart 2780f8829a4aSRandall Stewart /* release sctp_inpcb unbind the port */ 2781f8829a4aSRandall Stewart void 2782f8829a4aSRandall Stewart sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from) 2783f8829a4aSRandall Stewart { 2784f8829a4aSRandall Stewart /* 2785f8829a4aSRandall Stewart * Here we free a endpoint. We must find it (if it is in the Hash 2786f8829a4aSRandall Stewart * table) and remove it from there. Then we must also find it in the 2787f8829a4aSRandall Stewart * overall list and remove it from there. After all removals are 2788f8829a4aSRandall Stewart * complete then any timer has to be stopped. Then start the actual 2789f8829a4aSRandall Stewart * freeing. a) Any local lists. b) Any associations. c) The hash of 2790f8829a4aSRandall Stewart * all associations. d) finally the ep itself. 2791f8829a4aSRandall Stewart */ 2792f8829a4aSRandall Stewart struct sctp_pcb *m; 2793f8829a4aSRandall Stewart struct sctp_inpcb *inp_save; 2794f8829a4aSRandall Stewart struct sctp_tcb *asoc, *nasoc; 2795f8829a4aSRandall Stewart struct sctp_laddr *laddr, *nladdr; 2796f8829a4aSRandall Stewart struct inpcb *ip_pcb; 2797f8829a4aSRandall Stewart struct socket *so; 2798f8829a4aSRandall Stewart 2799f8829a4aSRandall Stewart struct sctp_queued_to_read *sq; 2800f8829a4aSRandall Stewart 2801bff64a4dSRandall Stewart 280293164cf9SRandall Stewart int cnt; 2803f8829a4aSRandall Stewart sctp_sharedkey_t *shared_key; 2804f8829a4aSRandall Stewart 2805f8829a4aSRandall Stewart 2806f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 2807f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 0); 2808f8829a4aSRandall Stewart #endif 2809f8829a4aSRandall Stewart SCTP_ITERATOR_LOCK(); 2810f8829a4aSRandall Stewart so = inp->sctp_socket; 2811f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { 2812f8829a4aSRandall Stewart /* been here before.. eeks.. get out of here */ 2813207304d4SRandall Stewart SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate); 2814f8829a4aSRandall Stewart SCTP_ITERATOR_UNLOCK(); 2815f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 2816f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 1); 2817f8829a4aSRandall Stewart #endif 2818f8829a4aSRandall Stewart return; 2819f8829a4aSRandall Stewart } 2820f8829a4aSRandall Stewart SCTP_ASOC_CREATE_LOCK(inp); 2821f8829a4aSRandall Stewart SCTP_INP_INFO_WLOCK(); 2822f8829a4aSRandall Stewart 2823f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 2824ad81507eSRandall Stewart /* First time through we have the socket lock, after that no more. */ 2825b0552ae2SRandall Stewart if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) { 2826f8829a4aSRandall Stewart /* 2827f8829a4aSRandall Stewart * Once we are in we can remove the flag from = 1 is only 2828f8829a4aSRandall Stewart * passed from the actual closing routines that are called 2829f8829a4aSRandall Stewart * via the sockets layer. 2830f8829a4aSRandall Stewart */ 2831f8829a4aSRandall Stewart inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP; 2832851b7298SRandall Stewart /* socket is gone, so no more wakeups allowed */ 2833851b7298SRandall Stewart inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE; 2834851b7298SRandall Stewart inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; 2835851b7298SRandall Stewart inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; 2836f8829a4aSRandall Stewart } 2837a5d547adSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL, 2838a5d547adSRandall Stewart SCTP_FROM_SCTP_PCB + SCTP_LOC_1); 2839f8829a4aSRandall Stewart 2840f8829a4aSRandall Stewart if (inp->control) { 2841f8829a4aSRandall Stewart sctp_m_freem(inp->control); 2842f8829a4aSRandall Stewart inp->control = NULL; 2843f8829a4aSRandall Stewart } 2844f8829a4aSRandall Stewart if (inp->pkt) { 2845f8829a4aSRandall Stewart sctp_m_freem(inp->pkt); 2846f8829a4aSRandall Stewart inp->pkt = NULL; 2847f8829a4aSRandall Stewart } 2848f8829a4aSRandall Stewart m = &inp->sctp_ep; 2849f8829a4aSRandall Stewart ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer 2850f8829a4aSRandall Stewart * here but I will be nice :> (i.e. 2851f8829a4aSRandall Stewart * ip_pcb = ep;) */ 2852b0552ae2SRandall Stewart if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { 2853f8829a4aSRandall Stewart int cnt_in_sd; 2854f8829a4aSRandall Stewart 2855f8829a4aSRandall Stewart cnt_in_sd = 0; 2856f8829a4aSRandall Stewart for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL; 2857f8829a4aSRandall Stewart asoc = nasoc) { 2858c4739e2fSRandall Stewart SCTP_TCB_LOCK(asoc); 2859ceaad40aSRandall Stewart nasoc = LIST_NEXT(asoc, sctp_tcblist); 2860f8829a4aSRandall Stewart if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 2861f8829a4aSRandall Stewart /* Skip guys being freed */ 2862ceaad40aSRandall Stewart /* asoc->sctp_socket = NULL; FIXME MT */ 2863f8829a4aSRandall Stewart cnt_in_sd++; 2864c4739e2fSRandall Stewart SCTP_TCB_UNLOCK(asoc); 2865f8829a4aSRandall Stewart continue; 2866f8829a4aSRandall Stewart } 2867f8829a4aSRandall Stewart if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) || 2868f8829a4aSRandall Stewart (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) { 286975298de2SRandall Stewart /* 287075298de2SRandall Stewart * If we have data in queue, we don't want 287175298de2SRandall Stewart * to just free since the app may have done, 287275298de2SRandall Stewart * send()/close or connect/send/close. And 287375298de2SRandall Stewart * it wants the data to get across first. 287475298de2SRandall Stewart */ 28752afb3e84SRandall Stewart /* Just abandon things in the front states */ 2876c4739e2fSRandall Stewart if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, 2877c4739e2fSRandall Stewart SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) { 2878c4739e2fSRandall Stewart cnt_in_sd++; 2879c4739e2fSRandall Stewart } 2880f8829a4aSRandall Stewart continue; 2881f8829a4aSRandall Stewart } 2882f8829a4aSRandall Stewart /* Disconnect the socket please */ 2883f8829a4aSRandall Stewart asoc->sctp_socket = NULL; 2884f8829a4aSRandall Stewart asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET; 2885f8829a4aSRandall Stewart if ((asoc->asoc.size_on_reasm_queue > 0) || 2886f8829a4aSRandall Stewart (asoc->asoc.control_pdapi) || 2887f8829a4aSRandall Stewart (asoc->asoc.size_on_all_streams > 0) || 2888f8829a4aSRandall Stewart (so && (so->so_rcv.sb_cc > 0)) 2889f8829a4aSRandall Stewart ) { 2890f8829a4aSRandall Stewart /* Left with Data unread */ 2891f8829a4aSRandall Stewart struct mbuf *op_err; 2892f8829a4aSRandall Stewart 2893f8829a4aSRandall Stewart op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)), 2894f8829a4aSRandall Stewart 0, M_DONTWAIT, 1, MT_DATA); 2895f8829a4aSRandall Stewart if (op_err) { 2896f8829a4aSRandall Stewart /* Fill in the user initiated abort */ 2897f8829a4aSRandall Stewart struct sctp_paramhdr *ph; 2898f8829a4aSRandall Stewart uint32_t *ippp; 2899f8829a4aSRandall Stewart 2900139bc87fSRandall Stewart SCTP_BUF_LEN(op_err) = 2901f8829a4aSRandall Stewart sizeof(struct sctp_paramhdr) + sizeof(uint32_t); 2902f8829a4aSRandall Stewart ph = mtod(op_err, 2903f8829a4aSRandall Stewart struct sctp_paramhdr *); 2904f8829a4aSRandall Stewart ph->param_type = htons( 2905f8829a4aSRandall Stewart SCTP_CAUSE_USER_INITIATED_ABT); 2906139bc87fSRandall Stewart ph->param_length = htons(SCTP_BUF_LEN(op_err)); 2907f8829a4aSRandall Stewart ippp = (uint32_t *) (ph + 1); 2908a5d547adSRandall Stewart *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3); 2909f8829a4aSRandall Stewart } 2910a5d547adSRandall Stewart asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3; 2911d55b0b1bSRandall Stewart #if defined(SCTP_PANIC_ON_ABORT) 2912d55b0b1bSRandall Stewart panic("inpcb_free does an abort"); 2913d55b0b1bSRandall Stewart #endif 2914ceaad40aSRandall Stewart sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); 2915f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_aborted); 2916f8829a4aSRandall Stewart if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || 2917f8829a4aSRandall Stewart (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 2918f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 2919f8829a4aSRandall Stewart } 2920c4739e2fSRandall Stewart if (sctp_free_assoc(inp, asoc, 2921c4739e2fSRandall Stewart SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) { 2922c4739e2fSRandall Stewart cnt_in_sd++; 2923c4739e2fSRandall Stewart } 2924f8829a4aSRandall Stewart continue; 2925f8829a4aSRandall Stewart } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) && 2926f8829a4aSRandall Stewart TAILQ_EMPTY(&asoc->asoc.sent_queue) && 2927f8829a4aSRandall Stewart (asoc->asoc.stream_queue_cnt == 0) 2928f8829a4aSRandall Stewart ) { 2929f8829a4aSRandall Stewart if (asoc->asoc.locked_on_sending) { 2930f8829a4aSRandall Stewart goto abort_anyway; 2931f8829a4aSRandall Stewart } 2932f8829a4aSRandall Stewart if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) && 2933f8829a4aSRandall Stewart (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 2934f8829a4aSRandall Stewart /* 2935f8829a4aSRandall Stewart * there is nothing queued to send, 2936f8829a4aSRandall Stewart * so I send shutdown 2937f8829a4aSRandall Stewart */ 2938f8829a4aSRandall Stewart sctp_send_shutdown(asoc, asoc->asoc.primary_destination); 2939f42a358aSRandall Stewart if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || 2940f42a358aSRandall Stewart (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 2941f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 2942f42a358aSRandall Stewart } 2943c4739e2fSRandall Stewart SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT); 2944f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc, 2945f8829a4aSRandall Stewart asoc->asoc.primary_destination); 2946f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, 2947f8829a4aSRandall Stewart asoc->asoc.primary_destination); 2948ceaad40aSRandall Stewart sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED); 2949f8829a4aSRandall Stewart } 2950f8829a4aSRandall Stewart } else { 2951f8829a4aSRandall Stewart /* mark into shutdown pending */ 2952f8829a4aSRandall Stewart struct sctp_stream_queue_pending *sp; 2953f8829a4aSRandall Stewart 2954f8829a4aSRandall Stewart asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING; 2955f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, 2956f8829a4aSRandall Stewart asoc->asoc.primary_destination); 2957f8829a4aSRandall Stewart if (asoc->asoc.locked_on_sending) { 2958f8829a4aSRandall Stewart sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue), 2959f8829a4aSRandall Stewart sctp_streamhead); 2960f8829a4aSRandall Stewart if (sp == NULL) { 2961ad81507eSRandall Stewart SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n", 296250514179SJohn Birrell asoc->asoc.locked_on_sending, 2963f8829a4aSRandall Stewart asoc->asoc.locked_on_sending->stream_no); 2964f8829a4aSRandall Stewart } else { 2965f8829a4aSRandall Stewart if ((sp->length == 0) && (sp->msg_is_complete == 0)) 2966f8829a4aSRandall Stewart asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT; 2967f8829a4aSRandall Stewart } 2968f8829a4aSRandall Stewart } 2969f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->asoc.send_queue) && 2970f8829a4aSRandall Stewart TAILQ_EMPTY(&asoc->asoc.sent_queue) && 2971f8829a4aSRandall Stewart (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 2972f8829a4aSRandall Stewart struct mbuf *op_err; 2973f8829a4aSRandall Stewart 2974f8829a4aSRandall Stewart abort_anyway: 2975f8829a4aSRandall Stewart op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)), 2976f8829a4aSRandall Stewart 0, M_DONTWAIT, 1, MT_DATA); 2977f8829a4aSRandall Stewart if (op_err) { 2978f8829a4aSRandall Stewart /* 2979f8829a4aSRandall Stewart * Fill in the user 2980f8829a4aSRandall Stewart * initiated abort 2981f8829a4aSRandall Stewart */ 2982f8829a4aSRandall Stewart struct sctp_paramhdr *ph; 2983f8829a4aSRandall Stewart uint32_t *ippp; 2984f8829a4aSRandall Stewart 2985139bc87fSRandall Stewart SCTP_BUF_LEN(op_err) = 2986f8829a4aSRandall Stewart (sizeof(struct sctp_paramhdr) + 2987f8829a4aSRandall Stewart sizeof(uint32_t)); 2988f8829a4aSRandall Stewart ph = mtod(op_err, 2989f8829a4aSRandall Stewart struct sctp_paramhdr *); 2990f8829a4aSRandall Stewart ph->param_type = htons( 2991f8829a4aSRandall Stewart SCTP_CAUSE_USER_INITIATED_ABT); 2992139bc87fSRandall Stewart ph->param_length = htons(SCTP_BUF_LEN(op_err)); 2993f8829a4aSRandall Stewart ippp = (uint32_t *) (ph + 1); 2994a5d547adSRandall Stewart *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5); 2995f8829a4aSRandall Stewart } 2996a5d547adSRandall Stewart asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5; 2997d55b0b1bSRandall Stewart #if defined(SCTP_PANIC_ON_ABORT) 2998d55b0b1bSRandall Stewart panic("inpcb_free does an abort"); 2999d55b0b1bSRandall Stewart #endif 3000d55b0b1bSRandall Stewart 3001ceaad40aSRandall Stewart sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); 3002f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_aborted); 3003f8829a4aSRandall Stewart if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || 3004f8829a4aSRandall Stewart (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 3005f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 3006f8829a4aSRandall Stewart } 3007c4739e2fSRandall Stewart if (sctp_free_assoc(inp, asoc, 3008c4739e2fSRandall Stewart SCTP_PCBFREE_NOFORCE, 3009c4739e2fSRandall Stewart SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) { 3010c4739e2fSRandall Stewart cnt_in_sd++; 3011c4739e2fSRandall Stewart } 3012f8829a4aSRandall Stewart continue; 30132afb3e84SRandall Stewart } else { 3014ceaad40aSRandall Stewart sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); 3015f8829a4aSRandall Stewart } 3016f8829a4aSRandall Stewart } 3017f8829a4aSRandall Stewart cnt_in_sd++; 3018f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(asoc); 3019f8829a4aSRandall Stewart } 3020f8829a4aSRandall Stewart /* now is there some left in our SHUTDOWN state? */ 3021f8829a4aSRandall Stewart if (cnt_in_sd) { 3022f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 3023f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp); 3024f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 3025f8829a4aSRandall Stewart SCTP_ITERATOR_UNLOCK(); 3026f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 3027f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 2); 3028f8829a4aSRandall Stewart #endif 3029f8829a4aSRandall Stewart return; 3030f8829a4aSRandall Stewart } 3031f8829a4aSRandall Stewart } 3032f8829a4aSRandall Stewart inp->sctp_socket = NULL; 3033f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 3034f8829a4aSRandall Stewart SCTP_PCB_FLAGS_UNBOUND) { 3035f8829a4aSRandall Stewart /* 3036f8829a4aSRandall Stewart * ok, this guy has been bound. It's port is somewhere in 3037f8829a4aSRandall Stewart * the sctppcbinfo hash table. Remove it! 3038f8829a4aSRandall Stewart */ 3039f8829a4aSRandall Stewart LIST_REMOVE(inp, sctp_hash); 3040f8829a4aSRandall Stewart inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND; 3041f8829a4aSRandall Stewart } 3042f8829a4aSRandall Stewart /* 3043f8829a4aSRandall Stewart * If there is a timer running to kill us, forget it, since it may 3044f8829a4aSRandall Stewart * have a contest on the INP lock.. which would cause us to die ... 3045f8829a4aSRandall Stewart */ 3046f8829a4aSRandall Stewart cnt = 0; 3047f8829a4aSRandall Stewart for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL; 3048f8829a4aSRandall Stewart asoc = nasoc) { 3049f8829a4aSRandall Stewart nasoc = LIST_NEXT(asoc, sctp_tcblist); 3050f8829a4aSRandall Stewart if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 3051f8829a4aSRandall Stewart cnt++; 3052f8829a4aSRandall Stewart continue; 3053f8829a4aSRandall Stewart } 3054f8829a4aSRandall Stewart /* Free associations that are NOT killing us */ 3055f8829a4aSRandall Stewart SCTP_TCB_LOCK(asoc); 3056f8829a4aSRandall Stewart if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) && 3057f8829a4aSRandall Stewart ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) { 3058f8829a4aSRandall Stewart struct mbuf *op_err; 3059f8829a4aSRandall Stewart uint32_t *ippp; 3060f8829a4aSRandall Stewart 3061f8829a4aSRandall Stewart op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)), 3062f8829a4aSRandall Stewart 0, M_DONTWAIT, 1, MT_DATA); 3063f8829a4aSRandall Stewart if (op_err) { 3064f8829a4aSRandall Stewart /* Fill in the user initiated abort */ 3065f8829a4aSRandall Stewart struct sctp_paramhdr *ph; 3066f8829a4aSRandall Stewart 3067139bc87fSRandall Stewart SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) + 3068f8829a4aSRandall Stewart sizeof(uint32_t)); 3069f8829a4aSRandall Stewart ph = mtod(op_err, struct sctp_paramhdr *); 3070f8829a4aSRandall Stewart ph->param_type = htons( 3071f8829a4aSRandall Stewart SCTP_CAUSE_USER_INITIATED_ABT); 3072139bc87fSRandall Stewart ph->param_length = htons(SCTP_BUF_LEN(op_err)); 3073f8829a4aSRandall Stewart ippp = (uint32_t *) (ph + 1); 3074a5d547adSRandall Stewart *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7); 3075f8829a4aSRandall Stewart 3076f8829a4aSRandall Stewart } 3077a5d547adSRandall Stewart asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7; 3078ceaad40aSRandall Stewart sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); 3079f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_aborted); 3080f8829a4aSRandall Stewart } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 3081f8829a4aSRandall Stewart cnt++; 3082f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(asoc); 3083f8829a4aSRandall Stewart continue; 3084f8829a4aSRandall Stewart } 3085f8829a4aSRandall Stewart if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || 3086f8829a4aSRandall Stewart (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 3087f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 3088f8829a4aSRandall Stewart } 3089c4739e2fSRandall Stewart if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) { 3090c4739e2fSRandall Stewart cnt++; 3091c4739e2fSRandall Stewart } 3092f8829a4aSRandall Stewart } 3093f8829a4aSRandall Stewart if (cnt) { 3094f8829a4aSRandall Stewart /* Ok we have someone out there that will kill us */ 30956e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); 3096f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 3097f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp); 3098f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 3099f8829a4aSRandall Stewart SCTP_ITERATOR_UNLOCK(); 3100f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 3101f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 3); 3102f8829a4aSRandall Stewart #endif 3103f8829a4aSRandall Stewart return; 3104f8829a4aSRandall Stewart } 3105f8829a4aSRandall Stewart if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) { 31066e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); 3107f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL); 3108f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 3109f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp); 3110f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 3111f8829a4aSRandall Stewart SCTP_ITERATOR_UNLOCK(); 3112f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 3113f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 4); 3114f8829a4aSRandall Stewart #endif 3115f8829a4aSRandall Stewart return; 3116f8829a4aSRandall Stewart } 31176e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); 3118f8829a4aSRandall Stewart inp->sctp_ep.signature_change.type = 0; 3119f8829a4aSRandall Stewart inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE; 3120f8829a4aSRandall Stewart 3121f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 3122f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 5); 3123f8829a4aSRandall Stewart #endif 3124f8829a4aSRandall Stewart 31256e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); 3126f8829a4aSRandall Stewart inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE; 3127f8829a4aSRandall Stewart /* Clear the read queue */ 31283c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 3129f8829a4aSRandall Stewart while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) { 3130f1f73e57SRandall Stewart /* Its only abandoned if it had data left */ 3131f1f73e57SRandall Stewart if (sq->length) 3132f1f73e57SRandall Stewart SCTP_STAT_INCR(sctps_left_abandon); 3133f1f73e57SRandall Stewart 3134f8829a4aSRandall Stewart TAILQ_REMOVE(&inp->read_queue, sq, next); 3135f8829a4aSRandall Stewart sctp_free_remote_addr(sq->whoFrom); 3136f8829a4aSRandall Stewart if (so) 3137f8829a4aSRandall Stewart so->so_rcv.sb_cc -= sq->length; 3138f8829a4aSRandall Stewart if (sq->data) { 3139f8829a4aSRandall Stewart sctp_m_freem(sq->data); 3140f8829a4aSRandall Stewart sq->data = NULL; 3141f8829a4aSRandall Stewart } 3142f8829a4aSRandall Stewart /* 3143f8829a4aSRandall Stewart * no need to free the net count, since at this point all 3144f8829a4aSRandall Stewart * assoc's are gone. 3145f8829a4aSRandall Stewart */ 3146f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq); 3147f8829a4aSRandall Stewart SCTP_DECR_READQ_COUNT(); 3148f8829a4aSRandall Stewart } 3149f8829a4aSRandall Stewart /* Now the sctp_pcb things */ 3150f8829a4aSRandall Stewart /* 3151f8829a4aSRandall Stewart * free each asoc if it is not already closed/free. we can't use the 3152f8829a4aSRandall Stewart * macro here since le_next will get freed as part of the 3153f8829a4aSRandall Stewart * sctp_free_assoc() call. 3154f8829a4aSRandall Stewart */ 3155f8829a4aSRandall Stewart cnt = 0; 3156f8829a4aSRandall Stewart if (so) { 3157b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 3158f8829a4aSRandall Stewart ipsec4_delete_pcbpolicy(ip_pcb); 3159b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */ 3160f8829a4aSRandall Stewart 3161f8829a4aSRandall Stewart /* Unlocks not needed since the socket is gone now */ 3162f8829a4aSRandall Stewart } 3163f8829a4aSRandall Stewart if (ip_pcb->inp_options) { 3164f8829a4aSRandall Stewart (void)sctp_m_free(ip_pcb->inp_options); 3165f8829a4aSRandall Stewart ip_pcb->inp_options = 0; 3166f8829a4aSRandall Stewart } 3167f8829a4aSRandall Stewart if (ip_pcb->inp_moptions) { 316871498f30SBruce M Simpson inp_freemoptions(ip_pcb->inp_moptions); 3169f8829a4aSRandall Stewart ip_pcb->inp_moptions = 0; 3170f8829a4aSRandall Stewart } 3171f8829a4aSRandall Stewart #ifdef INET6 3172f8829a4aSRandall Stewart if (ip_pcb->inp_vflag & INP_IPV6) { 3173f8829a4aSRandall Stewart struct in6pcb *in6p; 3174f8829a4aSRandall Stewart 3175f8829a4aSRandall Stewart in6p = (struct in6pcb *)inp; 3176f8829a4aSRandall Stewart ip6_freepcbopts(in6p->in6p_outputopts); 3177f8829a4aSRandall Stewart } 3178f8829a4aSRandall Stewart #endif /* INET6 */ 3179f8829a4aSRandall Stewart ip_pcb->inp_vflag = 0; 3180f8829a4aSRandall Stewart /* free up authentication fields */ 3181f8829a4aSRandall Stewart if (inp->sctp_ep.local_auth_chunks != NULL) 3182f8829a4aSRandall Stewart sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); 3183f8829a4aSRandall Stewart if (inp->sctp_ep.local_hmacs != NULL) 3184f8829a4aSRandall Stewart sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 3185f8829a4aSRandall Stewart 3186f8829a4aSRandall Stewart shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys); 3187f8829a4aSRandall Stewart while (shared_key) { 3188f8829a4aSRandall Stewart LIST_REMOVE(shared_key, next); 3189f8829a4aSRandall Stewart sctp_free_sharedkey(shared_key); 31903c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 3191f8829a4aSRandall Stewart shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys); 3192f8829a4aSRandall Stewart } 3193f8829a4aSRandall Stewart 3194f8829a4aSRandall Stewart inp_save = LIST_NEXT(inp, sctp_list); 3195f8829a4aSRandall Stewart LIST_REMOVE(inp, sctp_list); 3196f8829a4aSRandall Stewart 3197f8829a4aSRandall Stewart /* fix any iterators only after out of the list */ 3198f8829a4aSRandall Stewart sctp_iterator_inp_being_freed(inp, inp_save); 3199f8829a4aSRandall Stewart /* 3200f8829a4aSRandall Stewart * if we have an address list the following will free the list of 3201f8829a4aSRandall Stewart * ifaddr's that are set into this ep. Again macro limitations here, 3202f8829a4aSRandall Stewart * since the LIST_FOREACH could be a bad idea. 3203f8829a4aSRandall Stewart */ 3204f8829a4aSRandall Stewart for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL; 3205f8829a4aSRandall Stewart laddr = nladdr) { 3206f8829a4aSRandall Stewart nladdr = LIST_NEXT(laddr, sctp_nxt_addr); 320742551e99SRandall Stewart sctp_remove_laddr(laddr); 3208f8829a4aSRandall Stewart } 3209a5d547adSRandall Stewart 3210a5d547adSRandall Stewart #ifdef SCTP_TRACK_FREED_ASOCS 3211a5d547adSRandall Stewart /* TEMP CODE */ 3212a5d547adSRandall Stewart for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL; 3213a5d547adSRandall Stewart asoc = nasoc) { 3214a5d547adSRandall Stewart nasoc = LIST_NEXT(asoc, sctp_tcblist); 3215a5d547adSRandall Stewart LIST_REMOVE(asoc, sctp_tcblist); 3216a5d547adSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc); 3217a5d547adSRandall Stewart SCTP_DECR_ASOC_COUNT(); 3218a5d547adSRandall Stewart } 3219a5d547adSRandall Stewart /* *** END TEMP CODE *** */ 3220a5d547adSRandall Stewart #endif 3221f8829a4aSRandall Stewart /* Now lets see about freeing the EP hash table. */ 3222f8829a4aSRandall Stewart if (inp->sctp_tcbhash != NULL) { 322344b7479bSRandall Stewart SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); 322444b7479bSRandall Stewart inp->sctp_tcbhash = NULL; 3225f8829a4aSRandall Stewart } 3226f8829a4aSRandall Stewart /* Now we must put the ep memory back into the zone pool */ 3227e1461651SRandall Stewart INP_LOCK_DESTROY(&inp->ip_inp.inp); 3228f8829a4aSRandall Stewart SCTP_INP_LOCK_DESTROY(inp); 3229f8829a4aSRandall Stewart SCTP_INP_READ_DESTROY(inp); 3230f8829a4aSRandall Stewart SCTP_ASOC_CREATE_LOCK_DESTROY(inp); 3231f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 3232f8829a4aSRandall Stewart SCTP_ITERATOR_UNLOCK(); 3233f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp); 3234f8829a4aSRandall Stewart SCTP_DECR_EP_COUNT(); 3235f8829a4aSRandall Stewart } 3236f8829a4aSRandall Stewart 3237f8829a4aSRandall Stewart 3238f8829a4aSRandall Stewart struct sctp_nets * 3239f8829a4aSRandall Stewart sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) 3240f8829a4aSRandall Stewart { 3241f8829a4aSRandall Stewart struct sctp_nets *net; 3242f8829a4aSRandall Stewart 3243f8829a4aSRandall Stewart /* locate the address */ 3244f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3245f8829a4aSRandall Stewart if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) 3246f8829a4aSRandall Stewart return (net); 3247f8829a4aSRandall Stewart } 3248f8829a4aSRandall Stewart return (NULL); 3249f8829a4aSRandall Stewart } 3250f8829a4aSRandall Stewart 3251f8829a4aSRandall Stewart 3252f8829a4aSRandall Stewart int 325342551e99SRandall Stewart sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) 3254f8829a4aSRandall Stewart { 325542551e99SRandall Stewart struct sctp_ifa *sctp_ifa; 3256f8829a4aSRandall Stewart 3257851b7298SRandall Stewart sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED); 325842551e99SRandall Stewart if (sctp_ifa) { 3259f8829a4aSRandall Stewart return (1); 326042551e99SRandall Stewart } else { 3261f8829a4aSRandall Stewart return (0); 3262f8829a4aSRandall Stewart } 326342551e99SRandall Stewart } 326442551e99SRandall Stewart 3265ad81507eSRandall Stewart /* 3266ad81507eSRandall Stewart * add's a remote endpoint address, done with the INIT/INIT-ACK as well as 3267ad81507eSRandall Stewart * when a ASCONF arrives that adds it. It will also initialize all the cwnd 3268ad81507eSRandall Stewart * stats of stuff. 3269ad81507eSRandall Stewart */ 3270f8829a4aSRandall Stewart int 3271f8829a4aSRandall Stewart sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, 3272f8829a4aSRandall Stewart int set_scope, int from) 3273f8829a4aSRandall Stewart { 3274f8829a4aSRandall Stewart /* 3275f8829a4aSRandall Stewart * The following is redundant to the same lines in the 3276f8829a4aSRandall Stewart * sctp_aloc_assoc() but is needed since other's call the add 3277f8829a4aSRandall Stewart * address function 3278f8829a4aSRandall Stewart */ 3279f8829a4aSRandall Stewart struct sctp_nets *net, *netfirst; 3280f8829a4aSRandall Stewart int addr_inscope; 3281f8829a4aSRandall Stewart 3282ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ", 3283ad81507eSRandall Stewart from); 3284ad81507eSRandall Stewart SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr); 3285f8829a4aSRandall Stewart 3286f8829a4aSRandall Stewart netfirst = sctp_findnet(stcb, newaddr); 3287f8829a4aSRandall Stewart if (netfirst) { 3288f8829a4aSRandall Stewart /* 3289f8829a4aSRandall Stewart * Lie and return ok, we don't want to make the association 3290f8829a4aSRandall Stewart * go away for this behavior. It will happen in the TCP 3291f8829a4aSRandall Stewart * model in a connected socket. It does not reach the hash 3292f8829a4aSRandall Stewart * table until after the association is built so it can't be 3293f8829a4aSRandall Stewart * found. Mark as reachable, since the initial creation will 3294f8829a4aSRandall Stewart * have been cleared and the NOT_IN_ASSOC flag will have 3295f8829a4aSRandall Stewart * been added... and we don't want to end up removing it 3296f8829a4aSRandall Stewart * back out. 3297f8829a4aSRandall Stewart */ 3298f8829a4aSRandall Stewart if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { 3299f8829a4aSRandall Stewart netfirst->dest_state = (SCTP_ADDR_REACHABLE | 3300f8829a4aSRandall Stewart SCTP_ADDR_UNCONFIRMED); 3301f8829a4aSRandall Stewart } else { 3302f8829a4aSRandall Stewart netfirst->dest_state = SCTP_ADDR_REACHABLE; 3303f8829a4aSRandall Stewart } 3304f8829a4aSRandall Stewart 3305f8829a4aSRandall Stewart return (0); 3306f8829a4aSRandall Stewart } 3307f8829a4aSRandall Stewart addr_inscope = 1; 3308f8829a4aSRandall Stewart if (newaddr->sa_family == AF_INET) { 3309f8829a4aSRandall Stewart struct sockaddr_in *sin; 3310f8829a4aSRandall Stewart 3311f8829a4aSRandall Stewart sin = (struct sockaddr_in *)newaddr; 3312f8829a4aSRandall Stewart if (sin->sin_addr.s_addr == 0) { 3313f8829a4aSRandall Stewart /* Invalid address */ 3314f8829a4aSRandall Stewart return (-1); 3315f8829a4aSRandall Stewart } 3316f8829a4aSRandall Stewart /* zero out the bzero area */ 3317f8829a4aSRandall Stewart memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 3318f8829a4aSRandall Stewart 3319f8829a4aSRandall Stewart /* assure len is set */ 3320f8829a4aSRandall Stewart sin->sin_len = sizeof(struct sockaddr_in); 3321f8829a4aSRandall Stewart if (set_scope) { 3322f8829a4aSRandall Stewart #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE 3323f8829a4aSRandall Stewart stcb->ipv4_local_scope = 1; 3324f8829a4aSRandall Stewart #else 3325f8829a4aSRandall Stewart if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { 3326f8829a4aSRandall Stewart stcb->asoc.ipv4_local_scope = 1; 3327f8829a4aSRandall Stewart } 3328f8829a4aSRandall Stewart #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */ 3329f8829a4aSRandall Stewart } else { 3330f8829a4aSRandall Stewart /* Validate the address is in scope */ 3331f8829a4aSRandall Stewart if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && 3332f8829a4aSRandall Stewart (stcb->asoc.ipv4_local_scope == 0)) { 3333f8829a4aSRandall Stewart addr_inscope = 0; 3334f8829a4aSRandall Stewart } 3335f8829a4aSRandall Stewart } 3336f8829a4aSRandall Stewart } else if (newaddr->sa_family == AF_INET6) { 3337f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 3338f8829a4aSRandall Stewart 3339f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)newaddr; 3340f8829a4aSRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 3341f8829a4aSRandall Stewart /* Invalid address */ 3342f8829a4aSRandall Stewart return (-1); 3343f8829a4aSRandall Stewart } 3344f8829a4aSRandall Stewart /* assure len is set */ 3345f8829a4aSRandall Stewart sin6->sin6_len = sizeof(struct sockaddr_in6); 3346f8829a4aSRandall Stewart if (set_scope) { 334742551e99SRandall Stewart if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { 3348f8829a4aSRandall Stewart stcb->asoc.loopback_scope = 1; 334942551e99SRandall Stewart stcb->asoc.local_scope = 0; 3350f8829a4aSRandall Stewart stcb->asoc.ipv4_local_scope = 1; 3351f8829a4aSRandall Stewart stcb->asoc.site_scope = 1; 3352f8829a4aSRandall Stewart } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 3353f8829a4aSRandall Stewart /* 3354f8829a4aSRandall Stewart * If the new destination is a LINK_LOCAL we 3355f8829a4aSRandall Stewart * must have common site scope. Don't set 3356f8829a4aSRandall Stewart * the local scope since we may not share 3357f8829a4aSRandall Stewart * all links, only loopback can do this. 3358f8829a4aSRandall Stewart * Links on the local network would also be 3359f8829a4aSRandall Stewart * on our private network for v4 too. 3360f8829a4aSRandall Stewart */ 3361f8829a4aSRandall Stewart stcb->asoc.ipv4_local_scope = 1; 3362f8829a4aSRandall Stewart stcb->asoc.site_scope = 1; 3363f8829a4aSRandall Stewart } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { 3364f8829a4aSRandall Stewart /* 3365f8829a4aSRandall Stewart * If the new destination is SITE_LOCAL then 3366f8829a4aSRandall Stewart * we must have site scope in common. 3367f8829a4aSRandall Stewart */ 3368f8829a4aSRandall Stewart stcb->asoc.site_scope = 1; 3369f8829a4aSRandall Stewart } 3370f8829a4aSRandall Stewart } else { 3371f8829a4aSRandall Stewart /* Validate the address is in scope */ 3372f8829a4aSRandall Stewart if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && 3373f8829a4aSRandall Stewart (stcb->asoc.loopback_scope == 0)) { 3374f8829a4aSRandall Stewart addr_inscope = 0; 3375f8829a4aSRandall Stewart } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && 3376f8829a4aSRandall Stewart (stcb->asoc.local_scope == 0)) { 3377f8829a4aSRandall Stewart addr_inscope = 0; 3378f8829a4aSRandall Stewart } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && 3379f8829a4aSRandall Stewart (stcb->asoc.site_scope == 0)) { 3380f8829a4aSRandall Stewart addr_inscope = 0; 3381f8829a4aSRandall Stewart } 3382f8829a4aSRandall Stewart } 3383f8829a4aSRandall Stewart } else { 3384f8829a4aSRandall Stewart /* not supported family type */ 3385f8829a4aSRandall Stewart return (-1); 3386f8829a4aSRandall Stewart } 3387f42a358aSRandall Stewart net = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net, struct sctp_nets); 3388f8829a4aSRandall Stewart if (net == NULL) { 3389f8829a4aSRandall Stewart return (-1); 3390f8829a4aSRandall Stewart } 3391f8829a4aSRandall Stewart SCTP_INCR_RADDR_COUNT(); 3392f8829a4aSRandall Stewart bzero(net, sizeof(*net)); 33936e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&net->start_time); 3394f8829a4aSRandall Stewart memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); 3395f8829a4aSRandall Stewart if (newaddr->sa_family == AF_INET) { 3396f8829a4aSRandall Stewart ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; 3397f8829a4aSRandall Stewart } else if (newaddr->sa_family == AF_INET6) { 3398f8829a4aSRandall Stewart ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; 3399f8829a4aSRandall Stewart } 340042551e99SRandall Stewart net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); 340142551e99SRandall Stewart if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { 340242551e99SRandall Stewart stcb->asoc.loopback_scope = 1; 340342551e99SRandall Stewart stcb->asoc.ipv4_local_scope = 1; 340442551e99SRandall Stewart stcb->asoc.local_scope = 0; 340542551e99SRandall Stewart stcb->asoc.site_scope = 1; 340642551e99SRandall Stewart addr_inscope = 1; 340742551e99SRandall Stewart } 3408f8829a4aSRandall Stewart net->failure_threshold = stcb->asoc.def_net_failure; 3409f8829a4aSRandall Stewart if (addr_inscope == 0) { 3410f8829a4aSRandall Stewart net->dest_state = (SCTP_ADDR_REACHABLE | 3411f8829a4aSRandall Stewart SCTP_ADDR_OUT_OF_SCOPE); 3412f8829a4aSRandall Stewart } else { 3413a5d547adSRandall Stewart if (from == SCTP_ADDR_IS_CONFIRMED) 3414a5d547adSRandall Stewart /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ 3415f8829a4aSRandall Stewart net->dest_state = SCTP_ADDR_REACHABLE; 3416f8829a4aSRandall Stewart else 3417f8829a4aSRandall Stewart net->dest_state = SCTP_ADDR_REACHABLE | 3418f8829a4aSRandall Stewart SCTP_ADDR_UNCONFIRMED; 3419f8829a4aSRandall Stewart } 3420108df27cSRandall Stewart /* 3421108df27cSRandall Stewart * We set this to 0, the timer code knows that this means its an 3422108df27cSRandall Stewart * initial value 3423108df27cSRandall Stewart */ 3424108df27cSRandall Stewart net->RTO = 0; 34259a972525SRandall Stewart net->RTO_measured = 0; 3426f8829a4aSRandall Stewart stcb->asoc.numnets++; 3427f8829a4aSRandall Stewart *(&net->ref_count) = 1; 3428f8829a4aSRandall Stewart net->tos_flowlabel = 0; 342942551e99SRandall Stewart #ifdef INET 3430f8829a4aSRandall Stewart if (newaddr->sa_family == AF_INET) 3431f8829a4aSRandall Stewart net->tos_flowlabel = stcb->asoc.default_tos; 3432f8829a4aSRandall Stewart #endif 343342551e99SRandall Stewart #ifdef INET6 3434f8829a4aSRandall Stewart if (newaddr->sa_family == AF_INET6) 3435f8829a4aSRandall Stewart net->tos_flowlabel = stcb->asoc.default_flowlabel; 3436f8829a4aSRandall Stewart #endif 3437f8829a4aSRandall Stewart /* Init the timer structure */ 3438139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); 3439139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&net->fr_timer.timer); 3440139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); 3441f8829a4aSRandall Stewart 3442f8829a4aSRandall Stewart /* Now generate a route for this guy */ 3443f8829a4aSRandall Stewart /* KAME hack: embed scopeid */ 3444f8829a4aSRandall Stewart if (newaddr->sa_family == AF_INET6) { 3445f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 3446f8829a4aSRandall Stewart 3447f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 3448f8829a4aSRandall Stewart (void)sa6_embedscope(sin6, ip6_use_defzone); 3449f8829a4aSRandall Stewart sin6->sin6_scope_id = 0; 3450f8829a4aSRandall Stewart } 3451ad21a364SRandall Stewart SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id); 3452bff64a4dSRandall Stewart 3453f8829a4aSRandall Stewart if (newaddr->sa_family == AF_INET6) { 3454f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 3455f8829a4aSRandall Stewart 3456f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; 3457f8829a4aSRandall Stewart (void)sa6_recoverscope(sin6); 3458f8829a4aSRandall Stewart } 345917205eccSRandall Stewart if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) { 346017205eccSRandall Stewart /* Get source address */ 346117205eccSRandall Stewart net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep, 346217205eccSRandall Stewart stcb, 346317205eccSRandall Stewart (sctp_route_t *) & net->ro, 346417205eccSRandall Stewart net, 346517205eccSRandall Stewart 0, 346617205eccSRandall Stewart stcb->asoc.vrf_id); 346717205eccSRandall Stewart /* Now get the interface MTU */ 346817205eccSRandall Stewart if (net->ro._s_addr && net->ro._s_addr->ifn_p) { 346917205eccSRandall Stewart net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p); 347017205eccSRandall Stewart } else { 347117205eccSRandall Stewart net->mtu = 0; 347217205eccSRandall Stewart } 347317205eccSRandall Stewart #ifdef SCTP_PRINT_FOR_B_AND_M 3474ad81507eSRandall Stewart SCTP_PRINTF("We have found an interface mtu of %d\n", net->mtu); 347517205eccSRandall Stewart #endif 347617205eccSRandall Stewart if (net->mtu == 0) { 347717205eccSRandall Stewart /* Huh ?? */ 347817205eccSRandall Stewart net->mtu = SCTP_DEFAULT_MTU; 347917205eccSRandall Stewart } else { 348017205eccSRandall Stewart uint32_t rmtu; 348117205eccSRandall Stewart 348217205eccSRandall Stewart rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt); 348317205eccSRandall Stewart #ifdef SCTP_PRINT_FOR_B_AND_M 3484ad81507eSRandall Stewart SCTP_PRINTF("The route mtu is %d\n", rmtu); 348517205eccSRandall Stewart #endif 348617205eccSRandall Stewart if (rmtu == 0) { 348717205eccSRandall Stewart /* 348817205eccSRandall Stewart * Start things off to match mtu of 348917205eccSRandall Stewart * interface please. 349017205eccSRandall Stewart */ 349117205eccSRandall Stewart SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa, 349217205eccSRandall Stewart net->ro.ro_rt, net->mtu); 349317205eccSRandall Stewart } else { 349417205eccSRandall Stewart /* 349517205eccSRandall Stewart * we take the route mtu over the interface, 349617205eccSRandall Stewart * since the route may be leading out the 349717205eccSRandall Stewart * loopback, or a different interface. 349817205eccSRandall Stewart */ 349917205eccSRandall Stewart net->mtu = rmtu; 350017205eccSRandall Stewart } 350117205eccSRandall Stewart } 3502a5d547adSRandall Stewart if (from == SCTP_ALLOC_ASOC) { 350317205eccSRandall Stewart #ifdef SCTP_PRINT_FOR_B_AND_M 3504ad81507eSRandall Stewart SCTP_PRINTF("New assoc sets mtu to :%d\n", net->mtu); 350517205eccSRandall Stewart #endif 3506f8829a4aSRandall Stewart stcb->asoc.smallest_mtu = net->mtu; 3507f8829a4aSRandall Stewart } 3508f8829a4aSRandall Stewart } else { 3509f8829a4aSRandall Stewart net->mtu = stcb->asoc.smallest_mtu; 3510f8829a4aSRandall Stewart } 3511f8829a4aSRandall Stewart if (stcb->asoc.smallest_mtu > net->mtu) { 351217205eccSRandall Stewart #ifdef SCTP_PRINT_FOR_B_AND_M 3513ad81507eSRandall Stewart SCTP_PRINTF("new address mtu:%d smaller than smallest:%d\n", 351417205eccSRandall Stewart net->mtu, stcb->asoc.smallest_mtu); 351517205eccSRandall Stewart #endif 3516f8829a4aSRandall Stewart stcb->asoc.smallest_mtu = net->mtu; 3517f8829a4aSRandall Stewart } 3518b54d3a6cSRandall Stewart /* JRS - Use the congestion control given in the CC module */ 3519b54d3a6cSRandall Stewart stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net); 3520f8829a4aSRandall Stewart 3521f8829a4aSRandall Stewart /* 3522f8829a4aSRandall Stewart * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning 3523f8829a4aSRandall Stewart * of assoc (2005/06/27, iyengar@cis.udel.edu) 3524f8829a4aSRandall Stewart */ 3525f8829a4aSRandall Stewart net->find_pseudo_cumack = 1; 3526f8829a4aSRandall Stewart net->find_rtx_pseudo_cumack = 1; 3527f8829a4aSRandall Stewart net->src_addr_selected = 0; 3528f8829a4aSRandall Stewart netfirst = TAILQ_FIRST(&stcb->asoc.nets); 3529f8829a4aSRandall Stewart if (net->ro.ro_rt == NULL) { 3530f8829a4aSRandall Stewart /* Since we have no route put it at the back */ 3531f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); 3532f8829a4aSRandall Stewart } else if (netfirst == NULL) { 3533f8829a4aSRandall Stewart /* We are the first one in the pool. */ 3534f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3535f8829a4aSRandall Stewart } else if (netfirst->ro.ro_rt == NULL) { 3536f8829a4aSRandall Stewart /* 3537f8829a4aSRandall Stewart * First one has NO route. Place this one ahead of the first 3538f8829a4aSRandall Stewart * one. 3539f8829a4aSRandall Stewart */ 3540f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3541f8829a4aSRandall Stewart } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) { 3542f8829a4aSRandall Stewart /* 3543f8829a4aSRandall Stewart * This one has a different interface than the one at the 3544f8829a4aSRandall Stewart * top of the list. Place it ahead. 3545f8829a4aSRandall Stewart */ 3546f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); 3547f8829a4aSRandall Stewart } else { 3548f8829a4aSRandall Stewart /* 3549f8829a4aSRandall Stewart * Ok we have the same interface as the first one. Move 3550f8829a4aSRandall Stewart * forward until we find either a) one with a NULL route... 3551f8829a4aSRandall Stewart * insert ahead of that b) one with a different ifp.. insert 3552f8829a4aSRandall Stewart * after that. c) end of the list.. insert at the tail. 3553f8829a4aSRandall Stewart */ 3554f8829a4aSRandall Stewart struct sctp_nets *netlook; 3555f8829a4aSRandall Stewart 3556f8829a4aSRandall Stewart do { 3557f8829a4aSRandall Stewart netlook = TAILQ_NEXT(netfirst, sctp_next); 3558f8829a4aSRandall Stewart if (netlook == NULL) { 3559f8829a4aSRandall Stewart /* End of the list */ 3560bff64a4dSRandall Stewart TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); 3561f8829a4aSRandall Stewart break; 3562f8829a4aSRandall Stewart } else if (netlook->ro.ro_rt == NULL) { 3563f8829a4aSRandall Stewart /* next one has NO route */ 3564f8829a4aSRandall Stewart TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); 3565f8829a4aSRandall Stewart break; 3566bff64a4dSRandall Stewart } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) { 3567f8829a4aSRandall Stewart TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, 3568f8829a4aSRandall Stewart net, sctp_next); 3569f8829a4aSRandall Stewart break; 3570f8829a4aSRandall Stewart } 3571f8829a4aSRandall Stewart /* Shift forward */ 3572f8829a4aSRandall Stewart netfirst = netlook; 3573f8829a4aSRandall Stewart } while (netlook != NULL); 3574f8829a4aSRandall Stewart } 3575f8829a4aSRandall Stewart 3576f8829a4aSRandall Stewart /* got to have a primary set */ 3577f8829a4aSRandall Stewart if (stcb->asoc.primary_destination == 0) { 3578f8829a4aSRandall Stewart stcb->asoc.primary_destination = net; 3579f8829a4aSRandall Stewart } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) && 3580f8829a4aSRandall Stewart (net->ro.ro_rt) && 3581f8829a4aSRandall Stewart ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { 3582f8829a4aSRandall Stewart /* No route to current primary adopt new primary */ 3583f8829a4aSRandall Stewart stcb->asoc.primary_destination = net; 3584f8829a4aSRandall Stewart } 3585f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, 3586f8829a4aSRandall Stewart net); 3587f8829a4aSRandall Stewart /* Validate primary is first */ 3588f8829a4aSRandall Stewart net = TAILQ_FIRST(&stcb->asoc.nets); 3589f8829a4aSRandall Stewart if ((net != stcb->asoc.primary_destination) && 3590f8829a4aSRandall Stewart (stcb->asoc.primary_destination)) { 3591f8829a4aSRandall Stewart /* 3592f8829a4aSRandall Stewart * first one on the list is NOT the primary sctp_cmpaddr() 3593f8829a4aSRandall Stewart * is much more efficent if the primary is the first on the 3594f8829a4aSRandall Stewart * list, make it so. 3595f8829a4aSRandall Stewart */ 3596f8829a4aSRandall Stewart TAILQ_REMOVE(&stcb->asoc.nets, 3597f8829a4aSRandall Stewart stcb->asoc.primary_destination, sctp_next); 3598f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&stcb->asoc.nets, 3599f8829a4aSRandall Stewart stcb->asoc.primary_destination, sctp_next); 3600f8829a4aSRandall Stewart } 3601f8829a4aSRandall Stewart return (0); 3602f8829a4aSRandall Stewart } 3603f8829a4aSRandall Stewart 3604f8829a4aSRandall Stewart 3605f8829a4aSRandall Stewart /* 3606f8829a4aSRandall Stewart * allocate an association and add it to the endpoint. The caller must be 3607f8829a4aSRandall Stewart * careful to add all additional addresses once they are know right away or 3608f8829a4aSRandall Stewart * else the assoc will be may experience a blackout scenario. 3609f8829a4aSRandall Stewart */ 3610f8829a4aSRandall Stewart struct sctp_tcb * 3611f8829a4aSRandall Stewart sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, 361252be287eSRandall Stewart int for_a_init, int *error, uint32_t override_tag, uint32_t vrf_id, 361352be287eSRandall Stewart struct thread *p 361452be287eSRandall Stewart ) 3615f8829a4aSRandall Stewart { 361652be287eSRandall Stewart /* note the p argument is only valid in unbound sockets */ 361752be287eSRandall Stewart 3618f8829a4aSRandall Stewart struct sctp_tcb *stcb; 3619f8829a4aSRandall Stewart struct sctp_association *asoc; 3620f8829a4aSRandall Stewart struct sctpasochead *head; 3621f8829a4aSRandall Stewart uint16_t rport; 3622f8829a4aSRandall Stewart int err; 3623f8829a4aSRandall Stewart 3624f8829a4aSRandall Stewart /* 3625f8829a4aSRandall Stewart * Assumption made here: Caller has done a 3626f8829a4aSRandall Stewart * sctp_findassociation_ep_addr(ep, addr's); to make sure the 3627f8829a4aSRandall Stewart * address does not exist already. 3628f8829a4aSRandall Stewart */ 3629f8829a4aSRandall Stewart if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) { 3630f8829a4aSRandall Stewart /* Hit max assoc, sorry no more */ 3631c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 3632f8829a4aSRandall Stewart *error = ENOBUFS; 3633f8829a4aSRandall Stewart return (NULL); 3634f8829a4aSRandall Stewart } 3635ad81507eSRandall Stewart if (firstaddr == NULL) { 3636c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 3637ad81507eSRandall Stewart *error = EINVAL; 3638ad81507eSRandall Stewart return (NULL); 3639ad81507eSRandall Stewart } 3640f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp); 3641f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { 3642f8829a4aSRandall Stewart /* 3643f8829a4aSRandall Stewart * If its in the TCP pool, its NOT allowed to create an 3644f8829a4aSRandall Stewart * association. The parent listener needs to call 3645f8829a4aSRandall Stewart * sctp_aloc_assoc.. or the one-2-many socket. If a peeled 3646f8829a4aSRandall Stewart * off, or connected one does this.. its an error. 3647f8829a4aSRandall Stewart */ 3648f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 3649c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 3650f8829a4aSRandall Stewart *error = EINVAL; 3651f8829a4aSRandall Stewart return (NULL); 3652f8829a4aSRandall Stewart } 3653ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:"); 3654f8829a4aSRandall Stewart #ifdef SCTP_DEBUG 36556e55db54SRandall Stewart if (firstaddr) { 3656ad81507eSRandall Stewart SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr); 3657ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", 3658f8829a4aSRandall Stewart ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); 3659ad81507eSRandall Stewart } else { 3660ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB3, "None\n"); 3661f8829a4aSRandall Stewart } 3662f8829a4aSRandall Stewart #endif /* SCTP_DEBUG */ 3663f8829a4aSRandall Stewart if (firstaddr->sa_family == AF_INET) { 3664f8829a4aSRandall Stewart struct sockaddr_in *sin; 3665f8829a4aSRandall Stewart 3666f8829a4aSRandall Stewart sin = (struct sockaddr_in *)firstaddr; 3667f8829a4aSRandall Stewart if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) { 3668f8829a4aSRandall Stewart /* Invalid address */ 3669f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 3670c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 3671f8829a4aSRandall Stewart *error = EINVAL; 3672f8829a4aSRandall Stewart return (NULL); 3673f8829a4aSRandall Stewart } 3674f8829a4aSRandall Stewart rport = sin->sin_port; 3675f8829a4aSRandall Stewart } else if (firstaddr->sa_family == AF_INET6) { 3676f8829a4aSRandall Stewart struct sockaddr_in6 *sin6; 3677f8829a4aSRandall Stewart 3678f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)firstaddr; 3679f8829a4aSRandall Stewart if ((sin6->sin6_port == 0) || 3680f8829a4aSRandall Stewart (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) { 3681f8829a4aSRandall Stewart /* Invalid address */ 3682f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 3683c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 3684f8829a4aSRandall Stewart *error = EINVAL; 3685f8829a4aSRandall Stewart return (NULL); 3686f8829a4aSRandall Stewart } 3687f8829a4aSRandall Stewart rport = sin6->sin6_port; 3688f8829a4aSRandall Stewart } else { 3689f8829a4aSRandall Stewart /* not supported family type */ 3690f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 3691c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 3692f8829a4aSRandall Stewart *error = EINVAL; 3693f8829a4aSRandall Stewart return (NULL); 3694f8829a4aSRandall Stewart } 3695f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 3696f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 3697f8829a4aSRandall Stewart /* 3698f8829a4aSRandall Stewart * If you have not performed a bind, then we need to do the 3699f8829a4aSRandall Stewart * ephemerial bind for you. 3700f8829a4aSRandall Stewart */ 3701f8829a4aSRandall Stewart if ((err = sctp_inpcb_bind(inp->sctp_socket, 370218e198d3SRandall Stewart (struct sockaddr *)NULL, 37031b649582SRandall Stewart (struct sctp_ifa *)NULL, 370452be287eSRandall Stewart p 3705f8829a4aSRandall Stewart ))) { 3706f8829a4aSRandall Stewart /* bind error, probably perm */ 3707f8829a4aSRandall Stewart *error = err; 3708f8829a4aSRandall Stewart return (NULL); 3709f8829a4aSRandall Stewart } 3710f8829a4aSRandall Stewart } 3711f42a358aSRandall Stewart stcb = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc, struct sctp_tcb); 3712f8829a4aSRandall Stewart if (stcb == NULL) { 3713f8829a4aSRandall Stewart /* out of memory? */ 3714c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); 3715f8829a4aSRandall Stewart *error = ENOMEM; 3716f8829a4aSRandall Stewart return (NULL); 3717f8829a4aSRandall Stewart } 3718f8829a4aSRandall Stewart SCTP_INCR_ASOC_COUNT(); 3719f8829a4aSRandall Stewart 3720f8829a4aSRandall Stewart bzero(stcb, sizeof(*stcb)); 3721f8829a4aSRandall Stewart asoc = &stcb->asoc; 3722f8829a4aSRandall Stewart SCTP_TCB_LOCK_INIT(stcb); 3723f8829a4aSRandall Stewart SCTP_TCB_SEND_LOCK_INIT(stcb); 3724f8829a4aSRandall Stewart /* setup back pointer's */ 3725f8829a4aSRandall Stewart stcb->sctp_ep = inp; 3726f8829a4aSRandall Stewart stcb->sctp_socket = inp->sctp_socket; 37270696e120SRandall Stewart if ((err = sctp_init_asoc(inp, stcb, for_a_init, override_tag, vrf_id))) { 3728f8829a4aSRandall Stewart /* failed */ 3729f8829a4aSRandall Stewart SCTP_TCB_LOCK_DESTROY(stcb); 3730f8829a4aSRandall Stewart SCTP_TCB_SEND_LOCK_DESTROY(stcb); 3731f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 3732f8829a4aSRandall Stewart SCTP_DECR_ASOC_COUNT(); 3733f8829a4aSRandall Stewart *error = err; 3734f8829a4aSRandall Stewart return (NULL); 3735f8829a4aSRandall Stewart } 3736f8829a4aSRandall Stewart /* and the port */ 3737f8829a4aSRandall Stewart stcb->rport = rport; 3738f8829a4aSRandall Stewart SCTP_INP_INFO_WLOCK(); 3739f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 3740f8829a4aSRandall Stewart if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 3741f8829a4aSRandall Stewart /* inpcb freed while alloc going on */ 3742f8829a4aSRandall Stewart SCTP_TCB_LOCK_DESTROY(stcb); 3743f8829a4aSRandall Stewart SCTP_TCB_SEND_LOCK_DESTROY(stcb); 3744f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 3745f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 3746f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 3747f8829a4aSRandall Stewart SCTP_DECR_ASOC_COUNT(); 3748c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 3749f8829a4aSRandall Stewart *error = EINVAL; 3750f8829a4aSRandall Stewart return (NULL); 3751f8829a4aSRandall Stewart } 3752f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 3753f8829a4aSRandall Stewart 3754f8829a4aSRandall Stewart /* now that my_vtag is set, add it to the hash */ 3755f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, 3756f8829a4aSRandall Stewart sctppcbinfo.hashasocmark)]; 3757f8829a4aSRandall Stewart /* put it in the bucket in the vtag hash of assoc's for the system */ 3758f8829a4aSRandall Stewart LIST_INSERT_HEAD(head, stcb, sctp_asocs); 3759851b7298SRandall Stewart sctp_delete_from_timewait(stcb->asoc.my_vtag); 3760851b7298SRandall Stewart 3761f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 3762f8829a4aSRandall Stewart 3763a5d547adSRandall Stewart if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) { 3764f8829a4aSRandall Stewart /* failure.. memory error? */ 3765207304d4SRandall Stewart if (asoc->strmout) { 3766207304d4SRandall Stewart SCTP_FREE(asoc->strmout, SCTP_M_STRMO); 3767207304d4SRandall Stewart asoc->strmout = NULL; 3768207304d4SRandall Stewart } 3769207304d4SRandall Stewart if (asoc->mapping_array) { 3770207304d4SRandall Stewart SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); 3771207304d4SRandall Stewart asoc->mapping_array = NULL; 3772207304d4SRandall Stewart } 3773f8829a4aSRandall Stewart SCTP_DECR_ASOC_COUNT(); 3774f8829a4aSRandall Stewart SCTP_TCB_LOCK_DESTROY(stcb); 3775f8829a4aSRandall Stewart SCTP_TCB_SEND_LOCK_DESTROY(stcb); 3776eacc51c5SRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 3777bff64a4dSRandall Stewart SCTP_INP_WUNLOCK(inp); 3778c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); 3779f8829a4aSRandall Stewart *error = ENOBUFS; 3780f8829a4aSRandall Stewart return (NULL); 3781f8829a4aSRandall Stewart } 3782f8829a4aSRandall Stewart /* Init all the timers */ 3783139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer); 3784139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); 3785139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); 3786139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); 3787139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); 3788139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); 3789139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer); 3790851b7298SRandall Stewart SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); 3791139bc87fSRandall Stewart 3792f8829a4aSRandall Stewart LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); 3793f8829a4aSRandall Stewart /* now file the port under the hash as well */ 3794f8829a4aSRandall Stewart if (inp->sctp_tcbhash != NULL) { 3795f8829a4aSRandall Stewart head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, 3796f8829a4aSRandall Stewart inp->sctp_hashmark)]; 3797f8829a4aSRandall Stewart LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); 3798f8829a4aSRandall Stewart } 3799f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 3800ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb); 3801f8829a4aSRandall Stewart return (stcb); 3802f8829a4aSRandall Stewart } 3803f8829a4aSRandall Stewart 3804f8829a4aSRandall Stewart 3805f8829a4aSRandall Stewart void 3806f8829a4aSRandall Stewart sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) 3807f8829a4aSRandall Stewart { 3808f8829a4aSRandall Stewart struct sctp_association *asoc; 3809f8829a4aSRandall Stewart 3810f8829a4aSRandall Stewart asoc = &stcb->asoc; 3811f8829a4aSRandall Stewart asoc->numnets--; 3812f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->nets, net, sctp_next); 3813f8829a4aSRandall Stewart if (net == asoc->primary_destination) { 3814f8829a4aSRandall Stewart /* Reset primary */ 3815f8829a4aSRandall Stewart struct sctp_nets *lnet; 3816f8829a4aSRandall Stewart 3817f8829a4aSRandall Stewart lnet = TAILQ_FIRST(&asoc->nets); 3818851b7298SRandall Stewart /* 3819851b7298SRandall Stewart * Mobility adaptation Ideally, if deleted destination is 3820851b7298SRandall Stewart * the primary, it becomes a fast retransmission trigger by 3821851b7298SRandall Stewart * the subsequent SET PRIMARY. (by micchie) 3822851b7298SRandall Stewart */ 3823851b7298SRandall Stewart if (sctp_is_mobility_feature_on(stcb->sctp_ep, 3824851b7298SRandall Stewart SCTP_MOBILITY_FASTHANDOFF)) { 3825851b7298SRandall Stewart SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); 3826851b7298SRandall Stewart if (asoc->deleted_primary != NULL) { 3827851b7298SRandall Stewart SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); 3828851b7298SRandall Stewart goto leave; 3829851b7298SRandall Stewart } 3830851b7298SRandall Stewart asoc->deleted_primary = net; 3831851b7298SRandall Stewart atomic_add_int(&net->ref_count, 1); 3832d55b0b1bSRandall Stewart memset(&net->lastsa, 0, sizeof(net->lastsa)); 3833d55b0b1bSRandall Stewart memset(&net->lastsv, 0, sizeof(net->lastsv)); 3834851b7298SRandall Stewart sctp_mobility_feature_on(stcb->sctp_ep, 3835851b7298SRandall Stewart SCTP_MOBILITY_PRIM_DELETED); 3836851b7298SRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, 3837851b7298SRandall Stewart stcb->sctp_ep, stcb, NULL); 3838851b7298SRandall Stewart } 3839851b7298SRandall Stewart leave: 3840f8829a4aSRandall Stewart /* Try to find a confirmed primary */ 384142551e99SRandall Stewart asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); 3842f8829a4aSRandall Stewart } 3843f8829a4aSRandall Stewart if (net == asoc->last_data_chunk_from) { 3844f8829a4aSRandall Stewart /* Reset primary */ 3845f8829a4aSRandall Stewart asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); 3846f8829a4aSRandall Stewart } 3847f8829a4aSRandall Stewart if (net == asoc->last_control_chunk_from) { 3848f8829a4aSRandall Stewart /* Clear net */ 3849f8829a4aSRandall Stewart asoc->last_control_chunk_from = NULL; 3850f8829a4aSRandall Stewart } 385142551e99SRandall Stewart sctp_free_remote_addr(net); 3852f8829a4aSRandall Stewart } 3853f8829a4aSRandall Stewart 3854f8829a4aSRandall Stewart /* 3855f8829a4aSRandall Stewart * remove a remote endpoint address from an association, it will fail if the 3856f8829a4aSRandall Stewart * address does not exist. 3857f8829a4aSRandall Stewart */ 3858f8829a4aSRandall Stewart int 3859f8829a4aSRandall Stewart sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) 3860f8829a4aSRandall Stewart { 3861f8829a4aSRandall Stewart /* 3862f8829a4aSRandall Stewart * Here we need to remove a remote address. This is quite simple, we 3863f8829a4aSRandall Stewart * first find it in the list of address for the association 3864f8829a4aSRandall Stewart * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE 3865f8829a4aSRandall Stewart * on that item. Note we do not allow it to be removed if there are 3866f8829a4aSRandall Stewart * no other addresses. 3867f8829a4aSRandall Stewart */ 3868f8829a4aSRandall Stewart struct sctp_association *asoc; 3869f8829a4aSRandall Stewart struct sctp_nets *net, *net_tmp; 3870f8829a4aSRandall Stewart 3871f8829a4aSRandall Stewart asoc = &stcb->asoc; 3872f8829a4aSRandall Stewart 3873f8829a4aSRandall Stewart /* locate the address */ 3874f8829a4aSRandall Stewart for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) { 3875f8829a4aSRandall Stewart net_tmp = TAILQ_NEXT(net, sctp_next); 3876f8829a4aSRandall Stewart if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { 3877f8829a4aSRandall Stewart continue; 3878f8829a4aSRandall Stewart } 3879f8829a4aSRandall Stewart if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, 3880f8829a4aSRandall Stewart remaddr)) { 3881f8829a4aSRandall Stewart /* we found the guy */ 3882f8829a4aSRandall Stewart if (asoc->numnets < 2) { 3883f8829a4aSRandall Stewart /* Must have at LEAST two remote addresses */ 3884f8829a4aSRandall Stewart return (-1); 3885f8829a4aSRandall Stewart } else { 3886f8829a4aSRandall Stewart sctp_remove_net(stcb, net); 3887f8829a4aSRandall Stewart return (0); 3888f8829a4aSRandall Stewart } 3889f8829a4aSRandall Stewart } 3890f8829a4aSRandall Stewart } 3891f8829a4aSRandall Stewart /* not found. */ 3892f8829a4aSRandall Stewart return (-2); 3893f8829a4aSRandall Stewart } 3894f8829a4aSRandall Stewart 3895851b7298SRandall Stewart void 3896851b7298SRandall Stewart sctp_delete_from_timewait(uint32_t tag) 3897851b7298SRandall Stewart { 3898851b7298SRandall Stewart struct sctpvtaghead *chain; 3899851b7298SRandall Stewart struct sctp_tagblock *twait_block; 3900851b7298SRandall Stewart int found = 0; 3901851b7298SRandall Stewart int i; 3902851b7298SRandall Stewart 3903851b7298SRandall Stewart chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 3904851b7298SRandall Stewart if (!SCTP_LIST_EMPTY(chain)) { 3905851b7298SRandall Stewart LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 3906851b7298SRandall Stewart for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 3907851b7298SRandall Stewart if (twait_block->vtag_block[i].v_tag == tag) { 3908851b7298SRandall Stewart twait_block->vtag_block[i].tv_sec_at_expire = 0; 3909851b7298SRandall Stewart twait_block->vtag_block[i].v_tag = 0; 3910851b7298SRandall Stewart found = 1; 3911851b7298SRandall Stewart break; 3912851b7298SRandall Stewart } 3913851b7298SRandall Stewart } 3914851b7298SRandall Stewart if (found) 3915851b7298SRandall Stewart break; 3916851b7298SRandall Stewart } 3917851b7298SRandall Stewart } 3918851b7298SRandall Stewart } 3919f8829a4aSRandall Stewart 392044b7479bSRandall Stewart void 3921851b7298SRandall Stewart sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time) 3922f8829a4aSRandall Stewart { 3923f8829a4aSRandall Stewart struct sctpvtaghead *chain; 3924f8829a4aSRandall Stewart struct sctp_tagblock *twait_block; 3925f8829a4aSRandall Stewart struct timeval now; 3926f8829a4aSRandall Stewart int set, i; 3927f8829a4aSRandall Stewart 39286e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&now); 3929f8829a4aSRandall Stewart chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 3930f8829a4aSRandall Stewart set = 0; 3931f42a358aSRandall Stewart if (!SCTP_LIST_EMPTY(chain)) { 3932f8829a4aSRandall Stewart /* Block(s) present, lets find space, and expire on the fly */ 3933f8829a4aSRandall Stewart LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 3934f8829a4aSRandall Stewart for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 3935f8829a4aSRandall Stewart if ((twait_block->vtag_block[i].v_tag == 0) && 3936f8829a4aSRandall Stewart !set) { 3937f8829a4aSRandall Stewart twait_block->vtag_block[i].tv_sec_at_expire = 393844b7479bSRandall Stewart now.tv_sec + time; 3939f8829a4aSRandall Stewart twait_block->vtag_block[i].v_tag = tag; 3940f8829a4aSRandall Stewart set = 1; 3941f8829a4aSRandall Stewart } else if ((twait_block->vtag_block[i].v_tag) && 3942f8829a4aSRandall Stewart ((long)twait_block->vtag_block[i].tv_sec_at_expire > 3943f8829a4aSRandall Stewart now.tv_sec)) { 3944f8829a4aSRandall Stewart /* Audit expires this guy */ 3945f8829a4aSRandall Stewart twait_block->vtag_block[i].tv_sec_at_expire = 0; 3946f8829a4aSRandall Stewart twait_block->vtag_block[i].v_tag = 0; 3947f8829a4aSRandall Stewart if (set == 0) { 3948f8829a4aSRandall Stewart /* Reuse it for my new tag */ 3949f8829a4aSRandall Stewart twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT; 3950f8829a4aSRandall Stewart twait_block->vtag_block[0].v_tag = tag; 3951f8829a4aSRandall Stewart set = 1; 3952f8829a4aSRandall Stewart } 3953f8829a4aSRandall Stewart } 3954f8829a4aSRandall Stewart } 3955f8829a4aSRandall Stewart if (set) { 3956f8829a4aSRandall Stewart /* 3957f8829a4aSRandall Stewart * We only do up to the block where we can 3958f8829a4aSRandall Stewart * place our tag for audits 3959f8829a4aSRandall Stewart */ 3960f8829a4aSRandall Stewart break; 3961f8829a4aSRandall Stewart } 3962f8829a4aSRandall Stewart } 3963f8829a4aSRandall Stewart } 3964f8829a4aSRandall Stewart /* Need to add a new block to chain */ 3965f8829a4aSRandall Stewart if (!set) { 3966f8829a4aSRandall Stewart SCTP_MALLOC(twait_block, struct sctp_tagblock *, 3967207304d4SRandall Stewart sizeof(struct sctp_tagblock), SCTP_M_TIMW); 3968f8829a4aSRandall Stewart if (twait_block == NULL) { 3969f8829a4aSRandall Stewart return; 3970f8829a4aSRandall Stewart } 397144b7479bSRandall Stewart memset(twait_block, 0, sizeof(struct sctp_tagblock)); 3972f8829a4aSRandall Stewart LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); 3973f8829a4aSRandall Stewart twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + 3974f8829a4aSRandall Stewart SCTP_TIME_WAIT; 3975f8829a4aSRandall Stewart twait_block->vtag_block[0].v_tag = tag; 3976f8829a4aSRandall Stewart } 3977f8829a4aSRandall Stewart } 3978f8829a4aSRandall Stewart 3979f8829a4aSRandall Stewart 3980f8829a4aSRandall Stewart static void 3981f8829a4aSRandall Stewart sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 3982f8829a4aSRandall Stewart { 3983f8829a4aSRandall Stewart struct sctp_iterator *it; 3984f8829a4aSRandall Stewart 3985f8829a4aSRandall Stewart /* 3986f8829a4aSRandall Stewart * Unlock the tcb lock we do this so we avoid a dead lock scenario 3987f8829a4aSRandall Stewart * where the iterator is waiting on the TCB lock and the TCB lock is 3988f8829a4aSRandall Stewart * waiting on the iterator lock. 3989f8829a4aSRandall Stewart */ 3990f8829a4aSRandall Stewart it = stcb->asoc.stcb_starting_point_for_iterator; 3991f8829a4aSRandall Stewart if (it == NULL) { 3992f8829a4aSRandall Stewart return; 3993f8829a4aSRandall Stewart } 3994f8829a4aSRandall Stewart if (it->inp != stcb->sctp_ep) { 3995f8829a4aSRandall Stewart /* hmm, focused on the wrong one? */ 3996f8829a4aSRandall Stewart return; 3997f8829a4aSRandall Stewart } 3998f8829a4aSRandall Stewart if (it->stcb != stcb) { 3999f8829a4aSRandall Stewart return; 4000f8829a4aSRandall Stewart } 4001f8829a4aSRandall Stewart it->stcb = LIST_NEXT(stcb, sctp_tcblist); 4002f8829a4aSRandall Stewart if (it->stcb == NULL) { 4003f8829a4aSRandall Stewart /* done with all asoc's in this assoc */ 4004f8829a4aSRandall Stewart if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 4005f8829a4aSRandall Stewart it->inp = NULL; 4006f8829a4aSRandall Stewart } else { 4007f8829a4aSRandall Stewart it->inp = LIST_NEXT(inp, sctp_list); 4008f8829a4aSRandall Stewart } 4009f8829a4aSRandall Stewart } 4010f8829a4aSRandall Stewart } 4011f8829a4aSRandall Stewart 4012bff64a4dSRandall Stewart 4013c4739e2fSRandall Stewart /*- 4014c4739e2fSRandall Stewart * Free the association after un-hashing the remote port. This 4015c4739e2fSRandall Stewart * function ALWAYS returns holding NO LOCK on the stcb. It DOES 4016c4739e2fSRandall Stewart * expect that the input to this function IS a locked TCB. 4017c4739e2fSRandall Stewart * It will return 0, if it did NOT destroy the association (instead 4018c4739e2fSRandall Stewart * it unlocks it. It will return NON-zero if it either destroyed the 4019c4739e2fSRandall Stewart * association OR the association is already destroyed. 4020f8829a4aSRandall Stewart */ 4021f8829a4aSRandall Stewart int 4022a5d547adSRandall Stewart sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) 4023f8829a4aSRandall Stewart { 4024f8829a4aSRandall Stewart int i; 4025f8829a4aSRandall Stewart struct sctp_association *asoc; 4026f8829a4aSRandall Stewart struct sctp_nets *net, *prev; 4027f8829a4aSRandall Stewart struct sctp_laddr *laddr; 4028f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 4029f8829a4aSRandall Stewart struct sctp_asconf_addr *aparam; 40302afb3e84SRandall Stewart struct sctp_asconf_ack *aack; 4031f8829a4aSRandall Stewart struct sctp_stream_reset_list *liste; 4032f8829a4aSRandall Stewart struct sctp_queued_to_read *sq; 4033f8829a4aSRandall Stewart struct sctp_stream_queue_pending *sp; 4034f8829a4aSRandall Stewart sctp_sharedkey_t *shared_key; 4035f8829a4aSRandall Stewart struct socket *so; 4036f8829a4aSRandall Stewart int ccnt = 0; 403793164cf9SRandall Stewart int cnt = 0; 4038f8829a4aSRandall Stewart 4039f8829a4aSRandall Stewart /* first, lets purge the entry from the hash table. */ 4040f8829a4aSRandall Stewart 4041f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 4042f8829a4aSRandall Stewart sctp_log_closing(inp, stcb, 6); 4043f8829a4aSRandall Stewart #endif 4044f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 4045f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 4046f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 7); 4047f8829a4aSRandall Stewart #endif 4048f8829a4aSRandall Stewart /* there is no asoc, really TSNH :-0 */ 4049f8829a4aSRandall Stewart return (1); 4050f8829a4aSRandall Stewart } 4051a5d547adSRandall Stewart /* TEMP CODE */ 4052a5d547adSRandall Stewart if (stcb->freed_from_where == 0) { 4053a5d547adSRandall Stewart /* Only record the first place free happened from */ 4054a5d547adSRandall Stewart stcb->freed_from_where = from_location; 4055a5d547adSRandall Stewart } 4056a5d547adSRandall Stewart /* TEMP CODE */ 4057a5d547adSRandall Stewart 4058f8829a4aSRandall Stewart asoc = &stcb->asoc; 4059f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4060f8829a4aSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4061f8829a4aSRandall Stewart /* nothing around */ 4062f8829a4aSRandall Stewart so = NULL; 4063f8829a4aSRandall Stewart else 4064f8829a4aSRandall Stewart so = inp->sctp_socket; 4065f8829a4aSRandall Stewart 4066f8829a4aSRandall Stewart /* 4067f8829a4aSRandall Stewart * We used timer based freeing if a reader or writer is in the way. 4068f8829a4aSRandall Stewart * So we first check if we are actually being called from a timer, 4069f8829a4aSRandall Stewart * if so we abort early if a reader or writer is still in the way. 4070f8829a4aSRandall Stewart */ 4071f8829a4aSRandall Stewart if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && 4072a5d547adSRandall Stewart (from_inpcbfree == SCTP_NORMAL_PROC)) { 4073f8829a4aSRandall Stewart /* 4074f8829a4aSRandall Stewart * is it the timer driving us? if so are the reader/writers 4075f8829a4aSRandall Stewart * gone? 4076f8829a4aSRandall Stewart */ 4077f8829a4aSRandall Stewart if (stcb->asoc.refcnt) { 4078f8829a4aSRandall Stewart /* nope, reader or writer in the way */ 4079f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4080f8829a4aSRandall Stewart /* no asoc destroyed */ 4081f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 4082f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 4083f8829a4aSRandall Stewart sctp_log_closing(inp, stcb, 8); 4084f8829a4aSRandall Stewart #endif 4085f8829a4aSRandall Stewart return (0); 4086f8829a4aSRandall Stewart } 4087f8829a4aSRandall Stewart } 4088f8829a4aSRandall Stewart /* now clean up any other timers */ 40896e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer); 4090c105859eSRandall Stewart asoc->hb_timer.self = NULL; 40916e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); 4092c105859eSRandall Stewart asoc->dack_timer.self = NULL; 40936e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 4094c105859eSRandall Stewart /*- 4095c105859eSRandall Stewart * For stream reset we don't blast this unless 4096c105859eSRandall Stewart * it is a str-reset timer, it might be the 4097c105859eSRandall Stewart * free-asoc timer which we DON'T want to 4098c105859eSRandall Stewart * disturb. 4099c105859eSRandall Stewart */ 4100c105859eSRandall Stewart if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET) 4101c105859eSRandall Stewart asoc->strreset_timer.self = NULL; 41026e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); 4103c105859eSRandall Stewart asoc->asconf_timer.self = NULL; 41046e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); 4105c105859eSRandall Stewart asoc->autoclose_timer.self = NULL; 41066e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer); 4107c105859eSRandall Stewart asoc->shut_guard_timer.self = NULL; 41086e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); 4109c105859eSRandall Stewart asoc->delayed_event_timer.self = NULL; 4110851b7298SRandall Stewart /* Mobility adaptation */ 4111851b7298SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer); 4112851b7298SRandall Stewart asoc->delete_prim_timer.self = NULL; 4113f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 41146e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer); 4115c105859eSRandall Stewart net->fr_timer.self = NULL; 41166e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer); 4117c105859eSRandall Stewart net->rxt_timer.self = NULL; 41186e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); 4119c105859eSRandall Stewart net->pmtu_timer.self = NULL; 4120f8829a4aSRandall Stewart } 4121f4ad963cSRandall Stewart /* Now the read queue needs to be cleaned up (only once) */ 4122a5d547adSRandall Stewart cnt = 0; 4123f4ad963cSRandall Stewart if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { 4124d06c82f1SRandall Stewart stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED; 4125f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 4126f8829a4aSRandall Stewart TAILQ_FOREACH(sq, &inp->read_queue, next) { 4127f8829a4aSRandall Stewart if (sq->stcb == stcb) { 4128f8829a4aSRandall Stewart sq->do_not_ref_stcb = 1; 4129f8829a4aSRandall Stewart sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; 4130f8829a4aSRandall Stewart /* 413103b0b021SRandall Stewart * If there is no end, there never will be 413203b0b021SRandall Stewart * now. 4133f8829a4aSRandall Stewart */ 413403b0b021SRandall Stewart if (sq->end_added == 0) { 413503b0b021SRandall Stewart /* Held for PD-API clear that. */ 413603b0b021SRandall Stewart sq->pdapi_aborted = 1; 4137f8829a4aSRandall Stewart sq->held_length = 0; 41382dad8a55SRandall Stewart if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { 4139f8829a4aSRandall Stewart /* 414003b0b021SRandall Stewart * Need to add a PD-API 414103b0b021SRandall Stewart * aborted indication. 414203b0b021SRandall Stewart * Setting the control_pdapi 414303b0b021SRandall Stewart * assures that it will be 414403b0b021SRandall Stewart * added right after this 414503b0b021SRandall Stewart * msg. 4146f8829a4aSRandall Stewart */ 41479a6142d8SRandall Stewart uint32_t strseq; 41489a6142d8SRandall Stewart 4149f8829a4aSRandall Stewart stcb->asoc.control_pdapi = sq; 41509a6142d8SRandall Stewart strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn; 4151f8829a4aSRandall Stewart sctp_notify_partial_delivery_indication(stcb, 41529a6142d8SRandall Stewart SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq); 4153f8829a4aSRandall Stewart stcb->asoc.control_pdapi = NULL; 4154f8829a4aSRandall Stewart } 4155f8829a4aSRandall Stewart } 415603b0b021SRandall Stewart /* Add an end to wake them */ 4157f8829a4aSRandall Stewart sq->end_added = 1; 4158f8829a4aSRandall Stewart cnt++; 4159f8829a4aSRandall Stewart } 4160f8829a4aSRandall Stewart } 4161f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 4162f8829a4aSRandall Stewart if (stcb->block_entry) { 4163a5d547adSRandall Stewart cnt++; 4164c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); 4165f8829a4aSRandall Stewart stcb->block_entry->error = ECONNRESET; 4166f8829a4aSRandall Stewart stcb->block_entry = NULL; 4167f8829a4aSRandall Stewart } 4168f4ad963cSRandall Stewart } 4169c4739e2fSRandall Stewart if (stcb->asoc.refcnt) { 4170f4ad963cSRandall Stewart /* 4171f4ad963cSRandall Stewart * reader or writer in the way, we have hopefully given him 4172f4ad963cSRandall Stewart * something to chew on above. 4173f4ad963cSRandall Stewart */ 4174a5d547adSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); 4175a5d547adSRandall Stewart SCTP_TCB_UNLOCK(stcb); 4176a5d547adSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4177a5d547adSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4178a5d547adSRandall Stewart /* nothing around */ 4179a5d547adSRandall Stewart so = NULL; 4180f4ad963cSRandall Stewart if (so) { 4181f4ad963cSRandall Stewart /* Wake any reader/writers */ 4182f4ad963cSRandall Stewart sctp_sorwakeup(inp, so); 4183f4ad963cSRandall Stewart sctp_sowwakeup(inp, so); 4184f4ad963cSRandall Stewart } 4185f4ad963cSRandall Stewart #ifdef SCTP_LOG_CLOSING 4186f4ad963cSRandall Stewart sctp_log_closing(inp, stcb, 9); 4187f4ad963cSRandall Stewart #endif 4188f4ad963cSRandall Stewart /* no asoc destroyed */ 4189f4ad963cSRandall Stewart return (0); 4190f4ad963cSRandall Stewart } 4191f4ad963cSRandall Stewart #ifdef SCTP_LOG_CLOSING 4192f4ad963cSRandall Stewart sctp_log_closing(inp, stcb, 10); 4193f4ad963cSRandall Stewart #endif 4194a5d547adSRandall Stewart /* 4195a5d547adSRandall Stewart * When I reach here, no others want to kill the assoc yet.. and I 4196a5d547adSRandall Stewart * own the lock. Now its possible an abort comes in when I do the 4197a5d547adSRandall Stewart * lock exchange below to grab all the locks to do the final take 4198a5d547adSRandall Stewart * out. to prevent this we increment the count, which will start a 4199a5d547adSRandall Stewart * timer and blow out above thus assuring us that we hold exclusive 4200a5d547adSRandall Stewart * killing of the asoc. Note that after getting back the TCB lock we 4201a5d547adSRandall Stewart * will go ahead and increment the counter back up and stop any 4202a5d547adSRandall Stewart * timer a passing stranger may have started :-S 4203a5d547adSRandall Stewart */ 4204a5d547adSRandall Stewart if (from_inpcbfree == SCTP_NORMAL_PROC) { 4205a5d547adSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 4206a5d547adSRandall Stewart 4207a5d547adSRandall Stewart SCTP_TCB_UNLOCK(stcb); 4208a5d547adSRandall Stewart 4209a5d547adSRandall Stewart SCTP_ITERATOR_LOCK(); 4210a5d547adSRandall Stewart SCTP_INP_INFO_WLOCK(); 4211a5d547adSRandall Stewart SCTP_INP_WLOCK(inp); 4212a5d547adSRandall Stewart SCTP_TCB_LOCK(stcb); 4213f8829a4aSRandall Stewart } 4214a5d547adSRandall Stewart /* Double check the GONE flag */ 4215a5d547adSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 4216a5d547adSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) 4217a5d547adSRandall Stewart /* nothing around */ 4218a5d547adSRandall Stewart so = NULL; 4219a5d547adSRandall Stewart 4220f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4221f8829a4aSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4222f8829a4aSRandall Stewart /* 4223f8829a4aSRandall Stewart * For TCP type we need special handling when we are 4224f8829a4aSRandall Stewart * connected. We also include the peel'ed off ones to. 4225f8829a4aSRandall Stewart */ 4226f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 4227f8829a4aSRandall Stewart inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; 4228f8829a4aSRandall Stewart inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; 4229f8829a4aSRandall Stewart if (so) { 4230f8829a4aSRandall Stewart SOCK_LOCK(so); 4231f8829a4aSRandall Stewart if (so->so_rcv.sb_cc == 0) { 4232f8829a4aSRandall Stewart so->so_state &= ~(SS_ISCONNECTING | 4233f8829a4aSRandall Stewart SS_ISDISCONNECTING | 4234f8829a4aSRandall Stewart SS_ISCONFIRMING | 4235f8829a4aSRandall Stewart SS_ISCONNECTED); 4236f8829a4aSRandall Stewart } 4237f8829a4aSRandall Stewart SOCK_UNLOCK(so); 4238f8829a4aSRandall Stewart sctp_sowwakeup(inp, so); 4239f8829a4aSRandall Stewart sctp_sorwakeup(inp, so); 424017205eccSRandall Stewart SCTP_SOWAKEUP(so); 4241f8829a4aSRandall Stewart } 4242f8829a4aSRandall Stewart } 4243f8829a4aSRandall Stewart } 4244f8829a4aSRandall Stewart /* 4245f8829a4aSRandall Stewart * Make it invalid too, that way if its about to run it will abort 4246f8829a4aSRandall Stewart * and return. 4247f8829a4aSRandall Stewart */ 4248f8829a4aSRandall Stewart sctp_iterator_asoc_being_freed(inp, stcb); 4249f8829a4aSRandall Stewart /* re-increment the lock */ 4250a5d547adSRandall Stewart if (from_inpcbfree == SCTP_NORMAL_PROC) { 425150cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 4252f8829a4aSRandall Stewart } 4253f8829a4aSRandall Stewart asoc->state = 0; 4254f8829a4aSRandall Stewart if (inp->sctp_tcbhash) { 4255f8829a4aSRandall Stewart LIST_REMOVE(stcb, sctp_tcbhash); 4256f8829a4aSRandall Stewart } 4257f8829a4aSRandall Stewart if (stcb->asoc.in_restart_hash) { 4258f8829a4aSRandall Stewart LIST_REMOVE(stcb, sctp_tcbrestarhash); 4259f8829a4aSRandall Stewart } 4260f8829a4aSRandall Stewart /* Now lets remove it from the list of ALL associations in the EP */ 4261f8829a4aSRandall Stewart LIST_REMOVE(stcb, sctp_tcblist); 4262a5d547adSRandall Stewart if (from_inpcbfree == SCTP_NORMAL_PROC) { 4263f8829a4aSRandall Stewart SCTP_INP_INCR_REF(inp); 4264f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 4265f8829a4aSRandall Stewart SCTP_ITERATOR_UNLOCK(); 4266f8829a4aSRandall Stewart } 4267f8829a4aSRandall Stewart /* pull from vtag hash */ 4268f8829a4aSRandall Stewart LIST_REMOVE(stcb, sctp_asocs); 4269851b7298SRandall Stewart sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_TIME_WAIT); 4270f8829a4aSRandall Stewart 427144b7479bSRandall Stewart /* 427244b7479bSRandall Stewart * Now restop the timers to be sure - this is paranoia at is finest! 427344b7479bSRandall Stewart */ 42746e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 42756e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer); 42766e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); 42776e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 42786e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); 42796e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer); 42806e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); 42816e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); 428244b7479bSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 42836e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer); 42846e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer); 42856e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); 428644b7479bSRandall Stewart } 428744b7479bSRandall Stewart 428844b7479bSRandall Stewart asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE; 4289f8829a4aSRandall Stewart prev = NULL; 4290f8829a4aSRandall Stewart /* 4291f8829a4aSRandall Stewart * The chunk lists and such SHOULD be empty but we check them just 4292f8829a4aSRandall Stewart * in case. 4293f8829a4aSRandall Stewart */ 4294f8829a4aSRandall Stewart /* anything on the wheel needs to be removed */ 4295f8829a4aSRandall Stewart for (i = 0; i < asoc->streamoutcnt; i++) { 4296f8829a4aSRandall Stewart struct sctp_stream_out *outs; 4297f8829a4aSRandall Stewart 4298f8829a4aSRandall Stewart outs = &asoc->strmout[i]; 4299f8829a4aSRandall Stewart /* now clean up any chunks here */ 4300f8829a4aSRandall Stewart sp = TAILQ_FIRST(&outs->outqueue); 4301f8829a4aSRandall Stewart while (sp) { 4302f8829a4aSRandall Stewart TAILQ_REMOVE(&outs->outqueue, sp, next); 4303f8829a4aSRandall Stewart if (sp->data) { 4304f8829a4aSRandall Stewart sctp_m_freem(sp->data); 4305f8829a4aSRandall Stewart sp->data = NULL; 4306f8829a4aSRandall Stewart sp->tail_mbuf = NULL; 4307f8829a4aSRandall Stewart } 4308f8829a4aSRandall Stewart sctp_free_remote_addr(sp->net); 4309f8829a4aSRandall Stewart sctp_free_spbufspace(stcb, asoc, sp); 4310f8829a4aSRandall Stewart /* Free the zone stuff */ 4311f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp); 4312f8829a4aSRandall Stewart SCTP_DECR_STRMOQ_COUNT(); 43133c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4314f8829a4aSRandall Stewart sp = TAILQ_FIRST(&outs->outqueue); 4315f8829a4aSRandall Stewart } 4316f8829a4aSRandall Stewart } 4317f8829a4aSRandall Stewart 43183c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4319f8829a4aSRandall Stewart while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) { 4320f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->resetHead, liste, next_resp); 4321207304d4SRandall Stewart SCTP_FREE(liste, SCTP_M_STRESET); 4322f8829a4aSRandall Stewart } 4323f8829a4aSRandall Stewart 4324f8829a4aSRandall Stewart sq = TAILQ_FIRST(&asoc->pending_reply_queue); 4325f8829a4aSRandall Stewart while (sq) { 4326f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); 4327f8829a4aSRandall Stewart if (sq->data) { 4328f8829a4aSRandall Stewart sctp_m_freem(sq->data); 4329f8829a4aSRandall Stewart sq->data = NULL; 4330f8829a4aSRandall Stewart } 4331f8829a4aSRandall Stewart sctp_free_remote_addr(sq->whoFrom); 4332f8829a4aSRandall Stewart sq->whoFrom = NULL; 4333f8829a4aSRandall Stewart sq->stcb = NULL; 4334f8829a4aSRandall Stewart /* Free the ctl entry */ 4335f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq); 4336f8829a4aSRandall Stewart SCTP_DECR_READQ_COUNT(); 43373c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4338f8829a4aSRandall Stewart sq = TAILQ_FIRST(&asoc->pending_reply_queue); 4339f8829a4aSRandall Stewart } 4340f8829a4aSRandall Stewart 4341f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->free_chunks); 4342f8829a4aSRandall Stewart while (chk) { 4343f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); 4344f8829a4aSRandall Stewart if (chk->data) { 4345f8829a4aSRandall Stewart sctp_m_freem(chk->data); 4346f8829a4aSRandall Stewart chk->data = NULL; 4347f8829a4aSRandall Stewart } 4348f8829a4aSRandall Stewart ccnt++; 4349f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 4350f8829a4aSRandall Stewart SCTP_DECR_CHK_COUNT(); 4351f8829a4aSRandall Stewart atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1); 4352f8829a4aSRandall Stewart asoc->free_chunk_cnt--; 43533c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4354f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->free_chunks); 4355f8829a4aSRandall Stewart } 4356f8829a4aSRandall Stewart /* pending send queue SHOULD be empty */ 4357f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->send_queue)) { 4358f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->send_queue); 4359f8829a4aSRandall Stewart while (chk) { 4360f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 4361f8829a4aSRandall Stewart if (chk->data) { 4362f8829a4aSRandall Stewart sctp_m_freem(chk->data); 4363f8829a4aSRandall Stewart chk->data = NULL; 4364f8829a4aSRandall Stewart } 4365f8829a4aSRandall Stewart ccnt++; 4366f8829a4aSRandall Stewart sctp_free_remote_addr(chk->whoTo); 4367f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 4368f8829a4aSRandall Stewart SCTP_DECR_CHK_COUNT(); 43693c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4370f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->send_queue); 4371f8829a4aSRandall Stewart } 4372f8829a4aSRandall Stewart } 4373f8829a4aSRandall Stewart /* 4374f8829a4aSRandall Stewart if(ccnt) { 4375f8829a4aSRandall Stewart printf("Freed %d from send_queue\n", ccnt); 4376f8829a4aSRandall Stewart ccnt = 0; 4377f8829a4aSRandall Stewart } 4378f8829a4aSRandall Stewart */ 4379f8829a4aSRandall Stewart /* sent queue SHOULD be empty */ 4380f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->sent_queue)) { 4381f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->sent_queue); 4382f8829a4aSRandall Stewart while (chk) { 4383f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); 4384f8829a4aSRandall Stewart if (chk->data) { 4385f8829a4aSRandall Stewart sctp_m_freem(chk->data); 4386f8829a4aSRandall Stewart chk->data = NULL; 4387f8829a4aSRandall Stewart } 4388f8829a4aSRandall Stewart ccnt++; 4389f8829a4aSRandall Stewart sctp_free_remote_addr(chk->whoTo); 4390f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 4391f8829a4aSRandall Stewart SCTP_DECR_CHK_COUNT(); 43923c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4393f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->sent_queue); 4394f8829a4aSRandall Stewart } 4395f8829a4aSRandall Stewart } 4396f8829a4aSRandall Stewart /* 4397f8829a4aSRandall Stewart if(ccnt) { 4398f8829a4aSRandall Stewart printf("Freed %d from sent_queue\n", ccnt); 4399f8829a4aSRandall Stewart ccnt = 0; 4400f8829a4aSRandall Stewart } 4401f8829a4aSRandall Stewart */ 4402f8829a4aSRandall Stewart /* control queue MAY not be empty */ 4403f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->control_send_queue)) { 4404f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->control_send_queue); 4405f8829a4aSRandall Stewart while (chk) { 4406f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); 4407f8829a4aSRandall Stewart if (chk->data) { 4408f8829a4aSRandall Stewart sctp_m_freem(chk->data); 4409f8829a4aSRandall Stewart chk->data = NULL; 4410f8829a4aSRandall Stewart } 4411f8829a4aSRandall Stewart ccnt++; 4412f8829a4aSRandall Stewart sctp_free_remote_addr(chk->whoTo); 4413f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 4414f8829a4aSRandall Stewart SCTP_DECR_CHK_COUNT(); 44153c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4416f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->control_send_queue); 4417f8829a4aSRandall Stewart } 4418f8829a4aSRandall Stewart } 4419f8829a4aSRandall Stewart /* 4420f8829a4aSRandall Stewart if(ccnt) { 4421f8829a4aSRandall Stewart printf("Freed %d from ctrl_queue\n", ccnt); 4422f8829a4aSRandall Stewart ccnt = 0; 4423f8829a4aSRandall Stewart } 4424f8829a4aSRandall Stewart */ 4425f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->reasmqueue)) { 4426f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->reasmqueue); 4427f8829a4aSRandall Stewart while (chk) { 4428f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 4429f8829a4aSRandall Stewart if (chk->data) { 4430f8829a4aSRandall Stewart sctp_m_freem(chk->data); 4431f8829a4aSRandall Stewart chk->data = NULL; 4432f8829a4aSRandall Stewart } 4433f8829a4aSRandall Stewart sctp_free_remote_addr(chk->whoTo); 4434f8829a4aSRandall Stewart ccnt++; 4435f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk); 4436f8829a4aSRandall Stewart SCTP_DECR_CHK_COUNT(); 44373c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4438f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->reasmqueue); 4439f8829a4aSRandall Stewart } 4440f8829a4aSRandall Stewart } 4441f8829a4aSRandall Stewart /* 4442f8829a4aSRandall Stewart if(ccnt) { 4443f8829a4aSRandall Stewart printf("Freed %d from reasm_queue\n", ccnt); 4444f8829a4aSRandall Stewart ccnt = 0; 4445f8829a4aSRandall Stewart } 4446f8829a4aSRandall Stewart */ 4447f8829a4aSRandall Stewart if (asoc->mapping_array) { 4448207304d4SRandall Stewart SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); 4449f8829a4aSRandall Stewart asoc->mapping_array = NULL; 4450f8829a4aSRandall Stewart } 4451f8829a4aSRandall Stewart /* the stream outs */ 4452f8829a4aSRandall Stewart if (asoc->strmout) { 4453207304d4SRandall Stewart SCTP_FREE(asoc->strmout, SCTP_M_STRMO); 4454f8829a4aSRandall Stewart asoc->strmout = NULL; 4455f8829a4aSRandall Stewart } 4456f8829a4aSRandall Stewart asoc->streamoutcnt = 0; 4457f8829a4aSRandall Stewart if (asoc->strmin) { 4458f8829a4aSRandall Stewart struct sctp_queued_to_read *ctl; 4459f8829a4aSRandall Stewart 4460f8829a4aSRandall Stewart for (i = 0; i < asoc->streamincnt; i++) { 4461f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) { 4462f8829a4aSRandall Stewart /* We have somethings on the streamin queue */ 4463f8829a4aSRandall Stewart ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 4464f8829a4aSRandall Stewart while (ctl) { 4465f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->strmin[i].inqueue, 4466f8829a4aSRandall Stewart ctl, next); 4467f8829a4aSRandall Stewart sctp_free_remote_addr(ctl->whoFrom); 4468f8829a4aSRandall Stewart if (ctl->data) { 4469f8829a4aSRandall Stewart sctp_m_freem(ctl->data); 4470f8829a4aSRandall Stewart ctl->data = NULL; 4471f8829a4aSRandall Stewart } 4472f8829a4aSRandall Stewart /* 4473f8829a4aSRandall Stewart * We don't free the address here 4474f8829a4aSRandall Stewart * since all the net's were freed 4475f8829a4aSRandall Stewart * above. 4476f8829a4aSRandall Stewart */ 4477f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl); 4478f8829a4aSRandall Stewart SCTP_DECR_READQ_COUNT(); 4479f8829a4aSRandall Stewart ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue); 4480f8829a4aSRandall Stewart } 4481f8829a4aSRandall Stewart } 4482f8829a4aSRandall Stewart } 4483207304d4SRandall Stewart SCTP_FREE(asoc->strmin, SCTP_M_STRMI); 4484f8829a4aSRandall Stewart asoc->strmin = NULL; 4485f8829a4aSRandall Stewart } 4486f8829a4aSRandall Stewart asoc->streamincnt = 0; 4487f8829a4aSRandall Stewart while (!TAILQ_EMPTY(&asoc->nets)) { 44883c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4489f8829a4aSRandall Stewart net = TAILQ_FIRST(&asoc->nets); 4490f8829a4aSRandall Stewart /* pull from list */ 4491f8829a4aSRandall Stewart if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) { 4492a5d547adSRandall Stewart #ifdef INVARIANTS 4493f8829a4aSRandall Stewart panic("no net's left alloc'ed, or list points to itself"); 4494f8829a4aSRandall Stewart #endif 4495f8829a4aSRandall Stewart break; 4496f8829a4aSRandall Stewart } 4497f8829a4aSRandall Stewart prev = net; 4498f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->nets, net, sctp_next); 4499f8829a4aSRandall Stewart sctp_free_remote_addr(net); 4500f8829a4aSRandall Stewart } 4501f8829a4aSRandall Stewart 450242551e99SRandall Stewart while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) { 45033c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 450442551e99SRandall Stewart laddr = LIST_FIRST(&asoc->sctp_restricted_addrs); 450542551e99SRandall Stewart sctp_remove_laddr(laddr); 4506f8829a4aSRandall Stewart } 450742551e99SRandall Stewart 4508f8829a4aSRandall Stewart /* pending asconf (address) parameters */ 4509f8829a4aSRandall Stewart while (!TAILQ_EMPTY(&asoc->asconf_queue)) { 45103c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4511f8829a4aSRandall Stewart aparam = TAILQ_FIRST(&asoc->asconf_queue); 4512f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); 4513207304d4SRandall Stewart SCTP_FREE(aparam, SCTP_M_ASC_ADDR); 4514f8829a4aSRandall Stewart } 45152afb3e84SRandall Stewart while (!TAILQ_EMPTY(&asoc->asconf_ack_sent)) { 4516851b7298SRandall Stewart /* sa_ignore FREED_MEMORY */ 45172afb3e84SRandall Stewart aack = TAILQ_FIRST(&asoc->asconf_ack_sent); 45182afb3e84SRandall Stewart TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); 45192afb3e84SRandall Stewart if (aack->data != NULL) { 45202afb3e84SRandall Stewart sctp_m_freem(aack->data); 45212afb3e84SRandall Stewart } 45222afb3e84SRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asconf_ack, aack); 4523f8829a4aSRandall Stewart } 4524f8829a4aSRandall Stewart /* clean up auth stuff */ 4525f8829a4aSRandall Stewart if (asoc->local_hmacs) 4526f8829a4aSRandall Stewart sctp_free_hmaclist(asoc->local_hmacs); 4527f8829a4aSRandall Stewart if (asoc->peer_hmacs) 4528f8829a4aSRandall Stewart sctp_free_hmaclist(asoc->peer_hmacs); 4529f8829a4aSRandall Stewart 4530f8829a4aSRandall Stewart if (asoc->local_auth_chunks) 4531f8829a4aSRandall Stewart sctp_free_chunklist(asoc->local_auth_chunks); 4532f8829a4aSRandall Stewart if (asoc->peer_auth_chunks) 4533f8829a4aSRandall Stewart sctp_free_chunklist(asoc->peer_auth_chunks); 4534f8829a4aSRandall Stewart 4535f8829a4aSRandall Stewart sctp_free_authinfo(&asoc->authinfo); 4536f8829a4aSRandall Stewart 4537f8829a4aSRandall Stewart shared_key = LIST_FIRST(&asoc->shared_keys); 4538f8829a4aSRandall Stewart while (shared_key) { 4539f8829a4aSRandall Stewart LIST_REMOVE(shared_key, next); 4540f8829a4aSRandall Stewart sctp_free_sharedkey(shared_key); 45413c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 4542f8829a4aSRandall Stewart shared_key = LIST_FIRST(&asoc->shared_keys); 4543f8829a4aSRandall Stewart } 4544f8829a4aSRandall Stewart 4545f8829a4aSRandall Stewart /* Insert new items here :> */ 4546f8829a4aSRandall Stewart 4547f8829a4aSRandall Stewart /* Get rid of LOCK */ 4548f8829a4aSRandall Stewart SCTP_TCB_LOCK_DESTROY(stcb); 4549f8829a4aSRandall Stewart SCTP_TCB_SEND_LOCK_DESTROY(stcb); 4550a5d547adSRandall Stewart if (from_inpcbfree == SCTP_NORMAL_PROC) { 4551a5d547adSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 4552a5d547adSRandall Stewart SCTP_INP_RLOCK(inp); 4553a5d547adSRandall Stewart } 4554a5d547adSRandall Stewart #ifdef SCTP_TRACK_FREED_ASOCS 4555a5d547adSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4556f8829a4aSRandall Stewart /* now clean up the tasoc itself */ 4557f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 4558f8829a4aSRandall Stewart SCTP_DECR_ASOC_COUNT(); 4559a5d547adSRandall Stewart } else { 4560a5d547adSRandall Stewart LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); 4561a5d547adSRandall Stewart } 4562a5d547adSRandall Stewart #else 4563a5d547adSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb); 4564a5d547adSRandall Stewart SCTP_DECR_ASOC_COUNT(); 4565a5d547adSRandall Stewart #endif 4566a5d547adSRandall Stewart if (from_inpcbfree == SCTP_NORMAL_PROC) { 4567f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4568f8829a4aSRandall Stewart /* 4569f8829a4aSRandall Stewart * If its NOT the inp_free calling us AND sctp_close 4570f8829a4aSRandall Stewart * as been called, we call back... 4571f8829a4aSRandall Stewart */ 4572f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 4573f8829a4aSRandall Stewart /* 4574f8829a4aSRandall Stewart * This will start the kill timer (if we are the 4575f8829a4aSRandall Stewart * lastone) since we hold an increment yet. But this 4576f8829a4aSRandall Stewart * is the only safe way to do this since otherwise 4577f8829a4aSRandall Stewart * if the socket closes at the same time we are here 4578f8829a4aSRandall Stewart * we might collide in the cleanup. 4579f8829a4aSRandall Stewart */ 4580b0552ae2SRandall Stewart sctp_inpcb_free(inp, 4581b0552ae2SRandall Stewart SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, 4582b0552ae2SRandall Stewart SCTP_CALLED_DIRECTLY_NOCMPSET); 4583f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 4584a5d547adSRandall Stewart goto out_of; 4585f8829a4aSRandall Stewart } else { 4586f8829a4aSRandall Stewart /* The socket is still open. */ 4587f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 4588a5d547adSRandall Stewart } 4589a5d547adSRandall Stewart } 4590a5d547adSRandall Stewart if (from_inpcbfree == SCTP_NORMAL_PROC) { 4591f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 4592f8829a4aSRandall Stewart } 4593a5d547adSRandall Stewart out_of: 4594f8829a4aSRandall Stewart /* destroyed the asoc */ 4595f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING 4596f8829a4aSRandall Stewart sctp_log_closing(inp, NULL, 11); 4597f8829a4aSRandall Stewart #endif 4598f8829a4aSRandall Stewart return (1); 4599f8829a4aSRandall Stewart } 4600f8829a4aSRandall Stewart 4601f8829a4aSRandall Stewart 4602f8829a4aSRandall Stewart 4603f8829a4aSRandall Stewart /* 4604f8829a4aSRandall Stewart * determine if a destination is "reachable" based upon the addresses bound 4605f8829a4aSRandall Stewart * to the current endpoint (e.g. only v4 or v6 currently bound) 4606f8829a4aSRandall Stewart */ 4607f8829a4aSRandall Stewart /* 4608f8829a4aSRandall Stewart * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use 4609f8829a4aSRandall Stewart * assoc level v4/v6 flags, as the assoc *may* not have the same address 4610f8829a4aSRandall Stewart * types bound as its endpoint 4611f8829a4aSRandall Stewart */ 4612f8829a4aSRandall Stewart int 4613f8829a4aSRandall Stewart sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) 4614f8829a4aSRandall Stewart { 4615f8829a4aSRandall Stewart struct sctp_inpcb *inp; 4616f8829a4aSRandall Stewart int answer; 4617f8829a4aSRandall Stewart 4618f8829a4aSRandall Stewart /* 4619f8829a4aSRandall Stewart * No locks here, the TCB, in all cases is already locked and an 4620f8829a4aSRandall Stewart * assoc is up. There is either a INP lock by the caller applied (in 4621f8829a4aSRandall Stewart * asconf case when deleting an address) or NOT in the HB case, 4622f8829a4aSRandall Stewart * however if HB then the INP increment is up and the INP will not 4623f8829a4aSRandall Stewart * be removed (on top of the fact that we have a TCB lock). So we 4624f8829a4aSRandall Stewart * only want to read the sctp_flags, which is either bound-all or 4625f8829a4aSRandall Stewart * not.. no protection needed since once an assoc is up you can't be 4626f8829a4aSRandall Stewart * changing your binding. 4627f8829a4aSRandall Stewart */ 4628f8829a4aSRandall Stewart inp = stcb->sctp_ep; 4629f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 4630f8829a4aSRandall Stewart /* if bound all, destination is not restricted */ 4631f8829a4aSRandall Stewart /* 4632f8829a4aSRandall Stewart * RRS: Question during lock work: Is this correct? If you 4633f8829a4aSRandall Stewart * are bound-all you still might need to obey the V4--V6 4634f8829a4aSRandall Stewart * flags??? IMO this bound-all stuff needs to be removed! 4635f8829a4aSRandall Stewart */ 4636f8829a4aSRandall Stewart return (1); 4637f8829a4aSRandall Stewart } 4638f8829a4aSRandall Stewart /* NOTE: all "scope" checks are done when local addresses are added */ 4639f8829a4aSRandall Stewart if (destaddr->sa_family == AF_INET6) { 4640f8829a4aSRandall Stewart answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; 4641f8829a4aSRandall Stewart } else if (destaddr->sa_family == AF_INET) { 4642f8829a4aSRandall Stewart answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; 4643f8829a4aSRandall Stewart } else { 4644f8829a4aSRandall Stewart /* invalid family, so it's unreachable */ 4645f8829a4aSRandall Stewart answer = 0; 4646f8829a4aSRandall Stewart } 4647f8829a4aSRandall Stewart return (answer); 4648f8829a4aSRandall Stewart } 4649f8829a4aSRandall Stewart 4650f8829a4aSRandall Stewart /* 4651f8829a4aSRandall Stewart * update the inp_vflags on an endpoint 4652f8829a4aSRandall Stewart */ 4653f8829a4aSRandall Stewart static void 4654f8829a4aSRandall Stewart sctp_update_ep_vflag(struct sctp_inpcb *inp) 4655f8829a4aSRandall Stewart { 4656f8829a4aSRandall Stewart struct sctp_laddr *laddr; 4657f8829a4aSRandall Stewart 4658f8829a4aSRandall Stewart /* first clear the flag */ 4659f8829a4aSRandall Stewart inp->ip_inp.inp.inp_vflag = 0; 4660f8829a4aSRandall Stewart /* set the flag based on addresses on the ep list */ 4661f8829a4aSRandall Stewart LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 4662f8829a4aSRandall Stewart if (laddr->ifa == NULL) { 4663ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", 4664ad81507eSRandall Stewart __FUNCTION__); 4665f8829a4aSRandall Stewart continue; 4666f8829a4aSRandall Stewart } 466742551e99SRandall Stewart if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { 4668f8829a4aSRandall Stewart continue; 4669f8829a4aSRandall Stewart } 467042551e99SRandall Stewart if (laddr->ifa->address.sa.sa_family == AF_INET6) { 4671f8829a4aSRandall Stewart inp->ip_inp.inp.inp_vflag |= INP_IPV6; 467242551e99SRandall Stewart } else if (laddr->ifa->address.sa.sa_family == AF_INET) { 4673f8829a4aSRandall Stewart inp->ip_inp.inp.inp_vflag |= INP_IPV4; 4674f8829a4aSRandall Stewart } 4675f8829a4aSRandall Stewart } 4676f8829a4aSRandall Stewart } 4677f8829a4aSRandall Stewart 4678f8829a4aSRandall Stewart /* 4679f8829a4aSRandall Stewart * Add the address to the endpoint local address list There is nothing to be 4680f8829a4aSRandall Stewart * done if we are bound to all addresses 4681f8829a4aSRandall Stewart */ 4682ad81507eSRandall Stewart void 468342551e99SRandall Stewart sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) 4684f8829a4aSRandall Stewart { 4685f8829a4aSRandall Stewart struct sctp_laddr *laddr; 4686ad81507eSRandall Stewart int fnd, error = 0; 4687f8829a4aSRandall Stewart 4688f8829a4aSRandall Stewart fnd = 0; 4689f8829a4aSRandall Stewart 4690f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 4691f8829a4aSRandall Stewart /* You are already bound to all. You have it already */ 4692ad81507eSRandall Stewart return; 4693f8829a4aSRandall Stewart } 469442551e99SRandall Stewart if (ifa->address.sa.sa_family == AF_INET6) { 469542551e99SRandall Stewart if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 469642551e99SRandall Stewart /* Can't bind a non-useable addr. */ 4697ad81507eSRandall Stewart return; 4698f8829a4aSRandall Stewart } 469942551e99SRandall Stewart } 4700f8829a4aSRandall Stewart /* first, is it already present? */ 4701f8829a4aSRandall Stewart LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 4702f8829a4aSRandall Stewart if (laddr->ifa == ifa) { 4703f8829a4aSRandall Stewart fnd = 1; 4704f8829a4aSRandall Stewart break; 4705f8829a4aSRandall Stewart } 4706f8829a4aSRandall Stewart } 4707f8829a4aSRandall Stewart 470842551e99SRandall Stewart if (fnd == 0) { 470942551e99SRandall Stewart /* Not in the ep list */ 471042551e99SRandall Stewart error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); 4711f8829a4aSRandall Stewart if (error != 0) 4712ad81507eSRandall Stewart return; 4713f8829a4aSRandall Stewart inp->laddr_count++; 4714f8829a4aSRandall Stewart /* update inp_vflag flags */ 471542551e99SRandall Stewart if (ifa->address.sa.sa_family == AF_INET6) { 4716f8829a4aSRandall Stewart inp->ip_inp.inp.inp_vflag |= INP_IPV6; 471742551e99SRandall Stewart } else if (ifa->address.sa.sa_family == AF_INET) { 4718f8829a4aSRandall Stewart inp->ip_inp.inp.inp_vflag |= INP_IPV4; 4719f8829a4aSRandall Stewart } 4720f8829a4aSRandall Stewart } 4721ad81507eSRandall Stewart return; 4722f8829a4aSRandall Stewart } 4723f8829a4aSRandall Stewart 4724f8829a4aSRandall Stewart 4725f8829a4aSRandall Stewart /* 4726f8829a4aSRandall Stewart * select a new (hopefully reachable) destination net (should only be used 4727f8829a4aSRandall Stewart * when we deleted an ep addr that is the only usable source address to reach 4728f8829a4aSRandall Stewart * the destination net) 4729f8829a4aSRandall Stewart */ 4730f8829a4aSRandall Stewart static void 4731f8829a4aSRandall Stewart sctp_select_primary_destination(struct sctp_tcb *stcb) 4732f8829a4aSRandall Stewart { 4733f8829a4aSRandall Stewart struct sctp_nets *net; 4734f8829a4aSRandall Stewart 4735f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4736f8829a4aSRandall Stewart /* for now, we'll just pick the first reachable one we find */ 4737f8829a4aSRandall Stewart if (net->dest_state & SCTP_ADDR_UNCONFIRMED) 4738f8829a4aSRandall Stewart continue; 4739f8829a4aSRandall Stewart if (sctp_destination_is_reachable(stcb, 4740f8829a4aSRandall Stewart (struct sockaddr *)&net->ro._l_addr)) { 4741f8829a4aSRandall Stewart /* found a reachable destination */ 4742f8829a4aSRandall Stewart stcb->asoc.primary_destination = net; 4743f8829a4aSRandall Stewart } 4744f8829a4aSRandall Stewart } 4745f8829a4aSRandall Stewart /* I can't there from here! ...we're gonna die shortly... */ 4746f8829a4aSRandall Stewart } 4747f8829a4aSRandall Stewart 4748f8829a4aSRandall Stewart 4749f8829a4aSRandall Stewart /* 4750f8829a4aSRandall Stewart * Delete the address from the endpoint local address list There is nothing 4751f8829a4aSRandall Stewart * to be done if we are bound to all addresses 4752f8829a4aSRandall Stewart */ 4753ad81507eSRandall Stewart void 475442551e99SRandall Stewart sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) 4755f8829a4aSRandall Stewart { 4756f8829a4aSRandall Stewart struct sctp_laddr *laddr; 4757f8829a4aSRandall Stewart int fnd; 4758f8829a4aSRandall Stewart 4759f8829a4aSRandall Stewart fnd = 0; 4760f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 4761f8829a4aSRandall Stewart /* You are already bound to all. You have it already */ 4762ad81507eSRandall Stewart return; 4763f8829a4aSRandall Stewart } 4764f8829a4aSRandall Stewart LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 4765f8829a4aSRandall Stewart if (laddr->ifa == ifa) { 4766f8829a4aSRandall Stewart fnd = 1; 4767f8829a4aSRandall Stewart break; 4768f8829a4aSRandall Stewart } 4769f8829a4aSRandall Stewart } 4770f8829a4aSRandall Stewart if (fnd && (inp->laddr_count < 2)) { 4771f8829a4aSRandall Stewart /* can't delete unless there are at LEAST 2 addresses */ 4772ad81507eSRandall Stewart return; 4773f8829a4aSRandall Stewart } 477442551e99SRandall Stewart if (fnd) { 4775f8829a4aSRandall Stewart /* 4776f8829a4aSRandall Stewart * clean up any use of this address go through our 4777f8829a4aSRandall Stewart * associations and clear any last_used_address that match 4778f8829a4aSRandall Stewart * this one for each assoc, see if a new primary_destination 4779f8829a4aSRandall Stewart * is needed 4780f8829a4aSRandall Stewart */ 4781f8829a4aSRandall Stewart struct sctp_tcb *stcb; 4782f8829a4aSRandall Stewart 4783f8829a4aSRandall Stewart /* clean up "next_addr_touse" */ 4784f8829a4aSRandall Stewart if (inp->next_addr_touse == laddr) 4785f8829a4aSRandall Stewart /* delete this address */ 4786f8829a4aSRandall Stewart inp->next_addr_touse = NULL; 4787f8829a4aSRandall Stewart 4788f8829a4aSRandall Stewart /* clean up "last_used_address" */ 4789f8829a4aSRandall Stewart LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 479042551e99SRandall Stewart struct sctp_nets *net; 479142551e99SRandall Stewart 479242551e99SRandall Stewart SCTP_TCB_LOCK(stcb); 4793f8829a4aSRandall Stewart if (stcb->asoc.last_used_address == laddr) 4794f8829a4aSRandall Stewart /* delete this address */ 4795f8829a4aSRandall Stewart stcb->asoc.last_used_address = NULL; 479642551e99SRandall Stewart /* 479742551e99SRandall Stewart * Now spin through all the nets and purge any ref 479842551e99SRandall Stewart * to laddr 479942551e99SRandall Stewart */ 480042551e99SRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 480142551e99SRandall Stewart if (net->ro._s_addr && 480242551e99SRandall Stewart (net->ro._s_addr->ifa == laddr->ifa)) { 480342551e99SRandall Stewart /* Yep, purge src address selected */ 480417205eccSRandall Stewart sctp_rtentry_t *rt; 4805f8829a4aSRandall Stewart 480642551e99SRandall Stewart /* delete this address if cached */ 480742551e99SRandall Stewart rt = net->ro.ro_rt; 480842551e99SRandall Stewart if (rt != NULL) { 480942551e99SRandall Stewart RTFREE(rt); 481042551e99SRandall Stewart net->ro.ro_rt = NULL; 481142551e99SRandall Stewart } 481242551e99SRandall Stewart sctp_free_ifa(net->ro._s_addr); 481342551e99SRandall Stewart net->ro._s_addr = NULL; 481442551e99SRandall Stewart net->src_addr_selected = 0; 481542551e99SRandall Stewart } 481642551e99SRandall Stewart } 481742551e99SRandall Stewart SCTP_TCB_UNLOCK(stcb); 481842551e99SRandall Stewart } /* for each tcb */ 4819f8829a4aSRandall Stewart /* remove it from the ep list */ 4820f8829a4aSRandall Stewart sctp_remove_laddr(laddr); 4821f8829a4aSRandall Stewart inp->laddr_count--; 4822f8829a4aSRandall Stewart /* update inp_vflag flags */ 4823f8829a4aSRandall Stewart sctp_update_ep_vflag(inp); 4824f8829a4aSRandall Stewart } 4825ad81507eSRandall Stewart return; 4826f8829a4aSRandall Stewart } 4827f8829a4aSRandall Stewart 4828f8829a4aSRandall Stewart /* 48291b649582SRandall Stewart * Add the address to the TCB local address restricted list. 48301b649582SRandall Stewart * This is a "pending" address list (eg. addresses waiting for an 48311b649582SRandall Stewart * ASCONF-ACK response) and cannot be used as a valid source address. 4832f8829a4aSRandall Stewart */ 4833ad81507eSRandall Stewart void 48341b649582SRandall Stewart sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 4835f8829a4aSRandall Stewart { 4836f8829a4aSRandall Stewart struct sctp_inpcb *inp; 4837f8829a4aSRandall Stewart struct sctp_laddr *laddr; 483842551e99SRandall Stewart struct sctpladdr *list; 4839f8829a4aSRandall Stewart 4840f8829a4aSRandall Stewart /* 484142551e99SRandall Stewart * Assumes TCB is locked.. and possibly the INP. May need to 4842f8829a4aSRandall Stewart * confirm/fix that if we need it and is not the case. 4843f8829a4aSRandall Stewart */ 484442551e99SRandall Stewart list = &stcb->asoc.sctp_restricted_addrs; 4845f8829a4aSRandall Stewart 484642551e99SRandall Stewart inp = stcb->sctp_ep; 484742551e99SRandall Stewart if (ifa->address.sa.sa_family == AF_INET6) { 484842551e99SRandall Stewart if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { 4849f8829a4aSRandall Stewart /* Can't bind a non-existent addr. */ 4850ad81507eSRandall Stewart return; 4851f8829a4aSRandall Stewart } 485242551e99SRandall Stewart } 4853f8829a4aSRandall Stewart /* does the address already exist? */ 485442551e99SRandall Stewart LIST_FOREACH(laddr, list, sctp_nxt_addr) { 4855f8829a4aSRandall Stewart if (laddr->ifa == ifa) { 4856ad81507eSRandall Stewart return; 4857f8829a4aSRandall Stewart } 4858f8829a4aSRandall Stewart } 4859f8829a4aSRandall Stewart 4860f8829a4aSRandall Stewart /* add to the list */ 4861ad81507eSRandall Stewart (void)sctp_insert_laddr(list, ifa, 0); 4862ad81507eSRandall Stewart return; 4863f8829a4aSRandall Stewart } 4864f8829a4aSRandall Stewart 4865f8829a4aSRandall Stewart /* 4866f8829a4aSRandall Stewart * insert an laddr entry with the given ifa for the desired list 4867f8829a4aSRandall Stewart */ 4868f8829a4aSRandall Stewart int 486942551e99SRandall Stewart sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) 4870f8829a4aSRandall Stewart { 4871f8829a4aSRandall Stewart struct sctp_laddr *laddr; 4872f8829a4aSRandall Stewart 4873f42a358aSRandall Stewart laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr); 4874f8829a4aSRandall Stewart if (laddr == NULL) { 4875f8829a4aSRandall Stewart /* out of memory? */ 4876c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); 4877f8829a4aSRandall Stewart return (EINVAL); 4878f8829a4aSRandall Stewart } 4879f8829a4aSRandall Stewart SCTP_INCR_LADDR_COUNT(); 4880f8829a4aSRandall Stewart bzero(laddr, sizeof(*laddr)); 4881d61a0ae0SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); 4882f8829a4aSRandall Stewart laddr->ifa = ifa; 488342551e99SRandall Stewart laddr->action = act; 488442551e99SRandall Stewart atomic_add_int(&ifa->refcount, 1); 4885f8829a4aSRandall Stewart /* insert it */ 4886f8829a4aSRandall Stewart LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); 4887f8829a4aSRandall Stewart 4888f8829a4aSRandall Stewart return (0); 4889f8829a4aSRandall Stewart } 4890f8829a4aSRandall Stewart 4891f8829a4aSRandall Stewart /* 4892f8829a4aSRandall Stewart * Remove an laddr entry from the local address list (on an assoc) 4893f8829a4aSRandall Stewart */ 4894f8829a4aSRandall Stewart void 4895f8829a4aSRandall Stewart sctp_remove_laddr(struct sctp_laddr *laddr) 4896f8829a4aSRandall Stewart { 4897f8829a4aSRandall Stewart 4898f8829a4aSRandall Stewart /* remove from the list */ 4899f8829a4aSRandall Stewart LIST_REMOVE(laddr, sctp_nxt_addr); 490042551e99SRandall Stewart sctp_free_ifa(laddr->ifa); 4901f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr); 4902f8829a4aSRandall Stewart SCTP_DECR_LADDR_COUNT(); 4903f8829a4aSRandall Stewart } 4904f8829a4aSRandall Stewart 4905f8829a4aSRandall Stewart /* 49061b649582SRandall Stewart * Remove a local address from the TCB local address restricted list 4907f8829a4aSRandall Stewart */ 4908ad81507eSRandall Stewart void 49091b649582SRandall Stewart sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) 4910f8829a4aSRandall Stewart { 4911f8829a4aSRandall Stewart struct sctp_inpcb *inp; 4912f8829a4aSRandall Stewart struct sctp_laddr *laddr; 4913f8829a4aSRandall Stewart 4914f8829a4aSRandall Stewart /* 4915f8829a4aSRandall Stewart * This is called by asconf work. It is assumed that a) The TCB is 4916f8829a4aSRandall Stewart * locked and b) The INP is locked. This is true in as much as I can 4917f8829a4aSRandall Stewart * trace through the entry asconf code where I did these locks. 4918f8829a4aSRandall Stewart * Again, the ASCONF code is a bit different in that it does lock 4919f8829a4aSRandall Stewart * the INP during its work often times. This must be since we don't 4920f8829a4aSRandall Stewart * want other proc's looking up things while what they are looking 4921f8829a4aSRandall Stewart * up is changing :-D 4922f8829a4aSRandall Stewart */ 4923f8829a4aSRandall Stewart 4924f8829a4aSRandall Stewart inp = stcb->sctp_ep; 4925f8829a4aSRandall Stewart /* if subset bound and don't allow ASCONF's, can't delete last */ 4926f8829a4aSRandall Stewart if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && 492718e198d3SRandall Stewart sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { 492818e198d3SRandall Stewart if (stcb->sctp_ep->laddr_count < 2) { 4929f8829a4aSRandall Stewart /* can't delete last address */ 4930ad81507eSRandall Stewart return; 4931f8829a4aSRandall Stewart } 4932f8829a4aSRandall Stewart } 493342551e99SRandall Stewart LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { 4934f8829a4aSRandall Stewart /* remove the address if it exists */ 4935f8829a4aSRandall Stewart if (laddr->ifa == NULL) 4936f8829a4aSRandall Stewart continue; 4937f8829a4aSRandall Stewart if (laddr->ifa == ifa) { 4938f8829a4aSRandall Stewart sctp_remove_laddr(laddr); 4939ad81507eSRandall Stewart return; 4940f8829a4aSRandall Stewart } 4941f8829a4aSRandall Stewart } 4942f8829a4aSRandall Stewart 4943f8829a4aSRandall Stewart /* address not found! */ 4944ad81507eSRandall Stewart return; 4945f8829a4aSRandall Stewart } 4946f8829a4aSRandall Stewart 4947f8829a4aSRandall Stewart static char sctp_pcb_initialized = 0; 4948f8829a4aSRandall Stewart 4949f8829a4aSRandall Stewart /* 4950f8829a4aSRandall Stewart * Temporarily remove for __APPLE__ until we use the Tiger equivalents 4951f8829a4aSRandall Stewart */ 4952f8829a4aSRandall Stewart /* sysctl */ 4953f8829a4aSRandall Stewart static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; 4954f8829a4aSRandall Stewart static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; 4955f8829a4aSRandall Stewart 4956f8829a4aSRandall Stewart void 4957f8829a4aSRandall Stewart sctp_pcb_init() 4958f8829a4aSRandall Stewart { 4959f8829a4aSRandall Stewart /* 4960f8829a4aSRandall Stewart * SCTP initialization for the PCB structures should be called by 4961f8829a4aSRandall Stewart * the sctp_init() funciton. 4962f8829a4aSRandall Stewart */ 4963f8829a4aSRandall Stewart int i; 4964851b7298SRandall Stewart struct timeval tv; 4965f8829a4aSRandall Stewart 4966f8829a4aSRandall Stewart if (sctp_pcb_initialized != 0) { 4967f8829a4aSRandall Stewart /* error I was called twice */ 4968f8829a4aSRandall Stewart return; 4969f8829a4aSRandall Stewart } 4970f8829a4aSRandall Stewart sctp_pcb_initialized = 1; 4971f8829a4aSRandall Stewart 4972f8829a4aSRandall Stewart bzero(&sctpstat, sizeof(struct sctpstat)); 4973b27a6b7dSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF) 4974b27a6b7dSRandall Stewart bzero(&sctp_log, sizeof(struct sctp_log)); 4975b27a6b7dSRandall Stewart #endif 4976851b7298SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&tv); 4977851b7298SRandall Stewart sctpstat.sctps_discontinuitytime.tv_sec = (uint32_t) tv.tv_sec; 4978851b7298SRandall Stewart sctpstat.sctps_discontinuitytime.tv_usec = (uint32_t) tv.tv_usec; 4979f8829a4aSRandall Stewart /* init the empty list of (All) Endpoints */ 4980f8829a4aSRandall Stewart LIST_INIT(&sctppcbinfo.listhead); 4981f8829a4aSRandall Stewart 4982f8829a4aSRandall Stewart /* init the iterator head */ 498342551e99SRandall Stewart TAILQ_INIT(&sctppcbinfo.iteratorhead); 4984f8829a4aSRandall Stewart 4985f8829a4aSRandall Stewart /* init the hash table of endpoints */ 4986f8829a4aSRandall Stewart TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize); 4987f8829a4aSRandall Stewart TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize); 4988f8829a4aSRandall Stewart TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale); 498944b7479bSRandall Stewart sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31), 4990f8829a4aSRandall Stewart &sctppcbinfo.hashasocmark); 499144b7479bSRandall Stewart sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize, 4992f8829a4aSRandall Stewart &sctppcbinfo.hashmark); 499344b7479bSRandall Stewart sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize, 4994f8829a4aSRandall Stewart &sctppcbinfo.hashtcpmark); 4995f8829a4aSRandall Stewart sctppcbinfo.hashtblsize = sctp_hashtblsize; 4996f8829a4aSRandall Stewart 499744b7479bSRandall Stewart /* init the small hash table we use to track restarted asoc's */ 499844b7479bSRandall Stewart sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, 4999f8829a4aSRandall Stewart &sctppcbinfo.hashrestartmark); 5000f8829a4aSRandall Stewart 500142551e99SRandall Stewart 500242551e99SRandall Stewart sctppcbinfo.sctp_vrfhash = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, 500342551e99SRandall Stewart &sctppcbinfo.hashvrfmark); 500442551e99SRandall Stewart 5005f4c93d24SRandall Stewart sctppcbinfo.vrf_ifn_hash = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, 5006f4c93d24SRandall Stewart &sctppcbinfo.vrf_ifn_hashmark); 5007f4c93d24SRandall Stewart 5008f8829a4aSRandall Stewart /* init the zones */ 5009f8829a4aSRandall Stewart /* 5010f8829a4aSRandall Stewart * FIX ME: Should check for NULL returns, but if it does fail we are 5011f8829a4aSRandall Stewart * doomed to panic anyways... add later maybe. 5012f8829a4aSRandall Stewart */ 5013f8829a4aSRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep", 5014f8829a4aSRandall Stewart sizeof(struct sctp_inpcb), maxsockets); 5015f8829a4aSRandall Stewart 5016f8829a4aSRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc", 5017f8829a4aSRandall Stewart sizeof(struct sctp_tcb), sctp_max_number_of_assoc); 5018f8829a4aSRandall Stewart 5019f8829a4aSRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr", 5020f8829a4aSRandall Stewart sizeof(struct sctp_laddr), 5021f8829a4aSRandall Stewart (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 5022f8829a4aSRandall Stewart 5023f8829a4aSRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr", 5024f8829a4aSRandall Stewart sizeof(struct sctp_nets), 5025f8829a4aSRandall Stewart (sctp_max_number_of_assoc * sctp_scale_up_for_address)); 5026f8829a4aSRandall Stewart 5027f8829a4aSRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk", 5028f8829a4aSRandall Stewart sizeof(struct sctp_tmit_chunk), 5029f8829a4aSRandall Stewart (sctp_max_number_of_assoc * sctp_chunkscale)); 5030f8829a4aSRandall Stewart 5031f8829a4aSRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq", 5032f8829a4aSRandall Stewart sizeof(struct sctp_queued_to_read), 5033f8829a4aSRandall Stewart (sctp_max_number_of_assoc * sctp_chunkscale)); 5034f8829a4aSRandall Stewart 5035f8829a4aSRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out", 5036f8829a4aSRandall Stewart sizeof(struct sctp_stream_queue_pending), 5037f8829a4aSRandall Stewart (sctp_max_number_of_assoc * sctp_chunkscale)); 5038f8829a4aSRandall Stewart 50392afb3e84SRandall Stewart SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asconf_ack, "sctp_asconf_ack", 50402afb3e84SRandall Stewart sizeof(struct sctp_asconf_ack), 50412afb3e84SRandall Stewart (sctp_max_number_of_assoc * sctp_chunkscale)); 50422afb3e84SRandall Stewart 50432afb3e84SRandall Stewart 5044f8829a4aSRandall Stewart /* Master Lock INIT for info structure */ 5045f8829a4aSRandall Stewart SCTP_INP_INFO_LOCK_INIT(); 5046f8829a4aSRandall Stewart SCTP_STATLOG_INIT_LOCK(); 5047f8829a4aSRandall Stewart SCTP_ITERATOR_LOCK_INIT(); 5048a5d547adSRandall Stewart 5049f8829a4aSRandall Stewart SCTP_IPI_COUNT_INIT(); 5050f8829a4aSRandall Stewart SCTP_IPI_ADDR_INIT(); 505142551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_INIT(); 5052207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING 5053207304d4SRandall Stewart SCTP_IP_PKTLOG_INIT(); 5054207304d4SRandall Stewart #endif 5055f8829a4aSRandall Stewart LIST_INIT(&sctppcbinfo.addr_wq); 5056f8829a4aSRandall Stewart 5057f8829a4aSRandall Stewart /* not sure if we need all the counts */ 5058f8829a4aSRandall Stewart sctppcbinfo.ipi_count_ep = 0; 5059f8829a4aSRandall Stewart /* assoc/tcb zone info */ 5060f8829a4aSRandall Stewart sctppcbinfo.ipi_count_asoc = 0; 5061f8829a4aSRandall Stewart /* local addrlist zone info */ 5062f8829a4aSRandall Stewart sctppcbinfo.ipi_count_laddr = 0; 5063f8829a4aSRandall Stewart /* remote addrlist zone info */ 5064f8829a4aSRandall Stewart sctppcbinfo.ipi_count_raddr = 0; 5065f8829a4aSRandall Stewart /* chunk info */ 5066f8829a4aSRandall Stewart sctppcbinfo.ipi_count_chunk = 0; 5067f8829a4aSRandall Stewart 5068f8829a4aSRandall Stewart /* socket queue zone info */ 5069f8829a4aSRandall Stewart sctppcbinfo.ipi_count_readq = 0; 5070f8829a4aSRandall Stewart 5071f8829a4aSRandall Stewart /* stream out queue cont */ 5072f8829a4aSRandall Stewart sctppcbinfo.ipi_count_strmoq = 0; 5073f8829a4aSRandall Stewart 5074f8829a4aSRandall Stewart sctppcbinfo.ipi_free_strmoq = 0; 5075f8829a4aSRandall Stewart sctppcbinfo.ipi_free_chunks = 0; 5076f8829a4aSRandall Stewart 5077139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer); 5078f8829a4aSRandall Stewart 5079f8829a4aSRandall Stewart /* Init the TIMEWAIT list */ 50800696e120SRandall Stewart for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) { 5081f8829a4aSRandall Stewart LIST_INIT(&sctppcbinfo.vtag_timewait[i]); 5082f8829a4aSRandall Stewart } 5083f8829a4aSRandall Stewart 508442551e99SRandall Stewart #if defined(SCTP_USE_THREAD_BASED_ITERATOR) 508542551e99SRandall Stewart sctppcbinfo.iterator_running = 0; 508642551e99SRandall Stewart sctp_startup_iterator(); 508742551e99SRandall Stewart #endif 508842551e99SRandall Stewart 508942551e99SRandall Stewart /* 509042551e99SRandall Stewart * INIT the default VRF which for BSD is the only one, other O/S's 509142551e99SRandall Stewart * may have more. But initially they must start with one and then 509242551e99SRandall Stewart * add the VRF's as addresses are added. 509342551e99SRandall Stewart */ 509442551e99SRandall Stewart sctp_init_vrf_list(SCTP_DEFAULT_VRF); 509542551e99SRandall Stewart 5096f8829a4aSRandall Stewart } 5097f8829a4aSRandall Stewart 5098f8829a4aSRandall Stewart 5099f8829a4aSRandall Stewart int 5100f8829a4aSRandall Stewart sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, 5101f8829a4aSRandall Stewart int iphlen, int offset, int limit, struct sctphdr *sh, 5102f8829a4aSRandall Stewart struct sockaddr *altsa) 5103f8829a4aSRandall Stewart { 5104f8829a4aSRandall Stewart /* 5105f8829a4aSRandall Stewart * grub through the INIT pulling addresses and loading them to the 5106f8829a4aSRandall Stewart * nets structure in the asoc. The from address in the mbuf should 5107f8829a4aSRandall Stewart * also be loaded (if it is not already). This routine can be called 5108f8829a4aSRandall Stewart * with either INIT or INIT-ACK's as long as the m points to the IP 5109f8829a4aSRandall Stewart * packet and the offset points to the beginning of the parameters. 5110f8829a4aSRandall Stewart */ 5111f8829a4aSRandall Stewart struct sctp_inpcb *inp, *l_inp; 5112f8829a4aSRandall Stewart struct sctp_nets *net, *net_tmp; 5113f8829a4aSRandall Stewart struct ip *iph; 5114f8829a4aSRandall Stewart struct sctp_paramhdr *phdr, parm_buf; 5115f8829a4aSRandall Stewart struct sctp_tcb *stcb_tmp; 5116f8829a4aSRandall Stewart uint16_t ptype, plen; 5117f8829a4aSRandall Stewart struct sockaddr *sa; 5118f8829a4aSRandall Stewart struct sockaddr_storage dest_store; 5119f8829a4aSRandall Stewart struct sockaddr *local_sa = (struct sockaddr *)&dest_store; 5120f8829a4aSRandall Stewart struct sockaddr_in sin; 5121f8829a4aSRandall Stewart struct sockaddr_in6 sin6; 5122f42a358aSRandall Stewart uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; 51235e54f665SRandall Stewart struct sctp_auth_random *p_random = NULL; 5124f8829a4aSRandall Stewart uint16_t random_len = 0; 5125f42a358aSRandall Stewart uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; 5126f8829a4aSRandall Stewart struct sctp_auth_hmac_algo *hmacs = NULL; 5127f8829a4aSRandall Stewart uint16_t hmacs_len = 0; 51282afb3e84SRandall Stewart uint8_t saw_asconf = 0; 51292afb3e84SRandall Stewart uint8_t saw_asconf_ack = 0; 5130f42a358aSRandall Stewart uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; 5131f8829a4aSRandall Stewart struct sctp_auth_chunk_list *chunks = NULL; 5132f8829a4aSRandall Stewart uint16_t num_chunks = 0; 5133f8829a4aSRandall Stewart sctp_key_t *new_key; 5134f8829a4aSRandall Stewart uint32_t keylen; 5135f8829a4aSRandall Stewart int got_random = 0, got_hmacs = 0, got_chklist = 0; 5136f8829a4aSRandall Stewart 5137f8829a4aSRandall Stewart /* First get the destination address setup too. */ 5138f8829a4aSRandall Stewart memset(&sin, 0, sizeof(sin)); 5139f8829a4aSRandall Stewart memset(&sin6, 0, sizeof(sin6)); 5140f8829a4aSRandall Stewart 5141f8829a4aSRandall Stewart sin.sin_family = AF_INET; 5142f8829a4aSRandall Stewart sin.sin_len = sizeof(sin); 5143f8829a4aSRandall Stewart sin.sin_port = stcb->rport; 5144f8829a4aSRandall Stewart 5145f8829a4aSRandall Stewart sin6.sin6_family = AF_INET6; 5146f8829a4aSRandall Stewart sin6.sin6_len = sizeof(struct sockaddr_in6); 5147f8829a4aSRandall Stewart sin6.sin6_port = stcb->rport; 5148f8829a4aSRandall Stewart if (altsa == NULL) { 5149f8829a4aSRandall Stewart iph = mtod(m, struct ip *); 5150f8829a4aSRandall Stewart if (iph->ip_v == IPVERSION) { 5151f8829a4aSRandall Stewart /* its IPv4 */ 5152f8829a4aSRandall Stewart struct sockaddr_in *sin_2; 5153f8829a4aSRandall Stewart 5154f8829a4aSRandall Stewart sin_2 = (struct sockaddr_in *)(local_sa); 5155f8829a4aSRandall Stewart memset(sin_2, 0, sizeof(sin)); 5156f8829a4aSRandall Stewart sin_2->sin_family = AF_INET; 5157f8829a4aSRandall Stewart sin_2->sin_len = sizeof(sin); 5158f8829a4aSRandall Stewart sin_2->sin_port = sh->dest_port; 5159f8829a4aSRandall Stewart sin_2->sin_addr.s_addr = iph->ip_dst.s_addr; 5160f8829a4aSRandall Stewart sin.sin_addr = iph->ip_src; 5161f8829a4aSRandall Stewart sa = (struct sockaddr *)&sin; 5162f8829a4aSRandall Stewart } else if (iph->ip_v == (IPV6_VERSION >> 4)) { 5163f8829a4aSRandall Stewart /* its IPv6 */ 5164f8829a4aSRandall Stewart struct ip6_hdr *ip6; 5165f8829a4aSRandall Stewart struct sockaddr_in6 *sin6_2; 5166f8829a4aSRandall Stewart 5167f8829a4aSRandall Stewart ip6 = mtod(m, struct ip6_hdr *); 5168f8829a4aSRandall Stewart sin6_2 = (struct sockaddr_in6 *)(local_sa); 5169f8829a4aSRandall Stewart memset(sin6_2, 0, sizeof(sin6)); 5170f8829a4aSRandall Stewart sin6_2->sin6_family = AF_INET6; 5171f8829a4aSRandall Stewart sin6_2->sin6_len = sizeof(struct sockaddr_in6); 5172f8829a4aSRandall Stewart sin6_2->sin6_port = sh->dest_port; 5173f8829a4aSRandall Stewart sin6.sin6_addr = ip6->ip6_src; 5174f8829a4aSRandall Stewart sa = (struct sockaddr *)&sin6; 5175f8829a4aSRandall Stewart } else { 5176f8829a4aSRandall Stewart sa = NULL; 5177f8829a4aSRandall Stewart } 5178f8829a4aSRandall Stewart } else { 5179f8829a4aSRandall Stewart /* 5180f8829a4aSRandall Stewart * For cookies we use the src address NOT from the packet 5181f8829a4aSRandall Stewart * but from the original INIT 5182f8829a4aSRandall Stewart */ 5183f8829a4aSRandall Stewart sa = altsa; 5184f8829a4aSRandall Stewart } 5185f8829a4aSRandall Stewart /* Turn off ECN until we get through all params */ 5186f8829a4aSRandall Stewart stcb->asoc.ecn_allowed = 0; 5187f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5188f8829a4aSRandall Stewart /* mark all addresses that we have currently on the list */ 5189f8829a4aSRandall Stewart net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; 5190f8829a4aSRandall Stewart } 5191f8829a4aSRandall Stewart /* does the source address already exist? if so skip it */ 5192f8829a4aSRandall Stewart l_inp = inp = stcb->sctp_ep; 5193f8829a4aSRandall Stewart 519450cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 5195f8829a4aSRandall Stewart stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb); 519650cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 5197f8829a4aSRandall Stewart 5198f8829a4aSRandall Stewart if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { 5199f8829a4aSRandall Stewart /* we must add the source address */ 5200f8829a4aSRandall Stewart /* no scope set here since we have a tcb already. */ 5201f8829a4aSRandall Stewart if ((sa->sa_family == AF_INET) && 5202f8829a4aSRandall Stewart (stcb->asoc.ipv4_addr_legal)) { 5203a5d547adSRandall Stewart if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) { 5204f8829a4aSRandall Stewart return (-1); 5205f8829a4aSRandall Stewart } 5206f8829a4aSRandall Stewart } else if ((sa->sa_family == AF_INET6) && 5207f8829a4aSRandall Stewart (stcb->asoc.ipv6_addr_legal)) { 5208a5d547adSRandall Stewart if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { 5209f8829a4aSRandall Stewart return (-2); 5210f8829a4aSRandall Stewart } 5211f8829a4aSRandall Stewart } 5212f8829a4aSRandall Stewart } else { 5213f8829a4aSRandall Stewart if (net_tmp != NULL && stcb_tmp == stcb) { 5214f8829a4aSRandall Stewart net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; 5215f8829a4aSRandall Stewart } else if (stcb_tmp != stcb) { 5216f8829a4aSRandall Stewart /* It belongs to another association? */ 5217eacc51c5SRandall Stewart if (stcb_tmp) 5218f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb_tmp); 5219f8829a4aSRandall Stewart return (-3); 5220f8829a4aSRandall Stewart } 5221f8829a4aSRandall Stewart } 5222f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 5223f8829a4aSRandall Stewart /* the assoc was freed? */ 5224f8829a4aSRandall Stewart return (-4); 5225f8829a4aSRandall Stewart } 522618e198d3SRandall Stewart /* 522718e198d3SRandall Stewart * peer must explicitly turn this on. This may have been initialized 522818e198d3SRandall Stewart * to be "on" in order to allow local addr changes while INIT's are 522918e198d3SRandall Stewart * in flight. 523018e198d3SRandall Stewart */ 523118e198d3SRandall Stewart stcb->asoc.peer_supports_asconf = 0; 5232f8829a4aSRandall Stewart /* now we must go through each of the params. */ 5233f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); 5234f8829a4aSRandall Stewart while (phdr) { 5235f8829a4aSRandall Stewart ptype = ntohs(phdr->param_type); 5236f8829a4aSRandall Stewart plen = ntohs(phdr->param_length); 5237f8829a4aSRandall Stewart /* 5238f8829a4aSRandall Stewart * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype, 5239f8829a4aSRandall Stewart * (int)plen); 5240f8829a4aSRandall Stewart */ 5241f8829a4aSRandall Stewart if (offset + plen > limit) { 5242f8829a4aSRandall Stewart break; 5243f8829a4aSRandall Stewart } 5244f8829a4aSRandall Stewart if (plen == 0) { 5245f8829a4aSRandall Stewart break; 5246f8829a4aSRandall Stewart } 5247f8829a4aSRandall Stewart if (ptype == SCTP_IPV4_ADDRESS) { 5248f8829a4aSRandall Stewart if (stcb->asoc.ipv4_addr_legal) { 5249f8829a4aSRandall Stewart struct sctp_ipv4addr_param *p4, p4_buf; 5250f8829a4aSRandall Stewart 5251f8829a4aSRandall Stewart /* ok get the v4 address and check/add */ 5252f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5253f8829a4aSRandall Stewart (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf)); 5254f8829a4aSRandall Stewart if (plen != sizeof(struct sctp_ipv4addr_param) || 5255f8829a4aSRandall Stewart phdr == NULL) { 5256f8829a4aSRandall Stewart return (-5); 5257f8829a4aSRandall Stewart } 5258f8829a4aSRandall Stewart p4 = (struct sctp_ipv4addr_param *)phdr; 5259f8829a4aSRandall Stewart sin.sin_addr.s_addr = p4->addr; 5260d06c82f1SRandall Stewart if (IN_MULTICAST(sin.sin_addr.s_addr)) { 5261d06c82f1SRandall Stewart /* Skip multi-cast addresses */ 5262d06c82f1SRandall Stewart goto next_param; 5263d06c82f1SRandall Stewart } 526417205eccSRandall Stewart if ((sin.sin_addr.s_addr == INADDR_BROADCAST) || 526517205eccSRandall Stewart (sin.sin_addr.s_addr == INADDR_ANY)) { 526617205eccSRandall Stewart goto next_param; 526717205eccSRandall Stewart } 5268f8829a4aSRandall Stewart sa = (struct sockaddr *)&sin; 5269f8829a4aSRandall Stewart inp = stcb->sctp_ep; 527050cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 5271f8829a4aSRandall Stewart stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 5272f8829a4aSRandall Stewart local_sa, stcb); 527350cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 5274f8829a4aSRandall Stewart 5275f8829a4aSRandall Stewart if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || 5276f8829a4aSRandall Stewart inp == NULL) { 5277f8829a4aSRandall Stewart /* we must add the source address */ 5278f8829a4aSRandall Stewart /* 5279f8829a4aSRandall Stewart * no scope set since we have a tcb 5280f8829a4aSRandall Stewart * already 5281f8829a4aSRandall Stewart */ 5282f8829a4aSRandall Stewart 5283f8829a4aSRandall Stewart /* 5284f8829a4aSRandall Stewart * we must validate the state again 5285f8829a4aSRandall Stewart * here 5286f8829a4aSRandall Stewart */ 5287f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 5288f8829a4aSRandall Stewart /* the assoc was freed? */ 5289f8829a4aSRandall Stewart return (-7); 5290f8829a4aSRandall Stewart } 5291a5d547adSRandall Stewart if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) { 5292f8829a4aSRandall Stewart return (-8); 5293f8829a4aSRandall Stewart } 5294f8829a4aSRandall Stewart } else if (stcb_tmp == stcb) { 5295f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 5296f8829a4aSRandall Stewart /* the assoc was freed? */ 5297f8829a4aSRandall Stewart return (-10); 5298f8829a4aSRandall Stewart } 5299f8829a4aSRandall Stewart if (net != NULL) { 5300f8829a4aSRandall Stewart /* clear flag */ 5301f8829a4aSRandall Stewart net->dest_state &= 5302f8829a4aSRandall Stewart ~SCTP_ADDR_NOT_IN_ASSOC; 5303f8829a4aSRandall Stewart } 5304f8829a4aSRandall Stewart } else { 5305f8829a4aSRandall Stewart /* 5306f8829a4aSRandall Stewart * strange, address is in another 5307f8829a4aSRandall Stewart * assoc? straighten out locks. 5308f8829a4aSRandall Stewart */ 530952be287eSRandall Stewart if (stcb_tmp) 5310b54d3a6cSRandall Stewart SCTP_TCB_UNLOCK(stcb_tmp); 531152be287eSRandall Stewart 5312f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 5313f8829a4aSRandall Stewart /* the assoc was freed? */ 5314f8829a4aSRandall Stewart return (-12); 5315f8829a4aSRandall Stewart } 5316f8829a4aSRandall Stewart return (-13); 5317f8829a4aSRandall Stewart } 5318f8829a4aSRandall Stewart } 5319f8829a4aSRandall Stewart } else if (ptype == SCTP_IPV6_ADDRESS) { 5320f8829a4aSRandall Stewart if (stcb->asoc.ipv6_addr_legal) { 5321f8829a4aSRandall Stewart /* ok get the v6 address and check/add */ 5322f8829a4aSRandall Stewart struct sctp_ipv6addr_param *p6, p6_buf; 5323f8829a4aSRandall Stewart 5324f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5325f8829a4aSRandall Stewart (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf)); 5326f8829a4aSRandall Stewart if (plen != sizeof(struct sctp_ipv6addr_param) || 5327f8829a4aSRandall Stewart phdr == NULL) { 5328f8829a4aSRandall Stewart return (-14); 5329f8829a4aSRandall Stewart } 5330f8829a4aSRandall Stewart p6 = (struct sctp_ipv6addr_param *)phdr; 5331f8829a4aSRandall Stewart memcpy((caddr_t)&sin6.sin6_addr, p6->addr, 5332f8829a4aSRandall Stewart sizeof(p6->addr)); 5333d06c82f1SRandall Stewart if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { 5334d06c82f1SRandall Stewart /* Skip multi-cast addresses */ 5335d06c82f1SRandall Stewart goto next_param; 5336d06c82f1SRandall Stewart } 5337d06c82f1SRandall Stewart if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { 5338d06c82f1SRandall Stewart /* 5339d06c82f1SRandall Stewart * Link local make no sense without 5340d06c82f1SRandall Stewart * scope 5341d06c82f1SRandall Stewart */ 5342d06c82f1SRandall Stewart goto next_param; 5343d06c82f1SRandall Stewart } 5344f8829a4aSRandall Stewart sa = (struct sockaddr *)&sin6; 5345f8829a4aSRandall Stewart inp = stcb->sctp_ep; 534650cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 5347f8829a4aSRandall Stewart stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, 5348f8829a4aSRandall Stewart local_sa, stcb); 534950cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 5350f8829a4aSRandall Stewart if (stcb_tmp == NULL && (inp == stcb->sctp_ep || 5351f8829a4aSRandall Stewart inp == NULL)) { 5352f8829a4aSRandall Stewart /* 5353f8829a4aSRandall Stewart * we must validate the state again 5354f8829a4aSRandall Stewart * here 5355f8829a4aSRandall Stewart */ 5356f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 5357f8829a4aSRandall Stewart /* the assoc was freed? */ 5358f8829a4aSRandall Stewart return (-16); 5359f8829a4aSRandall Stewart } 5360f8829a4aSRandall Stewart /* 5361f8829a4aSRandall Stewart * we must add the address, no scope 5362f8829a4aSRandall Stewart * set 5363f8829a4aSRandall Stewart */ 5364a5d547adSRandall Stewart if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) { 5365f8829a4aSRandall Stewart return (-17); 5366f8829a4aSRandall Stewart } 5367f8829a4aSRandall Stewart } else if (stcb_tmp == stcb) { 5368f8829a4aSRandall Stewart /* 5369f8829a4aSRandall Stewart * we must validate the state again 5370f8829a4aSRandall Stewart * here 5371f8829a4aSRandall Stewart */ 5372f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 5373f8829a4aSRandall Stewart /* the assoc was freed? */ 5374f8829a4aSRandall Stewart return (-19); 5375f8829a4aSRandall Stewart } 5376f8829a4aSRandall Stewart if (net != NULL) { 5377f8829a4aSRandall Stewart /* clear flag */ 5378f8829a4aSRandall Stewart net->dest_state &= 5379f8829a4aSRandall Stewart ~SCTP_ADDR_NOT_IN_ASSOC; 5380f8829a4aSRandall Stewart } 5381f8829a4aSRandall Stewart } else { 5382f8829a4aSRandall Stewart /* 5383f8829a4aSRandall Stewart * strange, address is in another 5384f8829a4aSRandall Stewart * assoc? straighten out locks. 5385f8829a4aSRandall Stewart */ 538652be287eSRandall Stewart if (stcb_tmp) 5387b54d3a6cSRandall Stewart SCTP_TCB_UNLOCK(stcb_tmp); 538852be287eSRandall Stewart 5389f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 5390f8829a4aSRandall Stewart /* the assoc was freed? */ 5391f8829a4aSRandall Stewart return (-21); 5392f8829a4aSRandall Stewart } 5393f8829a4aSRandall Stewart return (-22); 5394f8829a4aSRandall Stewart } 5395f8829a4aSRandall Stewart } 5396f8829a4aSRandall Stewart } else if (ptype == SCTP_ECN_CAPABLE) { 5397f8829a4aSRandall Stewart stcb->asoc.ecn_allowed = 1; 5398f8829a4aSRandall Stewart } else if (ptype == SCTP_ULP_ADAPTATION) { 5399f8829a4aSRandall Stewart if (stcb->asoc.state != SCTP_STATE_OPEN) { 5400f8829a4aSRandall Stewart struct sctp_adaptation_layer_indication ai, 5401f8829a4aSRandall Stewart *aip; 5402f8829a4aSRandall Stewart 5403f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5404f8829a4aSRandall Stewart (struct sctp_paramhdr *)&ai, sizeof(ai)); 5405f8829a4aSRandall Stewart aip = (struct sctp_adaptation_layer_indication *)phdr; 54066e55db54SRandall Stewart if (aip) { 54072afb3e84SRandall Stewart stcb->asoc.peers_adaptation = ntohl(aip->indication); 54082afb3e84SRandall Stewart stcb->asoc.adaptation_needed = 1; 5409f8829a4aSRandall Stewart } 54106e55db54SRandall Stewart } 5411f8829a4aSRandall Stewart } else if (ptype == SCTP_SET_PRIM_ADDR) { 5412f8829a4aSRandall Stewart struct sctp_asconf_addr_param lstore, *fee; 5413f8829a4aSRandall Stewart struct sctp_asconf_addrv4_param *fii; 5414f8829a4aSRandall Stewart int lptype; 5415f8829a4aSRandall Stewart struct sockaddr *lsa = NULL; 5416f8829a4aSRandall Stewart 5417f8829a4aSRandall Stewart stcb->asoc.peer_supports_asconf = 1; 5418f8829a4aSRandall Stewart if (plen > sizeof(lstore)) { 5419f8829a4aSRandall Stewart return (-23); 5420f8829a4aSRandall Stewart } 5421f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5422d06c82f1SRandall Stewart (struct sctp_paramhdr *)&lstore, min(plen, sizeof(lstore))); 5423f8829a4aSRandall Stewart if (phdr == NULL) { 5424f8829a4aSRandall Stewart return (-24); 5425f8829a4aSRandall Stewart } 5426f8829a4aSRandall Stewart fee = (struct sctp_asconf_addr_param *)phdr; 5427f8829a4aSRandall Stewart lptype = ntohs(fee->addrp.ph.param_type); 5428f8829a4aSRandall Stewart if (lptype == SCTP_IPV4_ADDRESS) { 5429f8829a4aSRandall Stewart if (plen != 5430f8829a4aSRandall Stewart sizeof(struct sctp_asconf_addrv4_param)) { 5431ad81507eSRandall Stewart SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", 5432f8829a4aSRandall Stewart (int)sizeof(struct sctp_asconf_addrv4_param), 5433f8829a4aSRandall Stewart plen); 5434f8829a4aSRandall Stewart } else { 5435f8829a4aSRandall Stewart fii = (struct sctp_asconf_addrv4_param *)fee; 5436f8829a4aSRandall Stewart sin.sin_addr.s_addr = fii->addrp.addr; 5437f8829a4aSRandall Stewart lsa = (struct sockaddr *)&sin; 5438f8829a4aSRandall Stewart } 5439f8829a4aSRandall Stewart } else if (lptype == SCTP_IPV6_ADDRESS) { 5440f8829a4aSRandall Stewart if (plen != 5441f8829a4aSRandall Stewart sizeof(struct sctp_asconf_addr_param)) { 5442ad81507eSRandall Stewart SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", 5443f8829a4aSRandall Stewart (int)sizeof(struct sctp_asconf_addr_param), 5444f8829a4aSRandall Stewart plen); 5445f8829a4aSRandall Stewart } else { 5446f8829a4aSRandall Stewart memcpy(sin6.sin6_addr.s6_addr, 5447f8829a4aSRandall Stewart fee->addrp.addr, 5448f8829a4aSRandall Stewart sizeof(fee->addrp.addr)); 5449f8829a4aSRandall Stewart lsa = (struct sockaddr *)&sin6; 5450f8829a4aSRandall Stewart } 5451f8829a4aSRandall Stewart } 5452f8829a4aSRandall Stewart if (lsa) { 5453ad81507eSRandall Stewart (void)sctp_set_primary_addr(stcb, sa, NULL); 5454f8829a4aSRandall Stewart } 5455f8829a4aSRandall Stewart } else if (ptype == SCTP_PRSCTP_SUPPORTED) { 5456f8829a4aSRandall Stewart /* Peer supports pr-sctp */ 5457f8829a4aSRandall Stewart stcb->asoc.peer_supports_prsctp = 1; 5458f8829a4aSRandall Stewart } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 5459f8829a4aSRandall Stewart /* A supported extension chunk */ 5460f8829a4aSRandall Stewart struct sctp_supported_chunk_types_param *pr_supported; 5461f42a358aSRandall Stewart uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; 5462f8829a4aSRandall Stewart int num_ent, i; 5463f8829a4aSRandall Stewart 5464f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5465d06c82f1SRandall Stewart (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen)); 5466f8829a4aSRandall Stewart if (phdr == NULL) { 5467f8829a4aSRandall Stewart return (-25); 5468f8829a4aSRandall Stewart } 5469f8829a4aSRandall Stewart stcb->asoc.peer_supports_asconf = 0; 5470f8829a4aSRandall Stewart stcb->asoc.peer_supports_prsctp = 0; 5471f8829a4aSRandall Stewart stcb->asoc.peer_supports_pktdrop = 0; 5472f8829a4aSRandall Stewart stcb->asoc.peer_supports_strreset = 0; 5473f8829a4aSRandall Stewart stcb->asoc.peer_supports_auth = 0; 5474f8829a4aSRandall Stewart pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 5475f8829a4aSRandall Stewart num_ent = plen - sizeof(struct sctp_paramhdr); 5476f8829a4aSRandall Stewart for (i = 0; i < num_ent; i++) { 5477f8829a4aSRandall Stewart switch (pr_supported->chunk_types[i]) { 5478f8829a4aSRandall Stewart case SCTP_ASCONF: 5479f8829a4aSRandall Stewart case SCTP_ASCONF_ACK: 5480f8829a4aSRandall Stewart stcb->asoc.peer_supports_asconf = 1; 5481f8829a4aSRandall Stewart break; 5482f8829a4aSRandall Stewart case SCTP_FORWARD_CUM_TSN: 5483f8829a4aSRandall Stewart stcb->asoc.peer_supports_prsctp = 1; 5484f8829a4aSRandall Stewart break; 5485f8829a4aSRandall Stewart case SCTP_PACKET_DROPPED: 5486f8829a4aSRandall Stewart stcb->asoc.peer_supports_pktdrop = 1; 5487f8829a4aSRandall Stewart break; 5488f8829a4aSRandall Stewart case SCTP_STREAM_RESET: 5489f8829a4aSRandall Stewart stcb->asoc.peer_supports_strreset = 1; 5490f8829a4aSRandall Stewart break; 5491f8829a4aSRandall Stewart case SCTP_AUTHENTICATION: 5492f8829a4aSRandall Stewart stcb->asoc.peer_supports_auth = 1; 5493f8829a4aSRandall Stewart break; 5494f8829a4aSRandall Stewart default: 5495f8829a4aSRandall Stewart /* one I have not learned yet */ 5496f8829a4aSRandall Stewart break; 5497f8829a4aSRandall Stewart 5498f8829a4aSRandall Stewart } 5499f8829a4aSRandall Stewart } 5500f8829a4aSRandall Stewart } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) { 5501f8829a4aSRandall Stewart /* Peer supports ECN-nonce */ 5502f8829a4aSRandall Stewart stcb->asoc.peer_supports_ecn_nonce = 1; 5503f8829a4aSRandall Stewart stcb->asoc.ecn_nonce_allowed = 1; 5504f8829a4aSRandall Stewart } else if (ptype == SCTP_RANDOM) { 5505f42a358aSRandall Stewart if (plen > sizeof(random_store)) 5506f8829a4aSRandall Stewart break; 5507f8829a4aSRandall Stewart if (got_random) { 5508f8829a4aSRandall Stewart /* already processed a RANDOM */ 5509f8829a4aSRandall Stewart goto next_param; 5510f8829a4aSRandall Stewart } 5511f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5512f42a358aSRandall Stewart (struct sctp_paramhdr *)random_store, 5513d06c82f1SRandall Stewart min(sizeof(random_store), plen)); 5514f8829a4aSRandall Stewart if (phdr == NULL) 5515f8829a4aSRandall Stewart return (-26); 55165e54f665SRandall Stewart p_random = (struct sctp_auth_random *)phdr; 55175e54f665SRandall Stewart random_len = plen - sizeof(*p_random); 5518f8829a4aSRandall Stewart /* enforce the random length */ 5519f8829a4aSRandall Stewart if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { 5520ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); 5521f8829a4aSRandall Stewart return (-27); 5522f8829a4aSRandall Stewart } 5523f8829a4aSRandall Stewart got_random = 1; 5524f8829a4aSRandall Stewart } else if (ptype == SCTP_HMAC_LIST) { 5525f8829a4aSRandall Stewart int num_hmacs; 5526f8829a4aSRandall Stewart int i; 5527f8829a4aSRandall Stewart 5528f42a358aSRandall Stewart if (plen > sizeof(hmacs_store)) 5529f8829a4aSRandall Stewart break; 5530f8829a4aSRandall Stewart if (got_hmacs) { 5531f8829a4aSRandall Stewart /* already processed a HMAC list */ 5532f8829a4aSRandall Stewart goto next_param; 5533f8829a4aSRandall Stewart } 5534f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5535f42a358aSRandall Stewart (struct sctp_paramhdr *)hmacs_store, 5536d06c82f1SRandall Stewart min(plen, sizeof(hmacs_store))); 5537f8829a4aSRandall Stewart if (phdr == NULL) 5538f8829a4aSRandall Stewart return (-28); 5539f8829a4aSRandall Stewart hmacs = (struct sctp_auth_hmac_algo *)phdr; 5540f8829a4aSRandall Stewart hmacs_len = plen - sizeof(*hmacs); 5541f8829a4aSRandall Stewart num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 5542f8829a4aSRandall Stewart /* validate the hmac list */ 5543f8829a4aSRandall Stewart if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 5544f8829a4aSRandall Stewart return (-29); 5545f8829a4aSRandall Stewart } 5546f8829a4aSRandall Stewart if (stcb->asoc.peer_hmacs != NULL) 5547f8829a4aSRandall Stewart sctp_free_hmaclist(stcb->asoc.peer_hmacs); 5548f8829a4aSRandall Stewart stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); 5549f8829a4aSRandall Stewart if (stcb->asoc.peer_hmacs != NULL) { 5550f8829a4aSRandall Stewart for (i = 0; i < num_hmacs; i++) { 55516e55db54SRandall Stewart (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, 5552f8829a4aSRandall Stewart ntohs(hmacs->hmac_ids[i])); 5553f8829a4aSRandall Stewart } 5554f8829a4aSRandall Stewart } 5555f8829a4aSRandall Stewart got_hmacs = 1; 5556f8829a4aSRandall Stewart } else if (ptype == SCTP_CHUNK_LIST) { 5557f8829a4aSRandall Stewart int i; 5558f8829a4aSRandall Stewart 5559f42a358aSRandall Stewart if (plen > sizeof(chunks_store)) 5560f8829a4aSRandall Stewart break; 5561f8829a4aSRandall Stewart if (got_chklist) { 5562f8829a4aSRandall Stewart /* already processed a Chunks list */ 5563f8829a4aSRandall Stewart goto next_param; 5564f8829a4aSRandall Stewart } 5565f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, 5566f42a358aSRandall Stewart (struct sctp_paramhdr *)chunks_store, 5567d06c82f1SRandall Stewart min(plen, sizeof(chunks_store))); 5568f8829a4aSRandall Stewart if (phdr == NULL) 5569f8829a4aSRandall Stewart return (-30); 5570f8829a4aSRandall Stewart chunks = (struct sctp_auth_chunk_list *)phdr; 5571f8829a4aSRandall Stewart num_chunks = plen - sizeof(*chunks); 5572f8829a4aSRandall Stewart if (stcb->asoc.peer_auth_chunks != NULL) 5573f8829a4aSRandall Stewart sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); 5574f8829a4aSRandall Stewart else 5575f8829a4aSRandall Stewart stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); 5576f8829a4aSRandall Stewart for (i = 0; i < num_chunks; i++) { 55776e55db54SRandall Stewart (void)sctp_auth_add_chunk(chunks->chunk_types[i], 5578f8829a4aSRandall Stewart stcb->asoc.peer_auth_chunks); 55792afb3e84SRandall Stewart /* record asconf/asconf-ack if listed */ 55802afb3e84SRandall Stewart if (chunks->chunk_types[i] == SCTP_ASCONF) 55812afb3e84SRandall Stewart saw_asconf = 1; 55822afb3e84SRandall Stewart if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) 55832afb3e84SRandall Stewart saw_asconf_ack = 1; 55842afb3e84SRandall Stewart 5585f8829a4aSRandall Stewart } 5586f8829a4aSRandall Stewart got_chklist = 1; 5587f8829a4aSRandall Stewart } else if ((ptype == SCTP_HEARTBEAT_INFO) || 5588f8829a4aSRandall Stewart (ptype == SCTP_STATE_COOKIE) || 5589f8829a4aSRandall Stewart (ptype == SCTP_UNRECOG_PARAM) || 5590f8829a4aSRandall Stewart (ptype == SCTP_COOKIE_PRESERVE) || 5591f8829a4aSRandall Stewart (ptype == SCTP_SUPPORTED_ADDRTYPE) || 5592f8829a4aSRandall Stewart (ptype == SCTP_ADD_IP_ADDRESS) || 5593f8829a4aSRandall Stewart (ptype == SCTP_DEL_IP_ADDRESS) || 5594f8829a4aSRandall Stewart (ptype == SCTP_ERROR_CAUSE_IND) || 5595f8829a4aSRandall Stewart (ptype == SCTP_SUCCESS_REPORT)) { 5596f8829a4aSRandall Stewart /* don't care */ ; 5597f8829a4aSRandall Stewart } else { 5598f8829a4aSRandall Stewart if ((ptype & 0x8000) == 0x0000) { 5599f8829a4aSRandall Stewart /* 5600f8829a4aSRandall Stewart * must stop processing the rest of the 5601f8829a4aSRandall Stewart * param's. Any report bits were handled 5602f8829a4aSRandall Stewart * with the call to 5603f8829a4aSRandall Stewart * sctp_arethere_unrecognized_parameters() 5604f8829a4aSRandall Stewart * when the INIT or INIT-ACK was first seen. 5605f8829a4aSRandall Stewart */ 5606f8829a4aSRandall Stewart break; 5607f8829a4aSRandall Stewart } 5608f8829a4aSRandall Stewart } 5609f8829a4aSRandall Stewart next_param: 5610f8829a4aSRandall Stewart offset += SCTP_SIZE32(plen); 5611f8829a4aSRandall Stewart if (offset >= limit) { 5612f8829a4aSRandall Stewart break; 5613f8829a4aSRandall Stewart } 5614f8829a4aSRandall Stewart phdr = sctp_get_next_param(m, offset, &parm_buf, 5615f8829a4aSRandall Stewart sizeof(parm_buf)); 5616f8829a4aSRandall Stewart } 5617f8829a4aSRandall Stewart /* Now check to see if we need to purge any addresses */ 5618f8829a4aSRandall Stewart for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) { 5619f8829a4aSRandall Stewart net_tmp = TAILQ_NEXT(net, sctp_next); 5620f8829a4aSRandall Stewart if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == 5621f8829a4aSRandall Stewart SCTP_ADDR_NOT_IN_ASSOC) { 5622f8829a4aSRandall Stewart /* This address has been removed from the asoc */ 5623f8829a4aSRandall Stewart /* remove and free it */ 5624f8829a4aSRandall Stewart stcb->asoc.numnets--; 5625f8829a4aSRandall Stewart TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); 5626f8829a4aSRandall Stewart sctp_free_remote_addr(net); 5627f8829a4aSRandall Stewart if (net == stcb->asoc.primary_destination) { 5628f8829a4aSRandall Stewart stcb->asoc.primary_destination = NULL; 5629f8829a4aSRandall Stewart sctp_select_primary_destination(stcb); 5630f8829a4aSRandall Stewart } 5631f8829a4aSRandall Stewart } 5632f8829a4aSRandall Stewart } 5633f8829a4aSRandall Stewart /* validate authentication required parameters */ 5634f8829a4aSRandall Stewart if (got_random && got_hmacs) { 5635f8829a4aSRandall Stewart stcb->asoc.peer_supports_auth = 1; 5636f8829a4aSRandall Stewart } else { 5637f8829a4aSRandall Stewart stcb->asoc.peer_supports_auth = 0; 5638f8829a4aSRandall Stewart } 56395e54f665SRandall Stewart if (!stcb->asoc.peer_supports_auth && got_chklist) { 56405e54f665SRandall Stewart /* peer does not support auth but sent a chunks list? */ 56415e54f665SRandall Stewart return (-31); 56425e54f665SRandall Stewart } 5643f8829a4aSRandall Stewart if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf && 5644f8829a4aSRandall Stewart !stcb->asoc.peer_supports_auth) { 56455e54f665SRandall Stewart /* peer supports asconf but not auth? */ 56465e54f665SRandall Stewart return (-32); 56472afb3e84SRandall Stewart } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) && 56482afb3e84SRandall Stewart ((saw_asconf == 0) || (saw_asconf_ack == 0))) { 56492afb3e84SRandall Stewart return (-33); 5650f8829a4aSRandall Stewart } 5651f8829a4aSRandall Stewart /* concatenate the full random key */ 5652f42a358aSRandall Stewart #ifdef SCTP_AUTH_DRAFT_04 5653f42a358aSRandall Stewart keylen = random_len; 5654f8829a4aSRandall Stewart new_key = sctp_alloc_key(keylen); 5655f8829a4aSRandall Stewart if (new_key != NULL) { 5656f8829a4aSRandall Stewart /* copy in the RANDOM */ 56575e54f665SRandall Stewart if (p_random != NULL) 56585e54f665SRandall Stewart bcopy(p_random->random_data, new_key->key, random_len); 5659f42a358aSRandall Stewart } 5660f42a358aSRandall Stewart #else 56615e54f665SRandall Stewart keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks + 5662f42a358aSRandall Stewart sizeof(*hmacs) + hmacs_len; 5663f42a358aSRandall Stewart new_key = sctp_alloc_key(keylen); 5664f42a358aSRandall Stewart if (new_key != NULL) { 5665f42a358aSRandall Stewart /* copy in the RANDOM */ 56665e54f665SRandall Stewart if (p_random != NULL) { 56675e54f665SRandall Stewart keylen = sizeof(*p_random) + random_len; 56685e54f665SRandall Stewart bcopy(p_random, new_key->key, keylen); 5669f42a358aSRandall Stewart } 5670f8829a4aSRandall Stewart /* append in the AUTH chunks */ 5671f42a358aSRandall Stewart if (chunks != NULL) { 5672f42a358aSRandall Stewart bcopy(chunks, new_key->key + keylen, 5673f42a358aSRandall Stewart sizeof(*chunks) + num_chunks); 5674f42a358aSRandall Stewart keylen += sizeof(*chunks) + num_chunks; 5675f42a358aSRandall Stewart } 5676f8829a4aSRandall Stewart /* append in the HMACs */ 5677f42a358aSRandall Stewart if (hmacs != NULL) { 5678f42a358aSRandall Stewart bcopy(hmacs, new_key->key + keylen, 5679f42a358aSRandall Stewart sizeof(*hmacs) + hmacs_len); 5680f42a358aSRandall Stewart } 5681f42a358aSRandall Stewart } 5682f42a358aSRandall Stewart #endif 5683f42a358aSRandall Stewart else { 56845e54f665SRandall Stewart /* failed to get memory for the key */ 56852afb3e84SRandall Stewart return (-34); 5686f8829a4aSRandall Stewart } 5687f8829a4aSRandall Stewart if (stcb->asoc.authinfo.peer_random != NULL) 5688f8829a4aSRandall Stewart sctp_free_key(stcb->asoc.authinfo.peer_random); 5689f8829a4aSRandall Stewart stcb->asoc.authinfo.peer_random = new_key; 5690f8829a4aSRandall Stewart #ifdef SCTP_AUTH_DRAFT_04 5691f8829a4aSRandall Stewart /* don't include the chunks and hmacs for draft -04 */ 5692f8829a4aSRandall Stewart stcb->asoc.authinfo.peer_random->keylen = random_len; 5693f8829a4aSRandall Stewart #endif 5694f8829a4aSRandall Stewart sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 5695f8829a4aSRandall Stewart sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 5696f8829a4aSRandall Stewart 5697f8829a4aSRandall Stewart return (0); 5698f8829a4aSRandall Stewart } 5699f8829a4aSRandall Stewart 5700f8829a4aSRandall Stewart int 5701f8829a4aSRandall Stewart sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, 5702f8829a4aSRandall Stewart struct sctp_nets *net) 5703f8829a4aSRandall Stewart { 5704f8829a4aSRandall Stewart /* make sure the requested primary address exists in the assoc */ 5705f8829a4aSRandall Stewart if (net == NULL && sa) 5706f8829a4aSRandall Stewart net = sctp_findnet(stcb, sa); 5707f8829a4aSRandall Stewart 5708f8829a4aSRandall Stewart if (net == NULL) { 5709f8829a4aSRandall Stewart /* didn't find the requested primary address! */ 5710f8829a4aSRandall Stewart return (-1); 5711f8829a4aSRandall Stewart } else { 5712f8829a4aSRandall Stewart /* set the primary address */ 5713f8829a4aSRandall Stewart if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 571442551e99SRandall Stewart /* Must be confirmed, so queue to set */ 571542551e99SRandall Stewart net->dest_state |= SCTP_ADDR_REQ_PRIMARY; 571642551e99SRandall Stewart return (0); 5717f8829a4aSRandall Stewart } 5718f8829a4aSRandall Stewart stcb->asoc.primary_destination = net; 5719f8829a4aSRandall Stewart net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY; 5720f8829a4aSRandall Stewart net = TAILQ_FIRST(&stcb->asoc.nets); 5721f8829a4aSRandall Stewart if (net != stcb->asoc.primary_destination) { 5722f8829a4aSRandall Stewart /* 5723f8829a4aSRandall Stewart * first one on the list is NOT the primary 5724f8829a4aSRandall Stewart * sctp_cmpaddr() is much more efficent if the 5725f8829a4aSRandall Stewart * primary is the first on the list, make it so. 5726f8829a4aSRandall Stewart */ 5727f8829a4aSRandall Stewart TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 5728f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); 5729f8829a4aSRandall Stewart } 5730f8829a4aSRandall Stewart return (0); 5731f8829a4aSRandall Stewart } 5732f8829a4aSRandall Stewart } 5733f8829a4aSRandall Stewart 5734f8829a4aSRandall Stewart int 5735f8829a4aSRandall Stewart sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now) 5736f8829a4aSRandall Stewart { 5737f8829a4aSRandall Stewart /* 5738f8829a4aSRandall Stewart * This function serves two purposes. It will see if a TAG can be 5739f8829a4aSRandall Stewart * re-used and return 1 for yes it is ok and 0 for don't use that 5740f8829a4aSRandall Stewart * tag. A secondary function it will do is purge out old tags that 5741f8829a4aSRandall Stewart * can be removed. 5742f8829a4aSRandall Stewart */ 5743f8829a4aSRandall Stewart struct sctpasochead *head; 5744f8829a4aSRandall Stewart struct sctpvtaghead *chain; 5745f8829a4aSRandall Stewart struct sctp_tagblock *twait_block; 5746f8829a4aSRandall Stewart struct sctp_tcb *stcb; 5747f8829a4aSRandall Stewart int i; 5748f8829a4aSRandall Stewart 5749f8829a4aSRandall Stewart SCTP_INP_INFO_WLOCK(); 5750f8829a4aSRandall Stewart chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; 5751f8829a4aSRandall Stewart /* First is the vtag in use ? */ 5752f8829a4aSRandall Stewart 5753f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag, 5754f8829a4aSRandall Stewart sctppcbinfo.hashasocmark)]; 5755f8829a4aSRandall Stewart if (head == NULL) { 5756f8829a4aSRandall Stewart goto check_restart; 5757f8829a4aSRandall Stewart } 5758f8829a4aSRandall Stewart LIST_FOREACH(stcb, head, sctp_asocs) { 5759f8829a4aSRandall Stewart 5760f8829a4aSRandall Stewart if (stcb->asoc.my_vtag == tag) { 5761f8829a4aSRandall Stewart /* 5762f8829a4aSRandall Stewart * We should remove this if and return 0 always if 5763f8829a4aSRandall Stewart * we want vtags unique across all endpoints. For 5764f8829a4aSRandall Stewart * now within a endpoint is ok. 5765f8829a4aSRandall Stewart */ 5766f8829a4aSRandall Stewart if (inp == stcb->sctp_ep) { 5767f8829a4aSRandall Stewart /* bad tag, in use */ 5768f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 5769f8829a4aSRandall Stewart return (0); 5770f8829a4aSRandall Stewart } 5771f8829a4aSRandall Stewart } 5772f8829a4aSRandall Stewart } 5773f8829a4aSRandall Stewart check_restart: 5774f8829a4aSRandall Stewart /* Now lets check the restart hash */ 5775f8829a4aSRandall Stewart head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag, 5776f8829a4aSRandall Stewart sctppcbinfo.hashrestartmark)]; 5777f8829a4aSRandall Stewart if (head == NULL) { 5778f8829a4aSRandall Stewart goto check_time_wait; 5779f8829a4aSRandall Stewart } 5780f8829a4aSRandall Stewart LIST_FOREACH(stcb, head, sctp_tcbrestarhash) { 5781f8829a4aSRandall Stewart if (stcb->asoc.assoc_id == tag) { 5782f8829a4aSRandall Stewart /* candidate */ 5783f8829a4aSRandall Stewart if (inp == stcb->sctp_ep) { 5784f8829a4aSRandall Stewart /* bad tag, in use */ 5785f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 5786f8829a4aSRandall Stewart return (0); 5787f8829a4aSRandall Stewart } 5788f8829a4aSRandall Stewart } 5789f8829a4aSRandall Stewart } 5790f8829a4aSRandall Stewart check_time_wait: 5791f8829a4aSRandall Stewart /* Now what about timed wait ? */ 5792f42a358aSRandall Stewart if (!SCTP_LIST_EMPTY(chain)) { 5793f8829a4aSRandall Stewart /* 5794f8829a4aSRandall Stewart * Block(s) are present, lets see if we have this tag in the 5795f8829a4aSRandall Stewart * list 5796f8829a4aSRandall Stewart */ 5797f8829a4aSRandall Stewart LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { 5798f8829a4aSRandall Stewart for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { 5799f8829a4aSRandall Stewart if (twait_block->vtag_block[i].v_tag == 0) { 5800f8829a4aSRandall Stewart /* not used */ 5801f8829a4aSRandall Stewart continue; 5802f8829a4aSRandall Stewart } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire > 5803f8829a4aSRandall Stewart now->tv_sec) { 5804f8829a4aSRandall Stewart /* Audit expires this guy */ 5805f8829a4aSRandall Stewart twait_block->vtag_block[i].tv_sec_at_expire = 0; 5806f8829a4aSRandall Stewart twait_block->vtag_block[i].v_tag = 0; 5807f8829a4aSRandall Stewart } else if (twait_block->vtag_block[i].v_tag == 5808f8829a4aSRandall Stewart tag) { 5809f8829a4aSRandall Stewart /* Bad tag, sorry :< */ 5810f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 5811f8829a4aSRandall Stewart return (0); 5812f8829a4aSRandall Stewart } 5813f8829a4aSRandall Stewart } 5814f8829a4aSRandall Stewart } 5815f8829a4aSRandall Stewart } 5816851b7298SRandall Stewart /*- 5817851b7298SRandall Stewart * Not found, ok to use the tag, add it to the time wait hash 5818851b7298SRandall Stewart * as well this will prevent two sucessive cookies from getting 5819851b7298SRandall Stewart * the same tag or two inits sent quickly on multi-processors. 5820851b7298SRandall Stewart * We only keep the tag for the life of a cookie and when we 5821851b7298SRandall Stewart * add this tag to the assoc hash we need to purge it from 5822851b7298SRandall Stewart * the t-wait hash. 5823851b7298SRandall Stewart */ 5824851b7298SRandall Stewart sctp_add_vtag_to_timewait(tag, TICKS_TO_SEC(inp->sctp_ep.def_cookie_life)); 5825f8829a4aSRandall Stewart SCTP_INP_INFO_WUNLOCK(); 5826f8829a4aSRandall Stewart return (1); 5827f8829a4aSRandall Stewart } 5828f8829a4aSRandall Stewart 5829f8829a4aSRandall Stewart 5830f8829a4aSRandall Stewart static sctp_assoc_t reneged_asoc_ids[256]; 5831f8829a4aSRandall Stewart static uint8_t reneged_at = 0; 5832f8829a4aSRandall Stewart 5833f8829a4aSRandall Stewart 5834f8829a4aSRandall Stewart static void 5835f8829a4aSRandall Stewart sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 5836f8829a4aSRandall Stewart { 5837f8829a4aSRandall Stewart /* 5838f8829a4aSRandall Stewart * We must hunt this association for MBUF's past the cumack (i.e. 5839f8829a4aSRandall Stewart * out of order data that we can renege on). 5840f8829a4aSRandall Stewart */ 5841f8829a4aSRandall Stewart struct sctp_association *asoc; 5842f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk, *nchk; 5843f8829a4aSRandall Stewart uint32_t cumulative_tsn_p1, tsn; 5844f8829a4aSRandall Stewart struct sctp_queued_to_read *ctl, *nctl; 5845f8829a4aSRandall Stewart int cnt, strmat, gap; 5846f8829a4aSRandall Stewart 5847f8829a4aSRandall Stewart /* We look for anything larger than the cum-ack + 1 */ 5848f8829a4aSRandall Stewart 5849a5d547adSRandall Stewart SCTP_STAT_INCR(sctps_protocol_drain_calls); 5850f8829a4aSRandall Stewart if (sctp_do_drain == 0) { 5851f8829a4aSRandall Stewart return; 5852f8829a4aSRandall Stewart } 5853f8829a4aSRandall Stewart asoc = &stcb->asoc; 5854f8829a4aSRandall Stewart if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { 5855f8829a4aSRandall Stewart /* none we can reneg on. */ 5856f8829a4aSRandall Stewart return; 5857f8829a4aSRandall Stewart } 5858a5d547adSRandall Stewart SCTP_STAT_INCR(sctps_protocol_drains_done); 5859f8829a4aSRandall Stewart cumulative_tsn_p1 = asoc->cumulative_tsn + 1; 5860f8829a4aSRandall Stewart cnt = 0; 5861f8829a4aSRandall Stewart /* First look in the re-assembly queue */ 5862f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->reasmqueue); 5863f8829a4aSRandall Stewart while (chk) { 5864f8829a4aSRandall Stewart /* Get the next one */ 5865f8829a4aSRandall Stewart nchk = TAILQ_NEXT(chk, sctp_next); 5866f8829a4aSRandall Stewart if (compare_with_wrap(chk->rec.data.TSN_seq, 5867f8829a4aSRandall Stewart cumulative_tsn_p1, MAX_TSN)) { 5868f8829a4aSRandall Stewart /* Yep it is above cum-ack */ 5869f8829a4aSRandall Stewart cnt++; 5870f8829a4aSRandall Stewart tsn = chk->rec.data.TSN_seq; 5871f8829a4aSRandall Stewart if (tsn >= asoc->mapping_array_base_tsn) { 5872f8829a4aSRandall Stewart gap = tsn - asoc->mapping_array_base_tsn; 5873f8829a4aSRandall Stewart } else { 5874f8829a4aSRandall Stewart gap = (MAX_TSN - asoc->mapping_array_base_tsn) + 5875f8829a4aSRandall Stewart tsn + 1; 5876f8829a4aSRandall Stewart } 5877f8829a4aSRandall Stewart asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size); 5878f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_reasm_queue); 5879f8829a4aSRandall Stewart SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 5880f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 5881f8829a4aSRandall Stewart if (chk->data) { 5882f8829a4aSRandall Stewart sctp_m_freem(chk->data); 5883f8829a4aSRandall Stewart chk->data = NULL; 5884f8829a4aSRandall Stewart } 5885f8829a4aSRandall Stewart sctp_free_a_chunk(stcb, chk); 5886f8829a4aSRandall Stewart } 5887f8829a4aSRandall Stewart chk = nchk; 5888f8829a4aSRandall Stewart } 5889f8829a4aSRandall Stewart /* Ok that was fun, now we will drain all the inbound streams? */ 5890f8829a4aSRandall Stewart for (strmat = 0; strmat < asoc->streamincnt; strmat++) { 5891f8829a4aSRandall Stewart ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue); 5892f8829a4aSRandall Stewart while (ctl) { 5893f8829a4aSRandall Stewart nctl = TAILQ_NEXT(ctl, next); 5894f8829a4aSRandall Stewart if (compare_with_wrap(ctl->sinfo_tsn, 5895f8829a4aSRandall Stewart cumulative_tsn_p1, MAX_TSN)) { 5896f8829a4aSRandall Stewart /* Yep it is above cum-ack */ 5897f8829a4aSRandall Stewart cnt++; 5898f8829a4aSRandall Stewart tsn = ctl->sinfo_tsn; 5899f8829a4aSRandall Stewart if (tsn >= asoc->mapping_array_base_tsn) { 5900f8829a4aSRandall Stewart gap = tsn - 5901f8829a4aSRandall Stewart asoc->mapping_array_base_tsn; 5902f8829a4aSRandall Stewart } else { 5903f8829a4aSRandall Stewart gap = (MAX_TSN - 5904f8829a4aSRandall Stewart asoc->mapping_array_base_tsn) + 5905f8829a4aSRandall Stewart tsn + 1; 5906f8829a4aSRandall Stewart } 5907f8829a4aSRandall Stewart asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length); 5908f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_all_streams); 5909f8829a4aSRandall Stewart 5910f8829a4aSRandall Stewart SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, 5911f8829a4aSRandall Stewart gap); 5912f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, 5913f8829a4aSRandall Stewart ctl, next); 5914f8829a4aSRandall Stewart if (ctl->data) { 5915f8829a4aSRandall Stewart sctp_m_freem(ctl->data); 5916f8829a4aSRandall Stewart ctl->data = NULL; 5917f8829a4aSRandall Stewart } 5918f8829a4aSRandall Stewart sctp_free_remote_addr(ctl->whoFrom); 5919f8829a4aSRandall Stewart SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl); 5920f8829a4aSRandall Stewart SCTP_DECR_READQ_COUNT(); 5921f8829a4aSRandall Stewart } 5922f8829a4aSRandall Stewart ctl = nctl; 5923f8829a4aSRandall Stewart } 5924f8829a4aSRandall Stewart } 5925f8829a4aSRandall Stewart /* 5926f8829a4aSRandall Stewart * Question, should we go through the delivery queue? The only 5927f8829a4aSRandall Stewart * reason things are on here is the app not reading OR a p-d-api up. 5928f8829a4aSRandall Stewart * An attacker COULD send enough in to initiate the PD-API and then 5929f8829a4aSRandall Stewart * send a bunch of stuff to other streams... these would wind up on 5930f8829a4aSRandall Stewart * the delivery queue.. and then we would not get to them. But in 5931f8829a4aSRandall Stewart * order to do this I then have to back-track and un-deliver 5932f8829a4aSRandall Stewart * sequence numbers in streams.. el-yucko. I think for now we will 5933f8829a4aSRandall Stewart * NOT look at the delivery queue and leave it to be something to 5934f8829a4aSRandall Stewart * consider later. An alternative would be to abort the P-D-API with 5935f8829a4aSRandall Stewart * a notification and then deliver the data.... Or another method 5936f8829a4aSRandall Stewart * might be to keep track of how many times the situation occurs and 5937f8829a4aSRandall Stewart * if we see a possible attack underway just abort the association. 5938f8829a4aSRandall Stewart */ 5939f8829a4aSRandall Stewart #ifdef SCTP_DEBUG 5940f8829a4aSRandall Stewart if (cnt) { 5941ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); 5942f8829a4aSRandall Stewart } 5943ad81507eSRandall Stewart #endif 5944f8829a4aSRandall Stewart if (cnt) { 5945f8829a4aSRandall Stewart /* 5946f8829a4aSRandall Stewart * Now do we need to find a new 5947f8829a4aSRandall Stewart * asoc->highest_tsn_inside_map? 5948f8829a4aSRandall Stewart */ 5949f8829a4aSRandall Stewart if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) { 5950f8829a4aSRandall Stewart gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn; 5951f8829a4aSRandall Stewart } else { 5952f8829a4aSRandall Stewart gap = (MAX_TSN - asoc->mapping_array_base_tsn) + 5953f8829a4aSRandall Stewart asoc->highest_tsn_inside_map + 1; 5954f8829a4aSRandall Stewart } 5955f8829a4aSRandall Stewart if (gap >= (asoc->mapping_array_size << 3)) { 5956f8829a4aSRandall Stewart /* 5957f8829a4aSRandall Stewart * Something bad happened or cum-ack and high were 5958f8829a4aSRandall Stewart * behind the base, but if so earlier checks should 5959f8829a4aSRandall Stewart * have found NO data... wierd... we will start at 5960f8829a4aSRandall Stewart * end of mapping array. 5961f8829a4aSRandall Stewart */ 5962ad81507eSRandall Stewart SCTP_PRINTF("Gap was larger than array?? %d set to max:%d maparraymax:%x\n", 5963f8829a4aSRandall Stewart (int)gap, 5964f8829a4aSRandall Stewart (int)(asoc->mapping_array_size << 3), 5965f8829a4aSRandall Stewart (int)asoc->highest_tsn_inside_map); 5966f8829a4aSRandall Stewart gap = asoc->mapping_array_size << 3; 5967f8829a4aSRandall Stewart } 5968f8829a4aSRandall Stewart while (gap > 0) { 5969f8829a4aSRandall Stewart if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { 5970f8829a4aSRandall Stewart /* found the new highest */ 5971f8829a4aSRandall Stewart asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap; 5972f8829a4aSRandall Stewart break; 5973f8829a4aSRandall Stewart } 5974f8829a4aSRandall Stewart gap--; 5975f8829a4aSRandall Stewart } 5976f8829a4aSRandall Stewart if (gap == 0) { 5977f8829a4aSRandall Stewart /* Nothing left in map */ 5978f8829a4aSRandall Stewart memset(asoc->mapping_array, 0, asoc->mapping_array_size); 5979f8829a4aSRandall Stewart asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1; 5980f8829a4aSRandall Stewart asoc->highest_tsn_inside_map = asoc->cumulative_tsn; 5981f8829a4aSRandall Stewart } 5982f8829a4aSRandall Stewart asoc->last_revoke_count = cnt; 59836e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); 598404ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 5985f8829a4aSRandall Stewart sctp_send_sack(stcb); 5986ceaad40aSRandall Stewart sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED); 5987f8829a4aSRandall Stewart reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb); 5988f8829a4aSRandall Stewart reneged_at++; 5989f8829a4aSRandall Stewart } 5990f8829a4aSRandall Stewart /* 5991f8829a4aSRandall Stewart * Another issue, in un-setting the TSN's in the mapping array we 5992f8829a4aSRandall Stewart * DID NOT adjust the higest_tsn marker. This will cause one of two 5993f8829a4aSRandall Stewart * things to occur. It may cause us to do extra work in checking for 5994f8829a4aSRandall Stewart * our mapping array movement. More importantly it may cause us to 5995f8829a4aSRandall Stewart * SACK every datagram. This may not be a bad thing though since we 5996f8829a4aSRandall Stewart * will recover once we get our cum-ack above and all this stuff we 5997f8829a4aSRandall Stewart * dumped recovered. 5998f8829a4aSRandall Stewart */ 5999f8829a4aSRandall Stewart } 6000f8829a4aSRandall Stewart 6001f8829a4aSRandall Stewart void 6002f8829a4aSRandall Stewart sctp_drain() 6003f8829a4aSRandall Stewart { 6004f8829a4aSRandall Stewart /* 6005f8829a4aSRandall Stewart * We must walk the PCB lists for ALL associations here. The system 6006f8829a4aSRandall Stewart * is LOW on MBUF's and needs help. This is where reneging will 6007f8829a4aSRandall Stewart * occur. We really hope this does NOT happen! 6008f8829a4aSRandall Stewart */ 6009f8829a4aSRandall Stewart struct sctp_inpcb *inp; 6010f8829a4aSRandall Stewart struct sctp_tcb *stcb; 6011f8829a4aSRandall Stewart 6012f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 6013f8829a4aSRandall Stewart LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) { 6014f8829a4aSRandall Stewart /* For each endpoint */ 6015f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp); 6016f8829a4aSRandall Stewart LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 6017f8829a4aSRandall Stewart /* For each association */ 6018f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 6019f8829a4aSRandall Stewart sctp_drain_mbufs(inp, stcb); 6020f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 6021f8829a4aSRandall Stewart } 6022f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp); 6023f8829a4aSRandall Stewart } 6024f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 6025f8829a4aSRandall Stewart } 6026f8829a4aSRandall Stewart 6027f8829a4aSRandall Stewart /* 6028f8829a4aSRandall Stewart * start a new iterator 6029f8829a4aSRandall Stewart * iterates through all endpoints and associations based on the pcb_state 6030f8829a4aSRandall Stewart * flags and asoc_state. "af" (mandatory) is executed for all matching 6031f8829a4aSRandall Stewart * assocs and "ef" (optional) is executed when the iterator completes. 6032f8829a4aSRandall Stewart * "inpf" (optional) is executed for each new endpoint as it is being 6033bff64a4dSRandall Stewart * iterated through. inpe (optional) is called when the inp completes 6034bff64a4dSRandall Stewart * its way through all the stcbs. 6035f8829a4aSRandall Stewart */ 6036f8829a4aSRandall Stewart int 603742551e99SRandall Stewart sctp_initiate_iterator(inp_func inpf, 603842551e99SRandall Stewart asoc_func af, 603942551e99SRandall Stewart inp_func inpe, 604042551e99SRandall Stewart uint32_t pcb_state, 604142551e99SRandall Stewart uint32_t pcb_features, 604242551e99SRandall Stewart uint32_t asoc_state, 604342551e99SRandall Stewart void *argp, 604442551e99SRandall Stewart uint32_t argi, 604542551e99SRandall Stewart end_func ef, 604642551e99SRandall Stewart struct sctp_inpcb *s_inp, 604742551e99SRandall Stewart uint8_t chunk_output_off) 6048f8829a4aSRandall Stewart { 6049f8829a4aSRandall Stewart struct sctp_iterator *it = NULL; 6050f8829a4aSRandall Stewart 6051f8829a4aSRandall Stewart if (af == NULL) { 6052f8829a4aSRandall Stewart return (-1); 6053f8829a4aSRandall Stewart } 6054f8829a4aSRandall Stewart SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), 6055207304d4SRandall Stewart SCTP_M_ITER); 6056f8829a4aSRandall Stewart if (it == NULL) { 6057c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); 6058f8829a4aSRandall Stewart return (ENOMEM); 6059f8829a4aSRandall Stewart } 6060f8829a4aSRandall Stewart memset(it, 0, sizeof(*it)); 6061f8829a4aSRandall Stewart it->function_assoc = af; 6062f8829a4aSRandall Stewart it->function_inp = inpf; 606342551e99SRandall Stewart if (inpf) 606442551e99SRandall Stewart it->done_current_ep = 0; 606542551e99SRandall Stewart else 606642551e99SRandall Stewart it->done_current_ep = 1; 6067f8829a4aSRandall Stewart it->function_atend = ef; 6068f8829a4aSRandall Stewart it->pointer = argp; 6069f8829a4aSRandall Stewart it->val = argi; 6070f8829a4aSRandall Stewart it->pcb_flags = pcb_state; 6071f8829a4aSRandall Stewart it->pcb_features = pcb_features; 6072f8829a4aSRandall Stewart it->asoc_state = asoc_state; 607342551e99SRandall Stewart it->function_inp_end = inpe; 6074f8829a4aSRandall Stewart it->no_chunk_output = chunk_output_off; 6075f8829a4aSRandall Stewart if (s_inp) { 6076f8829a4aSRandall Stewart it->inp = s_inp; 6077f8829a4aSRandall Stewart it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; 6078f8829a4aSRandall Stewart } else { 6079f8829a4aSRandall Stewart SCTP_INP_INFO_RLOCK(); 6080f8829a4aSRandall Stewart it->inp = LIST_FIRST(&sctppcbinfo.listhead); 608142551e99SRandall Stewart 6082f8829a4aSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 6083f8829a4aSRandall Stewart it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; 6084f8829a4aSRandall Stewart 6085f8829a4aSRandall Stewart } 608642551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_LOCK(); 6087ad81507eSRandall Stewart if (it->inp) { 608842551e99SRandall Stewart SCTP_INP_INCR_REF(it->inp); 6089ad81507eSRandall Stewart } 609042551e99SRandall Stewart TAILQ_INSERT_TAIL(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr); 609142551e99SRandall Stewart #if defined(SCTP_USE_THREAD_BASED_ITERATOR) 609242551e99SRandall Stewart if (sctppcbinfo.iterator_running == 0) { 609342551e99SRandall Stewart sctp_wakeup_iterator(); 609442551e99SRandall Stewart } 609542551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_UNLOCK(); 609642551e99SRandall Stewart #else 609742551e99SRandall Stewart if (it->inp) 609842551e99SRandall Stewart SCTP_INP_DECR_REF(it->inp); 609942551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_UNLOCK(); 6100f8829a4aSRandall Stewart /* Init the timer */ 6101139bc87fSRandall Stewart SCTP_OS_TIMER_INIT(&it->tmr.timer); 6102f8829a4aSRandall Stewart /* add to the list of all iterators */ 6103f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it, 6104f8829a4aSRandall Stewart NULL, NULL); 610542551e99SRandall Stewart #endif 61063c503c28SRandall Stewart /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ 6107f8829a4aSRandall Stewart return (0); 6108f8829a4aSRandall Stewart } 6109