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 /*
23*116Skcpoon  * 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) {
1410Sstevel@tonic-gate 			fp->saddr = laddr;
1420Sstevel@tonic-gate 		} else {
1430Sstevel@tonic-gate 			ip2dbg(("ire2faddr: src addr is not part of assc\n"));
1440Sstevel@tonic-gate 			set_saddr(sctp, fp, B_FALSE);
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 	} else {
1470Sstevel@tonic-gate 		ire = ire_cache_lookup_v6(&fp->faddr, sctp->sctp_zoneid);
1480Sstevel@tonic-gate 		if (ire == NULL) {
1490Sstevel@tonic-gate 			dprint(3, ("ire2faddr: no ire for %x:%x:%x:%x\n",
1500Sstevel@tonic-gate 			    SCTP_PRINTADDR(fp->faddr)));
1510Sstevel@tonic-gate 			set_saddr(sctp, fp, B_TRUE);
1520Sstevel@tonic-gate 			goto set_current;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		ipif_seqid = ire->ire_ipif->ipif_seqid;
1550Sstevel@tonic-gate 		dprint(2, ("ire2faddr: got ire for %x:%x:%x:%x, ",
1560Sstevel@tonic-gate 		    SCTP_PRINTADDR(fp->faddr)));
1570Sstevel@tonic-gate 		dprint(2, ("src=%x:%x:%x:%x\n",
1580Sstevel@tonic-gate 		    SCTP_PRINTADDR(ire->ire_src_addr_v6)));
1590Sstevel@tonic-gate 		laddr = ire->ire_src_addr_v6;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		/* make sure the laddr is part of this association */
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if ((sp = sctp_ipif_lookup(sctp, ipif_seqid)) !=
1640Sstevel@tonic-gate 		    NULL && !sp->saddr_ipif_dontsrc) {
1650Sstevel@tonic-gate 			fp->saddr = laddr;
1660Sstevel@tonic-gate 		} else {
1670Sstevel@tonic-gate 			dprint(2, ("ire2faddr: src addr is not part "
1680Sstevel@tonic-gate 				"of assc\n"));
1690Sstevel@tonic-gate 			set_saddr(sctp, fp, B_TRUE);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/* Cache the IRE */
1740Sstevel@tonic-gate 	IRE_REFHOLD_NOTR(ire);
1750Sstevel@tonic-gate 	fp->ire = ire;
1760Sstevel@tonic-gate 	if (fp->ire->ire_type == IRE_LOOPBACK && !sctp->sctp_loopback)
1770Sstevel@tonic-gate 		sctp->sctp_loopback = 1;
1780Sstevel@tonic-gate 	IRE_REFRELE(ire);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * Pull out RTO information for this faddr and use it if we don't
1820Sstevel@tonic-gate 	 * have any yet.
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	if (fp->srtt == -1 && ire->ire_uinfo.iulp_rtt != 0) {
185*116Skcpoon 		/* The cached value is in ms. */
186*116Skcpoon 		fp->srtt = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt);
187*116Skcpoon 		fp->rttvar = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt_sd);
1880Sstevel@tonic-gate 		fp->rto = 3 * fp->srtt;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		/* Bound the RTO by configured min and max values */
1910Sstevel@tonic-gate 		if (fp->rto < sctp->sctp_rto_min) {
1920Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_min;
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		if (fp->rto > sctp->sctp_rto_max) {
1950Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_max;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * Record the MTU for this faddr. If the MTU for this faddr has
2010Sstevel@tonic-gate 	 * changed, check if the assc MTU will also change.
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	if (fp->isv4) {
2040Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
2050Sstevel@tonic-gate 	} else {
2060Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	if ((fp->sfa_pmss + hdrlen) != ire->ire_max_frag) {
2090Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
2100Sstevel@tonic-gate 		fp->sfa_pmss = (ire->ire_max_frag - hdrlen) & ~(SCTP_ALIGN - 1);
2110Sstevel@tonic-gate 		if (fp->cwnd < (fp->sfa_pmss * 2)) {
2120Sstevel@tonic-gate 			fp->cwnd = fp->sfa_pmss * sctp_slow_start_initial;
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate set_current:
2170Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
2180Sstevel@tonic-gate 		sctp_faddr2hdraddr(fp, sctp);
2190Sstevel@tonic-gate 		sctp->sctp_mss = fp->sfa_pmss;
2200Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
2210Sstevel@tonic-gate 			sctp_set_ulp_prop(sctp);
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*ARGSUSED*/
2270Sstevel@tonic-gate void
2280Sstevel@tonic-gate sctp_faddr2ire(sctp_t *sctp, sctp_faddr_t *fp)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	ire_t *ire;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if ((ire = fp->ire) == NULL) {
2330Sstevel@tonic-gate 		return;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/* If the cached IRE is going sway, there is no point to update it. */
2390Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_CONDEMNED) {
2400Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
2410Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
2420Sstevel@tonic-gate 		fp->ire = NULL;
2430Sstevel@tonic-gate 		return;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/*
2470Sstevel@tonic-gate 	 * Only record the PMTU for this faddr if we actually have
2480Sstevel@tonic-gate 	 * done discovery. This prevents initialized default from
2490Sstevel@tonic-gate 	 * clobbering any real info that IP may have.
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	if (fp->pmtu_discovered) {
2520Sstevel@tonic-gate 		if (fp->isv4) {
2530Sstevel@tonic-gate 			ire->ire_max_frag = fp->sfa_pmss + sctp->sctp_hdr_len;
2540Sstevel@tonic-gate 		} else {
2550Sstevel@tonic-gate 			ire->ire_max_frag = fp->sfa_pmss + sctp->sctp_hdr6_len;
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
259*116Skcpoon 	if (sctp_rtt_updates != 0 && fp->rtt_updates >= sctp_rtt_updates) {
2600Sstevel@tonic-gate 		/*
2610Sstevel@tonic-gate 		 * If there is no old cached values, initialize them
2620Sstevel@tonic-gate 		 * conservatively.  Set them to be (1.5 * new value).
263*116Skcpoon 		 * This code copied from ip_ire_advise().  The cached
264*116Skcpoon 		 * value is in ms.
2650Sstevel@tonic-gate 		 */
2660Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt != 0) {
2670Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt +
268*116Skcpoon 			    TICK_TO_MSEC(fp->srtt)) >> 1;
2690Sstevel@tonic-gate 		} else {
270*116Skcpoon 			ire->ire_uinfo.iulp_rtt = TICK_TO_MSEC(fp->srtt +
271*116Skcpoon 			    (fp->srtt >> 1));
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt_sd != 0) {
2740Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd =
2750Sstevel@tonic-gate 			    (ire->ire_uinfo.iulp_rtt_sd +
276*116Skcpoon 			    TICK_TO_MSEC(fp->rttvar)) >> 1;
2770Sstevel@tonic-gate 		} else {
278*116Skcpoon 			ire->ire_uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->rttvar +
279*116Skcpoon 			    (fp->rttvar >> 1));
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 		fp->rtt_updates = 0;
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * The sender must set the total length in the IP header.
2890Sstevel@tonic-gate  * If sendto == NULL, the current will be used.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate mblk_t *
2920Sstevel@tonic-gate sctp_make_mp(sctp_t *sctp, sctp_faddr_t *sendto, int trailer)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	mblk_t *mp;
2950Sstevel@tonic-gate 	size_t ipsctplen;
2960Sstevel@tonic-gate 	int isv4;
2970Sstevel@tonic-gate 	sctp_faddr_t *fp;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL || sendto != NULL);
3000Sstevel@tonic-gate 	if (sendto == NULL) {
3010Sstevel@tonic-gate 		fp = sctp->sctp_current;
3020Sstevel@tonic-gate 	} else {
3030Sstevel@tonic-gate 		fp = sendto;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	isv4 = fp->isv4;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/* Try to look for another IRE again. */
3080Sstevel@tonic-gate 	if (fp->ire == NULL)
3090Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/* There is no suitable source address to use, return. */
3120Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH)
3130Sstevel@tonic-gate 		return (NULL);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (isv4) {
3160Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr_len;
3170Sstevel@tonic-gate 	} else {
3180Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr6_len;
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	mp = allocb(ipsctplen + sctp_wroff_xtra + trailer, BPRI_MED);
3220Sstevel@tonic-gate 	if (mp == NULL) {
3230Sstevel@tonic-gate 		ip1dbg(("sctp_make_mp: error makign mp..\n"));
3240Sstevel@tonic-gate 		return (NULL);
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 	mp->b_rptr += sctp_wroff_xtra;
3270Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + ipsctplen;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_wptr));
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (isv4) {
3320Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)mp->b_rptr;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
3350Sstevel@tonic-gate 		if (fp != sctp->sctp_current) {
3360Sstevel@tonic-gate 			/* fiddle with the dst addr */
3370Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
3380Sstevel@tonic-gate 			/* fix up src addr */
3390Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) {
3400Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
3410Sstevel@tonic-gate 				    iph->ipha_src);
3420Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
3430Sstevel@tonic-gate 				iph->ipha_src = INADDR_ANY;
3440Sstevel@tonic-gate 			}
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 		/* set or clear the don't fragment bit */
3470Sstevel@tonic-gate 		if (fp->df) {
3480Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
3490Sstevel@tonic-gate 		} else {
3500Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 	} else {
3530Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
3540Sstevel@tonic-gate 		if (fp != sctp->sctp_current) {
3550Sstevel@tonic-gate 			/* fiddle with the dst addr */
3560Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
3570Sstevel@tonic-gate 			/* fix up src addr */
3580Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
3590Sstevel@tonic-gate 				((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
3600Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
3610Sstevel@tonic-gate 				bzero(&((ip6_t *)(mp->b_rptr))->ip6_src,
3620Sstevel@tonic-gate 				    sizeof (in6_addr_t));
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp != NULL);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * IP will not free this IRE if it is condemned.  SCTP needs to
3700Sstevel@tonic-gate 	 * free it.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
3730Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
3740Sstevel@tonic-gate 		fp->ire = NULL;
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 	/* Stash the conn and ire ptr info. for IP */
3770Sstevel@tonic-gate 	SCTP_STASH_IPINFO(mp, fp->ire);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	return (mp);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  * Notify upper layers about preferred write offset, write size.
3840Sstevel@tonic-gate  */
3850Sstevel@tonic-gate void
3860Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	int hdrlen;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (sctp->sctp_current->isv4) {
3910Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
3920Sstevel@tonic-gate 	} else {
3930Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss);
3980Sstevel@tonic-gate 	sctp->sctp_ulp_prop(sctp->sctp_ulpd,
3990Sstevel@tonic-gate 	    sctp_wroff_xtra + hdrlen + sizeof (sctp_data_hdr_t),
4000Sstevel@tonic-gate 	    sctp->sctp_mss - sizeof (sctp_data_hdr_t));
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate void
4040Sstevel@tonic-gate sctp_set_iplen(sctp_t *sctp, mblk_t *mp)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	uint16_t	sum = 0;
4070Sstevel@tonic-gate 	ipha_t		*iph;
4080Sstevel@tonic-gate 	ip6_t		*ip6h;
4090Sstevel@tonic-gate 	mblk_t		*pmp = mp;
4100Sstevel@tonic-gate 	boolean_t	isv4;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
4130Sstevel@tonic-gate 	for (; pmp; pmp = pmp->b_cont)
4140Sstevel@tonic-gate 		sum += pmp->b_wptr - pmp->b_rptr;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if (isv4) {
4170Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
4180Sstevel@tonic-gate 		iph->ipha_length = htons(sum);
4190Sstevel@tonic-gate 	} else {
4200Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
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  * Returns 0 on success, -1 on memory allocation failure. If sleep
4640Sstevel@tonic-gate  * is true, should never fail.
4650Sstevel@tonic-gate  * Caller must hold conn fanout lock.
4660Sstevel@tonic-gate  */
4670Sstevel@tonic-gate int
4680Sstevel@tonic-gate sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	sctp_faddr_t *faddr;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	dprint(4, ("add_faddr: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr),
4730Sstevel@tonic-gate 	    sleep));
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) {
4760Sstevel@tonic-gate 		return (-1);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	sctp_init_faddr(sctp, faddr, addr);
4800Sstevel@tonic-gate 	ASSERT(faddr->next == NULL);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/* tack it on to the end */
4830Sstevel@tonic-gate 	if (sctp->sctp_lastfaddr != NULL) {
4840Sstevel@tonic-gate 		sctp->sctp_lastfaddr->next = faddr;
4850Sstevel@tonic-gate 	} else {
4860Sstevel@tonic-gate 		/* list is empty */
4870Sstevel@tonic-gate 		ASSERT(sctp->sctp_faddrs == NULL);
4880Sstevel@tonic-gate 		sctp->sctp_faddrs = faddr;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 	sctp->sctp_lastfaddr = faddr;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	return (0);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * Caller must hold conn fanout lock.
4970Sstevel@tonic-gate  */
4980Sstevel@tonic-gate int
4990Sstevel@tonic-gate sctp_add_faddr_first(sctp_t *sctp, in6_addr_t *addr, int sleep)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	sctp_faddr_t *faddr;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	dprint(4, ("add_faddr_first: %x:%x:%x:%x %d\n", SCTP_PRINTADDR(*addr),
5040Sstevel@tonic-gate 	    sleep));
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL) {
5070Sstevel@tonic-gate 		return (-1);
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 	sctp_init_faddr(sctp, faddr, addr);
5100Sstevel@tonic-gate 	ASSERT(faddr->next == NULL);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/* Put it at the beginning of the list */
5130Sstevel@tonic-gate 	if (sctp->sctp_faddrs != NULL) {
5140Sstevel@tonic-gate 		faddr->next = sctp->sctp_faddrs;
5150Sstevel@tonic-gate 	} else {
5160Sstevel@tonic-gate 		sctp->sctp_lastfaddr = faddr;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 	sctp->sctp_faddrs = faddr;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	return (0);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate sctp_faddr_t *
5240Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	sctp_faddr_t *fp;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5290Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr))
5300Sstevel@tonic-gate 			break;
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	return (fp);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate sctp_faddr_t *
5370Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate 	for (; fp; fp = fp->next) {
5400Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) {
5410Sstevel@tonic-gate 			break;
5420Sstevel@tonic-gate 		}
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	return (fp);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate void
5490Sstevel@tonic-gate sctp_faddr2hdraddr(sctp_faddr_t *fp, sctp_t *sctp)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	if (fp->isv4) {
5520Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
5530Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
5540Sstevel@tonic-gate 		/* Must not allow unspec src addr if not bound to all */
5550Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr) &&
5560Sstevel@tonic-gate 		    !sctp->sctp_bound_to_all) {
5570Sstevel@tonic-gate 			/*
5580Sstevel@tonic-gate 			 * set the src to the first v4 saddr and hope
5590Sstevel@tonic-gate 			 * for the best
5600Sstevel@tonic-gate 			 */
5610Sstevel@tonic-gate 			fp->saddr = sctp_get_valid_addr(sctp, B_FALSE);
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src);
5640Sstevel@tonic-gate 		/* update don't fragment bit */
5650Sstevel@tonic-gate 		if (fp->df) {
5660Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
5670Sstevel@tonic-gate 			    htons(IPH_DF);
5680Sstevel@tonic-gate 		} else {
5690Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 	} else {
5720Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = fp->faddr;
5730Sstevel@tonic-gate 		/* Must not allow unspec src addr if not bound to all */
5740Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->saddr) &&
5750Sstevel@tonic-gate 		    !sctp->sctp_bound_to_all) {
5760Sstevel@tonic-gate 			/*
5770Sstevel@tonic-gate 			 * set the src to the first v6 saddr and hope
5780Sstevel@tonic-gate 			 * for the best
5790Sstevel@tonic-gate 			 */
5800Sstevel@tonic-gate 			fp->saddr = sctp_get_valid_addr(sctp, B_TRUE);
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = fp->saddr;
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate void
5870Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	sctp_faddr_t *fp;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5920Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	sctp_faddr2hdraddr(sctp->sctp_current, sctp);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate void
5990Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	int64_t now = lbolt64;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	fp->strikes = 0;
6040Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
6050Sstevel@tonic-gate 	fp->lastactive = now;
6060Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
6070Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
6080Sstevel@tonic-gate 	if (fp->state != SCTP_FADDRS_ALIVE) {
6090Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_ALIVE;
6100Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 		/* If this is the primary, switch back to it now */
6130Sstevel@tonic-gate 		if (fp == sctp->sctp_primary) {
6140Sstevel@tonic-gate 			sctp->sctp_current = fp;
6150Sstevel@tonic-gate 			sctp->sctp_mss = fp->sfa_pmss;
6160Sstevel@tonic-gate 			/* Reset the addrs in the composite header */
6170Sstevel@tonic-gate 			sctp_faddr2hdraddr(fp, sctp);
6180Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
6190Sstevel@tonic-gate 				sctp_set_ulp_prop(sctp);
6200Sstevel@tonic-gate 			}
6210Sstevel@tonic-gate 		}
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	if (fp->ire == NULL) {
6240Sstevel@tonic-gate 		/* Should have a full IRE now */
6250Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate int
6300Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	sctp_faddr_t *fp;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
6350Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) {
6360Sstevel@tonic-gate 			return (1);
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	return (0);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate  * Returns 0 if there is at leave one other active faddr, -1 if there
6450Sstevel@tonic-gate  * are none. If there are none left, faddr_dead() will start killing the
6460Sstevel@tonic-gate  * association.
6470Sstevel@tonic-gate  * If the downed faddr was the current faddr, a new current faddr
6480Sstevel@tonic-gate  * will be chosen.
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate int
6510Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	sctp_faddr_t *ofp;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_ALIVE) {
6560Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0);
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 	fp->state = newstate;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
6610Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), newstate));
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
6640Sstevel@tonic-gate 		/* Current faddr down; need to switch it */
6650Sstevel@tonic-gate 		sctp->sctp_current = NULL;
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/* Find next alive faddr */
6690Sstevel@tonic-gate 	ofp = fp;
6700Sstevel@tonic-gate 	for (fp = fp->next; fp; fp = fp->next) {
6710Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE) {
6720Sstevel@tonic-gate 			break;
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	if (fp == NULL) {
6770Sstevel@tonic-gate 		/* Continue from beginning of list */
6780Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) {
6790Sstevel@tonic-gate 			if (fp->state == SCTP_FADDRS_ALIVE) {
6800Sstevel@tonic-gate 				break;
6810Sstevel@tonic-gate 			}
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if (fp != ofp) {
6860Sstevel@tonic-gate 		if (sctp->sctp_current == NULL) {
6870Sstevel@tonic-gate 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
6880Sstevel@tonic-gate 			    SCTP_PRINTADDR(fp->faddr)));
6890Sstevel@tonic-gate 			sctp->sctp_current = fp;
6900Sstevel@tonic-gate 			sctp->sctp_mss = fp->sfa_pmss;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 			/* Reset the addrs in the composite header */
6930Sstevel@tonic-gate 			sctp_faddr2hdraddr(fp, sctp);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
6960Sstevel@tonic-gate 				sctp_set_ulp_prop(sctp);
6970Sstevel@tonic-gate 			}
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 		return (0);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/* All faddrs are down; kill the association */
7040Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
7050Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpAborted);
7060Sstevel@tonic-gate 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
7070Sstevel@tonic-gate 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
7080Sstevel@tonic-gate 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
7090Sstevel@tonic-gate 	    sctp->sctp_client_errno : ETIMEDOUT);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	return (-1);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate sctp_faddr_t *
7150Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate 	sctp_faddr_t *nfp = NULL;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (ofp == NULL) {
7200Sstevel@tonic-gate 		ofp = sctp->sctp_current;
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	/* Find the next live one */
7240Sstevel@tonic-gate 	for (nfp = ofp->next; nfp != NULL; nfp = nfp->next) {
7250Sstevel@tonic-gate 		if (nfp->state == SCTP_FADDRS_ALIVE) {
7260Sstevel@tonic-gate 			break;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (nfp == NULL) {
7310Sstevel@tonic-gate 		/* Continue from beginning of list */
7320Sstevel@tonic-gate 		for (nfp = sctp->sctp_faddrs; nfp != ofp; nfp = nfp->next) {
7330Sstevel@tonic-gate 			if (nfp->state == SCTP_FADDRS_ALIVE) {
7340Sstevel@tonic-gate 				break;
7350Sstevel@tonic-gate 			}
7360Sstevel@tonic-gate 		}
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	/*
7400Sstevel@tonic-gate 	 * nfp could only be NULL if all faddrs are down, and when
7410Sstevel@tonic-gate 	 * this happens, faddr_dead() should have killed the
7420Sstevel@tonic-gate 	 * association. Hence this assertion...
7430Sstevel@tonic-gate 	 */
7440Sstevel@tonic-gate 	ASSERT(nfp != NULL);
7450Sstevel@tonic-gate 	return (nfp);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate void
7490Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	sctp_faddr_t *fpp;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (!sctp->sctp_faddrs) {
7540Sstevel@tonic-gate 		return;
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if (fp->timer_mp != NULL) {
7580Sstevel@tonic-gate 		sctp_timer_free(fp->timer_mp);
7590Sstevel@tonic-gate 		fp->timer_mp = NULL;
7600Sstevel@tonic-gate 		fp->timer_running = 0;
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 	if (fp->rc_timer_mp != NULL) {
7630Sstevel@tonic-gate 		sctp_timer_free(fp->rc_timer_mp);
7640Sstevel@tonic-gate 		fp->rc_timer_mp = NULL;
7650Sstevel@tonic-gate 		fp->rc_timer_running = 0;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 	if (fp->ire != NULL) {
7680Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
7690Sstevel@tonic-gate 		fp->ire = NULL;
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
7730Sstevel@tonic-gate 		goto gotit;
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next)
7770Sstevel@tonic-gate 		;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate gotit:
7800Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
7810Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
7820Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
7830Sstevel@tonic-gate 		sctp->sctp_faddrs = fp->next;
7840Sstevel@tonic-gate 	} else {
7850Sstevel@tonic-gate 		fpp->next = fp->next;
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
7880Sstevel@tonic-gate 	/* XXX faddr2ire? */
7890Sstevel@tonic-gate 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate void
7930Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpn;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL) {
7980Sstevel@tonic-gate 		ASSERT(sctp->sctp_lastfaddr == NULL);
7990Sstevel@tonic-gate 		return;
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr != NULL);
8030Sstevel@tonic-gate 	sctp->sctp_lastfaddr = NULL;
8040Sstevel@tonic-gate 	sctp->sctp_current = NULL;
8050Sstevel@tonic-gate 	sctp->sctp_primary = NULL;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8100Sstevel@tonic-gate 		/* in conn fanout; need to hold lock */
8110Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
8150Sstevel@tonic-gate 		fpn = fp->next;
8160Sstevel@tonic-gate 		if (fp->ire != NULL)
8170Sstevel@tonic-gate 			IRE_REFRELE_NOTR(fp->ire);
8180Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	sctp->sctp_faddrs = NULL;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8240Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate void
8300Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp)
8310Sstevel@tonic-gate {
8320Sstevel@tonic-gate 	sctp_zap_faddrs(sctp, 0);
8330Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate /*
8370Sstevel@tonic-gate  * Initialize the IPv4 header. Loses any record of any IP options.
8380Sstevel@tonic-gate  */
8390Sstevel@tonic-gate int
8400Sstevel@tonic-gate sctp_header_init_ipv4(sctp_t *sctp, int sleep)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	/*
8450Sstevel@tonic-gate 	 * This is a simple initialization. If there's
8460Sstevel@tonic-gate 	 * already a template, it should never be too small,
8470Sstevel@tonic-gate 	 * so reuse it.  Otherwise, allocate space for the new one.
8480Sstevel@tonic-gate 	 */
8490Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
8500Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc_len >= SCTP_MAX_COMBINED_HEADER_LENGTH);
8510Sstevel@tonic-gate 		bzero(sctp->sctp_iphc, sctp->sctp_iphc_len);
8520Sstevel@tonic-gate 	} else {
8530Sstevel@tonic-gate 		sctp->sctp_iphc_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
8540Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(sctp->sctp_iphc_len, sleep);
8550Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL) {
8560Sstevel@tonic-gate 			sctp->sctp_iphc_len = 0;
8570Sstevel@tonic-gate 			return (ENOMEM);
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	sctp->sctp_hdr_len = sizeof (ipha_t) + sizeof (sctp_hdr_t);
8640Sstevel@tonic-gate 	sctp->sctp_ip_hdr_len = sizeof (ipha_t);
8650Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_length = htons(sizeof (ipha_t) +
8660Sstevel@tonic-gate 	    sizeof (sctp_hdr_t));
8670Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_version_and_hdr_length
8680Sstevel@tonic-gate 		= (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * These two fields should be zero, and are already set above.
8720Sstevel@tonic-gate 	 *
8730Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_ident,
8740Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_fragment_offset_and_flags.
8750Sstevel@tonic-gate 	 */
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_ttl = sctp_ipv4_ttl;
8780Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_protocol = IPPROTO_SCTP;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc + sizeof (ipha_t));
8810Sstevel@tonic-gate 	sctp->sctp_sctph = sctph;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	return (0);
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate /*
8870Sstevel@tonic-gate  * Update sctp_sticky_hdrs based on sctp_sticky_ipp.
8880Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, any sticky extension
8890Sstevel@tonic-gate  * headers, and the maximum size sctp header (to avoid reallocation
8900Sstevel@tonic-gate  * on the fly for additional sctp options).
8910Sstevel@tonic-gate  * Returns failure if can't allocate memory.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate int
8940Sstevel@tonic-gate sctp_build_hdrs(sctp_t *sctp)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	char		*hdrs;
8970Sstevel@tonic-gate 	uint_t		hdrs_len;
8980Sstevel@tonic-gate 	ip6i_t		*ip6i;
8990Sstevel@tonic-gate 	char		buf[SCTP_MAX_HDR_LENGTH];
9000Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
9010Sstevel@tonic-gate 	in6_addr_t	src;
9020Sstevel@tonic-gate 	in6_addr_t	dst;
9030Sstevel@tonic-gate 	uint8_t		hoplimit;
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * save the existing sctp header and source/dest IP addresses
9060Sstevel@tonic-gate 	 */
9070Sstevel@tonic-gate 	bcopy(sctp->sctp_sctph6, buf, sizeof (sctp_hdr_t));
9080Sstevel@tonic-gate 	src = sctp->sctp_ip6h->ip6_src;
9090Sstevel@tonic-gate 	dst = sctp->sctp_ip6h->ip6_dst;
9100Sstevel@tonic-gate 	hoplimit = sctp->sctp_ip6h->ip6_hops;
9110Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp) + SCTP_MAX_HDR_LENGTH;
9120Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
9130Sstevel@tonic-gate 	if (hdrs_len > sctp->sctp_iphc6_len) {
9140Sstevel@tonic-gate 		/* Need to reallocate */
9150Sstevel@tonic-gate 		hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP);
9160Sstevel@tonic-gate 		if (hdrs == NULL)
9170Sstevel@tonic-gate 			return (ENOMEM);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 		if (sctp->sctp_iphc6_len != 0)
9200Sstevel@tonic-gate 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
9210Sstevel@tonic-gate 		sctp->sctp_iphc6 = hdrs;
9220Sstevel@tonic-gate 		sctp->sctp_iphc6_len = hdrs_len;
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 	ip_build_hdrs_v6((uchar_t *)sctp->sctp_iphc6,
9250Sstevel@tonic-gate 	    hdrs_len - SCTP_MAX_HDR_LENGTH, ipp, IPPROTO_SCTP);
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/* Set header fields not in ipp */
9280Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
9290Sstevel@tonic-gate 		ip6i = (ip6i_t *)sctp->sctp_iphc6;
9300Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)&ip6i[1];
9310Sstevel@tonic-gate 	} else {
9320Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * sctp->sctp_ip_hdr_len will include ip6i_t if there is one.
9360Sstevel@tonic-gate 	 */
9370Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = hdrs_len - SCTP_MAX_HDR_LENGTH;
9380Sstevel@tonic-gate 	sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9390Sstevel@tonic-gate 	    sctp->sctp_ip_hdr6_len);
9400Sstevel@tonic-gate 	sctp->sctp_hdr6_len = sctp->sctp_ip_hdr6_len + sizeof (sctp_hdr_t);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	bcopy(buf, sctp->sctp_sctph6, sizeof (sctp_hdr_t));
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_src = src;
9450Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_dst = dst;
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * If IPV6_HOPLIMIT was set in ipp, use that value.
9480Sstevel@tonic-gate 	 * For sticky options, if it does not exist use
9490Sstevel@tonic-gate 	 * the default/saved value (which was set in ip_build_hdrs_v6())
9500Sstevel@tonic-gate 	 * All this as per RFC 2922.
9510Sstevel@tonic-gate 	 */
9520Sstevel@tonic-gate 	if (!(ipp->ipp_fields & IPPF_HOPLIMIT))
9530Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_hops = hoplimit;
9540Sstevel@tonic-gate 	/*
9550Sstevel@tonic-gate 	 * Set the IPv6 header payload length.
9560Sstevel@tonic-gate 	 * If there's an ip6i_t included, don't count it in the length.
9570Sstevel@tonic-gate 	 */
9580Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_plen = sctp->sctp_hdr6_len - IPV6_HDR_LEN;
9590Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I)
9600Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_plen -= sizeof (ip6i_t);
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * If we're setting extension headers after a connection
9630Sstevel@tonic-gate 	 * has been established, and if we have a routing header
9640Sstevel@tonic-gate 	 * among the extension headers, call ip_massage_options_v6 to
9650Sstevel@tonic-gate 	 * manipulate the routing header/ip6_dst set the checksum
9660Sstevel@tonic-gate 	 * difference in the sctp header template.
9670Sstevel@tonic-gate 	 * (This happens in sctp_connect_ipv6 if the routing header
9680Sstevel@tonic-gate 	 * is set prior to the connect.)
9690Sstevel@tonic-gate 	 */
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
9720Sstevel@tonic-gate 	    (sctp->sctp_sticky_ipp.ipp_fields & IPPF_RTHDR)) {
9730Sstevel@tonic-gate 		ip6_rthdr_t *rth;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
9760Sstevel@tonic-gate 		    (uint8_t *)sctp->sctp_sctph6);
9770Sstevel@tonic-gate 		if (rth != NULL)
9780Sstevel@tonic-gate 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth);
9790Sstevel@tonic-gate 	}
9800Sstevel@tonic-gate 	return (0);
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate /*
9840Sstevel@tonic-gate  * Initialize the IPv6 header. Loses any record of any IPv6 extension headers.
9850Sstevel@tonic-gate  */
9860Sstevel@tonic-gate int
9870Sstevel@tonic-gate sctp_header_init_ipv6(sctp_t *sctp, int sleep)
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	/*
9920Sstevel@tonic-gate 	 * This is a simple initialization. If there's
9930Sstevel@tonic-gate 	 * already a template, it should never be too small,
9940Sstevel@tonic-gate 	 * so reuse it. Otherwise, allocate space for the new one.
9950Sstevel@tonic-gate 	 * Ensure that there is enough space to "downgrade" the sctp_t
9960Sstevel@tonic-gate 	 * to an IPv4 sctp_t. This requires having space for a full load
9970Sstevel@tonic-gate 	 * of IPv4 options
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
10000Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc6_len >=
10010Sstevel@tonic-gate 		    SCTP_MAX_COMBINED_HEADER_LENGTH);
10020Sstevel@tonic-gate 		bzero(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
10030Sstevel@tonic-gate 	} else {
10040Sstevel@tonic-gate 		sctp->sctp_iphc6_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
10050Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(sctp->sctp_iphc_len, sleep);
10060Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
10070Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
10080Sstevel@tonic-gate 			return (ENOMEM);
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 	sctp->sctp_hdr6_len = IPV6_HDR_LEN + sizeof (sctp_hdr_t);
10120Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = IPV6_HDR_LEN;
10130Sstevel@tonic-gate 	sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	/* Initialize the header template */
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
10180Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_plen = ntohs(sizeof (sctp_hdr_t));
10190Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_nxt = IPPROTO_SCTP;
10200Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_hops = sctp_ipv6_hoplimit;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc6 + IPV6_HDR_LEN);
10230Sstevel@tonic-gate 	sctp->sctp_sctph6 = sctph;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	return (0);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /*
10290Sstevel@tonic-gate  * XXX implement more sophisticated logic
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate void
10320Sstevel@tonic-gate sctp_set_hdraddrs(sctp_t *sctp)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	sctp_faddr_t *fp;
10350Sstevel@tonic-gate 	int gotv4 = 0;
10360Sstevel@tonic-gate 	int gotv6 = 0;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs != NULL);
10390Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/* Set up using the primary first */
10420Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) {
10430Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->faddr,
10440Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
10450Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
10460Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->saddr,
10470Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_src);
10480Sstevel@tonic-gate 		gotv4 = 1;
10490Sstevel@tonic-gate 		if (sctp->sctp_ipversion == IPV4_VERSION) {
10500Sstevel@tonic-gate 			goto copyports;
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 	} else {
10530Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = sctp->sctp_primary->faddr;
10540Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
10550Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = sctp->sctp_primary->saddr;
10560Sstevel@tonic-gate 		gotv6 = 1;
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
10600Sstevel@tonic-gate 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
10610Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
10620Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_dst);
10630Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
10640Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
10650Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_src);
10660Sstevel@tonic-gate 			gotv4 = 1;
10670Sstevel@tonic-gate 			if (sctp->sctp_ipversion == IPV4_VERSION || gotv6) {
10680Sstevel@tonic-gate 				break;
10690Sstevel@tonic-gate 			}
10700Sstevel@tonic-gate 		} else if (!gotv6) {
10710Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_dst = fp->faddr;
10720Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
10730Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_src = fp->saddr;
10740Sstevel@tonic-gate 			gotv6 = 1;
10750Sstevel@tonic-gate 			if (gotv4) {
10760Sstevel@tonic-gate 				break;
10770Sstevel@tonic-gate 			}
10780Sstevel@tonic-gate 		}
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate copyports:
10820Sstevel@tonic-gate 	/* copy in the ports for good measure */
10830Sstevel@tonic-gate 	sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
10840Sstevel@tonic-gate 	sctp->sctp_sctph->sh_dport = sctp->sctp_fport;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_sport = sctp->sctp_lport;
10870Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_dport = sctp->sctp_fport;
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate void
10910Sstevel@tonic-gate sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	mblk_t *mp;
10940Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
10950Sstevel@tonic-gate 	size_t len;
10960Sstevel@tonic-gate 	int pad;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	len = sizeof (*ph) + ntohs(uph->sph_len);
10990Sstevel@tonic-gate 	if ((pad = len % 4) != 0) {
11000Sstevel@tonic-gate 		pad = 4 - pad;
11010Sstevel@tonic-gate 		len += pad;
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
11040Sstevel@tonic-gate 	if (mp == NULL) {
11050Sstevel@tonic-gate 		return;
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
11090Sstevel@tonic-gate 	ph->sph_type = htons(PARM_UNRECOGNIZED);
11100Sstevel@tonic-gate 	ph->sph_len = htons(len - pad);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	/* copy in the unrecognized parameter */
11130Sstevel@tonic-gate 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
11160Sstevel@tonic-gate 	if (*errmp != NULL) {
11170Sstevel@tonic-gate 		linkb(*errmp, mp);
11180Sstevel@tonic-gate 	} else {
11190Sstevel@tonic-gate 		*errmp = mp;
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate /*
11240Sstevel@tonic-gate  * o Bounds checking
11250Sstevel@tonic-gate  * o Updates remaining
11260Sstevel@tonic-gate  * o Checks alignment
11270Sstevel@tonic-gate  */
11280Sstevel@tonic-gate sctp_parm_hdr_t *
11290Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	int pad;
11320Sstevel@tonic-gate 	uint16_t len;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	len = ntohs(current->sph_len);
11350Sstevel@tonic-gate 	*remaining -= len;
11360Sstevel@tonic-gate 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
11370Sstevel@tonic-gate 		return (NULL);
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
11400Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
11410Sstevel@tonic-gate 		*remaining -= pad;
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
11440Sstevel@tonic-gate 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
11450Sstevel@tonic-gate 	return (current);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate /*
11490Sstevel@tonic-gate  * Sets the address parameters given in the INIT chunk into sctp's
11500Sstevel@tonic-gate  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
11510Sstevel@tonic-gate  * no address parameters in the INIT chunk, a single faddr is created
11520Sstevel@tonic-gate  * from the ip hdr at the beginning of pkt.
11530Sstevel@tonic-gate  * If there already are existing addresses hanging from sctp, merge
11540Sstevel@tonic-gate  * them in, if the old info contains addresses which are not present
11550Sstevel@tonic-gate  * in this new info, get rid of them, and clean the pointers if there's
11560Sstevel@tonic-gate  * messages which have this as their target address.
11570Sstevel@tonic-gate  *
11580Sstevel@tonic-gate  * Returns 0 on success, sys errno on failure
11590Sstevel@tonic-gate  */
11600Sstevel@tonic-gate int
11610Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
11620Sstevel@tonic-gate     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
11630Sstevel@tonic-gate {
11640Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
11650Sstevel@tonic-gate 	ipha_t			*iph;
11660Sstevel@tonic-gate 	ip6_t			*ip6h;
11670Sstevel@tonic-gate 	in6_addr_t		hdraddr[1];
11680Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
11690Sstevel@tonic-gate 	ssize_t			remaining;
11700Sstevel@tonic-gate 	int			isv4;
11710Sstevel@tonic-gate 	int			err;
11720Sstevel@tonic-gate 	sctp_faddr_t		*fp;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if (sctp_options != NULL)
11750Sstevel@tonic-gate 		*sctp_options = 0;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	/* inherit laddrs, if given */
11780Sstevel@tonic-gate 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
11790Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
11820Sstevel@tonic-gate 		if (err != 0)
11830Sstevel@tonic-gate 			return (err);
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	/* extract the address from the IP header */
11870Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
11880Sstevel@tonic-gate 	if (isv4) {
11890Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
11900Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
11910Sstevel@tonic-gate 	} else {
11920Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
11930Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	/* For loopback connections ignore address list */
11970Sstevel@tonic-gate 	if (sctp->sctp_loopback)
11980Sstevel@tonic-gate 		goto get_from_iphdr;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
12010Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
12020Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
12030Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
12040Sstevel@tonic-gate 		/* no parameters */
12050Sstevel@tonic-gate 		goto get_from_iphdr;
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
12080Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	while (ph != NULL) {
12110Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
12120Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
12130Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
12140Sstevel@tonic-gate 				in6_addr_t addr;
12150Sstevel@tonic-gate 				ipaddr_t ta;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 				/*
12180Sstevel@tonic-gate 				 * Screen out broad/multicasts & loopback.
12190Sstevel@tonic-gate 				 * If the endpoint only accepts v6 address,
12200Sstevel@tonic-gate 				 * go to the next one.
12210Sstevel@tonic-gate 				 */
12220Sstevel@tonic-gate 				bcopy(ph + 1, &ta, sizeof (ta));
12230Sstevel@tonic-gate 				if (ta == 0 ||
12240Sstevel@tonic-gate 				    ta == INADDR_BROADCAST ||
12250Sstevel@tonic-gate 				    ta == htonl(INADDR_LOOPBACK) ||
12260Sstevel@tonic-gate 				    IN_MULTICAST(ta) ||
12270Sstevel@tonic-gate 				    sctp->sctp_connp->conn_ipv6_v6only) {
12280Sstevel@tonic-gate 					goto next;
12290Sstevel@tonic-gate 				}
12300Sstevel@tonic-gate 				/*
12310Sstevel@tonic-gate 				 * XXX also need to check for subnet
12320Sstevel@tonic-gate 				 * broadcasts. This should probably
12330Sstevel@tonic-gate 				 * wait until we have full access
12340Sstevel@tonic-gate 				 * to the ILL tables.
12350Sstevel@tonic-gate 				 */
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
12380Sstevel@tonic-gate 				    (ph + 1), &addr);
12390Sstevel@tonic-gate 				/* Check for duplicate. */
12400Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
12410Sstevel@tonic-gate 					goto next;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 				/* OK, add it to the faddr set */
12440Sstevel@tonic-gate 				if (sctp_add_faddr(sctp, &addr,
12450Sstevel@tonic-gate 					KM_NOSLEEP) != 0) {
12460Sstevel@tonic-gate 					return (ENOMEM);
12470Sstevel@tonic-gate 				}
12480Sstevel@tonic-gate 			}
12490Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
12500Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
12510Sstevel@tonic-gate 			/* An v4 socket should not take v6 addresses. */
12520Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
12530Sstevel@tonic-gate 				in6_addr_t *addr6;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 				addr6 = (in6_addr_t *)(ph + 1);
12560Sstevel@tonic-gate 				/*
12570Sstevel@tonic-gate 				 * Screen out link locals, mcast, loopback
12580Sstevel@tonic-gate 				 * and bogus v6 address.
12590Sstevel@tonic-gate 				 */
12600Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
12610Sstevel@tonic-gate 				    IN6_IS_ADDR_MULTICAST(addr6) ||
12620Sstevel@tonic-gate 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
12630Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
12640Sstevel@tonic-gate 					goto next;
12650Sstevel@tonic-gate 				}
12660Sstevel@tonic-gate 				/* Check for duplicate. */
12670Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
12680Sstevel@tonic-gate 					goto next;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 				if (sctp_add_faddr(sctp,
12710Sstevel@tonic-gate 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP) != 0) {
12720Sstevel@tonic-gate 					return (ENOMEM);
12730Sstevel@tonic-gate 				}
12740Sstevel@tonic-gate 			}
12750Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
12760Sstevel@tonic-gate 			if (sctp_options != NULL)
12770Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
12780Sstevel@tonic-gate 		} /* else; skip */
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate next:
12810Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
12820Sstevel@tonic-gate 	}
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate get_from_iphdr:
12850Sstevel@tonic-gate 	/* Make sure the header's addr is in the list */
12860Sstevel@tonic-gate 	fp = sctp_lookup_faddr(sctp, hdraddr);
12870Sstevel@tonic-gate 	if (fp == NULL) {
12880Sstevel@tonic-gate 		/* not included; add it now */
12890Sstevel@tonic-gate 		if (sctp_add_faddr_first(sctp, hdraddr, KM_NOSLEEP) == -1)
12900Sstevel@tonic-gate 			return (ENOMEM);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 		/* sctp_faddrs will be the hdr addr */
12930Sstevel@tonic-gate 		fp = sctp->sctp_faddrs;
12940Sstevel@tonic-gate 	}
12950Sstevel@tonic-gate 	/* make the header addr the primary */
12960Sstevel@tonic-gate 	sctp->sctp_primary = fp;
12970Sstevel@tonic-gate 	sctp->sctp_current = fp;
12980Sstevel@tonic-gate 	sctp->sctp_mss = fp->sfa_pmss;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	return (0);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate /*
13040Sstevel@tonic-gate  * Returns 0 if the check failed and the restart should be refused,
13050Sstevel@tonic-gate  * 1 if the check succeeded.
13060Sstevel@tonic-gate  */
13070Sstevel@tonic-gate int
13080Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
13090Sstevel@tonic-gate     int sleep)
13100Sstevel@tonic-gate {
13110Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpa, *fphead = NULL;
13120Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
13130Sstevel@tonic-gate 	ssize_t remaining;
13140Sstevel@tonic-gate 	int isv4;
13150Sstevel@tonic-gate 	ipha_t *iph;
13160Sstevel@tonic-gate 	ip6_t *ip6h;
13170Sstevel@tonic-gate 	in6_addr_t hdraddr[1];
13180Sstevel@tonic-gate 	int retval = 0;
13190Sstevel@tonic-gate 	sctp_tf_t *tf;
13200Sstevel@tonic-gate 	sctp_t *sctp;
13210Sstevel@tonic-gate 	int compres;
13220Sstevel@tonic-gate 	sctp_init_chunk_t *init;
13230Sstevel@tonic-gate 	int nadded = 0;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/* extract the address from the IP header */
13260Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
13270Sstevel@tonic-gate 	if (isv4) {
13280Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
13290Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
13300Sstevel@tonic-gate 	} else {
13310Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
13320Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
13360Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
13370Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
13380Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
13390Sstevel@tonic-gate 		/* no parameters; restart OK */
13400Sstevel@tonic-gate 		return (1);
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
13430Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	while (ph != NULL) {
13460Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
13470Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
13480Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
13490Sstevel@tonic-gate 				in6_addr_t addr;
13500Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
13510Sstevel@tonic-gate 				    (ph + 1), &addr);
13520Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
13530Sstevel@tonic-gate 				    sleep);
13540Sstevel@tonic-gate 				if (!fpa) {
13550Sstevel@tonic-gate 					goto done;
13560Sstevel@tonic-gate 				}
13570Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
13580Sstevel@tonic-gate 				fpa->faddr = addr;
13590Sstevel@tonic-gate 				fpa->next = NULL;
13600Sstevel@tonic-gate 			}
13610Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
13620Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
13630Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
13640Sstevel@tonic-gate 				    sleep);
13650Sstevel@tonic-gate 				if (!fpa) {
13660Sstevel@tonic-gate 					goto done;
13670Sstevel@tonic-gate 				}
13680Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
13690Sstevel@tonic-gate 				bcopy(ph + 1, &fpa->faddr,
13700Sstevel@tonic-gate 				    sizeof (fpa->faddr));
13710Sstevel@tonic-gate 				fpa->next = NULL;
13720Sstevel@tonic-gate 			}
13730Sstevel@tonic-gate 		} else {
13740Sstevel@tonic-gate 			/* else not addr param; skip */
13750Sstevel@tonic-gate 			fpa = NULL;
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 		/* link in the new addr, if it was an addr param */
13780Sstevel@tonic-gate 		if (fpa) {
13790Sstevel@tonic-gate 			if (!fphead) {
13800Sstevel@tonic-gate 				fphead = fpa;
13810Sstevel@tonic-gate 				fp = fphead;
13820Sstevel@tonic-gate 			} else {
13830Sstevel@tonic-gate 				fp->next = fpa;
13840Sstevel@tonic-gate 				fp = fpa;
13850Sstevel@tonic-gate 			}
13860Sstevel@tonic-gate 		}
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (fphead == NULL) {
13920Sstevel@tonic-gate 		/* no addr parameters; restart OK */
13930Sstevel@tonic-gate 		return (1);
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	/*
13970Sstevel@tonic-gate 	 * got at least one; make sure the header's addr is
13980Sstevel@tonic-gate 	 * in the list
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
14010Sstevel@tonic-gate 	if (!fp) {
14020Sstevel@tonic-gate 		/* not included; add it now */
14030Sstevel@tonic-gate 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
14040Sstevel@tonic-gate 		if (!fp) {
14050Sstevel@tonic-gate 			goto done;
14060Sstevel@tonic-gate 		}
14070Sstevel@tonic-gate 		bzero(fp, sizeof (*fp));
14080Sstevel@tonic-gate 		fp->faddr = *hdraddr;
14090Sstevel@tonic-gate 		fp->next = fphead;
14100Sstevel@tonic-gate 		fphead = fp;
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	/*
14140Sstevel@tonic-gate 	 * Now, we can finally do the check: For each sctp instance
14150Sstevel@tonic-gate 	 * on the hash line for ports, compare its faddr set against
14160Sstevel@tonic-gate 	 * the new one. If the new one is a strict subset of any
14170Sstevel@tonic-gate 	 * existing sctp's faddrs, the restart is OK. However, if there
14180Sstevel@tonic-gate 	 * is an overlap, this could be an attack, so return failure.
14190Sstevel@tonic-gate 	 * If all sctp's faddrs are disjoint, this is a legitimate new
14200Sstevel@tonic-gate 	 * association.
14210Sstevel@tonic-gate 	 */
14220Sstevel@tonic-gate 	tf = &(sctp_conn_fanout[SCTP_CONN_HASH(ports)]);
14230Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
14260Sstevel@tonic-gate 		if (ports != sctp->sctp_ports) {
14270Sstevel@tonic-gate 			continue;
14280Sstevel@tonic-gate 		}
14290Sstevel@tonic-gate 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
14300Sstevel@tonic-gate 		if (compres <= SCTP_ADDR_SUBSET) {
14310Sstevel@tonic-gate 			retval = 1;
14320Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
14330Sstevel@tonic-gate 			goto done;
14340Sstevel@tonic-gate 		}
14350Sstevel@tonic-gate 		if (compres == SCTP_ADDR_OVERLAP) {
14360Sstevel@tonic-gate 			dprint(1,
14370Sstevel@tonic-gate 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
14380Sstevel@tonic-gate 			    SCTP_PRINTADDR(*hdraddr), sctp));
14390Sstevel@tonic-gate 			/*
14400Sstevel@tonic-gate 			 * While we still hold the lock, we need to
14410Sstevel@tonic-gate 			 * figure out which addresses have been
14420Sstevel@tonic-gate 			 * added so we can include them in the abort
14430Sstevel@tonic-gate 			 * we will send back. Since these faddrs will
14440Sstevel@tonic-gate 			 * never be used, we overload the rto field
14450Sstevel@tonic-gate 			 * here, setting it to 0 if the address was
14460Sstevel@tonic-gate 			 * not added, 1 if it was added.
14470Sstevel@tonic-gate 			 */
14480Sstevel@tonic-gate 			for (fp = fphead; fp; fp = fp->next) {
14490Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &fp->faddr)) {
14500Sstevel@tonic-gate 					fp->rto = 0;
14510Sstevel@tonic-gate 				} else {
14520Sstevel@tonic-gate 					fp->rto = 1;
14530Sstevel@tonic-gate 					nadded++;
14540Sstevel@tonic-gate 				}
14550Sstevel@tonic-gate 			}
14560Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
14570Sstevel@tonic-gate 			goto done;
14580Sstevel@tonic-gate 		}
14590Sstevel@tonic-gate 	}
14600Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/* All faddrs are disjoint; legit new association */
14630Sstevel@tonic-gate 	retval = 1;
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate done:
14660Sstevel@tonic-gate 	/* If are attempted adds, send back an abort listing the addrs */
14670Sstevel@tonic-gate 	if (nadded > 0) {
14680Sstevel@tonic-gate 		void *dtail;
14690Sstevel@tonic-gate 		size_t dlen;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
14720Sstevel@tonic-gate 		if (dtail == NULL) {
14730Sstevel@tonic-gate 			goto cleanup;
14740Sstevel@tonic-gate 		}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		ph = dtail;
14770Sstevel@tonic-gate 		dlen = 0;
14780Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fp->next) {
14790Sstevel@tonic-gate 			if (fp->rto == 0) {
14800Sstevel@tonic-gate 				continue;
14810Sstevel@tonic-gate 			}
14820Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
14830Sstevel@tonic-gate 				ipaddr_t addr4;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR4);
14860Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR4_LEN);
14870Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
14880Sstevel@tonic-gate 				ph++;
14890Sstevel@tonic-gate 				bcopy(&addr4, ph, sizeof (addr4));
14900Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
14910Sstevel@tonic-gate 				    ((char *)ph + sizeof (addr4));
14920Sstevel@tonic-gate 				dlen += PARM_ADDR4_LEN;
14930Sstevel@tonic-gate 			} else {
14940Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR6);
14950Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR6_LEN);
14960Sstevel@tonic-gate 				ph++;
14970Sstevel@tonic-gate 				bcopy(&fp->faddr, ph, sizeof (fp->faddr));
14980Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
14990Sstevel@tonic-gate 				    ((char *)ph + sizeof (fp->faddr));
15000Sstevel@tonic-gate 				dlen += PARM_ADDR6_LEN;
15010Sstevel@tonic-gate 			}
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 		/* Send off the abort */
15050Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ich),
15060Sstevel@tonic-gate 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate cleanup:
15120Sstevel@tonic-gate 	/* Clean up */
15130Sstevel@tonic-gate 	if (fphead) {
15140Sstevel@tonic-gate 		sctp_faddr_t *fpn;
15150Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fpn) {
15160Sstevel@tonic-gate 			fpn = fp->next;
15170Sstevel@tonic-gate 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
15180Sstevel@tonic-gate 		}
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	return (retval);
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate void
15250Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp)
15260Sstevel@tonic-gate {
15270Sstevel@tonic-gate 	sctp_faddr_t *fp;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
15300Sstevel@tonic-gate 		fp->ssthresh = sctp_initial_mtu;
15310Sstevel@tonic-gate 		fp->cwnd = fp->sfa_pmss * sctp_slow_start_initial;
15320Sstevel@tonic-gate 		fp->suna = 0;
15330Sstevel@tonic-gate 		fp->pba = 0;
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate /*
15380Sstevel@tonic-gate  * Return zero if the buffers are identical in length and content.
15390Sstevel@tonic-gate  * This is used for comparing extension header buffers.
15400Sstevel@tonic-gate  * Note that an extension header would be declared different
15410Sstevel@tonic-gate  * even if all that changed was the next header value in that header i.e.
15420Sstevel@tonic-gate  * what really changed is the next extension header.
15430Sstevel@tonic-gate  */
15440Sstevel@tonic-gate boolean_t
15450Sstevel@tonic-gate sctp_cmpbuf(void *a, uint_t alen, boolean_t b_valid, void *b, uint_t blen)
15460Sstevel@tonic-gate {
15470Sstevel@tonic-gate 	if (!b_valid)
15480Sstevel@tonic-gate 		blen = 0;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	if (alen != blen)
15510Sstevel@tonic-gate 		return (B_TRUE);
15520Sstevel@tonic-gate 	if (alen == 0)
15530Sstevel@tonic-gate 		return (B_FALSE);	/* Both zero length */
15540Sstevel@tonic-gate 	return (bcmp(a, b, alen));
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate /*
15580Sstevel@tonic-gate  * Preallocate memory for sctp_savebuf(). Returns B_TRUE if ok.
15590Sstevel@tonic-gate  * Return B_FALSE if memory allocation fails - don't change any state!
15600Sstevel@tonic-gate  */
15610Sstevel@tonic-gate boolean_t
15620Sstevel@tonic-gate sctp_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
15630Sstevel@tonic-gate     void *src, uint_t srclen)
15640Sstevel@tonic-gate {
15650Sstevel@tonic-gate 	void *dst;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	if (!src_valid)
15680Sstevel@tonic-gate 		srclen = 0;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	ASSERT(*dstlenp == 0);
15710Sstevel@tonic-gate 	if (src != NULL && srclen != 0) {
15720Sstevel@tonic-gate 		dst = mi_zalloc(srclen);
15730Sstevel@tonic-gate 		if (dst == NULL)
15740Sstevel@tonic-gate 			return (B_FALSE);
15750Sstevel@tonic-gate 	} else {
15760Sstevel@tonic-gate 		dst = NULL;
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 	if (*dstp != NULL) {
15790Sstevel@tonic-gate 		mi_free(*dstp);
15800Sstevel@tonic-gate 		*dstp = NULL;
15810Sstevel@tonic-gate 		*dstlenp = 0;
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 	*dstp = dst;
15840Sstevel@tonic-gate 	if (dst != NULL)
15850Sstevel@tonic-gate 		*dstlenp = srclen;
15860Sstevel@tonic-gate 	else
15870Sstevel@tonic-gate 		*dstlenp = 0;
15880Sstevel@tonic-gate 	return (B_TRUE);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate /*
15920Sstevel@tonic-gate  * Replace what is in *dst, *dstlen with the source.
15930Sstevel@tonic-gate  * Assumes sctp_allocbuf has already been called.
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate void
15960Sstevel@tonic-gate sctp_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
15970Sstevel@tonic-gate     void *src, uint_t srclen)
15980Sstevel@tonic-gate {
15990Sstevel@tonic-gate 	if (!src_valid)
16000Sstevel@tonic-gate 		srclen = 0;
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	ASSERT(*dstlenp == srclen);
16030Sstevel@tonic-gate 	if (src != NULL && srclen != 0) {
16040Sstevel@tonic-gate 		bcopy(src, *dstp, srclen);
16050Sstevel@tonic-gate 	}
16060Sstevel@tonic-gate }
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate static void
16090Sstevel@tonic-gate sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr)
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	bcopy(addr, &fp->faddr, sizeof (*addr));
16120Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
16130Sstevel@tonic-gate 		fp->isv4 = 1;
16140Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
16150Sstevel@tonic-gate 		fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr_len) &
16160Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
16170Sstevel@tonic-gate 	} else {
16180Sstevel@tonic-gate 		fp->isv4 = 0;
16190Sstevel@tonic-gate 		fp->sfa_pmss = (sctp_initial_mtu - sctp->sctp_hdr6_len) &
16200Sstevel@tonic-gate 			~(SCTP_ALIGN - 1);
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 	fp->cwnd = sctp_slow_start_initial * fp->sfa_pmss;
16230Sstevel@tonic-gate 	fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max);
16240Sstevel@tonic-gate 	fp->srtt = -1;
16250Sstevel@tonic-gate 	fp->rtt_updates = 0;
16260Sstevel@tonic-gate 	fp->strikes = 0;
16270Sstevel@tonic-gate 	fp->max_retr = sctp->sctp_pp_max_rxt;
16280Sstevel@tonic-gate 	/* Mark it as not confirmed. */
16290Sstevel@tonic-gate 	fp->state = SCTP_FADDRS_UNCONFIRMED;
16300Sstevel@tonic-gate 	fp->hb_interval = sctp->sctp_hb_interval;
16310Sstevel@tonic-gate 	fp->ssthresh = sctp_initial_ssthresh;
16320Sstevel@tonic-gate 	fp->suna = 0;
16330Sstevel@tonic-gate 	fp->pba = 0;
16340Sstevel@tonic-gate 	fp->acked = 0;
16350Sstevel@tonic-gate 	fp->lastactive = lbolt64;
16360Sstevel@tonic-gate 	fp->timer_mp = NULL;
16370Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
16380Sstevel@tonic-gate 	fp->timer_running = 0;
16390Sstevel@tonic-gate 	fp->df = 1;
16400Sstevel@tonic-gate 	fp->pmtu_discovered = 0;
16410Sstevel@tonic-gate 	fp->rc_timer_mp = NULL;
16420Sstevel@tonic-gate 	fp->rc_timer_running = 0;
16430Sstevel@tonic-gate 	fp->next = NULL;
16440Sstevel@tonic-gate 	fp->ire = NULL;
16450Sstevel@tonic-gate 	fp->T3expire = 0;
16460Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret,
16470Sstevel@tonic-gate 	    sizeof (fp->hb_secret));
16480Sstevel@tonic-gate 	fp->hb_expiry = lbolt64;
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	sctp_ire2faddr(sctp, fp);
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate /*ARGSUSED*/
16540Sstevel@tonic-gate static void
16550Sstevel@tonic-gate faddr_destructor(void *buf, void *cdrarg)
16560Sstevel@tonic-gate {
16570Sstevel@tonic-gate 	sctp_faddr_t *fp = buf;
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	ASSERT(fp->timer_mp == NULL);
16600Sstevel@tonic-gate 	ASSERT(fp->timer_running == 0);
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	ASSERT(fp->rc_timer_mp == NULL);
16630Sstevel@tonic-gate 	ASSERT(fp->rc_timer_running == 0);
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate void
16670Sstevel@tonic-gate sctp_faddr_init()
16680Sstevel@tonic-gate {
16690Sstevel@tonic-gate 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
16700Sstevel@tonic-gate 	    sizeof (sctp_faddr_t), 0, NULL, faddr_destructor,
16710Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate void
16750Sstevel@tonic-gate sctp_faddr_fini()
16760Sstevel@tonic-gate {
16770Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_faddr_cache);
16780Sstevel@tonic-gate }
1679