xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_common.c (revision 10212:1abfeb13f29b)
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 /*
238549SGeorge.Shepherd@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/stream.h>
301676Sjpk #include <sys/strsubr.h>
310Sstevel@tonic-gate #include <sys/ddi.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/random.h>
361676Sjpk #include <sys/tsol/tndb.h>
371676Sjpk #include <sys/tsol/tnet.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <netinet/in.h>
400Sstevel@tonic-gate #include <netinet/ip6.h>
410Sstevel@tonic-gate #include <netinet/sctp.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <inet/common.h>
440Sstevel@tonic-gate #include <inet/ip.h>
450Sstevel@tonic-gate #include <inet/ip6.h>
460Sstevel@tonic-gate #include <inet/ip_ire.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>
521676Sjpk 
530Sstevel@tonic-gate #include "sctp_impl.h"
540Sstevel@tonic-gate #include "sctp_addr.h"
551932Svi117747 #include "sctp_asconf.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_faddr_cache;
581735Skcpoon static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *);
590Sstevel@tonic-gate 
601735Skcpoon /* Set the source address.  Refer to comments in sctp_get_ire(). */
611932Svi117747 void
621932Svi117747 sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp)
630Sstevel@tonic-gate {
641719Sjarrett 	boolean_t v6 = !fp->isv4;
654818Skcpoon 	boolean_t addr_set;
661719Sjarrett 
674818Skcpoon 	fp->saddr = sctp_get_valid_addr(sctp, v6, &addr_set);
684818Skcpoon 	/*
694818Skcpoon 	 * If there is no source address avaialble, mark this peer address
704818Skcpoon 	 * as unreachable for now.  When the heartbeat timer fires, it will
714818Skcpoon 	 * call sctp_get_ire() to re-check if there is any source address
724818Skcpoon 	 * available.
734818Skcpoon 	 */
744818Skcpoon 	if (!addr_set)
754818Skcpoon 		fp->state = SCTP_FADDRS_UNREACH;
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Call this function to update the cached IRE of a peer addr fp.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate void
821735Skcpoon sctp_get_ire(sctp_t *sctp, sctp_faddr_t *fp)
830Sstevel@tonic-gate {
841735Skcpoon 	ire_t		*ire;
851735Skcpoon 	ipaddr_t	addr4;
861735Skcpoon 	in6_addr_t	laddr;
870Sstevel@tonic-gate 	sctp_saddr_ipif_t *sp;
881735Skcpoon 	int		hdrlen;
891735Skcpoon 	ts_label_t	*tsl;
903448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
913448Sdh155122 	ip_stack_t	*ipst = sctps->sctps_netstack->netstack_ip;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* Remove the previous cache IRE */
940Sstevel@tonic-gate 	if ((ire = fp->ire) != NULL) {
950Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
960Sstevel@tonic-gate 		fp->ire = NULL;
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * If this addr is not reachable, mark it as unconfirmed for now, the
1010Sstevel@tonic-gate 	 * state will be changed back to unreachable later in this function
1020Sstevel@tonic-gate 	 * if it is still the case.
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH) {
1050Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_UNCONFIRMED;
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1081676Sjpk 	tsl = crgetlabel(CONN_CRED(sctp->sctp_connp));
1091676Sjpk 
1100Sstevel@tonic-gate 	if (fp->isv4) {
1110Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
1123448Sdh155122 		ire = ire_cache_lookup(addr4, sctp->sctp_zoneid, tsl, ipst);
1131676Sjpk 		if (ire != NULL)
1141676Sjpk 			IN6_IPADDR_TO_V4MAPPED(ire->ire_src_addr, &laddr);
1151676Sjpk 	} else {
1163448Sdh155122 		ire = ire_cache_lookup_v6(&fp->faddr, sctp->sctp_zoneid, tsl,
1173448Sdh155122 		    ipst);
1181676Sjpk 		if (ire != NULL)
1191676Sjpk 			laddr = ire->ire_src_addr_v6;
1201676Sjpk 	}
1210Sstevel@tonic-gate 
1221676Sjpk 	if (ire == NULL) {
1231676Sjpk 		dprint(3, ("ire2faddr: no ire for %x:%x:%x:%x\n",
1241676Sjpk 		    SCTP_PRINTADDR(fp->faddr)));
1251676Sjpk 		/*
1261676Sjpk 		 * It is tempting to just leave the src addr
1271676Sjpk 		 * unspecified and let IP figure it out, but we
1281676Sjpk 		 * *cannot* do this, since IP may choose a src addr
1291676Sjpk 		 * that is not part of this association... unless
1301676Sjpk 		 * this sctp has bound to all addrs.  So if the ire
1311676Sjpk 		 * lookup fails, try to find one in our src addr
1321676Sjpk 		 * list, unless the sctp has bound to all addrs, in
1331676Sjpk 		 * which case we change the src addr to unspec.
1341676Sjpk 		 *
1351676Sjpk 		 * Note that if this is a v6 endpoint but it does
1361676Sjpk 		 * not have any v4 address at this point (e.g. may
1371676Sjpk 		 * have been  deleted), sctp_get_valid_addr() will
1381676Sjpk 		 * return mapped INADDR_ANY.  In this case, this
1391676Sjpk 		 * address should be marked not reachable so that
1401676Sjpk 		 * it won't be used to send data.
1411676Sjpk 		 */
1421932Svi117747 		sctp_set_saddr(sctp, fp);
1431735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH)
1441735Skcpoon 			return;
1451735Skcpoon 		goto check_current;
1461676Sjpk 	}
1471676Sjpk 
1481735Skcpoon 	/* Make sure the laddr is part of this association */
1493510Svi117747 	if ((sp = sctp_saddr_lookup(sctp, &ire->ire_ipif->ipif_v6lcl_addr,
1503510Svi117747 	    0)) != NULL && !sp->saddr_ipif_dontsrc) {
1511676Sjpk 		if (sp->saddr_ipif_unconfirmed == 1)
1521676Sjpk 			sp->saddr_ipif_unconfirmed = 0;
1531676Sjpk 		fp->saddr = laddr;
1541676Sjpk 	} else {
1551676Sjpk 		dprint(2, ("ire2faddr: src addr is not part of assc\n"));
1561735Skcpoon 
1571735Skcpoon 		/*
1581735Skcpoon 		 * Set the src to the first saddr and hope for the best.
1591735Skcpoon 		 * Note that we will still do the ire caching below.
1601735Skcpoon 		 * Otherwise, whenever we send a packet, we need to do
1611735Skcpoon 		 * the ire lookup again and still may not get the correct
1621735Skcpoon 		 * source address.  Note that this case should very seldomly
1631735Skcpoon 		 * happen.  One scenario this can happen is an app
1641735Skcpoon 		 * explicitly bind() to an address.  But that address is
1651735Skcpoon 		 * not the preferred source address to send to the peer.
1661735Skcpoon 		 */
1671932Svi117747 		sctp_set_saddr(sctp, fp);
1681735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH) {
1691735Skcpoon 			IRE_REFRELE(ire);
1701735Skcpoon 			return;
1711735Skcpoon 		}
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1741735Skcpoon 	/*
1751735Skcpoon 	 * Note that ire_cache_lookup_*() returns an ire with the tracing
1761735Skcpoon 	 * bits enabled.  This requires the thread holding the ire also
1771735Skcpoon 	 * do the IRE_REFRELE().  Thus we need to do IRE_REFHOLD_NOTR()
1781735Skcpoon 	 * and then IRE_REFRELE() the ire here to make the tracing bits
1791735Skcpoon 	 * work.
1801735Skcpoon 	 */
1811735Skcpoon 	IRE_REFHOLD_NOTR(ire);
1821735Skcpoon 	IRE_REFRELE(ire);
1831735Skcpoon 
1840Sstevel@tonic-gate 	/* Cache the IRE */
1850Sstevel@tonic-gate 	fp->ire = ire;
1860Sstevel@tonic-gate 	if (fp->ire->ire_type == IRE_LOOPBACK && !sctp->sctp_loopback)
1870Sstevel@tonic-gate 		sctp->sctp_loopback = 1;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * Pull out RTO information for this faddr and use it if we don't
1910Sstevel@tonic-gate 	 * have any yet.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	if (fp->srtt == -1 && ire->ire_uinfo.iulp_rtt != 0) {
194116Skcpoon 		/* The cached value is in ms. */
195116Skcpoon 		fp->srtt = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt);
196116Skcpoon 		fp->rttvar = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt_sd);
1970Sstevel@tonic-gate 		fp->rto = 3 * fp->srtt;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		/* Bound the RTO by configured min and max values */
2000Sstevel@tonic-gate 		if (fp->rto < sctp->sctp_rto_min) {
2010Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_min;
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 		if (fp->rto > sctp->sctp_rto_max) {
2040Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_max;
2050Sstevel@tonic-gate 		}
206*10212SGeorge.Shepherd@Sun.COM 		SCTP_MAX_RTO(sctp, fp);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * Record the MTU for this faddr. If the MTU for this faddr has
2110Sstevel@tonic-gate 	 * changed, check if the assc MTU will also change.
2120Sstevel@tonic-gate 	 */
2130Sstevel@tonic-gate 	if (fp->isv4) {
2140Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
2150Sstevel@tonic-gate 	} else {
2160Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 	if ((fp->sfa_pmss + hdrlen) != ire->ire_max_frag) {
2190Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
2200Sstevel@tonic-gate 		fp->sfa_pmss = (ire->ire_max_frag - hdrlen) & ~(SCTP_ALIGN - 1);
2210Sstevel@tonic-gate 		if (fp->cwnd < (fp->sfa_pmss * 2)) {
2223795Skcpoon 			SET_CWND(fp, fp->sfa_pmss,
2233795Skcpoon 			    sctps->sctps_slow_start_initial);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2271735Skcpoon check_current:
2281735Skcpoon 	if (fp == sctp->sctp_current)
2291735Skcpoon 		sctp_set_faddr_current(sctp, fp);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate void
2331735Skcpoon sctp_update_ire(sctp_t *sctp)
2340Sstevel@tonic-gate {
2351735Skcpoon 	ire_t		*ire;
2361735Skcpoon 	sctp_faddr_t	*fp;
2373448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2380Sstevel@tonic-gate 
2391735Skcpoon 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
2401735Skcpoon 		if ((ire = fp->ire) == NULL)
2411735Skcpoon 			continue;
2421735Skcpoon 		mutex_enter(&ire->ire_lock);
2430Sstevel@tonic-gate 
2441735Skcpoon 		/*
2451735Skcpoon 		 * If the cached IRE is going away, there is no point to
2461735Skcpoon 		 * update it.
2471735Skcpoon 		 */
2481735Skcpoon 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
2491735Skcpoon 			mutex_exit(&ire->ire_lock);
2501735Skcpoon 			IRE_REFRELE_NOTR(ire);
2511735Skcpoon 			fp->ire = NULL;
2521735Skcpoon 			continue;
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		/*
2561735Skcpoon 		 * Only record the PMTU for this faddr if we actually have
2571735Skcpoon 		 * done discovery. This prevents initialized default from
2581735Skcpoon 		 * clobbering any real info that IP may have.
2590Sstevel@tonic-gate 		 */
2601735Skcpoon 		if (fp->pmtu_discovered) {
2611735Skcpoon 			if (fp->isv4) {
2621735Skcpoon 				ire->ire_max_frag = fp->sfa_pmss +
2631735Skcpoon 				    sctp->sctp_hdr_len;
2641735Skcpoon 			} else {
2651735Skcpoon 				ire->ire_max_frag = fp->sfa_pmss +
2661735Skcpoon 				    sctp->sctp_hdr6_len;
2671735Skcpoon 			}
2680Sstevel@tonic-gate 		}
2691735Skcpoon 
2703448Sdh155122 		if (sctps->sctps_rtt_updates != 0 &&
2713448Sdh155122 		    fp->rtt_updates >= sctps->sctps_rtt_updates) {
2721735Skcpoon 			/*
2731735Skcpoon 			 * If there is no old cached values, initialize them
2741735Skcpoon 			 * conservatively.  Set them to be (1.5 * new value).
2751735Skcpoon 			 * This code copied from ip_ire_advise().  The cached
2761735Skcpoon 			 * value is in ms.
2771735Skcpoon 			 */
2781735Skcpoon 			if (ire->ire_uinfo.iulp_rtt != 0) {
2791735Skcpoon 				ire->ire_uinfo.iulp_rtt =
2801735Skcpoon 				    (ire->ire_uinfo.iulp_rtt +
2811735Skcpoon 				    TICK_TO_MSEC(fp->srtt)) >> 1;
2821735Skcpoon 			} else {
2831735Skcpoon 				ire->ire_uinfo.iulp_rtt =
2841735Skcpoon 				    TICK_TO_MSEC(fp->srtt + (fp->srtt >> 1));
2851735Skcpoon 			}
2861735Skcpoon 			if (ire->ire_uinfo.iulp_rtt_sd != 0) {
2871735Skcpoon 				ire->ire_uinfo.iulp_rtt_sd =
2884691Skcpoon 				    (ire->ire_uinfo.iulp_rtt_sd +
2894691Skcpoon 				    TICK_TO_MSEC(fp->rttvar)) >> 1;
2901735Skcpoon 			} else {
2911735Skcpoon 				ire->ire_uinfo.iulp_rtt_sd =
2921735Skcpoon 				    TICK_TO_MSEC(fp->rttvar +
2931735Skcpoon 				    (fp->rttvar >> 1));
2941735Skcpoon 			}
2951735Skcpoon 			fp->rtt_updates = 0;
2960Sstevel@tonic-gate 		}
2971735Skcpoon 		mutex_exit(&ire->ire_lock);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * The sender must set the total length in the IP header.
3030Sstevel@tonic-gate  * If sendto == NULL, the current will be used.
3040Sstevel@tonic-gate  */
3050Sstevel@tonic-gate mblk_t *
3060Sstevel@tonic-gate sctp_make_mp(sctp_t *sctp, sctp_faddr_t *sendto, int trailer)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	mblk_t *mp;
3090Sstevel@tonic-gate 	size_t ipsctplen;
3100Sstevel@tonic-gate 	int isv4;
3110Sstevel@tonic-gate 	sctp_faddr_t *fp;
3123448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
3134818Skcpoon 	boolean_t src_changed = B_FALSE;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL || sendto != NULL);
3160Sstevel@tonic-gate 	if (sendto == NULL) {
3170Sstevel@tonic-gate 		fp = sctp->sctp_current;
3180Sstevel@tonic-gate 	} else {
3190Sstevel@tonic-gate 		fp = sendto;
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 	isv4 = fp->isv4;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/* Try to look for another IRE again. */
3244818Skcpoon 	if (fp->ire == NULL) {
3251735Skcpoon 		sctp_get_ire(sctp, fp);
3264818Skcpoon 		/*
3274818Skcpoon 		 * Although we still may not get an IRE, the source address
3284818Skcpoon 		 * may be changed in sctp_get_ire().  Set src_changed to
3294818Skcpoon 		 * true so that the source address is copied again.
3304818Skcpoon 		 */
3314818Skcpoon 		src_changed = B_TRUE;
3324818Skcpoon 	}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/* There is no suitable source address to use, return. */
3350Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH)
3360Sstevel@tonic-gate 		return (NULL);
3378903SVenu.Iyer@Sun.COM 	ASSERT(!SCTP_IS_ADDR_UNSPEC(fp->isv4, fp->saddr));
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (isv4) {
3400Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr_len;
3410Sstevel@tonic-gate 	} else {
3420Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr6_len;
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3453448Sdh155122 	mp = allocb_cred(ipsctplen + sctps->sctps_wroff_xtra + trailer,
3468778SErik.Nordmark@Sun.COM 	    CONN_CRED(sctp->sctp_connp), sctp->sctp_cpid);
3470Sstevel@tonic-gate 	if (mp == NULL) {
3481676Sjpk 		ip1dbg(("sctp_make_mp: error making mp..\n"));
3490Sstevel@tonic-gate 		return (NULL);
3500Sstevel@tonic-gate 	}
3513448Sdh155122 	mp->b_rptr += sctps->sctps_wroff_xtra;
3520Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + ipsctplen;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_wptr));
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if (isv4) {
3570Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)mp->b_rptr;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
3604818Skcpoon 		if (fp != sctp->sctp_current || src_changed) {
3614818Skcpoon 			/* Fix the source and destination addresses. */
3620Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
3634818Skcpoon 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr, iph->ipha_src);
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		/* set or clear the don't fragment bit */
3660Sstevel@tonic-gate 		if (fp->df) {
3670Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
3680Sstevel@tonic-gate 		} else {
3690Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 	} else {
3720Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
3734818Skcpoon 		if (fp != sctp->sctp_current || src_changed) {
3744818Skcpoon 			/* Fix the source and destination addresses. */
3750Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
3764818Skcpoon 			((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp != NULL);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*
3820Sstevel@tonic-gate 	 * IP will not free this IRE if it is condemned.  SCTP needs to
3830Sstevel@tonic-gate 	 * free it.
3840Sstevel@tonic-gate 	 */
3850Sstevel@tonic-gate 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
3860Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
3870Sstevel@tonic-gate 		fp->ire = NULL;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	/* Stash the conn and ire ptr info. for IP */
3900Sstevel@tonic-gate 	SCTP_STASH_IPINFO(mp, fp->ire);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (mp);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * Notify upper layers about preferred write offset, write size.
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate void
3990Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	int hdrlen;
4028348SEric.Yu@Sun.COM 	struct sock_proto_props sopp;
4038348SEric.Yu@Sun.COM 
4043448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (sctp->sctp_current->isv4) {
4070Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
4080Sstevel@tonic-gate 	} else {
4090Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss);
4148348SEric.Yu@Sun.COM 	bzero(&sopp, sizeof (sopp));
4158348SEric.Yu@Sun.COM 	sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF;
4168348SEric.Yu@Sun.COM 	sopp.sopp_wroff = sctps->sctps_wroff_xtra + hdrlen +
4178348SEric.Yu@Sun.COM 	    sizeof (sctp_data_hdr_t);
4188348SEric.Yu@Sun.COM 	sopp.sopp_maxblk = sctp->sctp_mss - sizeof (sctp_data_hdr_t);
4198348SEric.Yu@Sun.COM 	sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate void
4230Sstevel@tonic-gate sctp_set_iplen(sctp_t *sctp, mblk_t *mp)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	uint16_t	sum = 0;
4260Sstevel@tonic-gate 	ipha_t		*iph;
4270Sstevel@tonic-gate 	ip6_t		*ip6h;
4280Sstevel@tonic-gate 	mblk_t		*pmp = mp;
4290Sstevel@tonic-gate 	boolean_t	isv4;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
4320Sstevel@tonic-gate 	for (; pmp; pmp = pmp->b_cont)
4330Sstevel@tonic-gate 		sum += pmp->b_wptr - pmp->b_rptr;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (isv4) {
4360Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
4370Sstevel@tonic-gate 		iph->ipha_length = htons(sum);
4380Sstevel@tonic-gate 	} else {
4390Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
440679Sseb 		/*
441679Sseb 		 * If an ip6i_t is present, the real IPv6 header
442679Sseb 		 * immediately follows.
443679Sseb 		 */
444679Sseb 		if (ip6h->ip6_nxt == IPPROTO_RAW)
445679Sseb 			ip6h = (ip6_t *)&ip6h[1];
4460Sstevel@tonic-gate 		ip6h->ip6_plen = htons(sum - ((char *)&sctp->sctp_ip6h[1] -
4470Sstevel@tonic-gate 		    sctp->sctp_iphc6));
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate int
4520Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	int na1 = 0;
4550Sstevel@tonic-gate 	int overlap = 0;
4560Sstevel@tonic-gate 	int equal = 1;
4570Sstevel@tonic-gate 	int onematch;
4580Sstevel@tonic-gate 	sctp_faddr_t *fp1, *fp2;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	for (fp1 = a1; fp1; fp1 = fp1->next) {
4610Sstevel@tonic-gate 		onematch = 0;
4620Sstevel@tonic-gate 		for (fp2 = a2; fp2; fp2 = fp2->next) {
4630Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) {
4640Sstevel@tonic-gate 				overlap++;
4650Sstevel@tonic-gate 				onematch = 1;
4660Sstevel@tonic-gate 				break;
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 			if (!onematch) {
4690Sstevel@tonic-gate 				equal = 0;
4700Sstevel@tonic-gate 			}
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 		na1++;
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (equal) {
4760Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	if (overlap == na1) {
4790Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	if (overlap) {
4820Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /*
4889710SKen.Powell@Sun.COM  * Returns 0 on success, ENOMEM on memory allocation failure, EHOSTUNREACH
4899710SKen.Powell@Sun.COM  * if the connection credentials fail remote host accreditation or
4909710SKen.Powell@Sun.COM  * if the new destination does not support the previously established
4919710SKen.Powell@Sun.COM  * connection security label. If sleep is true, this function should
4929710SKen.Powell@Sun.COM  * never fail for a memory allocation failure. The boolean parameter
4939710SKen.Powell@Sun.COM  * "first" decides whether the newly created faddr structure should be
4941735Skcpoon  * added at the beginning of the list or at the end.
4951735Skcpoon  *
4961735Skcpoon  * Note: caller must hold conn fanout lock.
4970Sstevel@tonic-gate  */
4981735Skcpoon int
4991735Skcpoon sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first)
5000Sstevel@tonic-gate {
5011735Skcpoon 	sctp_faddr_t	*faddr;
5021735Skcpoon 	mblk_t		*timer_mp;
5039710SKen.Powell@Sun.COM 	int		err;
5040Sstevel@tonic-gate 
5051676Sjpk 	if (is_system_labeled()) {
5069710SKen.Powell@Sun.COM 		cred_t *effective_cred;
5070Sstevel@tonic-gate 
5089710SKen.Powell@Sun.COM 		/*
5099710SKen.Powell@Sun.COM 		 * Verify the destination is allowed to receive packets
5109710SKen.Powell@Sun.COM 		 * at the security label of the connection we are initiating.
5119710SKen.Powell@Sun.COM 		 *
5129710SKen.Powell@Sun.COM 		 * tsol_check_dest() will create a new effective cred for
5139710SKen.Powell@Sun.COM 		 * this connection with a modified label or label flags only
5149710SKen.Powell@Sun.COM 		 * if there are changes from the original cred.
5159710SKen.Powell@Sun.COM 		 *
5169710SKen.Powell@Sun.COM 		 * conn_effective_cred may be non-NULL if a previous
5179710SKen.Powell@Sun.COM 		 * faddr was already added or if this is a server
5189710SKen.Powell@Sun.COM 		 * accepting a connection on a multi-label port.
5199710SKen.Powell@Sun.COM 		 *
5209710SKen.Powell@Sun.COM 		 * Accept whatever label we get if this is the first
5219710SKen.Powell@Sun.COM 		 * destination address for this connection. The security
5229710SKen.Powell@Sun.COM 		 * label and label flags must match any previuous settings
5239710SKen.Powell@Sun.COM 		 * for all subsequent destination addresses.
5249710SKen.Powell@Sun.COM 		 */
5259710SKen.Powell@Sun.COM 		if (IN6_IS_ADDR_V4MAPPED(addr)) {
5269710SKen.Powell@Sun.COM 			uint32_t dst;
5279710SKen.Powell@Sun.COM 			IN6_V4MAPPED_TO_IPADDR(addr, dst);
5289710SKen.Powell@Sun.COM 			err = tsol_check_dest(CONN_CRED(sctp->sctp_connp),
5299710SKen.Powell@Sun.COM 			    &dst, IPV4_VERSION, sctp->sctp_mac_exempt,
5309710SKen.Powell@Sun.COM 			    &effective_cred);
5319710SKen.Powell@Sun.COM 		} else {
5329710SKen.Powell@Sun.COM 			err = tsol_check_dest(CONN_CRED(sctp->sctp_connp),
5339710SKen.Powell@Sun.COM 			    addr, IPV6_VERSION, sctp->sctp_mac_exempt,
5349710SKen.Powell@Sun.COM 			    &effective_cred);
5351676Sjpk 		}
5369710SKen.Powell@Sun.COM 		if (err != 0)
5379710SKen.Powell@Sun.COM 			return (err);
5389710SKen.Powell@Sun.COM 		if (sctp->sctp_faddrs == NULL &&
5399710SKen.Powell@Sun.COM 		    sctp->sctp_connp->conn_effective_cred == NULL) {
5409710SKen.Powell@Sun.COM 			sctp->sctp_connp->conn_effective_cred = effective_cred;
5419710SKen.Powell@Sun.COM 		} else if (effective_cred != NULL) {
5429710SKen.Powell@Sun.COM 			crfree(effective_cred);
5439710SKen.Powell@Sun.COM 			return (EHOSTUNREACH);
5449710SKen.Powell@Sun.COM 		}
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5471676Sjpk 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL)
5481676Sjpk 		return (ENOMEM);
5494691Skcpoon 	timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep);
5501735Skcpoon 	if (timer_mp == NULL) {
5511735Skcpoon 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
5521735Skcpoon 		return (ENOMEM);
5531735Skcpoon 	}
5541735Skcpoon 	((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr;
5551676Sjpk 
5561735Skcpoon 	sctp_init_faddr(sctp, faddr, addr, timer_mp);
5574818Skcpoon 
5584818Skcpoon 	/* Check for subnet broadcast. */
5594818Skcpoon 	if (faddr->ire != NULL && faddr->ire->ire_type & IRE_BROADCAST) {
5604818Skcpoon 		IRE_REFRELE_NOTR(faddr->ire);
5614818Skcpoon 		sctp_timer_free(timer_mp);
5625240Snordmark 		faddr->timer_mp = NULL;
5634818Skcpoon 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
5644818Skcpoon 		return (EADDRNOTAVAIL);
5654818Skcpoon 	}
5660Sstevel@tonic-gate 	ASSERT(faddr->next == NULL);
5670Sstevel@tonic-gate 
5681676Sjpk 	if (sctp->sctp_faddrs == NULL) {
5691676Sjpk 		ASSERT(sctp->sctp_lastfaddr == NULL);
5701676Sjpk 		/* only element on list; first and last are same */
5711676Sjpk 		sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr;
5721676Sjpk 	} else if (first) {
5731676Sjpk 		ASSERT(sctp->sctp_lastfaddr != NULL);
5741676Sjpk 		faddr->next = sctp->sctp_faddrs;
5751676Sjpk 		sctp->sctp_faddrs = faddr;
5760Sstevel@tonic-gate 	} else {
5771676Sjpk 		sctp->sctp_lastfaddr->next = faddr;
5781676Sjpk 		sctp->sctp_lastfaddr = faddr;
5790Sstevel@tonic-gate 	}
580852Svi117747 	sctp->sctp_nfaddrs++;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	return (0);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate sctp_faddr_t *
5860Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	sctp_faddr_t *fp;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5910Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr))
5920Sstevel@tonic-gate 			break;
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	return (fp);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate sctp_faddr_t *
5990Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	for (; fp; fp = fp->next) {
6020Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) {
6030Sstevel@tonic-gate 			break;
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	return (fp);
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6101735Skcpoon /*
6111735Skcpoon  * To change the currently used peer address to the specified one.
6121735Skcpoon  */
6130Sstevel@tonic-gate void
6141735Skcpoon sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp)
6150Sstevel@tonic-gate {
6161735Skcpoon 	/* Now setup the composite header. */
6170Sstevel@tonic-gate 	if (fp->isv4) {
6180Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
6190Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
6200Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src);
6210Sstevel@tonic-gate 		/* update don't fragment bit */
6220Sstevel@tonic-gate 		if (fp->df) {
6230Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
6240Sstevel@tonic-gate 			    htons(IPH_DF);
6250Sstevel@tonic-gate 		} else {
6260Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 	} else {
6290Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = fp->faddr;
6300Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = fp->saddr;
6310Sstevel@tonic-gate 	}
6321735Skcpoon 
6331735Skcpoon 	sctp->sctp_current = fp;
6341735Skcpoon 	sctp->sctp_mss = fp->sfa_pmss;
6351735Skcpoon 
6361735Skcpoon 	/* Update the uppper layer for the change. */
6371735Skcpoon 	if (!SCTP_IS_DETACHED(sctp))
6381735Skcpoon 		sctp_set_ulp_prop(sctp);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate void
6420Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp)
6430Sstevel@tonic-gate {
6440Sstevel@tonic-gate 	sctp_faddr_t *fp;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
6471735Skcpoon 		sctp_get_ire(sctp, fp);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate void
6520Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	int64_t now = lbolt64;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	fp->strikes = 0;
6570Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
6580Sstevel@tonic-gate 	fp->lastactive = now;
6590Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
6600Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
6610Sstevel@tonic-gate 	if (fp->state != SCTP_FADDRS_ALIVE) {
6620Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_ALIVE;
6630Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0);
6644818Skcpoon 		/* Should have a full IRE now */
6654818Skcpoon 		sctp_get_ire(sctp, fp);
6660Sstevel@tonic-gate 
6671735Skcpoon 		/*
6681735Skcpoon 		 * If this is the primary, switch back to it now.  And
6691735Skcpoon 		 * we probably want to reset the source addr used to reach
6701735Skcpoon 		 * it.
6711735Skcpoon 		 */
6720Sstevel@tonic-gate 		if (fp == sctp->sctp_primary) {
6734818Skcpoon 			ASSERT(fp->state != SCTP_FADDRS_UNREACH);
6741735Skcpoon 			sctp_set_faddr_current(sctp, fp);
6751735Skcpoon 			return;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate int
6810Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	sctp_faddr_t *fp;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
6860Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) {
6870Sstevel@tonic-gate 			return (1);
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	return (0);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /*
6950Sstevel@tonic-gate  * Returns 0 if there is at leave one other active faddr, -1 if there
6960Sstevel@tonic-gate  * are none. If there are none left, faddr_dead() will start killing the
6970Sstevel@tonic-gate  * association.
6980Sstevel@tonic-gate  * If the downed faddr was the current faddr, a new current faddr
6990Sstevel@tonic-gate  * will be chosen.
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate int
7020Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate 	sctp_faddr_t *ofp;
7053448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_ALIVE) {
7080Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 	fp->state = newstate;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
7130Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), newstate));
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
7160Sstevel@tonic-gate 		/* Current faddr down; need to switch it */
7170Sstevel@tonic-gate 		sctp->sctp_current = NULL;
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/* Find next alive faddr */
7210Sstevel@tonic-gate 	ofp = fp;
7221735Skcpoon 	for (fp = fp->next; fp != NULL; fp = fp->next) {
7230Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE) {
7240Sstevel@tonic-gate 			break;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	if (fp == NULL) {
7290Sstevel@tonic-gate 		/* Continue from beginning of list */
7300Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) {
7310Sstevel@tonic-gate 			if (fp->state == SCTP_FADDRS_ALIVE) {
7320Sstevel@tonic-gate 				break;
7330Sstevel@tonic-gate 			}
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7371735Skcpoon 	/*
7381735Skcpoon 	 * Find a new fp, so if the current faddr is dead, use the new fp
7391735Skcpoon 	 * as the current one.
7401735Skcpoon 	 */
7410Sstevel@tonic-gate 	if (fp != ofp) {
7420Sstevel@tonic-gate 		if (sctp->sctp_current == NULL) {
7430Sstevel@tonic-gate 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
7440Sstevel@tonic-gate 			    SCTP_PRINTADDR(fp->faddr)));
7451735Skcpoon 			/*
7461735Skcpoon 			 * Note that we don't need to reset the source addr
7471735Skcpoon 			 * of the new fp.
7481735Skcpoon 			 */
7491735Skcpoon 			sctp_set_faddr_current(sctp, fp);
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 		return (0);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/* All faddrs are down; kill the association */
7560Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
7573448Sdh155122 	BUMP_MIB(&sctps->sctps_mib, sctpAborted);
7580Sstevel@tonic-gate 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
7590Sstevel@tonic-gate 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
7600Sstevel@tonic-gate 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
7610Sstevel@tonic-gate 	    sctp->sctp_client_errno : ETIMEDOUT);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	return (-1);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate sctp_faddr_t *
7670Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	sctp_faddr_t *nfp = NULL;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	if (ofp == NULL) {
7720Sstevel@tonic-gate 		ofp = sctp->sctp_current;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/* Find the next live one */
7760Sstevel@tonic-gate 	for (nfp = ofp->next; nfp != NULL; nfp = nfp->next) {
7770Sstevel@tonic-gate 		if (nfp->state == SCTP_FADDRS_ALIVE) {
7780Sstevel@tonic-gate 			break;
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	if (nfp == NULL) {
7830Sstevel@tonic-gate 		/* Continue from beginning of list */
7840Sstevel@tonic-gate 		for (nfp = sctp->sctp_faddrs; nfp != ofp; nfp = nfp->next) {
7850Sstevel@tonic-gate 			if (nfp->state == SCTP_FADDRS_ALIVE) {
7860Sstevel@tonic-gate 				break;
7870Sstevel@tonic-gate 			}
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/*
7920Sstevel@tonic-gate 	 * nfp could only be NULL if all faddrs are down, and when
7930Sstevel@tonic-gate 	 * this happens, faddr_dead() should have killed the
7940Sstevel@tonic-gate 	 * association. Hence this assertion...
7950Sstevel@tonic-gate 	 */
7960Sstevel@tonic-gate 	ASSERT(nfp != NULL);
7970Sstevel@tonic-gate 	return (nfp);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate void
8010Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	sctp_faddr_t *fpp;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (!sctp->sctp_faddrs) {
8060Sstevel@tonic-gate 		return;
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (fp->timer_mp != NULL) {
8100Sstevel@tonic-gate 		sctp_timer_free(fp->timer_mp);
8110Sstevel@tonic-gate 		fp->timer_mp = NULL;
8120Sstevel@tonic-gate 		fp->timer_running = 0;
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 	if (fp->rc_timer_mp != NULL) {
8150Sstevel@tonic-gate 		sctp_timer_free(fp->rc_timer_mp);
8160Sstevel@tonic-gate 		fp->rc_timer_mp = NULL;
8170Sstevel@tonic-gate 		fp->rc_timer_running = 0;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	if (fp->ire != NULL) {
8200Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
8210Sstevel@tonic-gate 		fp->ire = NULL;
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8250Sstevel@tonic-gate 		goto gotit;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next)
8290Sstevel@tonic-gate 		;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate gotit:
8320Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
8330Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8340Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8350Sstevel@tonic-gate 		sctp->sctp_faddrs = fp->next;
8360Sstevel@tonic-gate 	} else {
8370Sstevel@tonic-gate 		fpp->next = fp->next;
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8400Sstevel@tonic-gate 	/* XXX faddr2ire? */
8410Sstevel@tonic-gate 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
842852Svi117747 	sctp->sctp_nfaddrs--;
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate void
8460Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpn;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL) {
8510Sstevel@tonic-gate 		ASSERT(sctp->sctp_lastfaddr == NULL);
8520Sstevel@tonic-gate 		return;
8530Sstevel@tonic-gate 	}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr != NULL);
8560Sstevel@tonic-gate 	sctp->sctp_lastfaddr = NULL;
8570Sstevel@tonic-gate 	sctp->sctp_current = NULL;
8580Sstevel@tonic-gate 	sctp->sctp_primary = NULL;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8630Sstevel@tonic-gate 		/* in conn fanout; need to hold lock */
8640Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
8680Sstevel@tonic-gate 		fpn = fp->next;
8690Sstevel@tonic-gate 		if (fp->ire != NULL)
8700Sstevel@tonic-gate 			IRE_REFRELE_NOTR(fp->ire);
8710Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
872852Svi117747 		sctp->sctp_nfaddrs--;
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	sctp->sctp_faddrs = NULL;
876852Svi117747 	ASSERT(sctp->sctp_nfaddrs == 0);
8770Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8780Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate void
8840Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	sctp_zap_faddrs(sctp, 0);
8870Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate  * Initialize the IPv4 header. Loses any record of any IP options.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate int
8940Sstevel@tonic-gate sctp_header_init_ipv4(sctp_t *sctp, int sleep)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
8973448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	/*
9000Sstevel@tonic-gate 	 * This is a simple initialization. If there's
9010Sstevel@tonic-gate 	 * already a template, it should never be too small,
9020Sstevel@tonic-gate 	 * so reuse it.  Otherwise, allocate space for the new one.
9030Sstevel@tonic-gate 	 */
9040Sstevel@tonic-gate 	if (sctp->sctp_iphc != NULL) {
9050Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc_len >= SCTP_MAX_COMBINED_HEADER_LENGTH);
9060Sstevel@tonic-gate 		bzero(sctp->sctp_iphc, sctp->sctp_iphc_len);
9070Sstevel@tonic-gate 	} else {
9080Sstevel@tonic-gate 		sctp->sctp_iphc_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
9090Sstevel@tonic-gate 		sctp->sctp_iphc = kmem_zalloc(sctp->sctp_iphc_len, sleep);
9100Sstevel@tonic-gate 		if (sctp->sctp_iphc == NULL) {
9110Sstevel@tonic-gate 			sctp->sctp_iphc_len = 0;
9120Sstevel@tonic-gate 			return (ENOMEM);
9130Sstevel@tonic-gate 		}
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	sctp->sctp_hdr_len = sizeof (ipha_t) + sizeof (sctp_hdr_t);
9190Sstevel@tonic-gate 	sctp->sctp_ip_hdr_len = sizeof (ipha_t);
9200Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_length = htons(sizeof (ipha_t) +
9210Sstevel@tonic-gate 	    sizeof (sctp_hdr_t));
9224691Skcpoon 	sctp->sctp_ipha->ipha_version_and_hdr_length =
9234691Skcpoon 	    (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/*
9260Sstevel@tonic-gate 	 * These two fields should be zero, and are already set above.
9270Sstevel@tonic-gate 	 *
9280Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_ident,
9290Sstevel@tonic-gate 	 * sctp->sctp_ipha->ipha_fragment_offset_and_flags.
9300Sstevel@tonic-gate 	 */
9310Sstevel@tonic-gate 
9323448Sdh155122 	sctp->sctp_ipha->ipha_ttl = sctps->sctps_ipv4_ttl;
9330Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_protocol = IPPROTO_SCTP;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc + sizeof (ipha_t));
9360Sstevel@tonic-gate 	sctp->sctp_sctph = sctph;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	return (0);
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate /*
9420Sstevel@tonic-gate  * Update sctp_sticky_hdrs based on sctp_sticky_ipp.
9430Sstevel@tonic-gate  * The headers include ip6i_t (if needed), ip6_t, any sticky extension
9440Sstevel@tonic-gate  * headers, and the maximum size sctp header (to avoid reallocation
9450Sstevel@tonic-gate  * on the fly for additional sctp options).
9460Sstevel@tonic-gate  * Returns failure if can't allocate memory.
9470Sstevel@tonic-gate  */
9480Sstevel@tonic-gate int
9490Sstevel@tonic-gate sctp_build_hdrs(sctp_t *sctp)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate 	char		*hdrs;
9520Sstevel@tonic-gate 	uint_t		hdrs_len;
9530Sstevel@tonic-gate 	ip6i_t		*ip6i;
9540Sstevel@tonic-gate 	char		buf[SCTP_MAX_HDR_LENGTH];
9550Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
9560Sstevel@tonic-gate 	in6_addr_t	src;
9570Sstevel@tonic-gate 	in6_addr_t	dst;
9583448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
9591676Sjpk 
9600Sstevel@tonic-gate 	/*
9610Sstevel@tonic-gate 	 * save the existing sctp header and source/dest IP addresses
9620Sstevel@tonic-gate 	 */
9630Sstevel@tonic-gate 	bcopy(sctp->sctp_sctph6, buf, sizeof (sctp_hdr_t));
9640Sstevel@tonic-gate 	src = sctp->sctp_ip6h->ip6_src;
9650Sstevel@tonic-gate 	dst = sctp->sctp_ip6h->ip6_dst;
9660Sstevel@tonic-gate 	hdrs_len = ip_total_hdrs_len_v6(ipp) + SCTP_MAX_HDR_LENGTH;
9670Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
9680Sstevel@tonic-gate 	if (hdrs_len > sctp->sctp_iphc6_len) {
9690Sstevel@tonic-gate 		/* Need to reallocate */
9700Sstevel@tonic-gate 		hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP);
9710Sstevel@tonic-gate 		if (hdrs == NULL)
9720Sstevel@tonic-gate 			return (ENOMEM);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		if (sctp->sctp_iphc6_len != 0)
9750Sstevel@tonic-gate 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
9760Sstevel@tonic-gate 		sctp->sctp_iphc6 = hdrs;
9770Sstevel@tonic-gate 		sctp->sctp_iphc6_len = hdrs_len;
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 	ip_build_hdrs_v6((uchar_t *)sctp->sctp_iphc6,
9800Sstevel@tonic-gate 	    hdrs_len - SCTP_MAX_HDR_LENGTH, ipp, IPPROTO_SCTP);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/* Set header fields not in ipp */
9830Sstevel@tonic-gate 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
9840Sstevel@tonic-gate 		ip6i = (ip6i_t *)sctp->sctp_iphc6;
9850Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)&ip6i[1];
9860Sstevel@tonic-gate 	} else {
9870Sstevel@tonic-gate 		sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 	/*
9900Sstevel@tonic-gate 	 * sctp->sctp_ip_hdr_len will include ip6i_t if there is one.
9910Sstevel@tonic-gate 	 */
9920Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = hdrs_len - SCTP_MAX_HDR_LENGTH;
9930Sstevel@tonic-gate 	sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
9940Sstevel@tonic-gate 	    sctp->sctp_ip_hdr6_len);
9950Sstevel@tonic-gate 	sctp->sctp_hdr6_len = sctp->sctp_ip_hdr6_len + sizeof (sctp_hdr_t);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	bcopy(buf, sctp->sctp_sctph6, sizeof (sctp_hdr_t));
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_src = src;
10000Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_dst = dst;
10010Sstevel@tonic-gate 	/*
1002679Sseb 	 * If the hoplimit was not set by ip_build_hdrs_v6(), we need to
1003679Sseb 	 * set it to the default value for SCTP.
10040Sstevel@tonic-gate 	 */
1005679Sseb 	if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS))
10063448Sdh155122 		sctp->sctp_ip6h->ip6_hops = sctps->sctps_ipv6_hoplimit;
10070Sstevel@tonic-gate 	/*
10080Sstevel@tonic-gate 	 * If we're setting extension headers after a connection
10090Sstevel@tonic-gate 	 * has been established, and if we have a routing header
10100Sstevel@tonic-gate 	 * among the extension headers, call ip_massage_options_v6 to
10110Sstevel@tonic-gate 	 * manipulate the routing header/ip6_dst set the checksum
10120Sstevel@tonic-gate 	 * difference in the sctp header template.
10130Sstevel@tonic-gate 	 * (This happens in sctp_connect_ipv6 if the routing header
10140Sstevel@tonic-gate 	 * is set prior to the connect.)
10150Sstevel@tonic-gate 	 */
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
10180Sstevel@tonic-gate 	    (sctp->sctp_sticky_ipp.ipp_fields & IPPF_RTHDR)) {
10190Sstevel@tonic-gate 		ip6_rthdr_t *rth;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
10220Sstevel@tonic-gate 		    (uint8_t *)sctp->sctp_sctph6);
10233448Sdh155122 		if (rth != NULL) {
10243448Sdh155122 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth,
10253448Sdh155122 			    sctps->sctps_netstack);
10263448Sdh155122 		}
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 	return (0);
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate /*
10320Sstevel@tonic-gate  * Initialize the IPv6 header. Loses any record of any IPv6 extension headers.
10330Sstevel@tonic-gate  */
10340Sstevel@tonic-gate int
10350Sstevel@tonic-gate sctp_header_init_ipv6(sctp_t *sctp, int sleep)
10360Sstevel@tonic-gate {
10370Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
10383448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	/*
10410Sstevel@tonic-gate 	 * This is a simple initialization. If there's
10420Sstevel@tonic-gate 	 * already a template, it should never be too small,
10430Sstevel@tonic-gate 	 * so reuse it. Otherwise, allocate space for the new one.
10440Sstevel@tonic-gate 	 * Ensure that there is enough space to "downgrade" the sctp_t
10450Sstevel@tonic-gate 	 * to an IPv4 sctp_t. This requires having space for a full load
10460Sstevel@tonic-gate 	 * of IPv4 options
10470Sstevel@tonic-gate 	 */
10480Sstevel@tonic-gate 	if (sctp->sctp_iphc6 != NULL) {
10490Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc6_len >=
10500Sstevel@tonic-gate 		    SCTP_MAX_COMBINED_HEADER_LENGTH);
10510Sstevel@tonic-gate 		bzero(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
10520Sstevel@tonic-gate 	} else {
10530Sstevel@tonic-gate 		sctp->sctp_iphc6_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
10540Sstevel@tonic-gate 		sctp->sctp_iphc6 = kmem_zalloc(sctp->sctp_iphc_len, sleep);
10550Sstevel@tonic-gate 		if (sctp->sctp_iphc6 == NULL) {
10560Sstevel@tonic-gate 			sctp->sctp_iphc6_len = 0;
10570Sstevel@tonic-gate 			return (ENOMEM);
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 	sctp->sctp_hdr6_len = IPV6_HDR_LEN + sizeof (sctp_hdr_t);
10610Sstevel@tonic-gate 	sctp->sctp_ip_hdr6_len = IPV6_HDR_LEN;
10620Sstevel@tonic-gate 	sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	/* Initialize the header template */
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
10670Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_plen = ntohs(sizeof (sctp_hdr_t));
10680Sstevel@tonic-gate 	sctp->sctp_ip6h->ip6_nxt = IPPROTO_SCTP;
10693448Sdh155122 	sctp->sctp_ip6h->ip6_hops = sctps->sctps_ipv6_hoplimit;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc6 + IPV6_HDR_LEN);
10720Sstevel@tonic-gate 	sctp->sctp_sctph6 = sctph;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	return (0);
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate 
10771676Sjpk static int
10781676Sjpk sctp_v4_label(sctp_t *sctp)
10791676Sjpk {
10801676Sjpk 	uchar_t optbuf[IP_MAX_OPT_LENGTH];
10811676Sjpk 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
10821676Sjpk 	int added;
10831676Sjpk 
10841676Sjpk 	if (tsol_compute_label(cr, sctp->sctp_ipha->ipha_dst, optbuf,
10853448Sdh155122 	    sctp->sctp_sctps->sctps_netstack->netstack_ip) != 0)
10861676Sjpk 		return (EACCES);
10871676Sjpk 
10881676Sjpk 	added = tsol_remove_secopt(sctp->sctp_ipha, sctp->sctp_hdr_len);
10891676Sjpk 	if (added == -1)
10901676Sjpk 		return (EACCES);
10911676Sjpk 	sctp->sctp_hdr_len += added;
10921676Sjpk 	sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph + added);
10931676Sjpk 	sctp->sctp_ip_hdr_len += added;
10941676Sjpk 	if ((sctp->sctp_v4label_len = optbuf[IPOPT_OLEN]) != 0) {
10951676Sjpk 		sctp->sctp_v4label_len = (sctp->sctp_v4label_len + 3) & ~3;
10961676Sjpk 		added = tsol_prepend_option(optbuf, sctp->sctp_ipha,
10971676Sjpk 		    sctp->sctp_hdr_len);
10981676Sjpk 		if (added == -1)
10991676Sjpk 			return (EACCES);
11001676Sjpk 		sctp->sctp_hdr_len += added;
11011676Sjpk 		sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph +
11021676Sjpk 		    added);
11031676Sjpk 		sctp->sctp_ip_hdr_len += added;
11041676Sjpk 	}
11051676Sjpk 	return (0);
11061676Sjpk }
11071676Sjpk 
11081676Sjpk static int
11091676Sjpk sctp_v6_label(sctp_t *sctp)
11101676Sjpk {
11111676Sjpk 	uchar_t optbuf[TSOL_MAX_IPV6_OPTION];
11121676Sjpk 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
11131676Sjpk 
11141676Sjpk 	if (tsol_compute_label_v6(cr, &sctp->sctp_ip6h->ip6_dst, optbuf,
11153448Sdh155122 	    sctp->sctp_sctps->sctps_netstack->netstack_ip) != 0)
11161676Sjpk 		return (EACCES);
11171676Sjpk 	if (tsol_update_sticky(&sctp->sctp_sticky_ipp, &sctp->sctp_v6label_len,
11181676Sjpk 	    optbuf) != 0)
11191676Sjpk 		return (EACCES);
11201676Sjpk 	if (sctp_build_hdrs(sctp) != 0)
11211676Sjpk 		return (EACCES);
11221676Sjpk 	return (0);
11231676Sjpk }
11241676Sjpk 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * XXX implement more sophisticated logic
11270Sstevel@tonic-gate  */
11281676Sjpk int
11291735Skcpoon sctp_set_hdraddrs(sctp_t *sctp)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	sctp_faddr_t *fp;
11320Sstevel@tonic-gate 	int gotv4 = 0;
11330Sstevel@tonic-gate 	int gotv6 = 0;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs != NULL);
11360Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	/* Set up using the primary first */
11390Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) {
11400Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->faddr,
11410Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
11420Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
11430Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->saddr,
11440Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_src);
11451676Sjpk 		if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
11461676Sjpk 			gotv4 = 1;
11471676Sjpk 			if (sctp->sctp_ipversion == IPV4_VERSION) {
11481676Sjpk 				goto copyports;
11491676Sjpk 			}
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 	} else {
11520Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = sctp->sctp_primary->faddr;
11530Sstevel@tonic-gate 		/* saddr may be unspec; make_mp() will handle this */
11540Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = sctp->sctp_primary->saddr;
11551676Sjpk 		if (!is_system_labeled() || sctp_v6_label(sctp) == 0)
11561676Sjpk 			gotv6 = 1;
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
11600Sstevel@tonic-gate 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
11610Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
11620Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_dst);
11630Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
11640Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
11650Sstevel@tonic-gate 			    sctp->sctp_ipha->ipha_src);
11661676Sjpk 			if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
11671676Sjpk 				gotv4 = 1;
11681676Sjpk 				if (sctp->sctp_ipversion == IPV4_VERSION ||
11691676Sjpk 				    gotv6) {
11701676Sjpk 					break;
11711676Sjpk 				}
11720Sstevel@tonic-gate 			}
11732283Skp158701 		} else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
11740Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_dst = fp->faddr;
11750Sstevel@tonic-gate 			/* copy in the faddr_t's saddr */
11760Sstevel@tonic-gate 			sctp->sctp_ip6h->ip6_src = fp->saddr;
11771676Sjpk 			if (!is_system_labeled() || sctp_v6_label(sctp) == 0) {
11781676Sjpk 				gotv6 = 1;
11791676Sjpk 				if (gotv4)
11801676Sjpk 					break;
11810Sstevel@tonic-gate 			}
11820Sstevel@tonic-gate 		}
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate copyports:
11861676Sjpk 	if (!gotv4 && !gotv6)
11871676Sjpk 		return (EACCES);
11881676Sjpk 
11890Sstevel@tonic-gate 	/* copy in the ports for good measure */
11900Sstevel@tonic-gate 	sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
11910Sstevel@tonic-gate 	sctp->sctp_sctph->sh_dport = sctp->sctp_fport;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_sport = sctp->sctp_lport;
11940Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_dport = sctp->sctp_fport;
11951676Sjpk 	return (0);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate 
11988549SGeorge.Shepherd@Sun.COM /*
11998549SGeorge.Shepherd@Sun.COM  * got_errchunk is set B_TRUE only if called from validate_init_params(), when
12008549SGeorge.Shepherd@Sun.COM  * an ERROR chunk is already prepended the size of which needs updating for
12018549SGeorge.Shepherd@Sun.COM  * additional unrecognized parameters. Other callers either prepend the ERROR
12028549SGeorge.Shepherd@Sun.COM  * chunk with the correct size after calling this function, or they are calling
12038549SGeorge.Shepherd@Sun.COM  * to add an invalid parameter to an INIT_ACK chunk, in that case no ERROR chunk
12048549SGeorge.Shepherd@Sun.COM  * exists, the CAUSE blocks go into the INIT_ACK directly.
12058549SGeorge.Shepherd@Sun.COM  *
12068549SGeorge.Shepherd@Sun.COM  * *errmp will be non-NULL both when adding an additional CAUSE block to an
12078549SGeorge.Shepherd@Sun.COM  * existing prepended COOKIE ERROR chunk (processing params of an INIT_ACK),
12088549SGeorge.Shepherd@Sun.COM  * and when adding unrecognized parameters after the first, to an INIT_ACK
12098549SGeorge.Shepherd@Sun.COM  * (processing params of an INIT chunk).
12108549SGeorge.Shepherd@Sun.COM  */
12110Sstevel@tonic-gate void
12128549SGeorge.Shepherd@Sun.COM sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp,
12138549SGeorge.Shepherd@Sun.COM     boolean_t got_errchunk)
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate 	mblk_t *mp;
12160Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
12170Sstevel@tonic-gate 	size_t len;
12180Sstevel@tonic-gate 	int pad;
12198153SGeorge.Shepherd@Sun.COM 	sctp_chunk_hdr_t *ecp;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	len = sizeof (*ph) + ntohs(uph->sph_len);
12228153SGeorge.Shepherd@Sun.COM 	if ((pad = len % SCTP_ALIGN) != 0) {
12238153SGeorge.Shepherd@Sun.COM 		pad = SCTP_ALIGN - pad;
12240Sstevel@tonic-gate 		len += pad;
12250Sstevel@tonic-gate 	}
12260Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
12270Sstevel@tonic-gate 	if (mp == NULL) {
12280Sstevel@tonic-gate 		return;
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
12320Sstevel@tonic-gate 	ph->sph_type = htons(PARM_UNRECOGNIZED);
12330Sstevel@tonic-gate 	ph->sph_len = htons(len - pad);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	/* copy in the unrecognized parameter */
12360Sstevel@tonic-gate 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
12370Sstevel@tonic-gate 
12388153SGeorge.Shepherd@Sun.COM 	if (pad != 0)
12398153SGeorge.Shepherd@Sun.COM 		bzero((mp->b_rptr + len - pad), pad);
12408153SGeorge.Shepherd@Sun.COM 
12410Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
12420Sstevel@tonic-gate 	if (*errmp != NULL) {
12438153SGeorge.Shepherd@Sun.COM 		/*
12448549SGeorge.Shepherd@Sun.COM 		 * Update total length if an ERROR chunk, then link
12458549SGeorge.Shepherd@Sun.COM 		 * this CAUSE block to the possible chain of CAUSE
12468549SGeorge.Shepherd@Sun.COM 		 * blocks attached to the ERROR chunk or INIT_ACK
12478549SGeorge.Shepherd@Sun.COM 		 * being created.
12488153SGeorge.Shepherd@Sun.COM 		 */
12498549SGeorge.Shepherd@Sun.COM 		if (got_errchunk) {
12508549SGeorge.Shepherd@Sun.COM 			/* ERROR chunk already prepended */
12518549SGeorge.Shepherd@Sun.COM 			ecp = (sctp_chunk_hdr_t *)((*errmp)->b_rptr);
12528549SGeorge.Shepherd@Sun.COM 			ecp->sch_len = htons(ntohs(ecp->sch_len) + len);
12538549SGeorge.Shepherd@Sun.COM 		}
12540Sstevel@tonic-gate 		linkb(*errmp, mp);
12550Sstevel@tonic-gate 	} else {
12560Sstevel@tonic-gate 		*errmp = mp;
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate /*
12610Sstevel@tonic-gate  * o Bounds checking
12620Sstevel@tonic-gate  * o Updates remaining
12630Sstevel@tonic-gate  * o Checks alignment
12640Sstevel@tonic-gate  */
12650Sstevel@tonic-gate sctp_parm_hdr_t *
12660Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
12670Sstevel@tonic-gate {
12680Sstevel@tonic-gate 	int pad;
12690Sstevel@tonic-gate 	uint16_t len;
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	len = ntohs(current->sph_len);
12720Sstevel@tonic-gate 	*remaining -= len;
12730Sstevel@tonic-gate 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
12740Sstevel@tonic-gate 		return (NULL);
12750Sstevel@tonic-gate 	}
12760Sstevel@tonic-gate 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
12770Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
12780Sstevel@tonic-gate 		*remaining -= pad;
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
12810Sstevel@tonic-gate 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
12820Sstevel@tonic-gate 	return (current);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate /*
12860Sstevel@tonic-gate  * Sets the address parameters given in the INIT chunk into sctp's
12870Sstevel@tonic-gate  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
12880Sstevel@tonic-gate  * no address parameters in the INIT chunk, a single faddr is created
12890Sstevel@tonic-gate  * from the ip hdr at the beginning of pkt.
12900Sstevel@tonic-gate  * If there already are existing addresses hanging from sctp, merge
12910Sstevel@tonic-gate  * them in, if the old info contains addresses which are not present
12920Sstevel@tonic-gate  * in this new info, get rid of them, and clean the pointers if there's
12930Sstevel@tonic-gate  * messages which have this as their target address.
12940Sstevel@tonic-gate  *
1295432Svi117747  * We also re-adjust the source address list here since the list may
1296432Svi117747  * contain more than what is actually part of the association. If
1297432Svi117747  * we get here from sctp_send_cookie_echo(), we are on the active
1298432Svi117747  * side and psctp will be NULL and ich will be the INIT-ACK chunk.
1299432Svi117747  * If we get here from sctp_accept_comm(), ich will be the INIT chunk
1300432Svi117747  * and psctp will the listening endpoint.
1301432Svi117747  *
1302432Svi117747  * INIT processing: When processing the INIT we inherit the src address
1303432Svi117747  * list from the listener. For a loopback or linklocal association, we
1304432Svi117747  * delete the list and just take the address from the IP header (since
1305432Svi117747  * that's how we created the INIT-ACK). Additionally, for loopback we
1306432Svi117747  * ignore the address params in the INIT. For determining which address
1307432Svi117747  * types were sent in the INIT-ACK we follow the same logic as in
1308432Svi117747  * creating the INIT-ACK. We delete addresses of the type that are not
1309432Svi117747  * supported by the peer.
1310432Svi117747  *
1311432Svi117747  * INIT-ACK processing: When processing the INIT-ACK since we had not
1312432Svi117747  * included addr params for loopback or linklocal addresses when creating
1313432Svi117747  * the INIT, we just use the address from the IP header. Further, for
1314432Svi117747  * loopback we ignore the addr param list. We mark addresses of the
1315432Svi117747  * type not supported by the peer as unconfirmed.
1316432Svi117747  *
1317432Svi117747  * In case of INIT processing we look for supported address types in the
1318432Svi117747  * supported address param, if present. In both cases the address type in
1319432Svi117747  * the IP header is supported as well as types for addresses in the param
1320432Svi117747  * list, if any.
1321432Svi117747  *
1322432Svi117747  * Once we have the supported address types sctp_check_saddr() runs through
1323432Svi117747  * the source address list and deletes or marks as unconfirmed address of
1324432Svi117747  * types not supported by the peer.
1325432Svi117747  *
13260Sstevel@tonic-gate  * Returns 0 on success, sys errno on failure
13270Sstevel@tonic-gate  */
13280Sstevel@tonic-gate int
13290Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
13300Sstevel@tonic-gate     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
13330Sstevel@tonic-gate 	ipha_t			*iph;
13340Sstevel@tonic-gate 	ip6_t			*ip6h;
1335432Svi117747 	in6_addr_t		hdrsaddr[1];
1336432Svi117747 	in6_addr_t		hdrdaddr[1];
13370Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
13380Sstevel@tonic-gate 	ssize_t			remaining;
13390Sstevel@tonic-gate 	int			isv4;
13400Sstevel@tonic-gate 	int			err;
13410Sstevel@tonic-gate 	sctp_faddr_t		*fp;
1342432Svi117747 	int			supp_af = 0;
1343432Svi117747 	boolean_t		check_saddr = B_TRUE;
1344852Svi117747 	in6_addr_t		curaddr;
13453448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	if (sctp_options != NULL)
13480Sstevel@tonic-gate 		*sctp_options = 0;
13490Sstevel@tonic-gate 
1350432Svi117747 	/* extract the address from the IP header */
1351432Svi117747 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1352432Svi117747 	if (isv4) {
1353432Svi117747 		iph = (ipha_t *)pkt->b_rptr;
1354432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr);
1355432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr);
1356432Svi117747 		supp_af |= PARM_SUPP_V4;
1357432Svi117747 	} else {
1358432Svi117747 		ip6h = (ip6_t *)pkt->b_rptr;
1359432Svi117747 		hdrsaddr[0] = ip6h->ip6_src;
1360432Svi117747 		hdrdaddr[0] = ip6h->ip6_dst;
1361432Svi117747 		supp_af |= PARM_SUPP_V6;
1362432Svi117747 	}
1363432Svi117747 
1364432Svi117747 	/*
1365432Svi117747 	 * Unfortunately, we can't delay this because adding an faddr
1366432Svi117747 	 * looks for the presence of the source address (from the ire
1367432Svi117747 	 * for the faddr) in the source address list. We could have
1368432Svi117747 	 * delayed this if, say, this was a loopback/linklocal connection.
1369432Svi117747 	 * Now, we just end up nuking this list and taking the addr from
1370432Svi117747 	 * the IP header for loopback/linklocal.
1371432Svi117747 	 */
13720Sstevel@tonic-gate 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
13730Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
13760Sstevel@tonic-gate 		if (err != 0)
13770Sstevel@tonic-gate 			return (err);
13780Sstevel@tonic-gate 	}
1379432Svi117747 	/*
1380432Svi117747 	 * We will add the faddr before parsing the address list as this
1381432Svi117747 	 * might be a loopback connection and we would not have to
1382432Svi117747 	 * go through the list.
1383432Svi117747 	 *
1384432Svi117747 	 * Make sure the header's addr is in the list
1385432Svi117747 	 */
1386432Svi117747 	fp = sctp_lookup_faddr(sctp, hdrsaddr);
1387432Svi117747 	if (fp == NULL) {
1388432Svi117747 		/* not included; add it now */
13891735Skcpoon 		err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE);
13901676Sjpk 		if (err != 0)
13911676Sjpk 			return (err);
13920Sstevel@tonic-gate 
1393432Svi117747 		/* sctp_faddrs will be the hdr addr */
1394432Svi117747 		fp = sctp->sctp_faddrs;
13950Sstevel@tonic-gate 	}
1396432Svi117747 	/* make the header addr the primary */
1397852Svi117747 
1398852Svi117747 	if (cl_sctp_assoc_change != NULL && psctp == NULL)
1399852Svi117747 		curaddr = sctp->sctp_current->faddr;
1400852Svi117747 
1401432Svi117747 	sctp->sctp_primary = fp;
1402432Svi117747 	sctp->sctp_current = fp;
1403432Svi117747 	sctp->sctp_mss = fp->sfa_pmss;
14040Sstevel@tonic-gate 
1405432Svi117747 	/* For loopback connections & linklocal get address from the header */
1406432Svi117747 	if (sctp->sctp_loopback || sctp->sctp_linklocal) {
1407432Svi117747 		if (sctp->sctp_nsaddrs != 0)
1408432Svi117747 			sctp_free_saddrs(sctp);
1409852Svi117747 		if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0)
1410432Svi117747 			return (err);
1411432Svi117747 		/* For loopback ignore address list */
1412432Svi117747 		if (sctp->sctp_loopback)
1413432Svi117747 			return (0);
1414432Svi117747 		check_saddr = B_FALSE;
1415432Svi117747 	}
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
14180Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
14190Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
14200Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
1421432Svi117747 		if (check_saddr) {
1422432Svi117747 			sctp_check_saddr(sctp, supp_af, psctp == NULL ?
14234818Skcpoon 			    B_FALSE : B_TRUE, hdrdaddr);
1424432Svi117747 		}
1425852Svi117747 		ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1426432Svi117747 		return (0);
14270Sstevel@tonic-gate 	}
1428432Svi117747 
14290Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
14300Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
14310Sstevel@tonic-gate 
1432432Svi117747 	/* params will have already been byteordered when validating */
14330Sstevel@tonic-gate 	while (ph != NULL) {
1434432Svi117747 		if (ph->sph_type == htons(PARM_SUPP_ADDRS)) {
1435432Svi117747 			int		plen;
1436432Svi117747 			uint16_t	*p;
1437432Svi117747 			uint16_t	addrtype;
1438432Svi117747 
1439432Svi117747 			ASSERT(psctp != NULL);
1440432Svi117747 			plen = ntohs(ph->sph_len);
1441432Svi117747 			p = (uint16_t *)(ph + 1);
1442432Svi117747 			while (plen > 0) {
1443432Svi117747 				addrtype = ntohs(*p);
1444432Svi117747 				switch (addrtype) {
1445432Svi117747 					case PARM_ADDR6:
1446432Svi117747 						supp_af |= PARM_SUPP_V6;
1447432Svi117747 						break;
1448432Svi117747 					case PARM_ADDR4:
1449432Svi117747 						supp_af |= PARM_SUPP_V4;
1450432Svi117747 						break;
1451432Svi117747 					default:
1452432Svi117747 						break;
1453432Svi117747 				}
1454432Svi117747 				p++;
1455432Svi117747 				plen -= sizeof (*p);
1456432Svi117747 			}
1457432Svi117747 		} else if (ph->sph_type == htons(PARM_ADDR4)) {
14580Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
14590Sstevel@tonic-gate 				in6_addr_t addr;
14600Sstevel@tonic-gate 				ipaddr_t ta;
14610Sstevel@tonic-gate 
1462432Svi117747 				supp_af |= PARM_SUPP_V4;
14630Sstevel@tonic-gate 				/*
14640Sstevel@tonic-gate 				 * Screen out broad/multicasts & loopback.
14650Sstevel@tonic-gate 				 * If the endpoint only accepts v6 address,
14660Sstevel@tonic-gate 				 * go to the next one.
14674818Skcpoon 				 *
14684818Skcpoon 				 * Subnet broadcast check is done in
14694818Skcpoon 				 * sctp_add_faddr().  If the address is
14704818Skcpoon 				 * a broadcast address, it won't be added.
14710Sstevel@tonic-gate 				 */
14720Sstevel@tonic-gate 				bcopy(ph + 1, &ta, sizeof (ta));
14730Sstevel@tonic-gate 				if (ta == 0 ||
14740Sstevel@tonic-gate 				    ta == INADDR_BROADCAST ||
14750Sstevel@tonic-gate 				    ta == htonl(INADDR_LOOPBACK) ||
14765215Skcpoon 				    CLASSD(ta) ||
14770Sstevel@tonic-gate 				    sctp->sctp_connp->conn_ipv6_v6only) {
14780Sstevel@tonic-gate 					goto next;
14790Sstevel@tonic-gate 				}
14800Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
14810Sstevel@tonic-gate 				    (ph + 1), &addr);
14824818Skcpoon 
14830Sstevel@tonic-gate 				/* Check for duplicate. */
14840Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
14850Sstevel@tonic-gate 					goto next;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 				/* OK, add it to the faddr set */
14881735Skcpoon 				err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP,
14891735Skcpoon 				    B_FALSE);
14904818Skcpoon 				/* Something is wrong...  Try the next one. */
14911676Sjpk 				if (err != 0)
14924818Skcpoon 					goto next;
14930Sstevel@tonic-gate 			}
14940Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
14950Sstevel@tonic-gate 		    sctp->sctp_family == AF_INET6) {
14960Sstevel@tonic-gate 			/* An v4 socket should not take v6 addresses. */
14970Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
14980Sstevel@tonic-gate 				in6_addr_t *addr6;
14990Sstevel@tonic-gate 
1500432Svi117747 				supp_af |= PARM_SUPP_V6;
15010Sstevel@tonic-gate 				addr6 = (in6_addr_t *)(ph + 1);
15020Sstevel@tonic-gate 				/*
15030Sstevel@tonic-gate 				 * Screen out link locals, mcast, loopback
15040Sstevel@tonic-gate 				 * and bogus v6 address.
15050Sstevel@tonic-gate 				 */
15060Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
15070Sstevel@tonic-gate 				    IN6_IS_ADDR_MULTICAST(addr6) ||
15080Sstevel@tonic-gate 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
15090Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
15100Sstevel@tonic-gate 					goto next;
15110Sstevel@tonic-gate 				}
15120Sstevel@tonic-gate 				/* Check for duplicate. */
15130Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
15140Sstevel@tonic-gate 					goto next;
15150Sstevel@tonic-gate 
15161676Sjpk 				err = sctp_add_faddr(sctp,
15171735Skcpoon 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP,
15181735Skcpoon 				    B_FALSE);
15194818Skcpoon 				/* Something is wrong...  Try the next one. */
15201676Sjpk 				if (err != 0)
15214818Skcpoon 					goto next;
15220Sstevel@tonic-gate 			}
15230Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
15240Sstevel@tonic-gate 			if (sctp_options != NULL)
15250Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
15260Sstevel@tonic-gate 		} /* else; skip */
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate next:
15290Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
15300Sstevel@tonic-gate 	}
1531432Svi117747 	if (check_saddr) {
1532432Svi117747 		sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE :
15334818Skcpoon 		    B_TRUE, hdrdaddr);
15340Sstevel@tonic-gate 	}
1535852Svi117747 	ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1536852Svi117747 	/*
1537852Svi117747 	 * We have the right address list now, update clustering's
1538852Svi117747 	 * knowledge because when we sent the INIT we had just added
1539852Svi117747 	 * the address the INIT was sent to.
1540852Svi117747 	 */
1541852Svi117747 	if (psctp == NULL && cl_sctp_assoc_change != NULL) {
1542852Svi117747 		uchar_t	*alist;
1543852Svi117747 		size_t	asize;
1544852Svi117747 		uchar_t	*dlist;
1545852Svi117747 		size_t	dsize;
1546852Svi117747 
1547852Svi117747 		asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
1548852Svi117747 		alist = kmem_alloc(asize, KM_NOSLEEP);
15491735Skcpoon 		if (alist == NULL) {
15503448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1551852Svi117747 			return (ENOMEM);
15521735Skcpoon 		}
1553852Svi117747 		/*
1554852Svi117747 		 * Just include the address the INIT was sent to in the
1555852Svi117747 		 * delete list and send the entire faddr list. We could
1556852Svi117747 		 * do it differently (i.e include all the addresses in the
1557852Svi117747 		 * add list even if it contains the original address OR
1558852Svi117747 		 * remove the original address from the add list etc.), but
1559852Svi117747 		 * this seems reasonable enough.
1560852Svi117747 		 */
1561852Svi117747 		dsize = sizeof (in6_addr_t);
1562852Svi117747 		dlist = kmem_alloc(dsize, KM_NOSLEEP);
1563852Svi117747 		if (dlist == NULL) {
1564852Svi117747 			kmem_free(alist, asize);
15653448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1566852Svi117747 			return (ENOMEM);
1567852Svi117747 		}
1568852Svi117747 		bcopy(&curaddr, dlist, sizeof (curaddr));
1569852Svi117747 		sctp_get_faddr_list(sctp, alist, asize);
1570852Svi117747 		(*cl_sctp_assoc_change)(sctp->sctp_family, alist, asize,
1571852Svi117747 		    sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR,
1572852Svi117747 		    (cl_sctp_handle_t)sctp);
1573852Svi117747 		/* alist and dlist will be freed by the clustering module */
1574852Svi117747 	}
15750Sstevel@tonic-gate 	return (0);
15760Sstevel@tonic-gate }
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate /*
15790Sstevel@tonic-gate  * Returns 0 if the check failed and the restart should be refused,
15800Sstevel@tonic-gate  * 1 if the check succeeded.
15810Sstevel@tonic-gate  */
15820Sstevel@tonic-gate int
15830Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
15843448Sdh155122     int sleep, sctp_stack_t *sctps)
15850Sstevel@tonic-gate {
15864964Skcpoon 	sctp_faddr_t *fp, *fphead = NULL;
15870Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
15880Sstevel@tonic-gate 	ssize_t remaining;
15890Sstevel@tonic-gate 	int isv4;
15900Sstevel@tonic-gate 	ipha_t *iph;
15910Sstevel@tonic-gate 	ip6_t *ip6h;
15920Sstevel@tonic-gate 	in6_addr_t hdraddr[1];
15930Sstevel@tonic-gate 	int retval = 0;
15940Sstevel@tonic-gate 	sctp_tf_t *tf;
15950Sstevel@tonic-gate 	sctp_t *sctp;
15960Sstevel@tonic-gate 	int compres;
15970Sstevel@tonic-gate 	sctp_init_chunk_t *init;
15980Sstevel@tonic-gate 	int nadded = 0;
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	/* extract the address from the IP header */
16010Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
16020Sstevel@tonic-gate 	if (isv4) {
16030Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
16040Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
16050Sstevel@tonic-gate 	} else {
16060Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
16070Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
16080Sstevel@tonic-gate 	}
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
16110Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
16120Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
16130Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
16140Sstevel@tonic-gate 		/* no parameters; restart OK */
16150Sstevel@tonic-gate 		return (1);
16160Sstevel@tonic-gate 	}
16170Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
16180Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	while (ph != NULL) {
16214964Skcpoon 		sctp_faddr_t *fpa = NULL;
16224964Skcpoon 
16230Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
16240Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
16250Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
16260Sstevel@tonic-gate 				in6_addr_t addr;
16270Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
16280Sstevel@tonic-gate 				    (ph + 1), &addr);
16290Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
16300Sstevel@tonic-gate 				    sleep);
16314964Skcpoon 				if (fpa == NULL) {
16320Sstevel@tonic-gate 					goto done;
16330Sstevel@tonic-gate 				}
16340Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
16350Sstevel@tonic-gate 				fpa->faddr = addr;
16360Sstevel@tonic-gate 				fpa->next = NULL;
16370Sstevel@tonic-gate 			}
16380Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
16390Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
16400Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
16410Sstevel@tonic-gate 				    sleep);
16424964Skcpoon 				if (fpa == NULL) {
16430Sstevel@tonic-gate 					goto done;
16440Sstevel@tonic-gate 				}
16450Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
16460Sstevel@tonic-gate 				bcopy(ph + 1, &fpa->faddr,
16470Sstevel@tonic-gate 				    sizeof (fpa->faddr));
16480Sstevel@tonic-gate 				fpa->next = NULL;
16490Sstevel@tonic-gate 			}
16500Sstevel@tonic-gate 		}
16510Sstevel@tonic-gate 		/* link in the new addr, if it was an addr param */
16524964Skcpoon 		if (fpa != NULL) {
16534964Skcpoon 			if (fphead == NULL) {
16540Sstevel@tonic-gate 				fphead = fpa;
16550Sstevel@tonic-gate 			} else {
16564964Skcpoon 				fpa->next = fphead;
16574964Skcpoon 				fphead = fpa;
16580Sstevel@tonic-gate 			}
16590Sstevel@tonic-gate 		}
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	if (fphead == NULL) {
16650Sstevel@tonic-gate 		/* no addr parameters; restart OK */
16660Sstevel@tonic-gate 		return (1);
16670Sstevel@tonic-gate 	}
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	/*
16700Sstevel@tonic-gate 	 * got at least one; make sure the header's addr is
16710Sstevel@tonic-gate 	 * in the list
16720Sstevel@tonic-gate 	 */
16730Sstevel@tonic-gate 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
16744964Skcpoon 	if (fp == NULL) {
16750Sstevel@tonic-gate 		/* not included; add it now */
16760Sstevel@tonic-gate 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
16774964Skcpoon 		if (fp == NULL) {
16780Sstevel@tonic-gate 			goto done;
16790Sstevel@tonic-gate 		}
16800Sstevel@tonic-gate 		bzero(fp, sizeof (*fp));
16810Sstevel@tonic-gate 		fp->faddr = *hdraddr;
16820Sstevel@tonic-gate 		fp->next = fphead;
16830Sstevel@tonic-gate 		fphead = fp;
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	/*
16870Sstevel@tonic-gate 	 * Now, we can finally do the check: For each sctp instance
16880Sstevel@tonic-gate 	 * on the hash line for ports, compare its faddr set against
16890Sstevel@tonic-gate 	 * the new one. If the new one is a strict subset of any
16900Sstevel@tonic-gate 	 * existing sctp's faddrs, the restart is OK. However, if there
16910Sstevel@tonic-gate 	 * is an overlap, this could be an attack, so return failure.
16920Sstevel@tonic-gate 	 * If all sctp's faddrs are disjoint, this is a legitimate new
16930Sstevel@tonic-gate 	 * association.
16940Sstevel@tonic-gate 	 */
16953448Sdh155122 	tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]);
16960Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
16990Sstevel@tonic-gate 		if (ports != sctp->sctp_ports) {
17000Sstevel@tonic-gate 			continue;
17010Sstevel@tonic-gate 		}
17020Sstevel@tonic-gate 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
17030Sstevel@tonic-gate 		if (compres <= SCTP_ADDR_SUBSET) {
17040Sstevel@tonic-gate 			retval = 1;
17050Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
17060Sstevel@tonic-gate 			goto done;
17070Sstevel@tonic-gate 		}
17080Sstevel@tonic-gate 		if (compres == SCTP_ADDR_OVERLAP) {
17090Sstevel@tonic-gate 			dprint(1,
17100Sstevel@tonic-gate 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
17111676Sjpk 			    SCTP_PRINTADDR(*hdraddr), (void *)sctp));
17120Sstevel@tonic-gate 			/*
17130Sstevel@tonic-gate 			 * While we still hold the lock, we need to
17140Sstevel@tonic-gate 			 * figure out which addresses have been
17150Sstevel@tonic-gate 			 * added so we can include them in the abort
17160Sstevel@tonic-gate 			 * we will send back. Since these faddrs will
17170Sstevel@tonic-gate 			 * never be used, we overload the rto field
17180Sstevel@tonic-gate 			 * here, setting it to 0 if the address was
17190Sstevel@tonic-gate 			 * not added, 1 if it was added.
17200Sstevel@tonic-gate 			 */
17210Sstevel@tonic-gate 			for (fp = fphead; fp; fp = fp->next) {
17220Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &fp->faddr)) {
17230Sstevel@tonic-gate 					fp->rto = 0;
17240Sstevel@tonic-gate 				} else {
17250Sstevel@tonic-gate 					fp->rto = 1;
17260Sstevel@tonic-gate 					nadded++;
17270Sstevel@tonic-gate 				}
17280Sstevel@tonic-gate 			}
17290Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
17300Sstevel@tonic-gate 			goto done;
17310Sstevel@tonic-gate 		}
17320Sstevel@tonic-gate 	}
17330Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	/* All faddrs are disjoint; legit new association */
17360Sstevel@tonic-gate 	retval = 1;
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate done:
17390Sstevel@tonic-gate 	/* If are attempted adds, send back an abort listing the addrs */
17400Sstevel@tonic-gate 	if (nadded > 0) {
17410Sstevel@tonic-gate 		void *dtail;
17420Sstevel@tonic-gate 		size_t dlen;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
17450Sstevel@tonic-gate 		if (dtail == NULL) {
17460Sstevel@tonic-gate 			goto cleanup;
17470Sstevel@tonic-gate 		}
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 		ph = dtail;
17500Sstevel@tonic-gate 		dlen = 0;
17510Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fp->next) {
17520Sstevel@tonic-gate 			if (fp->rto == 0) {
17530Sstevel@tonic-gate 				continue;
17540Sstevel@tonic-gate 			}
17550Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
17560Sstevel@tonic-gate 				ipaddr_t addr4;
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR4);
17590Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR4_LEN);
17600Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
17610Sstevel@tonic-gate 				ph++;
17620Sstevel@tonic-gate 				bcopy(&addr4, ph, sizeof (addr4));
17630Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
17640Sstevel@tonic-gate 				    ((char *)ph + sizeof (addr4));
17650Sstevel@tonic-gate 				dlen += PARM_ADDR4_LEN;
17660Sstevel@tonic-gate 			} else {
17670Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR6);
17680Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR6_LEN);
17690Sstevel@tonic-gate 				ph++;
17700Sstevel@tonic-gate 				bcopy(&fp->faddr, ph, sizeof (fp->faddr));
17710Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
17720Sstevel@tonic-gate 				    ((char *)ph + sizeof (fp->faddr));
17730Sstevel@tonic-gate 				dlen += PARM_ADDR6_LEN;
17740Sstevel@tonic-gate 			}
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 		/* Send off the abort */
17780Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ich),
17790Sstevel@tonic-gate 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE);
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
17820Sstevel@tonic-gate 	}
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate cleanup:
17850Sstevel@tonic-gate 	/* Clean up */
17860Sstevel@tonic-gate 	if (fphead) {
17870Sstevel@tonic-gate 		sctp_faddr_t *fpn;
17880Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fpn) {
17890Sstevel@tonic-gate 			fpn = fp->next;
17900Sstevel@tonic-gate 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
17910Sstevel@tonic-gate 		}
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	return (retval);
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17971932Svi117747 /*
17981932Svi117747  * Reset any state related to transmitted chunks.
17991932Svi117747  */
18000Sstevel@tonic-gate void
18010Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp)
18020Sstevel@tonic-gate {
18031932Svi117747 	sctp_faddr_t	*fp;
18043448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
18051932Svi117747 	mblk_t		*mp;
18060Sstevel@tonic-gate 
18071932Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
18083448Sdh155122 		fp->ssthresh = sctps->sctps_initial_mtu;
18093795Skcpoon 		SET_CWND(fp, fp->sfa_pmss, sctps->sctps_slow_start_initial);
18100Sstevel@tonic-gate 		fp->suna = 0;
18110Sstevel@tonic-gate 		fp->pba = 0;
18120Sstevel@tonic-gate 	}
18131932Svi117747 	/*
18141932Svi117747 	 * Clean up the transmit list as well since we have reset accounting
18151932Svi117747 	 * on all the fps. Send event upstream, if required.
18161932Svi117747 	 */
18171932Svi117747 	while ((mp = sctp->sctp_xmit_head) != NULL) {
18181932Svi117747 		sctp->sctp_xmit_head = mp->b_next;
18191932Svi117747 		mp->b_next = NULL;
18201932Svi117747 		if (sctp->sctp_xmit_head != NULL)
18211932Svi117747 			sctp->sctp_xmit_head->b_prev = NULL;
18221932Svi117747 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
18231932Svi117747 	}
18241932Svi117747 	sctp->sctp_xmit_head = NULL;
18251932Svi117747 	sctp->sctp_xmit_tail = NULL;
18261932Svi117747 	sctp->sctp_xmit_unacked = NULL;
18271932Svi117747 
18281932Svi117747 	sctp->sctp_unacked = 0;
18291932Svi117747 	/*
18301932Svi117747 	 * Any control message as well. We will clean-up this list as well.
18311932Svi117747 	 * This contains any pending ASCONF request that we have queued/sent.
18321932Svi117747 	 * If we do get an ACK we will just drop it. However, given that
18331932Svi117747 	 * we are restarting chances are we aren't going to get any.
18341932Svi117747 	 */
18351932Svi117747 	if (sctp->sctp_cxmit_list != NULL)
18361932Svi117747 		sctp_asconf_free_cxmit(sctp, NULL);
18371932Svi117747 	sctp->sctp_cxmit_list = NULL;
18381932Svi117747 	sctp->sctp_cchunk_pend = 0;
18391932Svi117747 
18401932Svi117747 	sctp->sctp_rexmitting = B_FALSE;
18411932Svi117747 	sctp->sctp_rxt_nxttsn = 0;
18421932Svi117747 	sctp->sctp_rxt_maxtsn = 0;
18431932Svi117747 
18441932Svi117747 	sctp->sctp_zero_win_probe = B_FALSE;
18450Sstevel@tonic-gate }
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate static void
18481735Skcpoon sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr,
18491735Skcpoon     mblk_t *timer_mp)
18500Sstevel@tonic-gate {
18513448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
18523448Sdh155122 
18530Sstevel@tonic-gate 	bcopy(addr, &fp->faddr, sizeof (*addr));
18540Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
18550Sstevel@tonic-gate 		fp->isv4 = 1;
18560Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
18574691Skcpoon 		fp->sfa_pmss =
18584691Skcpoon 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) &
18594691Skcpoon 		    ~(SCTP_ALIGN - 1);
18600Sstevel@tonic-gate 	} else {
18610Sstevel@tonic-gate 		fp->isv4 = 0;
18623448Sdh155122 		fp->sfa_pmss =
18633448Sdh155122 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) &
18643448Sdh155122 		    ~(SCTP_ALIGN - 1);
18650Sstevel@tonic-gate 	}
18663448Sdh155122 	fp->cwnd = sctps->sctps_slow_start_initial * fp->sfa_pmss;
18670Sstevel@tonic-gate 	fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max);
1868*10212SGeorge.Shepherd@Sun.COM 	SCTP_MAX_RTO(sctp, fp);
18690Sstevel@tonic-gate 	fp->srtt = -1;
18700Sstevel@tonic-gate 	fp->rtt_updates = 0;
18710Sstevel@tonic-gate 	fp->strikes = 0;
18720Sstevel@tonic-gate 	fp->max_retr = sctp->sctp_pp_max_rxt;
18730Sstevel@tonic-gate 	/* Mark it as not confirmed. */
18740Sstevel@tonic-gate 	fp->state = SCTP_FADDRS_UNCONFIRMED;
18750Sstevel@tonic-gate 	fp->hb_interval = sctp->sctp_hb_interval;
18763448Sdh155122 	fp->ssthresh = sctps->sctps_initial_ssthresh;
18770Sstevel@tonic-gate 	fp->suna = 0;
18780Sstevel@tonic-gate 	fp->pba = 0;
18790Sstevel@tonic-gate 	fp->acked = 0;
18800Sstevel@tonic-gate 	fp->lastactive = lbolt64;
18811735Skcpoon 	fp->timer_mp = timer_mp;
18820Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
18834818Skcpoon 	fp->hb_enabled = B_TRUE;
18840Sstevel@tonic-gate 	fp->df = 1;
18850Sstevel@tonic-gate 	fp->pmtu_discovered = 0;
18860Sstevel@tonic-gate 	fp->next = NULL;
18870Sstevel@tonic-gate 	fp->ire = NULL;
18880Sstevel@tonic-gate 	fp->T3expire = 0;
18890Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret,
18900Sstevel@tonic-gate 	    sizeof (fp->hb_secret));
18910Sstevel@tonic-gate 	fp->hb_expiry = lbolt64;
18923795Skcpoon 	fp->rxt_unacked = 0;
18930Sstevel@tonic-gate 
18941735Skcpoon 	sctp_get_ire(sctp, fp);
18950Sstevel@tonic-gate }
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate /*ARGSUSED*/
18986712Stomee static int
18996712Stomee faddr_constructor(void *buf, void *arg, int flags)
19006712Stomee {
19016712Stomee 	sctp_faddr_t *fp = buf;
19026712Stomee 
19036712Stomee 	fp->timer_mp = NULL;
19046712Stomee 	fp->timer_running = 0;
19056712Stomee 
19066712Stomee 	fp->rc_timer_mp = NULL;
19076712Stomee 	fp->rc_timer_running = 0;
19086712Stomee 
19096712Stomee 	return (0);
19106712Stomee }
19116712Stomee 
19126712Stomee /*ARGSUSED*/
19130Sstevel@tonic-gate static void
19146712Stomee faddr_destructor(void *buf, void *arg)
19150Sstevel@tonic-gate {
19160Sstevel@tonic-gate 	sctp_faddr_t *fp = buf;
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	ASSERT(fp->timer_mp == NULL);
19190Sstevel@tonic-gate 	ASSERT(fp->timer_running == 0);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	ASSERT(fp->rc_timer_mp == NULL);
19220Sstevel@tonic-gate 	ASSERT(fp->rc_timer_running == 0);
19230Sstevel@tonic-gate }
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate void
19261676Sjpk sctp_faddr_init(void)
19270Sstevel@tonic-gate {
19280Sstevel@tonic-gate 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
19296712Stomee 	    sizeof (sctp_faddr_t), 0, faddr_constructor, faddr_destructor,
19300Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
19310Sstevel@tonic-gate }
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate void
19341676Sjpk sctp_faddr_fini(void)
19350Sstevel@tonic-gate {
19360Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_faddr_cache);
19370Sstevel@tonic-gate }
1938