xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_common.c (revision 4691:ca8edd01a7e2)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * 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  */
211735Skcpoon 
220Sstevel@tonic-gate /*
233448Sdh155122  * Copyright 2007 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>
321676Sjpk #include <sys/strsubr.h>
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include <sys/random.h>
381676Sjpk #include <sys/tsol/tndb.h>
391676Sjpk #include <sys/tsol/tnet.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <netinet/ip6.h>
430Sstevel@tonic-gate #include <netinet/sctp.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <inet/common.h>
460Sstevel@tonic-gate #include <inet/ip.h>
470Sstevel@tonic-gate #include <inet/ip6.h>
480Sstevel@tonic-gate #include <inet/ip_ire.h>
490Sstevel@tonic-gate #include <inet/mib2.h>
500Sstevel@tonic-gate #include <inet/nd.h>
510Sstevel@tonic-gate #include <inet/optcom.h>
520Sstevel@tonic-gate #include <inet/sctp_ip.h>
530Sstevel@tonic-gate #include <inet/ipclassifier.h>
541676Sjpk 
550Sstevel@tonic-gate #include "sctp_impl.h"
560Sstevel@tonic-gate #include "sctp_addr.h"
571932Svi117747 #include "sctp_asconf.h"
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_faddr_cache;
601735Skcpoon static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *);
610Sstevel@tonic-gate 
621735Skcpoon /* Set the source address.  Refer to comments in sctp_get_ire(). */
631932Svi117747 void
641932Svi117747 sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp)
650Sstevel@tonic-gate {
661719Sjarrett 	boolean_t v6 = !fp->isv4;
671719Sjarrett 
680Sstevel@tonic-gate 	if (sctp->sctp_bound_to_all) {
690Sstevel@tonic-gate 		V6_SET_ZERO(fp->saddr);
700Sstevel@tonic-gate 	} else {
710Sstevel@tonic-gate 		fp->saddr = sctp_get_valid_addr(sctp, v6);
720Sstevel@tonic-gate 		if (!v6 && IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr) ||
730Sstevel@tonic-gate 		    v6 && IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
740Sstevel@tonic-gate 			fp->state = SCTP_FADDRS_UNREACH;
750Sstevel@tonic-gate 			/* Disable heartbeat. */
760Sstevel@tonic-gate 			fp->hb_expiry = 0;
770Sstevel@tonic-gate 			fp->hb_pending = B_FALSE;
780Sstevel@tonic-gate 			fp->strikes = 0;
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Call this function to update the cached IRE of a peer addr fp.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate void
871735Skcpoon sctp_get_ire(sctp_t *sctp, sctp_faddr_t *fp)
880Sstevel@tonic-gate {
891735Skcpoon 	ire_t		*ire;
901735Skcpoon 	ipaddr_t	addr4;
911735Skcpoon 	in6_addr_t	laddr;
920Sstevel@tonic-gate 	sctp_saddr_ipif_t *sp;
931735Skcpoon 	int		hdrlen;
941735Skcpoon 	ts_label_t	*tsl;
953448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
963448Sdh155122 	ip_stack_t	*ipst = sctps->sctps_netstack->netstack_ip;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* Remove the previous cache IRE */
990Sstevel@tonic-gate 	if ((ire = fp->ire) != NULL) {
1000Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
1010Sstevel@tonic-gate 		fp->ire = NULL;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 * If this addr is not reachable, mark it as unconfirmed for now, the
1060Sstevel@tonic-gate 	 * state will be changed back to unreachable later in this function
1070Sstevel@tonic-gate 	 * if it is still the case.
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH) {
1100Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_UNCONFIRMED;
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1131676Sjpk 	tsl = crgetlabel(CONN_CRED(sctp->sctp_connp));
1141676Sjpk 
1150Sstevel@tonic-gate 	if (fp->isv4) {
1160Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
1173448Sdh155122 		ire = ire_cache_lookup(addr4, sctp->sctp_zoneid, tsl, ipst);
1181676Sjpk 		if (ire != NULL)
1191676Sjpk 			IN6_IPADDR_TO_V4MAPPED(ire->ire_src_addr, &laddr);
1201676Sjpk 	} else {
1213448Sdh155122 		ire = ire_cache_lookup_v6(&fp->faddr, sctp->sctp_zoneid, tsl,
1223448Sdh155122 		    ipst);
1231676Sjpk 		if (ire != NULL)
1241676Sjpk 			laddr = ire->ire_src_addr_v6;
1251676Sjpk 	}
1260Sstevel@tonic-gate 
1271676Sjpk 	if (ire == NULL) {
1281676Sjpk 		dprint(3, ("ire2faddr: no ire for %x:%x:%x:%x\n",
1291676Sjpk 		    SCTP_PRINTADDR(fp->faddr)));
1301676Sjpk 		/*
1311676Sjpk 		 * It is tempting to just leave the src addr
1321676Sjpk 		 * unspecified and let IP figure it out, but we
1331676Sjpk 		 * *cannot* do this, since IP may choose a src addr
1341676Sjpk 		 * that is not part of this association... unless
1351676Sjpk 		 * this sctp has bound to all addrs.  So if the ire
1361676Sjpk 		 * lookup fails, try to find one in our src addr
1371676Sjpk 		 * list, unless the sctp has bound to all addrs, in
1381676Sjpk 		 * which case we change the src addr to unspec.
1391676Sjpk 		 *
1401676Sjpk 		 * Note that if this is a v6 endpoint but it does
1411676Sjpk 		 * not have any v4 address at this point (e.g. may
1421676Sjpk 		 * have been  deleted), sctp_get_valid_addr() will
1431676Sjpk 		 * return mapped INADDR_ANY.  In this case, this
1441676Sjpk 		 * address should be marked not reachable so that
1451676Sjpk 		 * it won't be used to send data.
1461676Sjpk 		 */
1471932Svi117747 		sctp_set_saddr(sctp, fp);
1481735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH)
1491735Skcpoon 			return;
1501735Skcpoon 		goto check_current;
1511676Sjpk 	}
1521676Sjpk 
1531676Sjpk 	dprint(2, ("ire2faddr: got ire for %x:%x:%x:%x, ",
1541676Sjpk 	    SCTP_PRINTADDR(fp->faddr)));
1551676Sjpk 	if (fp->isv4) {
1560Sstevel@tonic-gate 		dprint(2, ("src = %x\n", ire->ire_src_addr));
1570Sstevel@tonic-gate 	} else {
1580Sstevel@tonic-gate 		dprint(2, ("src=%x:%x:%x:%x\n",
1590Sstevel@tonic-gate 		    SCTP_PRINTADDR(ire->ire_src_addr_v6)));
1601676Sjpk 	}
1610Sstevel@tonic-gate 
1621735Skcpoon 	/* Make sure the laddr is part of this association */
1633510Svi117747 	if ((sp = sctp_saddr_lookup(sctp, &ire->ire_ipif->ipif_v6lcl_addr,
1643510Svi117747 	    0)) != NULL && !sp->saddr_ipif_dontsrc) {
1651676Sjpk 		if (sp->saddr_ipif_unconfirmed == 1)
1661676Sjpk 			sp->saddr_ipif_unconfirmed = 0;
1671676Sjpk 		fp->saddr = laddr;
1681676Sjpk 	} else {
1691676Sjpk 		dprint(2, ("ire2faddr: src addr is not part of assc\n"));
1701735Skcpoon 
1711735Skcpoon 		/*
1721735Skcpoon 		 * Set the src to the first saddr and hope for the best.
1731735Skcpoon 		 * Note that we will still do the ire caching below.
1741735Skcpoon 		 * Otherwise, whenever we send a packet, we need to do
1751735Skcpoon 		 * the ire lookup again and still may not get the correct
1761735Skcpoon 		 * source address.  Note that this case should very seldomly
1771735Skcpoon 		 * happen.  One scenario this can happen is an app
1781735Skcpoon 		 * explicitly bind() to an address.  But that address is
1791735Skcpoon 		 * not the preferred source address to send to the peer.
1801735Skcpoon 		 */
1811932Svi117747 		sctp_set_saddr(sctp, fp);
1821735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH) {
1831735Skcpoon 			IRE_REFRELE(ire);
1841735Skcpoon 			return;
1851735Skcpoon 		}
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1881735Skcpoon 	/*
1891735Skcpoon 	 * Note that ire_cache_lookup_*() returns an ire with the tracing
1901735Skcpoon 	 * bits enabled.  This requires the thread holding the ire also
1911735Skcpoon 	 * do the IRE_REFRELE().  Thus we need to do IRE_REFHOLD_NOTR()
1921735Skcpoon 	 * and then IRE_REFRELE() the ire here to make the tracing bits
1931735Skcpoon 	 * work.
1941735Skcpoon 	 */
1951735Skcpoon 	IRE_REFHOLD_NOTR(ire);
1961735Skcpoon 	IRE_REFRELE(ire);
1971735Skcpoon 
1980Sstevel@tonic-gate 	/* Cache the IRE */
1990Sstevel@tonic-gate 	fp->ire = ire;
2000Sstevel@tonic-gate 	if (fp->ire->ire_type == IRE_LOOPBACK && !sctp->sctp_loopback)
2010Sstevel@tonic-gate 		sctp->sctp_loopback = 1;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/*
2040Sstevel@tonic-gate 	 * Pull out RTO information for this faddr and use it if we don't
2050Sstevel@tonic-gate 	 * have any yet.
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	if (fp->srtt == -1 && ire->ire_uinfo.iulp_rtt != 0) {
208116Skcpoon 		/* The cached value is in ms. */
209116Skcpoon 		fp->srtt = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt);
210116Skcpoon 		fp->rttvar = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt_sd);
2110Sstevel@tonic-gate 		fp->rto = 3 * fp->srtt;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		/* Bound the RTO by configured min and max values */
2140Sstevel@tonic-gate 		if (fp->rto < sctp->sctp_rto_min) {
2150Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_min;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		if (fp->rto > sctp->sctp_rto_max) {
2180Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_max;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * Record the MTU for this faddr. If the MTU for this faddr has
2240Sstevel@tonic-gate 	 * changed, check if the assc MTU will also change.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	if (fp->isv4) {
2270Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
2280Sstevel@tonic-gate 	} else {
2290Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 	if ((fp->sfa_pmss + hdrlen) != ire->ire_max_frag) {
2320Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
2330Sstevel@tonic-gate 		fp->sfa_pmss = (ire->ire_max_frag - hdrlen) & ~(SCTP_ALIGN - 1);
2340Sstevel@tonic-gate 		if (fp->cwnd < (fp->sfa_pmss * 2)) {
2353795Skcpoon 			SET_CWND(fp, fp->sfa_pmss,
2363795Skcpoon 			    sctps->sctps_slow_start_initial);
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2401735Skcpoon check_current:
2411735Skcpoon 	if (fp == sctp->sctp_current)
2421735Skcpoon 		sctp_set_faddr_current(sctp, fp);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate void
2461735Skcpoon sctp_update_ire(sctp_t *sctp)
2470Sstevel@tonic-gate {
2481735Skcpoon 	ire_t		*ire;
2491735Skcpoon 	sctp_faddr_t	*fp;
2503448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2510Sstevel@tonic-gate 
2521735Skcpoon 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
2531735Skcpoon 		if ((ire = fp->ire) == NULL)
2541735Skcpoon 			continue;
2551735Skcpoon 		mutex_enter(&ire->ire_lock);
2560Sstevel@tonic-gate 
2571735Skcpoon 		/*
2581735Skcpoon 		 * If the cached IRE is going away, there is no point to
2591735Skcpoon 		 * update it.
2601735Skcpoon 		 */
2611735Skcpoon 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
2621735Skcpoon 			mutex_exit(&ire->ire_lock);
2631735Skcpoon 			IRE_REFRELE_NOTR(ire);
2641735Skcpoon 			fp->ire = NULL;
2651735Skcpoon 			continue;
2660Sstevel@tonic-gate 		}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		/*
2691735Skcpoon 		 * Only record the PMTU for this faddr if we actually have
2701735Skcpoon 		 * done discovery. This prevents initialized default from
2711735Skcpoon 		 * clobbering any real info that IP may have.
2720Sstevel@tonic-gate 		 */
2731735Skcpoon 		if (fp->pmtu_discovered) {
2741735Skcpoon 			if (fp->isv4) {
2751735Skcpoon 				ire->ire_max_frag = fp->sfa_pmss +
2761735Skcpoon 				    sctp->sctp_hdr_len;
2771735Skcpoon 			} else {
2781735Skcpoon 				ire->ire_max_frag = fp->sfa_pmss +
2791735Skcpoon 				    sctp->sctp_hdr6_len;
2801735Skcpoon 			}
2810Sstevel@tonic-gate 		}
2821735Skcpoon 
2833448Sdh155122 		if (sctps->sctps_rtt_updates != 0 &&
2843448Sdh155122 		    fp->rtt_updates >= sctps->sctps_rtt_updates) {
2851735Skcpoon 			/*
2861735Skcpoon 			 * If there is no old cached values, initialize them
2871735Skcpoon 			 * conservatively.  Set them to be (1.5 * new value).
2881735Skcpoon 			 * This code copied from ip_ire_advise().  The cached
2891735Skcpoon 			 * value is in ms.
2901735Skcpoon 			 */
2911735Skcpoon 			if (ire->ire_uinfo.iulp_rtt != 0) {
2921735Skcpoon 				ire->ire_uinfo.iulp_rtt =
2931735Skcpoon 				    (ire->ire_uinfo.iulp_rtt +
2941735Skcpoon 				    TICK_TO_MSEC(fp->srtt)) >> 1;
2951735Skcpoon 			} else {
2961735Skcpoon 				ire->ire_uinfo.iulp_rtt =
2971735Skcpoon 				    TICK_TO_MSEC(fp->srtt + (fp->srtt >> 1));
2981735Skcpoon 			}
2991735Skcpoon 			if (ire->ire_uinfo.iulp_rtt_sd != 0) {
3001735Skcpoon 				ire->ire_uinfo.iulp_rtt_sd =
301*4691Skcpoon 				    (ire->ire_uinfo.iulp_rtt_sd +
302*4691Skcpoon 				    TICK_TO_MSEC(fp->rttvar)) >> 1;
3031735Skcpoon 			} else {
3041735Skcpoon 				ire->ire_uinfo.iulp_rtt_sd =
3051735Skcpoon 				    TICK_TO_MSEC(fp->rttvar +
3061735Skcpoon 				    (fp->rttvar >> 1));
3071735Skcpoon 			}
3081735Skcpoon 			fp->rtt_updates = 0;
3090Sstevel@tonic-gate 		}
3101735Skcpoon 		mutex_exit(&ire->ire_lock);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * The sender must set the total length in the IP header.
3160Sstevel@tonic-gate  * If sendto == NULL, the current will be used.
3170Sstevel@tonic-gate  */
3180Sstevel@tonic-gate mblk_t *
3190Sstevel@tonic-gate sctp_make_mp(sctp_t *sctp, sctp_faddr_t *sendto, int trailer)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	mblk_t *mp;
3220Sstevel@tonic-gate 	size_t ipsctplen;
3230Sstevel@tonic-gate 	int isv4;
3240Sstevel@tonic-gate 	sctp_faddr_t *fp;
3253448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL || sendto != NULL);
3280Sstevel@tonic-gate 	if (sendto == NULL) {
3290Sstevel@tonic-gate 		fp = sctp->sctp_current;
3300Sstevel@tonic-gate 	} else {
3310Sstevel@tonic-gate 		fp = sendto;
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	isv4 = fp->isv4;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/* Try to look for another IRE again. */
3360Sstevel@tonic-gate 	if (fp->ire == NULL)
3371735Skcpoon 		sctp_get_ire(sctp, fp);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/* There is no suitable source address to use, return. */
3400Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH)
3410Sstevel@tonic-gate 		return (NULL);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (isv4) {
3440Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr_len;
3450Sstevel@tonic-gate 	} else {
3460Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr6_len;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3493448Sdh155122 	mp = allocb_cred(ipsctplen + sctps->sctps_wroff_xtra + trailer,
3501676Sjpk 	    CONN_CRED(sctp->sctp_connp));
3510Sstevel@tonic-gate 	if (mp == NULL) {
3521676Sjpk 		ip1dbg(("sctp_make_mp: error making mp..\n"));
3530Sstevel@tonic-gate 		return (NULL);
3540Sstevel@tonic-gate 	}
3553448Sdh155122 	mp->b_rptr += sctps->sctps_wroff_xtra;
3560Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + ipsctplen;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_wptr));
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	if (isv4) {
3610Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)mp->b_rptr;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
3640Sstevel@tonic-gate 		if (fp != sctp->sctp_current) {
3650Sstevel@tonic-gate 			/* fiddle with the dst addr */
3660Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
3670Sstevel@tonic-gate 			/* fix up src addr */
3680Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) {
3690Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
3700Sstevel@tonic-gate 				    iph->ipha_src);
3710Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
3720Sstevel@tonic-gate 				iph->ipha_src = INADDR_ANY;
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 		/* set or clear the don't fragment bit */
3760Sstevel@tonic-gate 		if (fp->df) {
3770Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
3780Sstevel@tonic-gate 		} else {
3790Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 	} else {
3820Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
3830Sstevel@tonic-gate 		if (fp != sctp->sctp_current) {
3840Sstevel@tonic-gate 			/* fiddle with the dst addr */
3850Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
3860Sstevel@tonic-gate 			/* fix up src addr */
3870Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
3880Sstevel@tonic-gate 				((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
3890Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
3900Sstevel@tonic-gate 				bzero(&((ip6_t *)(mp->b_rptr))->ip6_src,
3910Sstevel@tonic-gate 				    sizeof (in6_addr_t));
3920Sstevel@tonic-gate 			}
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp != NULL);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * IP will not free this IRE if it is condemned.  SCTP needs to
3990Sstevel@tonic-gate 	 * free it.
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
4020Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
4030Sstevel@tonic-gate 		fp->ire = NULL;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	/* Stash the conn and ire ptr info. for IP */
4060Sstevel@tonic-gate 	SCTP_STASH_IPINFO(mp, fp->ire);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	return (mp);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate  * Notify upper layers about preferred write offset, write size.
4130Sstevel@tonic-gate  */
4140Sstevel@tonic-gate void
4150Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	int hdrlen;
4183448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (sctp->sctp_current->isv4) {
4210Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
4220Sstevel@tonic-gate 	} else {
4230Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss);
4280Sstevel@tonic-gate 	sctp->sctp_ulp_prop(sctp->sctp_ulpd,
4293448Sdh155122 	    sctps->sctps_wroff_xtra + hdrlen + sizeof (sctp_data_hdr_t),
4300Sstevel@tonic-gate 	    sctp->sctp_mss - sizeof (sctp_data_hdr_t));
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate void
4340Sstevel@tonic-gate sctp_set_iplen(sctp_t *sctp, mblk_t *mp)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	uint16_t	sum = 0;
4370Sstevel@tonic-gate 	ipha_t		*iph;
4380Sstevel@tonic-gate 	ip6_t		*ip6h;
4390Sstevel@tonic-gate 	mblk_t		*pmp = mp;
4400Sstevel@tonic-gate 	boolean_t	isv4;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
4430Sstevel@tonic-gate 	for (; pmp; pmp = pmp->b_cont)
4440Sstevel@tonic-gate 		sum += pmp->b_wptr - pmp->b_rptr;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	if (isv4) {
4470Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
4480Sstevel@tonic-gate 		iph->ipha_length = htons(sum);
4490Sstevel@tonic-gate 	} else {
4500Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
451679Sseb 		/*
452679Sseb 		 * If an ip6i_t is present, the real IPv6 header
453679Sseb 		 * immediately follows.
454679Sseb 		 */
455679Sseb 		if (ip6h->ip6_nxt == IPPROTO_RAW)
456679Sseb 			ip6h = (ip6_t *)&ip6h[1];
4570Sstevel@tonic-gate 		ip6h->ip6_plen = htons(sum - ((char *)&sctp->sctp_ip6h[1] -
4580Sstevel@tonic-gate 		    sctp->sctp_iphc6));
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate int
4630Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	int na1 = 0;
4660Sstevel@tonic-gate 	int overlap = 0;
4670Sstevel@tonic-gate 	int equal = 1;
4680Sstevel@tonic-gate 	int onematch;
4690Sstevel@tonic-gate 	sctp_faddr_t *fp1, *fp2;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	for (fp1 = a1; fp1; fp1 = fp1->next) {
4720Sstevel@tonic-gate 		onematch = 0;
4730Sstevel@tonic-gate 		for (fp2 = a2; fp2; fp2 = fp2->next) {
4740Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) {
4750Sstevel@tonic-gate 				overlap++;
4760Sstevel@tonic-gate 				onematch = 1;
4770Sstevel@tonic-gate 				break;
4780Sstevel@tonic-gate 			}
4790Sstevel@tonic-gate 			if (!onematch) {
4800Sstevel@tonic-gate 				equal = 0;
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 		}
4830Sstevel@tonic-gate 		na1++;
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (equal) {
4870Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 	if (overlap == na1) {
4900Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	if (overlap) {
4930Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /*
4991735Skcpoon  * Returns 0 on success, -1 on memory allocation failure. If sleep
5001735Skcpoon  * is true, this function should never fail.  The boolean parameter
5011735Skcpoon  * first decides whether the newly created faddr structure should be
5021735Skcpoon  * added at the beginning of the list or at the end.
5031735Skcpoon  *
5041735Skcpoon  * Note: caller must hold conn fanout lock.
5050Sstevel@tonic-gate  */
5061735Skcpoon int
5071735Skcpoon sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first)
5080Sstevel@tonic-gate {
5091735Skcpoon 	sctp_faddr_t	*faddr;
5101735Skcpoon 	mblk_t		*timer_mp;
5110Sstevel@tonic-gate 
5121676Sjpk 	if (is_system_labeled()) {
5131676Sjpk 		ts_label_t *tsl;
5141676Sjpk 		tsol_tpc_t *rhtp;
5151676Sjpk 		int retv;
5161676Sjpk 
5171676Sjpk 		tsl = crgetlabel(CONN_CRED(sctp->sctp_connp));
5181676Sjpk 		ASSERT(tsl != NULL);
5191676Sjpk 
5201676Sjpk 		/* find_tpc automatically does the right thing with IPv4 */
5211676Sjpk 		rhtp = find_tpc(addr, IPV6_VERSION, B_FALSE);
5221676Sjpk 		if (rhtp == NULL)
5231676Sjpk 			return (EACCES);
5240Sstevel@tonic-gate 
5251676Sjpk 		retv = EACCES;
5261676Sjpk 		if (tsl->tsl_doi == rhtp->tpc_tp.tp_doi) {
5271676Sjpk 			switch (rhtp->tpc_tp.host_type) {
5281676Sjpk 			case UNLABELED:
5291676Sjpk 				/*
5301676Sjpk 				 * Can talk to unlabeled hosts if any of the
5311676Sjpk 				 * following are true:
5321676Sjpk 				 *   1. zone's label matches the remote host's
5331676Sjpk 				 *	default label,
5341676Sjpk 				 *   2. mac_exempt is on and the zone dominates
5351676Sjpk 				 *	the remote host's label, or
5361676Sjpk 				 *   3. mac_exempt is on and the socket is from
5371676Sjpk 				 *	the global zone.
5381676Sjpk 				 */
5391676Sjpk 				if (blequal(&rhtp->tpc_tp.tp_def_label,
5401676Sjpk 				    &tsl->tsl_label) ||
5411676Sjpk 				    (sctp->sctp_mac_exempt &&
5421676Sjpk 				    (sctp->sctp_zoneid == GLOBAL_ZONEID ||
5431676Sjpk 				    bldominates(&tsl->tsl_label,
5441676Sjpk 				    &rhtp->tpc_tp.tp_def_label))))
5451676Sjpk 					retv = 0;
5461676Sjpk 				break;
5471676Sjpk 			case SUN_CIPSO:
5481676Sjpk 				if (_blinrange(&tsl->tsl_label,
5491676Sjpk 				    &rhtp->tpc_tp.tp_sl_range_cipso) ||
5501676Sjpk 				    blinlset(&tsl->tsl_label,
5511676Sjpk 				    rhtp->tpc_tp.tp_sl_set_cipso))
5521676Sjpk 					retv = 0;
5531676Sjpk 				break;
5541676Sjpk 			}
5551676Sjpk 		}
5561676Sjpk 		TPC_RELE(rhtp);
5571676Sjpk 		if (retv != 0)
5581676Sjpk 			return (retv);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5611676Sjpk 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL)
5621676Sjpk 		return (ENOMEM);
563*4691Skcpoon 	timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep);
5641735Skcpoon 	if (timer_mp == NULL) {
5651735Skcpoon 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
5661735Skcpoon 		return (ENOMEM);
5671735Skcpoon 	}
5681735Skcpoon 	((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr;
5691676Sjpk 
5701735Skcpoon 	sctp_init_faddr(sctp, faddr, addr, timer_mp);
5710Sstevel@tonic-gate 	ASSERT(faddr->next == NULL);
5720Sstevel@tonic-gate 
5731676Sjpk 	if (sctp->sctp_faddrs == NULL) {
5741676Sjpk 		ASSERT(sctp->sctp_lastfaddr == NULL);
5751676Sjpk 		/* only element on list; first and last are same */
5761676Sjpk 		sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr;
5771676Sjpk 	} else if (first) {
5781676Sjpk 		ASSERT(sctp->sctp_lastfaddr != NULL);
5791676Sjpk 		faddr->next = sctp->sctp_faddrs;
5801676Sjpk 		sctp->sctp_faddrs = faddr;
5810Sstevel@tonic-gate 	} else {
5821676Sjpk 		sctp->sctp_lastfaddr->next = faddr;
5831676Sjpk 		sctp->sctp_lastfaddr = faddr;
5840Sstevel@tonic-gate 	}
585852Svi117747 	sctp->sctp_nfaddrs++;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	return (0);
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate sctp_faddr_t *
5910Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
5920Sstevel@tonic-gate {
5930Sstevel@tonic-gate 	sctp_faddr_t *fp;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5960Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr))
5970Sstevel@tonic-gate 			break;
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	return (fp);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate sctp_faddr_t *
6040Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 	for (; fp; fp = fp->next) {
6070Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) {
6080Sstevel@tonic-gate 			break;
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	return (fp);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6151735Skcpoon /*
6161735Skcpoon  * To change the currently used peer address to the specified one.
6171735Skcpoon  */
6180Sstevel@tonic-gate void
6191735Skcpoon sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp)
6200Sstevel@tonic-gate {
6211735Skcpoon 	/* Now setup the composite header. */
6220Sstevel@tonic-gate 	if (fp->isv4) {
6230Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
6240Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
6250Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src);
6260Sstevel@tonic-gate 		/* update don't fragment bit */
6270Sstevel@tonic-gate 		if (fp->df) {
6280Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
6290Sstevel@tonic-gate 			    htons(IPH_DF);
6300Sstevel@tonic-gate 		} else {
6310Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
6320Sstevel@tonic-gate 		}
6330Sstevel@tonic-gate 	} else {
6340Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = fp->faddr;
6350Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = fp->saddr;
6360Sstevel@tonic-gate 	}
6371735Skcpoon 
6381735Skcpoon 	sctp->sctp_current = fp;
6391735Skcpoon 	sctp->sctp_mss = fp->sfa_pmss;
6401735Skcpoon 
6411735Skcpoon 	/* Update the uppper layer for the change. */
6421735Skcpoon 	if (!SCTP_IS_DETACHED(sctp))
6431735Skcpoon 		sctp_set_ulp_prop(sctp);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate void
6470Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	sctp_faddr_t *fp;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
6521735Skcpoon 		sctp_get_ire(sctp, fp);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate void
6570Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	int64_t now = lbolt64;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	fp->strikes = 0;
6620Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
6630Sstevel@tonic-gate 	fp->lastactive = now;
6640Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
6650Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
6660Sstevel@tonic-gate 	if (fp->state != SCTP_FADDRS_ALIVE) {
6670Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_ALIVE;
6680Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0);
6690Sstevel@tonic-gate 
6701735Skcpoon 		/*
6711735Skcpoon 		 * If this is the primary, switch back to it now.  And
6721735Skcpoon 		 * we probably want to reset the source addr used to reach
6731735Skcpoon 		 * it.
6741735Skcpoon 		 */
6750Sstevel@tonic-gate 		if (fp == sctp->sctp_primary) {
6761735Skcpoon 			sctp_set_faddr_current(sctp, fp);
6771735Skcpoon 			sctp_get_ire(sctp, fp);
6781735Skcpoon 			return;
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 	if (fp->ire == NULL) {
6820Sstevel@tonic-gate 		/* Should have a full IRE now */
6831735Skcpoon 		sctp_get_ire(sctp, fp);
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate int
6880Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	sctp_faddr_t *fp;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
6930Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) {
6940Sstevel@tonic-gate 			return (1);
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	return (0);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate /*
7020Sstevel@tonic-gate  * Returns 0 if there is at leave one other active faddr, -1 if there
7030Sstevel@tonic-gate  * are none. If there are none left, faddr_dead() will start killing the
7040Sstevel@tonic-gate  * association.
7050Sstevel@tonic-gate  * If the downed faddr was the current faddr, a new current faddr
7060Sstevel@tonic-gate  * will be chosen.
7070Sstevel@tonic-gate  */
7080Sstevel@tonic-gate int
7090Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	sctp_faddr_t *ofp;
7123448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_ALIVE) {
7150Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0);
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 	fp->state = newstate;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
7200Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), newstate));
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
7230Sstevel@tonic-gate 		/* Current faddr down; need to switch it */
7240Sstevel@tonic-gate 		sctp->sctp_current = NULL;
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/* Find next alive faddr */
7280Sstevel@tonic-gate 	ofp = fp;
7291735Skcpoon 	for (fp = fp->next; fp != NULL; fp = fp->next) {
7300Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE) {
7310Sstevel@tonic-gate 			break;
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	if (fp == NULL) {
7360Sstevel@tonic-gate 		/* Continue from beginning of list */
7370Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) {
7380Sstevel@tonic-gate 			if (fp->state == SCTP_FADDRS_ALIVE) {
7390Sstevel@tonic-gate 				break;
7400Sstevel@tonic-gate 			}
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 
7441735Skcpoon 	/*
7451735Skcpoon 	 * Find a new fp, so if the current faddr is dead, use the new fp
7461735Skcpoon 	 * as the current one.
7471735Skcpoon 	 */
7480Sstevel@tonic-gate 	if (fp != ofp) {
7490Sstevel@tonic-gate 		if (sctp->sctp_current == NULL) {
7500Sstevel@tonic-gate 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
7510Sstevel@tonic-gate 			    SCTP_PRINTADDR(fp->faddr)));
7521735Skcpoon 			/*
7531735Skcpoon 			 * Note that we don't need to reset the source addr
7541735Skcpoon 			 * of the new fp.
7551735Skcpoon 			 */
7561735Skcpoon 			sctp_set_faddr_current(sctp, fp);
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 		return (0);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	/* All faddrs are down; kill the association */
7630Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
7643448Sdh155122 	BUMP_MIB(&sctps->sctps_mib, sctpAborted);
7650Sstevel@tonic-gate 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
7660Sstevel@tonic-gate 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
7670Sstevel@tonic-gate 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
7680Sstevel@tonic-gate 	    sctp->sctp_client_errno : ETIMEDOUT);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	return (-1);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate sctp_faddr_t *
7740Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	sctp_faddr_t *nfp = NULL;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	if (ofp == NULL) {
7790Sstevel@tonic-gate 		ofp = sctp->sctp_current;
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/* Find the next live one */
7830Sstevel@tonic-gate 	for (nfp = ofp->next; nfp != NULL; nfp = nfp->next) {
7840Sstevel@tonic-gate 		if (nfp->state == SCTP_FADDRS_ALIVE) {
7850Sstevel@tonic-gate 			break;
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	if (nfp == NULL) {
7900Sstevel@tonic-gate 		/* Continue from beginning of list */
7910Sstevel@tonic-gate 		for (nfp = sctp->sctp_faddrs; nfp != ofp; nfp = nfp->next) {
7920Sstevel@tonic-gate 			if (nfp->state == SCTP_FADDRS_ALIVE) {
7930Sstevel@tonic-gate 				break;
7940Sstevel@tonic-gate 			}
7950Sstevel@tonic-gate 		}
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * nfp could only be NULL if all faddrs are down, and when
8000Sstevel@tonic-gate 	 * this happens, faddr_dead() should have killed the
8010Sstevel@tonic-gate 	 * association. Hence this assertion...
8020Sstevel@tonic-gate 	 */
8030Sstevel@tonic-gate 	ASSERT(nfp != NULL);
8040Sstevel@tonic-gate 	return (nfp);
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate void
8080Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	sctp_faddr_t *fpp;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if (!sctp->sctp_faddrs) {
8130Sstevel@tonic-gate 		return;
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	if (fp->timer_mp != NULL) {
8170Sstevel@tonic-gate 		sctp_timer_free(fp->timer_mp);
8180Sstevel@tonic-gate 		fp->timer_mp = NULL;
8190Sstevel@tonic-gate 		fp->timer_running = 0;
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 	if (fp->rc_timer_mp != NULL) {
8220Sstevel@tonic-gate 		sctp_timer_free(fp->rc_timer_mp);
8230Sstevel@tonic-gate 		fp->rc_timer_mp = NULL;
8240Sstevel@tonic-gate 		fp->rc_timer_running = 0;
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	if (fp->ire != NULL) {
8270Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
8280Sstevel@tonic-gate 		fp->ire = NULL;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8320Sstevel@tonic-gate 		goto gotit;
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next)
8360Sstevel@tonic-gate 		;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate gotit:
8390Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
8400Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8410Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8420Sstevel@tonic-gate 		sctp->sctp_faddrs = fp->next;
8430Sstevel@tonic-gate 	} else {
8440Sstevel@tonic-gate 		fpp->next = fp->next;
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8470Sstevel@tonic-gate 	/* XXX faddr2ire? */
8480Sstevel@tonic-gate 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
849852Svi117747 	sctp->sctp_nfaddrs--;
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate void
8530Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpn;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL) {
8580Sstevel@tonic-gate 		ASSERT(sctp->sctp_lastfaddr == NULL);
8590Sstevel@tonic-gate 		return;
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr != NULL);
8630Sstevel@tonic-gate 	sctp->sctp_lastfaddr = NULL;
8640Sstevel@tonic-gate 	sctp->sctp_current = NULL;
8650Sstevel@tonic-gate 	sctp->sctp_primary = NULL;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8700Sstevel@tonic-gate 		/* in conn fanout; need to hold lock */
8710Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
8750Sstevel@tonic-gate 		fpn = fp->next;
8760Sstevel@tonic-gate 		if (fp->ire != NULL)
8770Sstevel@tonic-gate 			IRE_REFRELE_NOTR(fp->ire);
8780Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
879852Svi117747 		sctp->sctp_nfaddrs--;
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	sctp->sctp_faddrs = NULL;
883852Svi117747 	ASSERT(sctp->sctp_nfaddrs == 0);
8840Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8850Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate void
8910Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp)
8920Sstevel@tonic-gate {
8930Sstevel@tonic-gate 	sctp_zap_faddrs(sctp, 0);
8940Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * Initialize the IPv4 header. Loses any record of any IP options.
8990Sstevel@tonic-gate  */
9000Sstevel@tonic-gate int
9010Sstevel@tonic-gate sctp_header_init_ipv4(sctp_t *sctp, int sleep)
9020Sstevel@tonic-gate {
9030Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
9043448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	/*
9070Sstevel@tonic-gate 	 * This is a simple initialization. If there's
9080Sstevel@tonic-gate 	 * already a template, it should never be too small,
9090Sstevel@tonic-gate 	 * so reuse it.  Otherwise, allocate space for the new one.
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
9120Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc_len >= SCTP_MAX_COMBINED_HEADER_LENGTH);
9130Sstevel@tonic-gate 		bzero(sctp->sctp_iphc, sctp->sctp_iphc_len);
9140Sstevel@tonic-gate 	} else {
9150Sstevel@tonic-gate 		sctp->sctp_iphc_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
9160Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(sctp->sctp_iphc_len, sleep);
9170Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL) {
9180Sstevel@tonic-gate 			sctp->sctp_iphc_len = 0;
9190Sstevel@tonic-gate 			return (ENOMEM);
9200Sstevel@tonic-gate 		}
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	sctp->sctp_hdr_len = sizeof (ipha_t) + sizeof (sctp_hdr_t);
9260Sstevel@tonic-gate 	sctp->sctp_ip_hdr_len = sizeof (ipha_t);
9270Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_length = htons(sizeof (ipha_t) +
9280Sstevel@tonic-gate 	    sizeof (sctp_hdr_t));
929*4691Skcpoon 	sctp->sctp_ipha->ipha_version_and_hdr_length =
930*4691Skcpoon 	    (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	/*
9330Sstevel@tonic-gate 	 * These two fields should be zero, and are already set above.
9340Sstevel@tonic-gate 	 *
9350Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_ident,
9360Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_fragment_offset_and_flags.
9370Sstevel@tonic-gate 	 */
9380Sstevel@tonic-gate 
9393448Sdh155122 	sctp->sctp_ipha->ipha_ttl = sctps->sctps_ipv4_ttl;
9400Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_protocol = IPPROTO_SCTP;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc + sizeof (ipha_t));
9430Sstevel@tonic-gate 	sctp->sctp_sctph = sctph;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	return (0);
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate  * Update sctp_sticky_hdrs based on sctp_sticky_ipp.
9500Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, any sticky extension
9510Sstevel@tonic-gate  * headers, and the maximum size sctp header (to avoid reallocation
9520Sstevel@tonic-gate  * on the fly for additional sctp options).
9530Sstevel@tonic-gate  * Returns failure if can't allocate memory.
9540Sstevel@tonic-gate  */
9550Sstevel@tonic-gate int
9560Sstevel@tonic-gate sctp_build_hdrs(sctp_t *sctp)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	char		*hdrs;
9590Sstevel@tonic-gate 	uint_t		hdrs_len;
9600Sstevel@tonic-gate 	ip6i_t		*ip6i;
9610Sstevel@tonic-gate 	char		buf[SCTP_MAX_HDR_LENGTH];
9620Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
9630Sstevel@tonic-gate 	in6_addr_t	src;
9640Sstevel@tonic-gate 	in6_addr_t	dst;
9653448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
9661676Sjpk 
9670Sstevel@tonic-gate 	/*
9680Sstevel@tonic-gate 	 * save the existing sctp header and source/dest IP addresses
9690Sstevel@tonic-gate 	 */
9700Sstevel@tonic-gate 	bcopy(sctp->sctp_sctph6, buf, sizeof (sctp_hdr_t));
9710Sstevel@tonic-gate 	src = sctp->sctp_ip6h->ip6_src;
9720Sstevel@tonic-gate 	dst = sctp->sctp_ip6h->ip6_dst;
9730Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp) + SCTP_MAX_HDR_LENGTH;
9740Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
9750Sstevel@tonic-gate 	if (hdrs_len > sctp->sctp_iphc6_len) {
9760Sstevel@tonic-gate 		/* Need to reallocate */
9770Sstevel@tonic-gate 		hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP);
9780Sstevel@tonic-gate 		if (hdrs == NULL)
9790Sstevel@tonic-gate 			return (ENOMEM);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 		if (sctp->sctp_iphc6_len != 0)
9820Sstevel@tonic-gate 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
9830Sstevel@tonic-gate 		sctp->sctp_iphc6 = hdrs;
9840Sstevel@tonic-gate 		sctp->sctp_iphc6_len = hdrs_len;
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 	ip_build_hdrs_v6((uchar_t *)sctp->sctp_iphc6,
9870Sstevel@tonic-gate 	    hdrs_len - SCTP_MAX_HDR_LENGTH, ipp, IPPROTO_SCTP);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	/* Set header fields not in ipp */
9900Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
9910Sstevel@tonic-gate 		ip6i = (ip6i_t *)sctp->sctp_iphc6;
9920Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)&ip6i[1];
9930Sstevel@tonic-gate 	} else {
9940Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * sctp->sctp_ip_hdr_len will include ip6i_t if there is one.
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = hdrs_len - SCTP_MAX_HDR_LENGTH;
10000Sstevel@tonic-gate 	sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
10010Sstevel@tonic-gate 	    sctp->sctp_ip_hdr6_len);
10020Sstevel@tonic-gate 	sctp->sctp_hdr6_len = sctp->sctp_ip_hdr6_len + sizeof (sctp_hdr_t);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	bcopy(buf, sctp->sctp_sctph6, sizeof (sctp_hdr_t));
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_src = src;
10070Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_dst = dst;
10080Sstevel@tonic-gate 	/*
1009679Sseb 	 * If the hoplimit was not set by ip_build_hdrs_v6(), we need to
1010679Sseb 	 * set it to the default value for SCTP.
10110Sstevel@tonic-gate 	 */
1012679Sseb 	if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS))
10133448Sdh155122 		sctp->sctp_ip6h->ip6_hops = sctps->sctps_ipv6_hoplimit;
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * If we're setting extension headers after a connection
10160Sstevel@tonic-gate 	 * has been established, and if we have a routing header
10170Sstevel@tonic-gate 	 * among the extension headers, call ip_massage_options_v6 to
10180Sstevel@tonic-gate 	 * manipulate the routing header/ip6_dst set the checksum
10190Sstevel@tonic-gate 	 * difference in the sctp header template.
10200Sstevel@tonic-gate 	 * (This happens in sctp_connect_ipv6 if the routing header
10210Sstevel@tonic-gate 	 * is set prior to the connect.)
10220Sstevel@tonic-gate 	 */
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
10250Sstevel@tonic-gate 	    (sctp->sctp_sticky_ipp.ipp_fields & IPPF_RTHDR)) {
10260Sstevel@tonic-gate 		ip6_rthdr_t *rth;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
10290Sstevel@tonic-gate 		    (uint8_t *)sctp->sctp_sctph6);
10303448Sdh155122 		if (rth != NULL) {
10313448Sdh155122 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth,
10323448Sdh155122 			    sctps->sctps_netstack);
10333448Sdh155122 		}
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	return (0);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate /*
10390Sstevel@tonic-gate  * Initialize the IPv6 header. Loses any record of any IPv6 extension headers.
10400Sstevel@tonic-gate  */
10410Sstevel@tonic-gate int
10420Sstevel@tonic-gate sctp_header_init_ipv6(sctp_t *sctp, int sleep)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
10453448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10480Sstevel@tonic-gate 	 * This is a simple initialization. If there's
10490Sstevel@tonic-gate 	 * already a template, it should never be too small,
10500Sstevel@tonic-gate 	 * so reuse it. Otherwise, allocate space for the new one.
10510Sstevel@tonic-gate 	 * Ensure that there is enough space to "downgrade" the sctp_t
10520Sstevel@tonic-gate 	 * to an IPv4 sctp_t. This requires having space for a full load
10530Sstevel@tonic-gate 	 * of IPv4 options
10540Sstevel@tonic-gate 	 */
10550Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
10560Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc6_len >=
10570Sstevel@tonic-gate 		    SCTP_MAX_COMBINED_HEADER_LENGTH);
10580Sstevel@tonic-gate 		bzero(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
10590Sstevel@tonic-gate 	} else {
10600Sstevel@tonic-gate 		sctp->sctp_iphc6_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
10610Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(sctp->sctp_iphc_len, sleep);
10620Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
10630Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
10640Sstevel@tonic-gate 			return (ENOMEM);
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 	sctp->sctp_hdr6_len = IPV6_HDR_LEN + sizeof (sctp_hdr_t);
10680Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = IPV6_HDR_LEN;
10690Sstevel@tonic-gate 	sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	/* Initialize the header template */
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
10740Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_plen = ntohs(sizeof (sctp_hdr_t));
10750Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_nxt = IPPROTO_SCTP;
10763448Sdh155122 	sctp->sctp_ip6h->ip6_hops = sctps->sctps_ipv6_hoplimit;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc6 + IPV6_HDR_LEN);
10790Sstevel@tonic-gate 	sctp->sctp_sctph6 = sctph;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	return (0);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10841676Sjpk static int
10851676Sjpk sctp_v4_label(sctp_t *sctp)
10861676Sjpk {
10871676Sjpk 	uchar_t optbuf[IP_MAX_OPT_LENGTH];
10881676Sjpk 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
10891676Sjpk 	int added;
10901676Sjpk 
10911676Sjpk 	if (tsol_compute_label(cr, sctp->sctp_ipha->ipha_dst, optbuf,
10923448Sdh155122 	    sctp->sctp_mac_exempt,
10933448Sdh155122 	    sctp->sctp_sctps->sctps_netstack->netstack_ip) != 0)
10941676Sjpk 		return (EACCES);
10951676Sjpk 
10961676Sjpk 	added = tsol_remove_secopt(sctp->sctp_ipha, sctp->sctp_hdr_len);
10971676Sjpk 	if (added == -1)
10981676Sjpk 		return (EACCES);
10991676Sjpk 	sctp->sctp_hdr_len += added;
11001676Sjpk 	sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph + added);
11011676Sjpk 	sctp->sctp_ip_hdr_len += added;
11021676Sjpk 	if ((sctp->sctp_v4label_len = optbuf[IPOPT_OLEN]) != 0) {
11031676Sjpk 		sctp->sctp_v4label_len = (sctp->sctp_v4label_len + 3) & ~3;
11041676Sjpk 		added = tsol_prepend_option(optbuf, sctp->sctp_ipha,
11051676Sjpk 		    sctp->sctp_hdr_len);
11061676Sjpk 		if (added == -1)
11071676Sjpk 			return (EACCES);
11081676Sjpk 		sctp->sctp_hdr_len += added;
11091676Sjpk 		sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph +
11101676Sjpk 		    added);
11111676Sjpk 		sctp->sctp_ip_hdr_len += added;
11121676Sjpk 	}
11131676Sjpk 	return (0);
11141676Sjpk }
11151676Sjpk 
11161676Sjpk static int
11171676Sjpk sctp_v6_label(sctp_t *sctp)
11181676Sjpk {
11191676Sjpk 	uchar_t optbuf[TSOL_MAX_IPV6_OPTION];
11201676Sjpk 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
11211676Sjpk 
11221676Sjpk 	if (tsol_compute_label_v6(cr, &sctp->sctp_ip6h->ip6_dst, optbuf,
11233448Sdh155122 	    sctp->sctp_mac_exempt,
11243448Sdh155122 	    sctp->sctp_sctps->sctps_netstack->netstack_ip) != 0)
11251676Sjpk 		return (EACCES);
11261676Sjpk 	if (tsol_update_sticky(&sctp->sctp_sticky_ipp, &sctp->sctp_v6label_len,
11271676Sjpk 	    optbuf) != 0)
11281676Sjpk 		return (EACCES);
11291676Sjpk 	if (sctp_build_hdrs(sctp) != 0)
11301676Sjpk 		return (EACCES);
11311676Sjpk 	return (0);
11321676Sjpk }
11331676Sjpk 
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate  * XXX implement more sophisticated logic
11360Sstevel@tonic-gate  */
11371676Sjpk int
11381735Skcpoon sctp_set_hdraddrs(sctp_t *sctp)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	sctp_faddr_t *fp;
11410Sstevel@tonic-gate 	int gotv4 = 0;
11420Sstevel@tonic-gate 	int gotv6 = 0;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs != NULL);
11450Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	/* Set up using the primary first */
11480Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) {
11490Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->faddr,
11500Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
11510Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
11520Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->saddr,
11530Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_src);
11541676Sjpk 		if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
11551676Sjpk 			gotv4 = 1;
11561676Sjpk 			if (sctp->sctp_ipversion == IPV4_VERSION) {
11571676Sjpk 				goto copyports;
11581676Sjpk 			}
11590Sstevel@tonic-gate 		}
11600Sstevel@tonic-gate 	} else {
11610Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = sctp->sctp_primary->faddr;
11620Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
11630Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = sctp->sctp_primary->saddr;
11641676Sjpk 		if (!is_system_labeled() || sctp_v6_label(sctp) == 0)
11651676Sjpk 			gotv6 = 1;
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
11690Sstevel@tonic-gate 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
11700Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
11710Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_dst);
11720Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
11730Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
11740Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_src);
11751676Sjpk 			if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
11761676Sjpk 				gotv4 = 1;
11771676Sjpk 				if (sctp->sctp_ipversion == IPV4_VERSION ||
11781676Sjpk 				    gotv6) {
11791676Sjpk 					break;
11801676Sjpk 				}
11810Sstevel@tonic-gate 			}
11822283Skp158701 		} else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
11830Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_dst = fp->faddr;
11840Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
11850Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_src = fp->saddr;
11861676Sjpk 			if (!is_system_labeled() || sctp_v6_label(sctp) == 0) {
11871676Sjpk 				gotv6 = 1;
11881676Sjpk 				if (gotv4)
11891676Sjpk 					break;
11900Sstevel@tonic-gate 			}
11910Sstevel@tonic-gate 		}
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate copyports:
11951676Sjpk 	if (!gotv4 && !gotv6)
11961676Sjpk 		return (EACCES);
11971676Sjpk 
11980Sstevel@tonic-gate 	/* copy in the ports for good measure */
11990Sstevel@tonic-gate 	sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
12000Sstevel@tonic-gate 	sctp->sctp_sctph->sh_dport = sctp->sctp_fport;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_sport = sctp->sctp_lport;
12030Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_dport = sctp->sctp_fport;
12041676Sjpk 	return (0);
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate void
12080Sstevel@tonic-gate sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	mblk_t *mp;
12110Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
12120Sstevel@tonic-gate 	size_t len;
12130Sstevel@tonic-gate 	int pad;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	len = sizeof (*ph) + ntohs(uph->sph_len);
12160Sstevel@tonic-gate 	if ((pad = len % 4) != 0) {
12170Sstevel@tonic-gate 		pad = 4 - pad;
12180Sstevel@tonic-gate 		len += pad;
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
12210Sstevel@tonic-gate 	if (mp == NULL) {
12220Sstevel@tonic-gate 		return;
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
12260Sstevel@tonic-gate 	ph->sph_type = htons(PARM_UNRECOGNIZED);
12270Sstevel@tonic-gate 	ph->sph_len = htons(len - pad);
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	/* copy in the unrecognized parameter */
12300Sstevel@tonic-gate 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
12330Sstevel@tonic-gate 	if (*errmp != NULL) {
12340Sstevel@tonic-gate 		linkb(*errmp, mp);
12350Sstevel@tonic-gate 	} else {
12360Sstevel@tonic-gate 		*errmp = mp;
12370Sstevel@tonic-gate 	}
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * o Bounds checking
12420Sstevel@tonic-gate  * o Updates remaining
12430Sstevel@tonic-gate  * o Checks alignment
12440Sstevel@tonic-gate  */
12450Sstevel@tonic-gate sctp_parm_hdr_t *
12460Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	int pad;
12490Sstevel@tonic-gate 	uint16_t len;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	len = ntohs(current->sph_len);
12520Sstevel@tonic-gate 	*remaining -= len;
12530Sstevel@tonic-gate 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
12540Sstevel@tonic-gate 		return (NULL);
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
12570Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
12580Sstevel@tonic-gate 		*remaining -= pad;
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
12610Sstevel@tonic-gate 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
12620Sstevel@tonic-gate 	return (current);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate /*
12660Sstevel@tonic-gate  * Sets the address parameters given in the INIT chunk into sctp's
12670Sstevel@tonic-gate  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
12680Sstevel@tonic-gate  * no address parameters in the INIT chunk, a single faddr is created
12690Sstevel@tonic-gate  * from the ip hdr at the beginning of pkt.
12700Sstevel@tonic-gate  * If there already are existing addresses hanging from sctp, merge
12710Sstevel@tonic-gate  * them in, if the old info contains addresses which are not present
12720Sstevel@tonic-gate  * in this new info, get rid of them, and clean the pointers if there's
12730Sstevel@tonic-gate  * messages which have this as their target address.
12740Sstevel@tonic-gate  *
1275432Svi117747  * We also re-adjust the source address list here since the list may
1276432Svi117747  * contain more than what is actually part of the association. If
1277432Svi117747  * we get here from sctp_send_cookie_echo(), we are on the active
1278432Svi117747  * side and psctp will be NULL and ich will be the INIT-ACK chunk.
1279432Svi117747  * If we get here from sctp_accept_comm(), ich will be the INIT chunk
1280432Svi117747  * and psctp will the listening endpoint.
1281432Svi117747  *
1282432Svi117747  * INIT processing: When processing the INIT we inherit the src address
1283432Svi117747  * list from the listener. For a loopback or linklocal association, we
1284432Svi117747  * delete the list and just take the address from the IP header (since
1285432Svi117747  * that's how we created the INIT-ACK). Additionally, for loopback we
1286432Svi117747  * ignore the address params in the INIT. For determining which address
1287432Svi117747  * types were sent in the INIT-ACK we follow the same logic as in
1288432Svi117747  * creating the INIT-ACK. We delete addresses of the type that are not
1289432Svi117747  * supported by the peer.
1290432Svi117747  *
1291432Svi117747  * INIT-ACK processing: When processing the INIT-ACK since we had not
1292432Svi117747  * included addr params for loopback or linklocal addresses when creating
1293432Svi117747  * the INIT, we just use the address from the IP header. Further, for
1294432Svi117747  * loopback we ignore the addr param list. We mark addresses of the
1295432Svi117747  * type not supported by the peer as unconfirmed.
1296432Svi117747  *
1297432Svi117747  * In case of INIT processing we look for supported address types in the
1298432Svi117747  * supported address param, if present. In both cases the address type in
1299432Svi117747  * the IP header is supported as well as types for addresses in the param
1300432Svi117747  * list, if any.
1301432Svi117747  *
1302432Svi117747  * Once we have the supported address types sctp_check_saddr() runs through
1303432Svi117747  * the source address list and deletes or marks as unconfirmed address of
1304432Svi117747  * types not supported by the peer.
1305432Svi117747  *
13060Sstevel@tonic-gate  * Returns 0 on success, sys errno on failure
13070Sstevel@tonic-gate  */
13080Sstevel@tonic-gate int
13090Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
13100Sstevel@tonic-gate     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
13110Sstevel@tonic-gate {
13120Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
13130Sstevel@tonic-gate 	ipha_t			*iph;
13140Sstevel@tonic-gate 	ip6_t			*ip6h;
1315432Svi117747 	in6_addr_t		hdrsaddr[1];
1316432Svi117747 	in6_addr_t		hdrdaddr[1];
13170Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
13180Sstevel@tonic-gate 	ssize_t			remaining;
13190Sstevel@tonic-gate 	int			isv4;
13200Sstevel@tonic-gate 	int			err;
13210Sstevel@tonic-gate 	sctp_faddr_t		*fp;
1322432Svi117747 	int			supp_af = 0;
1323432Svi117747 	boolean_t		check_saddr = B_TRUE;
1324852Svi117747 	in6_addr_t		curaddr;
13253448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	if (sctp_options != NULL)
13280Sstevel@tonic-gate 		*sctp_options = 0;
13290Sstevel@tonic-gate 
1330432Svi117747 	/* extract the address from the IP header */
1331432Svi117747 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1332432Svi117747 	if (isv4) {
1333432Svi117747 		iph = (ipha_t *)pkt->b_rptr;
1334432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr);
1335432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr);
1336432Svi117747 		supp_af |= PARM_SUPP_V4;
1337432Svi117747 	} else {
1338432Svi117747 		ip6h = (ip6_t *)pkt->b_rptr;
1339432Svi117747 		hdrsaddr[0] = ip6h->ip6_src;
1340432Svi117747 		hdrdaddr[0] = ip6h->ip6_dst;
1341432Svi117747 		supp_af |= PARM_SUPP_V6;
1342432Svi117747 	}
1343432Svi117747 
1344432Svi117747 	/*
1345432Svi117747 	 * Unfortunately, we can't delay this because adding an faddr
1346432Svi117747 	 * looks for the presence of the source address (from the ire
1347432Svi117747 	 * for the faddr) in the source address list. We could have
1348432Svi117747 	 * delayed this if, say, this was a loopback/linklocal connection.
1349432Svi117747 	 * Now, we just end up nuking this list and taking the addr from
1350432Svi117747 	 * the IP header for loopback/linklocal.
1351432Svi117747 	 */
13520Sstevel@tonic-gate 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
13530Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
13560Sstevel@tonic-gate 		if (err != 0)
13570Sstevel@tonic-gate 			return (err);
13580Sstevel@tonic-gate 	}
1359432Svi117747 	/*
1360432Svi117747 	 * We will add the faddr before parsing the address list as this
1361432Svi117747 	 * might be a loopback connection and we would not have to
1362432Svi117747 	 * go through the list.
1363432Svi117747 	 *
1364432Svi117747 	 * Make sure the header's addr is in the list
1365432Svi117747 	 */
1366432Svi117747 	fp = sctp_lookup_faddr(sctp, hdrsaddr);
1367432Svi117747 	if (fp == NULL) {
1368432Svi117747 		/* not included; add it now */
13691735Skcpoon 		err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE);
13701676Sjpk 		if (err != 0)
13711676Sjpk 			return (err);
13720Sstevel@tonic-gate 
1373432Svi117747 		/* sctp_faddrs will be the hdr addr */
1374432Svi117747 		fp = sctp->sctp_faddrs;
13750Sstevel@tonic-gate 	}
1376432Svi117747 	/* make the header addr the primary */
1377852Svi117747 
1378852Svi117747 	if (cl_sctp_assoc_change != NULL && psctp == NULL)
1379852Svi117747 		curaddr = sctp->sctp_current->faddr;
1380852Svi117747 
1381432Svi117747 	sctp->sctp_primary = fp;
1382432Svi117747 	sctp->sctp_current = fp;
1383432Svi117747 	sctp->sctp_mss = fp->sfa_pmss;
13840Sstevel@tonic-gate 
1385432Svi117747 	/* For loopback connections & linklocal get address from the header */
1386432Svi117747 	if (sctp->sctp_loopback || sctp->sctp_linklocal) {
1387432Svi117747 		if (sctp->sctp_nsaddrs != 0)
1388432Svi117747 			sctp_free_saddrs(sctp);
1389852Svi117747 		if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0)
1390432Svi117747 			return (err);
1391432Svi117747 		/* For loopback ignore address list */
1392432Svi117747 		if (sctp->sctp_loopback)
1393432Svi117747 			return (0);
1394432Svi117747 		check_saddr = B_FALSE;
1395432Svi117747 	}
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
13980Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
13990Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
14000Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
1401432Svi117747 		if (check_saddr) {
1402432Svi117747 			sctp_check_saddr(sctp, supp_af, psctp == NULL ?
1403432Svi117747 			    B_FALSE : B_TRUE);
1404432Svi117747 		}
1405852Svi117747 		ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1406432Svi117747 		return (0);
14070Sstevel@tonic-gate 	}
1408432Svi117747 
14090Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
14100Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
14110Sstevel@tonic-gate 
1412432Svi117747 	/* params will have already been byteordered when validating */
14130Sstevel@tonic-gate 	while (ph != NULL) {
1414432Svi117747 		if (ph->sph_type == htons(PARM_SUPP_ADDRS)) {
1415432Svi117747 			int		plen;
1416432Svi117747 			uint16_t	*p;
1417432Svi117747 			uint16_t	addrtype;
1418432Svi117747 
1419432Svi117747 			ASSERT(psctp != NULL);
1420432Svi117747 			plen = ntohs(ph->sph_len);
1421432Svi117747 			p = (uint16_t *)(ph + 1);
1422432Svi117747 			while (plen > 0) {
1423432Svi117747 				addrtype = ntohs(*p);
1424432Svi117747 				switch (addrtype) {
1425432Svi117747 					case PARM_ADDR6:
1426432Svi117747 						supp_af |= PARM_SUPP_V6;
1427432Svi117747 						break;
1428432Svi117747 					case PARM_ADDR4:
1429432Svi117747 						supp_af |= PARM_SUPP_V4;
1430432Svi117747 						break;
1431432Svi117747 					default:
1432432Svi117747 						break;
1433432Svi117747 				}
1434432Svi117747 				p++;
1435432Svi117747 				plen -= sizeof (*p);
1436432Svi117747 			}
1437432Svi117747 		} else if (ph->sph_type == htons(PARM_ADDR4)) {
14380Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
14390Sstevel@tonic-gate 				in6_addr_t addr;
14400Sstevel@tonic-gate 				ipaddr_t ta;
14410Sstevel@tonic-gate 
1442432Svi117747 				supp_af |= PARM_SUPP_V4;
14430Sstevel@tonic-gate 				/*
14440Sstevel@tonic-gate 				 * Screen out broad/multicasts & loopback.
14450Sstevel@tonic-gate 				 * If the endpoint only accepts v6 address,
14460Sstevel@tonic-gate 				 * go to the next one.
14470Sstevel@tonic-gate 				 */
14480Sstevel@tonic-gate 				bcopy(ph + 1, &ta, sizeof (ta));
14490Sstevel@tonic-gate 				if (ta == 0 ||
14500Sstevel@tonic-gate 				    ta == INADDR_BROADCAST ||
14510Sstevel@tonic-gate 				    ta == htonl(INADDR_LOOPBACK) ||
14520Sstevel@tonic-gate 				    IN_MULTICAST(ta) ||
14530Sstevel@tonic-gate 				    sctp->sctp_connp->conn_ipv6_v6only) {
14540Sstevel@tonic-gate 					goto next;
14550Sstevel@tonic-gate 				}
14560Sstevel@tonic-gate 				/*
14570Sstevel@tonic-gate 				 * XXX also need to check for subnet
14580Sstevel@tonic-gate 				 * broadcasts. This should probably
14590Sstevel@tonic-gate 				 * wait until we have full access
14600Sstevel@tonic-gate 				 * to the ILL tables.
14610Sstevel@tonic-gate 				 */
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
14640Sstevel@tonic-gate 				    (ph + 1), &addr);
14650Sstevel@tonic-gate 				/* Check for duplicate. */
14660Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
14670Sstevel@tonic-gate 					goto next;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 				/* OK, add it to the faddr set */
14701735Skcpoon 				err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP,
14711735Skcpoon 				    B_FALSE);
14721676Sjpk 				if (err != 0)
14731676Sjpk 					return (err);
14740Sstevel@tonic-gate 			}
14750Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
14760Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
14770Sstevel@tonic-gate 			/* An v4 socket should not take v6 addresses. */
14780Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
14790Sstevel@tonic-gate 				in6_addr_t *addr6;
14800Sstevel@tonic-gate 
1481432Svi117747 				supp_af |= PARM_SUPP_V6;
14820Sstevel@tonic-gate 				addr6 = (in6_addr_t *)(ph + 1);
14830Sstevel@tonic-gate 				/*
14840Sstevel@tonic-gate 				 * Screen out link locals, mcast, loopback
14850Sstevel@tonic-gate 				 * and bogus v6 address.
14860Sstevel@tonic-gate 				 */
14870Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
14880Sstevel@tonic-gate 				    IN6_IS_ADDR_MULTICAST(addr6) ||
14890Sstevel@tonic-gate 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
14900Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
14910Sstevel@tonic-gate 					goto next;
14920Sstevel@tonic-gate 				}
14930Sstevel@tonic-gate 				/* Check for duplicate. */
14940Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
14950Sstevel@tonic-gate 					goto next;
14960Sstevel@tonic-gate 
14971676Sjpk 				err = sctp_add_faddr(sctp,
14981735Skcpoon 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP,
14991735Skcpoon 				    B_FALSE);
15001676Sjpk 				if (err != 0)
15011676Sjpk 					return (err);
15020Sstevel@tonic-gate 			}
15030Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
15040Sstevel@tonic-gate 			if (sctp_options != NULL)
15050Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
15060Sstevel@tonic-gate 		} /* else; skip */
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate next:
15090Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
15100Sstevel@tonic-gate 	}
1511432Svi117747 	if (check_saddr) {
1512432Svi117747 		sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE :
1513432Svi117747 		    B_TRUE);
15140Sstevel@tonic-gate 	}
1515852Svi117747 	ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1516852Svi117747 	/*
1517852Svi117747 	 * We have the right address list now, update clustering's
1518852Svi117747 	 * knowledge because when we sent the INIT we had just added
1519852Svi117747 	 * the address the INIT was sent to.
1520852Svi117747 	 */
1521852Svi117747 	if (psctp == NULL && cl_sctp_assoc_change != NULL) {
1522852Svi117747 		uchar_t	*alist;
1523852Svi117747 		size_t	asize;
1524852Svi117747 		uchar_t	*dlist;
1525852Svi117747 		size_t	dsize;
1526852Svi117747 
1527852Svi117747 		asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
1528852Svi117747 		alist = kmem_alloc(asize, KM_NOSLEEP);
15291735Skcpoon 		if (alist == NULL) {
15303448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1531852Svi117747 			return (ENOMEM);
15321735Skcpoon 		}
1533852Svi117747 		/*
1534852Svi117747 		 * Just include the address the INIT was sent to in the
1535852Svi117747 		 * delete list and send the entire faddr list. We could
1536852Svi117747 		 * do it differently (i.e include all the addresses in the
1537852Svi117747 		 * add list even if it contains the original address OR
1538852Svi117747 		 * remove the original address from the add list etc.), but
1539852Svi117747 		 * this seems reasonable enough.
1540852Svi117747 		 */
1541852Svi117747 		dsize = sizeof (in6_addr_t);
1542852Svi117747 		dlist = kmem_alloc(dsize, KM_NOSLEEP);
1543852Svi117747 		if (dlist == NULL) {
1544852Svi117747 			kmem_free(alist, asize);
15453448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1546852Svi117747 			return (ENOMEM);
1547852Svi117747 		}
1548852Svi117747 		bcopy(&curaddr, dlist, sizeof (curaddr));
1549852Svi117747 		sctp_get_faddr_list(sctp, alist, asize);
1550852Svi117747 		(*cl_sctp_assoc_change)(sctp->sctp_family, alist, asize,
1551852Svi117747 		    sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR,
1552852Svi117747 		    (cl_sctp_handle_t)sctp);
1553852Svi117747 		/* alist and dlist will be freed by the clustering module */
1554852Svi117747 	}
15550Sstevel@tonic-gate 	return (0);
15560Sstevel@tonic-gate }
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate /*
15590Sstevel@tonic-gate  * Returns 0 if the check failed and the restart should be refused,
15600Sstevel@tonic-gate  * 1 if the check succeeded.
15610Sstevel@tonic-gate  */
15620Sstevel@tonic-gate int
15630Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
15643448Sdh155122     int sleep, sctp_stack_t *sctps)
15650Sstevel@tonic-gate {
15660Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpa, *fphead = NULL;
15670Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
15680Sstevel@tonic-gate 	ssize_t remaining;
15690Sstevel@tonic-gate 	int isv4;
15700Sstevel@tonic-gate 	ipha_t *iph;
15710Sstevel@tonic-gate 	ip6_t *ip6h;
15720Sstevel@tonic-gate 	in6_addr_t hdraddr[1];
15730Sstevel@tonic-gate 	int retval = 0;
15740Sstevel@tonic-gate 	sctp_tf_t *tf;
15750Sstevel@tonic-gate 	sctp_t *sctp;
15760Sstevel@tonic-gate 	int compres;
15770Sstevel@tonic-gate 	sctp_init_chunk_t *init;
15780Sstevel@tonic-gate 	int nadded = 0;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	/* extract the address from the IP header */
15810Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
15820Sstevel@tonic-gate 	if (isv4) {
15830Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
15840Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
15850Sstevel@tonic-gate 	} else {
15860Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
15870Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
15910Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
15920Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
15930Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
15940Sstevel@tonic-gate 		/* no parameters; restart OK */
15950Sstevel@tonic-gate 		return (1);
15960Sstevel@tonic-gate 	}
15970Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
15980Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	while (ph != NULL) {
16010Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
16020Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
16030Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
16040Sstevel@tonic-gate 				in6_addr_t addr;
16050Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
16060Sstevel@tonic-gate 				    (ph + 1), &addr);
16070Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
16080Sstevel@tonic-gate 				    sleep);
16090Sstevel@tonic-gate 				if (!fpa) {
16100Sstevel@tonic-gate 					goto done;
16110Sstevel@tonic-gate 				}
16120Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
16130Sstevel@tonic-gate 				fpa->faddr = addr;
16140Sstevel@tonic-gate 				fpa->next = NULL;
16150Sstevel@tonic-gate 			}
16160Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
16170Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
16180Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
16190Sstevel@tonic-gate 				    sleep);
16200Sstevel@tonic-gate 				if (!fpa) {
16210Sstevel@tonic-gate 					goto done;
16220Sstevel@tonic-gate 				}
16230Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
16240Sstevel@tonic-gate 				bcopy(ph + 1, &fpa->faddr,
16250Sstevel@tonic-gate 				    sizeof (fpa->faddr));
16260Sstevel@tonic-gate 				fpa->next = NULL;
16270Sstevel@tonic-gate 			}
16280Sstevel@tonic-gate 		} else {
16290Sstevel@tonic-gate 			/* else not addr param; skip */
16300Sstevel@tonic-gate 			fpa = NULL;
16310Sstevel@tonic-gate 		}
16320Sstevel@tonic-gate 		/* link in the new addr, if it was an addr param */
16330Sstevel@tonic-gate 		if (fpa) {
16340Sstevel@tonic-gate 			if (!fphead) {
16350Sstevel@tonic-gate 				fphead = fpa;
16360Sstevel@tonic-gate 				fp = fphead;
16370Sstevel@tonic-gate 			} else {
16380Sstevel@tonic-gate 				fp->next = fpa;
16390Sstevel@tonic-gate 				fp = fpa;
16400Sstevel@tonic-gate 			}
16410Sstevel@tonic-gate 		}
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	if (fphead == NULL) {
16470Sstevel@tonic-gate 		/* no addr parameters; restart OK */
16480Sstevel@tonic-gate 		return (1);
16490Sstevel@tonic-gate 	}
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	/*
16520Sstevel@tonic-gate 	 * got at least one; make sure the header's addr is
16530Sstevel@tonic-gate 	 * in the list
16540Sstevel@tonic-gate 	 */
16550Sstevel@tonic-gate 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
16560Sstevel@tonic-gate 	if (!fp) {
16570Sstevel@tonic-gate 		/* not included; add it now */
16580Sstevel@tonic-gate 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
16590Sstevel@tonic-gate 		if (!fp) {
16600Sstevel@tonic-gate 			goto done;
16610Sstevel@tonic-gate 		}
16620Sstevel@tonic-gate 		bzero(fp, sizeof (*fp));
16630Sstevel@tonic-gate 		fp->faddr = *hdraddr;
16640Sstevel@tonic-gate 		fp->next = fphead;
16650Sstevel@tonic-gate 		fphead = fp;
16660Sstevel@tonic-gate 	}
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	/*
16690Sstevel@tonic-gate 	 * Now, we can finally do the check: For each sctp instance
16700Sstevel@tonic-gate 	 * on the hash line for ports, compare its faddr set against
16710Sstevel@tonic-gate 	 * the new one. If the new one is a strict subset of any
16720Sstevel@tonic-gate 	 * existing sctp's faddrs, the restart is OK. However, if there
16730Sstevel@tonic-gate 	 * is an overlap, this could be an attack, so return failure.
16740Sstevel@tonic-gate 	 * If all sctp's faddrs are disjoint, this is a legitimate new
16750Sstevel@tonic-gate 	 * association.
16760Sstevel@tonic-gate 	 */
16773448Sdh155122 	tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]);
16780Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
16810Sstevel@tonic-gate 		if (ports != sctp->sctp_ports) {
16820Sstevel@tonic-gate 			continue;
16830Sstevel@tonic-gate 		}
16840Sstevel@tonic-gate 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
16850Sstevel@tonic-gate 		if (compres <= SCTP_ADDR_SUBSET) {
16860Sstevel@tonic-gate 			retval = 1;
16870Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16880Sstevel@tonic-gate 			goto done;
16890Sstevel@tonic-gate 		}
16900Sstevel@tonic-gate 		if (compres == SCTP_ADDR_OVERLAP) {
16910Sstevel@tonic-gate 			dprint(1,
16920Sstevel@tonic-gate 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
16931676Sjpk 			    SCTP_PRINTADDR(*hdraddr), (void *)sctp));
16940Sstevel@tonic-gate 			/*
16950Sstevel@tonic-gate 			 * While we still hold the lock, we need to
16960Sstevel@tonic-gate 			 * figure out which addresses have been
16970Sstevel@tonic-gate 			 * added so we can include them in the abort
16980Sstevel@tonic-gate 			 * we will send back. Since these faddrs will
16990Sstevel@tonic-gate 			 * never be used, we overload the rto field
17000Sstevel@tonic-gate 			 * here, setting it to 0 if the address was
17010Sstevel@tonic-gate 			 * not added, 1 if it was added.
17020Sstevel@tonic-gate 			 */
17030Sstevel@tonic-gate 			for (fp = fphead; fp; fp = fp->next) {
17040Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &fp->faddr)) {
17050Sstevel@tonic-gate 					fp->rto = 0;
17060Sstevel@tonic-gate 				} else {
17070Sstevel@tonic-gate 					fp->rto = 1;
17080Sstevel@tonic-gate 					nadded++;
17090Sstevel@tonic-gate 				}
17100Sstevel@tonic-gate 			}
17110Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
17120Sstevel@tonic-gate 			goto done;
17130Sstevel@tonic-gate 		}
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	/* All faddrs are disjoint; legit new association */
17180Sstevel@tonic-gate 	retval = 1;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate done:
17210Sstevel@tonic-gate 	/* If are attempted adds, send back an abort listing the addrs */
17220Sstevel@tonic-gate 	if (nadded > 0) {
17230Sstevel@tonic-gate 		void *dtail;
17240Sstevel@tonic-gate 		size_t dlen;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
17270Sstevel@tonic-gate 		if (dtail == NULL) {
17280Sstevel@tonic-gate 			goto cleanup;
17290Sstevel@tonic-gate 		}
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 		ph = dtail;
17320Sstevel@tonic-gate 		dlen = 0;
17330Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fp->next) {
17340Sstevel@tonic-gate 			if (fp->rto == 0) {
17350Sstevel@tonic-gate 				continue;
17360Sstevel@tonic-gate 			}
17370Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
17380Sstevel@tonic-gate 				ipaddr_t addr4;
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR4);
17410Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR4_LEN);
17420Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
17430Sstevel@tonic-gate 				ph++;
17440Sstevel@tonic-gate 				bcopy(&addr4, ph, sizeof (addr4));
17450Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
17460Sstevel@tonic-gate 				    ((char *)ph + sizeof (addr4));
17470Sstevel@tonic-gate 				dlen += PARM_ADDR4_LEN;
17480Sstevel@tonic-gate 			} else {
17490Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR6);
17500Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR6_LEN);
17510Sstevel@tonic-gate 				ph++;
17520Sstevel@tonic-gate 				bcopy(&fp->faddr, ph, sizeof (fp->faddr));
17530Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
17540Sstevel@tonic-gate 				    ((char *)ph + sizeof (fp->faddr));
17550Sstevel@tonic-gate 				dlen += PARM_ADDR6_LEN;
17560Sstevel@tonic-gate 			}
17570Sstevel@tonic-gate 		}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 		/* Send off the abort */
17600Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ich),
17610Sstevel@tonic-gate 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate cleanup:
17670Sstevel@tonic-gate 	/* Clean up */
17680Sstevel@tonic-gate 	if (fphead) {
17690Sstevel@tonic-gate 		sctp_faddr_t *fpn;
17700Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fpn) {
17710Sstevel@tonic-gate 			fpn = fp->next;
17720Sstevel@tonic-gate 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
17730Sstevel@tonic-gate 		}
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	return (retval);
17770Sstevel@tonic-gate }
17780Sstevel@tonic-gate 
17791932Svi117747 /*
17801932Svi117747  * Reset any state related to transmitted chunks.
17811932Svi117747  */
17820Sstevel@tonic-gate void
17830Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp)
17840Sstevel@tonic-gate {
17851932Svi117747 	sctp_faddr_t	*fp;
17863448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17871932Svi117747 	mblk_t		*mp;
17880Sstevel@tonic-gate 
17891932Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
17903448Sdh155122 		fp->ssthresh = sctps->sctps_initial_mtu;
17913795Skcpoon 		SET_CWND(fp, fp->sfa_pmss, sctps->sctps_slow_start_initial);
17920Sstevel@tonic-gate 		fp->suna = 0;
17930Sstevel@tonic-gate 		fp->pba = 0;
17940Sstevel@tonic-gate 	}
17951932Svi117747 	/*
17961932Svi117747 	 * Clean up the transmit list as well since we have reset accounting
17971932Svi117747 	 * on all the fps. Send event upstream, if required.
17981932Svi117747 	 */
17991932Svi117747 	while ((mp = sctp->sctp_xmit_head) != NULL) {
18001932Svi117747 		sctp->sctp_xmit_head = mp->b_next;
18011932Svi117747 		mp->b_next = NULL;
18021932Svi117747 		if (sctp->sctp_xmit_head != NULL)
18031932Svi117747 			sctp->sctp_xmit_head->b_prev = NULL;
18041932Svi117747 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
18051932Svi117747 	}
18061932Svi117747 	sctp->sctp_xmit_head = NULL;
18071932Svi117747 	sctp->sctp_xmit_tail = NULL;
18081932Svi117747 	sctp->sctp_xmit_unacked = NULL;
18091932Svi117747 
18101932Svi117747 	sctp->sctp_unacked = 0;
18111932Svi117747 	/*
18121932Svi117747 	 * Any control message as well. We will clean-up this list as well.
18131932Svi117747 	 * This contains any pending ASCONF request that we have queued/sent.
18141932Svi117747 	 * If we do get an ACK we will just drop it. However, given that
18151932Svi117747 	 * we are restarting chances are we aren't going to get any.
18161932Svi117747 	 */
18171932Svi117747 	if (sctp->sctp_cxmit_list != NULL)
18181932Svi117747 		sctp_asconf_free_cxmit(sctp, NULL);
18191932Svi117747 	sctp->sctp_cxmit_list = NULL;
18201932Svi117747 	sctp->sctp_cchunk_pend = 0;
18211932Svi117747 
18221932Svi117747 	sctp->sctp_rexmitting = B_FALSE;
18231932Svi117747 	sctp->sctp_rxt_nxttsn = 0;
18241932Svi117747 	sctp->sctp_rxt_maxtsn = 0;
18251932Svi117747 
18261932Svi117747 	sctp->sctp_zero_win_probe = B_FALSE;
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate static void
18301735Skcpoon sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr,
18311735Skcpoon     mblk_t *timer_mp)
18320Sstevel@tonic-gate {
18333448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
18343448Sdh155122 
18350Sstevel@tonic-gate 	bcopy(addr, &fp->faddr, sizeof (*addr));
18360Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
18370Sstevel@tonic-gate 		fp->isv4 = 1;
18380Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
1839*4691Skcpoon 		fp->sfa_pmss =
1840*4691Skcpoon 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) &
1841*4691Skcpoon 		    ~(SCTP_ALIGN - 1);
18420Sstevel@tonic-gate 	} else {
18430Sstevel@tonic-gate 		fp->isv4 = 0;
18443448Sdh155122 		fp->sfa_pmss =
18453448Sdh155122 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) &
18463448Sdh155122 		    ~(SCTP_ALIGN - 1);
18470Sstevel@tonic-gate 	}
18483448Sdh155122 	fp->cwnd = sctps->sctps_slow_start_initial * fp->sfa_pmss;
18490Sstevel@tonic-gate 	fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max);
18500Sstevel@tonic-gate 	fp->srtt = -1;
18510Sstevel@tonic-gate 	fp->rtt_updates = 0;
18520Sstevel@tonic-gate 	fp->strikes = 0;
18530Sstevel@tonic-gate 	fp->max_retr = sctp->sctp_pp_max_rxt;
18540Sstevel@tonic-gate 	/* Mark it as not confirmed. */
18550Sstevel@tonic-gate 	fp->state = SCTP_FADDRS_UNCONFIRMED;
18560Sstevel@tonic-gate 	fp->hb_interval = sctp->sctp_hb_interval;
18573448Sdh155122 	fp->ssthresh = sctps->sctps_initial_ssthresh;
18580Sstevel@tonic-gate 	fp->suna = 0;
18590Sstevel@tonic-gate 	fp->pba = 0;
18600Sstevel@tonic-gate 	fp->acked = 0;
18610Sstevel@tonic-gate 	fp->lastactive = lbolt64;
18621735Skcpoon 	fp->timer_mp = timer_mp;
18630Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
18640Sstevel@tonic-gate 	fp->timer_running = 0;
18650Sstevel@tonic-gate 	fp->df = 1;
18660Sstevel@tonic-gate 	fp->pmtu_discovered = 0;
18670Sstevel@tonic-gate 	fp->rc_timer_mp = NULL;
18680Sstevel@tonic-gate 	fp->rc_timer_running = 0;
18690Sstevel@tonic-gate 	fp->next = NULL;
18700Sstevel@tonic-gate 	fp->ire = NULL;
18710Sstevel@tonic-gate 	fp->T3expire = 0;
18720Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret,
18730Sstevel@tonic-gate 	    sizeof (fp->hb_secret));
18740Sstevel@tonic-gate 	fp->hb_expiry = lbolt64;
18753795Skcpoon 	fp->rxt_unacked = 0;
18760Sstevel@tonic-gate 
18771735Skcpoon 	sctp_get_ire(sctp, fp);
18780Sstevel@tonic-gate }
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate /*ARGSUSED*/
18810Sstevel@tonic-gate static void
18820Sstevel@tonic-gate faddr_destructor(void *buf, void *cdrarg)
18830Sstevel@tonic-gate {
18840Sstevel@tonic-gate 	sctp_faddr_t *fp = buf;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	ASSERT(fp->timer_mp == NULL);
18870Sstevel@tonic-gate 	ASSERT(fp->timer_running == 0);
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	ASSERT(fp->rc_timer_mp == NULL);
18900Sstevel@tonic-gate 	ASSERT(fp->rc_timer_running == 0);
18910Sstevel@tonic-gate }
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate void
18941676Sjpk sctp_faddr_init(void)
18950Sstevel@tonic-gate {
18960Sstevel@tonic-gate 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
18970Sstevel@tonic-gate 	    sizeof (sctp_faddr_t), 0, NULL, faddr_destructor,
18980Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
18990Sstevel@tonic-gate }
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate void
19021676Sjpk sctp_faddr_fini(void)
19030Sstevel@tonic-gate {
19040Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_faddr_cache);
19050Sstevel@tonic-gate }
1906