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 /*
23*11908SGeorge.Shepherd@Sun.COM  * Copyright 2010 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>
4711042SErik.Nordmark@Sun.COM #include <inet/ip_if.h>
4811042SErik.Nordmark@Sun.COM #include <inet/ip_ndp.h>
490Sstevel@tonic-gate #include <inet/mib2.h>
500Sstevel@tonic-gate #include <inet/nd.h>
510Sstevel@tonic-gate #include <inet/optcom.h>
520Sstevel@tonic-gate #include <inet/sctp_ip.h>
530Sstevel@tonic-gate #include <inet/ipclassifier.h>
541676Sjpk 
550Sstevel@tonic-gate #include "sctp_impl.h"
560Sstevel@tonic-gate #include "sctp_addr.h"
571932Svi117747 #include "sctp_asconf.h"
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_faddr_cache;
601735Skcpoon static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *);
610Sstevel@tonic-gate 
6211042SErik.Nordmark@Sun.COM /* Set the source address.  Refer to comments in sctp_get_dest(). */
631932Svi117747 void
641932Svi117747 sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp)
650Sstevel@tonic-gate {
661719Sjarrett 	boolean_t v6 = !fp->isv4;
674818Skcpoon 	boolean_t addr_set;
681719Sjarrett 
694818Skcpoon 	fp->saddr = sctp_get_valid_addr(sctp, v6, &addr_set);
704818Skcpoon 	/*
714818Skcpoon 	 * If there is no source address avaialble, mark this peer address
724818Skcpoon 	 * as unreachable for now.  When the heartbeat timer fires, it will
7311042SErik.Nordmark@Sun.COM 	 * call sctp_get_dest() to re-check if there is any source address
744818Skcpoon 	 * available.
754818Skcpoon 	 */
764818Skcpoon 	if (!addr_set)
774818Skcpoon 		fp->state = SCTP_FADDRS_UNREACH;
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
8111042SErik.Nordmark@Sun.COM  * Call this function to get information about a peer addr fp.
8211042SErik.Nordmark@Sun.COM  *
8311042SErik.Nordmark@Sun.COM  * Uses ip_attr_connect to avoid explicit use of ire and source address
8411042SErik.Nordmark@Sun.COM  * selection.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate void
8711042SErik.Nordmark@Sun.COM sctp_get_dest(sctp_t *sctp, sctp_faddr_t *fp)
880Sstevel@tonic-gate {
891735Skcpoon 	in6_addr_t	laddr;
9011042SErik.Nordmark@Sun.COM 	in6_addr_t	nexthop;
910Sstevel@tonic-gate 	sctp_saddr_ipif_t *sp;
921735Skcpoon 	int		hdrlen;
933448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
9411042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
9511042SErik.Nordmark@Sun.COM 	iulp_t		uinfo;
9611042SErik.Nordmark@Sun.COM 	uint_t		pmtu;
9711042SErik.Nordmark@Sun.COM 	int		error;
9811042SErik.Nordmark@Sun.COM 	uint32_t	flags = IPDF_VERIFY_DST | IPDF_IPSEC |
9911042SErik.Nordmark@Sun.COM 	    IPDF_SELECT_SRC | IPDF_UNIQUE_DCE;
1000Sstevel@tonic-gate 
10111042SErik.Nordmark@Sun.COM 	/*
10211042SErik.Nordmark@Sun.COM 	 * Tell sctp_make_mp it needs to call us again should we not
10311042SErik.Nordmark@Sun.COM 	 * complete and set the saddr.
10411042SErik.Nordmark@Sun.COM 	 */
10511042SErik.Nordmark@Sun.COM 	fp->saddr = ipv6_all_zeros;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * If this addr is not reachable, mark it as unconfirmed for now, the
1090Sstevel@tonic-gate 	 * state will be changed back to unreachable later in this function
1100Sstevel@tonic-gate 	 * if it is still the case.
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH) {
1130Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_UNCONFIRMED;
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
11611042SErik.Nordmark@Sun.COM 	/*
11711042SErik.Nordmark@Sun.COM 	 * Socket is connected - enable PMTU discovery.
11811042SErik.Nordmark@Sun.COM 	 */
11911042SErik.Nordmark@Sun.COM 	if (!sctps->sctps_ignore_path_mtu)
12011042SErik.Nordmark@Sun.COM 		fp->ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
1211676Sjpk 
12211042SErik.Nordmark@Sun.COM 	ip_attr_nexthop(&connp->conn_xmit_ipp, fp->ixa, &fp->faddr,
12311042SErik.Nordmark@Sun.COM 	    &nexthop);
1240Sstevel@tonic-gate 
12511042SErik.Nordmark@Sun.COM 	laddr = fp->saddr;
12611042SErik.Nordmark@Sun.COM 	error = ip_attr_connect(connp, fp->ixa, &laddr, &fp->faddr, &nexthop,
12711042SErik.Nordmark@Sun.COM 	    connp->conn_fport, &laddr, &uinfo, flags);
12811042SErik.Nordmark@Sun.COM 
12911042SErik.Nordmark@Sun.COM 	if (error != 0) {
13011042SErik.Nordmark@Sun.COM 		dprint(3, ("sctp_get_dest: no ire for %x:%x:%x:%x\n",
1311676Sjpk 		    SCTP_PRINTADDR(fp->faddr)));
1321676Sjpk 		/*
1331676Sjpk 		 * It is tempting to just leave the src addr
1341676Sjpk 		 * unspecified and let IP figure it out, but we
1351676Sjpk 		 * *cannot* do this, since IP may choose a src addr
1361676Sjpk 		 * that is not part of this association... unless
13711042SErik.Nordmark@Sun.COM 		 * this sctp has bound to all addrs.  So if the dest
1381676Sjpk 		 * lookup fails, try to find one in our src addr
1391676Sjpk 		 * list, unless the sctp has bound to all addrs, in
1401676Sjpk 		 * which case we change the src addr to unspec.
1411676Sjpk 		 *
1421676Sjpk 		 * Note that if this is a v6 endpoint but it does
1431676Sjpk 		 * not have any v4 address at this point (e.g. may
1441676Sjpk 		 * have been  deleted), sctp_get_valid_addr() will
1451676Sjpk 		 * return mapped INADDR_ANY.  In this case, this
1461676Sjpk 		 * address should be marked not reachable so that
1471676Sjpk 		 * it won't be used to send data.
1481676Sjpk 		 */
1491932Svi117747 		sctp_set_saddr(sctp, fp);
1501735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH)
1511735Skcpoon 			return;
1521735Skcpoon 		goto check_current;
1531676Sjpk 	}
15411042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa->ixa_ire != NULL);
15511042SErik.Nordmark@Sun.COM 	ASSERT(!(fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)));
15611042SErik.Nordmark@Sun.COM 
15711042SErik.Nordmark@Sun.COM 	if (!sctp->sctp_loopback)
15811042SErik.Nordmark@Sun.COM 		sctp->sctp_loopback = uinfo.iulp_loopback;
1591676Sjpk 
1601735Skcpoon 	/* Make sure the laddr is part of this association */
16111042SErik.Nordmark@Sun.COM 	if ((sp = sctp_saddr_lookup(sctp, &laddr, 0)) != NULL &&
16211042SErik.Nordmark@Sun.COM 	    !sp->saddr_ipif_dontsrc) {
1631676Sjpk 		if (sp->saddr_ipif_unconfirmed == 1)
1641676Sjpk 			sp->saddr_ipif_unconfirmed = 0;
16511042SErik.Nordmark@Sun.COM 		/* We did IPsec policy lookup for laddr already */
1661676Sjpk 		fp->saddr = laddr;
1671676Sjpk 	} else {
16811042SErik.Nordmark@Sun.COM 		dprint(2, ("sctp_get_dest: src addr is not part of assoc "
16911042SErik.Nordmark@Sun.COM 		    "%x:%x:%x:%x\n", SCTP_PRINTADDR(laddr)));
1701735Skcpoon 
1711735Skcpoon 		/*
1721735Skcpoon 		 * Set the src to the first saddr and hope for the best.
17311042SErik.Nordmark@Sun.COM 		 * Note that this case should very seldomly
1741735Skcpoon 		 * happen.  One scenario this can happen is an app
1751735Skcpoon 		 * explicitly bind() to an address.  But that address is
1761735Skcpoon 		 * not the preferred source address to send to the peer.
1771735Skcpoon 		 */
1781932Svi117747 		sctp_set_saddr(sctp, fp);
1791735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH) {
1801735Skcpoon 			return;
1811735Skcpoon 		}
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 
1841735Skcpoon 	/*
1850Sstevel@tonic-gate 	 * Pull out RTO information for this faddr and use it if we don't
1860Sstevel@tonic-gate 	 * have any yet.
1870Sstevel@tonic-gate 	 */
18811042SErik.Nordmark@Sun.COM 	if (fp->srtt == -1 && uinfo.iulp_rtt != 0) {
189116Skcpoon 		/* The cached value is in ms. */
19011042SErik.Nordmark@Sun.COM 		fp->srtt = MSEC_TO_TICK(uinfo.iulp_rtt);
19111042SErik.Nordmark@Sun.COM 		fp->rttvar = MSEC_TO_TICK(uinfo.iulp_rtt_sd);
1920Sstevel@tonic-gate 		fp->rto = 3 * fp->srtt;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		/* Bound the RTO by configured min and max values */
1950Sstevel@tonic-gate 		if (fp->rto < sctp->sctp_rto_min) {
1960Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_min;
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 		if (fp->rto > sctp->sctp_rto_max) {
1990Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_max;
2000Sstevel@tonic-gate 		}
20110212SGeorge.Shepherd@Sun.COM 		SCTP_MAX_RTO(sctp, fp);
2020Sstevel@tonic-gate 	}
20311042SErik.Nordmark@Sun.COM 	pmtu = uinfo.iulp_mtu;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * Record the MTU for this faddr. If the MTU for this faddr has
2070Sstevel@tonic-gate 	 * changed, check if the assc MTU will also change.
2080Sstevel@tonic-gate 	 */
2090Sstevel@tonic-gate 	if (fp->isv4) {
2100Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
2110Sstevel@tonic-gate 	} else {
2120Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
2130Sstevel@tonic-gate 	}
21411042SErik.Nordmark@Sun.COM 	if ((fp->sfa_pmss + hdrlen) != pmtu) {
2150Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
21611042SErik.Nordmark@Sun.COM 		fp->sfa_pmss = (pmtu - hdrlen) & ~(SCTP_ALIGN - 1);
2170Sstevel@tonic-gate 		if (fp->cwnd < (fp->sfa_pmss * 2)) {
2183795Skcpoon 			SET_CWND(fp, fp->sfa_pmss,
2193795Skcpoon 			    sctps->sctps_slow_start_initial);
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2231735Skcpoon check_current:
2241735Skcpoon 	if (fp == sctp->sctp_current)
2251735Skcpoon 		sctp_set_faddr_current(sctp, fp);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate void
22911042SErik.Nordmark@Sun.COM sctp_update_dce(sctp_t *sctp)
2300Sstevel@tonic-gate {
2311735Skcpoon 	sctp_faddr_t	*fp;
2323448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
23311042SErik.Nordmark@Sun.COM 	iulp_t		uinfo;
23411042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = sctps->sctps_netstack->netstack_ip;
23511042SErik.Nordmark@Sun.COM 	uint_t		ifindex;
2360Sstevel@tonic-gate 
2371735Skcpoon 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
23811042SErik.Nordmark@Sun.COM 		bzero(&uinfo, sizeof (uinfo));
2390Sstevel@tonic-gate 		/*
2401735Skcpoon 		 * Only record the PMTU for this faddr if we actually have
2411735Skcpoon 		 * done discovery. This prevents initialized default from
2421735Skcpoon 		 * clobbering any real info that IP may have.
2430Sstevel@tonic-gate 		 */
2441735Skcpoon 		if (fp->pmtu_discovered) {
2451735Skcpoon 			if (fp->isv4) {
24611042SErik.Nordmark@Sun.COM 				uinfo.iulp_mtu = fp->sfa_pmss +
2471735Skcpoon 				    sctp->sctp_hdr_len;
2481735Skcpoon 			} else {
24911042SErik.Nordmark@Sun.COM 				uinfo.iulp_mtu = fp->sfa_pmss +
2501735Skcpoon 				    sctp->sctp_hdr6_len;
2511735Skcpoon 			}
2520Sstevel@tonic-gate 		}
2533448Sdh155122 		if (sctps->sctps_rtt_updates != 0 &&
2543448Sdh155122 		    fp->rtt_updates >= sctps->sctps_rtt_updates) {
2551735Skcpoon 			/*
25611042SErik.Nordmark@Sun.COM 			 * dce_update_uinfo() merges these values with the
25711042SErik.Nordmark@Sun.COM 			 * old values.
2581735Skcpoon 			 */
25911042SErik.Nordmark@Sun.COM 			uinfo.iulp_rtt = TICK_TO_MSEC(fp->srtt);
26011042SErik.Nordmark@Sun.COM 			uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->rttvar);
2611735Skcpoon 			fp->rtt_updates = 0;
2620Sstevel@tonic-gate 		}
26311042SErik.Nordmark@Sun.COM 		ifindex = 0;
26411042SErik.Nordmark@Sun.COM 		if (IN6_IS_ADDR_LINKSCOPE(&fp->faddr)) {
26511042SErik.Nordmark@Sun.COM 			/*
26611042SErik.Nordmark@Sun.COM 			 * If we are going to create a DCE we'd better have
26711042SErik.Nordmark@Sun.COM 			 * an ifindex
26811042SErik.Nordmark@Sun.COM 			 */
26911042SErik.Nordmark@Sun.COM 			if (fp->ixa->ixa_nce != NULL) {
27011042SErik.Nordmark@Sun.COM 				ifindex = fp->ixa->ixa_nce->nce_common->
27111042SErik.Nordmark@Sun.COM 				    ncec_ill->ill_phyint->phyint_ifindex;
27211042SErik.Nordmark@Sun.COM 			} else {
27311042SErik.Nordmark@Sun.COM 				continue;
27411042SErik.Nordmark@Sun.COM 			}
27511042SErik.Nordmark@Sun.COM 		}
27611042SErik.Nordmark@Sun.COM 
27711042SErik.Nordmark@Sun.COM 		(void) dce_update_uinfo(&fp->faddr, ifindex, &uinfo, ipst);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*
28211042SErik.Nordmark@Sun.COM  * The sender must later set the total length in the IP header.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate mblk_t *
28511042SErik.Nordmark@Sun.COM sctp_make_mp(sctp_t *sctp, sctp_faddr_t *fp, int trailer)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	mblk_t *mp;
2880Sstevel@tonic-gate 	size_t ipsctplen;
2890Sstevel@tonic-gate 	int isv4;
2903448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
2914818Skcpoon 	boolean_t src_changed = B_FALSE;
2920Sstevel@tonic-gate 
29311042SErik.Nordmark@Sun.COM 	ASSERT(fp != NULL);
2940Sstevel@tonic-gate 	isv4 = fp->isv4;
2950Sstevel@tonic-gate 
29611042SErik.Nordmark@Sun.COM 	if (SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr) ||
29711042SErik.Nordmark@Sun.COM 	    (fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
29811042SErik.Nordmark@Sun.COM 		/* Need to pick a source */
29911042SErik.Nordmark@Sun.COM 		sctp_get_dest(sctp, fp);
3004818Skcpoon 		/*
3014818Skcpoon 		 * Although we still may not get an IRE, the source address
3024818Skcpoon 		 * may be changed in sctp_get_ire().  Set src_changed to
3034818Skcpoon 		 * true so that the source address is copied again.
3044818Skcpoon 		 */
3054818Skcpoon 		src_changed = B_TRUE;
3064818Skcpoon 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/* There is no suitable source address to use, return. */
3090Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH)
3100Sstevel@tonic-gate 		return (NULL);
31111042SErik.Nordmark@Sun.COM 
31211042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa->ixa_ire != NULL);
31311042SErik.Nordmark@Sun.COM 	ASSERT(!SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr));
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (isv4) {
3160Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr_len;
3170Sstevel@tonic-gate 	} else {
3180Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr6_len;
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
32111042SErik.Nordmark@Sun.COM 	mp = allocb(ipsctplen + sctps->sctps_wroff_xtra + trailer, BPRI_MED);
3220Sstevel@tonic-gate 	if (mp == NULL) {
3231676Sjpk 		ip1dbg(("sctp_make_mp: error making mp..\n"));
3240Sstevel@tonic-gate 		return (NULL);
3250Sstevel@tonic-gate 	}
3263448Sdh155122 	mp->b_rptr += sctps->sctps_wroff_xtra;
3270Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + ipsctplen;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_wptr));
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (isv4) {
3320Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)mp->b_rptr;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
3354818Skcpoon 		if (fp != sctp->sctp_current || src_changed) {
3364818Skcpoon 			/* Fix the source and destination addresses. */
3370Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
3384818Skcpoon 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr, iph->ipha_src);
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 		/* set or clear the don't fragment bit */
3410Sstevel@tonic-gate 		if (fp->df) {
3420Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
3430Sstevel@tonic-gate 		} else {
3440Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 	} else {
3470Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
3484818Skcpoon 		if (fp != sctp->sctp_current || src_changed) {
3494818Skcpoon 			/* Fix the source and destination addresses. */
3500Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
3514818Skcpoon 			((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp != NULL);
3550Sstevel@tonic-gate 	return (mp);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * Notify upper layers about preferred write offset, write size.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate void
3620Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	int hdrlen;
3658348SEric.Yu@Sun.COM 	struct sock_proto_props sopp;
3668348SEric.Yu@Sun.COM 
3673448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if (sctp->sctp_current->isv4) {
3700Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
3710Sstevel@tonic-gate 	} else {
3720Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd);
3750Sstevel@tonic-gate 
37611042SErik.Nordmark@Sun.COM 	sctp->sctp_connp->conn_wroff = sctps->sctps_wroff_xtra + hdrlen +
37711042SErik.Nordmark@Sun.COM 	    sizeof (sctp_data_hdr_t);
37811042SErik.Nordmark@Sun.COM 
3790Sstevel@tonic-gate 	ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss);
3808348SEric.Yu@Sun.COM 	bzero(&sopp, sizeof (sopp));
3818348SEric.Yu@Sun.COM 	sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF;
38211042SErik.Nordmark@Sun.COM 	sopp.sopp_wroff = sctp->sctp_connp->conn_wroff;
3838348SEric.Yu@Sun.COM 	sopp.sopp_maxblk = sctp->sctp_mss - sizeof (sctp_data_hdr_t);
3848348SEric.Yu@Sun.COM 	sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
38711042SErik.Nordmark@Sun.COM /*
38811042SErik.Nordmark@Sun.COM  * Set the lengths in the packet and the transmit attributes.
38911042SErik.Nordmark@Sun.COM  */
3900Sstevel@tonic-gate void
39111042SErik.Nordmark@Sun.COM sctp_set_iplen(sctp_t *sctp, mblk_t *mp, ip_xmit_attr_t *ixa)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	uint16_t	sum = 0;
3940Sstevel@tonic-gate 	ipha_t		*iph;
3950Sstevel@tonic-gate 	ip6_t		*ip6h;
3960Sstevel@tonic-gate 	mblk_t		*pmp = mp;
3970Sstevel@tonic-gate 	boolean_t	isv4;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
4000Sstevel@tonic-gate 	for (; pmp; pmp = pmp->b_cont)
4010Sstevel@tonic-gate 		sum += pmp->b_wptr - pmp->b_rptr;
4020Sstevel@tonic-gate 
40311042SErik.Nordmark@Sun.COM 	ixa->ixa_pktlen = sum;
4040Sstevel@tonic-gate 	if (isv4) {
4050Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
4060Sstevel@tonic-gate 		iph->ipha_length = htons(sum);
40711042SErik.Nordmark@Sun.COM 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr_len;
4080Sstevel@tonic-gate 	} else {
4090Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
41011042SErik.Nordmark@Sun.COM 		ip6h->ip6_plen = htons(sum - IPV6_HDR_LEN);
41111042SErik.Nordmark@Sun.COM 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr6_len;
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate int
4160Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	int na1 = 0;
4190Sstevel@tonic-gate 	int overlap = 0;
4200Sstevel@tonic-gate 	int equal = 1;
4210Sstevel@tonic-gate 	int onematch;
4220Sstevel@tonic-gate 	sctp_faddr_t *fp1, *fp2;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	for (fp1 = a1; fp1; fp1 = fp1->next) {
4250Sstevel@tonic-gate 		onematch = 0;
4260Sstevel@tonic-gate 		for (fp2 = a2; fp2; fp2 = fp2->next) {
4270Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) {
4280Sstevel@tonic-gate 				overlap++;
4290Sstevel@tonic-gate 				onematch = 1;
4300Sstevel@tonic-gate 				break;
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 			if (!onematch) {
4330Sstevel@tonic-gate 				equal = 0;
4340Sstevel@tonic-gate 			}
4350Sstevel@tonic-gate 		}
4360Sstevel@tonic-gate 		na1++;
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (equal) {
4400Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 	if (overlap == na1) {
4430Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	if (overlap) {
4460Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4529710SKen.Powell@Sun.COM  * Returns 0 on success, ENOMEM on memory allocation failure, EHOSTUNREACH
4539710SKen.Powell@Sun.COM  * if the connection credentials fail remote host accreditation or
4549710SKen.Powell@Sun.COM  * if the new destination does not support the previously established
4559710SKen.Powell@Sun.COM  * connection security label. If sleep is true, this function should
4569710SKen.Powell@Sun.COM  * never fail for a memory allocation failure. The boolean parameter
4579710SKen.Powell@Sun.COM  * "first" decides whether the newly created faddr structure should be
4581735Skcpoon  * added at the beginning of the list or at the end.
4591735Skcpoon  *
4601735Skcpoon  * Note: caller must hold conn fanout lock.
4610Sstevel@tonic-gate  */
4621735Skcpoon int
4631735Skcpoon sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first)
4640Sstevel@tonic-gate {
4651735Skcpoon 	sctp_faddr_t	*faddr;
4661735Skcpoon 	mblk_t		*timer_mp;
4679710SKen.Powell@Sun.COM 	int		err;
46811042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
4690Sstevel@tonic-gate 
4701676Sjpk 	if (is_system_labeled()) {
47111042SErik.Nordmark@Sun.COM 		ip_xmit_attr_t	*ixa = connp->conn_ixa;
47211042SErik.Nordmark@Sun.COM 		ts_label_t	*effective_tsl = NULL;
47311042SErik.Nordmark@Sun.COM 
47411042SErik.Nordmark@Sun.COM 		ASSERT(ixa->ixa_tsl != NULL);
4750Sstevel@tonic-gate 
4769710SKen.Powell@Sun.COM 		/*
4779710SKen.Powell@Sun.COM 		 * Verify the destination is allowed to receive packets
4789710SKen.Powell@Sun.COM 		 * at the security label of the connection we are initiating.
4799710SKen.Powell@Sun.COM 		 *
48011042SErik.Nordmark@Sun.COM 		 * tsol_check_dest() will create a new effective label for
4819710SKen.Powell@Sun.COM 		 * this connection with a modified label or label flags only
48211042SErik.Nordmark@Sun.COM 		 * if there are changes from the original label.
4839710SKen.Powell@Sun.COM 		 *
4849710SKen.Powell@Sun.COM 		 * Accept whatever label we get if this is the first
4859710SKen.Powell@Sun.COM 		 * destination address for this connection. The security
4869710SKen.Powell@Sun.COM 		 * label and label flags must match any previuous settings
4879710SKen.Powell@Sun.COM 		 * for all subsequent destination addresses.
4889710SKen.Powell@Sun.COM 		 */
4899710SKen.Powell@Sun.COM 		if (IN6_IS_ADDR_V4MAPPED(addr)) {
4909710SKen.Powell@Sun.COM 			uint32_t dst;
4919710SKen.Powell@Sun.COM 			IN6_V4MAPPED_TO_IPADDR(addr, dst);
49211042SErik.Nordmark@Sun.COM 			err = tsol_check_dest(ixa->ixa_tsl,
49311042SErik.Nordmark@Sun.COM 			    &dst, IPV4_VERSION, connp->conn_mac_mode,
49411042SErik.Nordmark@Sun.COM 			    connp->conn_zone_is_global, &effective_tsl);
4959710SKen.Powell@Sun.COM 		} else {
49611042SErik.Nordmark@Sun.COM 			err = tsol_check_dest(ixa->ixa_tsl,
49711042SErik.Nordmark@Sun.COM 			    addr, IPV6_VERSION, connp->conn_mac_mode,
49811042SErik.Nordmark@Sun.COM 			    connp->conn_zone_is_global, &effective_tsl);
4991676Sjpk 		}
5009710SKen.Powell@Sun.COM 		if (err != 0)
5019710SKen.Powell@Sun.COM 			return (err);
50211042SErik.Nordmark@Sun.COM 
50311042SErik.Nordmark@Sun.COM 		if (sctp->sctp_faddrs == NULL && effective_tsl != NULL) {
50411042SErik.Nordmark@Sun.COM 			ip_xmit_attr_replace_tsl(ixa, effective_tsl);
50511042SErik.Nordmark@Sun.COM 		} else if (effective_tsl != NULL) {
50611042SErik.Nordmark@Sun.COM 			label_rele(effective_tsl);
5079710SKen.Powell@Sun.COM 			return (EHOSTUNREACH);
5089710SKen.Powell@Sun.COM 		}
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5111676Sjpk 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL)
5121676Sjpk 		return (ENOMEM);
51311042SErik.Nordmark@Sun.COM 	bzero(faddr, sizeof (*faddr));
5144691Skcpoon 	timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep);
5151735Skcpoon 	if (timer_mp == NULL) {
5161735Skcpoon 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
5171735Skcpoon 		return (ENOMEM);
5181735Skcpoon 	}
5191735Skcpoon 	((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr;
5201676Sjpk 
52111042SErik.Nordmark@Sun.COM 	/* Start with any options set on the conn */
52211042SErik.Nordmark@Sun.COM 	faddr->ixa = conn_get_ixa_exclusive(connp);
52311042SErik.Nordmark@Sun.COM 	if (faddr->ixa == NULL) {
52411042SErik.Nordmark@Sun.COM 		freemsg(timer_mp);
52511042SErik.Nordmark@Sun.COM 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
52611042SErik.Nordmark@Sun.COM 		return (ENOMEM);
52711042SErik.Nordmark@Sun.COM 	}
52811042SErik.Nordmark@Sun.COM 	faddr->ixa->ixa_notify_cookie = connp->conn_sctp;
5294818Skcpoon 
53011042SErik.Nordmark@Sun.COM 	sctp_init_faddr(sctp, faddr, addr, timer_mp);
53111042SErik.Nordmark@Sun.COM 	ASSERT(faddr->ixa->ixa_cred != NULL);
53211042SErik.Nordmark@Sun.COM 
53311042SErik.Nordmark@Sun.COM 	/* ip_attr_connect didn't allow broadcats/multicast dest */
5340Sstevel@tonic-gate 	ASSERT(faddr->next == NULL);
5350Sstevel@tonic-gate 
5361676Sjpk 	if (sctp->sctp_faddrs == NULL) {
5371676Sjpk 		ASSERT(sctp->sctp_lastfaddr == NULL);
5381676Sjpk 		/* only element on list; first and last are same */
5391676Sjpk 		sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr;
5401676Sjpk 	} else if (first) {
5411676Sjpk 		ASSERT(sctp->sctp_lastfaddr != NULL);
5421676Sjpk 		faddr->next = sctp->sctp_faddrs;
5431676Sjpk 		sctp->sctp_faddrs = faddr;
5440Sstevel@tonic-gate 	} else {
5451676Sjpk 		sctp->sctp_lastfaddr->next = faddr;
5461676Sjpk 		sctp->sctp_lastfaddr = faddr;
5470Sstevel@tonic-gate 	}
548852Svi117747 	sctp->sctp_nfaddrs++;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	return (0);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate sctp_faddr_t *
5540Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	sctp_faddr_t *fp;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5590Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr))
5600Sstevel@tonic-gate 			break;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	return (fp);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate sctp_faddr_t *
5670Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate 	for (; fp; fp = fp->next) {
5700Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) {
5710Sstevel@tonic-gate 			break;
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	return (fp);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5781735Skcpoon /*
5791735Skcpoon  * To change the currently used peer address to the specified one.
5801735Skcpoon  */
5810Sstevel@tonic-gate void
5821735Skcpoon sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp)
5830Sstevel@tonic-gate {
5841735Skcpoon 	/* Now setup the composite header. */
5850Sstevel@tonic-gate 	if (fp->isv4) {
5860Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
5870Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
5880Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src);
5890Sstevel@tonic-gate 		/* update don't fragment bit */
5900Sstevel@tonic-gate 		if (fp->df) {
5910Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
5920Sstevel@tonic-gate 			    htons(IPH_DF);
5930Sstevel@tonic-gate 		} else {
5940Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 	} else {
5970Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = fp->faddr;
5980Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = fp->saddr;
5990Sstevel@tonic-gate 	}
6001735Skcpoon 
6011735Skcpoon 	sctp->sctp_current = fp;
6021735Skcpoon 	sctp->sctp_mss = fp->sfa_pmss;
6031735Skcpoon 
6041735Skcpoon 	/* Update the uppper layer for the change. */
6051735Skcpoon 	if (!SCTP_IS_DETACHED(sctp))
6061735Skcpoon 		sctp_set_ulp_prop(sctp);
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate void
6100Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	sctp_faddr_t *fp;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
61511042SErik.Nordmark@Sun.COM 		sctp_get_dest(sctp, fp);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate void
6200Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
6210Sstevel@tonic-gate {
62211066Srafael.vanoni@sun.com 	int64_t now = ddi_get_lbolt64();
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	fp->strikes = 0;
6250Sstevel@tonic-gate 	sctp->sctp_strikes = 0;
6260Sstevel@tonic-gate 	fp->lastactive = now;
6270Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
6280Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
6290Sstevel@tonic-gate 	if (fp->state != SCTP_FADDRS_ALIVE) {
6300Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_ALIVE;
6310Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0);
6324818Skcpoon 		/* Should have a full IRE now */
63311042SErik.Nordmark@Sun.COM 		sctp_get_dest(sctp, fp);
6340Sstevel@tonic-gate 
6351735Skcpoon 		/*
6361735Skcpoon 		 * If this is the primary, switch back to it now.  And
6371735Skcpoon 		 * we probably want to reset the source addr used to reach
6381735Skcpoon 		 * it.
63911042SErik.Nordmark@Sun.COM 		 * Note that if we didn't find a source in sctp_get_dest
64011042SErik.Nordmark@Sun.COM 		 * then we'd be unreachable at this point in time.
6411735Skcpoon 		 */
64211042SErik.Nordmark@Sun.COM 		if (fp == sctp->sctp_primary &&
64311042SErik.Nordmark@Sun.COM 		    fp->state != SCTP_FADDRS_UNREACH) {
6441735Skcpoon 			sctp_set_faddr_current(sctp, fp);
6451735Skcpoon 			return;
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate int
6510Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	sctp_faddr_t *fp;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
6560Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) {
6570Sstevel@tonic-gate 			return (1);
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	return (0);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate  * Returns 0 if there is at leave one other active faddr, -1 if there
6660Sstevel@tonic-gate  * are none. If there are none left, faddr_dead() will start killing the
6670Sstevel@tonic-gate  * association.
6680Sstevel@tonic-gate  * If the downed faddr was the current faddr, a new current faddr
6690Sstevel@tonic-gate  * will be chosen.
6700Sstevel@tonic-gate  */
6710Sstevel@tonic-gate int
6720Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate 	sctp_faddr_t *ofp;
6753448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_ALIVE) {
6780Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0);
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 	fp->state = newstate;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
6830Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), newstate));
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
6860Sstevel@tonic-gate 		/* Current faddr down; need to switch it */
6870Sstevel@tonic-gate 		sctp->sctp_current = NULL;
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/* Find next alive faddr */
6910Sstevel@tonic-gate 	ofp = fp;
6921735Skcpoon 	for (fp = fp->next; fp != NULL; fp = fp->next) {
6930Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE) {
6940Sstevel@tonic-gate 			break;
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if (fp == NULL) {
6990Sstevel@tonic-gate 		/* Continue from beginning of list */
7000Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) {
7010Sstevel@tonic-gate 			if (fp->state == SCTP_FADDRS_ALIVE) {
7020Sstevel@tonic-gate 				break;
7030Sstevel@tonic-gate 			}
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7071735Skcpoon 	/*
7081735Skcpoon 	 * Find a new fp, so if the current faddr is dead, use the new fp
7091735Skcpoon 	 * as the current one.
7101735Skcpoon 	 */
7110Sstevel@tonic-gate 	if (fp != ofp) {
7120Sstevel@tonic-gate 		if (sctp->sctp_current == NULL) {
7130Sstevel@tonic-gate 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
7140Sstevel@tonic-gate 			    SCTP_PRINTADDR(fp->faddr)));
7151735Skcpoon 			/*
7161735Skcpoon 			 * Note that we don't need to reset the source addr
7171735Skcpoon 			 * of the new fp.
7181735Skcpoon 			 */
7191735Skcpoon 			sctp_set_faddr_current(sctp, fp);
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 		return (0);
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/* All faddrs are down; kill the association */
7260Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
7273448Sdh155122 	BUMP_MIB(&sctps->sctps_mib, sctpAborted);
7280Sstevel@tonic-gate 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
7290Sstevel@tonic-gate 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
7300Sstevel@tonic-gate 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
7310Sstevel@tonic-gate 	    sctp->sctp_client_errno : ETIMEDOUT);
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	return (-1);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate sctp_faddr_t *
7370Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	sctp_faddr_t *nfp = NULL;
740*11908SGeorge.Shepherd@Sun.COM 	sctp_faddr_t *saved_fp = NULL;
741*11908SGeorge.Shepherd@Sun.COM 	int min_strikes;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (ofp == NULL) {
7440Sstevel@tonic-gate 		ofp = sctp->sctp_current;
7450Sstevel@tonic-gate 	}
746*11908SGeorge.Shepherd@Sun.COM 	/* Nothing to do */
747*11908SGeorge.Shepherd@Sun.COM 	if (sctp->sctp_nfaddrs < 2)
748*11908SGeorge.Shepherd@Sun.COM 		return (ofp);
7490Sstevel@tonic-gate 
750*11908SGeorge.Shepherd@Sun.COM 	min_strikes = ofp->strikes;
751*11908SGeorge.Shepherd@Sun.COM 
752*11908SGeorge.Shepherd@Sun.COM 	/*
753*11908SGeorge.Shepherd@Sun.COM 	 * Find the next live peer address with zero strikes. In case
754*11908SGeorge.Shepherd@Sun.COM 	 * there is none, find the one with the lowest number of strikes.
755*11908SGeorge.Shepherd@Sun.COM 	 */
756*11908SGeorge.Shepherd@Sun.COM 	for (nfp = ofp->next; nfp != ofp; nfp = nfp->next) {
757*11908SGeorge.Shepherd@Sun.COM 		/* If reached end of list, continue scan from the head */
758*11908SGeorge.Shepherd@Sun.COM 		if (nfp == NULL) {
759*11908SGeorge.Shepherd@Sun.COM 			nfp = sctp->sctp_faddrs;
760*11908SGeorge.Shepherd@Sun.COM 			continue;
7610Sstevel@tonic-gate 		}
762*11908SGeorge.Shepherd@Sun.COM 		if (nfp->state == SCTP_FADDRS_ALIVE) {
763*11908SGeorge.Shepherd@Sun.COM 			if (nfp->strikes == 0)
7640Sstevel@tonic-gate 				break;
765*11908SGeorge.Shepherd@Sun.COM 			if (nfp->strikes < min_strikes) {
766*11908SGeorge.Shepherd@Sun.COM 				min_strikes = nfp->strikes;
767*11908SGeorge.Shepherd@Sun.COM 				saved_fp = nfp;
7680Sstevel@tonic-gate 			}
7690Sstevel@tonic-gate 		}
7700Sstevel@tonic-gate 	}
771*11908SGeorge.Shepherd@Sun.COM 	/* If reached the old address, there is no zero strike path */
772*11908SGeorge.Shepherd@Sun.COM 	if (nfp == ofp)
773*11908SGeorge.Shepherd@Sun.COM 		nfp = NULL;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
776*11908SGeorge.Shepherd@Sun.COM 	 * If there is a peer address with zero strikes  we use that, if not
777*11908SGeorge.Shepherd@Sun.COM 	 * return a peer address with fewer strikes than the one last used,
778*11908SGeorge.Shepherd@Sun.COM 	 * if neither exist we may as well stay with the old one.
7790Sstevel@tonic-gate 	 */
780*11908SGeorge.Shepherd@Sun.COM 	if (nfp != NULL)
781*11908SGeorge.Shepherd@Sun.COM 		return (nfp);
782*11908SGeorge.Shepherd@Sun.COM 	if (saved_fp != NULL)
783*11908SGeorge.Shepherd@Sun.COM 		return (saved_fp);
784*11908SGeorge.Shepherd@Sun.COM 	return (ofp);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate void
7880Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 	sctp_faddr_t *fpp;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (!sctp->sctp_faddrs) {
7930Sstevel@tonic-gate 		return;
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if (fp->timer_mp != NULL) {
7970Sstevel@tonic-gate 		sctp_timer_free(fp->timer_mp);
7980Sstevel@tonic-gate 		fp->timer_mp = NULL;
7990Sstevel@tonic-gate 		fp->timer_running = 0;
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 	if (fp->rc_timer_mp != NULL) {
8020Sstevel@tonic-gate 		sctp_timer_free(fp->rc_timer_mp);
8030Sstevel@tonic-gate 		fp->rc_timer_mp = NULL;
8040Sstevel@tonic-gate 		fp->rc_timer_running = 0;
8050Sstevel@tonic-gate 	}
80611042SErik.Nordmark@Sun.COM 	if (fp->ixa != NULL) {
80711042SErik.Nordmark@Sun.COM 		ixa_refrele(fp->ixa);
80811042SErik.Nordmark@Sun.COM 		fp->ixa = NULL;
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8120Sstevel@tonic-gate 		goto gotit;
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next)
8160Sstevel@tonic-gate 		;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate gotit:
8190Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
8200Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8210Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8220Sstevel@tonic-gate 		sctp->sctp_faddrs = fp->next;
8230Sstevel@tonic-gate 	} else {
8240Sstevel@tonic-gate 		fpp->next = fp->next;
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8270Sstevel@tonic-gate 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
828852Svi117747 	sctp->sctp_nfaddrs--;
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate void
8320Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpn;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL) {
8370Sstevel@tonic-gate 		ASSERT(sctp->sctp_lastfaddr == NULL);
8380Sstevel@tonic-gate 		return;
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr != NULL);
8420Sstevel@tonic-gate 	sctp->sctp_lastfaddr = NULL;
8430Sstevel@tonic-gate 	sctp->sctp_current = NULL;
8440Sstevel@tonic-gate 	sctp->sctp_primary = NULL;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8490Sstevel@tonic-gate 		/* in conn fanout; need to hold lock */
8500Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
8540Sstevel@tonic-gate 		fpn = fp->next;
85511042SErik.Nordmark@Sun.COM 		if (fp->ixa != NULL) {
85611042SErik.Nordmark@Sun.COM 			ixa_refrele(fp->ixa);
85711042SErik.Nordmark@Sun.COM 			fp->ixa = NULL;
85811042SErik.Nordmark@Sun.COM 		}
8590Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
860852Svi117747 		sctp->sctp_nfaddrs--;
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	sctp->sctp_faddrs = NULL;
864852Svi117747 	ASSERT(sctp->sctp_nfaddrs == 0);
8650Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8660Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate void
8720Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	sctp_zap_faddrs(sctp, 0);
8750Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate /*
87911042SErik.Nordmark@Sun.COM  * Build two SCTP header templates; one for IPv4 and one for IPv6.
88011042SErik.Nordmark@Sun.COM  * Store them in sctp_iphc and sctp_iphc6 respectively (and related fields).
88111042SErik.Nordmark@Sun.COM  * There are no IP addresses in the templates, but the port numbers and
88211042SErik.Nordmark@Sun.COM  * verifier are field in from the conn_t and sctp_t.
88311042SErik.Nordmark@Sun.COM  *
88411042SErik.Nordmark@Sun.COM  * Returns failure if can't allocate memory, or if there is a problem
88511042SErik.Nordmark@Sun.COM  * with a routing header/option.
88611042SErik.Nordmark@Sun.COM  *
88711042SErik.Nordmark@Sun.COM  * We allocate space for the minimum sctp header (sctp_hdr_t).
88811042SErik.Nordmark@Sun.COM  *
88911042SErik.Nordmark@Sun.COM  * We massage an routing option/header. There is no checksum implication
89011042SErik.Nordmark@Sun.COM  * for a routing header for sctp.
89111042SErik.Nordmark@Sun.COM  *
89211042SErik.Nordmark@Sun.COM  * Caller needs to update conn_wroff if desired.
89311042SErik.Nordmark@Sun.COM  *
89411042SErik.Nordmark@Sun.COM  * TSol notes: This assumes that a SCTP association has a single peer label
89511042SErik.Nordmark@Sun.COM  * since we only track a single pair of ipp_label_v4/v6 and not a separate one
89611042SErik.Nordmark@Sun.COM  * for each faddr.
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate int
89911042SErik.Nordmark@Sun.COM sctp_build_hdrs(sctp_t *sctp, int sleep)
9000Sstevel@tonic-gate {
90111042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
90211042SErik.Nordmark@Sun.COM 	ip_pkt_t	*ipp = &connp->conn_xmit_ipp;
90311042SErik.Nordmark@Sun.COM 	uint_t		ip_hdr_length;
90411042SErik.Nordmark@Sun.COM 	uchar_t		*hdrs;
90511042SErik.Nordmark@Sun.COM 	uint_t		hdrs_len;
90611042SErik.Nordmark@Sun.COM 	uint_t		ulp_hdr_length = sizeof (sctp_hdr_t);
90711042SErik.Nordmark@Sun.COM 	ipha_t		*ipha;
90811042SErik.Nordmark@Sun.COM 	ip6_t		*ip6h;
9090Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
91011042SErik.Nordmark@Sun.COM 	in6_addr_t	v6src, v6dst;
91111042SErik.Nordmark@Sun.COM 	ipaddr_t	v4src, v4dst;
9120Sstevel@tonic-gate 
91311042SErik.Nordmark@Sun.COM 	v4src = connp->conn_saddr_v4;
91411042SErik.Nordmark@Sun.COM 	v4dst = connp->conn_faddr_v4;
91511042SErik.Nordmark@Sun.COM 	v6src = connp->conn_saddr_v6;
91611042SErik.Nordmark@Sun.COM 	v6dst = connp->conn_faddr_v6;
9170Sstevel@tonic-gate 
91811042SErik.Nordmark@Sun.COM 	/* First do IPv4 header */
91911042SErik.Nordmark@Sun.COM 	ip_hdr_length = ip_total_hdrs_len_v4(ipp);
9200Sstevel@tonic-gate 
92111042SErik.Nordmark@Sun.COM 	/* In case of TX label and IP options it can be too much */
92211042SErik.Nordmark@Sun.COM 	if (ip_hdr_length > IP_MAX_HDR_LENGTH) {
92311042SErik.Nordmark@Sun.COM 		/* Preserves existing TX errno for this */
92411042SErik.Nordmark@Sun.COM 		return (EHOSTUNREACH);
92511042SErik.Nordmark@Sun.COM 	}
92611042SErik.Nordmark@Sun.COM 	hdrs_len = ip_hdr_length + ulp_hdr_length;
92711042SErik.Nordmark@Sun.COM 	ASSERT(hdrs_len != 0);
9281676Sjpk 
92911042SErik.Nordmark@Sun.COM 	if (hdrs_len != sctp->sctp_iphc_len) {
93011042SErik.Nordmark@Sun.COM 		/* Allocate new before we free any old */
93111042SErik.Nordmark@Sun.COM 		hdrs = kmem_alloc(hdrs_len, sleep);
9320Sstevel@tonic-gate 		if (hdrs == NULL)
9330Sstevel@tonic-gate 			return (ENOMEM);
9340Sstevel@tonic-gate 
93511042SErik.Nordmark@Sun.COM 		if (sctp->sctp_iphc != NULL)
93611042SErik.Nordmark@Sun.COM 			kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
93711042SErik.Nordmark@Sun.COM 		sctp->sctp_iphc = hdrs;
93811042SErik.Nordmark@Sun.COM 		sctp->sctp_iphc_len = hdrs_len;
93911042SErik.Nordmark@Sun.COM 	} else {
94011042SErik.Nordmark@Sun.COM 		hdrs = sctp->sctp_iphc;
94111042SErik.Nordmark@Sun.COM 	}
94211042SErik.Nordmark@Sun.COM 	sctp->sctp_hdr_len = sctp->sctp_iphc_len;
94311042SErik.Nordmark@Sun.COM 	sctp->sctp_ip_hdr_len = ip_hdr_length;
94411042SErik.Nordmark@Sun.COM 
94511042SErik.Nordmark@Sun.COM 	sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length);
94611042SErik.Nordmark@Sun.COM 	sctp->sctp_sctph = sctph;
94711042SErik.Nordmark@Sun.COM 	sctph->sh_sport = connp->conn_lport;
94811042SErik.Nordmark@Sun.COM 	sctph->sh_dport = connp->conn_fport;
94911042SErik.Nordmark@Sun.COM 	sctph->sh_verf = sctp->sctp_fvtag;
95011042SErik.Nordmark@Sun.COM 	sctph->sh_chksum = 0;
95111042SErik.Nordmark@Sun.COM 
95211042SErik.Nordmark@Sun.COM 	ipha = (ipha_t *)hdrs;
95311042SErik.Nordmark@Sun.COM 	sctp->sctp_ipha = ipha;
95411042SErik.Nordmark@Sun.COM 
95511042SErik.Nordmark@Sun.COM 	ipha->ipha_src = v4src;
95611042SErik.Nordmark@Sun.COM 	ipha->ipha_dst = v4dst;
95711042SErik.Nordmark@Sun.COM 	ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, connp->conn_proto);
95811042SErik.Nordmark@Sun.COM 	ipha->ipha_length = htons(hdrs_len);
95911042SErik.Nordmark@Sun.COM 	ipha->ipha_fragment_offset_and_flags = 0;
96011042SErik.Nordmark@Sun.COM 
96111042SErik.Nordmark@Sun.COM 	if (ipp->ipp_fields & IPPF_IPV4_OPTIONS)
96211042SErik.Nordmark@Sun.COM 		(void) ip_massage_options(ipha, connp->conn_netstack);
96311042SErik.Nordmark@Sun.COM 
96411042SErik.Nordmark@Sun.COM 	/* Now IPv6 */
96511042SErik.Nordmark@Sun.COM 	ip_hdr_length = ip_total_hdrs_len_v6(ipp);
96611042SErik.Nordmark@Sun.COM 	hdrs_len = ip_hdr_length + ulp_hdr_length;
96711042SErik.Nordmark@Sun.COM 	ASSERT(hdrs_len != 0);
96811042SErik.Nordmark@Sun.COM 
96911042SErik.Nordmark@Sun.COM 	if (hdrs_len != sctp->sctp_iphc6_len) {
97011042SErik.Nordmark@Sun.COM 		/* Allocate new before we free any old */
97111042SErik.Nordmark@Sun.COM 		hdrs = kmem_alloc(hdrs_len, sleep);
97211042SErik.Nordmark@Sun.COM 		if (hdrs == NULL)
97311042SErik.Nordmark@Sun.COM 			return (ENOMEM);
97411042SErik.Nordmark@Sun.COM 
97511042SErik.Nordmark@Sun.COM 		if (sctp->sctp_iphc6 != NULL)
9760Sstevel@tonic-gate 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
9770Sstevel@tonic-gate 		sctp->sctp_iphc6 = hdrs;
9780Sstevel@tonic-gate 		sctp->sctp_iphc6_len = hdrs_len;
97911042SErik.Nordmark@Sun.COM 	} else {
98011042SErik.Nordmark@Sun.COM 		hdrs = sctp->sctp_iphc6;
9810Sstevel@tonic-gate 	}
98211042SErik.Nordmark@Sun.COM 	sctp->sctp_hdr6_len = sctp->sctp_iphc6_len;
98311042SErik.Nordmark@Sun.COM 	sctp->sctp_ip_hdr6_len = ip_hdr_length;
9840Sstevel@tonic-gate 
98511042SErik.Nordmark@Sun.COM 	sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length);
98611042SErik.Nordmark@Sun.COM 	sctp->sctp_sctph6 = sctph;
98711042SErik.Nordmark@Sun.COM 	sctph->sh_sport = connp->conn_lport;
98811042SErik.Nordmark@Sun.COM 	sctph->sh_dport = connp->conn_fport;
98911042SErik.Nordmark@Sun.COM 	sctph->sh_verf = sctp->sctp_fvtag;
99011042SErik.Nordmark@Sun.COM 	sctph->sh_chksum = 0;
9910Sstevel@tonic-gate 
99211042SErik.Nordmark@Sun.COM 	ip6h = (ip6_t *)hdrs;
99311042SErik.Nordmark@Sun.COM 	sctp->sctp_ip6h = ip6h;
9940Sstevel@tonic-gate 
99511042SErik.Nordmark@Sun.COM 	ip6h->ip6_src = v6src;
99611042SErik.Nordmark@Sun.COM 	ip6h->ip6_dst = v6dst;
99711042SErik.Nordmark@Sun.COM 	ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, connp->conn_proto,
99811042SErik.Nordmark@Sun.COM 	    connp->conn_flowinfo);
99911042SErik.Nordmark@Sun.COM 	ip6h->ip6_plen = htons(hdrs_len - IPV6_HDR_LEN);
10000Sstevel@tonic-gate 
100111042SErik.Nordmark@Sun.COM 	if (ipp->ipp_fields & IPPF_RTHDR) {
100211042SErik.Nordmark@Sun.COM 		uint8_t		*end;
100311042SErik.Nordmark@Sun.COM 		ip6_rthdr_t	*rth;
10040Sstevel@tonic-gate 
100511042SErik.Nordmark@Sun.COM 		end = (uint8_t *)ip6h + ip_hdr_length;
100611042SErik.Nordmark@Sun.COM 		rth = ip_find_rthdr_v6(ip6h, end);
100711042SErik.Nordmark@Sun.COM 		if (rth != NULL) {
100811042SErik.Nordmark@Sun.COM 			(void) ip_massage_options_v6(ip6h, rth,
100911042SErik.Nordmark@Sun.COM 			    connp->conn_netstack);
101011042SErik.Nordmark@Sun.COM 		}
10111676Sjpk 
101211042SErik.Nordmark@Sun.COM 		/*
101311042SErik.Nordmark@Sun.COM 		 * Verify that the first hop isn't a mapped address.
101411042SErik.Nordmark@Sun.COM 		 * Routers along the path need to do this verification
101511042SErik.Nordmark@Sun.COM 		 * for subsequent hops.
101611042SErik.Nordmark@Sun.COM 		 */
101711042SErik.Nordmark@Sun.COM 		if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst))
101811042SErik.Nordmark@Sun.COM 			return (EADDRNOTAVAIL);
10191676Sjpk 	}
10201676Sjpk 	return (0);
10211676Sjpk }
10221676Sjpk 
10231676Sjpk static int
102411042SErik.Nordmark@Sun.COM sctp_v4_label(sctp_t *sctp, sctp_faddr_t *fp)
10251676Sjpk {
102611042SErik.Nordmark@Sun.COM 	conn_t *connp = sctp->sctp_connp;
102711042SErik.Nordmark@Sun.COM 
102811042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa->ixa_flags & IXAF_IS_IPV4);
102911042SErik.Nordmark@Sun.COM 	return (conn_update_label(connp, fp->ixa, &fp->faddr,
103011042SErik.Nordmark@Sun.COM 	    &connp->conn_xmit_ipp));
103111042SErik.Nordmark@Sun.COM }
10321676Sjpk 
103311042SErik.Nordmark@Sun.COM static int
103411042SErik.Nordmark@Sun.COM sctp_v6_label(sctp_t *sctp, sctp_faddr_t *fp)
103511042SErik.Nordmark@Sun.COM {
103611042SErik.Nordmark@Sun.COM 	conn_t *connp = sctp->sctp_connp;
103711042SErik.Nordmark@Sun.COM 
103811042SErik.Nordmark@Sun.COM 	ASSERT(!(fp->ixa->ixa_flags & IXAF_IS_IPV4));
103911042SErik.Nordmark@Sun.COM 	return (conn_update_label(connp, fp->ixa, &fp->faddr,
104011042SErik.Nordmark@Sun.COM 	    &connp->conn_xmit_ipp));
10411676Sjpk }
10421676Sjpk 
10430Sstevel@tonic-gate /*
10440Sstevel@tonic-gate  * XXX implement more sophisticated logic
104511042SErik.Nordmark@Sun.COM  *
104611042SErik.Nordmark@Sun.COM  * Tsol note: We have already verified the addresses using tsol_check_dest
104711042SErik.Nordmark@Sun.COM  * in sctp_add_faddr, thus no need to redo that here.
104811042SErik.Nordmark@Sun.COM  * We do setup ipp_label_v4 and ipp_label_v6 based on which addresses
104911042SErik.Nordmark@Sun.COM  * we have.
10500Sstevel@tonic-gate  */
10511676Sjpk int
10521735Skcpoon sctp_set_hdraddrs(sctp_t *sctp)
10530Sstevel@tonic-gate {
10540Sstevel@tonic-gate 	sctp_faddr_t *fp;
10550Sstevel@tonic-gate 	int gotv4 = 0;
10560Sstevel@tonic-gate 	int gotv6 = 0;
105711042SErik.Nordmark@Sun.COM 	conn_t *connp = sctp->sctp_connp;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs != NULL);
10600Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	/* Set up using the primary first */
106311042SErik.Nordmark@Sun.COM 	connp->conn_faddr_v6 = sctp->sctp_primary->faddr;
106411042SErik.Nordmark@Sun.COM 	/* saddr may be unspec; make_mp() will handle this */
106511042SErik.Nordmark@Sun.COM 	connp->conn_saddr_v6 = sctp->sctp_primary->saddr;
106611042SErik.Nordmark@Sun.COM 	connp->conn_laddr_v6 = connp->conn_saddr_v6;
10670Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) {
106811042SErik.Nordmark@Sun.COM 		if (!is_system_labeled() ||
106911042SErik.Nordmark@Sun.COM 		    sctp_v4_label(sctp, sctp->sctp_primary) == 0) {
10701676Sjpk 			gotv4 = 1;
107111042SErik.Nordmark@Sun.COM 			if (connp->conn_family == AF_INET) {
107211042SErik.Nordmark@Sun.COM 				goto done;
10731676Sjpk 			}
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 	} else {
107611042SErik.Nordmark@Sun.COM 		if (!is_system_labeled() ||
107711042SErik.Nordmark@Sun.COM 		    sctp_v6_label(sctp, sctp->sctp_primary) == 0) {
10781676Sjpk 			gotv6 = 1;
107911042SErik.Nordmark@Sun.COM 		}
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
10830Sstevel@tonic-gate 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
108411042SErik.Nordmark@Sun.COM 			if (!is_system_labeled() ||
108511042SErik.Nordmark@Sun.COM 			    sctp_v4_label(sctp, fp) == 0) {
10861676Sjpk 				gotv4 = 1;
108711042SErik.Nordmark@Sun.COM 				if (connp->conn_family == AF_INET || gotv6) {
10881676Sjpk 					break;
10891676Sjpk 				}
10900Sstevel@tonic-gate 			}
10912283Skp158701 		} else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
109211042SErik.Nordmark@Sun.COM 			if (!is_system_labeled() ||
109311042SErik.Nordmark@Sun.COM 			    sctp_v6_label(sctp, fp) == 0) {
10941676Sjpk 				gotv6 = 1;
10951676Sjpk 				if (gotv4)
10961676Sjpk 					break;
10970Sstevel@tonic-gate 			}
10980Sstevel@tonic-gate 		}
10990Sstevel@tonic-gate 	}
11000Sstevel@tonic-gate 
110111042SErik.Nordmark@Sun.COM done:
11021676Sjpk 	if (!gotv4 && !gotv6)
11031676Sjpk 		return (EACCES);
11041676Sjpk 
11051676Sjpk 	return (0);
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate 
11088549SGeorge.Shepherd@Sun.COM /*
11098549SGeorge.Shepherd@Sun.COM  * got_errchunk is set B_TRUE only if called from validate_init_params(), when
11108549SGeorge.Shepherd@Sun.COM  * an ERROR chunk is already prepended the size of which needs updating for
11118549SGeorge.Shepherd@Sun.COM  * additional unrecognized parameters. Other callers either prepend the ERROR
11128549SGeorge.Shepherd@Sun.COM  * chunk with the correct size after calling this function, or they are calling
11138549SGeorge.Shepherd@Sun.COM  * to add an invalid parameter to an INIT_ACK chunk, in that case no ERROR chunk
11148549SGeorge.Shepherd@Sun.COM  * exists, the CAUSE blocks go into the INIT_ACK directly.
11158549SGeorge.Shepherd@Sun.COM  *
11168549SGeorge.Shepherd@Sun.COM  * *errmp will be non-NULL both when adding an additional CAUSE block to an
11178549SGeorge.Shepherd@Sun.COM  * existing prepended COOKIE ERROR chunk (processing params of an INIT_ACK),
11188549SGeorge.Shepherd@Sun.COM  * and when adding unrecognized parameters after the first, to an INIT_ACK
11198549SGeorge.Shepherd@Sun.COM  * (processing params of an INIT chunk).
11208549SGeorge.Shepherd@Sun.COM  */
11210Sstevel@tonic-gate void
11228549SGeorge.Shepherd@Sun.COM sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp,
11238549SGeorge.Shepherd@Sun.COM     boolean_t got_errchunk)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	mblk_t *mp;
11260Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
11270Sstevel@tonic-gate 	size_t len;
11280Sstevel@tonic-gate 	int pad;
11298153SGeorge.Shepherd@Sun.COM 	sctp_chunk_hdr_t *ecp;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	len = sizeof (*ph) + ntohs(uph->sph_len);
11328153SGeorge.Shepherd@Sun.COM 	if ((pad = len % SCTP_ALIGN) != 0) {
11338153SGeorge.Shepherd@Sun.COM 		pad = SCTP_ALIGN - pad;
11340Sstevel@tonic-gate 		len += pad;
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
11370Sstevel@tonic-gate 	if (mp == NULL) {
11380Sstevel@tonic-gate 		return;
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
11420Sstevel@tonic-gate 	ph->sph_type = htons(PARM_UNRECOGNIZED);
11430Sstevel@tonic-gate 	ph->sph_len = htons(len - pad);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/* copy in the unrecognized parameter */
11460Sstevel@tonic-gate 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
11470Sstevel@tonic-gate 
11488153SGeorge.Shepherd@Sun.COM 	if (pad != 0)
11498153SGeorge.Shepherd@Sun.COM 		bzero((mp->b_rptr + len - pad), pad);
11508153SGeorge.Shepherd@Sun.COM 
11510Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
11520Sstevel@tonic-gate 	if (*errmp != NULL) {
11538153SGeorge.Shepherd@Sun.COM 		/*
11548549SGeorge.Shepherd@Sun.COM 		 * Update total length if an ERROR chunk, then link
11558549SGeorge.Shepherd@Sun.COM 		 * this CAUSE block to the possible chain of CAUSE
11568549SGeorge.Shepherd@Sun.COM 		 * blocks attached to the ERROR chunk or INIT_ACK
11578549SGeorge.Shepherd@Sun.COM 		 * being created.
11588153SGeorge.Shepherd@Sun.COM 		 */
11598549SGeorge.Shepherd@Sun.COM 		if (got_errchunk) {
11608549SGeorge.Shepherd@Sun.COM 			/* ERROR chunk already prepended */
11618549SGeorge.Shepherd@Sun.COM 			ecp = (sctp_chunk_hdr_t *)((*errmp)->b_rptr);
11628549SGeorge.Shepherd@Sun.COM 			ecp->sch_len = htons(ntohs(ecp->sch_len) + len);
11638549SGeorge.Shepherd@Sun.COM 		}
11640Sstevel@tonic-gate 		linkb(*errmp, mp);
11650Sstevel@tonic-gate 	} else {
11660Sstevel@tonic-gate 		*errmp = mp;
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate /*
11710Sstevel@tonic-gate  * o Bounds checking
11720Sstevel@tonic-gate  * o Updates remaining
11730Sstevel@tonic-gate  * o Checks alignment
11740Sstevel@tonic-gate  */
11750Sstevel@tonic-gate sctp_parm_hdr_t *
11760Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	int pad;
11790Sstevel@tonic-gate 	uint16_t len;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	len = ntohs(current->sph_len);
11820Sstevel@tonic-gate 	*remaining -= len;
11830Sstevel@tonic-gate 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
11840Sstevel@tonic-gate 		return (NULL);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
11870Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
11880Sstevel@tonic-gate 		*remaining -= pad;
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
11910Sstevel@tonic-gate 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
11920Sstevel@tonic-gate 	return (current);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate /*
11960Sstevel@tonic-gate  * Sets the address parameters given in the INIT chunk into sctp's
11970Sstevel@tonic-gate  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
11980Sstevel@tonic-gate  * no address parameters in the INIT chunk, a single faddr is created
11990Sstevel@tonic-gate  * from the ip hdr at the beginning of pkt.
12000Sstevel@tonic-gate  * If there already are existing addresses hanging from sctp, merge
12010Sstevel@tonic-gate  * them in, if the old info contains addresses which are not present
12020Sstevel@tonic-gate  * in this new info, get rid of them, and clean the pointers if there's
12030Sstevel@tonic-gate  * messages which have this as their target address.
12040Sstevel@tonic-gate  *
1205432Svi117747  * We also re-adjust the source address list here since the list may
1206432Svi117747  * contain more than what is actually part of the association. If
1207432Svi117747  * we get here from sctp_send_cookie_echo(), we are on the active
1208432Svi117747  * side and psctp will be NULL and ich will be the INIT-ACK chunk.
1209432Svi117747  * If we get here from sctp_accept_comm(), ich will be the INIT chunk
1210432Svi117747  * and psctp will the listening endpoint.
1211432Svi117747  *
1212432Svi117747  * INIT processing: When processing the INIT we inherit the src address
1213432Svi117747  * list from the listener. For a loopback or linklocal association, we
1214432Svi117747  * delete the list and just take the address from the IP header (since
1215432Svi117747  * that's how we created the INIT-ACK). Additionally, for loopback we
1216432Svi117747  * ignore the address params in the INIT. For determining which address
1217432Svi117747  * types were sent in the INIT-ACK we follow the same logic as in
1218432Svi117747  * creating the INIT-ACK. We delete addresses of the type that are not
1219432Svi117747  * supported by the peer.
1220432Svi117747  *
1221432Svi117747  * INIT-ACK processing: When processing the INIT-ACK since we had not
1222432Svi117747  * included addr params for loopback or linklocal addresses when creating
1223432Svi117747  * the INIT, we just use the address from the IP header. Further, for
1224432Svi117747  * loopback we ignore the addr param list. We mark addresses of the
1225432Svi117747  * type not supported by the peer as unconfirmed.
1226432Svi117747  *
1227432Svi117747  * In case of INIT processing we look for supported address types in the
1228432Svi117747  * supported address param, if present. In both cases the address type in
1229432Svi117747  * the IP header is supported as well as types for addresses in the param
1230432Svi117747  * list, if any.
1231432Svi117747  *
1232432Svi117747  * Once we have the supported address types sctp_check_saddr() runs through
1233432Svi117747  * the source address list and deletes or marks as unconfirmed address of
1234432Svi117747  * types not supported by the peer.
1235432Svi117747  *
12360Sstevel@tonic-gate  * Returns 0 on success, sys errno on failure
12370Sstevel@tonic-gate  */
12380Sstevel@tonic-gate int
12390Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
12400Sstevel@tonic-gate     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
12430Sstevel@tonic-gate 	ipha_t			*iph;
12440Sstevel@tonic-gate 	ip6_t			*ip6h;
1245432Svi117747 	in6_addr_t		hdrsaddr[1];
1246432Svi117747 	in6_addr_t		hdrdaddr[1];
12470Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
12480Sstevel@tonic-gate 	ssize_t			remaining;
12490Sstevel@tonic-gate 	int			isv4;
12500Sstevel@tonic-gate 	int			err;
12510Sstevel@tonic-gate 	sctp_faddr_t		*fp;
1252432Svi117747 	int			supp_af = 0;
1253432Svi117747 	boolean_t		check_saddr = B_TRUE;
1254852Svi117747 	in6_addr_t		curaddr;
12553448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
125611042SErik.Nordmark@Sun.COM 	conn_t			*connp = sctp->sctp_connp;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if (sctp_options != NULL)
12590Sstevel@tonic-gate 		*sctp_options = 0;
12600Sstevel@tonic-gate 
1261432Svi117747 	/* extract the address from the IP header */
1262432Svi117747 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1263432Svi117747 	if (isv4) {
1264432Svi117747 		iph = (ipha_t *)pkt->b_rptr;
1265432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr);
1266432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr);
1267432Svi117747 		supp_af |= PARM_SUPP_V4;
1268432Svi117747 	} else {
1269432Svi117747 		ip6h = (ip6_t *)pkt->b_rptr;
1270432Svi117747 		hdrsaddr[0] = ip6h->ip6_src;
1271432Svi117747 		hdrdaddr[0] = ip6h->ip6_dst;
1272432Svi117747 		supp_af |= PARM_SUPP_V6;
1273432Svi117747 	}
1274432Svi117747 
1275432Svi117747 	/*
1276432Svi117747 	 * Unfortunately, we can't delay this because adding an faddr
1277432Svi117747 	 * looks for the presence of the source address (from the ire
1278432Svi117747 	 * for the faddr) in the source address list. We could have
1279432Svi117747 	 * delayed this if, say, this was a loopback/linklocal connection.
1280432Svi117747 	 * Now, we just end up nuking this list and taking the addr from
1281432Svi117747 	 * the IP header for loopback/linklocal.
1282432Svi117747 	 */
12830Sstevel@tonic-gate 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
12840Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
12870Sstevel@tonic-gate 		if (err != 0)
12880Sstevel@tonic-gate 			return (err);
12890Sstevel@tonic-gate 	}
1290432Svi117747 	/*
1291432Svi117747 	 * We will add the faddr before parsing the address list as this
1292432Svi117747 	 * might be a loopback connection and we would not have to
1293432Svi117747 	 * go through the list.
1294432Svi117747 	 *
1295432Svi117747 	 * Make sure the header's addr is in the list
1296432Svi117747 	 */
1297432Svi117747 	fp = sctp_lookup_faddr(sctp, hdrsaddr);
1298432Svi117747 	if (fp == NULL) {
1299432Svi117747 		/* not included; add it now */
13001735Skcpoon 		err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE);
13011676Sjpk 		if (err != 0)
13021676Sjpk 			return (err);
13030Sstevel@tonic-gate 
1304432Svi117747 		/* sctp_faddrs will be the hdr addr */
1305432Svi117747 		fp = sctp->sctp_faddrs;
13060Sstevel@tonic-gate 	}
1307432Svi117747 	/* make the header addr the primary */
1308852Svi117747 
1309852Svi117747 	if (cl_sctp_assoc_change != NULL && psctp == NULL)
1310852Svi117747 		curaddr = sctp->sctp_current->faddr;
1311852Svi117747 
1312432Svi117747 	sctp->sctp_primary = fp;
1313432Svi117747 	sctp->sctp_current = fp;
1314432Svi117747 	sctp->sctp_mss = fp->sfa_pmss;
13150Sstevel@tonic-gate 
1316432Svi117747 	/* For loopback connections & linklocal get address from the header */
1317432Svi117747 	if (sctp->sctp_loopback || sctp->sctp_linklocal) {
1318432Svi117747 		if (sctp->sctp_nsaddrs != 0)
1319432Svi117747 			sctp_free_saddrs(sctp);
1320852Svi117747 		if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0)
1321432Svi117747 			return (err);
1322432Svi117747 		/* For loopback ignore address list */
1323432Svi117747 		if (sctp->sctp_loopback)
1324432Svi117747 			return (0);
1325432Svi117747 		check_saddr = B_FALSE;
1326432Svi117747 	}
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
13290Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
13300Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
13310Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
1332432Svi117747 		if (check_saddr) {
1333432Svi117747 			sctp_check_saddr(sctp, supp_af, psctp == NULL ?
13344818Skcpoon 			    B_FALSE : B_TRUE, hdrdaddr);
1335432Svi117747 		}
1336852Svi117747 		ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1337432Svi117747 		return (0);
13380Sstevel@tonic-gate 	}
1339432Svi117747 
13400Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
13410Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
13420Sstevel@tonic-gate 
1343432Svi117747 	/* params will have already been byteordered when validating */
13440Sstevel@tonic-gate 	while (ph != NULL) {
1345432Svi117747 		if (ph->sph_type == htons(PARM_SUPP_ADDRS)) {
1346432Svi117747 			int		plen;
1347432Svi117747 			uint16_t	*p;
1348432Svi117747 			uint16_t	addrtype;
1349432Svi117747 
1350432Svi117747 			ASSERT(psctp != NULL);
1351432Svi117747 			plen = ntohs(ph->sph_len);
1352432Svi117747 			p = (uint16_t *)(ph + 1);
1353432Svi117747 			while (plen > 0) {
1354432Svi117747 				addrtype = ntohs(*p);
1355432Svi117747 				switch (addrtype) {
1356432Svi117747 					case PARM_ADDR6:
1357432Svi117747 						supp_af |= PARM_SUPP_V6;
1358432Svi117747 						break;
1359432Svi117747 					case PARM_ADDR4:
1360432Svi117747 						supp_af |= PARM_SUPP_V4;
1361432Svi117747 						break;
1362432Svi117747 					default:
1363432Svi117747 						break;
1364432Svi117747 				}
1365432Svi117747 				p++;
1366432Svi117747 				plen -= sizeof (*p);
1367432Svi117747 			}
1368432Svi117747 		} else if (ph->sph_type == htons(PARM_ADDR4)) {
13690Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
13700Sstevel@tonic-gate 				in6_addr_t addr;
13710Sstevel@tonic-gate 				ipaddr_t ta;
13720Sstevel@tonic-gate 
1373432Svi117747 				supp_af |= PARM_SUPP_V4;
13740Sstevel@tonic-gate 				/*
13750Sstevel@tonic-gate 				 * Screen out broad/multicasts & loopback.
13760Sstevel@tonic-gate 				 * If the endpoint only accepts v6 address,
13770Sstevel@tonic-gate 				 * go to the next one.
13784818Skcpoon 				 *
13794818Skcpoon 				 * Subnet broadcast check is done in
13804818Skcpoon 				 * sctp_add_faddr().  If the address is
13814818Skcpoon 				 * a broadcast address, it won't be added.
13820Sstevel@tonic-gate 				 */
13830Sstevel@tonic-gate 				bcopy(ph + 1, &ta, sizeof (ta));
13840Sstevel@tonic-gate 				if (ta == 0 ||
13850Sstevel@tonic-gate 				    ta == INADDR_BROADCAST ||
13860Sstevel@tonic-gate 				    ta == htonl(INADDR_LOOPBACK) ||
138711042SErik.Nordmark@Sun.COM 				    CLASSD(ta) || connp->conn_ipv6_v6only) {
13880Sstevel@tonic-gate 					goto next;
13890Sstevel@tonic-gate 				}
13900Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
13910Sstevel@tonic-gate 				    (ph + 1), &addr);
13924818Skcpoon 
13930Sstevel@tonic-gate 				/* Check for duplicate. */
13940Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
13950Sstevel@tonic-gate 					goto next;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 				/* OK, add it to the faddr set */
13981735Skcpoon 				err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP,
13991735Skcpoon 				    B_FALSE);
14004818Skcpoon 				/* Something is wrong...  Try the next one. */
14011676Sjpk 				if (err != 0)
14024818Skcpoon 					goto next;
14030Sstevel@tonic-gate 			}
14040Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
140511042SErik.Nordmark@Sun.COM 		    connp->conn_family == AF_INET6) {
14060Sstevel@tonic-gate 			/* An v4 socket should not take v6 addresses. */
14070Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
14080Sstevel@tonic-gate 				in6_addr_t *addr6;
14090Sstevel@tonic-gate 
1410432Svi117747 				supp_af |= PARM_SUPP_V6;
14110Sstevel@tonic-gate 				addr6 = (in6_addr_t *)(ph + 1);
14120Sstevel@tonic-gate 				/*
14130Sstevel@tonic-gate 				 * Screen out link locals, mcast, loopback
14140Sstevel@tonic-gate 				 * and bogus v6 address.
14150Sstevel@tonic-gate 				 */
14160Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
14170Sstevel@tonic-gate 				    IN6_IS_ADDR_MULTICAST(addr6) ||
14180Sstevel@tonic-gate 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
14190Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
14200Sstevel@tonic-gate 					goto next;
14210Sstevel@tonic-gate 				}
14220Sstevel@tonic-gate 				/* Check for duplicate. */
14230Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
14240Sstevel@tonic-gate 					goto next;
14250Sstevel@tonic-gate 
14261676Sjpk 				err = sctp_add_faddr(sctp,
14271735Skcpoon 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP,
14281735Skcpoon 				    B_FALSE);
14294818Skcpoon 				/* Something is wrong...  Try the next one. */
14301676Sjpk 				if (err != 0)
14314818Skcpoon 					goto next;
14320Sstevel@tonic-gate 			}
14330Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
14340Sstevel@tonic-gate 			if (sctp_options != NULL)
14350Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
14360Sstevel@tonic-gate 		} /* else; skip */
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate next:
14390Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
14400Sstevel@tonic-gate 	}
1441432Svi117747 	if (check_saddr) {
1442432Svi117747 		sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE :
14434818Skcpoon 		    B_TRUE, hdrdaddr);
14440Sstevel@tonic-gate 	}
1445852Svi117747 	ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1446852Svi117747 	/*
1447852Svi117747 	 * We have the right address list now, update clustering's
1448852Svi117747 	 * knowledge because when we sent the INIT we had just added
1449852Svi117747 	 * the address the INIT was sent to.
1450852Svi117747 	 */
1451852Svi117747 	if (psctp == NULL && cl_sctp_assoc_change != NULL) {
1452852Svi117747 		uchar_t	*alist;
1453852Svi117747 		size_t	asize;
1454852Svi117747 		uchar_t	*dlist;
1455852Svi117747 		size_t	dsize;
1456852Svi117747 
1457852Svi117747 		asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
1458852Svi117747 		alist = kmem_alloc(asize, KM_NOSLEEP);
14591735Skcpoon 		if (alist == NULL) {
14603448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1461852Svi117747 			return (ENOMEM);
14621735Skcpoon 		}
1463852Svi117747 		/*
1464852Svi117747 		 * Just include the address the INIT was sent to in the
1465852Svi117747 		 * delete list and send the entire faddr list. We could
1466852Svi117747 		 * do it differently (i.e include all the addresses in the
1467852Svi117747 		 * add list even if it contains the original address OR
1468852Svi117747 		 * remove the original address from the add list etc.), but
1469852Svi117747 		 * this seems reasonable enough.
1470852Svi117747 		 */
1471852Svi117747 		dsize = sizeof (in6_addr_t);
1472852Svi117747 		dlist = kmem_alloc(dsize, KM_NOSLEEP);
1473852Svi117747 		if (dlist == NULL) {
1474852Svi117747 			kmem_free(alist, asize);
14753448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1476852Svi117747 			return (ENOMEM);
1477852Svi117747 		}
1478852Svi117747 		bcopy(&curaddr, dlist, sizeof (curaddr));
1479852Svi117747 		sctp_get_faddr_list(sctp, alist, asize);
148011042SErik.Nordmark@Sun.COM 		(*cl_sctp_assoc_change)(connp->conn_family, alist, asize,
1481852Svi117747 		    sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR,
1482852Svi117747 		    (cl_sctp_handle_t)sctp);
1483852Svi117747 		/* alist and dlist will be freed by the clustering module */
1484852Svi117747 	}
14850Sstevel@tonic-gate 	return (0);
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate /*
14890Sstevel@tonic-gate  * Returns 0 if the check failed and the restart should be refused,
14900Sstevel@tonic-gate  * 1 if the check succeeded.
14910Sstevel@tonic-gate  */
14920Sstevel@tonic-gate int
14930Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
149411042SErik.Nordmark@Sun.COM     int sleep, sctp_stack_t *sctps, ip_recv_attr_t *ira)
14950Sstevel@tonic-gate {
14964964Skcpoon 	sctp_faddr_t *fp, *fphead = NULL;
14970Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
14980Sstevel@tonic-gate 	ssize_t remaining;
14990Sstevel@tonic-gate 	int isv4;
15000Sstevel@tonic-gate 	ipha_t *iph;
15010Sstevel@tonic-gate 	ip6_t *ip6h;
15020Sstevel@tonic-gate 	in6_addr_t hdraddr[1];
15030Sstevel@tonic-gate 	int retval = 0;
15040Sstevel@tonic-gate 	sctp_tf_t *tf;
15050Sstevel@tonic-gate 	sctp_t *sctp;
15060Sstevel@tonic-gate 	int compres;
15070Sstevel@tonic-gate 	sctp_init_chunk_t *init;
15080Sstevel@tonic-gate 	int nadded = 0;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	/* extract the address from the IP header */
15110Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
15120Sstevel@tonic-gate 	if (isv4) {
15130Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
15140Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
15150Sstevel@tonic-gate 	} else {
15160Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
15170Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
15210Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
15220Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
15230Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
15240Sstevel@tonic-gate 		/* no parameters; restart OK */
15250Sstevel@tonic-gate 		return (1);
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
15280Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	while (ph != NULL) {
15314964Skcpoon 		sctp_faddr_t *fpa = NULL;
15324964Skcpoon 
15330Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
15340Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
15350Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
15360Sstevel@tonic-gate 				in6_addr_t addr;
15370Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
15380Sstevel@tonic-gate 				    (ph + 1), &addr);
15390Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15400Sstevel@tonic-gate 				    sleep);
15414964Skcpoon 				if (fpa == NULL) {
15420Sstevel@tonic-gate 					goto done;
15430Sstevel@tonic-gate 				}
15440Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
15450Sstevel@tonic-gate 				fpa->faddr = addr;
15460Sstevel@tonic-gate 				fpa->next = NULL;
15470Sstevel@tonic-gate 			}
15480Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
15490Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
15500Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15510Sstevel@tonic-gate 				    sleep);
15524964Skcpoon 				if (fpa == NULL) {
15530Sstevel@tonic-gate 					goto done;
15540Sstevel@tonic-gate 				}
15550Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
15560Sstevel@tonic-gate 				bcopy(ph + 1, &fpa->faddr,
15570Sstevel@tonic-gate 				    sizeof (fpa->faddr));
15580Sstevel@tonic-gate 				fpa->next = NULL;
15590Sstevel@tonic-gate 			}
15600Sstevel@tonic-gate 		}
15610Sstevel@tonic-gate 		/* link in the new addr, if it was an addr param */
15624964Skcpoon 		if (fpa != NULL) {
15634964Skcpoon 			if (fphead == NULL) {
15640Sstevel@tonic-gate 				fphead = fpa;
15650Sstevel@tonic-gate 			} else {
15664964Skcpoon 				fpa->next = fphead;
15674964Skcpoon 				fphead = fpa;
15680Sstevel@tonic-gate 			}
15690Sstevel@tonic-gate 		}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	if (fphead == NULL) {
15750Sstevel@tonic-gate 		/* no addr parameters; restart OK */
15760Sstevel@tonic-gate 		return (1);
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	/*
15800Sstevel@tonic-gate 	 * got at least one; make sure the header's addr is
15810Sstevel@tonic-gate 	 * in the list
15820Sstevel@tonic-gate 	 */
15830Sstevel@tonic-gate 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
15844964Skcpoon 	if (fp == NULL) {
15850Sstevel@tonic-gate 		/* not included; add it now */
15860Sstevel@tonic-gate 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
15874964Skcpoon 		if (fp == NULL) {
15880Sstevel@tonic-gate 			goto done;
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 		bzero(fp, sizeof (*fp));
15910Sstevel@tonic-gate 		fp->faddr = *hdraddr;
15920Sstevel@tonic-gate 		fp->next = fphead;
15930Sstevel@tonic-gate 		fphead = fp;
15940Sstevel@tonic-gate 	}
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	/*
15970Sstevel@tonic-gate 	 * Now, we can finally do the check: For each sctp instance
15980Sstevel@tonic-gate 	 * on the hash line for ports, compare its faddr set against
15990Sstevel@tonic-gate 	 * the new one. If the new one is a strict subset of any
16000Sstevel@tonic-gate 	 * existing sctp's faddrs, the restart is OK. However, if there
16010Sstevel@tonic-gate 	 * is an overlap, this could be an attack, so return failure.
16020Sstevel@tonic-gate 	 * If all sctp's faddrs are disjoint, this is a legitimate new
16030Sstevel@tonic-gate 	 * association.
16040Sstevel@tonic-gate 	 */
16053448Sdh155122 	tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]);
16060Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
160911042SErik.Nordmark@Sun.COM 		if (ports != sctp->sctp_connp->conn_ports) {
16100Sstevel@tonic-gate 			continue;
16110Sstevel@tonic-gate 		}
16120Sstevel@tonic-gate 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
16130Sstevel@tonic-gate 		if (compres <= SCTP_ADDR_SUBSET) {
16140Sstevel@tonic-gate 			retval = 1;
16150Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16160Sstevel@tonic-gate 			goto done;
16170Sstevel@tonic-gate 		}
16180Sstevel@tonic-gate 		if (compres == SCTP_ADDR_OVERLAP) {
16190Sstevel@tonic-gate 			dprint(1,
16200Sstevel@tonic-gate 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
16211676Sjpk 			    SCTP_PRINTADDR(*hdraddr), (void *)sctp));
16220Sstevel@tonic-gate 			/*
16230Sstevel@tonic-gate 			 * While we still hold the lock, we need to
16240Sstevel@tonic-gate 			 * figure out which addresses have been
16250Sstevel@tonic-gate 			 * added so we can include them in the abort
16260Sstevel@tonic-gate 			 * we will send back. Since these faddrs will
16270Sstevel@tonic-gate 			 * never be used, we overload the rto field
16280Sstevel@tonic-gate 			 * here, setting it to 0 if the address was
16290Sstevel@tonic-gate 			 * not added, 1 if it was added.
16300Sstevel@tonic-gate 			 */
16310Sstevel@tonic-gate 			for (fp = fphead; fp; fp = fp->next) {
16320Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &fp->faddr)) {
16330Sstevel@tonic-gate 					fp->rto = 0;
16340Sstevel@tonic-gate 				} else {
16350Sstevel@tonic-gate 					fp->rto = 1;
16360Sstevel@tonic-gate 					nadded++;
16370Sstevel@tonic-gate 				}
16380Sstevel@tonic-gate 			}
16390Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16400Sstevel@tonic-gate 			goto done;
16410Sstevel@tonic-gate 		}
16420Sstevel@tonic-gate 	}
16430Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	/* All faddrs are disjoint; legit new association */
16460Sstevel@tonic-gate 	retval = 1;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate done:
16490Sstevel@tonic-gate 	/* If are attempted adds, send back an abort listing the addrs */
16500Sstevel@tonic-gate 	if (nadded > 0) {
16510Sstevel@tonic-gate 		void *dtail;
16520Sstevel@tonic-gate 		size_t dlen;
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
16550Sstevel@tonic-gate 		if (dtail == NULL) {
16560Sstevel@tonic-gate 			goto cleanup;
16570Sstevel@tonic-gate 		}
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 		ph = dtail;
16600Sstevel@tonic-gate 		dlen = 0;
16610Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fp->next) {
16620Sstevel@tonic-gate 			if (fp->rto == 0) {
16630Sstevel@tonic-gate 				continue;
16640Sstevel@tonic-gate 			}
16650Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
16660Sstevel@tonic-gate 				ipaddr_t addr4;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR4);
16690Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR4_LEN);
16700Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
16710Sstevel@tonic-gate 				ph++;
16720Sstevel@tonic-gate 				bcopy(&addr4, ph, sizeof (addr4));
16730Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
16740Sstevel@tonic-gate 				    ((char *)ph + sizeof (addr4));
16750Sstevel@tonic-gate 				dlen += PARM_ADDR4_LEN;
16760Sstevel@tonic-gate 			} else {
16770Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR6);
16780Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR6_LEN);
16790Sstevel@tonic-gate 				ph++;
16800Sstevel@tonic-gate 				bcopy(&fp->faddr, ph, sizeof (fp->faddr));
16810Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
16820Sstevel@tonic-gate 				    ((char *)ph + sizeof (fp->faddr));
16830Sstevel@tonic-gate 				dlen += PARM_ADDR6_LEN;
16840Sstevel@tonic-gate 			}
16850Sstevel@tonic-gate 		}
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 		/* Send off the abort */
16880Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ich),
168911042SErik.Nordmark@Sun.COM 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE,
169011042SErik.Nordmark@Sun.COM 		    ira);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
16930Sstevel@tonic-gate 	}
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate cleanup:
16960Sstevel@tonic-gate 	/* Clean up */
16970Sstevel@tonic-gate 	if (fphead) {
16980Sstevel@tonic-gate 		sctp_faddr_t *fpn;
16990Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fpn) {
17000Sstevel@tonic-gate 			fpn = fp->next;
170111042SErik.Nordmark@Sun.COM 			if (fp->ixa != NULL) {
170211042SErik.Nordmark@Sun.COM 				ixa_refrele(fp->ixa);
170311042SErik.Nordmark@Sun.COM 				fp->ixa = NULL;
170411042SErik.Nordmark@Sun.COM 			}
17050Sstevel@tonic-gate 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
17060Sstevel@tonic-gate 		}
17070Sstevel@tonic-gate 	}
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 	return (retval);
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate 
17121932Svi117747 /*
17131932Svi117747  * Reset any state related to transmitted chunks.
17141932Svi117747  */
17150Sstevel@tonic-gate void
17160Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp)
17170Sstevel@tonic-gate {
17181932Svi117747 	sctp_faddr_t	*fp;
17193448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17201932Svi117747 	mblk_t		*mp;
17210Sstevel@tonic-gate 
17221932Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
17233448Sdh155122 		fp->ssthresh = sctps->sctps_initial_mtu;
17243795Skcpoon 		SET_CWND(fp, fp->sfa_pmss, sctps->sctps_slow_start_initial);
17250Sstevel@tonic-gate 		fp->suna = 0;
17260Sstevel@tonic-gate 		fp->pba = 0;
17270Sstevel@tonic-gate 	}
17281932Svi117747 	/*
17291932Svi117747 	 * Clean up the transmit list as well since we have reset accounting
17301932Svi117747 	 * on all the fps. Send event upstream, if required.
17311932Svi117747 	 */
17321932Svi117747 	while ((mp = sctp->sctp_xmit_head) != NULL) {
17331932Svi117747 		sctp->sctp_xmit_head = mp->b_next;
17341932Svi117747 		mp->b_next = NULL;
17351932Svi117747 		if (sctp->sctp_xmit_head != NULL)
17361932Svi117747 			sctp->sctp_xmit_head->b_prev = NULL;
17371932Svi117747 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
17381932Svi117747 	}
17391932Svi117747 	sctp->sctp_xmit_head = NULL;
17401932Svi117747 	sctp->sctp_xmit_tail = NULL;
17411932Svi117747 	sctp->sctp_xmit_unacked = NULL;
17421932Svi117747 
17431932Svi117747 	sctp->sctp_unacked = 0;
17441932Svi117747 	/*
17451932Svi117747 	 * Any control message as well. We will clean-up this list as well.
17461932Svi117747 	 * This contains any pending ASCONF request that we have queued/sent.
17471932Svi117747 	 * If we do get an ACK we will just drop it. However, given that
17481932Svi117747 	 * we are restarting chances are we aren't going to get any.
17491932Svi117747 	 */
17501932Svi117747 	if (sctp->sctp_cxmit_list != NULL)
17511932Svi117747 		sctp_asconf_free_cxmit(sctp, NULL);
17521932Svi117747 	sctp->sctp_cxmit_list = NULL;
17531932Svi117747 	sctp->sctp_cchunk_pend = 0;
17541932Svi117747 
17551932Svi117747 	sctp->sctp_rexmitting = B_FALSE;
17561932Svi117747 	sctp->sctp_rxt_nxttsn = 0;
17571932Svi117747 	sctp->sctp_rxt_maxtsn = 0;
17581932Svi117747 
17591932Svi117747 	sctp->sctp_zero_win_probe = B_FALSE;
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate static void
17631735Skcpoon sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr,
17641735Skcpoon     mblk_t *timer_mp)
17650Sstevel@tonic-gate {
17663448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17673448Sdh155122 
176811042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa != NULL);
176911042SErik.Nordmark@Sun.COM 
17700Sstevel@tonic-gate 	bcopy(addr, &fp->faddr, sizeof (*addr));
17710Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
17720Sstevel@tonic-gate 		fp->isv4 = 1;
17730Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
17744691Skcpoon 		fp->sfa_pmss =
17754691Skcpoon 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) &
17764691Skcpoon 		    ~(SCTP_ALIGN - 1);
177711042SErik.Nordmark@Sun.COM 		fp->ixa->ixa_flags |= IXAF_IS_IPV4;
17780Sstevel@tonic-gate 	} else {
17790Sstevel@tonic-gate 		fp->isv4 = 0;
17803448Sdh155122 		fp->sfa_pmss =
17813448Sdh155122 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) &
17823448Sdh155122 		    ~(SCTP_ALIGN - 1);
178311042SErik.Nordmark@Sun.COM 		fp->ixa->ixa_flags &= ~IXAF_IS_IPV4;
17840Sstevel@tonic-gate 	}
17853448Sdh155122 	fp->cwnd = sctps->sctps_slow_start_initial * fp->sfa_pmss;
17860Sstevel@tonic-gate 	fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max);
178710212SGeorge.Shepherd@Sun.COM 	SCTP_MAX_RTO(sctp, fp);
17880Sstevel@tonic-gate 	fp->srtt = -1;
17890Sstevel@tonic-gate 	fp->rtt_updates = 0;
17900Sstevel@tonic-gate 	fp->strikes = 0;
17910Sstevel@tonic-gate 	fp->max_retr = sctp->sctp_pp_max_rxt;
17920Sstevel@tonic-gate 	/* Mark it as not confirmed. */
17930Sstevel@tonic-gate 	fp->state = SCTP_FADDRS_UNCONFIRMED;
17940Sstevel@tonic-gate 	fp->hb_interval = sctp->sctp_hb_interval;
17953448Sdh155122 	fp->ssthresh = sctps->sctps_initial_ssthresh;
17960Sstevel@tonic-gate 	fp->suna = 0;
17970Sstevel@tonic-gate 	fp->pba = 0;
17980Sstevel@tonic-gate 	fp->acked = 0;
179911066Srafael.vanoni@sun.com 	fp->lastactive = fp->hb_expiry = ddi_get_lbolt64();
18001735Skcpoon 	fp->timer_mp = timer_mp;
18010Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
18024818Skcpoon 	fp->hb_enabled = B_TRUE;
18030Sstevel@tonic-gate 	fp->df = 1;
18040Sstevel@tonic-gate 	fp->pmtu_discovered = 0;
18050Sstevel@tonic-gate 	fp->next = NULL;
18060Sstevel@tonic-gate 	fp->T3expire = 0;
18070Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret,
18080Sstevel@tonic-gate 	    sizeof (fp->hb_secret));
18093795Skcpoon 	fp->rxt_unacked = 0;
18100Sstevel@tonic-gate 
181111042SErik.Nordmark@Sun.COM 	sctp_get_dest(sctp, fp);
18120Sstevel@tonic-gate }
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate /*ARGSUSED*/
18156712Stomee static int
18166712Stomee faddr_constructor(void *buf, void *arg, int flags)
18176712Stomee {
18186712Stomee 	sctp_faddr_t *fp = buf;
18196712Stomee 
18206712Stomee 	fp->timer_mp = NULL;
18216712Stomee 	fp->timer_running = 0;
18226712Stomee 
18236712Stomee 	fp->rc_timer_mp = NULL;
18246712Stomee 	fp->rc_timer_running = 0;
18256712Stomee 
18266712Stomee 	return (0);
18276712Stomee }
18286712Stomee 
18296712Stomee /*ARGSUSED*/
18300Sstevel@tonic-gate static void
18316712Stomee faddr_destructor(void *buf, void *arg)
18320Sstevel@tonic-gate {
18330Sstevel@tonic-gate 	sctp_faddr_t *fp = buf;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	ASSERT(fp->timer_mp == NULL);
18360Sstevel@tonic-gate 	ASSERT(fp->timer_running == 0);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	ASSERT(fp->rc_timer_mp == NULL);
18390Sstevel@tonic-gate 	ASSERT(fp->rc_timer_running == 0);
18400Sstevel@tonic-gate }
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate void
18431676Sjpk sctp_faddr_init(void)
18440Sstevel@tonic-gate {
18450Sstevel@tonic-gate 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
18466712Stomee 	    sizeof (sctp_faddr_t), 0, faddr_constructor, faddr_destructor,
18470Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
18480Sstevel@tonic-gate }
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate void
18511676Sjpk sctp_faddr_fini(void)
18520Sstevel@tonic-gate {
18530Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_faddr_cache);
18540Sstevel@tonic-gate }
1855