10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23116Skcpoon * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/systm.h> 310Sstevel@tonic-gate #include <sys/stream.h> 320Sstevel@tonic-gate #include <sys/ddi.h> 330Sstevel@tonic-gate #include <sys/sunddi.h> 340Sstevel@tonic-gate #include <sys/kmem.h> 350Sstevel@tonic-gate #include <sys/socket.h> 360Sstevel@tonic-gate #include <sys/random.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <netinet/in.h> 390Sstevel@tonic-gate #include <netinet/ip6.h> 400Sstevel@tonic-gate #include <netinet/sctp.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate #include <inet/common.h> 430Sstevel@tonic-gate #include <inet/ip.h> 440Sstevel@tonic-gate #include <inet/ip6.h> 450Sstevel@tonic-gate #include <inet/ip_ire.h> 460Sstevel@tonic-gate #include <inet/mi.h> 470Sstevel@tonic-gate #include <inet/mib2.h> 480Sstevel@tonic-gate #include <inet/nd.h> 490Sstevel@tonic-gate #include <inet/optcom.h> 500Sstevel@tonic-gate #include <inet/sctp_ip.h> 510Sstevel@tonic-gate #include <inet/ipclassifier.h> 520Sstevel@tonic-gate #include "sctp_impl.h" 530Sstevel@tonic-gate #include "sctp_addr.h" 540Sstevel@tonic-gate 550Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_faddr_cache; 560Sstevel@tonic-gate static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *); 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* Set the source address. Refer to comments in sctp_ire2faddr(). */ 590Sstevel@tonic-gate static void 600Sstevel@tonic-gate set_saddr(sctp_t *sctp, sctp_faddr_t *fp, boolean_t v6) 610Sstevel@tonic-gate { 620Sstevel@tonic-gate if (sctp->sctp_bound_to_all) { 630Sstevel@tonic-gate V6_SET_ZERO(fp->saddr); 640Sstevel@tonic-gate } else { 650Sstevel@tonic-gate fp->saddr = sctp_get_valid_addr(sctp, v6); 660Sstevel@tonic-gate if (!v6 && IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr) || 670Sstevel@tonic-gate v6 && IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) { 680Sstevel@tonic-gate fp->state = SCTP_FADDRS_UNREACH; 690Sstevel@tonic-gate /* Disable heartbeat. */ 700Sstevel@tonic-gate fp->hb_expiry = 0; 710Sstevel@tonic-gate fp->hb_pending = B_FALSE; 720Sstevel@tonic-gate fp->strikes = 0; 730Sstevel@tonic-gate } 740Sstevel@tonic-gate } 750Sstevel@tonic-gate } 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * Call this function to update the cached IRE of a peer addr fp. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate void 810Sstevel@tonic-gate sctp_ire2faddr(sctp_t *sctp, sctp_faddr_t *fp) 820Sstevel@tonic-gate { 830Sstevel@tonic-gate ire_t *ire; 840Sstevel@tonic-gate ipaddr_t addr4; 850Sstevel@tonic-gate in6_addr_t laddr; 860Sstevel@tonic-gate sctp_saddr_ipif_t *sp; 870Sstevel@tonic-gate uint_t ipif_seqid; 880Sstevel@tonic-gate int hdrlen; 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* Remove the previous cache IRE */ 910Sstevel@tonic-gate if ((ire = fp->ire) != NULL) { 920Sstevel@tonic-gate IRE_REFRELE_NOTR(ire); 930Sstevel@tonic-gate fp->ire = NULL; 940Sstevel@tonic-gate } 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * If this addr is not reachable, mark it as unconfirmed for now, the 980Sstevel@tonic-gate * state will be changed back to unreachable later in this function 990Sstevel@tonic-gate * if it is still the case. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_UNREACH) { 1020Sstevel@tonic-gate fp->state = SCTP_FADDRS_UNCONFIRMED; 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if (fp->isv4) { 1060Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate ire = ire_cache_lookup(addr4, sctp->sctp_zoneid); 1090Sstevel@tonic-gate if (ire == NULL) { 1100Sstevel@tonic-gate dprint(3, ("ire2faddr: no ire for %x:%x:%x:%x\n", 1110Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr))); 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * It is tempting to just leave the src addr 1140Sstevel@tonic-gate * unspecified and let IP figure it out, but we 1150Sstevel@tonic-gate * *cannot* do this, since IP may choose a src addr 1160Sstevel@tonic-gate * that is not part of this association... unless 1170Sstevel@tonic-gate * this sctp has bound to all addrs. So if the ire 1180Sstevel@tonic-gate * lookup fails, try to find one in our src addr 1190Sstevel@tonic-gate * list, unless the sctp has bound to all addrs, in 1200Sstevel@tonic-gate * which case we change the src addr to unspec. 1210Sstevel@tonic-gate * 1220Sstevel@tonic-gate * Note that if this is a v6 endpoint but it does 1230Sstevel@tonic-gate * not have any v4 address at this point (e.g. may 1240Sstevel@tonic-gate * have been deleted), sctp_get_valid_addr() will 1250Sstevel@tonic-gate * return mapped INADDR_ANY. In this case, this 1260Sstevel@tonic-gate * address should be marked not reachable so that 1270Sstevel@tonic-gate * it won't be used to send data. 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate set_saddr(sctp, fp, B_FALSE); 1300Sstevel@tonic-gate goto set_current; 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate ipif_seqid = ire->ire_ipif->ipif_seqid; 1330Sstevel@tonic-gate dprint(2, ("ire2faddr: got ire for %x:%x:%x:%x, ", 1340Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr))); 1350Sstevel@tonic-gate dprint(2, ("src = %x\n", ire->ire_src_addr)); 1360Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(ire->ire_src_addr, &laddr); 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* make sure the laddr is part of this association */ 1390Sstevel@tonic-gate if ((sp = sctp_ipif_lookup(sctp, ipif_seqid)) != 1400Sstevel@tonic-gate NULL && !sp->saddr_ipif_dontsrc) { 141432Svi117747 if (sp->saddr_ipif_unconfirmed == 1) 142432Svi117747 sp->saddr_ipif_unconfirmed = 0; 1430Sstevel@tonic-gate fp->saddr = laddr; 1440Sstevel@tonic-gate } else { 1450Sstevel@tonic-gate ip2dbg(("ire2faddr: src addr is not part of assc\n")); 1460Sstevel@tonic-gate set_saddr(sctp, fp, B_FALSE); 1470Sstevel@tonic-gate } 1480Sstevel@tonic-gate } else { 1490Sstevel@tonic-gate ire = ire_cache_lookup_v6(&fp->faddr, sctp->sctp_zoneid); 1500Sstevel@tonic-gate if (ire == NULL) { 1510Sstevel@tonic-gate dprint(3, ("ire2faddr: no ire for %x:%x:%x:%x\n", 1520Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr))); 1530Sstevel@tonic-gate set_saddr(sctp, fp, B_TRUE); 1540Sstevel@tonic-gate goto set_current; 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate ipif_seqid = ire->ire_ipif->ipif_seqid; 1570Sstevel@tonic-gate dprint(2, ("ire2faddr: got ire for %x:%x:%x:%x, ", 1580Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr))); 1590Sstevel@tonic-gate dprint(2, ("src=%x:%x:%x:%x\n", 1600Sstevel@tonic-gate SCTP_PRINTADDR(ire->ire_src_addr_v6))); 1610Sstevel@tonic-gate laddr = ire->ire_src_addr_v6; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* make sure the laddr is part of this association */ 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate if ((sp = sctp_ipif_lookup(sctp, ipif_seqid)) != 1660Sstevel@tonic-gate NULL && !sp->saddr_ipif_dontsrc) { 167432Svi117747 if (sp->saddr_ipif_unconfirmed == 1) 168432Svi117747 sp->saddr_ipif_unconfirmed = 0; 1690Sstevel@tonic-gate fp->saddr = laddr; 1700Sstevel@tonic-gate } else { 1710Sstevel@tonic-gate dprint(2, ("ire2faddr: src addr is not part " 1720Sstevel@tonic-gate "of assc\n")); 1730Sstevel@tonic-gate set_saddr(sctp, fp, B_TRUE); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* Cache the IRE */ 1780Sstevel@tonic-gate IRE_REFHOLD_NOTR(ire); 1790Sstevel@tonic-gate fp->ire = ire; 1800Sstevel@tonic-gate if (fp->ire->ire_type == IRE_LOOPBACK && !sctp->sctp_loopback) 1810Sstevel@tonic-gate sctp->sctp_loopback = 1; 1820Sstevel@tonic-gate IRE_REFRELE(ire); 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * Pull out RTO information for this faddr and use it if we don't 1860Sstevel@tonic-gate * have any yet. 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate if (fp->srtt == -1 && ire->ire_uinfo.iulp_rtt != 0) { 189116Skcpoon /* The cached value is in ms. */ 190116Skcpoon fp->srtt = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt); 191116Skcpoon fp->rttvar = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt_sd); 1920Sstevel@tonic-gate fp->rto = 3 * fp->srtt; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* Bound the RTO by configured min and max values */ 1950Sstevel@tonic-gate if (fp->rto < sctp->sctp_rto_min) { 1960Sstevel@tonic-gate fp->rto = sctp->sctp_rto_min; 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate if (fp->rto > sctp->sctp_rto_max) { 1990Sstevel@tonic-gate fp->rto = sctp->sctp_rto_max; 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* 2040Sstevel@tonic-gate * Record the MTU for this faddr. If the MTU for this faddr has 2050Sstevel@tonic-gate * changed, check if the assc MTU will also change. 2060Sstevel@tonic-gate */ 2070Sstevel@tonic-gate if (fp->isv4) { 2080Sstevel@tonic-gate hdrlen = sctp->sctp_hdr_len; 2090Sstevel@tonic-gate } else { 2100Sstevel@tonic-gate hdrlen = sctp->sctp_hdr6_len; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate if ((fp->sfa_pmss + hdrlen) != ire->ire_max_frag) { 2130Sstevel@tonic-gate /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 2140Sstevel@tonic-gate fp->sfa_pmss = (ire->ire_max_frag - hdrlen) & ~(SCTP_ALIGN - 1); 2150Sstevel@tonic-gate if (fp->cwnd < (fp->sfa_pmss * 2)) { 2160Sstevel@tonic-gate fp->cwnd = fp->sfa_pmss * sctp_slow_start_initial; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate set_current: 2210Sstevel@tonic-gate if (fp == sctp->sctp_current) { 2220Sstevel@tonic-gate sctp_faddr2hdraddr(fp, sctp); 2230Sstevel@tonic-gate sctp->sctp_mss = fp->sfa_pmss; 2240Sstevel@tonic-gate if (!SCTP_IS_DETACHED(sctp)) { 2250Sstevel@tonic-gate sctp_set_ulp_prop(sctp); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /*ARGSUSED*/ 2310Sstevel@tonic-gate void 2320Sstevel@tonic-gate sctp_faddr2ire(sctp_t *sctp, sctp_faddr_t *fp) 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate ire_t *ire; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate if ((ire = fp->ire) == NULL) { 2370Sstevel@tonic-gate return; 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate mutex_enter(&ire->ire_lock); 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate /* If the cached IRE is going sway, there is no point to update it. */ 2430Sstevel@tonic-gate if (ire->ire_marks & IRE_MARK_CONDEMNED) { 2440Sstevel@tonic-gate mutex_exit(&ire->ire_lock); 2450Sstevel@tonic-gate IRE_REFRELE_NOTR(ire); 2460Sstevel@tonic-gate fp->ire = NULL; 2470Sstevel@tonic-gate return; 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* 2510Sstevel@tonic-gate * Only record the PMTU for this faddr if we actually have 2520Sstevel@tonic-gate * done discovery. This prevents initialized default from 2530Sstevel@tonic-gate * clobbering any real info that IP may have. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate if (fp->pmtu_discovered) { 2560Sstevel@tonic-gate if (fp->isv4) { 2570Sstevel@tonic-gate ire->ire_max_frag = fp->sfa_pmss + sctp->sctp_hdr_len; 2580Sstevel@tonic-gate } else { 2590Sstevel@tonic-gate ire->ire_max_frag = fp->sfa_pmss + sctp->sctp_hdr6_len; 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 263116Skcpoon if (sctp_rtt_updates != 0 && fp->rtt_updates >= sctp_rtt_updates) { 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * If there is no old cached values, initialize them 2660Sstevel@tonic-gate * conservatively. Set them to be (1.5 * new value). 267116Skcpoon * This code copied from ip_ire_advise(). The cached 268116Skcpoon * value is in ms. 2690Sstevel@tonic-gate */ 2700Sstevel@tonic-gate if (ire->ire_uinfo.iulp_rtt != 0) { 2710Sstevel@tonic-gate ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt + 272116Skcpoon TICK_TO_MSEC(fp->srtt)) >> 1; 2730Sstevel@tonic-gate } else { 274116Skcpoon ire->ire_uinfo.iulp_rtt = TICK_TO_MSEC(fp->srtt + 275116Skcpoon (fp->srtt >> 1)); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate if (ire->ire_uinfo.iulp_rtt_sd != 0) { 2780Sstevel@tonic-gate ire->ire_uinfo.iulp_rtt_sd = 2790Sstevel@tonic-gate (ire->ire_uinfo.iulp_rtt_sd + 280116Skcpoon TICK_TO_MSEC(fp->rttvar)) >> 1; 2810Sstevel@tonic-gate } else { 282116Skcpoon ire->ire_uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->rttvar + 283116Skcpoon (fp->rttvar >> 1)); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate fp->rtt_updates = 0; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate mutex_exit(&ire->ire_lock); 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate /* 2920Sstevel@tonic-gate * The sender must set the total length in the IP header. 2930Sstevel@tonic-gate * If sendto == NULL, the current will be used. 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate mblk_t * 2960Sstevel@tonic-gate sctp_make_mp(sctp_t *sctp, sctp_faddr_t *sendto, int trailer) 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate mblk_t *mp; 2990Sstevel@tonic-gate size_t ipsctplen; 3000Sstevel@tonic-gate int isv4; 3010Sstevel@tonic-gate sctp_faddr_t *fp; 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate ASSERT(sctp->sctp_current != NULL || sendto != NULL); 3040Sstevel@tonic-gate if (sendto == NULL) { 3050Sstevel@tonic-gate fp = sctp->sctp_current; 3060Sstevel@tonic-gate } else { 3070Sstevel@tonic-gate fp = sendto; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate isv4 = fp->isv4; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate /* Try to look for another IRE again. */ 3120Sstevel@tonic-gate if (fp->ire == NULL) 3130Sstevel@tonic-gate sctp_ire2faddr(sctp, fp); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* There is no suitable source address to use, return. */ 3160Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_UNREACH) 3170Sstevel@tonic-gate return (NULL); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate if (isv4) { 3200Sstevel@tonic-gate ipsctplen = sctp->sctp_hdr_len; 3210Sstevel@tonic-gate } else { 3220Sstevel@tonic-gate ipsctplen = sctp->sctp_hdr6_len; 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate mp = allocb(ipsctplen + sctp_wroff_xtra + trailer, BPRI_MED); 3260Sstevel@tonic-gate if (mp == NULL) { 3270Sstevel@tonic-gate ip1dbg(("sctp_make_mp: error makign mp..\n")); 3280Sstevel@tonic-gate return (NULL); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate mp->b_rptr += sctp_wroff_xtra; 3310Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + ipsctplen; 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate ASSERT(OK_32PTR(mp->b_wptr)); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate if (isv4) { 3360Sstevel@tonic-gate ipha_t *iph = (ipha_t *)mp->b_rptr; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen); 3390Sstevel@tonic-gate if (fp != sctp->sctp_current) { 3400Sstevel@tonic-gate /* fiddle with the dst addr */ 3410Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst); 3420Sstevel@tonic-gate /* fix up src addr */ 3430Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) { 3440Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->saddr, 3450Sstevel@tonic-gate iph->ipha_src); 3460Sstevel@tonic-gate } else if (sctp->sctp_bound_to_all) { 3470Sstevel@tonic-gate iph->ipha_src = INADDR_ANY; 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate /* set or clear the don't fragment bit */ 3510Sstevel@tonic-gate if (fp->df) { 3520Sstevel@tonic-gate iph->ipha_fragment_offset_and_flags = htons(IPH_DF); 3530Sstevel@tonic-gate } else { 3540Sstevel@tonic-gate iph->ipha_fragment_offset_and_flags = 0; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate } else { 3570Sstevel@tonic-gate bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen); 3580Sstevel@tonic-gate if (fp != sctp->sctp_current) { 3590Sstevel@tonic-gate /* fiddle with the dst addr */ 3600Sstevel@tonic-gate ((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr; 3610Sstevel@tonic-gate /* fix up src addr */ 3620Sstevel@tonic-gate if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) { 3630Sstevel@tonic-gate ((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr; 3640Sstevel@tonic-gate } else if (sctp->sctp_bound_to_all) { 3650Sstevel@tonic-gate bzero(&((ip6_t *)(mp->b_rptr))->ip6_src, 3660Sstevel@tonic-gate sizeof (in6_addr_t)); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate ASSERT(sctp->sctp_connp != NULL); 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * IP will not free this IRE if it is condemned. SCTP needs to 3740Sstevel@tonic-gate * free it. 3750Sstevel@tonic-gate */ 3760Sstevel@tonic-gate if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) { 3770Sstevel@tonic-gate IRE_REFRELE_NOTR(fp->ire); 3780Sstevel@tonic-gate fp->ire = NULL; 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate /* Stash the conn and ire ptr info. for IP */ 3810Sstevel@tonic-gate SCTP_STASH_IPINFO(mp, fp->ire); 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate return (mp); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* 3870Sstevel@tonic-gate * Notify upper layers about preferred write offset, write size. 3880Sstevel@tonic-gate */ 3890Sstevel@tonic-gate void 3900Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp) 3910Sstevel@tonic-gate { 3920Sstevel@tonic-gate int hdrlen; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate if (sctp->sctp_current->isv4) { 3950Sstevel@tonic-gate hdrlen = sctp->sctp_hdr_len; 3960Sstevel@tonic-gate } else { 3970Sstevel@tonic-gate hdrlen = sctp->sctp_hdr6_len; 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate ASSERT(sctp->sctp_ulpd); 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss); 4020Sstevel@tonic-gate sctp->sctp_ulp_prop(sctp->sctp_ulpd, 4030Sstevel@tonic-gate sctp_wroff_xtra + hdrlen + sizeof (sctp_data_hdr_t), 4040Sstevel@tonic-gate sctp->sctp_mss - sizeof (sctp_data_hdr_t)); 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate void 4080Sstevel@tonic-gate sctp_set_iplen(sctp_t *sctp, mblk_t *mp) 4090Sstevel@tonic-gate { 4100Sstevel@tonic-gate uint16_t sum = 0; 4110Sstevel@tonic-gate ipha_t *iph; 4120Sstevel@tonic-gate ip6_t *ip6h; 4130Sstevel@tonic-gate mblk_t *pmp = mp; 4140Sstevel@tonic-gate boolean_t isv4; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); 4170Sstevel@tonic-gate for (; pmp; pmp = pmp->b_cont) 4180Sstevel@tonic-gate sum += pmp->b_wptr - pmp->b_rptr; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate if (isv4) { 4210Sstevel@tonic-gate iph = (ipha_t *)mp->b_rptr; 4220Sstevel@tonic-gate iph->ipha_length = htons(sum); 4230Sstevel@tonic-gate } else { 4240Sstevel@tonic-gate ip6h = (ip6_t *)mp->b_rptr; 425679Sseb /* 426679Sseb * If an ip6i_t is present, the real IPv6 header 427679Sseb * immediately follows. 428679Sseb */ 429679Sseb if (ip6h->ip6_nxt == IPPROTO_RAW) 430679Sseb ip6h = (ip6_t *)&ip6h[1]; 4310Sstevel@tonic-gate ip6h->ip6_plen = htons(sum - ((char *)&sctp->sctp_ip6h[1] - 4320Sstevel@tonic-gate sctp->sctp_iphc6)); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate int 4370Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2) 4380Sstevel@tonic-gate { 4390Sstevel@tonic-gate int na1 = 0; 4400Sstevel@tonic-gate int overlap = 0; 4410Sstevel@tonic-gate int equal = 1; 4420Sstevel@tonic-gate int onematch; 4430Sstevel@tonic-gate sctp_faddr_t *fp1, *fp2; 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate for (fp1 = a1; fp1; fp1 = fp1->next) { 4460Sstevel@tonic-gate onematch = 0; 4470Sstevel@tonic-gate for (fp2 = a2; fp2; fp2 = fp2->next) { 4480Sstevel@tonic-gate if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) { 4490Sstevel@tonic-gate overlap++; 4500Sstevel@tonic-gate onematch = 1; 4510Sstevel@tonic-gate break; 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate if (!onematch) { 4540Sstevel@tonic-gate equal = 0; 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate na1++; 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate if (equal) { 4610Sstevel@tonic-gate return (SCTP_ADDR_EQUAL); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate if (overlap == na1) { 4640Sstevel@tonic-gate return (SCTP_ADDR_SUBSET); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate if (overlap) { 4670Sstevel@tonic-gate return (SCTP_ADDR_OVERLAP); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate return (SCTP_ADDR_DISJOINT); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * Returns 0 on success, -1 on memory allocation failure. If sleep 4740Sstevel@tonic-gate * is true, should never fail. 4750Sstevel@tonic-gate * Caller must hold conn fanout lock. 4760Sstevel@tonic-gate */ 4770Sstevel@tonic-gate int 4780Sstevel@tonic-gate sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep) 4790Sstevel@tonic-gate { 4800Sstevel@tonic-gate sctp_faddr_t *faddr; 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate dprint(4, ("add_faddr: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr), 4830Sstevel@tonic-gate sleep)); 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) { 4860Sstevel@tonic-gate return (-1); 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate sctp_init_faddr(sctp, faddr, addr); 4900Sstevel@tonic-gate ASSERT(faddr->next == NULL); 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate /* tack it on to the end */ 4930Sstevel@tonic-gate if (sctp->sctp_lastfaddr != NULL) { 4940Sstevel@tonic-gate sctp->sctp_lastfaddr->next = faddr; 4950Sstevel@tonic-gate } else { 4960Sstevel@tonic-gate /* list is empty */ 4970Sstevel@tonic-gate ASSERT(sctp->sctp_faddrs == NULL); 4980Sstevel@tonic-gate sctp->sctp_faddrs = faddr; 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate sctp->sctp_lastfaddr = faddr; 501*852Svi117747 sctp->sctp_nfaddrs++; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate return (0); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * Caller must hold conn fanout lock. 5080Sstevel@tonic-gate */ 5090Sstevel@tonic-gate int 5100Sstevel@tonic-gate sctp_add_faddr_first(sctp_t *sctp, in6_addr_t *addr, int sleep) 5110Sstevel@tonic-gate { 5120Sstevel@tonic-gate sctp_faddr_t *faddr; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate dprint(4, ("add_faddr_first: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr), 5150Sstevel@tonic-gate sleep)); 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) { 5180Sstevel@tonic-gate return (-1); 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate sctp_init_faddr(sctp, faddr, addr); 5210Sstevel@tonic-gate ASSERT(faddr->next == NULL); 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* Put it at the beginning of the list */ 5240Sstevel@tonic-gate if (sctp->sctp_faddrs != NULL) { 5250Sstevel@tonic-gate faddr->next = sctp->sctp_faddrs; 5260Sstevel@tonic-gate } else { 5270Sstevel@tonic-gate sctp->sctp_lastfaddr = faddr; 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate sctp->sctp_faddrs = faddr; 530*852Svi117747 sctp->sctp_nfaddrs++; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate return (0); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate sctp_faddr_t * 5360Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr) 5370Sstevel@tonic-gate { 5380Sstevel@tonic-gate sctp_faddr_t *fp; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 5410Sstevel@tonic-gate if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) 5420Sstevel@tonic-gate break; 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate return (fp); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate sctp_faddr_t * 5490Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr) 5500Sstevel@tonic-gate { 5510Sstevel@tonic-gate for (; fp; fp = fp->next) { 5520Sstevel@tonic-gate if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) { 5530Sstevel@tonic-gate break; 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate return (fp); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate void 5610Sstevel@tonic-gate sctp_faddr2hdraddr(sctp_faddr_t *fp, sctp_t *sctp) 5620Sstevel@tonic-gate { 5630Sstevel@tonic-gate if (fp->isv4) { 5640Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, 5650Sstevel@tonic-gate sctp->sctp_ipha->ipha_dst); 5660Sstevel@tonic-gate /* Must not allow unspec src addr if not bound to all */ 5670Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr) && 5680Sstevel@tonic-gate !sctp->sctp_bound_to_all) { 5690Sstevel@tonic-gate /* 5700Sstevel@tonic-gate * set the src to the first v4 saddr and hope 5710Sstevel@tonic-gate * for the best 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate fp->saddr = sctp_get_valid_addr(sctp, B_FALSE); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src); 5760Sstevel@tonic-gate /* update don't fragment bit */ 5770Sstevel@tonic-gate if (fp->df) { 5780Sstevel@tonic-gate sctp->sctp_ipha->ipha_fragment_offset_and_flags = 5790Sstevel@tonic-gate htons(IPH_DF); 5800Sstevel@tonic-gate } else { 5810Sstevel@tonic-gate sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0; 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate } else { 5840Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = fp->faddr; 5850Sstevel@tonic-gate /* Must not allow unspec src addr if not bound to all */ 5860Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(&fp->saddr) && 5870Sstevel@tonic-gate !sctp->sctp_bound_to_all) { 5880Sstevel@tonic-gate /* 5890Sstevel@tonic-gate * set the src to the first v6 saddr and hope 5900Sstevel@tonic-gate * for the best 5910Sstevel@tonic-gate */ 5920Sstevel@tonic-gate fp->saddr = sctp_get_valid_addr(sctp, B_TRUE); 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = fp->saddr; 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate void 5990Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp) 6000Sstevel@tonic-gate { 6010Sstevel@tonic-gate sctp_faddr_t *fp; 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 6040Sstevel@tonic-gate sctp_ire2faddr(sctp, fp); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate sctp_faddr2hdraddr(sctp->sctp_current, sctp); 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate void 6110Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp) 6120Sstevel@tonic-gate { 6130Sstevel@tonic-gate int64_t now = lbolt64; 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate fp->strikes = 0; 6160Sstevel@tonic-gate sctp->sctp_strikes = 0; 6170Sstevel@tonic-gate fp->lastactive = now; 6180Sstevel@tonic-gate fp->hb_expiry = now + SET_HB_INTVL(fp); 6190Sstevel@tonic-gate fp->hb_pending = B_FALSE; 6200Sstevel@tonic-gate if (fp->state != SCTP_FADDRS_ALIVE) { 6210Sstevel@tonic-gate fp->state = SCTP_FADDRS_ALIVE; 6220Sstevel@tonic-gate sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0); 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* If this is the primary, switch back to it now */ 6250Sstevel@tonic-gate if (fp == sctp->sctp_primary) { 6260Sstevel@tonic-gate sctp->sctp_current = fp; 6270Sstevel@tonic-gate sctp->sctp_mss = fp->sfa_pmss; 6280Sstevel@tonic-gate /* Reset the addrs in the composite header */ 6290Sstevel@tonic-gate sctp_faddr2hdraddr(fp, sctp); 6300Sstevel@tonic-gate if (!SCTP_IS_DETACHED(sctp)) { 6310Sstevel@tonic-gate sctp_set_ulp_prop(sctp); 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate if (fp->ire == NULL) { 6360Sstevel@tonic-gate /* Should have a full IRE now */ 6370Sstevel@tonic-gate sctp_ire2faddr(sctp, fp); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate int 6420Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp) 6430Sstevel@tonic-gate { 6440Sstevel@tonic-gate sctp_faddr_t *fp; 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 6470Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) { 6480Sstevel@tonic-gate return (1); 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate return (0); 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate /* 6560Sstevel@tonic-gate * Returns 0 if there is at leave one other active faddr, -1 if there 6570Sstevel@tonic-gate * are none. If there are none left, faddr_dead() will start killing the 6580Sstevel@tonic-gate * association. 6590Sstevel@tonic-gate * If the downed faddr was the current faddr, a new current faddr 6600Sstevel@tonic-gate * will be chosen. 6610Sstevel@tonic-gate */ 6620Sstevel@tonic-gate int 6630Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate) 6640Sstevel@tonic-gate { 6650Sstevel@tonic-gate sctp_faddr_t *ofp; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_ALIVE) { 6680Sstevel@tonic-gate sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0); 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate fp->state = newstate; 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n", 6730Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr), newstate)); 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate if (fp == sctp->sctp_current) { 6760Sstevel@tonic-gate /* Current faddr down; need to switch it */ 6770Sstevel@tonic-gate sctp->sctp_current = NULL; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* Find next alive faddr */ 6810Sstevel@tonic-gate ofp = fp; 6820Sstevel@tonic-gate for (fp = fp->next; fp; fp = fp->next) { 6830Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_ALIVE) { 6840Sstevel@tonic-gate break; 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate if (fp == NULL) { 6890Sstevel@tonic-gate /* Continue from beginning of list */ 6900Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) { 6910Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_ALIVE) { 6920Sstevel@tonic-gate break; 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate if (fp != ofp) { 6980Sstevel@tonic-gate if (sctp->sctp_current == NULL) { 6990Sstevel@tonic-gate dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n", 7000Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr))); 7010Sstevel@tonic-gate sctp->sctp_current = fp; 7020Sstevel@tonic-gate sctp->sctp_mss = fp->sfa_pmss; 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* Reset the addrs in the composite header */ 7050Sstevel@tonic-gate sctp_faddr2hdraddr(fp, sctp); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate if (!SCTP_IS_DETACHED(sctp)) { 7080Sstevel@tonic-gate sctp_set_ulp_prop(sctp); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate return (0); 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate /* All faddrs are down; kill the association */ 7160Sstevel@tonic-gate dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n")); 7170Sstevel@tonic-gate BUMP_MIB(&sctp_mib, sctpAborted); 7180Sstevel@tonic-gate sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ? 7190Sstevel@tonic-gate SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL); 7200Sstevel@tonic-gate sctp_clean_death(sctp, sctp->sctp_client_errno ? 7210Sstevel@tonic-gate sctp->sctp_client_errno : ETIMEDOUT); 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate return (-1); 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate sctp_faddr_t * 7270Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp) 7280Sstevel@tonic-gate { 7290Sstevel@tonic-gate sctp_faddr_t *nfp = NULL; 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate if (ofp == NULL) { 7320Sstevel@tonic-gate ofp = sctp->sctp_current; 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /* Find the next live one */ 7360Sstevel@tonic-gate for (nfp = ofp->next; nfp != NULL; nfp = nfp->next) { 7370Sstevel@tonic-gate if (nfp->state == SCTP_FADDRS_ALIVE) { 7380Sstevel@tonic-gate break; 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate if (nfp == NULL) { 7430Sstevel@tonic-gate /* Continue from beginning of list */ 7440Sstevel@tonic-gate for (nfp = sctp->sctp_faddrs; nfp != ofp; nfp = nfp->next) { 7450Sstevel@tonic-gate if (nfp->state == SCTP_FADDRS_ALIVE) { 7460Sstevel@tonic-gate break; 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate /* 7520Sstevel@tonic-gate * nfp could only be NULL if all faddrs are down, and when 7530Sstevel@tonic-gate * this happens, faddr_dead() should have killed the 7540Sstevel@tonic-gate * association. Hence this assertion... 7550Sstevel@tonic-gate */ 7560Sstevel@tonic-gate ASSERT(nfp != NULL); 7570Sstevel@tonic-gate return (nfp); 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate void 7610Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp) 7620Sstevel@tonic-gate { 7630Sstevel@tonic-gate sctp_faddr_t *fpp; 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate if (!sctp->sctp_faddrs) { 7660Sstevel@tonic-gate return; 7670Sstevel@tonic-gate } 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate if (fp->timer_mp != NULL) { 7700Sstevel@tonic-gate sctp_timer_free(fp->timer_mp); 7710Sstevel@tonic-gate fp->timer_mp = NULL; 7720Sstevel@tonic-gate fp->timer_running = 0; 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate if (fp->rc_timer_mp != NULL) { 7750Sstevel@tonic-gate sctp_timer_free(fp->rc_timer_mp); 7760Sstevel@tonic-gate fp->rc_timer_mp = NULL; 7770Sstevel@tonic-gate fp->rc_timer_running = 0; 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate if (fp->ire != NULL) { 7800Sstevel@tonic-gate IRE_REFRELE_NOTR(fp->ire); 7810Sstevel@tonic-gate fp->ire = NULL; 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate if (fp == sctp->sctp_faddrs) { 7850Sstevel@tonic-gate goto gotit; 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next) 7890Sstevel@tonic-gate ; 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate gotit: 7920Sstevel@tonic-gate ASSERT(sctp->sctp_conn_tfp != NULL); 7930Sstevel@tonic-gate mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 7940Sstevel@tonic-gate if (fp == sctp->sctp_faddrs) { 7950Sstevel@tonic-gate sctp->sctp_faddrs = fp->next; 7960Sstevel@tonic-gate } else { 7970Sstevel@tonic-gate fpp->next = fp->next; 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 8000Sstevel@tonic-gate /* XXX faddr2ire? */ 8010Sstevel@tonic-gate kmem_cache_free(sctp_kmem_faddr_cache, fp); 802*852Svi117747 sctp->sctp_nfaddrs--; 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate void 8060Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock) 8070Sstevel@tonic-gate { 8080Sstevel@tonic-gate sctp_faddr_t *fp, *fpn; 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate if (sctp->sctp_faddrs == NULL) { 8110Sstevel@tonic-gate ASSERT(sctp->sctp_lastfaddr == NULL); 8120Sstevel@tonic-gate return; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate ASSERT(sctp->sctp_lastfaddr != NULL); 8160Sstevel@tonic-gate sctp->sctp_lastfaddr = NULL; 8170Sstevel@tonic-gate sctp->sctp_current = NULL; 8180Sstevel@tonic-gate sctp->sctp_primary = NULL; 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate sctp_free_faddr_timers(sctp); 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 8230Sstevel@tonic-gate /* in conn fanout; need to hold lock */ 8240Sstevel@tonic-gate mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fpn) { 8280Sstevel@tonic-gate fpn = fp->next; 8290Sstevel@tonic-gate if (fp->ire != NULL) 8300Sstevel@tonic-gate IRE_REFRELE_NOTR(fp->ire); 8310Sstevel@tonic-gate kmem_cache_free(sctp_kmem_faddr_cache, fp); 832*852Svi117747 sctp->sctp_nfaddrs--; 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate sctp->sctp_faddrs = NULL; 836*852Svi117747 ASSERT(sctp->sctp_nfaddrs == 0); 8370Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 8380Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate void 8440Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp) 8450Sstevel@tonic-gate { 8460Sstevel@tonic-gate sctp_zap_faddrs(sctp, 0); 8470Sstevel@tonic-gate sctp_free_saddrs(sctp); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * Initialize the IPv4 header. Loses any record of any IP options. 8520Sstevel@tonic-gate */ 8530Sstevel@tonic-gate int 8540Sstevel@tonic-gate sctp_header_init_ipv4(sctp_t *sctp, int sleep) 8550Sstevel@tonic-gate { 8560Sstevel@tonic-gate sctp_hdr_t *sctph; 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* 8590Sstevel@tonic-gate * This is a simple initialization. If there's 8600Sstevel@tonic-gate * already a template, it should never be too small, 8610Sstevel@tonic-gate * so reuse it. Otherwise, allocate space for the new one. 8620Sstevel@tonic-gate */ 8630Sstevel@tonic-gate if (sctp->sctp_iphc != NULL) { 8640Sstevel@tonic-gate ASSERT(sctp->sctp_iphc_len >= SCTP_MAX_COMBINED_HEADER_LENGTH); 8650Sstevel@tonic-gate bzero(sctp->sctp_iphc, sctp->sctp_iphc_len); 8660Sstevel@tonic-gate } else { 8670Sstevel@tonic-gate sctp->sctp_iphc_len = SCTP_MAX_COMBINED_HEADER_LENGTH; 8680Sstevel@tonic-gate sctp->sctp_iphc = kmem_zalloc(sctp->sctp_iphc_len, sleep); 8690Sstevel@tonic-gate if (sctp->sctp_iphc == NULL) { 8700Sstevel@tonic-gate sctp->sctp_iphc_len = 0; 8710Sstevel@tonic-gate return (ENOMEM); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate } 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc; 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate sctp->sctp_hdr_len = sizeof (ipha_t) + sizeof (sctp_hdr_t); 8780Sstevel@tonic-gate sctp->sctp_ip_hdr_len = sizeof (ipha_t); 8790Sstevel@tonic-gate sctp->sctp_ipha->ipha_length = htons(sizeof (ipha_t) + 8800Sstevel@tonic-gate sizeof (sctp_hdr_t)); 8810Sstevel@tonic-gate sctp->sctp_ipha->ipha_version_and_hdr_length 8820Sstevel@tonic-gate = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * These two fields should be zero, and are already set above. 8860Sstevel@tonic-gate * 8870Sstevel@tonic-gate * sctp->sctp_ipha->ipha_ident, 8880Sstevel@tonic-gate * sctp->sctp_ipha->ipha_fragment_offset_and_flags. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate sctp->sctp_ipha->ipha_ttl = sctp_ipv4_ttl; 8920Sstevel@tonic-gate sctp->sctp_ipha->ipha_protocol = IPPROTO_SCTP; 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate sctph = (sctp_hdr_t *)(sctp->sctp_iphc + sizeof (ipha_t)); 8950Sstevel@tonic-gate sctp->sctp_sctph = sctph; 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate return (0); 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate /* 9010Sstevel@tonic-gate * Update sctp_sticky_hdrs based on sctp_sticky_ipp. 9020Sstevel@tonic-gate * The headers include ip6i_t (if needed), ip6_t, any sticky extension 9030Sstevel@tonic-gate * headers, and the maximum size sctp header (to avoid reallocation 9040Sstevel@tonic-gate * on the fly for additional sctp options). 9050Sstevel@tonic-gate * Returns failure if can't allocate memory. 9060Sstevel@tonic-gate */ 9070Sstevel@tonic-gate int 9080Sstevel@tonic-gate sctp_build_hdrs(sctp_t *sctp) 9090Sstevel@tonic-gate { 9100Sstevel@tonic-gate char *hdrs; 9110Sstevel@tonic-gate uint_t hdrs_len; 9120Sstevel@tonic-gate ip6i_t *ip6i; 9130Sstevel@tonic-gate char buf[SCTP_MAX_HDR_LENGTH]; 9140Sstevel@tonic-gate ip6_pkt_t *ipp = &sctp->sctp_sticky_ipp; 9150Sstevel@tonic-gate in6_addr_t src; 9160Sstevel@tonic-gate in6_addr_t dst; 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * save the existing sctp header and source/dest IP addresses 9190Sstevel@tonic-gate */ 9200Sstevel@tonic-gate bcopy(sctp->sctp_sctph6, buf, sizeof (sctp_hdr_t)); 9210Sstevel@tonic-gate src = sctp->sctp_ip6h->ip6_src; 9220Sstevel@tonic-gate dst = sctp->sctp_ip6h->ip6_dst; 9230Sstevel@tonic-gate hdrs_len = ip_total_hdrs_len_v6(ipp) + SCTP_MAX_HDR_LENGTH; 9240Sstevel@tonic-gate ASSERT(hdrs_len != 0); 9250Sstevel@tonic-gate if (hdrs_len > sctp->sctp_iphc6_len) { 9260Sstevel@tonic-gate /* Need to reallocate */ 9270Sstevel@tonic-gate hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 9280Sstevel@tonic-gate if (hdrs == NULL) 9290Sstevel@tonic-gate return (ENOMEM); 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate if (sctp->sctp_iphc6_len != 0) 9320Sstevel@tonic-gate kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len); 9330Sstevel@tonic-gate sctp->sctp_iphc6 = hdrs; 9340Sstevel@tonic-gate sctp->sctp_iphc6_len = hdrs_len; 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate ip_build_hdrs_v6((uchar_t *)sctp->sctp_iphc6, 9370Sstevel@tonic-gate hdrs_len - SCTP_MAX_HDR_LENGTH, ipp, IPPROTO_SCTP); 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate /* Set header fields not in ipp */ 9400Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_HAS_IP6I) { 9410Sstevel@tonic-gate ip6i = (ip6i_t *)sctp->sctp_iphc6; 9420Sstevel@tonic-gate sctp->sctp_ip6h = (ip6_t *)&ip6i[1]; 9430Sstevel@tonic-gate } else { 9440Sstevel@tonic-gate sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6; 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate /* 9470Sstevel@tonic-gate * sctp->sctp_ip_hdr_len will include ip6i_t if there is one. 9480Sstevel@tonic-gate */ 9490Sstevel@tonic-gate sctp->sctp_ip_hdr6_len = hdrs_len - SCTP_MAX_HDR_LENGTH; 9500Sstevel@tonic-gate sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 + 9510Sstevel@tonic-gate sctp->sctp_ip_hdr6_len); 9520Sstevel@tonic-gate sctp->sctp_hdr6_len = sctp->sctp_ip_hdr6_len + sizeof (sctp_hdr_t); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate bcopy(buf, sctp->sctp_sctph6, sizeof (sctp_hdr_t)); 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = src; 9570Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = dst; 9580Sstevel@tonic-gate /* 959679Sseb * If the hoplimit was not set by ip_build_hdrs_v6(), we need to 960679Sseb * set it to the default value for SCTP. 9610Sstevel@tonic-gate */ 962679Sseb if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 963679Sseb sctp->sctp_ip6h->ip6_hops = sctp_ipv6_hoplimit; 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * If we're setting extension headers after a connection 9660Sstevel@tonic-gate * has been established, and if we have a routing header 9670Sstevel@tonic-gate * among the extension headers, call ip_massage_options_v6 to 9680Sstevel@tonic-gate * manipulate the routing header/ip6_dst set the checksum 9690Sstevel@tonic-gate * difference in the sctp header template. 9700Sstevel@tonic-gate * (This happens in sctp_connect_ipv6 if the routing header 9710Sstevel@tonic-gate * is set prior to the connect.) 9720Sstevel@tonic-gate */ 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) && 9750Sstevel@tonic-gate (sctp->sctp_sticky_ipp.ipp_fields & IPPF_RTHDR)) { 9760Sstevel@tonic-gate ip6_rthdr_t *rth; 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate rth = ip_find_rthdr_v6(sctp->sctp_ip6h, 9790Sstevel@tonic-gate (uint8_t *)sctp->sctp_sctph6); 9800Sstevel@tonic-gate if (rth != NULL) 9810Sstevel@tonic-gate (void) ip_massage_options_v6(sctp->sctp_ip6h, rth); 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate return (0); 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate /* 9870Sstevel@tonic-gate * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 9880Sstevel@tonic-gate */ 9890Sstevel@tonic-gate int 9900Sstevel@tonic-gate sctp_header_init_ipv6(sctp_t *sctp, int sleep) 9910Sstevel@tonic-gate { 9920Sstevel@tonic-gate sctp_hdr_t *sctph; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * This is a simple initialization. If there's 9960Sstevel@tonic-gate * already a template, it should never be too small, 9970Sstevel@tonic-gate * so reuse it. Otherwise, allocate space for the new one. 9980Sstevel@tonic-gate * Ensure that there is enough space to "downgrade" the sctp_t 9990Sstevel@tonic-gate * to an IPv4 sctp_t. This requires having space for a full load 10000Sstevel@tonic-gate * of IPv4 options 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate if (sctp->sctp_iphc6 != NULL) { 10030Sstevel@tonic-gate ASSERT(sctp->sctp_iphc6_len >= 10040Sstevel@tonic-gate SCTP_MAX_COMBINED_HEADER_LENGTH); 10050Sstevel@tonic-gate bzero(sctp->sctp_iphc6, sctp->sctp_iphc6_len); 10060Sstevel@tonic-gate } else { 10070Sstevel@tonic-gate sctp->sctp_iphc6_len = SCTP_MAX_COMBINED_HEADER_LENGTH; 10080Sstevel@tonic-gate sctp->sctp_iphc6 = kmem_zalloc(sctp->sctp_iphc_len, sleep); 10090Sstevel@tonic-gate if (sctp->sctp_iphc6 == NULL) { 10100Sstevel@tonic-gate sctp->sctp_iphc6_len = 0; 10110Sstevel@tonic-gate return (ENOMEM); 10120Sstevel@tonic-gate } 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate sctp->sctp_hdr6_len = IPV6_HDR_LEN + sizeof (sctp_hdr_t); 10150Sstevel@tonic-gate sctp->sctp_ip_hdr6_len = IPV6_HDR_LEN; 10160Sstevel@tonic-gate sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6; 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate /* Initialize the header template */ 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate sctp->sctp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 10210Sstevel@tonic-gate sctp->sctp_ip6h->ip6_plen = ntohs(sizeof (sctp_hdr_t)); 10220Sstevel@tonic-gate sctp->sctp_ip6h->ip6_nxt = IPPROTO_SCTP; 10230Sstevel@tonic-gate sctp->sctp_ip6h->ip6_hops = sctp_ipv6_hoplimit; 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate sctph = (sctp_hdr_t *)(sctp->sctp_iphc6 + IPV6_HDR_LEN); 10260Sstevel@tonic-gate sctp->sctp_sctph6 = sctph; 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate return (0); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate /* 10320Sstevel@tonic-gate * XXX implement more sophisticated logic 10330Sstevel@tonic-gate */ 10340Sstevel@tonic-gate void 10350Sstevel@tonic-gate sctp_set_hdraddrs(sctp_t *sctp) 10360Sstevel@tonic-gate { 10370Sstevel@tonic-gate sctp_faddr_t *fp; 10380Sstevel@tonic-gate int gotv4 = 0; 10390Sstevel@tonic-gate int gotv6 = 0; 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate ASSERT(sctp->sctp_faddrs != NULL); 10420Sstevel@tonic-gate ASSERT(sctp->sctp_nsaddrs > 0); 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate /* Set up using the primary first */ 10450Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) { 10460Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->faddr, 10470Sstevel@tonic-gate sctp->sctp_ipha->ipha_dst); 10480Sstevel@tonic-gate /* saddr may be unspec; make_mp() will handle this */ 10490Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->saddr, 10500Sstevel@tonic-gate sctp->sctp_ipha->ipha_src); 10510Sstevel@tonic-gate gotv4 = 1; 10520Sstevel@tonic-gate if (sctp->sctp_ipversion == IPV4_VERSION) { 10530Sstevel@tonic-gate goto copyports; 10540Sstevel@tonic-gate } 10550Sstevel@tonic-gate } else { 10560Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = sctp->sctp_primary->faddr; 10570Sstevel@tonic-gate /* saddr may be unspec; make_mp() will handle this */ 10580Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = sctp->sctp_primary->saddr; 10590Sstevel@tonic-gate gotv6 = 1; 10600Sstevel@tonic-gate } 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 10630Sstevel@tonic-gate if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 10640Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, 10650Sstevel@tonic-gate sctp->sctp_ipha->ipha_dst); 10660Sstevel@tonic-gate /* copy in the faddr_t's saddr */ 10670Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->saddr, 10680Sstevel@tonic-gate sctp->sctp_ipha->ipha_src); 10690Sstevel@tonic-gate gotv4 = 1; 10700Sstevel@tonic-gate if (sctp->sctp_ipversion == IPV4_VERSION || gotv6) { 10710Sstevel@tonic-gate break; 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate } else if (!gotv6) { 10740Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = fp->faddr; 10750Sstevel@tonic-gate /* copy in the faddr_t's saddr */ 10760Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = fp->saddr; 10770Sstevel@tonic-gate gotv6 = 1; 10780Sstevel@tonic-gate if (gotv4) { 10790Sstevel@tonic-gate break; 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate } 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate copyports: 10850Sstevel@tonic-gate /* copy in the ports for good measure */ 10860Sstevel@tonic-gate sctp->sctp_sctph->sh_sport = sctp->sctp_lport; 10870Sstevel@tonic-gate sctp->sctp_sctph->sh_dport = sctp->sctp_fport; 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate sctp->sctp_sctph6->sh_sport = sctp->sctp_lport; 10900Sstevel@tonic-gate sctp->sctp_sctph6->sh_dport = sctp->sctp_fport; 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate void 10940Sstevel@tonic-gate sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp) 10950Sstevel@tonic-gate { 10960Sstevel@tonic-gate mblk_t *mp; 10970Sstevel@tonic-gate sctp_parm_hdr_t *ph; 10980Sstevel@tonic-gate size_t len; 10990Sstevel@tonic-gate int pad; 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate len = sizeof (*ph) + ntohs(uph->sph_len); 11020Sstevel@tonic-gate if ((pad = len % 4) != 0) { 11030Sstevel@tonic-gate pad = 4 - pad; 11040Sstevel@tonic-gate len += pad; 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate mp = allocb(len, BPRI_MED); 11070Sstevel@tonic-gate if (mp == NULL) { 11080Sstevel@tonic-gate return; 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate ph = (sctp_parm_hdr_t *)(mp->b_rptr); 11120Sstevel@tonic-gate ph->sph_type = htons(PARM_UNRECOGNIZED); 11130Sstevel@tonic-gate ph->sph_len = htons(len - pad); 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate /* copy in the unrecognized parameter */ 11160Sstevel@tonic-gate bcopy(uph, ph + 1, ntohs(uph->sph_len)); 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + len; 11190Sstevel@tonic-gate if (*errmp != NULL) { 11200Sstevel@tonic-gate linkb(*errmp, mp); 11210Sstevel@tonic-gate } else { 11220Sstevel@tonic-gate *errmp = mp; 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate 11260Sstevel@tonic-gate /* 11270Sstevel@tonic-gate * o Bounds checking 11280Sstevel@tonic-gate * o Updates remaining 11290Sstevel@tonic-gate * o Checks alignment 11300Sstevel@tonic-gate */ 11310Sstevel@tonic-gate sctp_parm_hdr_t * 11320Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining) 11330Sstevel@tonic-gate { 11340Sstevel@tonic-gate int pad; 11350Sstevel@tonic-gate uint16_t len; 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate len = ntohs(current->sph_len); 11380Sstevel@tonic-gate *remaining -= len; 11390Sstevel@tonic-gate if (*remaining < sizeof (*current) || len < sizeof (*current)) { 11400Sstevel@tonic-gate return (NULL); 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate if ((pad = len & (SCTP_ALIGN - 1)) != 0) { 11430Sstevel@tonic-gate pad = SCTP_ALIGN - pad; 11440Sstevel@tonic-gate *remaining -= pad; 11450Sstevel@tonic-gate } 11460Sstevel@tonic-gate /*LINTED pointer cast may result in improper alignment*/ 11470Sstevel@tonic-gate current = (sctp_parm_hdr_t *)((char *)current + len + pad); 11480Sstevel@tonic-gate return (current); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate /* 11520Sstevel@tonic-gate * Sets the address parameters given in the INIT chunk into sctp's 11530Sstevel@tonic-gate * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are 11540Sstevel@tonic-gate * no address parameters in the INIT chunk, a single faddr is created 11550Sstevel@tonic-gate * from the ip hdr at the beginning of pkt. 11560Sstevel@tonic-gate * If there already are existing addresses hanging from sctp, merge 11570Sstevel@tonic-gate * them in, if the old info contains addresses which are not present 11580Sstevel@tonic-gate * in this new info, get rid of them, and clean the pointers if there's 11590Sstevel@tonic-gate * messages which have this as their target address. 11600Sstevel@tonic-gate * 1161432Svi117747 * We also re-adjust the source address list here since the list may 1162432Svi117747 * contain more than what is actually part of the association. If 1163432Svi117747 * we get here from sctp_send_cookie_echo(), we are on the active 1164432Svi117747 * side and psctp will be NULL and ich will be the INIT-ACK chunk. 1165432Svi117747 * If we get here from sctp_accept_comm(), ich will be the INIT chunk 1166432Svi117747 * and psctp will the listening endpoint. 1167432Svi117747 * 1168432Svi117747 * INIT processing: When processing the INIT we inherit the src address 1169432Svi117747 * list from the listener. For a loopback or linklocal association, we 1170432Svi117747 * delete the list and just take the address from the IP header (since 1171432Svi117747 * that's how we created the INIT-ACK). Additionally, for loopback we 1172432Svi117747 * ignore the address params in the INIT. For determining which address 1173432Svi117747 * types were sent in the INIT-ACK we follow the same logic as in 1174432Svi117747 * creating the INIT-ACK. We delete addresses of the type that are not 1175432Svi117747 * supported by the peer. 1176432Svi117747 * 1177432Svi117747 * INIT-ACK processing: When processing the INIT-ACK since we had not 1178432Svi117747 * included addr params for loopback or linklocal addresses when creating 1179432Svi117747 * the INIT, we just use the address from the IP header. Further, for 1180432Svi117747 * loopback we ignore the addr param list. We mark addresses of the 1181432Svi117747 * type not supported by the peer as unconfirmed. 1182432Svi117747 * 1183432Svi117747 * In case of INIT processing we look for supported address types in the 1184432Svi117747 * supported address param, if present. In both cases the address type in 1185432Svi117747 * the IP header is supported as well as types for addresses in the param 1186432Svi117747 * list, if any. 1187432Svi117747 * 1188432Svi117747 * Once we have the supported address types sctp_check_saddr() runs through 1189432Svi117747 * the source address list and deletes or marks as unconfirmed address of 1190432Svi117747 * types not supported by the peer. 1191432Svi117747 * 11920Sstevel@tonic-gate * Returns 0 on success, sys errno on failure 11930Sstevel@tonic-gate */ 11940Sstevel@tonic-gate int 11950Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt, 11960Sstevel@tonic-gate sctp_chunk_hdr_t *ich, uint_t *sctp_options) 11970Sstevel@tonic-gate { 11980Sstevel@tonic-gate sctp_init_chunk_t *init; 11990Sstevel@tonic-gate ipha_t *iph; 12000Sstevel@tonic-gate ip6_t *ip6h; 1201432Svi117747 in6_addr_t hdrsaddr[1]; 1202432Svi117747 in6_addr_t hdrdaddr[1]; 12030Sstevel@tonic-gate sctp_parm_hdr_t *ph; 12040Sstevel@tonic-gate ssize_t remaining; 12050Sstevel@tonic-gate int isv4; 12060Sstevel@tonic-gate int err; 12070Sstevel@tonic-gate sctp_faddr_t *fp; 1208432Svi117747 int supp_af = 0; 1209432Svi117747 boolean_t check_saddr = B_TRUE; 1210*852Svi117747 in6_addr_t curaddr; 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate if (sctp_options != NULL) 12130Sstevel@tonic-gate *sctp_options = 0; 12140Sstevel@tonic-gate 1215432Svi117747 /* extract the address from the IP header */ 1216432Svi117747 isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 1217432Svi117747 if (isv4) { 1218432Svi117747 iph = (ipha_t *)pkt->b_rptr; 1219432Svi117747 IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr); 1220432Svi117747 IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr); 1221432Svi117747 supp_af |= PARM_SUPP_V4; 1222432Svi117747 } else { 1223432Svi117747 ip6h = (ip6_t *)pkt->b_rptr; 1224432Svi117747 hdrsaddr[0] = ip6h->ip6_src; 1225432Svi117747 hdrdaddr[0] = ip6h->ip6_dst; 1226432Svi117747 supp_af |= PARM_SUPP_V6; 1227432Svi117747 } 1228432Svi117747 1229432Svi117747 /* 1230432Svi117747 * Unfortunately, we can't delay this because adding an faddr 1231432Svi117747 * looks for the presence of the source address (from the ire 1232432Svi117747 * for the faddr) in the source address list. We could have 1233432Svi117747 * delayed this if, say, this was a loopback/linklocal connection. 1234432Svi117747 * Now, we just end up nuking this list and taking the addr from 1235432Svi117747 * the IP header for loopback/linklocal. 1236432Svi117747 */ 12370Sstevel@tonic-gate if (psctp != NULL && psctp->sctp_nsaddrs > 0) { 12380Sstevel@tonic-gate ASSERT(sctp->sctp_nsaddrs == 0); 12390Sstevel@tonic-gate 12400Sstevel@tonic-gate err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP); 12410Sstevel@tonic-gate if (err != 0) 12420Sstevel@tonic-gate return (err); 12430Sstevel@tonic-gate } 1244432Svi117747 /* 1245432Svi117747 * We will add the faddr before parsing the address list as this 1246432Svi117747 * might be a loopback connection and we would not have to 1247432Svi117747 * go through the list. 1248432Svi117747 * 1249432Svi117747 * Make sure the header's addr is in the list 1250432Svi117747 */ 1251432Svi117747 fp = sctp_lookup_faddr(sctp, hdrsaddr); 1252432Svi117747 if (fp == NULL) { 1253432Svi117747 /* not included; add it now */ 1254432Svi117747 if (sctp_add_faddr_first(sctp, hdrsaddr, KM_NOSLEEP) == -1) 1255432Svi117747 return (ENOMEM); 12560Sstevel@tonic-gate 1257432Svi117747 /* sctp_faddrs will be the hdr addr */ 1258432Svi117747 fp = sctp->sctp_faddrs; 12590Sstevel@tonic-gate } 1260432Svi117747 /* make the header addr the primary */ 1261*852Svi117747 1262*852Svi117747 if (cl_sctp_assoc_change != NULL && psctp == NULL) 1263*852Svi117747 curaddr = sctp->sctp_current->faddr; 1264*852Svi117747 1265432Svi117747 sctp->sctp_primary = fp; 1266432Svi117747 sctp->sctp_current = fp; 1267432Svi117747 sctp->sctp_mss = fp->sfa_pmss; 12680Sstevel@tonic-gate 1269432Svi117747 /* For loopback connections & linklocal get address from the header */ 1270432Svi117747 if (sctp->sctp_loopback || sctp->sctp_linklocal) { 1271432Svi117747 if (sctp->sctp_nsaddrs != 0) 1272432Svi117747 sctp_free_saddrs(sctp); 1273*852Svi117747 if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0) 1274432Svi117747 return (err); 1275432Svi117747 /* For loopback ignore address list */ 1276432Svi117747 if (sctp->sctp_loopback) 1277432Svi117747 return (0); 1278432Svi117747 check_saddr = B_FALSE; 1279432Svi117747 } 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate /* Walk the params in the INIT [ACK], pulling out addr params */ 12820Sstevel@tonic-gate remaining = ntohs(ich->sch_len) - sizeof (*ich) - 12830Sstevel@tonic-gate sizeof (sctp_init_chunk_t); 12840Sstevel@tonic-gate if (remaining < sizeof (*ph)) { 1285432Svi117747 if (check_saddr) { 1286432Svi117747 sctp_check_saddr(sctp, supp_af, psctp == NULL ? 1287432Svi117747 B_FALSE : B_TRUE); 1288432Svi117747 } 1289*852Svi117747 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL); 1290432Svi117747 return (0); 12910Sstevel@tonic-gate } 1292432Svi117747 12930Sstevel@tonic-gate init = (sctp_init_chunk_t *)(ich + 1); 12940Sstevel@tonic-gate ph = (sctp_parm_hdr_t *)(init + 1); 12950Sstevel@tonic-gate 1296432Svi117747 /* params will have already been byteordered when validating */ 12970Sstevel@tonic-gate while (ph != NULL) { 1298432Svi117747 if (ph->sph_type == htons(PARM_SUPP_ADDRS)) { 1299432Svi117747 int plen; 1300432Svi117747 uint16_t *p; 1301432Svi117747 uint16_t addrtype; 1302432Svi117747 1303432Svi117747 ASSERT(psctp != NULL); 1304432Svi117747 plen = ntohs(ph->sph_len); 1305432Svi117747 p = (uint16_t *)(ph + 1); 1306432Svi117747 while (plen > 0) { 1307432Svi117747 addrtype = ntohs(*p); 1308432Svi117747 switch (addrtype) { 1309432Svi117747 case PARM_ADDR6: 1310432Svi117747 supp_af |= PARM_SUPP_V6; 1311432Svi117747 break; 1312432Svi117747 case PARM_ADDR4: 1313432Svi117747 supp_af |= PARM_SUPP_V4; 1314432Svi117747 break; 1315432Svi117747 default: 1316432Svi117747 break; 1317432Svi117747 } 1318432Svi117747 p++; 1319432Svi117747 plen -= sizeof (*p); 1320432Svi117747 } 1321432Svi117747 } else if (ph->sph_type == htons(PARM_ADDR4)) { 13220Sstevel@tonic-gate if (remaining >= PARM_ADDR4_LEN) { 13230Sstevel@tonic-gate in6_addr_t addr; 13240Sstevel@tonic-gate ipaddr_t ta; 13250Sstevel@tonic-gate 1326432Svi117747 supp_af |= PARM_SUPP_V4; 13270Sstevel@tonic-gate /* 13280Sstevel@tonic-gate * Screen out broad/multicasts & loopback. 13290Sstevel@tonic-gate * If the endpoint only accepts v6 address, 13300Sstevel@tonic-gate * go to the next one. 13310Sstevel@tonic-gate */ 13320Sstevel@tonic-gate bcopy(ph + 1, &ta, sizeof (ta)); 13330Sstevel@tonic-gate if (ta == 0 || 13340Sstevel@tonic-gate ta == INADDR_BROADCAST || 13350Sstevel@tonic-gate ta == htonl(INADDR_LOOPBACK) || 13360Sstevel@tonic-gate IN_MULTICAST(ta) || 13370Sstevel@tonic-gate sctp->sctp_connp->conn_ipv6_v6only) { 13380Sstevel@tonic-gate goto next; 13390Sstevel@tonic-gate } 13400Sstevel@tonic-gate /* 13410Sstevel@tonic-gate * XXX also need to check for subnet 13420Sstevel@tonic-gate * broadcasts. This should probably 13430Sstevel@tonic-gate * wait until we have full access 13440Sstevel@tonic-gate * to the ILL tables. 13450Sstevel@tonic-gate */ 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED((struct in_addr *) 13480Sstevel@tonic-gate (ph + 1), &addr); 13490Sstevel@tonic-gate /* Check for duplicate. */ 13500Sstevel@tonic-gate if (sctp_lookup_faddr(sctp, &addr) != NULL) 13510Sstevel@tonic-gate goto next; 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate /* OK, add it to the faddr set */ 13540Sstevel@tonic-gate if (sctp_add_faddr(sctp, &addr, 13550Sstevel@tonic-gate KM_NOSLEEP) != 0) { 13560Sstevel@tonic-gate return (ENOMEM); 13570Sstevel@tonic-gate } 13580Sstevel@tonic-gate } 13590Sstevel@tonic-gate } else if (ph->sph_type == htons(PARM_ADDR6) && 13600Sstevel@tonic-gate sctp->sctp_family == AF_INET6) { 13610Sstevel@tonic-gate /* An v4 socket should not take v6 addresses. */ 13620Sstevel@tonic-gate if (remaining >= PARM_ADDR6_LEN) { 13630Sstevel@tonic-gate in6_addr_t *addr6; 13640Sstevel@tonic-gate 1365432Svi117747 supp_af |= PARM_SUPP_V6; 13660Sstevel@tonic-gate addr6 = (in6_addr_t *)(ph + 1); 13670Sstevel@tonic-gate /* 13680Sstevel@tonic-gate * Screen out link locals, mcast, loopback 13690Sstevel@tonic-gate * and bogus v6 address. 13700Sstevel@tonic-gate */ 13710Sstevel@tonic-gate if (IN6_IS_ADDR_LINKLOCAL(addr6) || 13720Sstevel@tonic-gate IN6_IS_ADDR_MULTICAST(addr6) || 13730Sstevel@tonic-gate IN6_IS_ADDR_LOOPBACK(addr6) || 13740Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED(addr6)) { 13750Sstevel@tonic-gate goto next; 13760Sstevel@tonic-gate } 13770Sstevel@tonic-gate /* Check for duplicate. */ 13780Sstevel@tonic-gate if (sctp_lookup_faddr(sctp, addr6) != NULL) 13790Sstevel@tonic-gate goto next; 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate if (sctp_add_faddr(sctp, 13820Sstevel@tonic-gate (in6_addr_t *)(ph + 1), KM_NOSLEEP) != 0) { 13830Sstevel@tonic-gate return (ENOMEM); 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate } else if (ph->sph_type == htons(PARM_FORWARD_TSN)) { 13870Sstevel@tonic-gate if (sctp_options != NULL) 13880Sstevel@tonic-gate *sctp_options |= SCTP_PRSCTP_OPTION; 13890Sstevel@tonic-gate } /* else; skip */ 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate next: 13920Sstevel@tonic-gate ph = sctp_next_parm(ph, &remaining); 13930Sstevel@tonic-gate } 1394432Svi117747 if (check_saddr) { 1395432Svi117747 sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE : 1396432Svi117747 B_TRUE); 13970Sstevel@tonic-gate } 1398*852Svi117747 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL); 1399*852Svi117747 /* 1400*852Svi117747 * We have the right address list now, update clustering's 1401*852Svi117747 * knowledge because when we sent the INIT we had just added 1402*852Svi117747 * the address the INIT was sent to. 1403*852Svi117747 */ 1404*852Svi117747 if (psctp == NULL && cl_sctp_assoc_change != NULL) { 1405*852Svi117747 uchar_t *alist; 1406*852Svi117747 size_t asize; 1407*852Svi117747 uchar_t *dlist; 1408*852Svi117747 size_t dsize; 1409*852Svi117747 1410*852Svi117747 asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs; 1411*852Svi117747 alist = kmem_alloc(asize, KM_NOSLEEP); 1412*852Svi117747 if (alist == NULL) 1413*852Svi117747 return (ENOMEM); 1414*852Svi117747 /* 1415*852Svi117747 * Just include the address the INIT was sent to in the 1416*852Svi117747 * delete list and send the entire faddr list. We could 1417*852Svi117747 * do it differently (i.e include all the addresses in the 1418*852Svi117747 * add list even if it contains the original address OR 1419*852Svi117747 * remove the original address from the add list etc.), but 1420*852Svi117747 * this seems reasonable enough. 1421*852Svi117747 */ 1422*852Svi117747 dsize = sizeof (in6_addr_t); 1423*852Svi117747 dlist = kmem_alloc(dsize, KM_NOSLEEP); 1424*852Svi117747 if (dlist == NULL) { 1425*852Svi117747 kmem_free(alist, asize); 1426*852Svi117747 return (ENOMEM); 1427*852Svi117747 } 1428*852Svi117747 bcopy(&curaddr, dlist, sizeof (curaddr)); 1429*852Svi117747 sctp_get_faddr_list(sctp, alist, asize); 1430*852Svi117747 (*cl_sctp_assoc_change)(sctp->sctp_family, alist, asize, 1431*852Svi117747 sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR, 1432*852Svi117747 (cl_sctp_handle_t)sctp); 1433*852Svi117747 /* alist and dlist will be freed by the clustering module */ 1434*852Svi117747 } 14350Sstevel@tonic-gate return (0); 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * Returns 0 if the check failed and the restart should be refused, 14400Sstevel@tonic-gate * 1 if the check succeeded. 14410Sstevel@tonic-gate */ 14420Sstevel@tonic-gate int 14430Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports, 14440Sstevel@tonic-gate int sleep) 14450Sstevel@tonic-gate { 14460Sstevel@tonic-gate sctp_faddr_t *fp, *fpa, *fphead = NULL; 14470Sstevel@tonic-gate sctp_parm_hdr_t *ph; 14480Sstevel@tonic-gate ssize_t remaining; 14490Sstevel@tonic-gate int isv4; 14500Sstevel@tonic-gate ipha_t *iph; 14510Sstevel@tonic-gate ip6_t *ip6h; 14520Sstevel@tonic-gate in6_addr_t hdraddr[1]; 14530Sstevel@tonic-gate int retval = 0; 14540Sstevel@tonic-gate sctp_tf_t *tf; 14550Sstevel@tonic-gate sctp_t *sctp; 14560Sstevel@tonic-gate int compres; 14570Sstevel@tonic-gate sctp_init_chunk_t *init; 14580Sstevel@tonic-gate int nadded = 0; 14590Sstevel@tonic-gate 14600Sstevel@tonic-gate /* extract the address from the IP header */ 14610Sstevel@tonic-gate isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 14620Sstevel@tonic-gate if (isv4) { 14630Sstevel@tonic-gate iph = (ipha_t *)pkt->b_rptr; 14640Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr); 14650Sstevel@tonic-gate } else { 14660Sstevel@tonic-gate ip6h = (ip6_t *)pkt->b_rptr; 14670Sstevel@tonic-gate hdraddr[0] = ip6h->ip6_src; 14680Sstevel@tonic-gate } 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate /* Walk the params in the INIT [ACK], pulling out addr params */ 14710Sstevel@tonic-gate remaining = ntohs(ich->sch_len) - sizeof (*ich) - 14720Sstevel@tonic-gate sizeof (sctp_init_chunk_t); 14730Sstevel@tonic-gate if (remaining < sizeof (*ph)) { 14740Sstevel@tonic-gate /* no parameters; restart OK */ 14750Sstevel@tonic-gate return (1); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate init = (sctp_init_chunk_t *)(ich + 1); 14780Sstevel@tonic-gate ph = (sctp_parm_hdr_t *)(init + 1); 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate while (ph != NULL) { 14810Sstevel@tonic-gate /* params will have already been byteordered when validating */ 14820Sstevel@tonic-gate if (ph->sph_type == htons(PARM_ADDR4)) { 14830Sstevel@tonic-gate if (remaining >= PARM_ADDR4_LEN) { 14840Sstevel@tonic-gate in6_addr_t addr; 14850Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED((struct in_addr *) 14860Sstevel@tonic-gate (ph + 1), &addr); 14870Sstevel@tonic-gate fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 14880Sstevel@tonic-gate sleep); 14890Sstevel@tonic-gate if (!fpa) { 14900Sstevel@tonic-gate goto done; 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate bzero(fpa, sizeof (*fpa)); 14930Sstevel@tonic-gate fpa->faddr = addr; 14940Sstevel@tonic-gate fpa->next = NULL; 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate } else if (ph->sph_type == htons(PARM_ADDR6)) { 14970Sstevel@tonic-gate if (remaining >= PARM_ADDR6_LEN) { 14980Sstevel@tonic-gate fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 14990Sstevel@tonic-gate sleep); 15000Sstevel@tonic-gate if (!fpa) { 15010Sstevel@tonic-gate goto done; 15020Sstevel@tonic-gate } 15030Sstevel@tonic-gate bzero(fpa, sizeof (*fpa)); 15040Sstevel@tonic-gate bcopy(ph + 1, &fpa->faddr, 15050Sstevel@tonic-gate sizeof (fpa->faddr)); 15060Sstevel@tonic-gate fpa->next = NULL; 15070Sstevel@tonic-gate } 15080Sstevel@tonic-gate } else { 15090Sstevel@tonic-gate /* else not addr param; skip */ 15100Sstevel@tonic-gate fpa = NULL; 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate /* link in the new addr, if it was an addr param */ 15130Sstevel@tonic-gate if (fpa) { 15140Sstevel@tonic-gate if (!fphead) { 15150Sstevel@tonic-gate fphead = fpa; 15160Sstevel@tonic-gate fp = fphead; 15170Sstevel@tonic-gate } else { 15180Sstevel@tonic-gate fp->next = fpa; 15190Sstevel@tonic-gate fp = fpa; 15200Sstevel@tonic-gate } 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate 15230Sstevel@tonic-gate ph = sctp_next_parm(ph, &remaining); 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate 15260Sstevel@tonic-gate if (fphead == NULL) { 15270Sstevel@tonic-gate /* no addr parameters; restart OK */ 15280Sstevel@tonic-gate return (1); 15290Sstevel@tonic-gate } 15300Sstevel@tonic-gate 15310Sstevel@tonic-gate /* 15320Sstevel@tonic-gate * got at least one; make sure the header's addr is 15330Sstevel@tonic-gate * in the list 15340Sstevel@tonic-gate */ 15350Sstevel@tonic-gate fp = sctp_lookup_faddr_nosctp(fphead, hdraddr); 15360Sstevel@tonic-gate if (!fp) { 15370Sstevel@tonic-gate /* not included; add it now */ 15380Sstevel@tonic-gate fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep); 15390Sstevel@tonic-gate if (!fp) { 15400Sstevel@tonic-gate goto done; 15410Sstevel@tonic-gate } 15420Sstevel@tonic-gate bzero(fp, sizeof (*fp)); 15430Sstevel@tonic-gate fp->faddr = *hdraddr; 15440Sstevel@tonic-gate fp->next = fphead; 15450Sstevel@tonic-gate fphead = fp; 15460Sstevel@tonic-gate } 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate /* 15490Sstevel@tonic-gate * Now, we can finally do the check: For each sctp instance 15500Sstevel@tonic-gate * on the hash line for ports, compare its faddr set against 15510Sstevel@tonic-gate * the new one. If the new one is a strict subset of any 15520Sstevel@tonic-gate * existing sctp's faddrs, the restart is OK. However, if there 15530Sstevel@tonic-gate * is an overlap, this could be an attack, so return failure. 15540Sstevel@tonic-gate * If all sctp's faddrs are disjoint, this is a legitimate new 15550Sstevel@tonic-gate * association. 15560Sstevel@tonic-gate */ 15570Sstevel@tonic-gate tf = &(sctp_conn_fanout[SCTP_CONN_HASH(ports)]); 15580Sstevel@tonic-gate mutex_enter(&tf->tf_lock); 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) { 15610Sstevel@tonic-gate if (ports != sctp->sctp_ports) { 15620Sstevel@tonic-gate continue; 15630Sstevel@tonic-gate } 15640Sstevel@tonic-gate compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs); 15650Sstevel@tonic-gate if (compres <= SCTP_ADDR_SUBSET) { 15660Sstevel@tonic-gate retval = 1; 15670Sstevel@tonic-gate mutex_exit(&tf->tf_lock); 15680Sstevel@tonic-gate goto done; 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate if (compres == SCTP_ADDR_OVERLAP) { 15710Sstevel@tonic-gate dprint(1, 15720Sstevel@tonic-gate ("new assoc from %x:%x:%x:%x overlaps with %p\n", 15730Sstevel@tonic-gate SCTP_PRINTADDR(*hdraddr), sctp)); 15740Sstevel@tonic-gate /* 15750Sstevel@tonic-gate * While we still hold the lock, we need to 15760Sstevel@tonic-gate * figure out which addresses have been 15770Sstevel@tonic-gate * added so we can include them in the abort 15780Sstevel@tonic-gate * we will send back. Since these faddrs will 15790Sstevel@tonic-gate * never be used, we overload the rto field 15800Sstevel@tonic-gate * here, setting it to 0 if the address was 15810Sstevel@tonic-gate * not added, 1 if it was added. 15820Sstevel@tonic-gate */ 15830Sstevel@tonic-gate for (fp = fphead; fp; fp = fp->next) { 15840Sstevel@tonic-gate if (sctp_lookup_faddr(sctp, &fp->faddr)) { 15850Sstevel@tonic-gate fp->rto = 0; 15860Sstevel@tonic-gate } else { 15870Sstevel@tonic-gate fp->rto = 1; 15880Sstevel@tonic-gate nadded++; 15890Sstevel@tonic-gate } 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate mutex_exit(&tf->tf_lock); 15920Sstevel@tonic-gate goto done; 15930Sstevel@tonic-gate } 15940Sstevel@tonic-gate } 15950Sstevel@tonic-gate mutex_exit(&tf->tf_lock); 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate /* All faddrs are disjoint; legit new association */ 15980Sstevel@tonic-gate retval = 1; 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate done: 16010Sstevel@tonic-gate /* If are attempted adds, send back an abort listing the addrs */ 16020Sstevel@tonic-gate if (nadded > 0) { 16030Sstevel@tonic-gate void *dtail; 16040Sstevel@tonic-gate size_t dlen; 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP); 16070Sstevel@tonic-gate if (dtail == NULL) { 16080Sstevel@tonic-gate goto cleanup; 16090Sstevel@tonic-gate } 16100Sstevel@tonic-gate 16110Sstevel@tonic-gate ph = dtail; 16120Sstevel@tonic-gate dlen = 0; 16130Sstevel@tonic-gate for (fp = fphead; fp; fp = fp->next) { 16140Sstevel@tonic-gate if (fp->rto == 0) { 16150Sstevel@tonic-gate continue; 16160Sstevel@tonic-gate } 16170Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 16180Sstevel@tonic-gate ipaddr_t addr4; 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate ph->sph_type = htons(PARM_ADDR4); 16210Sstevel@tonic-gate ph->sph_len = htons(PARM_ADDR4_LEN); 16220Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4); 16230Sstevel@tonic-gate ph++; 16240Sstevel@tonic-gate bcopy(&addr4, ph, sizeof (addr4)); 16250Sstevel@tonic-gate ph = (sctp_parm_hdr_t *) 16260Sstevel@tonic-gate ((char *)ph + sizeof (addr4)); 16270Sstevel@tonic-gate dlen += PARM_ADDR4_LEN; 16280Sstevel@tonic-gate } else { 16290Sstevel@tonic-gate ph->sph_type = htons(PARM_ADDR6); 16300Sstevel@tonic-gate ph->sph_len = htons(PARM_ADDR6_LEN); 16310Sstevel@tonic-gate ph++; 16320Sstevel@tonic-gate bcopy(&fp->faddr, ph, sizeof (fp->faddr)); 16330Sstevel@tonic-gate ph = (sctp_parm_hdr_t *) 16340Sstevel@tonic-gate ((char *)ph + sizeof (fp->faddr)); 16350Sstevel@tonic-gate dlen += PARM_ADDR6_LEN; 16360Sstevel@tonic-gate } 16370Sstevel@tonic-gate } 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate /* Send off the abort */ 16400Sstevel@tonic-gate sctp_send_abort(sctp, sctp_init2vtag(ich), 16410Sstevel@tonic-gate SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE); 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate kmem_free(dtail, PARM_ADDR6_LEN * nadded); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate 16460Sstevel@tonic-gate cleanup: 16470Sstevel@tonic-gate /* Clean up */ 16480Sstevel@tonic-gate if (fphead) { 16490Sstevel@tonic-gate sctp_faddr_t *fpn; 16500Sstevel@tonic-gate for (fp = fphead; fp; fp = fpn) { 16510Sstevel@tonic-gate fpn = fp->next; 16520Sstevel@tonic-gate kmem_cache_free(sctp_kmem_faddr_cache, fp); 16530Sstevel@tonic-gate } 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate return (retval); 16570Sstevel@tonic-gate } 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate void 16600Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp) 16610Sstevel@tonic-gate { 16620Sstevel@tonic-gate sctp_faddr_t *fp; 16630Sstevel@tonic-gate 16640Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 16650Sstevel@tonic-gate fp->ssthresh = sctp_initial_mtu; 16660Sstevel@tonic-gate fp->cwnd = fp->sfa_pmss * sctp_slow_start_initial; 16670Sstevel@tonic-gate fp->suna = 0; 16680Sstevel@tonic-gate fp->pba = 0; 16690Sstevel@tonic-gate } 16700Sstevel@tonic-gate } 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate /* 16730Sstevel@tonic-gate * Return zero if the buffers are identical in length and content. 16740Sstevel@tonic-gate * This is used for comparing extension header buffers. 16750Sstevel@tonic-gate * Note that an extension header would be declared different 16760Sstevel@tonic-gate * even if all that changed was the next header value in that header i.e. 16770Sstevel@tonic-gate * what really changed is the next extension header. 16780Sstevel@tonic-gate */ 16790Sstevel@tonic-gate boolean_t 16800Sstevel@tonic-gate sctp_cmpbuf(void *a, uint_t alen, boolean_t b_valid, void *b, uint_t blen) 16810Sstevel@tonic-gate { 16820Sstevel@tonic-gate if (!b_valid) 16830Sstevel@tonic-gate blen = 0; 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate if (alen != blen) 16860Sstevel@tonic-gate return (B_TRUE); 16870Sstevel@tonic-gate if (alen == 0) 16880Sstevel@tonic-gate return (B_FALSE); /* Both zero length */ 16890Sstevel@tonic-gate return (bcmp(a, b, alen)); 16900Sstevel@tonic-gate } 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate /* 16930Sstevel@tonic-gate * Preallocate memory for sctp_savebuf(). Returns B_TRUE if ok. 16940Sstevel@tonic-gate * Return B_FALSE if memory allocation fails - don't change any state! 16950Sstevel@tonic-gate */ 16960Sstevel@tonic-gate boolean_t 16970Sstevel@tonic-gate sctp_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 16980Sstevel@tonic-gate void *src, uint_t srclen) 16990Sstevel@tonic-gate { 17000Sstevel@tonic-gate void *dst; 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate if (!src_valid) 17030Sstevel@tonic-gate srclen = 0; 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate ASSERT(*dstlenp == 0); 17060Sstevel@tonic-gate if (src != NULL && srclen != 0) { 17070Sstevel@tonic-gate dst = mi_zalloc(srclen); 17080Sstevel@tonic-gate if (dst == NULL) 17090Sstevel@tonic-gate return (B_FALSE); 17100Sstevel@tonic-gate } else { 17110Sstevel@tonic-gate dst = NULL; 17120Sstevel@tonic-gate } 17130Sstevel@tonic-gate if (*dstp != NULL) { 17140Sstevel@tonic-gate mi_free(*dstp); 17150Sstevel@tonic-gate *dstp = NULL; 17160Sstevel@tonic-gate *dstlenp = 0; 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate *dstp = dst; 17190Sstevel@tonic-gate if (dst != NULL) 17200Sstevel@tonic-gate *dstlenp = srclen; 17210Sstevel@tonic-gate else 17220Sstevel@tonic-gate *dstlenp = 0; 17230Sstevel@tonic-gate return (B_TRUE); 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate /* 17270Sstevel@tonic-gate * Replace what is in *dst, *dstlen with the source. 17280Sstevel@tonic-gate * Assumes sctp_allocbuf has already been called. 17290Sstevel@tonic-gate */ 17300Sstevel@tonic-gate void 17310Sstevel@tonic-gate sctp_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 17320Sstevel@tonic-gate void *src, uint_t srclen) 17330Sstevel@tonic-gate { 17340Sstevel@tonic-gate if (!src_valid) 17350Sstevel@tonic-gate srclen = 0; 17360Sstevel@tonic-gate 17370Sstevel@tonic-gate ASSERT(*dstlenp == srclen); 17380Sstevel@tonic-gate if (src != NULL && srclen != 0) { 17390Sstevel@tonic-gate bcopy(src, *dstp, srclen); 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate } 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate static void 17440Sstevel@tonic-gate sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr) 17450Sstevel@tonic-gate { 17460Sstevel@tonic-gate bcopy(addr, &fp->faddr, sizeof (*addr)); 17470Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(addr)) { 17480Sstevel@tonic-gate fp->isv4 = 1; 17490Sstevel@tonic-gate /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 17500Sstevel@tonic-gate fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr_len) & 17510Sstevel@tonic-gate ~(SCTP_ALIGN - 1); 17520Sstevel@tonic-gate } else { 17530Sstevel@tonic-gate fp->isv4 = 0; 17540Sstevel@tonic-gate fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr6_len) & 17550Sstevel@tonic-gate ~(SCTP_ALIGN - 1); 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate fp->cwnd = sctp_slow_start_initial * fp->sfa_pmss; 17580Sstevel@tonic-gate fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max); 17590Sstevel@tonic-gate fp->srtt = -1; 17600Sstevel@tonic-gate fp->rtt_updates = 0; 17610Sstevel@tonic-gate fp->strikes = 0; 17620Sstevel@tonic-gate fp->max_retr = sctp->sctp_pp_max_rxt; 17630Sstevel@tonic-gate /* Mark it as not confirmed. */ 17640Sstevel@tonic-gate fp->state = SCTP_FADDRS_UNCONFIRMED; 17650Sstevel@tonic-gate fp->hb_interval = sctp->sctp_hb_interval; 17660Sstevel@tonic-gate fp->ssthresh = sctp_initial_ssthresh; 17670Sstevel@tonic-gate fp->suna = 0; 17680Sstevel@tonic-gate fp->pba = 0; 17690Sstevel@tonic-gate fp->acked = 0; 17700Sstevel@tonic-gate fp->lastactive = lbolt64; 17710Sstevel@tonic-gate fp->timer_mp = NULL; 17720Sstevel@tonic-gate fp->hb_pending = B_FALSE; 17730Sstevel@tonic-gate fp->timer_running = 0; 17740Sstevel@tonic-gate fp->df = 1; 17750Sstevel@tonic-gate fp->pmtu_discovered = 0; 17760Sstevel@tonic-gate fp->rc_timer_mp = NULL; 17770Sstevel@tonic-gate fp->rc_timer_running = 0; 17780Sstevel@tonic-gate fp->next = NULL; 17790Sstevel@tonic-gate fp->ire = NULL; 17800Sstevel@tonic-gate fp->T3expire = 0; 17810Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret, 17820Sstevel@tonic-gate sizeof (fp->hb_secret)); 17830Sstevel@tonic-gate fp->hb_expiry = lbolt64; 17840Sstevel@tonic-gate 17850Sstevel@tonic-gate sctp_ire2faddr(sctp, fp); 17860Sstevel@tonic-gate } 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate /*ARGSUSED*/ 17890Sstevel@tonic-gate static void 17900Sstevel@tonic-gate faddr_destructor(void *buf, void *cdrarg) 17910Sstevel@tonic-gate { 17920Sstevel@tonic-gate sctp_faddr_t *fp = buf; 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate ASSERT(fp->timer_mp == NULL); 17950Sstevel@tonic-gate ASSERT(fp->timer_running == 0); 17960Sstevel@tonic-gate 17970Sstevel@tonic-gate ASSERT(fp->rc_timer_mp == NULL); 17980Sstevel@tonic-gate ASSERT(fp->rc_timer_running == 0); 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate void 18020Sstevel@tonic-gate sctp_faddr_init() 18030Sstevel@tonic-gate { 18040Sstevel@tonic-gate sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache", 18050Sstevel@tonic-gate sizeof (sctp_faddr_t), 0, NULL, faddr_destructor, 18060Sstevel@tonic-gate NULL, NULL, NULL, 0); 18070Sstevel@tonic-gate } 18080Sstevel@tonic-gate 18090Sstevel@tonic-gate void 18100Sstevel@tonic-gate sctp_faddr_fini() 18110Sstevel@tonic-gate { 18120Sstevel@tonic-gate kmem_cache_destroy(sctp_kmem_faddr_cache); 18130Sstevel@tonic-gate } 1814