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
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/stream.h>
31*1676Sjpk #include <sys/strsubr.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>
37*1676Sjpk #include <sys/tsol/tndb.h>
38*1676Sjpk #include <sys/tsol/tnet.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <netinet/ip6.h>
420Sstevel@tonic-gate #include <netinet/sctp.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <inet/common.h>
450Sstevel@tonic-gate #include <inet/ip.h>
460Sstevel@tonic-gate #include <inet/ip6.h>
470Sstevel@tonic-gate #include <inet/ip_ire.h>
480Sstevel@tonic-gate #include <inet/mib2.h>
490Sstevel@tonic-gate #include <inet/nd.h>
500Sstevel@tonic-gate #include <inet/optcom.h>
510Sstevel@tonic-gate #include <inet/sctp_ip.h>
520Sstevel@tonic-gate #include <inet/ipclassifier.h>
53*1676Sjpk 
540Sstevel@tonic-gate #include "sctp_impl.h"
550Sstevel@tonic-gate #include "sctp_addr.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_faddr_cache;
580Sstevel@tonic-gate static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* Set the source address.  Refer to comments in sctp_ire2faddr(). */
610Sstevel@tonic-gate static void
620Sstevel@tonic-gate set_saddr(sctp_t *sctp, sctp_faddr_t *fp, boolean_t v6)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	if (sctp->sctp_bound_to_all) {
650Sstevel@tonic-gate 		V6_SET_ZERO(fp->saddr);
660Sstevel@tonic-gate 	} else {
670Sstevel@tonic-gate 		fp->saddr = sctp_get_valid_addr(sctp, v6);
680Sstevel@tonic-gate 		if (!v6 && IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr) ||
690Sstevel@tonic-gate 		    v6 && IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
700Sstevel@tonic-gate 			fp->state = SCTP_FADDRS_UNREACH;
710Sstevel@tonic-gate 			/* Disable heartbeat. */
720Sstevel@tonic-gate 			fp->hb_expiry = 0;
730Sstevel@tonic-gate 			fp->hb_pending = B_FALSE;
740Sstevel@tonic-gate 			fp->strikes = 0;
750Sstevel@tonic-gate 		}
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Call this function to update the cached IRE of a peer addr fp.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate void
830Sstevel@tonic-gate sctp_ire2faddr(sctp_t *sctp, sctp_faddr_t *fp)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	ire_t *ire;
860Sstevel@tonic-gate 	ipaddr_t addr4;
870Sstevel@tonic-gate 	in6_addr_t laddr;
880Sstevel@tonic-gate 	sctp_saddr_ipif_t *sp;
890Sstevel@tonic-gate 	uint_t	ipif_seqid;
900Sstevel@tonic-gate 	int hdrlen;
91*1676Sjpk 	ts_label_t *tsl;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* Remove the previous cache IRE */
940Sstevel@tonic-gate 	if ((ire = fp->ire) != NULL) {
950Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
960Sstevel@tonic-gate 		fp->ire = NULL;
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * If this addr is not reachable, mark it as unconfirmed for now, the
1010Sstevel@tonic-gate 	 * state will be changed back to unreachable later in this function
1020Sstevel@tonic-gate 	 * if it is still the case.
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH) {
1050Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_UNCONFIRMED;
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
108*1676Sjpk 	tsl = crgetlabel(CONN_CRED(sctp->sctp_connp));
109*1676Sjpk 
1100Sstevel@tonic-gate 	if (fp->isv4) {
1110Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
112*1676Sjpk 		ire = ire_cache_lookup(addr4, sctp->sctp_zoneid, tsl);
113*1676Sjpk 		if (ire != NULL)
114*1676Sjpk 			IN6_IPADDR_TO_V4MAPPED(ire->ire_src_addr, &laddr);
115*1676Sjpk 	} else {
116*1676Sjpk 		ire = ire_cache_lookup_v6(&fp->faddr, sctp->sctp_zoneid, tsl);
117*1676Sjpk 		if (ire != NULL)
118*1676Sjpk 			laddr = ire->ire_src_addr_v6;
119*1676Sjpk 	}
1200Sstevel@tonic-gate 
121*1676Sjpk 	if (ire == NULL) {
122*1676Sjpk 		dprint(3, ("ire2faddr: no ire for %x:%x:%x:%x\n",
123*1676Sjpk 		    SCTP_PRINTADDR(fp->faddr)));
124*1676Sjpk 		/*
125*1676Sjpk 		 * It is tempting to just leave the src addr
126*1676Sjpk 		 * unspecified and let IP figure it out, but we
127*1676Sjpk 		 * *cannot* do this, since IP may choose a src addr
128*1676Sjpk 		 * that is not part of this association... unless
129*1676Sjpk 		 * this sctp has bound to all addrs.  So if the ire
130*1676Sjpk 		 * lookup fails, try to find one in our src addr
131*1676Sjpk 		 * list, unless the sctp has bound to all addrs, in
132*1676Sjpk 		 * which case we change the src addr to unspec.
133*1676Sjpk 		 *
134*1676Sjpk 		 * Note that if this is a v6 endpoint but it does
135*1676Sjpk 		 * not have any v4 address at this point (e.g. may
136*1676Sjpk 		 * have been  deleted), sctp_get_valid_addr() will
137*1676Sjpk 		 * return mapped INADDR_ANY.  In this case, this
138*1676Sjpk 		 * address should be marked not reachable so that
139*1676Sjpk 		 * it won't be used to send data.
140*1676Sjpk 		 */
141*1676Sjpk 		set_saddr(sctp, fp, B_FALSE);
142*1676Sjpk 		goto set_current;
143*1676Sjpk 	}
144*1676Sjpk 
145*1676Sjpk 	ipif_seqid = ire->ire_ipif->ipif_seqid;
146*1676Sjpk 	dprint(2, ("ire2faddr: got ire for %x:%x:%x:%x, ",
147*1676Sjpk 	    SCTP_PRINTADDR(fp->faddr)));
148*1676Sjpk 	if (fp->isv4) {
1490Sstevel@tonic-gate 		dprint(2, ("src = %x\n", ire->ire_src_addr));
1500Sstevel@tonic-gate 	} else {
1510Sstevel@tonic-gate 		dprint(2, ("src=%x:%x:%x:%x\n",
1520Sstevel@tonic-gate 		    SCTP_PRINTADDR(ire->ire_src_addr_v6)));
153*1676Sjpk 	}
1540Sstevel@tonic-gate 
155*1676Sjpk 	/* make sure the laddr is part of this association */
156*1676Sjpk 	if ((sp = sctp_ipif_lookup(sctp, ipif_seqid)) != NULL &&
157*1676Sjpk 	    !sp->saddr_ipif_dontsrc) {
158*1676Sjpk 		if (sp->saddr_ipif_unconfirmed == 1)
159*1676Sjpk 			sp->saddr_ipif_unconfirmed = 0;
160*1676Sjpk 		fp->saddr = laddr;
161*1676Sjpk 	} else {
162*1676Sjpk 		dprint(2, ("ire2faddr: src addr is not part of assc\n"));
163*1676Sjpk 		/* set the src to the first saddr and hope for the best */
164*1676Sjpk 		set_saddr(sctp, fp, B_TRUE);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/* Cache the IRE */
1680Sstevel@tonic-gate 	IRE_REFHOLD_NOTR(ire);
1690Sstevel@tonic-gate 	fp->ire = ire;
1700Sstevel@tonic-gate 	if (fp->ire->ire_type == IRE_LOOPBACK && !sctp->sctp_loopback)
1710Sstevel@tonic-gate 		sctp->sctp_loopback = 1;
1720Sstevel@tonic-gate 	IRE_REFRELE(ire);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	/*
1750Sstevel@tonic-gate 	 * Pull out RTO information for this faddr and use it if we don't
1760Sstevel@tonic-gate 	 * have any yet.
1770Sstevel@tonic-gate 	 */
1780Sstevel@tonic-gate 	if (fp->srtt == -1 && ire->ire_uinfo.iulp_rtt != 0) {
179116Skcpoon 		/* The cached value is in ms. */
180116Skcpoon 		fp->srtt = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt);
181116Skcpoon 		fp->rttvar = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt_sd);
1820Sstevel@tonic-gate 		fp->rto = 3 * fp->srtt;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		/* Bound the RTO by configured min and max values */
1850Sstevel@tonic-gate 		if (fp->rto < sctp->sctp_rto_min) {
1860Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_min;
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 		if (fp->rto > sctp->sctp_rto_max) {
1890Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_max;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * Record the MTU for this faddr. If the MTU for this faddr has
1950Sstevel@tonic-gate 	 * changed, check if the assc MTU will also change.
1960Sstevel@tonic-gate 	 */
1970Sstevel@tonic-gate 	if (fp->isv4) {
1980Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
1990Sstevel@tonic-gate 	} else {
2000Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 	if ((fp->sfa_pmss + hdrlen) != ire->ire_max_frag) {
2030Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
2040Sstevel@tonic-gate 		fp->sfa_pmss = (ire->ire_max_frag - hdrlen) & ~(SCTP_ALIGN - 1);
2050Sstevel@tonic-gate 		if (fp->cwnd < (fp->sfa_pmss * 2)) {
2060Sstevel@tonic-gate 			fp->cwnd = fp->sfa_pmss * sctp_slow_start_initial;
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate set_current:
2110Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
2120Sstevel@tonic-gate 		sctp_faddr2hdraddr(fp, sctp);
2130Sstevel@tonic-gate 		sctp->sctp_mss = fp->sfa_pmss;
2140Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
2150Sstevel@tonic-gate 			sctp_set_ulp_prop(sctp);
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate void
2210Sstevel@tonic-gate sctp_faddr2ire(sctp_t *sctp, sctp_faddr_t *fp)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	ire_t *ire;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if ((ire = fp->ire) == NULL) {
2260Sstevel@tonic-gate 		return;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
2300Sstevel@tonic-gate 
231*1676Sjpk 	/* If the cached IRE is going away, there is no point to update it. */
2320Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_CONDEMNED) {
2330Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
2340Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
2350Sstevel@tonic-gate 		fp->ire = NULL;
2360Sstevel@tonic-gate 		return;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * Only record the PMTU for this faddr if we actually have
2410Sstevel@tonic-gate 	 * done discovery. This prevents initialized default from
2420Sstevel@tonic-gate 	 * clobbering any real info that IP may have.
2430Sstevel@tonic-gate 	 */
2440Sstevel@tonic-gate 	if (fp->pmtu_discovered) {
2450Sstevel@tonic-gate 		if (fp->isv4) {
2460Sstevel@tonic-gate 			ire->ire_max_frag = fp->sfa_pmss + sctp->sctp_hdr_len;
2470Sstevel@tonic-gate 		} else {
2480Sstevel@tonic-gate 			ire->ire_max_frag = fp->sfa_pmss + sctp->sctp_hdr6_len;
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
252116Skcpoon 	if (sctp_rtt_updates != 0 && fp->rtt_updates >= sctp_rtt_updates) {
2530Sstevel@tonic-gate 		/*
2540Sstevel@tonic-gate 		 * If there is no old cached values, initialize them
2550Sstevel@tonic-gate 		 * conservatively.  Set them to be (1.5 * new value).
256116Skcpoon 		 * This code copied from ip_ire_advise().  The cached
257116Skcpoon 		 * value is in ms.
2580Sstevel@tonic-gate 		 */
2590Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt != 0) {
2600Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt +
261116Skcpoon 			    TICK_TO_MSEC(fp->srtt)) >> 1;
2620Sstevel@tonic-gate 		} else {
263116Skcpoon 			ire->ire_uinfo.iulp_rtt = TICK_TO_MSEC(fp->srtt +
264116Skcpoon 			    (fp->srtt >> 1));
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt_sd != 0) {
2670Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd =
2680Sstevel@tonic-gate 			    (ire->ire_uinfo.iulp_rtt_sd +
269116Skcpoon 			    TICK_TO_MSEC(fp->rttvar)) >> 1;
2700Sstevel@tonic-gate 		} else {
271116Skcpoon 			ire->ire_uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->rttvar +
272116Skcpoon 			    (fp->rttvar >> 1));
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 		fp->rtt_updates = 0;
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * The sender must set the total length in the IP header.
2820Sstevel@tonic-gate  * If sendto == NULL, the current will be used.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate mblk_t *
2850Sstevel@tonic-gate sctp_make_mp(sctp_t *sctp, sctp_faddr_t *sendto, int trailer)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	mblk_t *mp;
2880Sstevel@tonic-gate 	size_t ipsctplen;
2890Sstevel@tonic-gate 	int isv4;
2900Sstevel@tonic-gate 	sctp_faddr_t *fp;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL || sendto != NULL);
2930Sstevel@tonic-gate 	if (sendto == NULL) {
2940Sstevel@tonic-gate 		fp = sctp->sctp_current;
2950Sstevel@tonic-gate 	} else {
2960Sstevel@tonic-gate 		fp = sendto;
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 	isv4 = fp->isv4;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/* Try to look for another IRE again. */
3010Sstevel@tonic-gate 	if (fp->ire == NULL)
3020Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/* There is no suitable source address to use, return. */
3050Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH)
3060Sstevel@tonic-gate 		return (NULL);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (isv4) {
3090Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr_len;
3100Sstevel@tonic-gate 	} else {
3110Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr6_len;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
314*1676Sjpk 	mp = allocb_cred(ipsctplen + sctp_wroff_xtra + trailer,
315*1676Sjpk 	    CONN_CRED(sctp->sctp_connp));
3160Sstevel@tonic-gate 	if (mp == NULL) {
317*1676Sjpk 		ip1dbg(("sctp_make_mp: error making mp..\n"));
3180Sstevel@tonic-gate 		return (NULL);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	mp->b_rptr += sctp_wroff_xtra;
3210Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + ipsctplen;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_wptr));
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	if (isv4) {
3260Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)mp->b_rptr;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
3290Sstevel@tonic-gate 		if (fp != sctp->sctp_current) {
3300Sstevel@tonic-gate 			/* fiddle with the dst addr */
3310Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
3320Sstevel@tonic-gate 			/* fix up src addr */
3330Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) {
3340Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
3350Sstevel@tonic-gate 				    iph->ipha_src);
3360Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
3370Sstevel@tonic-gate 				iph->ipha_src = INADDR_ANY;
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 		/* set or clear the don't fragment bit */
3410Sstevel@tonic-gate 		if (fp->df) {
3420Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
3430Sstevel@tonic-gate 		} else {
3440Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 	} else {
3470Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
3480Sstevel@tonic-gate 		if (fp != sctp->sctp_current) {
3490Sstevel@tonic-gate 			/* fiddle with the dst addr */
3500Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
3510Sstevel@tonic-gate 			/* fix up src addr */
3520Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
3530Sstevel@tonic-gate 				((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
3540Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
3550Sstevel@tonic-gate 				bzero(&((ip6_t *)(mp->b_rptr))->ip6_src,
3560Sstevel@tonic-gate 				    sizeof (in6_addr_t));
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp != NULL);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/*
3630Sstevel@tonic-gate 	 * IP will not free this IRE if it is condemned.  SCTP needs to
3640Sstevel@tonic-gate 	 * free it.
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
3670Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
3680Sstevel@tonic-gate 		fp->ire = NULL;
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	/* Stash the conn and ire ptr info. for IP */
3710Sstevel@tonic-gate 	SCTP_STASH_IPINFO(mp, fp->ire);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	return (mp);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate  * Notify upper layers about preferred write offset, write size.
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate void
3800Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	int hdrlen;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (sctp->sctp_current->isv4) {
3850Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
3860Sstevel@tonic-gate 	} else {
3870Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss);
3920Sstevel@tonic-gate 	sctp->sctp_ulp_prop(sctp->sctp_ulpd,
3930Sstevel@tonic-gate 	    sctp_wroff_xtra + hdrlen + sizeof (sctp_data_hdr_t),
3940Sstevel@tonic-gate 	    sctp->sctp_mss - sizeof (sctp_data_hdr_t));
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate void
3980Sstevel@tonic-gate sctp_set_iplen(sctp_t *sctp, mblk_t *mp)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	uint16_t	sum = 0;
4010Sstevel@tonic-gate 	ipha_t		*iph;
4020Sstevel@tonic-gate 	ip6_t		*ip6h;
4030Sstevel@tonic-gate 	mblk_t		*pmp = mp;
4040Sstevel@tonic-gate 	boolean_t	isv4;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
4070Sstevel@tonic-gate 	for (; pmp; pmp = pmp->b_cont)
4080Sstevel@tonic-gate 		sum += pmp->b_wptr - pmp->b_rptr;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (isv4) {
4110Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
4120Sstevel@tonic-gate 		iph->ipha_length = htons(sum);
4130Sstevel@tonic-gate 	} else {
4140Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
415679Sseb 		/*
416679Sseb 		 * If an ip6i_t is present, the real IPv6 header
417679Sseb 		 * immediately follows.
418679Sseb 		 */
419679Sseb 		if (ip6h->ip6_nxt == IPPROTO_RAW)
420679Sseb 			ip6h = (ip6_t *)&ip6h[1];
4210Sstevel@tonic-gate 		ip6h->ip6_plen = htons(sum - ((char *)&sctp->sctp_ip6h[1] -
4220Sstevel@tonic-gate 		    sctp->sctp_iphc6));
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate int
4270Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	int na1 = 0;
4300Sstevel@tonic-gate 	int overlap = 0;
4310Sstevel@tonic-gate 	int equal = 1;
4320Sstevel@tonic-gate 	int onematch;
4330Sstevel@tonic-gate 	sctp_faddr_t *fp1, *fp2;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	for (fp1 = a1; fp1; fp1 = fp1->next) {
4360Sstevel@tonic-gate 		onematch = 0;
4370Sstevel@tonic-gate 		for (fp2 = a2; fp2; fp2 = fp2->next) {
4380Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) {
4390Sstevel@tonic-gate 				overlap++;
4400Sstevel@tonic-gate 				onematch = 1;
4410Sstevel@tonic-gate 				break;
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 			if (!onematch) {
4440Sstevel@tonic-gate 				equal = 0;
4450Sstevel@tonic-gate 			}
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 		na1++;
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if (equal) {
4510Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	if (overlap == na1) {
4540Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	if (overlap) {
4570Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * Caller must hold conn fanout lock.
4640Sstevel@tonic-gate  */
465*1676Sjpk static int
466*1676Sjpk sctp_add_faddr_entry(sctp_t *sctp, in6_addr_t *addr, int sleep,
467*1676Sjpk     boolean_t first)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	sctp_faddr_t *faddr;
4700Sstevel@tonic-gate 
471*1676Sjpk 	if (is_system_labeled()) {
472*1676Sjpk 		ts_label_t *tsl;
473*1676Sjpk 		tsol_tpc_t *rhtp;
474*1676Sjpk 		int retv;
475*1676Sjpk 
476*1676Sjpk 		tsl = crgetlabel(CONN_CRED(sctp->sctp_connp));
477*1676Sjpk 		ASSERT(tsl != NULL);
478*1676Sjpk 
479*1676Sjpk 		/* find_tpc automatically does the right thing with IPv4 */
480*1676Sjpk 		rhtp = find_tpc(addr, IPV6_VERSION, B_FALSE);
481*1676Sjpk 		if (rhtp == NULL)
482*1676Sjpk 			return (EACCES);
4830Sstevel@tonic-gate 
484*1676Sjpk 		retv = EACCES;
485*1676Sjpk 		if (tsl->tsl_doi == rhtp->tpc_tp.tp_doi) {
486*1676Sjpk 			switch (rhtp->tpc_tp.host_type) {
487*1676Sjpk 			case UNLABELED:
488*1676Sjpk 				/*
489*1676Sjpk 				 * Can talk to unlabeled hosts if any of the
490*1676Sjpk 				 * following are true:
491*1676Sjpk 				 *   1. zone's label matches the remote host's
492*1676Sjpk 				 *	default label,
493*1676Sjpk 				 *   2. mac_exempt is on and the zone dominates
494*1676Sjpk 				 *	the remote host's label, or
495*1676Sjpk 				 *   3. mac_exempt is on and the socket is from
496*1676Sjpk 				 *	the global zone.
497*1676Sjpk 				 */
498*1676Sjpk 				if (blequal(&rhtp->tpc_tp.tp_def_label,
499*1676Sjpk 				    &tsl->tsl_label) ||
500*1676Sjpk 				    (sctp->sctp_mac_exempt &&
501*1676Sjpk 				    (sctp->sctp_zoneid == GLOBAL_ZONEID ||
502*1676Sjpk 				    bldominates(&tsl->tsl_label,
503*1676Sjpk 				    &rhtp->tpc_tp.tp_def_label))))
504*1676Sjpk 					retv = 0;
505*1676Sjpk 				break;
506*1676Sjpk 			case SUN_CIPSO:
507*1676Sjpk 				if (_blinrange(&tsl->tsl_label,
508*1676Sjpk 				    &rhtp->tpc_tp.tp_sl_range_cipso) ||
509*1676Sjpk 				    blinlset(&tsl->tsl_label,
510*1676Sjpk 				    rhtp->tpc_tp.tp_sl_set_cipso))
511*1676Sjpk 					retv = 0;
512*1676Sjpk 				break;
513*1676Sjpk 			}
514*1676Sjpk 		}
515*1676Sjpk 		TPC_RELE(rhtp);
516*1676Sjpk 		if (retv != 0)
517*1676Sjpk 			return (retv);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
520*1676Sjpk 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL)
521*1676Sjpk 		return (ENOMEM);
522*1676Sjpk 
5230Sstevel@tonic-gate 	sctp_init_faddr(sctp, faddr, addr);
5240Sstevel@tonic-gate 	ASSERT(faddr->next == NULL);
5250Sstevel@tonic-gate 
526*1676Sjpk 	if (sctp->sctp_faddrs == NULL) {
527*1676Sjpk 		ASSERT(sctp->sctp_lastfaddr == NULL);
528*1676Sjpk 		/* only element on list; first and last are same */
529*1676Sjpk 		sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr;
530*1676Sjpk 	} else if (first) {
531*1676Sjpk 		ASSERT(sctp->sctp_lastfaddr != NULL);
532*1676Sjpk 		faddr->next = sctp->sctp_faddrs;
533*1676Sjpk 		sctp->sctp_faddrs = faddr;
5340Sstevel@tonic-gate 	} else {
535*1676Sjpk 		sctp->sctp_lastfaddr->next = faddr;
536*1676Sjpk 		sctp->sctp_lastfaddr = faddr;
5370Sstevel@tonic-gate 	}
538852Svi117747 	sctp->sctp_nfaddrs++;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	return (0);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /*
544*1676Sjpk  * Add new address to end of list.
545*1676Sjpk  * Returns 0 on success, or errno on failure:
546*1676Sjpk  *	ENOMEM	- allocation failure; only for sleep==KM_NOSLEEP
547*1676Sjpk  *	EACCES	- label is incompatible with caller or connection
548*1676Sjpk  *		  (labeled [trusted] solaris only)
549*1676Sjpk  * Caller must hold conn fanout lock.
550*1676Sjpk  */
551*1676Sjpk int
552*1676Sjpk sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep)
553*1676Sjpk {
554*1676Sjpk 	dprint(4, ("add_faddr: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr),
555*1676Sjpk 	    sleep));
556*1676Sjpk 
557*1676Sjpk 	return (sctp_add_faddr_entry(sctp, addr, sleep, B_FALSE));
558*1676Sjpk }
559*1676Sjpk 
560*1676Sjpk /*
561*1676Sjpk  * Same as sctp_add_faddr above, but put new entry at front of list.
5620Sstevel@tonic-gate  * Caller must hold conn fanout lock.
5630Sstevel@tonic-gate  */
5640Sstevel@tonic-gate int
5650Sstevel@tonic-gate sctp_add_faddr_first(sctp_t *sctp, in6_addr_t *addr, int sleep)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	dprint(4, ("add_faddr_first: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr),
5680Sstevel@tonic-gate 	    sleep));
5690Sstevel@tonic-gate 
570*1676Sjpk 	return (sctp_add_faddr_entry(sctp, addr, sleep, B_TRUE));
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate sctp_faddr_t *
5740Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	sctp_faddr_t *fp;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5790Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr))
5800Sstevel@tonic-gate 			break;
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	return (fp);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate sctp_faddr_t *
5870Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	for (; fp; fp = fp->next) {
5900Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) {
5910Sstevel@tonic-gate 			break;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	return (fp);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate void
5990Sstevel@tonic-gate sctp_faddr2hdraddr(sctp_faddr_t *fp, sctp_t *sctp)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	if (fp->isv4) {
6020Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
6030Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
6040Sstevel@tonic-gate 		/* Must not allow unspec src addr if not bound to all */
6050Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr) &&
6060Sstevel@tonic-gate 		    !sctp->sctp_bound_to_all) {
6070Sstevel@tonic-gate 			/*
6080Sstevel@tonic-gate 			 * set the src to the first v4 saddr and hope
6090Sstevel@tonic-gate 			 * for the best
6100Sstevel@tonic-gate 			 */
6110Sstevel@tonic-gate 			fp->saddr = sctp_get_valid_addr(sctp, B_FALSE);
6120Sstevel@tonic-gate 		}
6130Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src);
6140Sstevel@tonic-gate 		/* update don't fragment bit */
6150Sstevel@tonic-gate 		if (fp->df) {
6160Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
6170Sstevel@tonic-gate 			    htons(IPH_DF);
6180Sstevel@tonic-gate 		} else {
6190Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 	} else {
6220Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = fp->faddr;
6230Sstevel@tonic-gate 		/* Must not allow unspec src addr if not bound to all */
6240Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->saddr) &&
6250Sstevel@tonic-gate 		    !sctp->sctp_bound_to_all) {
6260Sstevel@tonic-gate 			/*
6270Sstevel@tonic-gate 			 * set the src to the first v6 saddr and hope
6280Sstevel@tonic-gate 			 * for the best
6290Sstevel@tonic-gate 			 */
6300Sstevel@tonic-gate 			fp->saddr = sctp_get_valid_addr(sctp, B_TRUE);
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = fp->saddr;
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate void
6370Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	sctp_faddr_t *fp;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
6420Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	sctp_faddr2hdraddr(sctp->sctp_current, sctp);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate void
6490Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	int64_t now = lbolt64;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	fp->strikes = 0;
6540Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
6550Sstevel@tonic-gate 	fp->lastactive = now;
6560Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
6570Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
6580Sstevel@tonic-gate 	if (fp->state != SCTP_FADDRS_ALIVE) {
6590Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_ALIVE;
6600Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		/* If this is the primary, switch back to it now */
6630Sstevel@tonic-gate 		if (fp == sctp->sctp_primary) {
6640Sstevel@tonic-gate 			sctp->sctp_current = fp;
6650Sstevel@tonic-gate 			sctp->sctp_mss = fp->sfa_pmss;
6660Sstevel@tonic-gate 			/* Reset the addrs in the composite header */
6670Sstevel@tonic-gate 			sctp_faddr2hdraddr(fp, sctp);
6680Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
6690Sstevel@tonic-gate 				sctp_set_ulp_prop(sctp);
6700Sstevel@tonic-gate 			}
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	if (fp->ire == NULL) {
6740Sstevel@tonic-gate 		/* Should have a full IRE now */
6750Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate int
6800Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	sctp_faddr_t *fp;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
6850Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) {
6860Sstevel@tonic-gate 			return (1);
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	return (0);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate /*
6940Sstevel@tonic-gate  * Returns 0 if there is at leave one other active faddr, -1 if there
6950Sstevel@tonic-gate  * are none. If there are none left, faddr_dead() will start killing the
6960Sstevel@tonic-gate  * association.
6970Sstevel@tonic-gate  * If the downed faddr was the current faddr, a new current faddr
6980Sstevel@tonic-gate  * will be chosen.
6990Sstevel@tonic-gate  */
7000Sstevel@tonic-gate int
7010Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	sctp_faddr_t *ofp;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_ALIVE) {
7060Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0);
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 	fp->state = newstate;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
7110Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), newstate));
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
7140Sstevel@tonic-gate 		/* Current faddr down; need to switch it */
7150Sstevel@tonic-gate 		sctp->sctp_current = NULL;
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	/* Find next alive faddr */
7190Sstevel@tonic-gate 	ofp = fp;
7200Sstevel@tonic-gate 	for (fp = fp->next; fp; fp = fp->next) {
7210Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE) {
7220Sstevel@tonic-gate 			break;
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	if (fp == NULL) {
7270Sstevel@tonic-gate 		/* Continue from beginning of list */
7280Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) {
7290Sstevel@tonic-gate 			if (fp->state == SCTP_FADDRS_ALIVE) {
7300Sstevel@tonic-gate 				break;
7310Sstevel@tonic-gate 			}
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	if (fp != ofp) {
7360Sstevel@tonic-gate 		if (sctp->sctp_current == NULL) {
7370Sstevel@tonic-gate 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
7380Sstevel@tonic-gate 			    SCTP_PRINTADDR(fp->faddr)));
7390Sstevel@tonic-gate 			sctp->sctp_current = fp;
7400Sstevel@tonic-gate 			sctp->sctp_mss = fp->sfa_pmss;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 			/* Reset the addrs in the composite header */
7430Sstevel@tonic-gate 			sctp_faddr2hdraddr(fp, sctp);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
7460Sstevel@tonic-gate 				sctp_set_ulp_prop(sctp);
7470Sstevel@tonic-gate 			}
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 		return (0);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	/* All faddrs are down; kill the association */
7540Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
7550Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpAborted);
7560Sstevel@tonic-gate 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
7570Sstevel@tonic-gate 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
7580Sstevel@tonic-gate 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
7590Sstevel@tonic-gate 	    sctp->sctp_client_errno : ETIMEDOUT);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	return (-1);
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate sctp_faddr_t *
7650Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	sctp_faddr_t *nfp = NULL;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	if (ofp == NULL) {
7700Sstevel@tonic-gate 		ofp = sctp->sctp_current;
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	/* Find the next live one */
7740Sstevel@tonic-gate 	for (nfp = ofp->next; nfp != NULL; nfp = nfp->next) {
7750Sstevel@tonic-gate 		if (nfp->state == SCTP_FADDRS_ALIVE) {
7760Sstevel@tonic-gate 			break;
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (nfp == NULL) {
7810Sstevel@tonic-gate 		/* Continue from beginning of list */
7820Sstevel@tonic-gate 		for (nfp = sctp->sctp_faddrs; nfp != ofp; nfp = nfp->next) {
7830Sstevel@tonic-gate 			if (nfp->state == SCTP_FADDRS_ALIVE) {
7840Sstevel@tonic-gate 				break;
7850Sstevel@tonic-gate 			}
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * nfp could only be NULL if all faddrs are down, and when
7910Sstevel@tonic-gate 	 * this happens, faddr_dead() should have killed the
7920Sstevel@tonic-gate 	 * association. Hence this assertion...
7930Sstevel@tonic-gate 	 */
7940Sstevel@tonic-gate 	ASSERT(nfp != NULL);
7950Sstevel@tonic-gate 	return (nfp);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate void
7990Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	sctp_faddr_t *fpp;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if (!sctp->sctp_faddrs) {
8040Sstevel@tonic-gate 		return;
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	if (fp->timer_mp != NULL) {
8080Sstevel@tonic-gate 		sctp_timer_free(fp->timer_mp);
8090Sstevel@tonic-gate 		fp->timer_mp = NULL;
8100Sstevel@tonic-gate 		fp->timer_running = 0;
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 	if (fp->rc_timer_mp != NULL) {
8130Sstevel@tonic-gate 		sctp_timer_free(fp->rc_timer_mp);
8140Sstevel@tonic-gate 		fp->rc_timer_mp = NULL;
8150Sstevel@tonic-gate 		fp->rc_timer_running = 0;
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 	if (fp->ire != NULL) {
8180Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
8190Sstevel@tonic-gate 		fp->ire = NULL;
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8230Sstevel@tonic-gate 		goto gotit;
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next)
8270Sstevel@tonic-gate 		;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate gotit:
8300Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
8310Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8320Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8330Sstevel@tonic-gate 		sctp->sctp_faddrs = fp->next;
8340Sstevel@tonic-gate 	} else {
8350Sstevel@tonic-gate 		fpp->next = fp->next;
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8380Sstevel@tonic-gate 	/* XXX faddr2ire? */
8390Sstevel@tonic-gate 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
840852Svi117747 	sctp->sctp_nfaddrs--;
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate void
8440Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpn;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL) {
8490Sstevel@tonic-gate 		ASSERT(sctp->sctp_lastfaddr == NULL);
8500Sstevel@tonic-gate 		return;
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr != NULL);
8540Sstevel@tonic-gate 	sctp->sctp_lastfaddr = NULL;
8550Sstevel@tonic-gate 	sctp->sctp_current = NULL;
8560Sstevel@tonic-gate 	sctp->sctp_primary = NULL;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8610Sstevel@tonic-gate 		/* in conn fanout; need to hold lock */
8620Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
8660Sstevel@tonic-gate 		fpn = fp->next;
8670Sstevel@tonic-gate 		if (fp->ire != NULL)
8680Sstevel@tonic-gate 			IRE_REFRELE_NOTR(fp->ire);
8690Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
870852Svi117747 		sctp->sctp_nfaddrs--;
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	sctp->sctp_faddrs = NULL;
874852Svi117747 	ASSERT(sctp->sctp_nfaddrs == 0);
8750Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8760Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate void
8820Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate 	sctp_zap_faddrs(sctp, 0);
8850Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*
8890Sstevel@tonic-gate  * Initialize the IPv4 header. Loses any record of any IP options.
8900Sstevel@tonic-gate  */
8910Sstevel@tonic-gate int
8920Sstevel@tonic-gate sctp_header_init_ipv4(sctp_t *sctp, int sleep)
8930Sstevel@tonic-gate {
8940Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/*
8970Sstevel@tonic-gate 	 * This is a simple initialization. If there's
8980Sstevel@tonic-gate 	 * already a template, it should never be too small,
8990Sstevel@tonic-gate 	 * so reuse it.  Otherwise, allocate space for the new one.
9000Sstevel@tonic-gate 	 */
9010Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
9020Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc_len >= SCTP_MAX_COMBINED_HEADER_LENGTH);
9030Sstevel@tonic-gate 		bzero(sctp->sctp_iphc, sctp->sctp_iphc_len);
9040Sstevel@tonic-gate 	} else {
9050Sstevel@tonic-gate 		sctp->sctp_iphc_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
9060Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(sctp->sctp_iphc_len, sleep);
9070Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL) {
9080Sstevel@tonic-gate 			sctp->sctp_iphc_len = 0;
9090Sstevel@tonic-gate 			return (ENOMEM);
9100Sstevel@tonic-gate 		}
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	sctp->sctp_hdr_len = sizeof (ipha_t) + sizeof (sctp_hdr_t);
9160Sstevel@tonic-gate 	sctp->sctp_ip_hdr_len = sizeof (ipha_t);
9170Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_length = htons(sizeof (ipha_t) +
9180Sstevel@tonic-gate 	    sizeof (sctp_hdr_t));
9190Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_version_and_hdr_length
9200Sstevel@tonic-gate 		= (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/*
9230Sstevel@tonic-gate 	 * These two fields should be zero, and are already set above.
9240Sstevel@tonic-gate 	 *
9250Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_ident,
9260Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_fragment_offset_and_flags.
9270Sstevel@tonic-gate 	 */
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_ttl = sctp_ipv4_ttl;
9300Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_protocol = IPPROTO_SCTP;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc + sizeof (ipha_t));
9330Sstevel@tonic-gate 	sctp->sctp_sctph = sctph;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	return (0);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate /*
9390Sstevel@tonic-gate  * Update sctp_sticky_hdrs based on sctp_sticky_ipp.
9400Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, any sticky extension
9410Sstevel@tonic-gate  * headers, and the maximum size sctp header (to avoid reallocation
9420Sstevel@tonic-gate  * on the fly for additional sctp options).
9430Sstevel@tonic-gate  * Returns failure if can't allocate memory.
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate int
9460Sstevel@tonic-gate sctp_build_hdrs(sctp_t *sctp)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 	char		*hdrs;
9490Sstevel@tonic-gate 	uint_t		hdrs_len;
9500Sstevel@tonic-gate 	ip6i_t		*ip6i;
9510Sstevel@tonic-gate 	char		buf[SCTP_MAX_HDR_LENGTH];
9520Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
9530Sstevel@tonic-gate 	in6_addr_t	src;
9540Sstevel@tonic-gate 	in6_addr_t	dst;
955*1676Sjpk 
9560Sstevel@tonic-gate 	/*
9570Sstevel@tonic-gate 	 * save the existing sctp header and source/dest IP addresses
9580Sstevel@tonic-gate 	 */
9590Sstevel@tonic-gate 	bcopy(sctp->sctp_sctph6, buf, sizeof (sctp_hdr_t));
9600Sstevel@tonic-gate 	src = sctp->sctp_ip6h->ip6_src;
9610Sstevel@tonic-gate 	dst = sctp->sctp_ip6h->ip6_dst;
9620Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp) + SCTP_MAX_HDR_LENGTH;
9630Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
9640Sstevel@tonic-gate 	if (hdrs_len > sctp->sctp_iphc6_len) {
9650Sstevel@tonic-gate 		/* Need to reallocate */
9660Sstevel@tonic-gate 		hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP);
9670Sstevel@tonic-gate 		if (hdrs == NULL)
9680Sstevel@tonic-gate 			return (ENOMEM);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		if (sctp->sctp_iphc6_len != 0)
9710Sstevel@tonic-gate 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
9720Sstevel@tonic-gate 		sctp->sctp_iphc6 = hdrs;
9730Sstevel@tonic-gate 		sctp->sctp_iphc6_len = hdrs_len;
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 	ip_build_hdrs_v6((uchar_t *)sctp->sctp_iphc6,
9760Sstevel@tonic-gate 	    hdrs_len - SCTP_MAX_HDR_LENGTH, ipp, IPPROTO_SCTP);
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	/* Set header fields not in ipp */
9790Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
9800Sstevel@tonic-gate 		ip6i = (ip6i_t *)sctp->sctp_iphc6;
9810Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)&ip6i[1];
9820Sstevel@tonic-gate 	} else {
9830Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * sctp->sctp_ip_hdr_len will include ip6i_t if there is one.
9870Sstevel@tonic-gate 	 */
9880Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = hdrs_len - SCTP_MAX_HDR_LENGTH;
9890Sstevel@tonic-gate 	sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9900Sstevel@tonic-gate 	    sctp->sctp_ip_hdr6_len);
9910Sstevel@tonic-gate 	sctp->sctp_hdr6_len = sctp->sctp_ip_hdr6_len + sizeof (sctp_hdr_t);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	bcopy(buf, sctp->sctp_sctph6, sizeof (sctp_hdr_t));
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_src = src;
9960Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_dst = dst;
9970Sstevel@tonic-gate 	/*
998679Sseb 	 * If the hoplimit was not set by ip_build_hdrs_v6(), we need to
999679Sseb 	 * set it to the default value for SCTP.
10000Sstevel@tonic-gate 	 */
1001679Sseb 	if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS))
1002679Sseb 		sctp->sctp_ip6h->ip6_hops = sctp_ipv6_hoplimit;
10030Sstevel@tonic-gate 	/*
10040Sstevel@tonic-gate 	 * If we're setting extension headers after a connection
10050Sstevel@tonic-gate 	 * has been established, and if we have a routing header
10060Sstevel@tonic-gate 	 * among the extension headers, call ip_massage_options_v6 to
10070Sstevel@tonic-gate 	 * manipulate the routing header/ip6_dst set the checksum
10080Sstevel@tonic-gate 	 * difference in the sctp header template.
10090Sstevel@tonic-gate 	 * (This happens in sctp_connect_ipv6 if the routing header
10100Sstevel@tonic-gate 	 * is set prior to the connect.)
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
10140Sstevel@tonic-gate 	    (sctp->sctp_sticky_ipp.ipp_fields & IPPF_RTHDR)) {
10150Sstevel@tonic-gate 		ip6_rthdr_t *rth;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
10180Sstevel@tonic-gate 		    (uint8_t *)sctp->sctp_sctph6);
10190Sstevel@tonic-gate 		if (rth != NULL)
10200Sstevel@tonic-gate 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth);
10210Sstevel@tonic-gate 	}
10220Sstevel@tonic-gate 	return (0);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate  * Initialize the IPv6 header. Loses any record of any IPv6 extension headers.
10270Sstevel@tonic-gate  */
10280Sstevel@tonic-gate int
10290Sstevel@tonic-gate sctp_header_init_ipv6(sctp_t *sctp, int sleep)
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	/*
10340Sstevel@tonic-gate 	 * This is a simple initialization. If there's
10350Sstevel@tonic-gate 	 * already a template, it should never be too small,
10360Sstevel@tonic-gate 	 * so reuse it. Otherwise, allocate space for the new one.
10370Sstevel@tonic-gate 	 * Ensure that there is enough space to "downgrade" the sctp_t
10380Sstevel@tonic-gate 	 * to an IPv4 sctp_t. This requires having space for a full load
10390Sstevel@tonic-gate 	 * of IPv4 options
10400Sstevel@tonic-gate 	 */
10410Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
10420Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc6_len >=
10430Sstevel@tonic-gate 		    SCTP_MAX_COMBINED_HEADER_LENGTH);
10440Sstevel@tonic-gate 		bzero(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
10450Sstevel@tonic-gate 	} else {
10460Sstevel@tonic-gate 		sctp->sctp_iphc6_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
10470Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(sctp->sctp_iphc_len, sleep);
10480Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
10490Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
10500Sstevel@tonic-gate 			return (ENOMEM);
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	sctp->sctp_hdr6_len = IPV6_HDR_LEN + sizeof (sctp_hdr_t);
10540Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = IPV6_HDR_LEN;
10550Sstevel@tonic-gate 	sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/* Initialize the header template */
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
10600Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_plen = ntohs(sizeof (sctp_hdr_t));
10610Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_nxt = IPPROTO_SCTP;
10620Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_hops = sctp_ipv6_hoplimit;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc6 + IPV6_HDR_LEN);
10650Sstevel@tonic-gate 	sctp->sctp_sctph6 = sctph;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	return (0);
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
1070*1676Sjpk static int
1071*1676Sjpk sctp_v4_label(sctp_t *sctp)
1072*1676Sjpk {
1073*1676Sjpk 	uchar_t optbuf[IP_MAX_OPT_LENGTH];
1074*1676Sjpk 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
1075*1676Sjpk 	int added;
1076*1676Sjpk 
1077*1676Sjpk 	if (tsol_compute_label(cr, sctp->sctp_ipha->ipha_dst, optbuf,
1078*1676Sjpk 	    sctp->sctp_mac_exempt) != 0)
1079*1676Sjpk 		return (EACCES);
1080*1676Sjpk 
1081*1676Sjpk 	added = tsol_remove_secopt(sctp->sctp_ipha, sctp->sctp_hdr_len);
1082*1676Sjpk 	if (added == -1)
1083*1676Sjpk 		return (EACCES);
1084*1676Sjpk 	sctp->sctp_hdr_len += added;
1085*1676Sjpk 	sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph + added);
1086*1676Sjpk 	sctp->sctp_ip_hdr_len += added;
1087*1676Sjpk 	if ((sctp->sctp_v4label_len = optbuf[IPOPT_OLEN]) != 0) {
1088*1676Sjpk 		sctp->sctp_v4label_len = (sctp->sctp_v4label_len + 3) & ~3;
1089*1676Sjpk 		added = tsol_prepend_option(optbuf, sctp->sctp_ipha,
1090*1676Sjpk 		    sctp->sctp_hdr_len);
1091*1676Sjpk 		if (added == -1)
1092*1676Sjpk 			return (EACCES);
1093*1676Sjpk 		sctp->sctp_hdr_len += added;
1094*1676Sjpk 		sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph +
1095*1676Sjpk 		    added);
1096*1676Sjpk 		sctp->sctp_ip_hdr_len += added;
1097*1676Sjpk 	}
1098*1676Sjpk 	return (0);
1099*1676Sjpk }
1100*1676Sjpk 
1101*1676Sjpk static int
1102*1676Sjpk sctp_v6_label(sctp_t *sctp)
1103*1676Sjpk {
1104*1676Sjpk 	uchar_t optbuf[TSOL_MAX_IPV6_OPTION];
1105*1676Sjpk 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
1106*1676Sjpk 
1107*1676Sjpk 	if (tsol_compute_label_v6(cr, &sctp->sctp_ip6h->ip6_dst, optbuf,
1108*1676Sjpk 	    sctp->sctp_mac_exempt) != 0)
1109*1676Sjpk 		return (EACCES);
1110*1676Sjpk 	if (tsol_update_sticky(&sctp->sctp_sticky_ipp, &sctp->sctp_v6label_len,
1111*1676Sjpk 	    optbuf) != 0)
1112*1676Sjpk 		return (EACCES);
1113*1676Sjpk 	if (sctp_build_hdrs(sctp) != 0)
1114*1676Sjpk 		return (EACCES);
1115*1676Sjpk 	return (0);
1116*1676Sjpk }
1117*1676Sjpk 
11180Sstevel@tonic-gate /*
11190Sstevel@tonic-gate  * XXX implement more sophisticated logic
11200Sstevel@tonic-gate  */
1121*1676Sjpk /* ARGSUSED */
1122*1676Sjpk int
1123*1676Sjpk sctp_set_hdraddrs(sctp_t *sctp, cred_t *cr)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	sctp_faddr_t *fp;
11260Sstevel@tonic-gate 	int gotv4 = 0;
11270Sstevel@tonic-gate 	int gotv6 = 0;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs != NULL);
11300Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/* Set up using the primary first */
11330Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) {
11340Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->faddr,
11350Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
11360Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
11370Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->saddr,
11380Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_src);
1139*1676Sjpk 		if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
1140*1676Sjpk 			gotv4 = 1;
1141*1676Sjpk 			if (sctp->sctp_ipversion == IPV4_VERSION) {
1142*1676Sjpk 				goto copyports;
1143*1676Sjpk 			}
11440Sstevel@tonic-gate 		}
11450Sstevel@tonic-gate 	} else {
11460Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = sctp->sctp_primary->faddr;
11470Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
11480Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = sctp->sctp_primary->saddr;
1149*1676Sjpk 		if (!is_system_labeled() || sctp_v6_label(sctp) == 0)
1150*1676Sjpk 			gotv6 = 1;
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
11540Sstevel@tonic-gate 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
11550Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
11560Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_dst);
11570Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
11580Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
11590Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_src);
1160*1676Sjpk 			if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
1161*1676Sjpk 				gotv4 = 1;
1162*1676Sjpk 				if (sctp->sctp_ipversion == IPV4_VERSION ||
1163*1676Sjpk 				    gotv6) {
1164*1676Sjpk 					break;
1165*1676Sjpk 				}
11660Sstevel@tonic-gate 			}
11670Sstevel@tonic-gate 		} else if (!gotv6) {
11680Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_dst = fp->faddr;
11690Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
11700Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_src = fp->saddr;
1171*1676Sjpk 			if (!is_system_labeled() || sctp_v6_label(sctp) == 0) {
1172*1676Sjpk 				gotv6 = 1;
1173*1676Sjpk 				if (gotv4)
1174*1676Sjpk 					break;
11750Sstevel@tonic-gate 			}
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate copyports:
1180*1676Sjpk 	if (!gotv4 && !gotv6)
1181*1676Sjpk 		return (EACCES);
1182*1676Sjpk 
11830Sstevel@tonic-gate 	/* copy in the ports for good measure */
11840Sstevel@tonic-gate 	sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
11850Sstevel@tonic-gate 	sctp->sctp_sctph->sh_dport = sctp->sctp_fport;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_sport = sctp->sctp_lport;
11880Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_dport = sctp->sctp_fport;
1189*1676Sjpk 	return (0);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate void
11930Sstevel@tonic-gate sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp)
11940Sstevel@tonic-gate {
11950Sstevel@tonic-gate 	mblk_t *mp;
11960Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
11970Sstevel@tonic-gate 	size_t len;
11980Sstevel@tonic-gate 	int pad;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	len = sizeof (*ph) + ntohs(uph->sph_len);
12010Sstevel@tonic-gate 	if ((pad = len % 4) != 0) {
12020Sstevel@tonic-gate 		pad = 4 - pad;
12030Sstevel@tonic-gate 		len += pad;
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
12060Sstevel@tonic-gate 	if (mp == NULL) {
12070Sstevel@tonic-gate 		return;
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
12110Sstevel@tonic-gate 	ph->sph_type = htons(PARM_UNRECOGNIZED);
12120Sstevel@tonic-gate 	ph->sph_len = htons(len - pad);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	/* copy in the unrecognized parameter */
12150Sstevel@tonic-gate 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
12180Sstevel@tonic-gate 	if (*errmp != NULL) {
12190Sstevel@tonic-gate 		linkb(*errmp, mp);
12200Sstevel@tonic-gate 	} else {
12210Sstevel@tonic-gate 		*errmp = mp;
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate /*
12260Sstevel@tonic-gate  * o Bounds checking
12270Sstevel@tonic-gate  * o Updates remaining
12280Sstevel@tonic-gate  * o Checks alignment
12290Sstevel@tonic-gate  */
12300Sstevel@tonic-gate sctp_parm_hdr_t *
12310Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
12320Sstevel@tonic-gate {
12330Sstevel@tonic-gate 	int pad;
12340Sstevel@tonic-gate 	uint16_t len;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	len = ntohs(current->sph_len);
12370Sstevel@tonic-gate 	*remaining -= len;
12380Sstevel@tonic-gate 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
12390Sstevel@tonic-gate 		return (NULL);
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
12420Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
12430Sstevel@tonic-gate 		*remaining -= pad;
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
12460Sstevel@tonic-gate 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
12470Sstevel@tonic-gate 	return (current);
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate /*
12510Sstevel@tonic-gate  * Sets the address parameters given in the INIT chunk into sctp's
12520Sstevel@tonic-gate  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
12530Sstevel@tonic-gate  * no address parameters in the INIT chunk, a single faddr is created
12540Sstevel@tonic-gate  * from the ip hdr at the beginning of pkt.
12550Sstevel@tonic-gate  * If there already are existing addresses hanging from sctp, merge
12560Sstevel@tonic-gate  * them in, if the old info contains addresses which are not present
12570Sstevel@tonic-gate  * in this new info, get rid of them, and clean the pointers if there's
12580Sstevel@tonic-gate  * messages which have this as their target address.
12590Sstevel@tonic-gate  *
1260432Svi117747  * We also re-adjust the source address list here since the list may
1261432Svi117747  * contain more than what is actually part of the association. If
1262432Svi117747  * we get here from sctp_send_cookie_echo(), we are on the active
1263432Svi117747  * side and psctp will be NULL and ich will be the INIT-ACK chunk.
1264432Svi117747  * If we get here from sctp_accept_comm(), ich will be the INIT chunk
1265432Svi117747  * and psctp will the listening endpoint.
1266432Svi117747  *
1267432Svi117747  * INIT processing: When processing the INIT we inherit the src address
1268432Svi117747  * list from the listener. For a loopback or linklocal association, we
1269432Svi117747  * delete the list and just take the address from the IP header (since
1270432Svi117747  * that's how we created the INIT-ACK). Additionally, for loopback we
1271432Svi117747  * ignore the address params in the INIT. For determining which address
1272432Svi117747  * types were sent in the INIT-ACK we follow the same logic as in
1273432Svi117747  * creating the INIT-ACK. We delete addresses of the type that are not
1274432Svi117747  * supported by the peer.
1275432Svi117747  *
1276432Svi117747  * INIT-ACK processing: When processing the INIT-ACK since we had not
1277432Svi117747  * included addr params for loopback or linklocal addresses when creating
1278432Svi117747  * the INIT, we just use the address from the IP header. Further, for
1279432Svi117747  * loopback we ignore the addr param list. We mark addresses of the
1280432Svi117747  * type not supported by the peer as unconfirmed.
1281432Svi117747  *
1282432Svi117747  * In case of INIT processing we look for supported address types in the
1283432Svi117747  * supported address param, if present. In both cases the address type in
1284432Svi117747  * the IP header is supported as well as types for addresses in the param
1285432Svi117747  * list, if any.
1286432Svi117747  *
1287432Svi117747  * Once we have the supported address types sctp_check_saddr() runs through
1288432Svi117747  * the source address list and deletes or marks as unconfirmed address of
1289432Svi117747  * types not supported by the peer.
1290432Svi117747  *
12910Sstevel@tonic-gate  * Returns 0 on success, sys errno on failure
12920Sstevel@tonic-gate  */
12930Sstevel@tonic-gate int
12940Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
12950Sstevel@tonic-gate     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
12980Sstevel@tonic-gate 	ipha_t			*iph;
12990Sstevel@tonic-gate 	ip6_t			*ip6h;
1300432Svi117747 	in6_addr_t		hdrsaddr[1];
1301432Svi117747 	in6_addr_t		hdrdaddr[1];
13020Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
13030Sstevel@tonic-gate 	ssize_t			remaining;
13040Sstevel@tonic-gate 	int			isv4;
13050Sstevel@tonic-gate 	int			err;
13060Sstevel@tonic-gate 	sctp_faddr_t		*fp;
1307432Svi117747 	int			supp_af = 0;
1308432Svi117747 	boolean_t		check_saddr = B_TRUE;
1309852Svi117747 	in6_addr_t		curaddr;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	if (sctp_options != NULL)
13120Sstevel@tonic-gate 		*sctp_options = 0;
13130Sstevel@tonic-gate 
1314432Svi117747 	/* extract the address from the IP header */
1315432Svi117747 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1316432Svi117747 	if (isv4) {
1317432Svi117747 		iph = (ipha_t *)pkt->b_rptr;
1318432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr);
1319432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr);
1320432Svi117747 		supp_af |= PARM_SUPP_V4;
1321432Svi117747 	} else {
1322432Svi117747 		ip6h = (ip6_t *)pkt->b_rptr;
1323432Svi117747 		hdrsaddr[0] = ip6h->ip6_src;
1324432Svi117747 		hdrdaddr[0] = ip6h->ip6_dst;
1325432Svi117747 		supp_af |= PARM_SUPP_V6;
1326432Svi117747 	}
1327432Svi117747 
1328432Svi117747 	/*
1329432Svi117747 	 * Unfortunately, we can't delay this because adding an faddr
1330432Svi117747 	 * looks for the presence of the source address (from the ire
1331432Svi117747 	 * for the faddr) in the source address list. We could have
1332432Svi117747 	 * delayed this if, say, this was a loopback/linklocal connection.
1333432Svi117747 	 * Now, we just end up nuking this list and taking the addr from
1334432Svi117747 	 * the IP header for loopback/linklocal.
1335432Svi117747 	 */
13360Sstevel@tonic-gate 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
13370Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
13400Sstevel@tonic-gate 		if (err != 0)
13410Sstevel@tonic-gate 			return (err);
13420Sstevel@tonic-gate 	}
1343432Svi117747 	/*
1344432Svi117747 	 * We will add the faddr before parsing the address list as this
1345432Svi117747 	 * might be a loopback connection and we would not have to
1346432Svi117747 	 * go through the list.
1347432Svi117747 	 *
1348432Svi117747 	 * Make sure the header's addr is in the list
1349432Svi117747 	 */
1350432Svi117747 	fp = sctp_lookup_faddr(sctp, hdrsaddr);
1351432Svi117747 	if (fp == NULL) {
1352432Svi117747 		/* not included; add it now */
1353*1676Sjpk 		err = sctp_add_faddr_first(sctp, hdrsaddr, KM_NOSLEEP);
1354*1676Sjpk 		if (err != 0)
1355*1676Sjpk 			return (err);
13560Sstevel@tonic-gate 
1357432Svi117747 		/* sctp_faddrs will be the hdr addr */
1358432Svi117747 		fp = sctp->sctp_faddrs;
13590Sstevel@tonic-gate 	}
1360432Svi117747 	/* make the header addr the primary */
1361852Svi117747 
1362852Svi117747 	if (cl_sctp_assoc_change != NULL && psctp == NULL)
1363852Svi117747 		curaddr = sctp->sctp_current->faddr;
1364852Svi117747 
1365432Svi117747 	sctp->sctp_primary = fp;
1366432Svi117747 	sctp->sctp_current = fp;
1367432Svi117747 	sctp->sctp_mss = fp->sfa_pmss;
13680Sstevel@tonic-gate 
1369432Svi117747 	/* For loopback connections & linklocal get address from the header */
1370432Svi117747 	if (sctp->sctp_loopback || sctp->sctp_linklocal) {
1371432Svi117747 		if (sctp->sctp_nsaddrs != 0)
1372432Svi117747 			sctp_free_saddrs(sctp);
1373852Svi117747 		if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0)
1374432Svi117747 			return (err);
1375432Svi117747 		/* For loopback ignore address list */
1376432Svi117747 		if (sctp->sctp_loopback)
1377432Svi117747 			return (0);
1378432Svi117747 		check_saddr = B_FALSE;
1379432Svi117747 	}
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
13820Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
13830Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
13840Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
1385432Svi117747 		if (check_saddr) {
1386432Svi117747 			sctp_check_saddr(sctp, supp_af, psctp == NULL ?
1387432Svi117747 			    B_FALSE : B_TRUE);
1388432Svi117747 		}
1389852Svi117747 		ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1390432Svi117747 		return (0);
13910Sstevel@tonic-gate 	}
1392432Svi117747 
13930Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
13940Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
13950Sstevel@tonic-gate 
1396432Svi117747 	/* params will have already been byteordered when validating */
13970Sstevel@tonic-gate 	while (ph != NULL) {
1398432Svi117747 		if (ph->sph_type == htons(PARM_SUPP_ADDRS)) {
1399432Svi117747 			int		plen;
1400432Svi117747 			uint16_t	*p;
1401432Svi117747 			uint16_t	addrtype;
1402432Svi117747 
1403432Svi117747 			ASSERT(psctp != NULL);
1404432Svi117747 			plen = ntohs(ph->sph_len);
1405432Svi117747 			p = (uint16_t *)(ph + 1);
1406432Svi117747 			while (plen > 0) {
1407432Svi117747 				addrtype = ntohs(*p);
1408432Svi117747 				switch (addrtype) {
1409432Svi117747 					case PARM_ADDR6:
1410432Svi117747 						supp_af |= PARM_SUPP_V6;
1411432Svi117747 						break;
1412432Svi117747 					case PARM_ADDR4:
1413432Svi117747 						supp_af |= PARM_SUPP_V4;
1414432Svi117747 						break;
1415432Svi117747 					default:
1416432Svi117747 						break;
1417432Svi117747 				}
1418432Svi117747 				p++;
1419432Svi117747 				plen -= sizeof (*p);
1420432Svi117747 			}
1421432Svi117747 		} else if (ph->sph_type == htons(PARM_ADDR4)) {
14220Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
14230Sstevel@tonic-gate 				in6_addr_t addr;
14240Sstevel@tonic-gate 				ipaddr_t ta;
14250Sstevel@tonic-gate 
1426432Svi117747 				supp_af |= PARM_SUPP_V4;
14270Sstevel@tonic-gate 				/*
14280Sstevel@tonic-gate 				 * Screen out broad/multicasts & loopback.
14290Sstevel@tonic-gate 				 * If the endpoint only accepts v6 address,
14300Sstevel@tonic-gate 				 * go to the next one.
14310Sstevel@tonic-gate 				 */
14320Sstevel@tonic-gate 				bcopy(ph + 1, &ta, sizeof (ta));
14330Sstevel@tonic-gate 				if (ta == 0 ||
14340Sstevel@tonic-gate 				    ta == INADDR_BROADCAST ||
14350Sstevel@tonic-gate 				    ta == htonl(INADDR_LOOPBACK) ||
14360Sstevel@tonic-gate 				    IN_MULTICAST(ta) ||
14370Sstevel@tonic-gate 				    sctp->sctp_connp->conn_ipv6_v6only) {
14380Sstevel@tonic-gate 					goto next;
14390Sstevel@tonic-gate 				}
14400Sstevel@tonic-gate 				/*
14410Sstevel@tonic-gate 				 * XXX also need to check for subnet
14420Sstevel@tonic-gate 				 * broadcasts. This should probably
14430Sstevel@tonic-gate 				 * wait until we have full access
14440Sstevel@tonic-gate 				 * to the ILL tables.
14450Sstevel@tonic-gate 				 */
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
14480Sstevel@tonic-gate 				    (ph + 1), &addr);
14490Sstevel@tonic-gate 				/* Check for duplicate. */
14500Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
14510Sstevel@tonic-gate 					goto next;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 				/* OK, add it to the faddr set */
1454*1676Sjpk 				err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP);
1455*1676Sjpk 				if (err != 0)
1456*1676Sjpk 					return (err);
14570Sstevel@tonic-gate 			}
14580Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
14590Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
14600Sstevel@tonic-gate 			/* An v4 socket should not take v6 addresses. */
14610Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
14620Sstevel@tonic-gate 				in6_addr_t *addr6;
14630Sstevel@tonic-gate 
1464432Svi117747 				supp_af |= PARM_SUPP_V6;
14650Sstevel@tonic-gate 				addr6 = (in6_addr_t *)(ph + 1);
14660Sstevel@tonic-gate 				/*
14670Sstevel@tonic-gate 				 * Screen out link locals, mcast, loopback
14680Sstevel@tonic-gate 				 * and bogus v6 address.
14690Sstevel@tonic-gate 				 */
14700Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
14710Sstevel@tonic-gate 				    IN6_IS_ADDR_MULTICAST(addr6) ||
14720Sstevel@tonic-gate 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
14730Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
14740Sstevel@tonic-gate 					goto next;
14750Sstevel@tonic-gate 				}
14760Sstevel@tonic-gate 				/* Check for duplicate. */
14770Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
14780Sstevel@tonic-gate 					goto next;
14790Sstevel@tonic-gate 
1480*1676Sjpk 				err = sctp_add_faddr(sctp,
1481*1676Sjpk 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP);
1482*1676Sjpk 				if (err != 0)
1483*1676Sjpk 					return (err);
14840Sstevel@tonic-gate 			}
14850Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
14860Sstevel@tonic-gate 			if (sctp_options != NULL)
14870Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
14880Sstevel@tonic-gate 		} /* else; skip */
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate next:
14910Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
14920Sstevel@tonic-gate 	}
1493432Svi117747 	if (check_saddr) {
1494432Svi117747 		sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE :
1495432Svi117747 		    B_TRUE);
14960Sstevel@tonic-gate 	}
1497852Svi117747 	ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1498852Svi117747 	/*
1499852Svi117747 	 * We have the right address list now, update clustering's
1500852Svi117747 	 * knowledge because when we sent the INIT we had just added
1501852Svi117747 	 * the address the INIT was sent to.
1502852Svi117747 	 */
1503852Svi117747 	if (psctp == NULL && cl_sctp_assoc_change != NULL) {
1504852Svi117747 		uchar_t	*alist;
1505852Svi117747 		size_t	asize;
1506852Svi117747 		uchar_t	*dlist;
1507852Svi117747 		size_t	dsize;
1508852Svi117747 
1509852Svi117747 		asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
1510852Svi117747 		alist = kmem_alloc(asize, KM_NOSLEEP);
1511852Svi117747 		if (alist == NULL)
1512852Svi117747 			return (ENOMEM);
1513852Svi117747 		/*
1514852Svi117747 		 * Just include the address the INIT was sent to in the
1515852Svi117747 		 * delete list and send the entire faddr list. We could
1516852Svi117747 		 * do it differently (i.e include all the addresses in the
1517852Svi117747 		 * add list even if it contains the original address OR
1518852Svi117747 		 * remove the original address from the add list etc.), but
1519852Svi117747 		 * this seems reasonable enough.
1520852Svi117747 		 */
1521852Svi117747 		dsize = sizeof (in6_addr_t);
1522852Svi117747 		dlist = kmem_alloc(dsize, KM_NOSLEEP);
1523852Svi117747 		if (dlist == NULL) {
1524852Svi117747 			kmem_free(alist, asize);
1525852Svi117747 			return (ENOMEM);
1526852Svi117747 		}
1527852Svi117747 		bcopy(&curaddr, dlist, sizeof (curaddr));
1528852Svi117747 		sctp_get_faddr_list(sctp, alist, asize);
1529852Svi117747 		(*cl_sctp_assoc_change)(sctp->sctp_family, alist, asize,
1530852Svi117747 		    sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR,
1531852Svi117747 		    (cl_sctp_handle_t)sctp);
1532852Svi117747 		/* alist and dlist will be freed by the clustering module */
1533852Svi117747 	}
15340Sstevel@tonic-gate 	return (0);
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate /*
15380Sstevel@tonic-gate  * Returns 0 if the check failed and the restart should be refused,
15390Sstevel@tonic-gate  * 1 if the check succeeded.
15400Sstevel@tonic-gate  */
15410Sstevel@tonic-gate int
15420Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
15430Sstevel@tonic-gate     int sleep)
15440Sstevel@tonic-gate {
15450Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpa, *fphead = NULL;
15460Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
15470Sstevel@tonic-gate 	ssize_t remaining;
15480Sstevel@tonic-gate 	int isv4;
15490Sstevel@tonic-gate 	ipha_t *iph;
15500Sstevel@tonic-gate 	ip6_t *ip6h;
15510Sstevel@tonic-gate 	in6_addr_t hdraddr[1];
15520Sstevel@tonic-gate 	int retval = 0;
15530Sstevel@tonic-gate 	sctp_tf_t *tf;
15540Sstevel@tonic-gate 	sctp_t *sctp;
15550Sstevel@tonic-gate 	int compres;
15560Sstevel@tonic-gate 	sctp_init_chunk_t *init;
15570Sstevel@tonic-gate 	int nadded = 0;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/* extract the address from the IP header */
15600Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
15610Sstevel@tonic-gate 	if (isv4) {
15620Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
15630Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
15640Sstevel@tonic-gate 	} else {
15650Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
15660Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
15670Sstevel@tonic-gate 	}
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
15700Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
15710Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
15720Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
15730Sstevel@tonic-gate 		/* no parameters; restart OK */
15740Sstevel@tonic-gate 		return (1);
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
15770Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	while (ph != NULL) {
15800Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
15810Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
15820Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
15830Sstevel@tonic-gate 				in6_addr_t addr;
15840Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
15850Sstevel@tonic-gate 				    (ph + 1), &addr);
15860Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15870Sstevel@tonic-gate 				    sleep);
15880Sstevel@tonic-gate 				if (!fpa) {
15890Sstevel@tonic-gate 					goto done;
15900Sstevel@tonic-gate 				}
15910Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
15920Sstevel@tonic-gate 				fpa->faddr = addr;
15930Sstevel@tonic-gate 				fpa->next = NULL;
15940Sstevel@tonic-gate 			}
15950Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
15960Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
15970Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15980Sstevel@tonic-gate 				    sleep);
15990Sstevel@tonic-gate 				if (!fpa) {
16000Sstevel@tonic-gate 					goto done;
16010Sstevel@tonic-gate 				}
16020Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
16030Sstevel@tonic-gate 				bcopy(ph + 1, &fpa->faddr,
16040Sstevel@tonic-gate 				    sizeof (fpa->faddr));
16050Sstevel@tonic-gate 				fpa->next = NULL;
16060Sstevel@tonic-gate 			}
16070Sstevel@tonic-gate 		} else {
16080Sstevel@tonic-gate 			/* else not addr param; skip */
16090Sstevel@tonic-gate 			fpa = NULL;
16100Sstevel@tonic-gate 		}
16110Sstevel@tonic-gate 		/* link in the new addr, if it was an addr param */
16120Sstevel@tonic-gate 		if (fpa) {
16130Sstevel@tonic-gate 			if (!fphead) {
16140Sstevel@tonic-gate 				fphead = fpa;
16150Sstevel@tonic-gate 				fp = fphead;
16160Sstevel@tonic-gate 			} else {
16170Sstevel@tonic-gate 				fp->next = fpa;
16180Sstevel@tonic-gate 				fp = fpa;
16190Sstevel@tonic-gate 			}
16200Sstevel@tonic-gate 		}
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
16230Sstevel@tonic-gate 	}
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	if (fphead == NULL) {
16260Sstevel@tonic-gate 		/* no addr parameters; restart OK */
16270Sstevel@tonic-gate 		return (1);
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	/*
16310Sstevel@tonic-gate 	 * got at least one; make sure the header's addr is
16320Sstevel@tonic-gate 	 * in the list
16330Sstevel@tonic-gate 	 */
16340Sstevel@tonic-gate 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
16350Sstevel@tonic-gate 	if (!fp) {
16360Sstevel@tonic-gate 		/* not included; add it now */
16370Sstevel@tonic-gate 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
16380Sstevel@tonic-gate 		if (!fp) {
16390Sstevel@tonic-gate 			goto done;
16400Sstevel@tonic-gate 		}
16410Sstevel@tonic-gate 		bzero(fp, sizeof (*fp));
16420Sstevel@tonic-gate 		fp->faddr = *hdraddr;
16430Sstevel@tonic-gate 		fp->next = fphead;
16440Sstevel@tonic-gate 		fphead = fp;
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	/*
16480Sstevel@tonic-gate 	 * Now, we can finally do the check: For each sctp instance
16490Sstevel@tonic-gate 	 * on the hash line for ports, compare its faddr set against
16500Sstevel@tonic-gate 	 * the new one. If the new one is a strict subset of any
16510Sstevel@tonic-gate 	 * existing sctp's faddrs, the restart is OK. However, if there
16520Sstevel@tonic-gate 	 * is an overlap, this could be an attack, so return failure.
16530Sstevel@tonic-gate 	 * If all sctp's faddrs are disjoint, this is a legitimate new
16540Sstevel@tonic-gate 	 * association.
16550Sstevel@tonic-gate 	 */
16560Sstevel@tonic-gate 	tf = &(sctp_conn_fanout[SCTP_CONN_HASH(ports)]);
16570Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
16600Sstevel@tonic-gate 		if (ports != sctp->sctp_ports) {
16610Sstevel@tonic-gate 			continue;
16620Sstevel@tonic-gate 		}
16630Sstevel@tonic-gate 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
16640Sstevel@tonic-gate 		if (compres <= SCTP_ADDR_SUBSET) {
16650Sstevel@tonic-gate 			retval = 1;
16660Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16670Sstevel@tonic-gate 			goto done;
16680Sstevel@tonic-gate 		}
16690Sstevel@tonic-gate 		if (compres == SCTP_ADDR_OVERLAP) {
16700Sstevel@tonic-gate 			dprint(1,
16710Sstevel@tonic-gate 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
1672*1676Sjpk 			    SCTP_PRINTADDR(*hdraddr), (void *)sctp));
16730Sstevel@tonic-gate 			/*
16740Sstevel@tonic-gate 			 * While we still hold the lock, we need to
16750Sstevel@tonic-gate 			 * figure out which addresses have been
16760Sstevel@tonic-gate 			 * added so we can include them in the abort
16770Sstevel@tonic-gate 			 * we will send back. Since these faddrs will
16780Sstevel@tonic-gate 			 * never be used, we overload the rto field
16790Sstevel@tonic-gate 			 * here, setting it to 0 if the address was
16800Sstevel@tonic-gate 			 * not added, 1 if it was added.
16810Sstevel@tonic-gate 			 */
16820Sstevel@tonic-gate 			for (fp = fphead; fp; fp = fp->next) {
16830Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &fp->faddr)) {
16840Sstevel@tonic-gate 					fp->rto = 0;
16850Sstevel@tonic-gate 				} else {
16860Sstevel@tonic-gate 					fp->rto = 1;
16870Sstevel@tonic-gate 					nadded++;
16880Sstevel@tonic-gate 				}
16890Sstevel@tonic-gate 			}
16900Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16910Sstevel@tonic-gate 			goto done;
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 	}
16940Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	/* All faddrs are disjoint; legit new association */
16970Sstevel@tonic-gate 	retval = 1;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate done:
17000Sstevel@tonic-gate 	/* If are attempted adds, send back an abort listing the addrs */
17010Sstevel@tonic-gate 	if (nadded > 0) {
17020Sstevel@tonic-gate 		void *dtail;
17030Sstevel@tonic-gate 		size_t dlen;
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
17060Sstevel@tonic-gate 		if (dtail == NULL) {
17070Sstevel@tonic-gate 			goto cleanup;
17080Sstevel@tonic-gate 		}
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 		ph = dtail;
17110Sstevel@tonic-gate 		dlen = 0;
17120Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fp->next) {
17130Sstevel@tonic-gate 			if (fp->rto == 0) {
17140Sstevel@tonic-gate 				continue;
17150Sstevel@tonic-gate 			}
17160Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
17170Sstevel@tonic-gate 				ipaddr_t addr4;
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR4);
17200Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR4_LEN);
17210Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
17220Sstevel@tonic-gate 				ph++;
17230Sstevel@tonic-gate 				bcopy(&addr4, ph, sizeof (addr4));
17240Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
17250Sstevel@tonic-gate 				    ((char *)ph + sizeof (addr4));
17260Sstevel@tonic-gate 				dlen += PARM_ADDR4_LEN;
17270Sstevel@tonic-gate 			} else {
17280Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR6);
17290Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR6_LEN);
17300Sstevel@tonic-gate 				ph++;
17310Sstevel@tonic-gate 				bcopy(&fp->faddr, ph, sizeof (fp->faddr));
17320Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
17330Sstevel@tonic-gate 				    ((char *)ph + sizeof (fp->faddr));
17340Sstevel@tonic-gate 				dlen += PARM_ADDR6_LEN;
17350Sstevel@tonic-gate 			}
17360Sstevel@tonic-gate 		}
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 		/* Send off the abort */
17390Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ich),
17400Sstevel@tonic-gate 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE);
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate cleanup:
17460Sstevel@tonic-gate 	/* Clean up */
17470Sstevel@tonic-gate 	if (fphead) {
17480Sstevel@tonic-gate 		sctp_faddr_t *fpn;
17490Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fpn) {
17500Sstevel@tonic-gate 			fpn = fp->next;
17510Sstevel@tonic-gate 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
17520Sstevel@tonic-gate 		}
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	return (retval);
17560Sstevel@tonic-gate }
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate void
17590Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp)
17600Sstevel@tonic-gate {
17610Sstevel@tonic-gate 	sctp_faddr_t *fp;
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
17640Sstevel@tonic-gate 		fp->ssthresh = sctp_initial_mtu;
17650Sstevel@tonic-gate 		fp->cwnd = fp->sfa_pmss * sctp_slow_start_initial;
17660Sstevel@tonic-gate 		fp->suna = 0;
17670Sstevel@tonic-gate 		fp->pba = 0;
17680Sstevel@tonic-gate 	}
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate static void
17720Sstevel@tonic-gate sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr)
17730Sstevel@tonic-gate {
17740Sstevel@tonic-gate 	bcopy(addr, &fp->faddr, sizeof (*addr));
17750Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
17760Sstevel@tonic-gate 		fp->isv4 = 1;
17770Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
17780Sstevel@tonic-gate 		fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr_len) &
17790Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
17800Sstevel@tonic-gate 	} else {
17810Sstevel@tonic-gate 		fp->isv4 = 0;
17820Sstevel@tonic-gate 		fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr6_len) &
17830Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
17840Sstevel@tonic-gate 	}
17850Sstevel@tonic-gate 	fp->cwnd = sctp_slow_start_initial * fp->sfa_pmss;
17860Sstevel@tonic-gate 	fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max);
17870Sstevel@tonic-gate 	fp->srtt = -1;
17880Sstevel@tonic-gate 	fp->rtt_updates = 0;
17890Sstevel@tonic-gate 	fp->strikes = 0;
17900Sstevel@tonic-gate 	fp->max_retr = sctp->sctp_pp_max_rxt;
17910Sstevel@tonic-gate 	/* Mark it as not confirmed. */
17920Sstevel@tonic-gate 	fp->state = SCTP_FADDRS_UNCONFIRMED;
17930Sstevel@tonic-gate 	fp->hb_interval = sctp->sctp_hb_interval;
17940Sstevel@tonic-gate 	fp->ssthresh = sctp_initial_ssthresh;
17950Sstevel@tonic-gate 	fp->suna = 0;
17960Sstevel@tonic-gate 	fp->pba = 0;
17970Sstevel@tonic-gate 	fp->acked = 0;
17980Sstevel@tonic-gate 	fp->lastactive = lbolt64;
17990Sstevel@tonic-gate 	fp->timer_mp = NULL;
18000Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
18010Sstevel@tonic-gate 	fp->timer_running = 0;
18020Sstevel@tonic-gate 	fp->df = 1;
18030Sstevel@tonic-gate 	fp->pmtu_discovered = 0;
18040Sstevel@tonic-gate 	fp->rc_timer_mp = NULL;
18050Sstevel@tonic-gate 	fp->rc_timer_running = 0;
18060Sstevel@tonic-gate 	fp->next = NULL;
18070Sstevel@tonic-gate 	fp->ire = NULL;
18080Sstevel@tonic-gate 	fp->T3expire = 0;
18090Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret,
18100Sstevel@tonic-gate 	    sizeof (fp->hb_secret));
18110Sstevel@tonic-gate 	fp->hb_expiry = lbolt64;
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	sctp_ire2faddr(sctp, fp);
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate /*ARGSUSED*/
18170Sstevel@tonic-gate static void
18180Sstevel@tonic-gate faddr_destructor(void *buf, void *cdrarg)
18190Sstevel@tonic-gate {
18200Sstevel@tonic-gate 	sctp_faddr_t *fp = buf;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	ASSERT(fp->timer_mp == NULL);
18230Sstevel@tonic-gate 	ASSERT(fp->timer_running == 0);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	ASSERT(fp->rc_timer_mp == NULL);
18260Sstevel@tonic-gate 	ASSERT(fp->rc_timer_running == 0);
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate void
1830*1676Sjpk sctp_faddr_init(void)
18310Sstevel@tonic-gate {
18320Sstevel@tonic-gate 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
18330Sstevel@tonic-gate 	    sizeof (sctp_faddr_t), 0, NULL, faddr_destructor,
18340Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
18350Sstevel@tonic-gate }
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate void
1838*1676Sjpk sctp_faddr_fini(void)
18390Sstevel@tonic-gate {
18400Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_faddr_cache);
18410Sstevel@tonic-gate }
1842