17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2245916cd2Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/systm.h> 307c478bd9Sstevel@tonic-gate #include <sys/stream.h> 317c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 337c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 347c478bd9Sstevel@tonic-gate #include <sys/socket.h> 357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 367c478bd9Sstevel@tonic-gate #include <sys/list.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <netinet/in.h> 397c478bd9Sstevel@tonic-gate #include <netinet/ip6.h> 407c478bd9Sstevel@tonic-gate #include <netinet/sctp.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <inet/common.h> 437c478bd9Sstevel@tonic-gate #include <inet/ip.h> 447c478bd9Sstevel@tonic-gate #include <inet/ip6.h> 457c478bd9Sstevel@tonic-gate #include <inet/ip_if.h> 467c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h> 477c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h> 487c478bd9Sstevel@tonic-gate #include "sctp_impl.h" 497c478bd9Sstevel@tonic-gate #include "sctp_addr.h" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static void sctp_ipif_inactive(sctp_ipif_t *); 527c478bd9Sstevel@tonic-gate static sctp_ipif_t *sctp_lookup_ipif_addr(in6_addr_t *, boolean_t, 53*5d0bc3edSsommerfe sctp_t *, uint_t); 547c478bd9Sstevel@tonic-gate static int sctp_get_all_ipifs(sctp_t *, int); 551d8c4025Svi int sctp_valid_addr_list(sctp_t *, const void *, uint32_t, 561d8c4025Svi uchar_t *, size_t); 577c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *sctp_ipif_lookup(sctp_t *, uint_t); 58f551bb10Svi static int sctp_ipif_hash_insert(sctp_t *, sctp_ipif_t *, int, 59f551bb10Svi boolean_t dontsrc); 607c478bd9Sstevel@tonic-gate static void sctp_ipif_hash_remove(sctp_t *, sctp_ipif_t *); 617c478bd9Sstevel@tonic-gate static int sctp_compare_ipif_list(sctp_ipif_hash_t *, 627c478bd9Sstevel@tonic-gate sctp_ipif_hash_t *); 637c478bd9Sstevel@tonic-gate int sctp_compare_saddrs(sctp_t *, sctp_t *); 647c478bd9Sstevel@tonic-gate static int sctp_copy_ipifs(sctp_ipif_hash_t *, sctp_t *, int); 657c478bd9Sstevel@tonic-gate int sctp_dup_saddrs(sctp_t *, sctp_t *, int); 667c478bd9Sstevel@tonic-gate void sctp_free_saddrs(sctp_t *); 677c478bd9Sstevel@tonic-gate void sctp_update_ill(ill_t *, int); 687c478bd9Sstevel@tonic-gate void sctp_update_ipif(ipif_t *, int); 697c478bd9Sstevel@tonic-gate void sctp_move_ipif(ipif_t *, ill_t *, ill_t *); 707c478bd9Sstevel@tonic-gate void sctp_del_saddr(sctp_t *, sctp_saddr_ipif_t *); 717c478bd9Sstevel@tonic-gate void sctp_del_saddr_list(sctp_t *, const void *, int, 727c478bd9Sstevel@tonic-gate boolean_t); 731d8c4025Svi sctp_saddr_ipif_t *sctp_saddr_lookup(sctp_t *, in6_addr_t *, uint_t); 747c478bd9Sstevel@tonic-gate in6_addr_t sctp_get_valid_addr(sctp_t *, boolean_t); 757c478bd9Sstevel@tonic-gate int sctp_getmyaddrs(void *, void *, int *); 767c478bd9Sstevel@tonic-gate void sctp_saddr_init(); 777c478bd9Sstevel@tonic-gate void sctp_saddr_fini(); 78f551bb10Svi 797c478bd9Sstevel@tonic-gate #define SCTP_IPIF_USABLE(sctp_ipif_state) \ 807c478bd9Sstevel@tonic-gate ((sctp_ipif_state) == SCTP_IPIFS_UP || \ 81f551bb10Svi (sctp_ipif_state) == SCTP_IPIFS_DOWN) 82f551bb10Svi 83f551bb10Svi #define SCTP_IPIF_DISCARD(sctp_ipif_flags) \ 84f551bb10Svi ((sctp_ipif_flags) & (IPIF_PRIVATE | IPIF_DEPRECATED)) 85f551bb10Svi 861d8c4025Svi #define SCTP_IS_IPIF_LOOPBACK(ipif) \ 871d8c4025Svi ((ipif)->sctp_ipif_ill->sctp_ill_flags & PHYI_LOOPBACK) 881d8c4025Svi 891d8c4025Svi #define SCTP_IS_IPIF_LINKLOCAL(ipif) \ 901d8c4025Svi ((ipif)->sctp_ipif_isv6 && \ 911d8c4025Svi IN6_IS_ADDR_LINKLOCAL(&(ipif)->sctp_ipif_saddr)) 92f551bb10Svi 93f551bb10Svi #define SCTP_UNSUPP_AF(ipif, supp_af) \ 94f551bb10Svi ((!(ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V4)) || \ 95f551bb10Svi ((ipif)->sctp_ipif_isv6 && !((supp_af) & PARM_SUPP_V6))) 967c478bd9Sstevel@tonic-gate 97*5d0bc3edSsommerfe #define SCTP_IPIF_ZONE_MATCH(sctp, ipif) \ 98*5d0bc3edSsommerfe IPCL_ZONE_MATCH((sctp)->sctp_connp, (ipif)->sctp_ipif_zoneid) 99*5d0bc3edSsommerfe 1007c478bd9Sstevel@tonic-gate #define SCTP_ILL_HASH_FN(index) ((index) % SCTP_ILL_HASH) 1017c478bd9Sstevel@tonic-gate #define SCTP_IPIF_HASH_FN(seqid) ((seqid) % SCTP_IPIF_HASH) 1027c478bd9Sstevel@tonic-gate #define SCTP_ILL_TO_PHYINDEX(ill) ((ill)->ill_phyint->phyint_ifindex) 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* Global list of SCTP ILLs */ 1057c478bd9Sstevel@tonic-gate sctp_ill_hash_t sctp_g_ills[SCTP_ILL_HASH]; 1067c478bd9Sstevel@tonic-gate uint32_t sctp_ills_count = 0; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate /* Global list of SCTP IPIFs */ 1097c478bd9Sstevel@tonic-gate sctp_ipif_hash_t sctp_g_ipifs[SCTP_IPIF_HASH]; 1101d8c4025Svi uint32_t sctp_g_ipifs_count = 0; 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * SCTP Interface list manipulation functions, locking used. 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * 1177c478bd9Sstevel@tonic-gate */ 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Delete an SCTP IPIF from the list if the refcount goes to 0 and it is 1217c478bd9Sstevel@tonic-gate * marked as condemned. Also, check if the ILL needs to go away. 1227c478bd9Sstevel@tonic-gate * Called with no locks held. 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate static void 1257c478bd9Sstevel@tonic-gate sctp_ipif_inactive(sctp_ipif_t *sctp_ipif) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate sctp_ill_t *sctp_ill; 1287c478bd9Sstevel@tonic-gate uint_t ipif_index; 1297c478bd9Sstevel@tonic-gate uint_t ill_index; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ills_lock, RW_READER); 1327c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ipifs_lock, RW_WRITER); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate ipif_index = SCTP_IPIF_HASH_FN(sctp_ipif->sctp_ipif_id); 1357c478bd9Sstevel@tonic-gate sctp_ill = sctp_ipif->sctp_ipif_ill; 1367c478bd9Sstevel@tonic-gate ASSERT(sctp_ill != NULL); 1377c478bd9Sstevel@tonic-gate ill_index = SCTP_ILL_HASH_FN(sctp_ill->sctp_ill_index); 1387c478bd9Sstevel@tonic-gate if (sctp_ipif->sctp_ipif_state != SCTP_IPIFS_CONDEMNED || 1397c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_refcnt != 0) { 1407c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 1417c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 1427c478bd9Sstevel@tonic-gate return; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate list_remove(&sctp_g_ipifs[ipif_index].sctp_ipif_list, sctp_ipif); 1457c478bd9Sstevel@tonic-gate sctp_g_ipifs[ipif_index].ipif_count--; 1467c478bd9Sstevel@tonic-gate sctp_g_ipifs_count--; 1477c478bd9Sstevel@tonic-gate rw_destroy(&sctp_ipif->sctp_ipif_lock); 1487c478bd9Sstevel@tonic-gate kmem_free(sctp_ipif, sizeof (sctp_ipif_t)); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate (void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1); 1517c478bd9Sstevel@tonic-gate if (rw_tryupgrade(&sctp_g_ills_lock) != 0) { 1527c478bd9Sstevel@tonic-gate rw_downgrade(&sctp_g_ipifs_lock); 1537c478bd9Sstevel@tonic-gate if (sctp_ill->sctp_ill_ipifcnt == 0 && 1547c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) { 1557c478bd9Sstevel@tonic-gate list_remove(&sctp_g_ills[ill_index].sctp_ill_list, 1567c478bd9Sstevel@tonic-gate (void *)sctp_ill); 1577c478bd9Sstevel@tonic-gate sctp_g_ills[ill_index].ill_count--; 1587c478bd9Sstevel@tonic-gate sctp_ills_count--; 1597c478bd9Sstevel@tonic-gate kmem_free(sctp_ill->sctp_ill_name, 1607c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_name_length); 1617c478bd9Sstevel@tonic-gate kmem_free(sctp_ill, sizeof (sctp_ill_t)); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 1657c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* 1697c478bd9Sstevel@tonic-gate * Lookup an SCTP IPIF given an IP address. Increments sctp_ipif refcnt. 1707c478bd9Sstevel@tonic-gate * Called with no locks held. 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate static sctp_ipif_t * 173*5d0bc3edSsommerfe sctp_lookup_ipif_addr(in6_addr_t *addr, boolean_t refhold, sctp_t *sctp, 1741d8c4025Svi uint_t ifindex) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate int i; 1777c478bd9Sstevel@tonic-gate int j; 1787c478bd9Sstevel@tonic-gate sctp_ipif_t *sctp_ipif; 1797c478bd9Sstevel@tonic-gate 180*5d0bc3edSsommerfe ASSERT(sctp->sctp_zoneid != ALL_ZONES); 1817c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ipifs_lock, RW_READER); 1827c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 1837c478bd9Sstevel@tonic-gate if (sctp_g_ipifs[i].ipif_count == 0) 1847c478bd9Sstevel@tonic-gate continue; 1857c478bd9Sstevel@tonic-gate sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list); 1867c478bd9Sstevel@tonic-gate for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) { 1877c478bd9Sstevel@tonic-gate rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER); 188*5d0bc3edSsommerfe if (SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) && 1897c478bd9Sstevel@tonic-gate SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) && 1901d8c4025Svi (ifindex == 0 || ifindex == 1911d8c4025Svi sctp_ipif->sctp_ipif_ill->sctp_ill_index) && 1927c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&sctp_ipif->sctp_ipif_saddr, 1937c478bd9Sstevel@tonic-gate addr)) { 1947c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 1957c478bd9Sstevel@tonic-gate if (refhold) 1967c478bd9Sstevel@tonic-gate SCTP_IPIF_REFHOLD(sctp_ipif); 1977c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 1987c478bd9Sstevel@tonic-gate return (sctp_ipif); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 2017c478bd9Sstevel@tonic-gate sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list, 2027c478bd9Sstevel@tonic-gate sctp_ipif); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 2067c478bd9Sstevel@tonic-gate return (NULL); 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate /* 2107c478bd9Sstevel@tonic-gate * Populate the list with all the SCTP ipifs for a given ipversion. 2117c478bd9Sstevel@tonic-gate * Increments sctp_ipif refcnt. 2127c478bd9Sstevel@tonic-gate * Called with no locks held. 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate static int 2157c478bd9Sstevel@tonic-gate sctp_get_all_ipifs(sctp_t *sctp, int sleep) 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate sctp_ipif_t *sctp_ipif; 2187c478bd9Sstevel@tonic-gate int i; 2197c478bd9Sstevel@tonic-gate int j; 2207c478bd9Sstevel@tonic-gate int error = 0; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ipifs_lock, RW_READER); 2237c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 2247c478bd9Sstevel@tonic-gate if (sctp_g_ipifs[i].ipif_count == 0) 2257c478bd9Sstevel@tonic-gate continue; 2267c478bd9Sstevel@tonic-gate sctp_ipif = list_head(&sctp_g_ipifs[i].sctp_ipif_list); 2277c478bd9Sstevel@tonic-gate for (j = 0; j < sctp_g_ipifs[i].ipif_count; j++) { 2287c478bd9Sstevel@tonic-gate rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER); 229f551bb10Svi if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) || 2307c478bd9Sstevel@tonic-gate !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) || 231*5d0bc3edSsommerfe !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) || 2327c478bd9Sstevel@tonic-gate (sctp->sctp_ipversion == IPV4_VERSION && 233f551bb10Svi sctp_ipif->sctp_ipif_isv6) || 2347c478bd9Sstevel@tonic-gate (sctp->sctp_connp->conn_ipv6_v6only && 235f551bb10Svi !sctp_ipif->sctp_ipif_isv6)) { 2367c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 2377c478bd9Sstevel@tonic-gate sctp_ipif = list_next( 2387c478bd9Sstevel@tonic-gate &sctp_g_ipifs[i].sctp_ipif_list, sctp_ipif); 2397c478bd9Sstevel@tonic-gate continue; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 2427c478bd9Sstevel@tonic-gate SCTP_IPIF_REFHOLD(sctp_ipif); 243f551bb10Svi error = sctp_ipif_hash_insert(sctp, sctp_ipif, sleep, 244f551bb10Svi B_FALSE); 2457c478bd9Sstevel@tonic-gate if (error != 0) 2467c478bd9Sstevel@tonic-gate goto free_stuff; 2477c478bd9Sstevel@tonic-gate sctp_ipif = list_next(&sctp_g_ipifs[i].sctp_ipif_list, 2487c478bd9Sstevel@tonic-gate sctp_ipif); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 2527c478bd9Sstevel@tonic-gate return (0); 2537c478bd9Sstevel@tonic-gate free_stuff: 2547c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 2557c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp); 2567c478bd9Sstevel@tonic-gate return (ENOMEM); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate /* 2607c478bd9Sstevel@tonic-gate * Given a list of address, fills in the list of SCTP ipifs if all the addresses 2617c478bd9Sstevel@tonic-gate * are present in the SCTP interface list, return number of addresses filled 2621d8c4025Svi * or error. If the caller wants the list of addresses, it sends a pre-allocated 2631d8c4025Svi * buffer - list. Currently, this list is only used on a clustered node when 2641d8c4025Svi * the SCTP is in the listen state (from sctp_bind_add()). When called on a 2651d8c4025Svi * clustered node, the input is always a list of addresses (even if the 2661d8c4025Svi * original bind() was to INADDR_ANY). 2677c478bd9Sstevel@tonic-gate * Called with no locks held. 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate int 2701d8c4025Svi sctp_valid_addr_list(sctp_t *sctp, const void *addrs, uint32_t addrcnt, 2711d8c4025Svi uchar_t *list, size_t lsize) 2727c478bd9Sstevel@tonic-gate { 2737c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4; 2747c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 2757c478bd9Sstevel@tonic-gate struct in_addr *addr4; 2767c478bd9Sstevel@tonic-gate in6_addr_t addr; 2777c478bd9Sstevel@tonic-gate int cnt; 2787c478bd9Sstevel@tonic-gate int err = 0; 2797c478bd9Sstevel@tonic-gate int saddr_cnt = 0; 2807c478bd9Sstevel@tonic-gate sctp_ipif_t *ipif; 2817c478bd9Sstevel@tonic-gate boolean_t bind_to_all = B_FALSE; 2827c478bd9Sstevel@tonic-gate boolean_t check_addrs = B_FALSE; 2837c478bd9Sstevel@tonic-gate boolean_t check_lport = B_FALSE; 2841d8c4025Svi uchar_t *p = list; 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Need to check for port and address depending on the state. 2887c478bd9Sstevel@tonic-gate * After a socket is bound, we need to make sure that subsequent 2897c478bd9Sstevel@tonic-gate * bindx() has correct port. After an association is established, 2907c478bd9Sstevel@tonic-gate * we need to check for changing the bound address to invalid 2917c478bd9Sstevel@tonic-gate * addresses. 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate if (sctp->sctp_state >= SCTPS_BOUND) { 2947c478bd9Sstevel@tonic-gate check_lport = B_TRUE; 2957c478bd9Sstevel@tonic-gate if (sctp->sctp_state > SCTPS_LISTEN) 2967c478bd9Sstevel@tonic-gate check_addrs = B_TRUE; 2977c478bd9Sstevel@tonic-gate } 2981d8c4025Svi 2997c478bd9Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL) 3007c478bd9Sstevel@tonic-gate mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 3017c478bd9Sstevel@tonic-gate if (sctp->sctp_listen_tfp != NULL) 3027c478bd9Sstevel@tonic-gate mutex_enter(&sctp->sctp_listen_tfp->tf_lock); 3037c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < addrcnt; cnt++) { 3047c478bd9Sstevel@tonic-gate boolean_t lookup_saddr = B_TRUE; 3051d8c4025Svi uint_t ifindex = 0; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate switch (sctp->sctp_family) { 3087c478bd9Sstevel@tonic-gate case AF_INET: 3097c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)addrs + cnt; 3107c478bd9Sstevel@tonic-gate if (sin4->sin_family != AF_INET || (check_lport && 3117c478bd9Sstevel@tonic-gate sin4->sin_port != sctp->sctp_lport)) { 3127c478bd9Sstevel@tonic-gate err = EINVAL; 3137c478bd9Sstevel@tonic-gate goto free_ret; 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate addr4 = &sin4->sin_addr; 3167c478bd9Sstevel@tonic-gate if (check_addrs && 3177c478bd9Sstevel@tonic-gate (addr4->s_addr == INADDR_ANY || 3187c478bd9Sstevel@tonic-gate addr4->s_addr == INADDR_BROADCAST || 3197c478bd9Sstevel@tonic-gate IN_MULTICAST(addr4->s_addr))) { 3207c478bd9Sstevel@tonic-gate err = EINVAL; 3217c478bd9Sstevel@tonic-gate goto free_ret; 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(addr4, &addr); 3247c478bd9Sstevel@tonic-gate if (!check_addrs && addr4->s_addr == INADDR_ANY) { 3257c478bd9Sstevel@tonic-gate lookup_saddr = B_FALSE; 3267c478bd9Sstevel@tonic-gate bind_to_all = B_TRUE; 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate break; 3307c478bd9Sstevel@tonic-gate case AF_INET6: 3317c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)addrs + cnt; 3327c478bd9Sstevel@tonic-gate if (sin6->sin6_family != AF_INET6 || (check_lport && 3337c478bd9Sstevel@tonic-gate sin6->sin6_port != sctp->sctp_lport)) { 3347c478bd9Sstevel@tonic-gate err = EINVAL; 3357c478bd9Sstevel@tonic-gate goto free_ret; 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate addr = sin6->sin6_addr; 3381d8c4025Svi /* Contains the interface index */ 3391d8c4025Svi ifindex = sin6->sin6_scope_id; 3407c478bd9Sstevel@tonic-gate if (sctp->sctp_connp->conn_ipv6_v6only && 3417c478bd9Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED(&addr)) { 3427c478bd9Sstevel@tonic-gate err = EAFNOSUPPORT; 3437c478bd9Sstevel@tonic-gate goto free_ret; 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate if (check_addrs && 3467c478bd9Sstevel@tonic-gate (IN6_IS_ADDR_LINKLOCAL(&addr) || 3477c478bd9Sstevel@tonic-gate IN6_IS_ADDR_MULTICAST(&addr) || 3487c478bd9Sstevel@tonic-gate IN6_IS_ADDR_UNSPECIFIED(&addr))) { 3497c478bd9Sstevel@tonic-gate err = EINVAL; 3507c478bd9Sstevel@tonic-gate goto free_ret; 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate if (!check_addrs && IN6_IS_ADDR_UNSPECIFIED(&addr)) { 3537c478bd9Sstevel@tonic-gate lookup_saddr = B_FALSE; 3547c478bd9Sstevel@tonic-gate bind_to_all = B_TRUE; 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate break; 3587c478bd9Sstevel@tonic-gate default: 3597c478bd9Sstevel@tonic-gate err = EAFNOSUPPORT; 3607c478bd9Sstevel@tonic-gate goto free_ret; 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate if (lookup_saddr) { 363*5d0bc3edSsommerfe ipif = sctp_lookup_ipif_addr(&addr, B_TRUE, sctp, 364*5d0bc3edSsommerfe ifindex); 3657c478bd9Sstevel@tonic-gate if (ipif == NULL) { 3667c478bd9Sstevel@tonic-gate /* Address not in the list */ 3677c478bd9Sstevel@tonic-gate err = EINVAL; 3687c478bd9Sstevel@tonic-gate goto free_ret; 3691d8c4025Svi } else if (check_addrs && SCTP_IS_IPIF_LOOPBACK(ipif) && 3701d8c4025Svi cl_sctp_check_addrs == NULL) { 3717c478bd9Sstevel@tonic-gate SCTP_IPIF_REFRELE(ipif); 3727c478bd9Sstevel@tonic-gate err = EINVAL; 3737c478bd9Sstevel@tonic-gate goto free_ret; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate if (!bind_to_all) { 377f551bb10Svi /* 378f551bb10Svi * If an address is added after association setup, 379f551bb10Svi * we need to wait for the peer to send us an ASCONF 380f551bb10Svi * ACK before we can start using it. 381f551bb10Svi * saddr_ipif_dontsrc will be reset (to 0) when we 382f551bb10Svi * get the ASCONF ACK for this address. 383f551bb10Svi */ 384f551bb10Svi err = sctp_ipif_hash_insert(sctp, ipif, KM_SLEEP, 385f551bb10Svi check_addrs ? B_TRUE : B_FALSE); 3867c478bd9Sstevel@tonic-gate if (err != 0) { 3877c478bd9Sstevel@tonic-gate SCTP_IPIF_REFRELE(ipif); 3887c478bd9Sstevel@tonic-gate if (check_addrs && err == EALREADY) 3897c478bd9Sstevel@tonic-gate err = EADDRINUSE; 3907c478bd9Sstevel@tonic-gate goto free_ret; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate saddr_cnt++; 3931d8c4025Svi if (lsize >= sizeof (addr)) { 3941d8c4025Svi bcopy(&addr, p, sizeof (addr)); 3951d8c4025Svi p += sizeof (addr); 3961d8c4025Svi lsize -= sizeof (addr); 3971d8c4025Svi } 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate if (bind_to_all) { 4017c478bd9Sstevel@tonic-gate /* 4027c478bd9Sstevel@tonic-gate * Free whatever we might have added before encountering 4037c478bd9Sstevel@tonic-gate * inaddr_any. 4047c478bd9Sstevel@tonic-gate */ 4057c478bd9Sstevel@tonic-gate if (sctp->sctp_nsaddrs > 0) { 4067c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp); 4077c478bd9Sstevel@tonic-gate ASSERT(sctp->sctp_nsaddrs == 0); 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate err = sctp_get_all_ipifs(sctp, KM_SLEEP); 4107c478bd9Sstevel@tonic-gate if (err != 0) 4117c478bd9Sstevel@tonic-gate return (err); 4127c478bd9Sstevel@tonic-gate sctp->sctp_bound_to_all = 1; 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate if (sctp->sctp_listen_tfp != NULL) 4157c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_listen_tfp->tf_lock); 4167c478bd9Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL) 4177c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 4187c478bd9Sstevel@tonic-gate return (0); 4197c478bd9Sstevel@tonic-gate free_ret: 4207c478bd9Sstevel@tonic-gate if (saddr_cnt != 0) 4217c478bd9Sstevel@tonic-gate sctp_del_saddr_list(sctp, addrs, saddr_cnt, B_TRUE); 4227c478bd9Sstevel@tonic-gate if (sctp->sctp_listen_tfp != NULL) 4237c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_listen_tfp->tf_lock); 4247c478bd9Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL) 4257c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 4267c478bd9Sstevel@tonic-gate return (err); 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t * 4307c478bd9Sstevel@tonic-gate sctp_ipif_lookup(sctp_t *sctp, uint_t ipif_index) 4317c478bd9Sstevel@tonic-gate { 4327c478bd9Sstevel@tonic-gate int cnt; 4337c478bd9Sstevel@tonic-gate int seqid = SCTP_IPIF_HASH_FN(ipif_index); 4347c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *ipif_obj; 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate if (sctp->sctp_saddrs[seqid].ipif_count == 0) 4377c478bd9Sstevel@tonic-gate return (NULL); 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list); 4407c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) { 4417c478bd9Sstevel@tonic-gate if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif_index) 4427c478bd9Sstevel@tonic-gate return (ipif_obj); 4437c478bd9Sstevel@tonic-gate ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list, 4447c478bd9Sstevel@tonic-gate ipif_obj); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate return (NULL); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate static int 450f551bb10Svi sctp_ipif_hash_insert(sctp_t *sctp, sctp_ipif_t *ipif, int sleep, 451f551bb10Svi boolean_t dontsrc) 4527c478bd9Sstevel@tonic-gate { 4537c478bd9Sstevel@tonic-gate int cnt; 4547c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *ipif_obj; 4557c478bd9Sstevel@tonic-gate int seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id); 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list); 4587c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) { 4597c478bd9Sstevel@tonic-gate if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) 4607c478bd9Sstevel@tonic-gate return (EALREADY); 4617c478bd9Sstevel@tonic-gate ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list, 4627c478bd9Sstevel@tonic-gate ipif_obj); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate ipif_obj = kmem_zalloc(sizeof (sctp_saddr_ipif_t), sleep); 4657c478bd9Sstevel@tonic-gate if (ipif_obj == NULL) { 4667c478bd9Sstevel@tonic-gate /* Need to do something */ 4677c478bd9Sstevel@tonic-gate return (ENOMEM); 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate ipif_obj->saddr_ipifp = ipif; 470f551bb10Svi ipif_obj->saddr_ipif_dontsrc = dontsrc ? 1 : 0; 4717c478bd9Sstevel@tonic-gate list_insert_tail(&sctp->sctp_saddrs[seqid].sctp_ipif_list, ipif_obj); 4727c478bd9Sstevel@tonic-gate sctp->sctp_saddrs[seqid].ipif_count++; 4737c478bd9Sstevel@tonic-gate sctp->sctp_nsaddrs++; 4747c478bd9Sstevel@tonic-gate return (0); 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate static void 4787c478bd9Sstevel@tonic-gate sctp_ipif_hash_remove(sctp_t *sctp, sctp_ipif_t *ipif) 4797c478bd9Sstevel@tonic-gate { 4807c478bd9Sstevel@tonic-gate int cnt; 4817c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *ipif_obj; 4827c478bd9Sstevel@tonic-gate int seqid = SCTP_IPIF_HASH_FN(ipif->sctp_ipif_id); 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate ipif_obj = list_head(&sctp->sctp_saddrs[seqid].sctp_ipif_list); 4857c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < sctp->sctp_saddrs[seqid].ipif_count; cnt++) { 4867c478bd9Sstevel@tonic-gate if (ipif_obj->saddr_ipifp->sctp_ipif_id == ipif->sctp_ipif_id) { 4877c478bd9Sstevel@tonic-gate list_remove(&sctp->sctp_saddrs[seqid].sctp_ipif_list, 4887c478bd9Sstevel@tonic-gate ipif_obj); 4897c478bd9Sstevel@tonic-gate sctp->sctp_nsaddrs--; 4907c478bd9Sstevel@tonic-gate sctp->sctp_saddrs[seqid].ipif_count--; 4917c478bd9Sstevel@tonic-gate SCTP_IPIF_REFRELE(ipif_obj->saddr_ipifp); 4927c478bd9Sstevel@tonic-gate kmem_free(ipif_obj, sizeof (sctp_saddr_ipif_t)); 4937c478bd9Sstevel@tonic-gate break; 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate ipif_obj = list_next(&sctp->sctp_saddrs[seqid].sctp_ipif_list, 4967c478bd9Sstevel@tonic-gate ipif_obj); 4977c478bd9Sstevel@tonic-gate } 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate static int 5017c478bd9Sstevel@tonic-gate sctp_compare_ipif_list(sctp_ipif_hash_t *list1, sctp_ipif_hash_t *list2) 5027c478bd9Sstevel@tonic-gate { 5037c478bd9Sstevel@tonic-gate int i; 5047c478bd9Sstevel@tonic-gate int j; 5057c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *obj1; 5067c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *obj2; 5077c478bd9Sstevel@tonic-gate int overlap = 0; 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate obj1 = list_head(&list1->sctp_ipif_list); 5107c478bd9Sstevel@tonic-gate for (i = 0; i < list1->ipif_count; i++) { 5117c478bd9Sstevel@tonic-gate obj2 = list_head(&list2->sctp_ipif_list); 5127c478bd9Sstevel@tonic-gate for (j = 0; j < list2->ipif_count; j++) { 5137c478bd9Sstevel@tonic-gate if (obj1->saddr_ipifp->sctp_ipif_id == 5147c478bd9Sstevel@tonic-gate obj2->saddr_ipifp->sctp_ipif_id) { 5157c478bd9Sstevel@tonic-gate overlap++; 5167c478bd9Sstevel@tonic-gate break; 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate obj2 = list_next(&list2->sctp_ipif_list, 5197c478bd9Sstevel@tonic-gate obj2); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate obj1 = list_next(&list1->sctp_ipif_list, obj1); 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate return (overlap); 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate int 5277c478bd9Sstevel@tonic-gate sctp_compare_saddrs(sctp_t *sctp1, sctp_t *sctp2) 5287c478bd9Sstevel@tonic-gate { 5297c478bd9Sstevel@tonic-gate int i; 5307c478bd9Sstevel@tonic-gate int overlap = 0; 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 5337c478bd9Sstevel@tonic-gate overlap += sctp_compare_ipif_list(&sctp1->sctp_saddrs[i], 5347c478bd9Sstevel@tonic-gate &sctp2->sctp_saddrs[i]); 5357c478bd9Sstevel@tonic-gate } 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate if (sctp1->sctp_nsaddrs == sctp2->sctp_nsaddrs && 5387c478bd9Sstevel@tonic-gate overlap == sctp1->sctp_nsaddrs) { 5397c478bd9Sstevel@tonic-gate return (SCTP_ADDR_EQUAL); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate if (overlap == sctp1->sctp_nsaddrs) 5437c478bd9Sstevel@tonic-gate return (SCTP_ADDR_SUBSET); 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate if (overlap > 0) 5467c478bd9Sstevel@tonic-gate return (SCTP_ADDR_OVERLAP); 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate return (SCTP_ADDR_DISJOINT); 5497c478bd9Sstevel@tonic-gate } 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate static int 5527c478bd9Sstevel@tonic-gate sctp_copy_ipifs(sctp_ipif_hash_t *list1, sctp_t *sctp2, int sleep) 5537c478bd9Sstevel@tonic-gate { 5547c478bd9Sstevel@tonic-gate int i; 5557c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *obj; 5567c478bd9Sstevel@tonic-gate int error = 0; 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate obj = list_head(&list1->sctp_ipif_list); 5597c478bd9Sstevel@tonic-gate for (i = 0; i < list1->ipif_count; i++) { 5607c478bd9Sstevel@tonic-gate SCTP_IPIF_REFHOLD(obj->saddr_ipifp); 561f551bb10Svi error = sctp_ipif_hash_insert(sctp2, obj->saddr_ipifp, sleep, 562f551bb10Svi B_FALSE); 5637c478bd9Sstevel@tonic-gate if (error != 0) 5647c478bd9Sstevel@tonic-gate return (error); 5657c478bd9Sstevel@tonic-gate obj = list_next(&list1->sctp_ipif_list, obj); 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate return (error); 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate int 5717c478bd9Sstevel@tonic-gate sctp_dup_saddrs(sctp_t *sctp1, sctp_t *sctp2, int sleep) 5727c478bd9Sstevel@tonic-gate { 5737c478bd9Sstevel@tonic-gate int error = 0; 5747c478bd9Sstevel@tonic-gate int i; 5757c478bd9Sstevel@tonic-gate 576f551bb10Svi if (sctp1 == NULL || sctp1->sctp_bound_to_all == 1) 5777c478bd9Sstevel@tonic-gate return (sctp_get_all_ipifs(sctp2, sleep)); 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 5807c478bd9Sstevel@tonic-gate if (sctp1->sctp_saddrs[i].ipif_count == 0) 5817c478bd9Sstevel@tonic-gate continue; 5827c478bd9Sstevel@tonic-gate error = sctp_copy_ipifs(&sctp1->sctp_saddrs[i], sctp2, sleep); 5837c478bd9Sstevel@tonic-gate if (error != 0) { 5847c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp2); 5857c478bd9Sstevel@tonic-gate return (error); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate return (0); 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate void 5927c478bd9Sstevel@tonic-gate sctp_free_saddrs(sctp_t *sctp) 5937c478bd9Sstevel@tonic-gate { 5947c478bd9Sstevel@tonic-gate int i; 5957c478bd9Sstevel@tonic-gate int l; 5967c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *obj; 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 0) 5997c478bd9Sstevel@tonic-gate return; 6007c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 6017c478bd9Sstevel@tonic-gate if (sctp->sctp_saddrs[i].ipif_count == 0) 6027c478bd9Sstevel@tonic-gate continue; 6037c478bd9Sstevel@tonic-gate obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list); 6047c478bd9Sstevel@tonic-gate for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) { 6057c478bd9Sstevel@tonic-gate list_remove(&sctp->sctp_saddrs[i].sctp_ipif_list, obj); 6067c478bd9Sstevel@tonic-gate SCTP_IPIF_REFRELE(obj->saddr_ipifp); 6077c478bd9Sstevel@tonic-gate sctp->sctp_nsaddrs--; 6087c478bd9Sstevel@tonic-gate kmem_free(obj, sizeof (sctp_saddr_ipif_t)); 6097c478bd9Sstevel@tonic-gate obj = list_tail(&sctp->sctp_saddrs[i].sctp_ipif_list); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate sctp->sctp_saddrs[i].ipif_count = 0; 6127c478bd9Sstevel@tonic-gate } 613f551bb10Svi if (sctp->sctp_bound_to_all == 1) 614f551bb10Svi sctp->sctp_bound_to_all = 0; 6157c478bd9Sstevel@tonic-gate ASSERT(sctp->sctp_nsaddrs == 0); 6167c478bd9Sstevel@tonic-gate } 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate /* 6197c478bd9Sstevel@tonic-gate * Add/Delete the given ILL from the SCTP ILL list. Called with no locks 6207c478bd9Sstevel@tonic-gate * held. 6217c478bd9Sstevel@tonic-gate */ 6227c478bd9Sstevel@tonic-gate void 6237c478bd9Sstevel@tonic-gate sctp_update_ill(ill_t *ill, int op) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate int i; 6267c478bd9Sstevel@tonic-gate sctp_ill_t *sctp_ill = NULL; 6277c478bd9Sstevel@tonic-gate uint_t index; 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate ip2dbg(("sctp_update_ill: %s\n", ill->ill_name)); 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ills_lock, RW_WRITER); 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill)); 6347c478bd9Sstevel@tonic-gate sctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list); 6357c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_ills[index].ill_count; i++) { 6367c478bd9Sstevel@tonic-gate if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill)) 6377c478bd9Sstevel@tonic-gate break; 6387c478bd9Sstevel@tonic-gate sctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list, 6397c478bd9Sstevel@tonic-gate sctp_ill); 6407c478bd9Sstevel@tonic-gate } 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate switch (op) { 6437c478bd9Sstevel@tonic-gate case SCTP_ILL_INSERT: 6447c478bd9Sstevel@tonic-gate if (sctp_ill != NULL) { 6457c478bd9Sstevel@tonic-gate /* Unmark it if it is condemned */ 6467c478bd9Sstevel@tonic-gate if (sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) 6477c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_state = 0; 6487c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 6497c478bd9Sstevel@tonic-gate return; 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate sctp_ill = kmem_zalloc(sizeof (sctp_ill_t), KM_NOSLEEP); 6527c478bd9Sstevel@tonic-gate /* Need to re-try? */ 6537c478bd9Sstevel@tonic-gate if (sctp_ill == NULL) { 6547c478bd9Sstevel@tonic-gate ip1dbg(("sctp_ill_insert: mem error..\n")); 6557c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 6567c478bd9Sstevel@tonic-gate return; 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_name = 6597c478bd9Sstevel@tonic-gate kmem_zalloc(ill->ill_name_length, KM_NOSLEEP); 6607c478bd9Sstevel@tonic-gate if (sctp_ill->sctp_ill_name == NULL) { 6617c478bd9Sstevel@tonic-gate ip1dbg(("sctp_ill_insert: mem error..\n")); 6627c478bd9Sstevel@tonic-gate kmem_free(sctp_ill, sizeof (sctp_ill_t)); 6637c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 6647c478bd9Sstevel@tonic-gate return; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate bcopy(ill->ill_name, sctp_ill->sctp_ill_name, 6677c478bd9Sstevel@tonic-gate ill->ill_name_length); 6687c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_name_length = ill->ill_name_length; 6697c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_index = SCTP_ILL_TO_PHYINDEX(ill); 6707c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_flags = ill->ill_phyint->phyint_flags; 6717c478bd9Sstevel@tonic-gate list_insert_tail(&sctp_g_ills[index].sctp_ill_list, 6727c478bd9Sstevel@tonic-gate (void *)sctp_ill); 6737c478bd9Sstevel@tonic-gate sctp_g_ills[index].ill_count++; 6747c478bd9Sstevel@tonic-gate sctp_ills_count++; 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate break; 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate case SCTP_ILL_REMOVE: 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate if (sctp_ill == NULL) { 6817c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 6827c478bd9Sstevel@tonic-gate return; 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate if (sctp_ill->sctp_ill_ipifcnt == 0) { 6857c478bd9Sstevel@tonic-gate list_remove(&sctp_g_ills[index].sctp_ill_list, 6867c478bd9Sstevel@tonic-gate (void *)sctp_ill); 6877c478bd9Sstevel@tonic-gate sctp_g_ills[index].ill_count--; 6887c478bd9Sstevel@tonic-gate sctp_ills_count--; 6897c478bd9Sstevel@tonic-gate kmem_free(sctp_ill->sctp_ill_name, 6907c478bd9Sstevel@tonic-gate ill->ill_name_length); 6917c478bd9Sstevel@tonic-gate kmem_free(sctp_ill, sizeof (sctp_ill_t)); 6927c478bd9Sstevel@tonic-gate } else { 6937c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_state = SCTP_ILLS_CONDEMNED; 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate break; 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 6997c478bd9Sstevel@tonic-gate } 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate /* move ipif from f_ill to t_ill */ 7027c478bd9Sstevel@tonic-gate void 7037c478bd9Sstevel@tonic-gate sctp_move_ipif(ipif_t *ipif, ill_t *f_ill, ill_t *t_ill) 7047c478bd9Sstevel@tonic-gate { 7057c478bd9Sstevel@tonic-gate sctp_ill_t *fsctp_ill = NULL; 7067c478bd9Sstevel@tonic-gate sctp_ill_t *tsctp_ill = NULL; 7077c478bd9Sstevel@tonic-gate sctp_ipif_t *sctp_ipif; 7087c478bd9Sstevel@tonic-gate uint_t index; 7097c478bd9Sstevel@tonic-gate int i; 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ills_lock, RW_READER); 7127c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ipifs_lock, RW_READER); 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(f_ill)); 7157c478bd9Sstevel@tonic-gate fsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list); 7167c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_ills[index].ill_count; i++) { 7177c478bd9Sstevel@tonic-gate if (fsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(f_ill)) 7187c478bd9Sstevel@tonic-gate break; 7197c478bd9Sstevel@tonic-gate fsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list, 7207c478bd9Sstevel@tonic-gate fsctp_ill); 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(t_ill)); 7247c478bd9Sstevel@tonic-gate tsctp_ill = list_head(&sctp_g_ills[index].sctp_ill_list); 7257c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_ills[index].ill_count; i++) { 7267c478bd9Sstevel@tonic-gate if (tsctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(t_ill)) 7277c478bd9Sstevel@tonic-gate break; 7287c478bd9Sstevel@tonic-gate tsctp_ill = list_next(&sctp_g_ills[index].sctp_ill_list, 7297c478bd9Sstevel@tonic-gate tsctp_ill); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid); 7337c478bd9Sstevel@tonic-gate sctp_ipif = list_head(&sctp_g_ipifs[index].sctp_ipif_list); 7347c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_ipifs[index].ipif_count; i++) { 7357c478bd9Sstevel@tonic-gate if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid) 7367c478bd9Sstevel@tonic-gate break; 7377c478bd9Sstevel@tonic-gate sctp_ipif = list_next(&sctp_g_ipifs[index].sctp_ipif_list, 7387c478bd9Sstevel@tonic-gate sctp_ipif); 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate /* Should be an ASSERT? */ 7417c478bd9Sstevel@tonic-gate if (fsctp_ill == NULL || tsctp_ill == NULL || sctp_ipif == NULL) { 7427c478bd9Sstevel@tonic-gate ip1dbg(("sctp_move_ipif: error moving ipif %p from %p to %p\n", 7437c478bd9Sstevel@tonic-gate (void *)ipif, (void *)f_ill, (void *)t_ill)); 7447c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 7457c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 7467c478bd9Sstevel@tonic-gate return; 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER); 7497c478bd9Sstevel@tonic-gate ASSERT(sctp_ipif->sctp_ipif_ill == fsctp_ill); 7507c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_ill = tsctp_ill; 7517c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 7527c478bd9Sstevel@tonic-gate (void) atomic_add_32_nv(&fsctp_ill->sctp_ill_ipifcnt, -1); 7537c478bd9Sstevel@tonic-gate atomic_add_32(&tsctp_ill->sctp_ill_ipifcnt, 1); 7547c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 7557c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate /* Insert, Remove, Mark up or Mark down the ipif */ 7597c478bd9Sstevel@tonic-gate void 7607c478bd9Sstevel@tonic-gate sctp_update_ipif(ipif_t *ipif, int op) 7617c478bd9Sstevel@tonic-gate { 7627c478bd9Sstevel@tonic-gate ill_t *ill = ipif->ipif_ill; 7637c478bd9Sstevel@tonic-gate int i; 7647c478bd9Sstevel@tonic-gate sctp_ill_t *sctp_ill; 7657c478bd9Sstevel@tonic-gate sctp_ipif_t *sctp_ipif; 7667c478bd9Sstevel@tonic-gate uint_t ill_index; 7677c478bd9Sstevel@tonic-gate uint_t ipif_index; 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate ip2dbg(("sctp_update_ipif: %s %d\n", ill->ill_name, ipif->ipif_seqid)); 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ills_lock, RW_READER); 7727c478bd9Sstevel@tonic-gate rw_enter(&sctp_g_ipifs_lock, RW_WRITER); 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate ill_index = SCTP_ILL_HASH_FN(SCTP_ILL_TO_PHYINDEX(ill)); 7757c478bd9Sstevel@tonic-gate sctp_ill = list_head(&sctp_g_ills[ill_index].sctp_ill_list); 7767c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_ills[ill_index].ill_count; i++) { 7777c478bd9Sstevel@tonic-gate if (sctp_ill->sctp_ill_index == SCTP_ILL_TO_PHYINDEX(ill)) 7787c478bd9Sstevel@tonic-gate break; 7797c478bd9Sstevel@tonic-gate sctp_ill = list_next(&sctp_g_ills[ill_index].sctp_ill_list, 7807c478bd9Sstevel@tonic-gate sctp_ill); 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate if (sctp_ill == NULL) { 7837c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 7847c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 7857c478bd9Sstevel@tonic-gate return; 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate ipif_index = SCTP_IPIF_HASH_FN(ipif->ipif_seqid); 7897c478bd9Sstevel@tonic-gate sctp_ipif = list_head(&sctp_g_ipifs[ipif_index].sctp_ipif_list); 7907c478bd9Sstevel@tonic-gate for (i = 0; i < sctp_g_ipifs[ipif_index].ipif_count; i++) { 7917c478bd9Sstevel@tonic-gate if (sctp_ipif->sctp_ipif_id == ipif->ipif_seqid) 7927c478bd9Sstevel@tonic-gate break; 7937c478bd9Sstevel@tonic-gate sctp_ipif = list_next(&sctp_g_ipifs[ipif_index].sctp_ipif_list, 7947c478bd9Sstevel@tonic-gate sctp_ipif); 7957c478bd9Sstevel@tonic-gate } 7967c478bd9Sstevel@tonic-gate if (op != SCTP_IPIF_INSERT && sctp_ipif == NULL) { 7977c478bd9Sstevel@tonic-gate ip1dbg(("sctp_update_ipif: null sctp_ipif for %d\n", op)); 7987c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 7997c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 8007c478bd9Sstevel@tonic-gate return; 8017c478bd9Sstevel@tonic-gate } 8027c478bd9Sstevel@tonic-gate #ifdef DEBUG 8037c478bd9Sstevel@tonic-gate if (sctp_ipif != NULL) 8047c478bd9Sstevel@tonic-gate ASSERT(sctp_ill == sctp_ipif->sctp_ipif_ill); 8057c478bd9Sstevel@tonic-gate #endif 8067c478bd9Sstevel@tonic-gate switch (op) { 8077c478bd9Sstevel@tonic-gate case SCTP_IPIF_INSERT: 8087c478bd9Sstevel@tonic-gate if (sctp_ipif != NULL) { 8097c478bd9Sstevel@tonic-gate if (sctp_ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) 810df19b344Svi sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID; 8117c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 8127c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 8137c478bd9Sstevel@tonic-gate return; 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate sctp_ipif = kmem_zalloc(sizeof (sctp_ipif_t), KM_NOSLEEP); 8167c478bd9Sstevel@tonic-gate /* Try again? */ 8177c478bd9Sstevel@tonic-gate if (sctp_ipif == NULL) { 8187c478bd9Sstevel@tonic-gate ip1dbg(("sctp_ipif_insert: mem failure..\n")); 8197c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 8207c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 8217c478bd9Sstevel@tonic-gate return; 8227c478bd9Sstevel@tonic-gate } 8237c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_id = ipif->ipif_seqid; 8247c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_ill = sctp_ill; 825df19b344Svi sctp_ipif->sctp_ipif_state = SCTP_IPIFS_INVALID; 8267c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu; 8277c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid; 8287c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_isv6 = ill->ill_isv6; 829f551bb10Svi sctp_ipif->sctp_ipif_flags = ipif->ipif_flags; 8307c478bd9Sstevel@tonic-gate rw_init(&sctp_ipif->sctp_ipif_lock, NULL, RW_DEFAULT, NULL); 8317c478bd9Sstevel@tonic-gate list_insert_tail(&sctp_g_ipifs[ipif_index].sctp_ipif_list, 8327c478bd9Sstevel@tonic-gate (void *)sctp_ipif); 8337c478bd9Sstevel@tonic-gate sctp_g_ipifs[ipif_index].ipif_count++; 8347c478bd9Sstevel@tonic-gate sctp_g_ipifs_count++; 8357c478bd9Sstevel@tonic-gate atomic_add_32(&sctp_ill->sctp_ill_ipifcnt, 1); 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate break; 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate case SCTP_IPIF_REMOVE: 8407c478bd9Sstevel@tonic-gate { 8417c478bd9Sstevel@tonic-gate list_t *ipif_list; 8427c478bd9Sstevel@tonic-gate list_t *ill_list; 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate ill_list = &sctp_g_ills[ill_index].sctp_ill_list; 8457c478bd9Sstevel@tonic-gate ipif_list = &sctp_g_ipifs[ipif_index].sctp_ipif_list; 8467c478bd9Sstevel@tonic-gate if (sctp_ipif->sctp_ipif_refcnt != 0) { 8477c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_state = SCTP_IPIFS_CONDEMNED; 8487c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 8497c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 8507c478bd9Sstevel@tonic-gate return; 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate list_remove(ipif_list, (void *)sctp_ipif); 8537c478bd9Sstevel@tonic-gate sctp_g_ipifs[ipif_index].ipif_count--; 8547c478bd9Sstevel@tonic-gate sctp_g_ipifs_count--; 8557c478bd9Sstevel@tonic-gate rw_destroy(&sctp_ipif->sctp_ipif_lock); 8567c478bd9Sstevel@tonic-gate kmem_free(sctp_ipif, sizeof (sctp_ipif_t)); 8577c478bd9Sstevel@tonic-gate (void) atomic_add_32_nv(&sctp_ill->sctp_ill_ipifcnt, -1); 8587c478bd9Sstevel@tonic-gate if (rw_tryupgrade(&sctp_g_ills_lock) != 0) { 8597c478bd9Sstevel@tonic-gate rw_downgrade(&sctp_g_ipifs_lock); 8607c478bd9Sstevel@tonic-gate if (sctp_ill->sctp_ill_ipifcnt == 0 && 8617c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_state == SCTP_ILLS_CONDEMNED) { 8627c478bd9Sstevel@tonic-gate list_remove(ill_list, (void *)sctp_ill); 8637c478bd9Sstevel@tonic-gate sctp_ills_count--; 8647c478bd9Sstevel@tonic-gate sctp_g_ills[ill_index].ill_count--; 8657c478bd9Sstevel@tonic-gate kmem_free(sctp_ill->sctp_ill_name, 8667c478bd9Sstevel@tonic-gate sctp_ill->sctp_ill_name_length); 8677c478bd9Sstevel@tonic-gate kmem_free(sctp_ill, sizeof (sctp_ill_t)); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate break; 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate case SCTP_IPIF_UP: 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate rw_downgrade(&sctp_g_ipifs_lock); 8767c478bd9Sstevel@tonic-gate rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER); 8777c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_state = SCTP_IPIFS_UP; 8787c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr; 879f551bb10Svi sctp_ipif->sctp_ipif_flags = ipif->ipif_flags; 880f551bb10Svi sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu; 8817c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate break; 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate case SCTP_IPIF_UPDATE: 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate rw_downgrade(&sctp_g_ipifs_lock); 8887c478bd9Sstevel@tonic-gate rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER); 8897c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_mtu = ipif->ipif_mtu; 8907c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_saddr = ipif->ipif_v6lcl_addr; 8917c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_zoneid = ipif->ipif_zoneid; 892f551bb10Svi sctp_ipif->sctp_ipif_flags = ipif->ipif_flags; 8937c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate break; 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate case SCTP_IPIF_DOWN: 8987c478bd9Sstevel@tonic-gate 8997c478bd9Sstevel@tonic-gate rw_downgrade(&sctp_g_ipifs_lock); 9007c478bd9Sstevel@tonic-gate rw_enter(&sctp_ipif->sctp_ipif_lock, RW_WRITER); 9017c478bd9Sstevel@tonic-gate sctp_ipif->sctp_ipif_state = SCTP_IPIFS_DOWN; 9027c478bd9Sstevel@tonic-gate rw_exit(&sctp_ipif->sctp_ipif_lock); 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate break; 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ipifs_lock); 9077c478bd9Sstevel@tonic-gate rw_exit(&sctp_g_ills_lock); 9087c478bd9Sstevel@tonic-gate } 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate /* 9117c478bd9Sstevel@tonic-gate * 9127c478bd9Sstevel@tonic-gate * 9137c478bd9Sstevel@tonic-gate * SCTP source address list manipulaton, locking not used (except for 9147c478bd9Sstevel@tonic-gate * sctp locking by the caller. 9157c478bd9Sstevel@tonic-gate * 9167c478bd9Sstevel@tonic-gate * 9177c478bd9Sstevel@tonic-gate */ 9187c478bd9Sstevel@tonic-gate 9197c478bd9Sstevel@tonic-gate /* Remove a specific saddr from the list */ 9207c478bd9Sstevel@tonic-gate void 9217c478bd9Sstevel@tonic-gate sctp_del_saddr(sctp_t *sctp, sctp_saddr_ipif_t *sp) 9227c478bd9Sstevel@tonic-gate { 9237c478bd9Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL) 9247c478bd9Sstevel@tonic-gate mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate if (sctp->sctp_listen_tfp != NULL) 9277c478bd9Sstevel@tonic-gate mutex_enter(&sctp->sctp_listen_tfp->tf_lock); 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate sctp_ipif_hash_remove(sctp, sp->saddr_ipifp); 9307c478bd9Sstevel@tonic-gate 931f551bb10Svi if (sctp->sctp_bound_to_all == 1) 9327c478bd9Sstevel@tonic-gate sctp->sctp_bound_to_all = 0; 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL) 9357c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate if (sctp->sctp_listen_tfp != NULL) 9387c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_listen_tfp->tf_lock); 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate /* 9427c478bd9Sstevel@tonic-gate * Delete source address from the existing list. No error checking done here 9437c478bd9Sstevel@tonic-gate * Called with no locks held. 9447c478bd9Sstevel@tonic-gate */ 9457c478bd9Sstevel@tonic-gate void 9467c478bd9Sstevel@tonic-gate sctp_del_saddr_list(sctp_t *sctp, const void *addrs, int addcnt, 9477c478bd9Sstevel@tonic-gate boolean_t fanout_locked) 9487c478bd9Sstevel@tonic-gate { 9497c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4; 9507c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 9517c478bd9Sstevel@tonic-gate int cnt; 9527c478bd9Sstevel@tonic-gate in6_addr_t addr; 9537c478bd9Sstevel@tonic-gate sctp_ipif_t *sctp_ipif; 9541d8c4025Svi int ifindex = 0; 9557c478bd9Sstevel@tonic-gate 9561d8c4025Svi ASSERT(sctp->sctp_nsaddrs >= addcnt); 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate if (!fanout_locked) { 9597c478bd9Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL) 9607c478bd9Sstevel@tonic-gate mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 9617c478bd9Sstevel@tonic-gate if (sctp->sctp_listen_tfp != NULL) 9627c478bd9Sstevel@tonic-gate mutex_enter(&sctp->sctp_listen_tfp->tf_lock); 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < addcnt; cnt++) { 9667c478bd9Sstevel@tonic-gate switch (sctp->sctp_family) { 9677c478bd9Sstevel@tonic-gate case AF_INET: 9687c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)addrs + cnt; 9697c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &addr); 9707c478bd9Sstevel@tonic-gate break; 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate case AF_INET6: 9737c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)addrs + cnt; 9747c478bd9Sstevel@tonic-gate addr = sin6->sin6_addr; 9751d8c4025Svi ifindex = sin6->sin6_scope_id; 9767c478bd9Sstevel@tonic-gate break; 9777c478bd9Sstevel@tonic-gate } 978*5d0bc3edSsommerfe sctp_ipif = sctp_lookup_ipif_addr(&addr, B_FALSE, sctp, 979*5d0bc3edSsommerfe ifindex); 9807c478bd9Sstevel@tonic-gate ASSERT(sctp_ipif != NULL); 9817c478bd9Sstevel@tonic-gate sctp_ipif_hash_remove(sctp, sctp_ipif); 9827c478bd9Sstevel@tonic-gate } 983f551bb10Svi if (sctp->sctp_bound_to_all == 1) 9847c478bd9Sstevel@tonic-gate sctp->sctp_bound_to_all = 0; 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate if (!fanout_locked) { 9877c478bd9Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL) 9887c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 9897c478bd9Sstevel@tonic-gate if (sctp->sctp_listen_tfp != NULL) 9907c478bd9Sstevel@tonic-gate mutex_exit(&sctp->sctp_listen_tfp->tf_lock); 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate } 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate /* 9957c478bd9Sstevel@tonic-gate * Given an address get the corresponding entry from the list 9967c478bd9Sstevel@tonic-gate * Called with no locks held. 9977c478bd9Sstevel@tonic-gate */ 9987c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t * 9991d8c4025Svi sctp_saddr_lookup(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex) 10007c478bd9Sstevel@tonic-gate { 10017c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *saddr_ipifs; 10027c478bd9Sstevel@tonic-gate sctp_ipif_t *sctp_ipif; 10037c478bd9Sstevel@tonic-gate 1004*5d0bc3edSsommerfe sctp_ipif = sctp_lookup_ipif_addr(addr, B_FALSE, sctp, ifindex); 10057c478bd9Sstevel@tonic-gate if (sctp_ipif == NULL) 10067c478bd9Sstevel@tonic-gate return (NULL); 10077c478bd9Sstevel@tonic-gate 10087c478bd9Sstevel@tonic-gate saddr_ipifs = sctp_ipif_lookup(sctp, sctp_ipif->sctp_ipif_id); 10097c478bd9Sstevel@tonic-gate return (saddr_ipifs); 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate 1012f551bb10Svi /* Given an address, add it to the source address list */ 1013f551bb10Svi int 10141d8c4025Svi sctp_saddr_add_addr(sctp_t *sctp, in6_addr_t *addr, uint_t ifindex) 1015f551bb10Svi { 1016f551bb10Svi sctp_ipif_t *sctp_ipif; 1017f551bb10Svi 1018*5d0bc3edSsommerfe sctp_ipif = sctp_lookup_ipif_addr(addr, B_TRUE, sctp, ifindex); 1019f551bb10Svi if (sctp_ipif == NULL) 1020f551bb10Svi return (EINVAL); 1021f551bb10Svi 1022f551bb10Svi if (sctp_ipif_hash_insert(sctp, sctp_ipif, KM_NOSLEEP, B_FALSE) != 0) { 1023f551bb10Svi SCTP_IPIF_REFRELE(sctp_ipif); 1024f551bb10Svi return (EINVAL); 1025f551bb10Svi } 1026f551bb10Svi return (0); 1027f551bb10Svi } 1028f551bb10Svi 1029f551bb10Svi /* 1030f551bb10Svi * Remove or mark as dontsrc addresses that are currently not part of the 1031f551bb10Svi * association. One would delete addresses when processing an INIT and 1032f551bb10Svi * mark as dontsrc when processing an INIT-ACK. 1033f551bb10Svi */ 1034f551bb10Svi void 1035f551bb10Svi sctp_check_saddr(sctp_t *sctp, int supp_af, boolean_t delete) 1036f551bb10Svi { 1037f551bb10Svi int i; 1038f551bb10Svi int l; 1039f551bb10Svi sctp_saddr_ipif_t *obj; 1040f551bb10Svi int scanned = 0; 1041f551bb10Svi int naddr; 1042f551bb10Svi int nsaddr; 1043f551bb10Svi 1044f551bb10Svi ASSERT(!sctp->sctp_loopback && !sctp->sctp_linklocal && supp_af != 0); 1045f551bb10Svi 1046f551bb10Svi /* 1047f551bb10Svi * Irregardless of the supported address in the INIT, v4 1048f551bb10Svi * must be supported. 1049f551bb10Svi */ 1050f551bb10Svi if (sctp->sctp_family == AF_INET) 1051f551bb10Svi supp_af = PARM_SUPP_V4; 1052f551bb10Svi 1053f551bb10Svi nsaddr = sctp->sctp_nsaddrs; 1054f551bb10Svi for (i = 0; i < SCTP_IPIF_HASH; i++) { 1055f551bb10Svi if (sctp->sctp_saddrs[i].ipif_count == 0) 1056f551bb10Svi continue; 1057f551bb10Svi obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list); 1058f551bb10Svi naddr = sctp->sctp_saddrs[i].ipif_count; 1059f551bb10Svi for (l = 0; l < naddr; l++) { 1060f551bb10Svi sctp_ipif_t *ipif; 1061f551bb10Svi 1062f551bb10Svi ipif = obj->saddr_ipifp; 1063f551bb10Svi scanned++; 1064f551bb10Svi 1065f551bb10Svi /* 1066f551bb10Svi * Delete/mark dontsrc loopback/linklocal addresses and 1067f551bb10Svi * unsupported address. 10681d8c4025Svi * On a clustered node, we trust the clustering module 10691d8c4025Svi * to do the right thing w.r.t loopback addresses, so 10701d8c4025Svi * we ignore loopback addresses in this check. 1071f551bb10Svi */ 10721d8c4025Svi if ((SCTP_IS_IPIF_LOOPBACK(ipif) && 10731d8c4025Svi cl_sctp_check_addrs == NULL) || 10741d8c4025Svi SCTP_IS_IPIF_LINKLOCAL(ipif) || 1075f551bb10Svi SCTP_UNSUPP_AF(ipif, supp_af)) { 1076f551bb10Svi if (!delete) { 1077f551bb10Svi obj->saddr_ipif_unconfirmed = 1; 1078f551bb10Svi goto next_obj; 1079f551bb10Svi } 1080f551bb10Svi if (sctp->sctp_bound_to_all == 1) 1081f551bb10Svi sctp->sctp_bound_to_all = 0; 1082f551bb10Svi if (scanned < nsaddr) { 1083f551bb10Svi obj = list_next(&sctp->sctp_saddrs[i]. 1084f551bb10Svi sctp_ipif_list, obj); 1085f551bb10Svi sctp_ipif_hash_remove(sctp, ipif); 1086f551bb10Svi continue; 1087f551bb10Svi } 1088f551bb10Svi sctp_ipif_hash_remove(sctp, ipif); 1089f551bb10Svi } 1090f551bb10Svi next_obj: 1091f551bb10Svi if (scanned >= nsaddr) 1092f551bb10Svi return; 1093f551bb10Svi obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list, 1094f551bb10Svi obj); 1095f551bb10Svi } 1096f551bb10Svi } 1097f551bb10Svi } 1098f551bb10Svi 1099f551bb10Svi 11007c478bd9Sstevel@tonic-gate /* Get the first valid address from the list. Called with no locks held */ 11017c478bd9Sstevel@tonic-gate in6_addr_t 11027c478bd9Sstevel@tonic-gate sctp_get_valid_addr(sctp_t *sctp, boolean_t isv6) 11037c478bd9Sstevel@tonic-gate { 11047c478bd9Sstevel@tonic-gate int i; 11057c478bd9Sstevel@tonic-gate int l; 11067c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *obj; 11077c478bd9Sstevel@tonic-gate int scanned = 0; 11087c478bd9Sstevel@tonic-gate in6_addr_t addr; 11097c478bd9Sstevel@tonic-gate 11107c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 11117c478bd9Sstevel@tonic-gate if (sctp->sctp_saddrs[i].ipif_count == 0) 11127c478bd9Sstevel@tonic-gate continue; 11137c478bd9Sstevel@tonic-gate obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list); 11147c478bd9Sstevel@tonic-gate for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) { 11157c478bd9Sstevel@tonic-gate sctp_ipif_t *ipif; 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate ipif = obj->saddr_ipifp; 1118f551bb10Svi if (!SCTP_DONT_SRC(obj) && 11197c478bd9Sstevel@tonic-gate ipif->sctp_ipif_isv6 == isv6 && 1120f551bb10Svi ipif->sctp_ipif_state == SCTP_IPIFS_UP) { 11217c478bd9Sstevel@tonic-gate return (ipif->sctp_ipif_saddr); 11227c478bd9Sstevel@tonic-gate } 11237c478bd9Sstevel@tonic-gate scanned++; 11247c478bd9Sstevel@tonic-gate if (scanned >= sctp->sctp_nsaddrs) 11257c478bd9Sstevel@tonic-gate goto got_none; 11267c478bd9Sstevel@tonic-gate obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list, 11277c478bd9Sstevel@tonic-gate obj); 11287c478bd9Sstevel@tonic-gate } 11297c478bd9Sstevel@tonic-gate } 11307c478bd9Sstevel@tonic-gate got_none: 11317c478bd9Sstevel@tonic-gate /* Need to double check this */ 11327c478bd9Sstevel@tonic-gate if (isv6 == B_TRUE) 11337c478bd9Sstevel@tonic-gate addr = ipv6_all_zeros; 11347c478bd9Sstevel@tonic-gate else 11357c478bd9Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(0, &addr); 11367c478bd9Sstevel@tonic-gate 11377c478bd9Sstevel@tonic-gate return (addr); 11387c478bd9Sstevel@tonic-gate } 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate /* 11417c478bd9Sstevel@tonic-gate * Return the list of local addresses of an association. The parameter 11427c478bd9Sstevel@tonic-gate * myaddrs is supposed to be either (struct sockaddr_in *) or (struct 11437c478bd9Sstevel@tonic-gate * sockaddr_in6 *) depending on the address family. 11447c478bd9Sstevel@tonic-gate */ 11457c478bd9Sstevel@tonic-gate int 11467c478bd9Sstevel@tonic-gate sctp_getmyaddrs(void *conn, void *myaddrs, int *addrcnt) 11477c478bd9Sstevel@tonic-gate { 11487c478bd9Sstevel@tonic-gate int i; 11497c478bd9Sstevel@tonic-gate int l; 11507c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *obj; 11517c478bd9Sstevel@tonic-gate sctp_t *sctp = (sctp_t *)conn; 11527c478bd9Sstevel@tonic-gate int family = sctp->sctp_family; 11537c478bd9Sstevel@tonic-gate int max = *addrcnt; 11547c478bd9Sstevel@tonic-gate size_t added = 0; 11557c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 11567c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4; 11577c478bd9Sstevel@tonic-gate int scanned = 0; 11587c478bd9Sstevel@tonic-gate boolean_t skip_lback = B_FALSE; 11597c478bd9Sstevel@tonic-gate 11607c478bd9Sstevel@tonic-gate if (sctp->sctp_nsaddrs == 0) 11617c478bd9Sstevel@tonic-gate return (EINVAL); 11627c478bd9Sstevel@tonic-gate 11631d8c4025Svi /* 11641d8c4025Svi * Skip loopback addresses for non-loopback assoc., ignore 11651d8c4025Svi * this on a clustered node. 11661d8c4025Svi */ 11671d8c4025Svi if (sctp->sctp_state >= SCTPS_ESTABLISHED && !sctp->sctp_loopback && 11681d8c4025Svi (cl_sctp_check_addrs == NULL)) { 11697c478bd9Sstevel@tonic-gate skip_lback = B_TRUE; 11701d8c4025Svi } 11711d8c4025Svi 11727c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 11737c478bd9Sstevel@tonic-gate if (sctp->sctp_saddrs[i].ipif_count == 0) 11747c478bd9Sstevel@tonic-gate continue; 11757c478bd9Sstevel@tonic-gate obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list); 11767c478bd9Sstevel@tonic-gate for (l = 0; l < sctp->sctp_saddrs[i].ipif_count; l++) { 11777c478bd9Sstevel@tonic-gate sctp_ipif_t *ipif = obj->saddr_ipifp; 11787c478bd9Sstevel@tonic-gate in6_addr_t addr = ipif->sctp_ipif_saddr; 11797c478bd9Sstevel@tonic-gate 11807c478bd9Sstevel@tonic-gate scanned++; 11817c478bd9Sstevel@tonic-gate if ((ipif->sctp_ipif_state == SCTP_IPIFS_CONDEMNED) || 1182f551bb10Svi SCTP_DONT_SRC(obj) || 11831d8c4025Svi (SCTP_IS_IPIF_LOOPBACK(ipif) && skip_lback)) { 11847c478bd9Sstevel@tonic-gate if (scanned >= sctp->sctp_nsaddrs) 11857c478bd9Sstevel@tonic-gate goto done; 11867c478bd9Sstevel@tonic-gate obj = list_next(&sctp->sctp_saddrs[i]. 11877c478bd9Sstevel@tonic-gate sctp_ipif_list, obj); 11887c478bd9Sstevel@tonic-gate continue; 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate switch (family) { 11917c478bd9Sstevel@tonic-gate case AF_INET: 11927c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)myaddrs + added; 11937c478bd9Sstevel@tonic-gate sin4->sin_family = AF_INET; 11947c478bd9Sstevel@tonic-gate sin4->sin_port = sctp->sctp_lport; 11957c478bd9Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr); 11967c478bd9Sstevel@tonic-gate break; 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate case AF_INET6: 11997c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)myaddrs + added; 12007c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 12017c478bd9Sstevel@tonic-gate sin6->sin6_port = sctp->sctp_lport; 12027c478bd9Sstevel@tonic-gate sin6->sin6_addr = addr; 12037c478bd9Sstevel@tonic-gate break; 12047c478bd9Sstevel@tonic-gate } 12057c478bd9Sstevel@tonic-gate added++; 12067c478bd9Sstevel@tonic-gate if (added >= max || scanned >= sctp->sctp_nsaddrs) 12077c478bd9Sstevel@tonic-gate goto done; 12087c478bd9Sstevel@tonic-gate obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list, 12097c478bd9Sstevel@tonic-gate obj); 12107c478bd9Sstevel@tonic-gate } 12117c478bd9Sstevel@tonic-gate } 12127c478bd9Sstevel@tonic-gate done: 12137c478bd9Sstevel@tonic-gate *addrcnt = added; 12147c478bd9Sstevel@tonic-gate return (0); 12157c478bd9Sstevel@tonic-gate } 12167c478bd9Sstevel@tonic-gate 12177c478bd9Sstevel@tonic-gate /* 1218df19b344Svi * Given the supported address family, walk through the source address list 1219df19b344Svi * and return the total length of the available addresses. If 'p' is not 1220df19b344Svi * null, construct the parameter list for the addresses in 'p'. 1221f551bb10Svi * 'modify' will only be set when we want the source address list to 1222f551bb10Svi * be modified. The source address list will be modified only when 1223f551bb10Svi * generating an INIT chunk. For generating an INIT-ACK 'modify' will 1224f551bb10Svi * be false since the 'sctp' will be that of the listener. 12257c478bd9Sstevel@tonic-gate */ 12267c478bd9Sstevel@tonic-gate size_t 1227f551bb10Svi sctp_saddr_info(sctp_t *sctp, int supp_af, uchar_t *p, boolean_t modify) 12287c478bd9Sstevel@tonic-gate { 12297c478bd9Sstevel@tonic-gate int i; 12307c478bd9Sstevel@tonic-gate int l; 12317c478bd9Sstevel@tonic-gate sctp_saddr_ipif_t *obj; 1232df19b344Svi size_t paramlen = 0; 12337c478bd9Sstevel@tonic-gate sctp_parm_hdr_t *hdr; 12347c478bd9Sstevel@tonic-gate int scanned = 0; 1235f551bb10Svi int naddr; 1236f551bb10Svi int nsaddr; 12371d8c4025Svi boolean_t del_ll = B_FALSE; 12381d8c4025Svi boolean_t del_lb = B_FALSE; 12391d8c4025Svi 12401d8c4025Svi 12411d8c4025Svi /* 12421d8c4025Svi * On a clustered node don't bother changing anything 12431d8c4025Svi * on the loopback interface. 12441d8c4025Svi */ 12451d8c4025Svi if (modify && !sctp->sctp_loopback && (cl_sctp_check_addrs == NULL)) 12461d8c4025Svi del_lb = B_TRUE; 1247f551bb10Svi 12481d8c4025Svi if (modify && !sctp->sctp_linklocal) 12491d8c4025Svi del_ll = B_TRUE; 12507c478bd9Sstevel@tonic-gate 1251f551bb10Svi nsaddr = sctp->sctp_nsaddrs; 12527c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 12537c478bd9Sstevel@tonic-gate if (sctp->sctp_saddrs[i].ipif_count == 0) 12547c478bd9Sstevel@tonic-gate continue; 12557c478bd9Sstevel@tonic-gate obj = list_head(&sctp->sctp_saddrs[i].sctp_ipif_list); 1256f551bb10Svi naddr = sctp->sctp_saddrs[i].ipif_count; 1257f551bb10Svi for (l = 0; l < naddr; l++) { 12587c478bd9Sstevel@tonic-gate in6_addr_t addr; 12597c478bd9Sstevel@tonic-gate sctp_ipif_t *ipif; 12601d8c4025Svi boolean_t ipif_lb; 12611d8c4025Svi boolean_t ipif_ll; 1262f551bb10Svi boolean_t unsupp_af; 12637c478bd9Sstevel@tonic-gate 12647c478bd9Sstevel@tonic-gate ipif = obj->saddr_ipifp; 12657c478bd9Sstevel@tonic-gate scanned++; 1266f551bb10Svi 12671d8c4025Svi ipif_lb = SCTP_IS_IPIF_LOOPBACK(ipif); 12681d8c4025Svi ipif_ll = SCTP_IS_IPIF_LINKLOCAL(ipif); 1269f551bb10Svi unsupp_af = SCTP_UNSUPP_AF(ipif, supp_af); 1270f551bb10Svi /* 1271f551bb10Svi * We need to either delete or skip loopback/linklocal 12721d8c4025Svi * or unsupported addresses, if required. 1273f551bb10Svi */ 12741d8c4025Svi if ((ipif_ll && del_ll) || (ipif_lb && del_lb) || 12751d8c4025Svi (unsupp_af && modify)) { 1276f551bb10Svi if (sctp->sctp_bound_to_all == 1) 1277f551bb10Svi sctp->sctp_bound_to_all = 0; 1278f551bb10Svi if (scanned < nsaddr) { 1279f551bb10Svi obj = list_next(&sctp->sctp_saddrs[i]. 1280f551bb10Svi sctp_ipif_list, obj); 1281f551bb10Svi sctp_ipif_hash_remove(sctp, ipif); 1282f551bb10Svi continue; 1283f551bb10Svi } 1284f551bb10Svi sctp_ipif_hash_remove(sctp, ipif); 1285f551bb10Svi goto next_addr; 12861d8c4025Svi } else if (ipif_ll || unsupp_af || 12871d8c4025Svi (ipif_lb && (cl_sctp_check_addrs == NULL))) { 1288df19b344Svi goto next_addr; 12897c478bd9Sstevel@tonic-gate } 1290f551bb10Svi 1291f551bb10Svi if (!SCTP_IPIF_USABLE(ipif->sctp_ipif_state)) 1292f551bb10Svi goto next_addr; 1293df19b344Svi if (p != NULL) 1294df19b344Svi hdr = (sctp_parm_hdr_t *)(p + paramlen); 12957c478bd9Sstevel@tonic-gate addr = ipif->sctp_ipif_saddr; 1296f551bb10Svi if (!ipif->sctp_ipif_isv6) { 12977c478bd9Sstevel@tonic-gate struct in_addr *v4; 12987c478bd9Sstevel@tonic-gate 1299df19b344Svi if (p != NULL) { 1300df19b344Svi hdr->sph_type = htons(PARM_ADDR4); 1301df19b344Svi hdr->sph_len = htons(PARM_ADDR4_LEN); 1302df19b344Svi v4 = (struct in_addr *)(hdr + 1); 1303df19b344Svi IN6_V4MAPPED_TO_INADDR(&addr, v4); 1304df19b344Svi } 1305df19b344Svi paramlen += PARM_ADDR4_LEN; 1306f551bb10Svi } else { 1307df19b344Svi if (p != NULL) { 1308df19b344Svi hdr->sph_type = htons(PARM_ADDR6); 1309df19b344Svi hdr->sph_len = htons(PARM_ADDR6_LEN); 1310df19b344Svi bcopy(&addr, hdr + 1, sizeof (addr)); 1311df19b344Svi } 1312df19b344Svi paramlen += PARM_ADDR6_LEN; 13137c478bd9Sstevel@tonic-gate } 1314df19b344Svi next_addr: 1315f551bb10Svi if (scanned >= nsaddr) 1316df19b344Svi return (paramlen); 13177c478bd9Sstevel@tonic-gate obj = list_next(&sctp->sctp_saddrs[i].sctp_ipif_list, 13187c478bd9Sstevel@tonic-gate obj); 13197c478bd9Sstevel@tonic-gate } 13207c478bd9Sstevel@tonic-gate } 1321df19b344Svi return (paramlen); 13227c478bd9Sstevel@tonic-gate } 13237c478bd9Sstevel@tonic-gate 13241d8c4025Svi /* 13251d8c4025Svi * This is used on a clustered node to obtain a list of addresses, the list 13261d8c4025Svi * consists of sockaddr_in structs for v4 and sockaddr_in6 for v6. The list 13271d8c4025Svi * is then passed onto the clustering module which sends back the correct 13281d8c4025Svi * list based on the port info. Regardless of the input, i.e INADDR_ANY 13291d8c4025Svi * or specific address(es), we create the list since it could be modified by 13301d8c4025Svi * the clustering module. When given a list of addresses, we simply 13311d8c4025Svi * create the list of sockaddr_in or sockaddr_in6 structs using those 13321d8c4025Svi * addresses. If there is an INADDR_ANY in the input list, or if the 13331d8c4025Svi * input is INADDR_ANY, we create a list of sockaddr_in or sockaddr_in6 13341d8c4025Svi * structs consisting all the addresses in the global interface list 13351d8c4025Svi * except those that are hosted on the loopback interface. We create 13361d8c4025Svi * a list of sockaddr_in[6] structs just so that it can be directly input 13371d8c4025Svi * to sctp_valid_addr_list() once the clustering module has processed it. 13381d8c4025Svi */ 13391d8c4025Svi int 13401d8c4025Svi sctp_get_addrlist(sctp_t *sctp, const void *addrs, uint32_t *addrcnt, 13411d8c4025Svi uchar_t **addrlist, int *uspec, size_t *size) 13421d8c4025Svi { 13431d8c4025Svi int cnt; 13441d8c4025Svi int icnt; 13451d8c4025Svi sctp_ipif_t *sctp_ipif; 13461d8c4025Svi struct sockaddr_in *s4; 13471d8c4025Svi struct sockaddr_in6 *s6; 13481d8c4025Svi uchar_t *p; 13491d8c4025Svi int err = 0; 13501d8c4025Svi 13511d8c4025Svi *addrlist = NULL; 13521d8c4025Svi *size = 0; 13531d8c4025Svi 13541d8c4025Svi /* 13551d8c4025Svi * Create a list of sockaddr_in[6] structs using the input list. 13561d8c4025Svi */ 13571d8c4025Svi if (sctp->sctp_family == AF_INET) { 13581d8c4025Svi *size = sizeof (struct sockaddr_in) * *addrcnt; 13591d8c4025Svi *addrlist = kmem_zalloc(*size, KM_SLEEP); 13601d8c4025Svi p = *addrlist; 13611d8c4025Svi for (cnt = 0; cnt < *addrcnt; cnt++) { 13621d8c4025Svi s4 = (struct sockaddr_in *)addrs + cnt; 13631d8c4025Svi /* 13641d8c4025Svi * We need to create a list of all the available 13651d8c4025Svi * addresses if there is an INADDR_ANY. However, 13661d8c4025Svi * if we are beyond LISTEN, then this is invalid 13671d8c4025Svi * (see sctp_valid_addr_list(). So, we just fail 13681d8c4025Svi * it here rather than wait till it fails in 13691d8c4025Svi * sctp_valid_addr_list(). 13701d8c4025Svi */ 13711d8c4025Svi if (s4->sin_addr.s_addr == INADDR_ANY) { 13721d8c4025Svi kmem_free(*addrlist, *size); 13731d8c4025Svi *addrlist = NULL; 13741d8c4025Svi *size = 0; 13751d8c4025Svi if (sctp->sctp_state > SCTPS_LISTEN) { 13761d8c4025Svi *addrcnt = 0; 13771d8c4025Svi return (EINVAL); 13781d8c4025Svi } 13791d8c4025Svi if (uspec != NULL) 13801d8c4025Svi *uspec = 1; 13811d8c4025Svi goto get_all_addrs; 13821d8c4025Svi } else { 13831d8c4025Svi bcopy(s4, p, sizeof (*s4)); 13841d8c4025Svi p += sizeof (*s4); 13851d8c4025Svi } 13861d8c4025Svi } 13871d8c4025Svi } else { 13881d8c4025Svi *size = sizeof (struct sockaddr_in6) * *addrcnt; 13891d8c4025Svi *addrlist = kmem_zalloc(*size, KM_SLEEP); 13901d8c4025Svi p = *addrlist; 13911d8c4025Svi for (cnt = 0; cnt < *addrcnt; cnt++) { 13921d8c4025Svi s6 = (struct sockaddr_in6 *)addrs + cnt; 13931d8c4025Svi /* 13941d8c4025Svi * Comments for INADDR_ANY, above, apply here too. 13951d8c4025Svi */ 13961d8c4025Svi if (IN6_IS_ADDR_UNSPECIFIED(&s6->sin6_addr)) { 13971d8c4025Svi kmem_free(*addrlist, *size); 13981d8c4025Svi *size = 0; 13991d8c4025Svi *addrlist = NULL; 14001d8c4025Svi if (sctp->sctp_state > SCTPS_LISTEN) { 14011d8c4025Svi *addrcnt = 0; 14021d8c4025Svi return (EINVAL); 14031d8c4025Svi } 14041d8c4025Svi if (uspec != NULL) 14051d8c4025Svi *uspec = 1; 14061d8c4025Svi goto get_all_addrs; 14071d8c4025Svi } else { 14081d8c4025Svi bcopy(addrs, p, sizeof (*s6)); 14091d8c4025Svi p += sizeof (*s6); 14101d8c4025Svi } 14111d8c4025Svi } 14121d8c4025Svi } 14131d8c4025Svi return (err); 14141d8c4025Svi get_all_addrs: 14151d8c4025Svi 14161d8c4025Svi /* 14171d8c4025Svi * Allocate max possible size. We allocate the max. size here because 14181d8c4025Svi * the clustering module could end up adding addresses to the list. 14191d8c4025Svi * We allocate upfront so that the clustering module need to bother 14201d8c4025Svi * re-sizing the list. 14211d8c4025Svi */ 14221d8c4025Svi if (sctp->sctp_family == AF_INET) 14231d8c4025Svi *size = sizeof (struct sockaddr_in) * sctp_g_ipifs_count; 14241d8c4025Svi else 14251d8c4025Svi *size = sizeof (struct sockaddr_in6) * sctp_g_ipifs_count; 14261d8c4025Svi 14271d8c4025Svi *addrlist = kmem_zalloc(*size, KM_SLEEP); 14281d8c4025Svi *addrcnt = 0; 14291d8c4025Svi p = *addrlist; 14301d8c4025Svi rw_enter(&sctp_g_ipifs_lock, RW_READER); 14311d8c4025Svi 14321d8c4025Svi /* 14331d8c4025Svi * Walk through the global interface list and add all addresses, 14341d8c4025Svi * except those that are hosted on loopback interfaces. 14351d8c4025Svi */ 14361d8c4025Svi for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) { 14371d8c4025Svi if (sctp_g_ipifs[cnt].ipif_count == 0) 14381d8c4025Svi continue; 14391d8c4025Svi sctp_ipif = list_head(&sctp_g_ipifs[cnt].sctp_ipif_list); 14401d8c4025Svi for (icnt = 0; icnt < sctp_g_ipifs[cnt].ipif_count; icnt++) { 14411d8c4025Svi in6_addr_t addr; 14421d8c4025Svi 14431d8c4025Svi rw_enter(&sctp_ipif->sctp_ipif_lock, RW_READER); 14441d8c4025Svi addr = sctp_ipif->sctp_ipif_saddr; 14451d8c4025Svi if (SCTP_IPIF_DISCARD(sctp_ipif->sctp_ipif_flags) || 14461d8c4025Svi !SCTP_IPIF_USABLE(sctp_ipif->sctp_ipif_state) || 14471d8c4025Svi SCTP_IS_IPIF_LOOPBACK(sctp_ipif) || 14481d8c4025Svi SCTP_IS_IPIF_LINKLOCAL(sctp_ipif) || 1449*5d0bc3edSsommerfe !SCTP_IPIF_ZONE_MATCH(sctp, sctp_ipif) || 14501d8c4025Svi (sctp->sctp_ipversion == IPV4_VERSION && 14511d8c4025Svi sctp_ipif->sctp_ipif_isv6) || 14521d8c4025Svi (sctp->sctp_connp->conn_ipv6_v6only && 14531d8c4025Svi !sctp_ipif->sctp_ipif_isv6)) { 14541d8c4025Svi rw_exit(&sctp_ipif->sctp_ipif_lock); 14551d8c4025Svi sctp_ipif = list_next( 14561d8c4025Svi &sctp_g_ipifs[cnt].sctp_ipif_list, 14571d8c4025Svi sctp_ipif); 14581d8c4025Svi continue; 14591d8c4025Svi } 14601d8c4025Svi rw_exit(&sctp_ipif->sctp_ipif_lock); 14611d8c4025Svi if (sctp->sctp_family == AF_INET) { 14621d8c4025Svi s4 = (struct sockaddr_in *)p; 14631d8c4025Svi IN6_V4MAPPED_TO_INADDR(&addr, &s4->sin_addr); 14641d8c4025Svi s4->sin_family = AF_INET; 14651d8c4025Svi p += sizeof (*s4); 14661d8c4025Svi } else { 14671d8c4025Svi s6 = (struct sockaddr_in6 *)p; 14681d8c4025Svi s6->sin6_addr = addr; 14691d8c4025Svi s6->sin6_family = AF_INET6; 14701d8c4025Svi s6->sin6_scope_id = 14711d8c4025Svi sctp_ipif->sctp_ipif_ill->sctp_ill_index; 14721d8c4025Svi p += sizeof (*s6); 14731d8c4025Svi } 14741d8c4025Svi (*addrcnt)++; 14751d8c4025Svi sctp_ipif = list_next(&sctp_g_ipifs[cnt].sctp_ipif_list, 14761d8c4025Svi sctp_ipif); 14771d8c4025Svi } 14781d8c4025Svi } 14791d8c4025Svi rw_exit(&sctp_g_ipifs_lock); 14801d8c4025Svi return (err); 14811d8c4025Svi } 14821d8c4025Svi 14831d8c4025Svi /* 14841d8c4025Svi * Get a list of addresses from the source address list. The caller is 14851d8c4025Svi * responsible for allocating sufficient buffer for this. 14861d8c4025Svi */ 14871d8c4025Svi void 14881d8c4025Svi sctp_get_saddr_list(sctp_t *sctp, uchar_t *p, size_t psize) 14891d8c4025Svi { 14901d8c4025Svi int cnt; 14911d8c4025Svi int icnt; 14921d8c4025Svi sctp_saddr_ipif_t *obj; 14931d8c4025Svi int naddr; 14941d8c4025Svi int scanned = 0; 14951d8c4025Svi 14961d8c4025Svi for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) { 14971d8c4025Svi if (sctp->sctp_saddrs[cnt].ipif_count == 0) 14981d8c4025Svi continue; 14991d8c4025Svi obj = list_head(&sctp->sctp_saddrs[cnt].sctp_ipif_list); 15001d8c4025Svi naddr = sctp->sctp_saddrs[cnt].ipif_count; 15011d8c4025Svi for (icnt = 0; icnt < naddr; icnt++) { 15021d8c4025Svi sctp_ipif_t *ipif; 15031d8c4025Svi 15041d8c4025Svi if (psize < sizeof (ipif->sctp_ipif_saddr)) 15051d8c4025Svi return; 15061d8c4025Svi 15071d8c4025Svi scanned++; 15081d8c4025Svi ipif = obj->saddr_ipifp; 15091d8c4025Svi bcopy(&ipif->sctp_ipif_saddr, p, 15101d8c4025Svi sizeof (ipif->sctp_ipif_saddr)); 15111d8c4025Svi p += sizeof (ipif->sctp_ipif_saddr); 15121d8c4025Svi psize -= sizeof (ipif->sctp_ipif_saddr); 15131d8c4025Svi if (scanned >= sctp->sctp_nsaddrs) 15141d8c4025Svi return; 15151d8c4025Svi obj = list_next(&sctp->sctp_saddrs[icnt].sctp_ipif_list, 15161d8c4025Svi obj); 15171d8c4025Svi } 15181d8c4025Svi } 15191d8c4025Svi } 15201d8c4025Svi 15211d8c4025Svi /* 15221d8c4025Svi * Get a list of addresses from the remote address list. The caller is 15231d8c4025Svi * responsible for allocating sufficient buffer for this. 15241d8c4025Svi */ 15251d8c4025Svi void 15261d8c4025Svi sctp_get_faddr_list(sctp_t *sctp, uchar_t *p, size_t psize) 15271d8c4025Svi { 15281d8c4025Svi sctp_faddr_t *fp; 15291d8c4025Svi 15301d8c4025Svi for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 15311d8c4025Svi if (psize < sizeof (fp->faddr)) 15321d8c4025Svi return; 15331d8c4025Svi bcopy(&fp->faddr, p, sizeof (fp->faddr)); 15341d8c4025Svi p += sizeof (fp->faddr); 15351d8c4025Svi psize -= sizeof (fp->faddr); 15361d8c4025Svi } 15371d8c4025Svi } 15387c478bd9Sstevel@tonic-gate 15397c478bd9Sstevel@tonic-gate /* Initialize the SCTP ILL list and lock */ 15407c478bd9Sstevel@tonic-gate void 15417c478bd9Sstevel@tonic-gate sctp_saddr_init() 15427c478bd9Sstevel@tonic-gate { 15437c478bd9Sstevel@tonic-gate int i; 15447c478bd9Sstevel@tonic-gate 15457c478bd9Sstevel@tonic-gate rw_init(&sctp_g_ills_lock, NULL, RW_DEFAULT, NULL); 15467c478bd9Sstevel@tonic-gate rw_init(&sctp_g_ipifs_lock, NULL, RW_DEFAULT, NULL); 15477c478bd9Sstevel@tonic-gate 15487c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_ILL_HASH; i++) { 15497c478bd9Sstevel@tonic-gate sctp_g_ills[i].ill_count = 0; 15507c478bd9Sstevel@tonic-gate list_create(&sctp_g_ills[i].sctp_ill_list, sizeof (sctp_ill_t), 15517c478bd9Sstevel@tonic-gate offsetof(sctp_ill_t, sctp_ills)); 15527c478bd9Sstevel@tonic-gate } 15537c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) { 15547c478bd9Sstevel@tonic-gate sctp_g_ipifs[i].ipif_count = 0; 15557c478bd9Sstevel@tonic-gate list_create(&sctp_g_ipifs[i].sctp_ipif_list, 15567c478bd9Sstevel@tonic-gate sizeof (sctp_ipif_t), offsetof(sctp_ipif_t, sctp_ipifs)); 15577c478bd9Sstevel@tonic-gate } 15587c478bd9Sstevel@tonic-gate } 15597c478bd9Sstevel@tonic-gate 15607c478bd9Sstevel@tonic-gate void 15617c478bd9Sstevel@tonic-gate sctp_saddr_fini() 15627c478bd9Sstevel@tonic-gate { 15637c478bd9Sstevel@tonic-gate int i; 15647c478bd9Sstevel@tonic-gate 15657c478bd9Sstevel@tonic-gate rw_destroy(&sctp_g_ills_lock); 15667c478bd9Sstevel@tonic-gate rw_destroy(&sctp_g_ipifs_lock); 15677c478bd9Sstevel@tonic-gate ASSERT(sctp_ills_count == 0 && sctp_g_ipifs_count == 0); 15687c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_ILL_HASH; i++) 15697c478bd9Sstevel@tonic-gate list_destroy(&sctp_g_ills[i].sctp_ill_list); 15707c478bd9Sstevel@tonic-gate for (i = 0; i < SCTP_IPIF_HASH; i++) 15717c478bd9Sstevel@tonic-gate list_destroy(&sctp_g_ipifs[i].sctp_ipif_list); 15727c478bd9Sstevel@tonic-gate } 1573