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) { 141*432Svi117747 if (sp->saddr_ipif_unconfirmed == 1) 142*432Svi117747 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) { 167*432Svi117747 if (sp->saddr_ipif_unconfirmed == 1) 168*432Svi117747 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; 4250Sstevel@tonic-gate ip6h->ip6_plen = htons(sum - ((char *)&sctp->sctp_ip6h[1] - 4260Sstevel@tonic-gate sctp->sctp_iphc6)); 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate int 4310Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2) 4320Sstevel@tonic-gate { 4330Sstevel@tonic-gate int na1 = 0; 4340Sstevel@tonic-gate int overlap = 0; 4350Sstevel@tonic-gate int equal = 1; 4360Sstevel@tonic-gate int onematch; 4370Sstevel@tonic-gate sctp_faddr_t *fp1, *fp2; 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate for (fp1 = a1; fp1; fp1 = fp1->next) { 4400Sstevel@tonic-gate onematch = 0; 4410Sstevel@tonic-gate for (fp2 = a2; fp2; fp2 = fp2->next) { 4420Sstevel@tonic-gate if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) { 4430Sstevel@tonic-gate overlap++; 4440Sstevel@tonic-gate onematch = 1; 4450Sstevel@tonic-gate break; 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate if (!onematch) { 4480Sstevel@tonic-gate equal = 0; 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate na1++; 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate if (equal) { 4550Sstevel@tonic-gate return (SCTP_ADDR_EQUAL); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate if (overlap == na1) { 4580Sstevel@tonic-gate return (SCTP_ADDR_SUBSET); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate if (overlap) { 4610Sstevel@tonic-gate return (SCTP_ADDR_OVERLAP); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate return (SCTP_ADDR_DISJOINT); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * Returns 0 on success, -1 on memory allocation failure. If sleep 4680Sstevel@tonic-gate * is true, should never fail. 4690Sstevel@tonic-gate * Caller must hold conn fanout lock. 4700Sstevel@tonic-gate */ 4710Sstevel@tonic-gate int 4720Sstevel@tonic-gate sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep) 4730Sstevel@tonic-gate { 4740Sstevel@tonic-gate sctp_faddr_t *faddr; 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate dprint(4, ("add_faddr: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr), 4770Sstevel@tonic-gate sleep)); 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) { 4800Sstevel@tonic-gate return (-1); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate sctp_init_faddr(sctp, faddr, addr); 4840Sstevel@tonic-gate ASSERT(faddr->next == NULL); 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* tack it on to the end */ 4870Sstevel@tonic-gate if (sctp->sctp_lastfaddr != NULL) { 4880Sstevel@tonic-gate sctp->sctp_lastfaddr->next = faddr; 4890Sstevel@tonic-gate } else { 4900Sstevel@tonic-gate /* list is empty */ 4910Sstevel@tonic-gate ASSERT(sctp->sctp_faddrs == NULL); 4920Sstevel@tonic-gate sctp->sctp_faddrs = faddr; 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate sctp->sctp_lastfaddr = faddr; 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate return (0); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate /* 5000Sstevel@tonic-gate * Caller must hold conn fanout lock. 5010Sstevel@tonic-gate */ 5020Sstevel@tonic-gate int 5030Sstevel@tonic-gate sctp_add_faddr_first(sctp_t *sctp, in6_addr_t *addr, int sleep) 5040Sstevel@tonic-gate { 5050Sstevel@tonic-gate sctp_faddr_t *faddr; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate dprint(4, ("add_faddr_first: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr), 5080Sstevel@tonic-gate sleep)); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) { 5110Sstevel@tonic-gate return (-1); 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate sctp_init_faddr(sctp, faddr, addr); 5140Sstevel@tonic-gate ASSERT(faddr->next == NULL); 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* Put it at the beginning of the list */ 5170Sstevel@tonic-gate if (sctp->sctp_faddrs != NULL) { 5180Sstevel@tonic-gate faddr->next = sctp->sctp_faddrs; 5190Sstevel@tonic-gate } else { 5200Sstevel@tonic-gate sctp->sctp_lastfaddr = faddr; 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate sctp->sctp_faddrs = faddr; 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate return (0); 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate sctp_faddr_t * 5280Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr) 5290Sstevel@tonic-gate { 5300Sstevel@tonic-gate sctp_faddr_t *fp; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 5330Sstevel@tonic-gate if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) 5340Sstevel@tonic-gate break; 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate return (fp); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate sctp_faddr_t * 5410Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr) 5420Sstevel@tonic-gate { 5430Sstevel@tonic-gate for (; fp; fp = fp->next) { 5440Sstevel@tonic-gate if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) { 5450Sstevel@tonic-gate break; 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate return (fp); 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate void 5530Sstevel@tonic-gate sctp_faddr2hdraddr(sctp_faddr_t *fp, sctp_t *sctp) 5540Sstevel@tonic-gate { 5550Sstevel@tonic-gate if (fp->isv4) { 5560Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, 5570Sstevel@tonic-gate sctp->sctp_ipha->ipha_dst); 5580Sstevel@tonic-gate /* Must not allow unspec src addr if not bound to all */ 5590Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr) && 5600Sstevel@tonic-gate !sctp->sctp_bound_to_all) { 5610Sstevel@tonic-gate /* 5620Sstevel@tonic-gate * set the src to the first v4 saddr and hope 5630Sstevel@tonic-gate * for the best 5640Sstevel@tonic-gate */ 5650Sstevel@tonic-gate fp->saddr = sctp_get_valid_addr(sctp, B_FALSE); 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src); 5680Sstevel@tonic-gate /* update don't fragment bit */ 5690Sstevel@tonic-gate if (fp->df) { 5700Sstevel@tonic-gate sctp->sctp_ipha->ipha_fragment_offset_and_flags = 5710Sstevel@tonic-gate htons(IPH_DF); 5720Sstevel@tonic-gate } else { 5730Sstevel@tonic-gate sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0; 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate } else { 5760Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = fp->faddr; 5770Sstevel@tonic-gate /* Must not allow unspec src addr if not bound to all */ 5780Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(&fp->saddr) && 5790Sstevel@tonic-gate !sctp->sctp_bound_to_all) { 5800Sstevel@tonic-gate /* 5810Sstevel@tonic-gate * set the src to the first v6 saddr and hope 5820Sstevel@tonic-gate * for the best 5830Sstevel@tonic-gate */ 5840Sstevel@tonic-gate fp->saddr = sctp_get_valid_addr(sctp, B_TRUE); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = fp->saddr; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate void 5910Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp) 5920Sstevel@tonic-gate { 5930Sstevel@tonic-gate sctp_faddr_t *fp; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 5960Sstevel@tonic-gate sctp_ire2faddr(sctp, fp); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate sctp_faddr2hdraddr(sctp->sctp_current, sctp); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate void 6030Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp) 6040Sstevel@tonic-gate { 6050Sstevel@tonic-gate int64_t now = lbolt64; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate fp->strikes = 0; 6080Sstevel@tonic-gate sctp->sctp_strikes = 0; 6090Sstevel@tonic-gate fp->lastactive = now; 6100Sstevel@tonic-gate fp->hb_expiry = now + SET_HB_INTVL(fp); 6110Sstevel@tonic-gate fp->hb_pending = B_FALSE; 6120Sstevel@tonic-gate if (fp->state != SCTP_FADDRS_ALIVE) { 6130Sstevel@tonic-gate fp->state = SCTP_FADDRS_ALIVE; 6140Sstevel@tonic-gate sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate /* If this is the primary, switch back to it now */ 6170Sstevel@tonic-gate if (fp == sctp->sctp_primary) { 6180Sstevel@tonic-gate sctp->sctp_current = fp; 6190Sstevel@tonic-gate sctp->sctp_mss = fp->sfa_pmss; 6200Sstevel@tonic-gate /* Reset the addrs in the composite header */ 6210Sstevel@tonic-gate sctp_faddr2hdraddr(fp, sctp); 6220Sstevel@tonic-gate if (!SCTP_IS_DETACHED(sctp)) { 6230Sstevel@tonic-gate sctp_set_ulp_prop(sctp); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate if (fp->ire == NULL) { 6280Sstevel@tonic-gate /* Should have a full IRE now */ 6290Sstevel@tonic-gate sctp_ire2faddr(sctp, fp); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate int 6340Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp) 6350Sstevel@tonic-gate { 6360Sstevel@tonic-gate sctp_faddr_t *fp; 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 6390Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) { 6400Sstevel@tonic-gate return (1); 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate return (0); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * Returns 0 if there is at leave one other active faddr, -1 if there 6490Sstevel@tonic-gate * are none. If there are none left, faddr_dead() will start killing the 6500Sstevel@tonic-gate * association. 6510Sstevel@tonic-gate * If the downed faddr was the current faddr, a new current faddr 6520Sstevel@tonic-gate * will be chosen. 6530Sstevel@tonic-gate */ 6540Sstevel@tonic-gate int 6550Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate) 6560Sstevel@tonic-gate { 6570Sstevel@tonic-gate sctp_faddr_t *ofp; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_ALIVE) { 6600Sstevel@tonic-gate sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate fp->state = newstate; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n", 6650Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr), newstate)); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if (fp == sctp->sctp_current) { 6680Sstevel@tonic-gate /* Current faddr down; need to switch it */ 6690Sstevel@tonic-gate sctp->sctp_current = NULL; 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate /* Find next alive faddr */ 6730Sstevel@tonic-gate ofp = fp; 6740Sstevel@tonic-gate for (fp = fp->next; fp; fp = fp->next) { 6750Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_ALIVE) { 6760Sstevel@tonic-gate break; 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate if (fp == NULL) { 6810Sstevel@tonic-gate /* Continue from beginning of list */ 6820Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp != ofp; 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 6890Sstevel@tonic-gate if (fp != ofp) { 6900Sstevel@tonic-gate if (sctp->sctp_current == NULL) { 6910Sstevel@tonic-gate dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n", 6920Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr))); 6930Sstevel@tonic-gate sctp->sctp_current = fp; 6940Sstevel@tonic-gate sctp->sctp_mss = fp->sfa_pmss; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate /* Reset the addrs in the composite header */ 6970Sstevel@tonic-gate sctp_faddr2hdraddr(fp, sctp); 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate if (!SCTP_IS_DETACHED(sctp)) { 7000Sstevel@tonic-gate sctp_set_ulp_prop(sctp); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate return (0); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate /* All faddrs are down; kill the association */ 7080Sstevel@tonic-gate dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n")); 7090Sstevel@tonic-gate BUMP_MIB(&sctp_mib, sctpAborted); 7100Sstevel@tonic-gate sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ? 7110Sstevel@tonic-gate SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL); 7120Sstevel@tonic-gate sctp_clean_death(sctp, sctp->sctp_client_errno ? 7130Sstevel@tonic-gate sctp->sctp_client_errno : ETIMEDOUT); 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate return (-1); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate sctp_faddr_t * 7190Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp) 7200Sstevel@tonic-gate { 7210Sstevel@tonic-gate sctp_faddr_t *nfp = NULL; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (ofp == NULL) { 7240Sstevel@tonic-gate ofp = sctp->sctp_current; 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate /* Find the next live one */ 7280Sstevel@tonic-gate for (nfp = ofp->next; nfp != NULL; nfp = nfp->next) { 7290Sstevel@tonic-gate if (nfp->state == SCTP_FADDRS_ALIVE) { 7300Sstevel@tonic-gate break; 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate if (nfp == NULL) { 7350Sstevel@tonic-gate /* Continue from beginning of list */ 7360Sstevel@tonic-gate for (nfp = sctp->sctp_faddrs; nfp != ofp; 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 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * nfp could only be NULL if all faddrs are down, and when 7450Sstevel@tonic-gate * this happens, faddr_dead() should have killed the 7460Sstevel@tonic-gate * association. Hence this assertion... 7470Sstevel@tonic-gate */ 7480Sstevel@tonic-gate ASSERT(nfp != NULL); 7490Sstevel@tonic-gate return (nfp); 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate void 7530Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp) 7540Sstevel@tonic-gate { 7550Sstevel@tonic-gate sctp_faddr_t *fpp; 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate if (!sctp->sctp_faddrs) { 7580Sstevel@tonic-gate return; 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate if (fp->timer_mp != NULL) { 7620Sstevel@tonic-gate sctp_timer_free(fp->timer_mp); 7630Sstevel@tonic-gate fp->timer_mp = NULL; 7640Sstevel@tonic-gate fp->timer_running = 0; 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate if (fp->rc_timer_mp != NULL) { 7670Sstevel@tonic-gate sctp_timer_free(fp->rc_timer_mp); 7680Sstevel@tonic-gate fp->rc_timer_mp = NULL; 7690Sstevel@tonic-gate fp->rc_timer_running = 0; 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate if (fp->ire != NULL) { 7720Sstevel@tonic-gate IRE_REFRELE_NOTR(fp->ire); 7730Sstevel@tonic-gate fp->ire = NULL; 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate if (fp == sctp->sctp_faddrs) { 7770Sstevel@tonic-gate goto gotit; 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next) 7810Sstevel@tonic-gate ; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate gotit: 7840Sstevel@tonic-gate ASSERT(sctp->sctp_conn_tfp != NULL); 7850Sstevel@tonic-gate mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 7860Sstevel@tonic-gate if (fp == sctp->sctp_faddrs) { 7870Sstevel@tonic-gate sctp->sctp_faddrs = fp->next; 7880Sstevel@tonic-gate } else { 7890Sstevel@tonic-gate fpp->next = fp->next; 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 7920Sstevel@tonic-gate /* XXX faddr2ire? */ 7930Sstevel@tonic-gate kmem_cache_free(sctp_kmem_faddr_cache, fp); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate void 7970Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock) 7980Sstevel@tonic-gate { 7990Sstevel@tonic-gate sctp_faddr_t *fp, *fpn; 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate if (sctp->sctp_faddrs == NULL) { 8020Sstevel@tonic-gate ASSERT(sctp->sctp_lastfaddr == NULL); 8030Sstevel@tonic-gate return; 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate ASSERT(sctp->sctp_lastfaddr != NULL); 8070Sstevel@tonic-gate sctp->sctp_lastfaddr = NULL; 8080Sstevel@tonic-gate sctp->sctp_current = NULL; 8090Sstevel@tonic-gate sctp->sctp_primary = NULL; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate sctp_free_faddr_timers(sctp); 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 8140Sstevel@tonic-gate /* in conn fanout; need to hold lock */ 8150Sstevel@tonic-gate mutex_enter(&sctp->sctp_conn_tfp->tf_lock); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fpn) { 8190Sstevel@tonic-gate fpn = fp->next; 8200Sstevel@tonic-gate if (fp->ire != NULL) 8210Sstevel@tonic-gate IRE_REFRELE_NOTR(fp->ire); 8220Sstevel@tonic-gate kmem_cache_free(sctp_kmem_faddr_cache, fp); 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate sctp->sctp_faddrs = NULL; 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) { 8280Sstevel@tonic-gate mutex_exit(&sctp->sctp_conn_tfp->tf_lock); 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate void 8340Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp) 8350Sstevel@tonic-gate { 8360Sstevel@tonic-gate sctp_zap_faddrs(sctp, 0); 8370Sstevel@tonic-gate sctp_free_saddrs(sctp); 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate /* 8410Sstevel@tonic-gate * Initialize the IPv4 header. Loses any record of any IP options. 8420Sstevel@tonic-gate */ 8430Sstevel@tonic-gate int 8440Sstevel@tonic-gate sctp_header_init_ipv4(sctp_t *sctp, int sleep) 8450Sstevel@tonic-gate { 8460Sstevel@tonic-gate sctp_hdr_t *sctph; 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate /* 8490Sstevel@tonic-gate * This is a simple initialization. If there's 8500Sstevel@tonic-gate * already a template, it should never be too small, 8510Sstevel@tonic-gate * so reuse it. Otherwise, allocate space for the new one. 8520Sstevel@tonic-gate */ 8530Sstevel@tonic-gate if (sctp->sctp_iphc != NULL) { 8540Sstevel@tonic-gate ASSERT(sctp->sctp_iphc_len >= SCTP_MAX_COMBINED_HEADER_LENGTH); 8550Sstevel@tonic-gate bzero(sctp->sctp_iphc, sctp->sctp_iphc_len); 8560Sstevel@tonic-gate } else { 8570Sstevel@tonic-gate sctp->sctp_iphc_len = SCTP_MAX_COMBINED_HEADER_LENGTH; 8580Sstevel@tonic-gate sctp->sctp_iphc = kmem_zalloc(sctp->sctp_iphc_len, sleep); 8590Sstevel@tonic-gate if (sctp->sctp_iphc == NULL) { 8600Sstevel@tonic-gate sctp->sctp_iphc_len = 0; 8610Sstevel@tonic-gate return (ENOMEM); 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc; 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate sctp->sctp_hdr_len = sizeof (ipha_t) + sizeof (sctp_hdr_t); 8680Sstevel@tonic-gate sctp->sctp_ip_hdr_len = sizeof (ipha_t); 8690Sstevel@tonic-gate sctp->sctp_ipha->ipha_length = htons(sizeof (ipha_t) + 8700Sstevel@tonic-gate sizeof (sctp_hdr_t)); 8710Sstevel@tonic-gate sctp->sctp_ipha->ipha_version_and_hdr_length 8720Sstevel@tonic-gate = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate /* 8750Sstevel@tonic-gate * These two fields should be zero, and are already set above. 8760Sstevel@tonic-gate * 8770Sstevel@tonic-gate * sctp->sctp_ipha->ipha_ident, 8780Sstevel@tonic-gate * sctp->sctp_ipha->ipha_fragment_offset_and_flags. 8790Sstevel@tonic-gate */ 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate sctp->sctp_ipha->ipha_ttl = sctp_ipv4_ttl; 8820Sstevel@tonic-gate sctp->sctp_ipha->ipha_protocol = IPPROTO_SCTP; 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate sctph = (sctp_hdr_t *)(sctp->sctp_iphc + sizeof (ipha_t)); 8850Sstevel@tonic-gate sctp->sctp_sctph = sctph; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate return (0); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate /* 8910Sstevel@tonic-gate * Update sctp_sticky_hdrs based on sctp_sticky_ipp. 8920Sstevel@tonic-gate * The headers include ip6i_t (if needed), ip6_t, any sticky extension 8930Sstevel@tonic-gate * headers, and the maximum size sctp header (to avoid reallocation 8940Sstevel@tonic-gate * on the fly for additional sctp options). 8950Sstevel@tonic-gate * Returns failure if can't allocate memory. 8960Sstevel@tonic-gate */ 8970Sstevel@tonic-gate int 8980Sstevel@tonic-gate sctp_build_hdrs(sctp_t *sctp) 8990Sstevel@tonic-gate { 9000Sstevel@tonic-gate char *hdrs; 9010Sstevel@tonic-gate uint_t hdrs_len; 9020Sstevel@tonic-gate ip6i_t *ip6i; 9030Sstevel@tonic-gate char buf[SCTP_MAX_HDR_LENGTH]; 9040Sstevel@tonic-gate ip6_pkt_t *ipp = &sctp->sctp_sticky_ipp; 9050Sstevel@tonic-gate in6_addr_t src; 9060Sstevel@tonic-gate in6_addr_t dst; 9070Sstevel@tonic-gate uint8_t hoplimit; 9080Sstevel@tonic-gate /* 9090Sstevel@tonic-gate * save the existing sctp header and source/dest IP addresses 9100Sstevel@tonic-gate */ 9110Sstevel@tonic-gate bcopy(sctp->sctp_sctph6, buf, sizeof (sctp_hdr_t)); 9120Sstevel@tonic-gate src = sctp->sctp_ip6h->ip6_src; 9130Sstevel@tonic-gate dst = sctp->sctp_ip6h->ip6_dst; 9140Sstevel@tonic-gate hoplimit = sctp->sctp_ip6h->ip6_hops; 9150Sstevel@tonic-gate hdrs_len = ip_total_hdrs_len_v6(ipp) + SCTP_MAX_HDR_LENGTH; 9160Sstevel@tonic-gate ASSERT(hdrs_len != 0); 9170Sstevel@tonic-gate if (hdrs_len > sctp->sctp_iphc6_len) { 9180Sstevel@tonic-gate /* Need to reallocate */ 9190Sstevel@tonic-gate hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 9200Sstevel@tonic-gate if (hdrs == NULL) 9210Sstevel@tonic-gate return (ENOMEM); 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate if (sctp->sctp_iphc6_len != 0) 9240Sstevel@tonic-gate kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len); 9250Sstevel@tonic-gate sctp->sctp_iphc6 = hdrs; 9260Sstevel@tonic-gate sctp->sctp_iphc6_len = hdrs_len; 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate ip_build_hdrs_v6((uchar_t *)sctp->sctp_iphc6, 9290Sstevel@tonic-gate hdrs_len - SCTP_MAX_HDR_LENGTH, ipp, IPPROTO_SCTP); 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate /* Set header fields not in ipp */ 9320Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_HAS_IP6I) { 9330Sstevel@tonic-gate ip6i = (ip6i_t *)sctp->sctp_iphc6; 9340Sstevel@tonic-gate sctp->sctp_ip6h = (ip6_t *)&ip6i[1]; 9350Sstevel@tonic-gate } else { 9360Sstevel@tonic-gate sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6; 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate /* 9390Sstevel@tonic-gate * sctp->sctp_ip_hdr_len will include ip6i_t if there is one. 9400Sstevel@tonic-gate */ 9410Sstevel@tonic-gate sctp->sctp_ip_hdr6_len = hdrs_len - SCTP_MAX_HDR_LENGTH; 9420Sstevel@tonic-gate sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 + 9430Sstevel@tonic-gate sctp->sctp_ip_hdr6_len); 9440Sstevel@tonic-gate sctp->sctp_hdr6_len = sctp->sctp_ip_hdr6_len + sizeof (sctp_hdr_t); 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate bcopy(buf, sctp->sctp_sctph6, sizeof (sctp_hdr_t)); 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = src; 9490Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = dst; 9500Sstevel@tonic-gate /* 9510Sstevel@tonic-gate * If IPV6_HOPLIMIT was set in ipp, use that value. 9520Sstevel@tonic-gate * For sticky options, if it does not exist use 9530Sstevel@tonic-gate * the default/saved value (which was set in ip_build_hdrs_v6()) 9540Sstevel@tonic-gate * All this as per RFC 2922. 9550Sstevel@tonic-gate */ 9560Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_HOPLIMIT)) 9570Sstevel@tonic-gate sctp->sctp_ip6h->ip6_hops = hoplimit; 9580Sstevel@tonic-gate /* 9590Sstevel@tonic-gate * Set the IPv6 header payload length. 9600Sstevel@tonic-gate * If there's an ip6i_t included, don't count it in the length. 9610Sstevel@tonic-gate */ 9620Sstevel@tonic-gate sctp->sctp_ip6h->ip6_plen = sctp->sctp_hdr6_len - IPV6_HDR_LEN; 9630Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_HAS_IP6I) 9640Sstevel@tonic-gate sctp->sctp_ip6h->ip6_plen -= sizeof (ip6i_t); 9650Sstevel@tonic-gate /* 9660Sstevel@tonic-gate * If we're setting extension headers after a connection 9670Sstevel@tonic-gate * has been established, and if we have a routing header 9680Sstevel@tonic-gate * among the extension headers, call ip_massage_options_v6 to 9690Sstevel@tonic-gate * manipulate the routing header/ip6_dst set the checksum 9700Sstevel@tonic-gate * difference in the sctp header template. 9710Sstevel@tonic-gate * (This happens in sctp_connect_ipv6 if the routing header 9720Sstevel@tonic-gate * is set prior to the connect.) 9730Sstevel@tonic-gate */ 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) && 9760Sstevel@tonic-gate (sctp->sctp_sticky_ipp.ipp_fields & IPPF_RTHDR)) { 9770Sstevel@tonic-gate ip6_rthdr_t *rth; 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate rth = ip_find_rthdr_v6(sctp->sctp_ip6h, 9800Sstevel@tonic-gate (uint8_t *)sctp->sctp_sctph6); 9810Sstevel@tonic-gate if (rth != NULL) 9820Sstevel@tonic-gate (void) ip_massage_options_v6(sctp->sctp_ip6h, rth); 9830Sstevel@tonic-gate } 9840Sstevel@tonic-gate return (0); 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* 9880Sstevel@tonic-gate * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 9890Sstevel@tonic-gate */ 9900Sstevel@tonic-gate int 9910Sstevel@tonic-gate sctp_header_init_ipv6(sctp_t *sctp, int sleep) 9920Sstevel@tonic-gate { 9930Sstevel@tonic-gate sctp_hdr_t *sctph; 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * This is a simple initialization. If there's 9970Sstevel@tonic-gate * already a template, it should never be too small, 9980Sstevel@tonic-gate * so reuse it. Otherwise, allocate space for the new one. 9990Sstevel@tonic-gate * Ensure that there is enough space to "downgrade" the sctp_t 10000Sstevel@tonic-gate * to an IPv4 sctp_t. This requires having space for a full load 10010Sstevel@tonic-gate * of IPv4 options 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate if (sctp->sctp_iphc6 != NULL) { 10040Sstevel@tonic-gate ASSERT(sctp->sctp_iphc6_len >= 10050Sstevel@tonic-gate SCTP_MAX_COMBINED_HEADER_LENGTH); 10060Sstevel@tonic-gate bzero(sctp->sctp_iphc6, sctp->sctp_iphc6_len); 10070Sstevel@tonic-gate } else { 10080Sstevel@tonic-gate sctp->sctp_iphc6_len = SCTP_MAX_COMBINED_HEADER_LENGTH; 10090Sstevel@tonic-gate sctp->sctp_iphc6 = kmem_zalloc(sctp->sctp_iphc_len, sleep); 10100Sstevel@tonic-gate if (sctp->sctp_iphc6 == NULL) { 10110Sstevel@tonic-gate sctp->sctp_iphc6_len = 0; 10120Sstevel@tonic-gate return (ENOMEM); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate sctp->sctp_hdr6_len = IPV6_HDR_LEN + sizeof (sctp_hdr_t); 10160Sstevel@tonic-gate sctp->sctp_ip_hdr6_len = IPV6_HDR_LEN; 10170Sstevel@tonic-gate sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6; 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate /* Initialize the header template */ 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate sctp->sctp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 10220Sstevel@tonic-gate sctp->sctp_ip6h->ip6_plen = ntohs(sizeof (sctp_hdr_t)); 10230Sstevel@tonic-gate sctp->sctp_ip6h->ip6_nxt = IPPROTO_SCTP; 10240Sstevel@tonic-gate sctp->sctp_ip6h->ip6_hops = sctp_ipv6_hoplimit; 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate sctph = (sctp_hdr_t *)(sctp->sctp_iphc6 + IPV6_HDR_LEN); 10270Sstevel@tonic-gate sctp->sctp_sctph6 = sctph; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate return (0); 10300Sstevel@tonic-gate } 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate /* 10330Sstevel@tonic-gate * XXX implement more sophisticated logic 10340Sstevel@tonic-gate */ 10350Sstevel@tonic-gate void 10360Sstevel@tonic-gate sctp_set_hdraddrs(sctp_t *sctp) 10370Sstevel@tonic-gate { 10380Sstevel@tonic-gate sctp_faddr_t *fp; 10390Sstevel@tonic-gate int gotv4 = 0; 10400Sstevel@tonic-gate int gotv6 = 0; 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate ASSERT(sctp->sctp_faddrs != NULL); 10430Sstevel@tonic-gate ASSERT(sctp->sctp_nsaddrs > 0); 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate /* Set up using the primary first */ 10460Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) { 10470Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->faddr, 10480Sstevel@tonic-gate sctp->sctp_ipha->ipha_dst); 10490Sstevel@tonic-gate /* saddr may be unspec; make_mp() will handle this */ 10500Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->saddr, 10510Sstevel@tonic-gate sctp->sctp_ipha->ipha_src); 10520Sstevel@tonic-gate gotv4 = 1; 10530Sstevel@tonic-gate if (sctp->sctp_ipversion == IPV4_VERSION) { 10540Sstevel@tonic-gate goto copyports; 10550Sstevel@tonic-gate } 10560Sstevel@tonic-gate } else { 10570Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = sctp->sctp_primary->faddr; 10580Sstevel@tonic-gate /* saddr may be unspec; make_mp() will handle this */ 10590Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = sctp->sctp_primary->saddr; 10600Sstevel@tonic-gate gotv6 = 1; 10610Sstevel@tonic-gate } 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 10640Sstevel@tonic-gate if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 10650Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, 10660Sstevel@tonic-gate sctp->sctp_ipha->ipha_dst); 10670Sstevel@tonic-gate /* copy in the faddr_t's saddr */ 10680Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->saddr, 10690Sstevel@tonic-gate sctp->sctp_ipha->ipha_src); 10700Sstevel@tonic-gate gotv4 = 1; 10710Sstevel@tonic-gate if (sctp->sctp_ipversion == IPV4_VERSION || gotv6) { 10720Sstevel@tonic-gate break; 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate } else if (!gotv6) { 10750Sstevel@tonic-gate sctp->sctp_ip6h->ip6_dst = fp->faddr; 10760Sstevel@tonic-gate /* copy in the faddr_t's saddr */ 10770Sstevel@tonic-gate sctp->sctp_ip6h->ip6_src = fp->saddr; 10780Sstevel@tonic-gate gotv6 = 1; 10790Sstevel@tonic-gate if (gotv4) { 10800Sstevel@tonic-gate break; 10810Sstevel@tonic-gate } 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate copyports: 10860Sstevel@tonic-gate /* copy in the ports for good measure */ 10870Sstevel@tonic-gate sctp->sctp_sctph->sh_sport = sctp->sctp_lport; 10880Sstevel@tonic-gate sctp->sctp_sctph->sh_dport = sctp->sctp_fport; 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate sctp->sctp_sctph6->sh_sport = sctp->sctp_lport; 10910Sstevel@tonic-gate sctp->sctp_sctph6->sh_dport = sctp->sctp_fport; 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate void 10950Sstevel@tonic-gate sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp) 10960Sstevel@tonic-gate { 10970Sstevel@tonic-gate mblk_t *mp; 10980Sstevel@tonic-gate sctp_parm_hdr_t *ph; 10990Sstevel@tonic-gate size_t len; 11000Sstevel@tonic-gate int pad; 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate len = sizeof (*ph) + ntohs(uph->sph_len); 11030Sstevel@tonic-gate if ((pad = len % 4) != 0) { 11040Sstevel@tonic-gate pad = 4 - pad; 11050Sstevel@tonic-gate len += pad; 11060Sstevel@tonic-gate } 11070Sstevel@tonic-gate mp = allocb(len, BPRI_MED); 11080Sstevel@tonic-gate if (mp == NULL) { 11090Sstevel@tonic-gate return; 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate ph = (sctp_parm_hdr_t *)(mp->b_rptr); 11130Sstevel@tonic-gate ph->sph_type = htons(PARM_UNRECOGNIZED); 11140Sstevel@tonic-gate ph->sph_len = htons(len - pad); 11150Sstevel@tonic-gate 11160Sstevel@tonic-gate /* copy in the unrecognized parameter */ 11170Sstevel@tonic-gate bcopy(uph, ph + 1, ntohs(uph->sph_len)); 11180Sstevel@tonic-gate 11190Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + len; 11200Sstevel@tonic-gate if (*errmp != NULL) { 11210Sstevel@tonic-gate linkb(*errmp, mp); 11220Sstevel@tonic-gate } else { 11230Sstevel@tonic-gate *errmp = mp; 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate } 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate /* 11280Sstevel@tonic-gate * o Bounds checking 11290Sstevel@tonic-gate * o Updates remaining 11300Sstevel@tonic-gate * o Checks alignment 11310Sstevel@tonic-gate */ 11320Sstevel@tonic-gate sctp_parm_hdr_t * 11330Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining) 11340Sstevel@tonic-gate { 11350Sstevel@tonic-gate int pad; 11360Sstevel@tonic-gate uint16_t len; 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate len = ntohs(current->sph_len); 11390Sstevel@tonic-gate *remaining -= len; 11400Sstevel@tonic-gate if (*remaining < sizeof (*current) || len < sizeof (*current)) { 11410Sstevel@tonic-gate return (NULL); 11420Sstevel@tonic-gate } 11430Sstevel@tonic-gate if ((pad = len & (SCTP_ALIGN - 1)) != 0) { 11440Sstevel@tonic-gate pad = SCTP_ALIGN - pad; 11450Sstevel@tonic-gate *remaining -= pad; 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate /*LINTED pointer cast may result in improper alignment*/ 11480Sstevel@tonic-gate current = (sctp_parm_hdr_t *)((char *)current + len + pad); 11490Sstevel@tonic-gate return (current); 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate /* 11530Sstevel@tonic-gate * Sets the address parameters given in the INIT chunk into sctp's 11540Sstevel@tonic-gate * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are 11550Sstevel@tonic-gate * no address parameters in the INIT chunk, a single faddr is created 11560Sstevel@tonic-gate * from the ip hdr at the beginning of pkt. 11570Sstevel@tonic-gate * If there already are existing addresses hanging from sctp, merge 11580Sstevel@tonic-gate * them in, if the old info contains addresses which are not present 11590Sstevel@tonic-gate * in this new info, get rid of them, and clean the pointers if there's 11600Sstevel@tonic-gate * messages which have this as their target address. 11610Sstevel@tonic-gate * 1162*432Svi117747 * We also re-adjust the source address list here since the list may 1163*432Svi117747 * contain more than what is actually part of the association. If 1164*432Svi117747 * we get here from sctp_send_cookie_echo(), we are on the active 1165*432Svi117747 * side and psctp will be NULL and ich will be the INIT-ACK chunk. 1166*432Svi117747 * If we get here from sctp_accept_comm(), ich will be the INIT chunk 1167*432Svi117747 * and psctp will the listening endpoint. 1168*432Svi117747 * 1169*432Svi117747 * INIT processing: When processing the INIT we inherit the src address 1170*432Svi117747 * list from the listener. For a loopback or linklocal association, we 1171*432Svi117747 * delete the list and just take the address from the IP header (since 1172*432Svi117747 * that's how we created the INIT-ACK). Additionally, for loopback we 1173*432Svi117747 * ignore the address params in the INIT. For determining which address 1174*432Svi117747 * types were sent in the INIT-ACK we follow the same logic as in 1175*432Svi117747 * creating the INIT-ACK. We delete addresses of the type that are not 1176*432Svi117747 * supported by the peer. 1177*432Svi117747 * 1178*432Svi117747 * INIT-ACK processing: When processing the INIT-ACK since we had not 1179*432Svi117747 * included addr params for loopback or linklocal addresses when creating 1180*432Svi117747 * the INIT, we just use the address from the IP header. Further, for 1181*432Svi117747 * loopback we ignore the addr param list. We mark addresses of the 1182*432Svi117747 * type not supported by the peer as unconfirmed. 1183*432Svi117747 * 1184*432Svi117747 * In case of INIT processing we look for supported address types in the 1185*432Svi117747 * supported address param, if present. In both cases the address type in 1186*432Svi117747 * the IP header is supported as well as types for addresses in the param 1187*432Svi117747 * list, if any. 1188*432Svi117747 * 1189*432Svi117747 * Once we have the supported address types sctp_check_saddr() runs through 1190*432Svi117747 * the source address list and deletes or marks as unconfirmed address of 1191*432Svi117747 * types not supported by the peer. 1192*432Svi117747 * 11930Sstevel@tonic-gate * Returns 0 on success, sys errno on failure 11940Sstevel@tonic-gate */ 11950Sstevel@tonic-gate int 11960Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt, 11970Sstevel@tonic-gate sctp_chunk_hdr_t *ich, uint_t *sctp_options) 11980Sstevel@tonic-gate { 11990Sstevel@tonic-gate sctp_init_chunk_t *init; 12000Sstevel@tonic-gate ipha_t *iph; 12010Sstevel@tonic-gate ip6_t *ip6h; 1202*432Svi117747 in6_addr_t hdrsaddr[1]; 1203*432Svi117747 in6_addr_t hdrdaddr[1]; 12040Sstevel@tonic-gate sctp_parm_hdr_t *ph; 12050Sstevel@tonic-gate ssize_t remaining; 12060Sstevel@tonic-gate int isv4; 12070Sstevel@tonic-gate int err; 12080Sstevel@tonic-gate sctp_faddr_t *fp; 1209*432Svi117747 int supp_af = 0; 1210*432Svi117747 boolean_t check_saddr = B_TRUE; 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate if (sctp_options != NULL) 12130Sstevel@tonic-gate *sctp_options = 0; 12140Sstevel@tonic-gate 1215*432Svi117747 /* extract the address from the IP header */ 1216*432Svi117747 isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 1217*432Svi117747 if (isv4) { 1218*432Svi117747 iph = (ipha_t *)pkt->b_rptr; 1219*432Svi117747 IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr); 1220*432Svi117747 IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr); 1221*432Svi117747 supp_af |= PARM_SUPP_V4; 1222*432Svi117747 } else { 1223*432Svi117747 ip6h = (ip6_t *)pkt->b_rptr; 1224*432Svi117747 hdrsaddr[0] = ip6h->ip6_src; 1225*432Svi117747 hdrdaddr[0] = ip6h->ip6_dst; 1226*432Svi117747 supp_af |= PARM_SUPP_V6; 1227*432Svi117747 } 1228*432Svi117747 1229*432Svi117747 /* 1230*432Svi117747 * Unfortunately, we can't delay this because adding an faddr 1231*432Svi117747 * looks for the presence of the source address (from the ire 1232*432Svi117747 * for the faddr) in the source address list. We could have 1233*432Svi117747 * delayed this if, say, this was a loopback/linklocal connection. 1234*432Svi117747 * Now, we just end up nuking this list and taking the addr from 1235*432Svi117747 * the IP header for loopback/linklocal. 1236*432Svi117747 */ 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 } 1244*432Svi117747 /* 1245*432Svi117747 * We will add the faddr before parsing the address list as this 1246*432Svi117747 * might be a loopback connection and we would not have to 1247*432Svi117747 * go through the list. 1248*432Svi117747 * 1249*432Svi117747 * Make sure the header's addr is in the list 1250*432Svi117747 */ 1251*432Svi117747 fp = sctp_lookup_faddr(sctp, hdrsaddr); 1252*432Svi117747 if (fp == NULL) { 1253*432Svi117747 /* not included; add it now */ 1254*432Svi117747 if (sctp_add_faddr_first(sctp, hdrsaddr, KM_NOSLEEP) == -1) 1255*432Svi117747 return (ENOMEM); 12560Sstevel@tonic-gate 1257*432Svi117747 /* sctp_faddrs will be the hdr addr */ 1258*432Svi117747 fp = sctp->sctp_faddrs; 12590Sstevel@tonic-gate } 1260*432Svi117747 /* make the header addr the primary */ 1261*432Svi117747 sctp->sctp_primary = fp; 1262*432Svi117747 sctp->sctp_current = fp; 1263*432Svi117747 sctp->sctp_mss = fp->sfa_pmss; 12640Sstevel@tonic-gate 1265*432Svi117747 /* For loopback connections & linklocal get address from the header */ 1266*432Svi117747 if (sctp->sctp_loopback || sctp->sctp_linklocal) { 1267*432Svi117747 if (sctp->sctp_nsaddrs != 0) 1268*432Svi117747 sctp_free_saddrs(sctp); 1269*432Svi117747 if ((err = sctp_saddr_add_addr(sctp, hdrdaddr)) != 0) 1270*432Svi117747 return (err); 1271*432Svi117747 /* For loopback ignore address list */ 1272*432Svi117747 if (sctp->sctp_loopback) 1273*432Svi117747 return (0); 1274*432Svi117747 check_saddr = B_FALSE; 1275*432Svi117747 } 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate /* Walk the params in the INIT [ACK], pulling out addr params */ 12780Sstevel@tonic-gate remaining = ntohs(ich->sch_len) - sizeof (*ich) - 12790Sstevel@tonic-gate sizeof (sctp_init_chunk_t); 12800Sstevel@tonic-gate if (remaining < sizeof (*ph)) { 1281*432Svi117747 if (check_saddr) { 1282*432Svi117747 sctp_check_saddr(sctp, supp_af, psctp == NULL ? 1283*432Svi117747 B_FALSE : B_TRUE); 1284*432Svi117747 } 1285*432Svi117747 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr) != NULL); 1286*432Svi117747 return (0); 12870Sstevel@tonic-gate } 1288*432Svi117747 12890Sstevel@tonic-gate init = (sctp_init_chunk_t *)(ich + 1); 12900Sstevel@tonic-gate ph = (sctp_parm_hdr_t *)(init + 1); 12910Sstevel@tonic-gate 1292*432Svi117747 /* params will have already been byteordered when validating */ 12930Sstevel@tonic-gate while (ph != NULL) { 1294*432Svi117747 if (ph->sph_type == htons(PARM_SUPP_ADDRS)) { 1295*432Svi117747 int plen; 1296*432Svi117747 uint16_t *p; 1297*432Svi117747 uint16_t addrtype; 1298*432Svi117747 1299*432Svi117747 ASSERT(psctp != NULL); 1300*432Svi117747 plen = ntohs(ph->sph_len); 1301*432Svi117747 p = (uint16_t *)(ph + 1); 1302*432Svi117747 while (plen > 0) { 1303*432Svi117747 addrtype = ntohs(*p); 1304*432Svi117747 switch (addrtype) { 1305*432Svi117747 case PARM_ADDR6: 1306*432Svi117747 supp_af |= PARM_SUPP_V6; 1307*432Svi117747 break; 1308*432Svi117747 case PARM_ADDR4: 1309*432Svi117747 supp_af |= PARM_SUPP_V4; 1310*432Svi117747 break; 1311*432Svi117747 default: 1312*432Svi117747 break; 1313*432Svi117747 } 1314*432Svi117747 p++; 1315*432Svi117747 plen -= sizeof (*p); 1316*432Svi117747 } 1317*432Svi117747 } else if (ph->sph_type == htons(PARM_ADDR4)) { 13180Sstevel@tonic-gate if (remaining >= PARM_ADDR4_LEN) { 13190Sstevel@tonic-gate in6_addr_t addr; 13200Sstevel@tonic-gate ipaddr_t ta; 13210Sstevel@tonic-gate 1322*432Svi117747 supp_af |= PARM_SUPP_V4; 13230Sstevel@tonic-gate /* 13240Sstevel@tonic-gate * Screen out broad/multicasts & loopback. 13250Sstevel@tonic-gate * If the endpoint only accepts v6 address, 13260Sstevel@tonic-gate * go to the next one. 13270Sstevel@tonic-gate */ 13280Sstevel@tonic-gate bcopy(ph + 1, &ta, sizeof (ta)); 13290Sstevel@tonic-gate if (ta == 0 || 13300Sstevel@tonic-gate ta == INADDR_BROADCAST || 13310Sstevel@tonic-gate ta == htonl(INADDR_LOOPBACK) || 13320Sstevel@tonic-gate IN_MULTICAST(ta) || 13330Sstevel@tonic-gate sctp->sctp_connp->conn_ipv6_v6only) { 13340Sstevel@tonic-gate goto next; 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate /* 13370Sstevel@tonic-gate * XXX also need to check for subnet 13380Sstevel@tonic-gate * broadcasts. This should probably 13390Sstevel@tonic-gate * wait until we have full access 13400Sstevel@tonic-gate * to the ILL tables. 13410Sstevel@tonic-gate */ 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED((struct in_addr *) 13440Sstevel@tonic-gate (ph + 1), &addr); 13450Sstevel@tonic-gate /* Check for duplicate. */ 13460Sstevel@tonic-gate if (sctp_lookup_faddr(sctp, &addr) != NULL) 13470Sstevel@tonic-gate goto next; 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate /* OK, add it to the faddr set */ 13500Sstevel@tonic-gate if (sctp_add_faddr(sctp, &addr, 13510Sstevel@tonic-gate KM_NOSLEEP) != 0) { 13520Sstevel@tonic-gate return (ENOMEM); 13530Sstevel@tonic-gate } 13540Sstevel@tonic-gate } 13550Sstevel@tonic-gate } else if (ph->sph_type == htons(PARM_ADDR6) && 13560Sstevel@tonic-gate sctp->sctp_family == AF_INET6) { 13570Sstevel@tonic-gate /* An v4 socket should not take v6 addresses. */ 13580Sstevel@tonic-gate if (remaining >= PARM_ADDR6_LEN) { 13590Sstevel@tonic-gate in6_addr_t *addr6; 13600Sstevel@tonic-gate 1361*432Svi117747 supp_af |= PARM_SUPP_V6; 13620Sstevel@tonic-gate addr6 = (in6_addr_t *)(ph + 1); 13630Sstevel@tonic-gate /* 13640Sstevel@tonic-gate * Screen out link locals, mcast, loopback 13650Sstevel@tonic-gate * and bogus v6 address. 13660Sstevel@tonic-gate */ 13670Sstevel@tonic-gate if (IN6_IS_ADDR_LINKLOCAL(addr6) || 13680Sstevel@tonic-gate IN6_IS_ADDR_MULTICAST(addr6) || 13690Sstevel@tonic-gate IN6_IS_ADDR_LOOPBACK(addr6) || 13700Sstevel@tonic-gate IN6_IS_ADDR_V4MAPPED(addr6)) { 13710Sstevel@tonic-gate goto next; 13720Sstevel@tonic-gate } 13730Sstevel@tonic-gate /* Check for duplicate. */ 13740Sstevel@tonic-gate if (sctp_lookup_faddr(sctp, addr6) != NULL) 13750Sstevel@tonic-gate goto next; 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate if (sctp_add_faddr(sctp, 13780Sstevel@tonic-gate (in6_addr_t *)(ph + 1), KM_NOSLEEP) != 0) { 13790Sstevel@tonic-gate return (ENOMEM); 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate } 13820Sstevel@tonic-gate } else if (ph->sph_type == htons(PARM_FORWARD_TSN)) { 13830Sstevel@tonic-gate if (sctp_options != NULL) 13840Sstevel@tonic-gate *sctp_options |= SCTP_PRSCTP_OPTION; 13850Sstevel@tonic-gate } /* else; skip */ 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate next: 13880Sstevel@tonic-gate ph = sctp_next_parm(ph, &remaining); 13890Sstevel@tonic-gate } 1390*432Svi117747 if (check_saddr) { 1391*432Svi117747 sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE : 1392*432Svi117747 B_TRUE); 13930Sstevel@tonic-gate } 1394*432Svi117747 ASSERT(sctp_saddr_lookup(sctp, hdrdaddr) != NULL); 13950Sstevel@tonic-gate return (0); 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate /* 13990Sstevel@tonic-gate * Returns 0 if the check failed and the restart should be refused, 14000Sstevel@tonic-gate * 1 if the check succeeded. 14010Sstevel@tonic-gate */ 14020Sstevel@tonic-gate int 14030Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports, 14040Sstevel@tonic-gate int sleep) 14050Sstevel@tonic-gate { 14060Sstevel@tonic-gate sctp_faddr_t *fp, *fpa, *fphead = NULL; 14070Sstevel@tonic-gate sctp_parm_hdr_t *ph; 14080Sstevel@tonic-gate ssize_t remaining; 14090Sstevel@tonic-gate int isv4; 14100Sstevel@tonic-gate ipha_t *iph; 14110Sstevel@tonic-gate ip6_t *ip6h; 14120Sstevel@tonic-gate in6_addr_t hdraddr[1]; 14130Sstevel@tonic-gate int retval = 0; 14140Sstevel@tonic-gate sctp_tf_t *tf; 14150Sstevel@tonic-gate sctp_t *sctp; 14160Sstevel@tonic-gate int compres; 14170Sstevel@tonic-gate sctp_init_chunk_t *init; 14180Sstevel@tonic-gate int nadded = 0; 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate /* extract the address from the IP header */ 14210Sstevel@tonic-gate isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION); 14220Sstevel@tonic-gate if (isv4) { 14230Sstevel@tonic-gate iph = (ipha_t *)pkt->b_rptr; 14240Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr); 14250Sstevel@tonic-gate } else { 14260Sstevel@tonic-gate ip6h = (ip6_t *)pkt->b_rptr; 14270Sstevel@tonic-gate hdraddr[0] = ip6h->ip6_src; 14280Sstevel@tonic-gate } 14290Sstevel@tonic-gate 14300Sstevel@tonic-gate /* Walk the params in the INIT [ACK], pulling out addr params */ 14310Sstevel@tonic-gate remaining = ntohs(ich->sch_len) - sizeof (*ich) - 14320Sstevel@tonic-gate sizeof (sctp_init_chunk_t); 14330Sstevel@tonic-gate if (remaining < sizeof (*ph)) { 14340Sstevel@tonic-gate /* no parameters; restart OK */ 14350Sstevel@tonic-gate return (1); 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate init = (sctp_init_chunk_t *)(ich + 1); 14380Sstevel@tonic-gate ph = (sctp_parm_hdr_t *)(init + 1); 14390Sstevel@tonic-gate 14400Sstevel@tonic-gate while (ph != NULL) { 14410Sstevel@tonic-gate /* params will have already been byteordered when validating */ 14420Sstevel@tonic-gate if (ph->sph_type == htons(PARM_ADDR4)) { 14430Sstevel@tonic-gate if (remaining >= PARM_ADDR4_LEN) { 14440Sstevel@tonic-gate in6_addr_t addr; 14450Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED((struct in_addr *) 14460Sstevel@tonic-gate (ph + 1), &addr); 14470Sstevel@tonic-gate fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 14480Sstevel@tonic-gate sleep); 14490Sstevel@tonic-gate if (!fpa) { 14500Sstevel@tonic-gate goto done; 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate bzero(fpa, sizeof (*fpa)); 14530Sstevel@tonic-gate fpa->faddr = addr; 14540Sstevel@tonic-gate fpa->next = NULL; 14550Sstevel@tonic-gate } 14560Sstevel@tonic-gate } else if (ph->sph_type == htons(PARM_ADDR6)) { 14570Sstevel@tonic-gate if (remaining >= PARM_ADDR6_LEN) { 14580Sstevel@tonic-gate fpa = kmem_cache_alloc(sctp_kmem_faddr_cache, 14590Sstevel@tonic-gate sleep); 14600Sstevel@tonic-gate if (!fpa) { 14610Sstevel@tonic-gate goto done; 14620Sstevel@tonic-gate } 14630Sstevel@tonic-gate bzero(fpa, sizeof (*fpa)); 14640Sstevel@tonic-gate bcopy(ph + 1, &fpa->faddr, 14650Sstevel@tonic-gate sizeof (fpa->faddr)); 14660Sstevel@tonic-gate fpa->next = NULL; 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate } else { 14690Sstevel@tonic-gate /* else not addr param; skip */ 14700Sstevel@tonic-gate fpa = NULL; 14710Sstevel@tonic-gate } 14720Sstevel@tonic-gate /* link in the new addr, if it was an addr param */ 14730Sstevel@tonic-gate if (fpa) { 14740Sstevel@tonic-gate if (!fphead) { 14750Sstevel@tonic-gate fphead = fpa; 14760Sstevel@tonic-gate fp = fphead; 14770Sstevel@tonic-gate } else { 14780Sstevel@tonic-gate fp->next = fpa; 14790Sstevel@tonic-gate fp = fpa; 14800Sstevel@tonic-gate } 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate ph = sctp_next_parm(ph, &remaining); 14840Sstevel@tonic-gate } 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate if (fphead == NULL) { 14870Sstevel@tonic-gate /* no addr parameters; restart OK */ 14880Sstevel@tonic-gate return (1); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate /* 14920Sstevel@tonic-gate * got at least one; make sure the header's addr is 14930Sstevel@tonic-gate * in the list 14940Sstevel@tonic-gate */ 14950Sstevel@tonic-gate fp = sctp_lookup_faddr_nosctp(fphead, hdraddr); 14960Sstevel@tonic-gate if (!fp) { 14970Sstevel@tonic-gate /* not included; add it now */ 14980Sstevel@tonic-gate fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep); 14990Sstevel@tonic-gate if (!fp) { 15000Sstevel@tonic-gate goto done; 15010Sstevel@tonic-gate } 15020Sstevel@tonic-gate bzero(fp, sizeof (*fp)); 15030Sstevel@tonic-gate fp->faddr = *hdraddr; 15040Sstevel@tonic-gate fp->next = fphead; 15050Sstevel@tonic-gate fphead = fp; 15060Sstevel@tonic-gate } 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate /* 15090Sstevel@tonic-gate * Now, we can finally do the check: For each sctp instance 15100Sstevel@tonic-gate * on the hash line for ports, compare its faddr set against 15110Sstevel@tonic-gate * the new one. If the new one is a strict subset of any 15120Sstevel@tonic-gate * existing sctp's faddrs, the restart is OK. However, if there 15130Sstevel@tonic-gate * is an overlap, this could be an attack, so return failure. 15140Sstevel@tonic-gate * If all sctp's faddrs are disjoint, this is a legitimate new 15150Sstevel@tonic-gate * association. 15160Sstevel@tonic-gate */ 15170Sstevel@tonic-gate tf = &(sctp_conn_fanout[SCTP_CONN_HASH(ports)]); 15180Sstevel@tonic-gate mutex_enter(&tf->tf_lock); 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) { 15210Sstevel@tonic-gate if (ports != sctp->sctp_ports) { 15220Sstevel@tonic-gate continue; 15230Sstevel@tonic-gate } 15240Sstevel@tonic-gate compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs); 15250Sstevel@tonic-gate if (compres <= SCTP_ADDR_SUBSET) { 15260Sstevel@tonic-gate retval = 1; 15270Sstevel@tonic-gate mutex_exit(&tf->tf_lock); 15280Sstevel@tonic-gate goto done; 15290Sstevel@tonic-gate } 15300Sstevel@tonic-gate if (compres == SCTP_ADDR_OVERLAP) { 15310Sstevel@tonic-gate dprint(1, 15320Sstevel@tonic-gate ("new assoc from %x:%x:%x:%x overlaps with %p\n", 15330Sstevel@tonic-gate SCTP_PRINTADDR(*hdraddr), sctp)); 15340Sstevel@tonic-gate /* 15350Sstevel@tonic-gate * While we still hold the lock, we need to 15360Sstevel@tonic-gate * figure out which addresses have been 15370Sstevel@tonic-gate * added so we can include them in the abort 15380Sstevel@tonic-gate * we will send back. Since these faddrs will 15390Sstevel@tonic-gate * never be used, we overload the rto field 15400Sstevel@tonic-gate * here, setting it to 0 if the address was 15410Sstevel@tonic-gate * not added, 1 if it was added. 15420Sstevel@tonic-gate */ 15430Sstevel@tonic-gate for (fp = fphead; fp; fp = fp->next) { 15440Sstevel@tonic-gate if (sctp_lookup_faddr(sctp, &fp->faddr)) { 15450Sstevel@tonic-gate fp->rto = 0; 15460Sstevel@tonic-gate } else { 15470Sstevel@tonic-gate fp->rto = 1; 15480Sstevel@tonic-gate nadded++; 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate mutex_exit(&tf->tf_lock); 15520Sstevel@tonic-gate goto done; 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate } 15550Sstevel@tonic-gate mutex_exit(&tf->tf_lock); 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate /* All faddrs are disjoint; legit new association */ 15580Sstevel@tonic-gate retval = 1; 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate done: 15610Sstevel@tonic-gate /* If are attempted adds, send back an abort listing the addrs */ 15620Sstevel@tonic-gate if (nadded > 0) { 15630Sstevel@tonic-gate void *dtail; 15640Sstevel@tonic-gate size_t dlen; 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP); 15670Sstevel@tonic-gate if (dtail == NULL) { 15680Sstevel@tonic-gate goto cleanup; 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate ph = dtail; 15720Sstevel@tonic-gate dlen = 0; 15730Sstevel@tonic-gate for (fp = fphead; fp; fp = fp->next) { 15740Sstevel@tonic-gate if (fp->rto == 0) { 15750Sstevel@tonic-gate continue; 15760Sstevel@tonic-gate } 15770Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) { 15780Sstevel@tonic-gate ipaddr_t addr4; 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate ph->sph_type = htons(PARM_ADDR4); 15810Sstevel@tonic-gate ph->sph_len = htons(PARM_ADDR4_LEN); 15820Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4); 15830Sstevel@tonic-gate ph++; 15840Sstevel@tonic-gate bcopy(&addr4, ph, sizeof (addr4)); 15850Sstevel@tonic-gate ph = (sctp_parm_hdr_t *) 15860Sstevel@tonic-gate ((char *)ph + sizeof (addr4)); 15870Sstevel@tonic-gate dlen += PARM_ADDR4_LEN; 15880Sstevel@tonic-gate } else { 15890Sstevel@tonic-gate ph->sph_type = htons(PARM_ADDR6); 15900Sstevel@tonic-gate ph->sph_len = htons(PARM_ADDR6_LEN); 15910Sstevel@tonic-gate ph++; 15920Sstevel@tonic-gate bcopy(&fp->faddr, ph, sizeof (fp->faddr)); 15930Sstevel@tonic-gate ph = (sctp_parm_hdr_t *) 15940Sstevel@tonic-gate ((char *)ph + sizeof (fp->faddr)); 15950Sstevel@tonic-gate dlen += PARM_ADDR6_LEN; 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate /* Send off the abort */ 16000Sstevel@tonic-gate sctp_send_abort(sctp, sctp_init2vtag(ich), 16010Sstevel@tonic-gate SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE); 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate kmem_free(dtail, PARM_ADDR6_LEN * nadded); 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate cleanup: 16070Sstevel@tonic-gate /* Clean up */ 16080Sstevel@tonic-gate if (fphead) { 16090Sstevel@tonic-gate sctp_faddr_t *fpn; 16100Sstevel@tonic-gate for (fp = fphead; fp; fp = fpn) { 16110Sstevel@tonic-gate fpn = fp->next; 16120Sstevel@tonic-gate kmem_cache_free(sctp_kmem_faddr_cache, fp); 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate return (retval); 16170Sstevel@tonic-gate } 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate void 16200Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp) 16210Sstevel@tonic-gate { 16220Sstevel@tonic-gate sctp_faddr_t *fp; 16230Sstevel@tonic-gate 16240Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp; fp = fp->next) { 16250Sstevel@tonic-gate fp->ssthresh = sctp_initial_mtu; 16260Sstevel@tonic-gate fp->cwnd = fp->sfa_pmss * sctp_slow_start_initial; 16270Sstevel@tonic-gate fp->suna = 0; 16280Sstevel@tonic-gate fp->pba = 0; 16290Sstevel@tonic-gate } 16300Sstevel@tonic-gate } 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate /* 16330Sstevel@tonic-gate * Return zero if the buffers are identical in length and content. 16340Sstevel@tonic-gate * This is used for comparing extension header buffers. 16350Sstevel@tonic-gate * Note that an extension header would be declared different 16360Sstevel@tonic-gate * even if all that changed was the next header value in that header i.e. 16370Sstevel@tonic-gate * what really changed is the next extension header. 16380Sstevel@tonic-gate */ 16390Sstevel@tonic-gate boolean_t 16400Sstevel@tonic-gate sctp_cmpbuf(void *a, uint_t alen, boolean_t b_valid, void *b, uint_t blen) 16410Sstevel@tonic-gate { 16420Sstevel@tonic-gate if (!b_valid) 16430Sstevel@tonic-gate blen = 0; 16440Sstevel@tonic-gate 16450Sstevel@tonic-gate if (alen != blen) 16460Sstevel@tonic-gate return (B_TRUE); 16470Sstevel@tonic-gate if (alen == 0) 16480Sstevel@tonic-gate return (B_FALSE); /* Both zero length */ 16490Sstevel@tonic-gate return (bcmp(a, b, alen)); 16500Sstevel@tonic-gate } 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate /* 16530Sstevel@tonic-gate * Preallocate memory for sctp_savebuf(). Returns B_TRUE if ok. 16540Sstevel@tonic-gate * Return B_FALSE if memory allocation fails - don't change any state! 16550Sstevel@tonic-gate */ 16560Sstevel@tonic-gate boolean_t 16570Sstevel@tonic-gate sctp_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 16580Sstevel@tonic-gate void *src, uint_t srclen) 16590Sstevel@tonic-gate { 16600Sstevel@tonic-gate void *dst; 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate if (!src_valid) 16630Sstevel@tonic-gate srclen = 0; 16640Sstevel@tonic-gate 16650Sstevel@tonic-gate ASSERT(*dstlenp == 0); 16660Sstevel@tonic-gate if (src != NULL && srclen != 0) { 16670Sstevel@tonic-gate dst = mi_zalloc(srclen); 16680Sstevel@tonic-gate if (dst == NULL) 16690Sstevel@tonic-gate return (B_FALSE); 16700Sstevel@tonic-gate } else { 16710Sstevel@tonic-gate dst = NULL; 16720Sstevel@tonic-gate } 16730Sstevel@tonic-gate if (*dstp != NULL) { 16740Sstevel@tonic-gate mi_free(*dstp); 16750Sstevel@tonic-gate *dstp = NULL; 16760Sstevel@tonic-gate *dstlenp = 0; 16770Sstevel@tonic-gate } 16780Sstevel@tonic-gate *dstp = dst; 16790Sstevel@tonic-gate if (dst != NULL) 16800Sstevel@tonic-gate *dstlenp = srclen; 16810Sstevel@tonic-gate else 16820Sstevel@tonic-gate *dstlenp = 0; 16830Sstevel@tonic-gate return (B_TRUE); 16840Sstevel@tonic-gate } 16850Sstevel@tonic-gate 16860Sstevel@tonic-gate /* 16870Sstevel@tonic-gate * Replace what is in *dst, *dstlen with the source. 16880Sstevel@tonic-gate * Assumes sctp_allocbuf has already been called. 16890Sstevel@tonic-gate */ 16900Sstevel@tonic-gate void 16910Sstevel@tonic-gate sctp_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 16920Sstevel@tonic-gate void *src, uint_t srclen) 16930Sstevel@tonic-gate { 16940Sstevel@tonic-gate if (!src_valid) 16950Sstevel@tonic-gate srclen = 0; 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate ASSERT(*dstlenp == srclen); 16980Sstevel@tonic-gate if (src != NULL && srclen != 0) { 16990Sstevel@tonic-gate bcopy(src, *dstp, srclen); 17000Sstevel@tonic-gate } 17010Sstevel@tonic-gate } 17020Sstevel@tonic-gate 17030Sstevel@tonic-gate static void 17040Sstevel@tonic-gate sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr) 17050Sstevel@tonic-gate { 17060Sstevel@tonic-gate bcopy(addr, &fp->faddr, sizeof (*addr)); 17070Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(addr)) { 17080Sstevel@tonic-gate fp->isv4 = 1; 17090Sstevel@tonic-gate /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 17100Sstevel@tonic-gate fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr_len) & 17110Sstevel@tonic-gate ~(SCTP_ALIGN - 1); 17120Sstevel@tonic-gate } else { 17130Sstevel@tonic-gate fp->isv4 = 0; 17140Sstevel@tonic-gate fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr6_len) & 17150Sstevel@tonic-gate ~(SCTP_ALIGN - 1); 17160Sstevel@tonic-gate } 17170Sstevel@tonic-gate fp->cwnd = sctp_slow_start_initial * fp->sfa_pmss; 17180Sstevel@tonic-gate fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max); 17190Sstevel@tonic-gate fp->srtt = -1; 17200Sstevel@tonic-gate fp->rtt_updates = 0; 17210Sstevel@tonic-gate fp->strikes = 0; 17220Sstevel@tonic-gate fp->max_retr = sctp->sctp_pp_max_rxt; 17230Sstevel@tonic-gate /* Mark it as not confirmed. */ 17240Sstevel@tonic-gate fp->state = SCTP_FADDRS_UNCONFIRMED; 17250Sstevel@tonic-gate fp->hb_interval = sctp->sctp_hb_interval; 17260Sstevel@tonic-gate fp->ssthresh = sctp_initial_ssthresh; 17270Sstevel@tonic-gate fp->suna = 0; 17280Sstevel@tonic-gate fp->pba = 0; 17290Sstevel@tonic-gate fp->acked = 0; 17300Sstevel@tonic-gate fp->lastactive = lbolt64; 17310Sstevel@tonic-gate fp->timer_mp = NULL; 17320Sstevel@tonic-gate fp->hb_pending = B_FALSE; 17330Sstevel@tonic-gate fp->timer_running = 0; 17340Sstevel@tonic-gate fp->df = 1; 17350Sstevel@tonic-gate fp->pmtu_discovered = 0; 17360Sstevel@tonic-gate fp->rc_timer_mp = NULL; 17370Sstevel@tonic-gate fp->rc_timer_running = 0; 17380Sstevel@tonic-gate fp->next = NULL; 17390Sstevel@tonic-gate fp->ire = NULL; 17400Sstevel@tonic-gate fp->T3expire = 0; 17410Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret, 17420Sstevel@tonic-gate sizeof (fp->hb_secret)); 17430Sstevel@tonic-gate fp->hb_expiry = lbolt64; 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate sctp_ire2faddr(sctp, fp); 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate /*ARGSUSED*/ 17490Sstevel@tonic-gate static void 17500Sstevel@tonic-gate faddr_destructor(void *buf, void *cdrarg) 17510Sstevel@tonic-gate { 17520Sstevel@tonic-gate sctp_faddr_t *fp = buf; 17530Sstevel@tonic-gate 17540Sstevel@tonic-gate ASSERT(fp->timer_mp == NULL); 17550Sstevel@tonic-gate ASSERT(fp->timer_running == 0); 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate ASSERT(fp->rc_timer_mp == NULL); 17580Sstevel@tonic-gate ASSERT(fp->rc_timer_running == 0); 17590Sstevel@tonic-gate } 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate void 17620Sstevel@tonic-gate sctp_faddr_init() 17630Sstevel@tonic-gate { 17640Sstevel@tonic-gate sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache", 17650Sstevel@tonic-gate sizeof (sctp_faddr_t), 0, NULL, faddr_destructor, 17660Sstevel@tonic-gate NULL, NULL, NULL, 0); 17670Sstevel@tonic-gate } 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate void 17700Sstevel@tonic-gate sctp_faddr_fini() 17710Sstevel@tonic-gate { 17720Sstevel@tonic-gate kmem_cache_destroy(sctp_kmem_faddr_cache); 17730Sstevel@tonic-gate } 1774