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 /*
2312474SGeorge.Shepherd@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/systm.h>
280Sstevel@tonic-gate #include <sys/stream.h>
291676Sjpk #include <sys/strsubr.h>
300Sstevel@tonic-gate #include <sys/ddi.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <sys/random.h>
351676Sjpk #include <sys/tsol/tndb.h>
361676Sjpk #include <sys/tsol/tnet.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <netinet/in.h>
390Sstevel@tonic-gate #include <netinet/ip6.h>
400Sstevel@tonic-gate #include <netinet/sctp.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <inet/common.h>
430Sstevel@tonic-gate #include <inet/ip.h>
440Sstevel@tonic-gate #include <inet/ip6.h>
450Sstevel@tonic-gate #include <inet/ip_ire.h>
4611042SErik.Nordmark@Sun.COM #include <inet/ip_if.h>
4711042SErik.Nordmark@Sun.COM #include <inet/ip_ndp.h>
480Sstevel@tonic-gate #include <inet/mib2.h>
490Sstevel@tonic-gate #include <inet/nd.h>
500Sstevel@tonic-gate #include <inet/optcom.h>
510Sstevel@tonic-gate #include <inet/sctp_ip.h>
520Sstevel@tonic-gate #include <inet/ipclassifier.h>
531676Sjpk 
540Sstevel@tonic-gate #include "sctp_impl.h"
550Sstevel@tonic-gate #include "sctp_addr.h"
561932Svi117747 #include "sctp_asconf.h"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_faddr_cache;
591735Skcpoon static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *);
600Sstevel@tonic-gate 
6111042SErik.Nordmark@Sun.COM /* Set the source address.  Refer to comments in sctp_get_dest(). */
621932Svi117747 void
631932Svi117747 sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp)
640Sstevel@tonic-gate {
651719Sjarrett 	boolean_t v6 = !fp->isv4;
664818Skcpoon 	boolean_t addr_set;
671719Sjarrett 
684818Skcpoon 	fp->saddr = sctp_get_valid_addr(sctp, v6, &addr_set);
694818Skcpoon 	/*
704818Skcpoon 	 * If there is no source address avaialble, mark this peer address
714818Skcpoon 	 * as unreachable for now.  When the heartbeat timer fires, it will
7211042SErik.Nordmark@Sun.COM 	 * call sctp_get_dest() to re-check if there is any source address
734818Skcpoon 	 * available.
744818Skcpoon 	 */
754818Skcpoon 	if (!addr_set)
764818Skcpoon 		fp->state = SCTP_FADDRS_UNREACH;
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
8011042SErik.Nordmark@Sun.COM  * Call this function to get information about a peer addr fp.
8111042SErik.Nordmark@Sun.COM  *
8211042SErik.Nordmark@Sun.COM  * Uses ip_attr_connect to avoid explicit use of ire and source address
8311042SErik.Nordmark@Sun.COM  * selection.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate void
8611042SErik.Nordmark@Sun.COM sctp_get_dest(sctp_t *sctp, sctp_faddr_t *fp)
870Sstevel@tonic-gate {
881735Skcpoon 	in6_addr_t	laddr;
8911042SErik.Nordmark@Sun.COM 	in6_addr_t	nexthop;
900Sstevel@tonic-gate 	sctp_saddr_ipif_t *sp;
911735Skcpoon 	int		hdrlen;
923448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
9311042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
9411042SErik.Nordmark@Sun.COM 	iulp_t		uinfo;
9511042SErik.Nordmark@Sun.COM 	uint_t		pmtu;
9611042SErik.Nordmark@Sun.COM 	int		error;
9711042SErik.Nordmark@Sun.COM 	uint32_t	flags = IPDF_VERIFY_DST | IPDF_IPSEC |
9811042SErik.Nordmark@Sun.COM 	    IPDF_SELECT_SRC | IPDF_UNIQUE_DCE;
990Sstevel@tonic-gate 
10011042SErik.Nordmark@Sun.COM 	/*
10111042SErik.Nordmark@Sun.COM 	 * Tell sctp_make_mp it needs to call us again should we not
10211042SErik.Nordmark@Sun.COM 	 * complete and set the saddr.
10311042SErik.Nordmark@Sun.COM 	 */
10411042SErik.Nordmark@Sun.COM 	fp->saddr = ipv6_all_zeros;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1070Sstevel@tonic-gate 	 * If this addr is not reachable, mark it as unconfirmed for now, the
1080Sstevel@tonic-gate 	 * state will be changed back to unreachable later in this function
1090Sstevel@tonic-gate 	 * if it is still the case.
1100Sstevel@tonic-gate 	 */
1110Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH) {
1120Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_UNCONFIRMED;
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
11511042SErik.Nordmark@Sun.COM 	/*
11611042SErik.Nordmark@Sun.COM 	 * Socket is connected - enable PMTU discovery.
11711042SErik.Nordmark@Sun.COM 	 */
11811042SErik.Nordmark@Sun.COM 	if (!sctps->sctps_ignore_path_mtu)
11911042SErik.Nordmark@Sun.COM 		fp->ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
1201676Sjpk 
12111042SErik.Nordmark@Sun.COM 	ip_attr_nexthop(&connp->conn_xmit_ipp, fp->ixa, &fp->faddr,
12211042SErik.Nordmark@Sun.COM 	    &nexthop);
1230Sstevel@tonic-gate 
12411042SErik.Nordmark@Sun.COM 	laddr = fp->saddr;
12511042SErik.Nordmark@Sun.COM 	error = ip_attr_connect(connp, fp->ixa, &laddr, &fp->faddr, &nexthop,
12611042SErik.Nordmark@Sun.COM 	    connp->conn_fport, &laddr, &uinfo, flags);
12711042SErik.Nordmark@Sun.COM 
12811042SErik.Nordmark@Sun.COM 	if (error != 0) {
12911042SErik.Nordmark@Sun.COM 		dprint(3, ("sctp_get_dest: no ire for %x:%x:%x:%x\n",
1301676Sjpk 		    SCTP_PRINTADDR(fp->faddr)));
1311676Sjpk 		/*
1321676Sjpk 		 * It is tempting to just leave the src addr
1331676Sjpk 		 * unspecified and let IP figure it out, but we
1341676Sjpk 		 * *cannot* do this, since IP may choose a src addr
1351676Sjpk 		 * that is not part of this association... unless
13611042SErik.Nordmark@Sun.COM 		 * this sctp has bound to all addrs.  So if the dest
1371676Sjpk 		 * lookup fails, try to find one in our src addr
1381676Sjpk 		 * list, unless the sctp has bound to all addrs, in
1391676Sjpk 		 * which case we change the src addr to unspec.
1401676Sjpk 		 *
1411676Sjpk 		 * Note that if this is a v6 endpoint but it does
1421676Sjpk 		 * not have any v4 address at this point (e.g. may
1431676Sjpk 		 * have been  deleted), sctp_get_valid_addr() will
1441676Sjpk 		 * return mapped INADDR_ANY.  In this case, this
1451676Sjpk 		 * address should be marked not reachable so that
1461676Sjpk 		 * it won't be used to send data.
1471676Sjpk 		 */
1481932Svi117747 		sctp_set_saddr(sctp, fp);
1491735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH)
1501735Skcpoon 			return;
1511735Skcpoon 		goto check_current;
1521676Sjpk 	}
15311042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa->ixa_ire != NULL);
15411042SErik.Nordmark@Sun.COM 	ASSERT(!(fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)));
15511042SErik.Nordmark@Sun.COM 
15611042SErik.Nordmark@Sun.COM 	if (!sctp->sctp_loopback)
15711042SErik.Nordmark@Sun.COM 		sctp->sctp_loopback = uinfo.iulp_loopback;
1581676Sjpk 
1591735Skcpoon 	/* Make sure the laddr is part of this association */
16011042SErik.Nordmark@Sun.COM 	if ((sp = sctp_saddr_lookup(sctp, &laddr, 0)) != NULL &&
16111042SErik.Nordmark@Sun.COM 	    !sp->saddr_ipif_dontsrc) {
1621676Sjpk 		if (sp->saddr_ipif_unconfirmed == 1)
1631676Sjpk 			sp->saddr_ipif_unconfirmed = 0;
16411042SErik.Nordmark@Sun.COM 		/* We did IPsec policy lookup for laddr already */
1651676Sjpk 		fp->saddr = laddr;
1661676Sjpk 	} else {
16711042SErik.Nordmark@Sun.COM 		dprint(2, ("sctp_get_dest: src addr is not part of assoc "
16811042SErik.Nordmark@Sun.COM 		    "%x:%x:%x:%x\n", SCTP_PRINTADDR(laddr)));
1691735Skcpoon 
1701735Skcpoon 		/*
1711735Skcpoon 		 * Set the src to the first saddr and hope for the best.
17211042SErik.Nordmark@Sun.COM 		 * Note that this case should very seldomly
1731735Skcpoon 		 * happen.  One scenario this can happen is an app
1741735Skcpoon 		 * explicitly bind() to an address.  But that address is
1751735Skcpoon 		 * not the preferred source address to send to the peer.
1761735Skcpoon 		 */
1771932Svi117747 		sctp_set_saddr(sctp, fp);
1781735Skcpoon 		if (fp->state == SCTP_FADDRS_UNREACH) {
1791735Skcpoon 			return;
1801735Skcpoon 		}
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1831735Skcpoon 	/*
1840Sstevel@tonic-gate 	 * Pull out RTO information for this faddr and use it if we don't
1850Sstevel@tonic-gate 	 * have any yet.
1860Sstevel@tonic-gate 	 */
18711042SErik.Nordmark@Sun.COM 	if (fp->srtt == -1 && uinfo.iulp_rtt != 0) {
188116Skcpoon 		/* The cached value is in ms. */
18911042SErik.Nordmark@Sun.COM 		fp->srtt = MSEC_TO_TICK(uinfo.iulp_rtt);
19011042SErik.Nordmark@Sun.COM 		fp->rttvar = MSEC_TO_TICK(uinfo.iulp_rtt_sd);
1910Sstevel@tonic-gate 		fp->rto = 3 * fp->srtt;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		/* Bound the RTO by configured min and max values */
1940Sstevel@tonic-gate 		if (fp->rto < sctp->sctp_rto_min) {
1950Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_min;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 		if (fp->rto > sctp->sctp_rto_max) {
1980Sstevel@tonic-gate 			fp->rto = sctp->sctp_rto_max;
1990Sstevel@tonic-gate 		}
20010212SGeorge.Shepherd@Sun.COM 		SCTP_MAX_RTO(sctp, fp);
2010Sstevel@tonic-gate 	}
20211042SErik.Nordmark@Sun.COM 	pmtu = uinfo.iulp_mtu;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Record the MTU for this faddr. If the MTU for this faddr has
2060Sstevel@tonic-gate 	 * changed, check if the assc MTU will also change.
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 	if (fp->isv4) {
2090Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
2100Sstevel@tonic-gate 	} else {
2110Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
2120Sstevel@tonic-gate 	}
21311042SErik.Nordmark@Sun.COM 	if ((fp->sfa_pmss + hdrlen) != pmtu) {
2140Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
21511042SErik.Nordmark@Sun.COM 		fp->sfa_pmss = (pmtu - hdrlen) & ~(SCTP_ALIGN - 1);
2160Sstevel@tonic-gate 		if (fp->cwnd < (fp->sfa_pmss * 2)) {
2173795Skcpoon 			SET_CWND(fp, fp->sfa_pmss,
2183795Skcpoon 			    sctps->sctps_slow_start_initial);
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2221735Skcpoon check_current:
2231735Skcpoon 	if (fp == sctp->sctp_current)
2241735Skcpoon 		sctp_set_faddr_current(sctp, fp);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate void
22811042SErik.Nordmark@Sun.COM sctp_update_dce(sctp_t *sctp)
2290Sstevel@tonic-gate {
2301735Skcpoon 	sctp_faddr_t	*fp;
2313448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
23211042SErik.Nordmark@Sun.COM 	iulp_t		uinfo;
23311042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = sctps->sctps_netstack->netstack_ip;
23411042SErik.Nordmark@Sun.COM 	uint_t		ifindex;
2350Sstevel@tonic-gate 
2361735Skcpoon 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
23711042SErik.Nordmark@Sun.COM 		bzero(&uinfo, sizeof (uinfo));
2380Sstevel@tonic-gate 		/*
2391735Skcpoon 		 * Only record the PMTU for this faddr if we actually have
2401735Skcpoon 		 * done discovery. This prevents initialized default from
2411735Skcpoon 		 * clobbering any real info that IP may have.
2420Sstevel@tonic-gate 		 */
2431735Skcpoon 		if (fp->pmtu_discovered) {
2441735Skcpoon 			if (fp->isv4) {
24511042SErik.Nordmark@Sun.COM 				uinfo.iulp_mtu = fp->sfa_pmss +
2461735Skcpoon 				    sctp->sctp_hdr_len;
2471735Skcpoon 			} else {
24811042SErik.Nordmark@Sun.COM 				uinfo.iulp_mtu = fp->sfa_pmss +
2491735Skcpoon 				    sctp->sctp_hdr6_len;
2501735Skcpoon 			}
2510Sstevel@tonic-gate 		}
2523448Sdh155122 		if (sctps->sctps_rtt_updates != 0 &&
2533448Sdh155122 		    fp->rtt_updates >= sctps->sctps_rtt_updates) {
2541735Skcpoon 			/*
25511042SErik.Nordmark@Sun.COM 			 * dce_update_uinfo() merges these values with the
25611042SErik.Nordmark@Sun.COM 			 * old values.
2571735Skcpoon 			 */
25811042SErik.Nordmark@Sun.COM 			uinfo.iulp_rtt = TICK_TO_MSEC(fp->srtt);
25911042SErik.Nordmark@Sun.COM 			uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->rttvar);
2601735Skcpoon 			fp->rtt_updates = 0;
2610Sstevel@tonic-gate 		}
26211042SErik.Nordmark@Sun.COM 		ifindex = 0;
26311042SErik.Nordmark@Sun.COM 		if (IN6_IS_ADDR_LINKSCOPE(&fp->faddr)) {
26411042SErik.Nordmark@Sun.COM 			/*
26511042SErik.Nordmark@Sun.COM 			 * If we are going to create a DCE we'd better have
26611042SErik.Nordmark@Sun.COM 			 * an ifindex
26711042SErik.Nordmark@Sun.COM 			 */
26811042SErik.Nordmark@Sun.COM 			if (fp->ixa->ixa_nce != NULL) {
26911042SErik.Nordmark@Sun.COM 				ifindex = fp->ixa->ixa_nce->nce_common->
27011042SErik.Nordmark@Sun.COM 				    ncec_ill->ill_phyint->phyint_ifindex;
27111042SErik.Nordmark@Sun.COM 			} else {
27211042SErik.Nordmark@Sun.COM 				continue;
27311042SErik.Nordmark@Sun.COM 			}
27411042SErik.Nordmark@Sun.COM 		}
27511042SErik.Nordmark@Sun.COM 
27611042SErik.Nordmark@Sun.COM 		(void) dce_update_uinfo(&fp->faddr, ifindex, &uinfo, ipst);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
28111042SErik.Nordmark@Sun.COM  * The sender must later set the total length in the IP header.
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate mblk_t *
28411042SErik.Nordmark@Sun.COM sctp_make_mp(sctp_t *sctp, sctp_faddr_t *fp, int trailer)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	mblk_t *mp;
2870Sstevel@tonic-gate 	size_t ipsctplen;
2880Sstevel@tonic-gate 	int isv4;
2893448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
2904818Skcpoon 	boolean_t src_changed = B_FALSE;
2910Sstevel@tonic-gate 
29211042SErik.Nordmark@Sun.COM 	ASSERT(fp != NULL);
2930Sstevel@tonic-gate 	isv4 = fp->isv4;
2940Sstevel@tonic-gate 
29511042SErik.Nordmark@Sun.COM 	if (SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr) ||
29611042SErik.Nordmark@Sun.COM 	    (fp->ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
29711042SErik.Nordmark@Sun.COM 		/* Need to pick a source */
29811042SErik.Nordmark@Sun.COM 		sctp_get_dest(sctp, fp);
2994818Skcpoon 		/*
3004818Skcpoon 		 * Although we still may not get an IRE, the source address
3014818Skcpoon 		 * may be changed in sctp_get_ire().  Set src_changed to
3024818Skcpoon 		 * true so that the source address is copied again.
3034818Skcpoon 		 */
3044818Skcpoon 		src_changed = B_TRUE;
3054818Skcpoon 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/* There is no suitable source address to use, return. */
3080Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_UNREACH)
3090Sstevel@tonic-gate 		return (NULL);
31011042SErik.Nordmark@Sun.COM 
31111042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa->ixa_ire != NULL);
31211042SErik.Nordmark@Sun.COM 	ASSERT(!SCTP_IS_ADDR_UNSPEC(isv4, fp->saddr));
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (isv4) {
3150Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr_len;
3160Sstevel@tonic-gate 	} else {
3170Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr6_len;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
32011042SErik.Nordmark@Sun.COM 	mp = allocb(ipsctplen + sctps->sctps_wroff_xtra + trailer, BPRI_MED);
3210Sstevel@tonic-gate 	if (mp == NULL) {
3221676Sjpk 		ip1dbg(("sctp_make_mp: error making mp..\n"));
3230Sstevel@tonic-gate 		return (NULL);
3240Sstevel@tonic-gate 	}
3253448Sdh155122 	mp->b_rptr += sctps->sctps_wroff_xtra;
3260Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + ipsctplen;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_wptr));
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	if (isv4) {
3310Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)mp->b_rptr;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
3344818Skcpoon 		if (fp != sctp->sctp_current || src_changed) {
3354818Skcpoon 			/* Fix the source and destination addresses. */
3360Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
3374818Skcpoon 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr, iph->ipha_src);
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 		/* set or clear the don't fragment bit */
3400Sstevel@tonic-gate 		if (fp->df) {
3410Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
3420Sstevel@tonic-gate 		} else {
3430Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	} else {
3460Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
3474818Skcpoon 		if (fp != sctp->sctp_current || src_changed) {
3484818Skcpoon 			/* Fix the source and destination addresses. */
3490Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
3504818Skcpoon 			((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp != NULL);
3540Sstevel@tonic-gate 	return (mp);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Notify upper layers about preferred write offset, write size.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate void
3610Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	int hdrlen;
3648348SEric.Yu@Sun.COM 	struct sock_proto_props sopp;
3658348SEric.Yu@Sun.COM 
3663448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (sctp->sctp_current->isv4) {
3690Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
3700Sstevel@tonic-gate 	} else {
3710Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd);
3740Sstevel@tonic-gate 
37511042SErik.Nordmark@Sun.COM 	sctp->sctp_connp->conn_wroff = sctps->sctps_wroff_xtra + hdrlen +
37611042SErik.Nordmark@Sun.COM 	    sizeof (sctp_data_hdr_t);
37711042SErik.Nordmark@Sun.COM 
3780Sstevel@tonic-gate 	ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss);
3798348SEric.Yu@Sun.COM 	bzero(&sopp, sizeof (sopp));
3808348SEric.Yu@Sun.COM 	sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF;
38111042SErik.Nordmark@Sun.COM 	sopp.sopp_wroff = sctp->sctp_connp->conn_wroff;
3828348SEric.Yu@Sun.COM 	sopp.sopp_maxblk = sctp->sctp_mss - sizeof (sctp_data_hdr_t);
3838348SEric.Yu@Sun.COM 	sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
38611042SErik.Nordmark@Sun.COM /*
38711042SErik.Nordmark@Sun.COM  * Set the lengths in the packet and the transmit attributes.
38811042SErik.Nordmark@Sun.COM  */
3890Sstevel@tonic-gate void
39011042SErik.Nordmark@Sun.COM sctp_set_iplen(sctp_t *sctp, mblk_t *mp, ip_xmit_attr_t *ixa)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	uint16_t	sum = 0;
3930Sstevel@tonic-gate 	ipha_t		*iph;
3940Sstevel@tonic-gate 	ip6_t		*ip6h;
3950Sstevel@tonic-gate 	mblk_t		*pmp = mp;
3960Sstevel@tonic-gate 	boolean_t	isv4;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
3990Sstevel@tonic-gate 	for (; pmp; pmp = pmp->b_cont)
4000Sstevel@tonic-gate 		sum += pmp->b_wptr - pmp->b_rptr;
4010Sstevel@tonic-gate 
40211042SErik.Nordmark@Sun.COM 	ixa->ixa_pktlen = sum;
4030Sstevel@tonic-gate 	if (isv4) {
4040Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
4050Sstevel@tonic-gate 		iph->ipha_length = htons(sum);
40611042SErik.Nordmark@Sun.COM 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr_len;
4070Sstevel@tonic-gate 	} else {
4080Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
40911042SErik.Nordmark@Sun.COM 		ip6h->ip6_plen = htons(sum - IPV6_HDR_LEN);
41011042SErik.Nordmark@Sun.COM 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr6_len;
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate int
4150Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	int na1 = 0;
4180Sstevel@tonic-gate 	int overlap = 0;
4190Sstevel@tonic-gate 	int equal = 1;
4200Sstevel@tonic-gate 	int onematch;
4210Sstevel@tonic-gate 	sctp_faddr_t *fp1, *fp2;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	for (fp1 = a1; fp1; fp1 = fp1->next) {
4240Sstevel@tonic-gate 		onematch = 0;
4250Sstevel@tonic-gate 		for (fp2 = a2; fp2; fp2 = fp2->next) {
4260Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) {
4270Sstevel@tonic-gate 				overlap++;
4280Sstevel@tonic-gate 				onematch = 1;
4290Sstevel@tonic-gate 				break;
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 			if (!onematch) {
4320Sstevel@tonic-gate 				equal = 0;
4330Sstevel@tonic-gate 			}
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 		na1++;
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	if (equal) {
4390Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	if (overlap == na1) {
4420Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 	if (overlap) {
4450Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4519710SKen.Powell@Sun.COM  * Returns 0 on success, ENOMEM on memory allocation failure, EHOSTUNREACH
4529710SKen.Powell@Sun.COM  * if the connection credentials fail remote host accreditation or
4539710SKen.Powell@Sun.COM  * if the new destination does not support the previously established
4549710SKen.Powell@Sun.COM  * connection security label. If sleep is true, this function should
4559710SKen.Powell@Sun.COM  * never fail for a memory allocation failure. The boolean parameter
4569710SKen.Powell@Sun.COM  * "first" decides whether the newly created faddr structure should be
4571735Skcpoon  * added at the beginning of the list or at the end.
4581735Skcpoon  *
4591735Skcpoon  * Note: caller must hold conn fanout lock.
4600Sstevel@tonic-gate  */
4611735Skcpoon int
4621735Skcpoon sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first)
4630Sstevel@tonic-gate {
4641735Skcpoon 	sctp_faddr_t	*faddr;
4651735Skcpoon 	mblk_t		*timer_mp;
4669710SKen.Powell@Sun.COM 	int		err;
46711042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
4680Sstevel@tonic-gate 
4691676Sjpk 	if (is_system_labeled()) {
47011042SErik.Nordmark@Sun.COM 		ip_xmit_attr_t	*ixa = connp->conn_ixa;
47111042SErik.Nordmark@Sun.COM 		ts_label_t	*effective_tsl = NULL;
47211042SErik.Nordmark@Sun.COM 
47311042SErik.Nordmark@Sun.COM 		ASSERT(ixa->ixa_tsl != NULL);
4740Sstevel@tonic-gate 
4759710SKen.Powell@Sun.COM 		/*
4769710SKen.Powell@Sun.COM 		 * Verify the destination is allowed to receive packets
4779710SKen.Powell@Sun.COM 		 * at the security label of the connection we are initiating.
4789710SKen.Powell@Sun.COM 		 *
47911042SErik.Nordmark@Sun.COM 		 * tsol_check_dest() will create a new effective label for
4809710SKen.Powell@Sun.COM 		 * this connection with a modified label or label flags only
48111042SErik.Nordmark@Sun.COM 		 * if there are changes from the original label.
4829710SKen.Powell@Sun.COM 		 *
4839710SKen.Powell@Sun.COM 		 * Accept whatever label we get if this is the first
4849710SKen.Powell@Sun.COM 		 * destination address for this connection. The security
4859710SKen.Powell@Sun.COM 		 * label and label flags must match any previuous settings
4869710SKen.Powell@Sun.COM 		 * for all subsequent destination addresses.
4879710SKen.Powell@Sun.COM 		 */
4889710SKen.Powell@Sun.COM 		if (IN6_IS_ADDR_V4MAPPED(addr)) {
4899710SKen.Powell@Sun.COM 			uint32_t dst;
4909710SKen.Powell@Sun.COM 			IN6_V4MAPPED_TO_IPADDR(addr, dst);
49111042SErik.Nordmark@Sun.COM 			err = tsol_check_dest(ixa->ixa_tsl,
49211042SErik.Nordmark@Sun.COM 			    &dst, IPV4_VERSION, connp->conn_mac_mode,
49311042SErik.Nordmark@Sun.COM 			    connp->conn_zone_is_global, &effective_tsl);
4949710SKen.Powell@Sun.COM 		} else {
49511042SErik.Nordmark@Sun.COM 			err = tsol_check_dest(ixa->ixa_tsl,
49611042SErik.Nordmark@Sun.COM 			    addr, IPV6_VERSION, connp->conn_mac_mode,
49711042SErik.Nordmark@Sun.COM 			    connp->conn_zone_is_global, &effective_tsl);
4981676Sjpk 		}
4999710SKen.Powell@Sun.COM 		if (err != 0)
5009710SKen.Powell@Sun.COM 			return (err);
50111042SErik.Nordmark@Sun.COM 
50211042SErik.Nordmark@Sun.COM 		if (sctp->sctp_faddrs == NULL && effective_tsl != NULL) {
50311042SErik.Nordmark@Sun.COM 			ip_xmit_attr_replace_tsl(ixa, effective_tsl);
50411042SErik.Nordmark@Sun.COM 		} else if (effective_tsl != NULL) {
50511042SErik.Nordmark@Sun.COM 			label_rele(effective_tsl);
5069710SKen.Powell@Sun.COM 			return (EHOSTUNREACH);
5079710SKen.Powell@Sun.COM 		}
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 
5101676Sjpk 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL)
5111676Sjpk 		return (ENOMEM);
51211042SErik.Nordmark@Sun.COM 	bzero(faddr, sizeof (*faddr));
5134691Skcpoon 	timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep);
5141735Skcpoon 	if (timer_mp == NULL) {
5151735Skcpoon 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
5161735Skcpoon 		return (ENOMEM);
5171735Skcpoon 	}
5181735Skcpoon 	((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr;
5191676Sjpk 
52011042SErik.Nordmark@Sun.COM 	/* Start with any options set on the conn */
52111042SErik.Nordmark@Sun.COM 	faddr->ixa = conn_get_ixa_exclusive(connp);
52211042SErik.Nordmark@Sun.COM 	if (faddr->ixa == NULL) {
52311042SErik.Nordmark@Sun.COM 		freemsg(timer_mp);
52411042SErik.Nordmark@Sun.COM 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
52511042SErik.Nordmark@Sun.COM 		return (ENOMEM);
52611042SErik.Nordmark@Sun.COM 	}
52711042SErik.Nordmark@Sun.COM 	faddr->ixa->ixa_notify_cookie = connp->conn_sctp;
5284818Skcpoon 
52911042SErik.Nordmark@Sun.COM 	sctp_init_faddr(sctp, faddr, addr, timer_mp);
53011042SErik.Nordmark@Sun.COM 	ASSERT(faddr->ixa->ixa_cred != NULL);
53111042SErik.Nordmark@Sun.COM 
53211042SErik.Nordmark@Sun.COM 	/* ip_attr_connect didn't allow broadcats/multicast dest */
5330Sstevel@tonic-gate 	ASSERT(faddr->next == NULL);
5340Sstevel@tonic-gate 
5351676Sjpk 	if (sctp->sctp_faddrs == NULL) {
5361676Sjpk 		ASSERT(sctp->sctp_lastfaddr == NULL);
5371676Sjpk 		/* only element on list; first and last are same */
5381676Sjpk 		sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr;
5391676Sjpk 	} else if (first) {
5401676Sjpk 		ASSERT(sctp->sctp_lastfaddr != NULL);
5411676Sjpk 		faddr->next = sctp->sctp_faddrs;
5421676Sjpk 		sctp->sctp_faddrs = faddr;
5430Sstevel@tonic-gate 	} else {
5441676Sjpk 		sctp->sctp_lastfaddr->next = faddr;
5451676Sjpk 		sctp->sctp_lastfaddr = faddr;
5460Sstevel@tonic-gate 	}
547852Svi117747 	sctp->sctp_nfaddrs++;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	return (0);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate sctp_faddr_t *
5530Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	sctp_faddr_t *fp;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
5580Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr))
5590Sstevel@tonic-gate 			break;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	return (fp);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate sctp_faddr_t *
5660Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate 	for (; fp; fp = fp->next) {
5690Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) {
5700Sstevel@tonic-gate 			break;
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	return (fp);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5771735Skcpoon /*
5781735Skcpoon  * To change the currently used peer address to the specified one.
5791735Skcpoon  */
5800Sstevel@tonic-gate void
5811735Skcpoon sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp)
5820Sstevel@tonic-gate {
5831735Skcpoon 	/* Now setup the composite header. */
5840Sstevel@tonic-gate 	if (fp->isv4) {
5850Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
5860Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
5870Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src);
5880Sstevel@tonic-gate 		/* update don't fragment bit */
5890Sstevel@tonic-gate 		if (fp->df) {
5900Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
5910Sstevel@tonic-gate 			    htons(IPH_DF);
5920Sstevel@tonic-gate 		} else {
5930Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 	} else {
5960Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_dst = fp->faddr;
5970Sstevel@tonic-gate 		sctp->sctp_ip6h->ip6_src = fp->saddr;
5980Sstevel@tonic-gate 	}
5991735Skcpoon 
6001735Skcpoon 	sctp->sctp_current = fp;
6011735Skcpoon 	sctp->sctp_mss = fp->sfa_pmss;
6021735Skcpoon 
6031735Skcpoon 	/* Update the uppper layer for the change. */
6041735Skcpoon 	if (!SCTP_IS_DETACHED(sctp))
6051735Skcpoon 		sctp_set_ulp_prop(sctp);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate void
6090Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	sctp_faddr_t *fp;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
61411042SErik.Nordmark@Sun.COM 		sctp_get_dest(sctp, fp);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate void
6190Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
6200Sstevel@tonic-gate {
621*12869SKacheong.Poon@Sun.COM 	int64_t now = LBOLT_FASTPATH64;
6220Sstevel@tonic-gate 
623*12869SKacheong.Poon@Sun.COM 	/*
624*12869SKacheong.Poon@Sun.COM 	 * If we are under memory pressure, we abort association waiting
625*12869SKacheong.Poon@Sun.COM 	 * in zero window probing state for too long.  We do this by not
626*12869SKacheong.Poon@Sun.COM 	 * resetting sctp_strikes.  So if sctp_zero_win_probe continues
627*12869SKacheong.Poon@Sun.COM 	 * while under memory pressure, this association will eventually
628*12869SKacheong.Poon@Sun.COM 	 * time out.
629*12869SKacheong.Poon@Sun.COM 	 */
630*12869SKacheong.Poon@Sun.COM 	if (!sctp->sctp_zero_win_probe || !sctp->sctp_sctps->sctps_reclaim) {
631*12869SKacheong.Poon@Sun.COM 		sctp->sctp_strikes = 0;
632*12869SKacheong.Poon@Sun.COM 	}
6330Sstevel@tonic-gate 	fp->strikes = 0;
6340Sstevel@tonic-gate 	fp->lastactive = now;
6350Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
6360Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
6370Sstevel@tonic-gate 	if (fp->state != SCTP_FADDRS_ALIVE) {
6380Sstevel@tonic-gate 		fp->state = SCTP_FADDRS_ALIVE;
6390Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0);
6404818Skcpoon 		/* Should have a full IRE now */
64111042SErik.Nordmark@Sun.COM 		sctp_get_dest(sctp, fp);
6420Sstevel@tonic-gate 
6431735Skcpoon 		/*
6441735Skcpoon 		 * If this is the primary, switch back to it now.  And
6451735Skcpoon 		 * we probably want to reset the source addr used to reach
6461735Skcpoon 		 * it.
64711042SErik.Nordmark@Sun.COM 		 * Note that if we didn't find a source in sctp_get_dest
64811042SErik.Nordmark@Sun.COM 		 * then we'd be unreachable at this point in time.
6491735Skcpoon 		 */
65011042SErik.Nordmark@Sun.COM 		if (fp == sctp->sctp_primary &&
65111042SErik.Nordmark@Sun.COM 		    fp->state != SCTP_FADDRS_UNREACH) {
6521735Skcpoon 			sctp_set_faddr_current(sctp, fp);
6531735Skcpoon 			return;
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
658*12869SKacheong.Poon@Sun.COM /*
659*12869SKacheong.Poon@Sun.COM  * Return B_TRUE if there is still an active peer address with zero strikes;
660*12869SKacheong.Poon@Sun.COM  * otherwise rturn B_FALSE.
661*12869SKacheong.Poon@Sun.COM  */
662*12869SKacheong.Poon@Sun.COM boolean_t
6630Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	sctp_faddr_t *fp;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
6680Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) {
669*12869SKacheong.Poon@Sun.COM 			return (B_TRUE);
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
673*12869SKacheong.Poon@Sun.COM 	return (B_FALSE);
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * Returns 0 if there is at leave one other active faddr, -1 if there
6780Sstevel@tonic-gate  * are none. If there are none left, faddr_dead() will start killing the
6790Sstevel@tonic-gate  * association.
6800Sstevel@tonic-gate  * If the downed faddr was the current faddr, a new current faddr
6810Sstevel@tonic-gate  * will be chosen.
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate int
6840Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	sctp_faddr_t *ofp;
6873448Sdh155122 	sctp_stack_t *sctps = sctp->sctp_sctps;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if (fp->state == SCTP_FADDRS_ALIVE) {
6900Sstevel@tonic-gate 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0);
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 	fp->state = newstate;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
6950Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), newstate));
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
6980Sstevel@tonic-gate 		/* Current faddr down; need to switch it */
6990Sstevel@tonic-gate 		sctp->sctp_current = NULL;
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/* Find next alive faddr */
7030Sstevel@tonic-gate 	ofp = fp;
7041735Skcpoon 	for (fp = fp->next; fp != NULL; fp = fp->next) {
7050Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_ALIVE) {
7060Sstevel@tonic-gate 			break;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if (fp == NULL) {
7110Sstevel@tonic-gate 		/* Continue from beginning of list */
7120Sstevel@tonic-gate 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) {
7130Sstevel@tonic-gate 			if (fp->state == SCTP_FADDRS_ALIVE) {
7140Sstevel@tonic-gate 				break;
7150Sstevel@tonic-gate 			}
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7191735Skcpoon 	/*
7201735Skcpoon 	 * Find a new fp, so if the current faddr is dead, use the new fp
7211735Skcpoon 	 * as the current one.
7221735Skcpoon 	 */
7230Sstevel@tonic-gate 	if (fp != ofp) {
7240Sstevel@tonic-gate 		if (sctp->sctp_current == NULL) {
7250Sstevel@tonic-gate 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
7260Sstevel@tonic-gate 			    SCTP_PRINTADDR(fp->faddr)));
7271735Skcpoon 			/*
7281735Skcpoon 			 * Note that we don't need to reset the source addr
7291735Skcpoon 			 * of the new fp.
7301735Skcpoon 			 */
7311735Skcpoon 			sctp_set_faddr_current(sctp, fp);
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 		return (0);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	/* All faddrs are down; kill the association */
7380Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
739*12869SKacheong.Poon@Sun.COM 	SCTPS_BUMP_MIB(sctps, sctpAborted);
7400Sstevel@tonic-gate 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
7410Sstevel@tonic-gate 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
7420Sstevel@tonic-gate 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
7430Sstevel@tonic-gate 	    sctp->sctp_client_errno : ETIMEDOUT);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	return (-1);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate sctp_faddr_t *
7490Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	sctp_faddr_t *nfp = NULL;
75211908SGeorge.Shepherd@Sun.COM 	sctp_faddr_t *saved_fp = NULL;
75311908SGeorge.Shepherd@Sun.COM 	int min_strikes;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if (ofp == NULL) {
7560Sstevel@tonic-gate 		ofp = sctp->sctp_current;
7570Sstevel@tonic-gate 	}
75811908SGeorge.Shepherd@Sun.COM 	/* Nothing to do */
75911908SGeorge.Shepherd@Sun.COM 	if (sctp->sctp_nfaddrs < 2)
76011908SGeorge.Shepherd@Sun.COM 		return (ofp);
7610Sstevel@tonic-gate 
76211908SGeorge.Shepherd@Sun.COM 	/*
76311908SGeorge.Shepherd@Sun.COM 	 * Find the next live peer address with zero strikes. In case
76411908SGeorge.Shepherd@Sun.COM 	 * there is none, find the one with the lowest number of strikes.
76511908SGeorge.Shepherd@Sun.COM 	 */
76612858SGeorge.Shepherd@Sun.COM 	min_strikes = ofp->strikes;
76712858SGeorge.Shepherd@Sun.COM 	nfp = ofp->next;
76812858SGeorge.Shepherd@Sun.COM 	while (nfp != ofp) {
76911908SGeorge.Shepherd@Sun.COM 		/* If reached end of list, continue scan from the head */
77011908SGeorge.Shepherd@Sun.COM 		if (nfp == NULL) {
77111908SGeorge.Shepherd@Sun.COM 			nfp = sctp->sctp_faddrs;
77211908SGeorge.Shepherd@Sun.COM 			continue;
7730Sstevel@tonic-gate 		}
77411908SGeorge.Shepherd@Sun.COM 		if (nfp->state == SCTP_FADDRS_ALIVE) {
77511908SGeorge.Shepherd@Sun.COM 			if (nfp->strikes == 0)
7760Sstevel@tonic-gate 				break;
77711908SGeorge.Shepherd@Sun.COM 			if (nfp->strikes < min_strikes) {
77811908SGeorge.Shepherd@Sun.COM 				min_strikes = nfp->strikes;
77911908SGeorge.Shepherd@Sun.COM 				saved_fp = nfp;
7800Sstevel@tonic-gate 			}
7810Sstevel@tonic-gate 		}
78212858SGeorge.Shepherd@Sun.COM 		nfp = nfp->next;
7830Sstevel@tonic-gate 	}
78411908SGeorge.Shepherd@Sun.COM 	/* If reached the old address, there is no zero strike path */
78511908SGeorge.Shepherd@Sun.COM 	if (nfp == ofp)
78611908SGeorge.Shepherd@Sun.COM 		nfp = NULL;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/*
78911908SGeorge.Shepherd@Sun.COM 	 * If there is a peer address with zero strikes  we use that, if not
79011908SGeorge.Shepherd@Sun.COM 	 * return a peer address with fewer strikes than the one last used,
79111908SGeorge.Shepherd@Sun.COM 	 * if neither exist we may as well stay with the old one.
7920Sstevel@tonic-gate 	 */
79311908SGeorge.Shepherd@Sun.COM 	if (nfp != NULL)
79411908SGeorge.Shepherd@Sun.COM 		return (nfp);
79511908SGeorge.Shepherd@Sun.COM 	if (saved_fp != NULL)
79611908SGeorge.Shepherd@Sun.COM 		return (saved_fp);
79711908SGeorge.Shepherd@Sun.COM 	return (ofp);
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 	}
81911042SErik.Nordmark@Sun.COM 	if (fp->ixa != NULL) {
82011042SErik.Nordmark@Sun.COM 		ixa_refrele(fp->ixa);
82111042SErik.Nordmark@Sun.COM 		fp->ixa = 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 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
841852Svi117747 	sctp->sctp_nfaddrs--;
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate void
8450Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
8460Sstevel@tonic-gate {
8470Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpn;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL) {
8500Sstevel@tonic-gate 		ASSERT(sctp->sctp_lastfaddr == NULL);
8510Sstevel@tonic-gate 		return;
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr != NULL);
8550Sstevel@tonic-gate 	sctp->sctp_lastfaddr = NULL;
8560Sstevel@tonic-gate 	sctp->sctp_current = NULL;
8570Sstevel@tonic-gate 	sctp->sctp_primary = NULL;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8620Sstevel@tonic-gate 		/* in conn fanout; need to hold lock */
8630Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
8670Sstevel@tonic-gate 		fpn = fp->next;
86811042SErik.Nordmark@Sun.COM 		if (fp->ixa != NULL) {
86911042SErik.Nordmark@Sun.COM 			ixa_refrele(fp->ixa);
87011042SErik.Nordmark@Sun.COM 			fp->ixa = NULL;
87111042SErik.Nordmark@Sun.COM 		}
8720Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
873852Svi117747 		sctp->sctp_nfaddrs--;
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	sctp->sctp_faddrs = NULL;
877852Svi117747 	ASSERT(sctp->sctp_nfaddrs == 0);
8780Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8790Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate void
8850Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate 	sctp_zap_faddrs(sctp, 0);
8880Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
89211042SErik.Nordmark@Sun.COM  * Build two SCTP header templates; one for IPv4 and one for IPv6.
89311042SErik.Nordmark@Sun.COM  * Store them in sctp_iphc and sctp_iphc6 respectively (and related fields).
89411042SErik.Nordmark@Sun.COM  * There are no IP addresses in the templates, but the port numbers and
89511042SErik.Nordmark@Sun.COM  * verifier are field in from the conn_t and sctp_t.
89611042SErik.Nordmark@Sun.COM  *
89711042SErik.Nordmark@Sun.COM  * Returns failure if can't allocate memory, or if there is a problem
89811042SErik.Nordmark@Sun.COM  * with a routing header/option.
89911042SErik.Nordmark@Sun.COM  *
90011042SErik.Nordmark@Sun.COM  * We allocate space for the minimum sctp header (sctp_hdr_t).
90111042SErik.Nordmark@Sun.COM  *
90211042SErik.Nordmark@Sun.COM  * We massage an routing option/header. There is no checksum implication
90311042SErik.Nordmark@Sun.COM  * for a routing header for sctp.
90411042SErik.Nordmark@Sun.COM  *
90511042SErik.Nordmark@Sun.COM  * Caller needs to update conn_wroff if desired.
90611042SErik.Nordmark@Sun.COM  *
90711042SErik.Nordmark@Sun.COM  * TSol notes: This assumes that a SCTP association has a single peer label
90811042SErik.Nordmark@Sun.COM  * since we only track a single pair of ipp_label_v4/v6 and not a separate one
90911042SErik.Nordmark@Sun.COM  * for each faddr.
9100Sstevel@tonic-gate  */
9110Sstevel@tonic-gate int
91211042SErik.Nordmark@Sun.COM sctp_build_hdrs(sctp_t *sctp, int sleep)
9130Sstevel@tonic-gate {
91411042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
91511042SErik.Nordmark@Sun.COM 	ip_pkt_t	*ipp = &connp->conn_xmit_ipp;
91611042SErik.Nordmark@Sun.COM 	uint_t		ip_hdr_length;
91711042SErik.Nordmark@Sun.COM 	uchar_t		*hdrs;
91811042SErik.Nordmark@Sun.COM 	uint_t		hdrs_len;
91911042SErik.Nordmark@Sun.COM 	uint_t		ulp_hdr_length = sizeof (sctp_hdr_t);
92011042SErik.Nordmark@Sun.COM 	ipha_t		*ipha;
92111042SErik.Nordmark@Sun.COM 	ip6_t		*ip6h;
9220Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
92311042SErik.Nordmark@Sun.COM 	in6_addr_t	v6src, v6dst;
92411042SErik.Nordmark@Sun.COM 	ipaddr_t	v4src, v4dst;
9250Sstevel@tonic-gate 
92611042SErik.Nordmark@Sun.COM 	v4src = connp->conn_saddr_v4;
92711042SErik.Nordmark@Sun.COM 	v4dst = connp->conn_faddr_v4;
92811042SErik.Nordmark@Sun.COM 	v6src = connp->conn_saddr_v6;
92911042SErik.Nordmark@Sun.COM 	v6dst = connp->conn_faddr_v6;
9300Sstevel@tonic-gate 
93111042SErik.Nordmark@Sun.COM 	/* First do IPv4 header */
93211042SErik.Nordmark@Sun.COM 	ip_hdr_length = ip_total_hdrs_len_v4(ipp);
9330Sstevel@tonic-gate 
93411042SErik.Nordmark@Sun.COM 	/* In case of TX label and IP options it can be too much */
93511042SErik.Nordmark@Sun.COM 	if (ip_hdr_length > IP_MAX_HDR_LENGTH) {
93611042SErik.Nordmark@Sun.COM 		/* Preserves existing TX errno for this */
93711042SErik.Nordmark@Sun.COM 		return (EHOSTUNREACH);
93811042SErik.Nordmark@Sun.COM 	}
93911042SErik.Nordmark@Sun.COM 	hdrs_len = ip_hdr_length + ulp_hdr_length;
94011042SErik.Nordmark@Sun.COM 	ASSERT(hdrs_len != 0);
9411676Sjpk 
94211042SErik.Nordmark@Sun.COM 	if (hdrs_len != sctp->sctp_iphc_len) {
94311042SErik.Nordmark@Sun.COM 		/* Allocate new before we free any old */
94411042SErik.Nordmark@Sun.COM 		hdrs = kmem_alloc(hdrs_len, sleep);
9450Sstevel@tonic-gate 		if (hdrs == NULL)
9460Sstevel@tonic-gate 			return (ENOMEM);
9470Sstevel@tonic-gate 
94811042SErik.Nordmark@Sun.COM 		if (sctp->sctp_iphc != NULL)
94911042SErik.Nordmark@Sun.COM 			kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
95011042SErik.Nordmark@Sun.COM 		sctp->sctp_iphc = hdrs;
95111042SErik.Nordmark@Sun.COM 		sctp->sctp_iphc_len = hdrs_len;
95211042SErik.Nordmark@Sun.COM 	} else {
95311042SErik.Nordmark@Sun.COM 		hdrs = sctp->sctp_iphc;
95411042SErik.Nordmark@Sun.COM 	}
95511042SErik.Nordmark@Sun.COM 	sctp->sctp_hdr_len = sctp->sctp_iphc_len;
95611042SErik.Nordmark@Sun.COM 	sctp->sctp_ip_hdr_len = ip_hdr_length;
95711042SErik.Nordmark@Sun.COM 
95811042SErik.Nordmark@Sun.COM 	sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length);
95911042SErik.Nordmark@Sun.COM 	sctp->sctp_sctph = sctph;
96011042SErik.Nordmark@Sun.COM 	sctph->sh_sport = connp->conn_lport;
96111042SErik.Nordmark@Sun.COM 	sctph->sh_dport = connp->conn_fport;
96211042SErik.Nordmark@Sun.COM 	sctph->sh_verf = sctp->sctp_fvtag;
96311042SErik.Nordmark@Sun.COM 	sctph->sh_chksum = 0;
96411042SErik.Nordmark@Sun.COM 
96511042SErik.Nordmark@Sun.COM 	ipha = (ipha_t *)hdrs;
96611042SErik.Nordmark@Sun.COM 	sctp->sctp_ipha = ipha;
96711042SErik.Nordmark@Sun.COM 
96811042SErik.Nordmark@Sun.COM 	ipha->ipha_src = v4src;
96911042SErik.Nordmark@Sun.COM 	ipha->ipha_dst = v4dst;
97011042SErik.Nordmark@Sun.COM 	ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, connp->conn_proto);
97111042SErik.Nordmark@Sun.COM 	ipha->ipha_length = htons(hdrs_len);
97211042SErik.Nordmark@Sun.COM 	ipha->ipha_fragment_offset_and_flags = 0;
97311042SErik.Nordmark@Sun.COM 
97411042SErik.Nordmark@Sun.COM 	if (ipp->ipp_fields & IPPF_IPV4_OPTIONS)
97511042SErik.Nordmark@Sun.COM 		(void) ip_massage_options(ipha, connp->conn_netstack);
97611042SErik.Nordmark@Sun.COM 
97711042SErik.Nordmark@Sun.COM 	/* Now IPv6 */
97811042SErik.Nordmark@Sun.COM 	ip_hdr_length = ip_total_hdrs_len_v6(ipp);
97911042SErik.Nordmark@Sun.COM 	hdrs_len = ip_hdr_length + ulp_hdr_length;
98011042SErik.Nordmark@Sun.COM 	ASSERT(hdrs_len != 0);
98111042SErik.Nordmark@Sun.COM 
98211042SErik.Nordmark@Sun.COM 	if (hdrs_len != sctp->sctp_iphc6_len) {
98311042SErik.Nordmark@Sun.COM 		/* Allocate new before we free any old */
98411042SErik.Nordmark@Sun.COM 		hdrs = kmem_alloc(hdrs_len, sleep);
98511042SErik.Nordmark@Sun.COM 		if (hdrs == NULL)
98611042SErik.Nordmark@Sun.COM 			return (ENOMEM);
98711042SErik.Nordmark@Sun.COM 
98811042SErik.Nordmark@Sun.COM 		if (sctp->sctp_iphc6 != NULL)
9890Sstevel@tonic-gate 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
9900Sstevel@tonic-gate 		sctp->sctp_iphc6 = hdrs;
9910Sstevel@tonic-gate 		sctp->sctp_iphc6_len = hdrs_len;
99211042SErik.Nordmark@Sun.COM 	} else {
99311042SErik.Nordmark@Sun.COM 		hdrs = sctp->sctp_iphc6;
9940Sstevel@tonic-gate 	}
99511042SErik.Nordmark@Sun.COM 	sctp->sctp_hdr6_len = sctp->sctp_iphc6_len;
99611042SErik.Nordmark@Sun.COM 	sctp->sctp_ip_hdr6_len = ip_hdr_length;
9970Sstevel@tonic-gate 
99811042SErik.Nordmark@Sun.COM 	sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length);
99911042SErik.Nordmark@Sun.COM 	sctp->sctp_sctph6 = sctph;
100011042SErik.Nordmark@Sun.COM 	sctph->sh_sport = connp->conn_lport;
100111042SErik.Nordmark@Sun.COM 	sctph->sh_dport = connp->conn_fport;
100211042SErik.Nordmark@Sun.COM 	sctph->sh_verf = sctp->sctp_fvtag;
100311042SErik.Nordmark@Sun.COM 	sctph->sh_chksum = 0;
10040Sstevel@tonic-gate 
100511042SErik.Nordmark@Sun.COM 	ip6h = (ip6_t *)hdrs;
100611042SErik.Nordmark@Sun.COM 	sctp->sctp_ip6h = ip6h;
10070Sstevel@tonic-gate 
100811042SErik.Nordmark@Sun.COM 	ip6h->ip6_src = v6src;
100911042SErik.Nordmark@Sun.COM 	ip6h->ip6_dst = v6dst;
101011042SErik.Nordmark@Sun.COM 	ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, connp->conn_proto,
101111042SErik.Nordmark@Sun.COM 	    connp->conn_flowinfo);
101211042SErik.Nordmark@Sun.COM 	ip6h->ip6_plen = htons(hdrs_len - IPV6_HDR_LEN);
10130Sstevel@tonic-gate 
101411042SErik.Nordmark@Sun.COM 	if (ipp->ipp_fields & IPPF_RTHDR) {
101511042SErik.Nordmark@Sun.COM 		uint8_t		*end;
101611042SErik.Nordmark@Sun.COM 		ip6_rthdr_t	*rth;
10170Sstevel@tonic-gate 
101811042SErik.Nordmark@Sun.COM 		end = (uint8_t *)ip6h + ip_hdr_length;
101911042SErik.Nordmark@Sun.COM 		rth = ip_find_rthdr_v6(ip6h, end);
102011042SErik.Nordmark@Sun.COM 		if (rth != NULL) {
102111042SErik.Nordmark@Sun.COM 			(void) ip_massage_options_v6(ip6h, rth,
102211042SErik.Nordmark@Sun.COM 			    connp->conn_netstack);
102311042SErik.Nordmark@Sun.COM 		}
10241676Sjpk 
102511042SErik.Nordmark@Sun.COM 		/*
102611042SErik.Nordmark@Sun.COM 		 * Verify that the first hop isn't a mapped address.
102711042SErik.Nordmark@Sun.COM 		 * Routers along the path need to do this verification
102811042SErik.Nordmark@Sun.COM 		 * for subsequent hops.
102911042SErik.Nordmark@Sun.COM 		 */
103011042SErik.Nordmark@Sun.COM 		if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst))
103111042SErik.Nordmark@Sun.COM 			return (EADDRNOTAVAIL);
10321676Sjpk 	}
10331676Sjpk 	return (0);
10341676Sjpk }
10351676Sjpk 
10361676Sjpk static int
103711042SErik.Nordmark@Sun.COM sctp_v4_label(sctp_t *sctp, sctp_faddr_t *fp)
10381676Sjpk {
103911042SErik.Nordmark@Sun.COM 	conn_t *connp = sctp->sctp_connp;
104011042SErik.Nordmark@Sun.COM 
104111042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa->ixa_flags & IXAF_IS_IPV4);
104211042SErik.Nordmark@Sun.COM 	return (conn_update_label(connp, fp->ixa, &fp->faddr,
104311042SErik.Nordmark@Sun.COM 	    &connp->conn_xmit_ipp));
104411042SErik.Nordmark@Sun.COM }
10451676Sjpk 
104611042SErik.Nordmark@Sun.COM static int
104711042SErik.Nordmark@Sun.COM sctp_v6_label(sctp_t *sctp, sctp_faddr_t *fp)
104811042SErik.Nordmark@Sun.COM {
104911042SErik.Nordmark@Sun.COM 	conn_t *connp = sctp->sctp_connp;
105011042SErik.Nordmark@Sun.COM 
105111042SErik.Nordmark@Sun.COM 	ASSERT(!(fp->ixa->ixa_flags & IXAF_IS_IPV4));
105211042SErik.Nordmark@Sun.COM 	return (conn_update_label(connp, fp->ixa, &fp->faddr,
105311042SErik.Nordmark@Sun.COM 	    &connp->conn_xmit_ipp));
10541676Sjpk }
10551676Sjpk 
10560Sstevel@tonic-gate /*
10570Sstevel@tonic-gate  * XXX implement more sophisticated logic
105811042SErik.Nordmark@Sun.COM  *
105911042SErik.Nordmark@Sun.COM  * Tsol note: We have already verified the addresses using tsol_check_dest
106011042SErik.Nordmark@Sun.COM  * in sctp_add_faddr, thus no need to redo that here.
106111042SErik.Nordmark@Sun.COM  * We do setup ipp_label_v4 and ipp_label_v6 based on which addresses
106211042SErik.Nordmark@Sun.COM  * we have.
10630Sstevel@tonic-gate  */
10641676Sjpk int
10651735Skcpoon sctp_set_hdraddrs(sctp_t *sctp)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate 	sctp_faddr_t *fp;
10680Sstevel@tonic-gate 	int gotv4 = 0;
10690Sstevel@tonic-gate 	int gotv6 = 0;
107011042SErik.Nordmark@Sun.COM 	conn_t *connp = sctp->sctp_connp;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs != NULL);
10730Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	/* Set up using the primary first */
107611042SErik.Nordmark@Sun.COM 	connp->conn_faddr_v6 = sctp->sctp_primary->faddr;
107711042SErik.Nordmark@Sun.COM 	/* saddr may be unspec; make_mp() will handle this */
107811042SErik.Nordmark@Sun.COM 	connp->conn_saddr_v6 = sctp->sctp_primary->saddr;
107911042SErik.Nordmark@Sun.COM 	connp->conn_laddr_v6 = connp->conn_saddr_v6;
10800Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) {
108111042SErik.Nordmark@Sun.COM 		if (!is_system_labeled() ||
108211042SErik.Nordmark@Sun.COM 		    sctp_v4_label(sctp, sctp->sctp_primary) == 0) {
10831676Sjpk 			gotv4 = 1;
108411042SErik.Nordmark@Sun.COM 			if (connp->conn_family == AF_INET) {
108511042SErik.Nordmark@Sun.COM 				goto done;
10861676Sjpk 			}
10870Sstevel@tonic-gate 		}
10880Sstevel@tonic-gate 	} else {
108911042SErik.Nordmark@Sun.COM 		if (!is_system_labeled() ||
109011042SErik.Nordmark@Sun.COM 		    sctp_v6_label(sctp, sctp->sctp_primary) == 0) {
10911676Sjpk 			gotv6 = 1;
109211042SErik.Nordmark@Sun.COM 		}
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
10960Sstevel@tonic-gate 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
109711042SErik.Nordmark@Sun.COM 			if (!is_system_labeled() ||
109811042SErik.Nordmark@Sun.COM 			    sctp_v4_label(sctp, fp) == 0) {
10991676Sjpk 				gotv4 = 1;
110011042SErik.Nordmark@Sun.COM 				if (connp->conn_family == AF_INET || gotv6) {
11011676Sjpk 					break;
11021676Sjpk 				}
11030Sstevel@tonic-gate 			}
11042283Skp158701 		} else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
110511042SErik.Nordmark@Sun.COM 			if (!is_system_labeled() ||
110611042SErik.Nordmark@Sun.COM 			    sctp_v6_label(sctp, fp) == 0) {
11071676Sjpk 				gotv6 = 1;
11081676Sjpk 				if (gotv4)
11091676Sjpk 					break;
11100Sstevel@tonic-gate 			}
11110Sstevel@tonic-gate 		}
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
111411042SErik.Nordmark@Sun.COM done:
11151676Sjpk 	if (!gotv4 && !gotv6)
11161676Sjpk 		return (EACCES);
11171676Sjpk 
11181676Sjpk 	return (0);
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate 
11218549SGeorge.Shepherd@Sun.COM /*
11228549SGeorge.Shepherd@Sun.COM  * got_errchunk is set B_TRUE only if called from validate_init_params(), when
11238549SGeorge.Shepherd@Sun.COM  * an ERROR chunk is already prepended the size of which needs updating for
11248549SGeorge.Shepherd@Sun.COM  * additional unrecognized parameters. Other callers either prepend the ERROR
11258549SGeorge.Shepherd@Sun.COM  * chunk with the correct size after calling this function, or they are calling
11268549SGeorge.Shepherd@Sun.COM  * to add an invalid parameter to an INIT_ACK chunk, in that case no ERROR chunk
11278549SGeorge.Shepherd@Sun.COM  * exists, the CAUSE blocks go into the INIT_ACK directly.
11288549SGeorge.Shepherd@Sun.COM  *
11298549SGeorge.Shepherd@Sun.COM  * *errmp will be non-NULL both when adding an additional CAUSE block to an
11308549SGeorge.Shepherd@Sun.COM  * existing prepended COOKIE ERROR chunk (processing params of an INIT_ACK),
11318549SGeorge.Shepherd@Sun.COM  * and when adding unrecognized parameters after the first, to an INIT_ACK
11328549SGeorge.Shepherd@Sun.COM  * (processing params of an INIT chunk).
11338549SGeorge.Shepherd@Sun.COM  */
11340Sstevel@tonic-gate void
11358549SGeorge.Shepherd@Sun.COM sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp,
11368549SGeorge.Shepherd@Sun.COM     boolean_t got_errchunk)
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	mblk_t *mp;
11390Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
11400Sstevel@tonic-gate 	size_t len;
11410Sstevel@tonic-gate 	int pad;
11428153SGeorge.Shepherd@Sun.COM 	sctp_chunk_hdr_t *ecp;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	len = sizeof (*ph) + ntohs(uph->sph_len);
11458153SGeorge.Shepherd@Sun.COM 	if ((pad = len % SCTP_ALIGN) != 0) {
11468153SGeorge.Shepherd@Sun.COM 		pad = SCTP_ALIGN - pad;
11470Sstevel@tonic-gate 		len += pad;
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
11500Sstevel@tonic-gate 	if (mp == NULL) {
11510Sstevel@tonic-gate 		return;
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
11550Sstevel@tonic-gate 	ph->sph_type = htons(PARM_UNRECOGNIZED);
11560Sstevel@tonic-gate 	ph->sph_len = htons(len - pad);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/* copy in the unrecognized parameter */
11590Sstevel@tonic-gate 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
11600Sstevel@tonic-gate 
11618153SGeorge.Shepherd@Sun.COM 	if (pad != 0)
11628153SGeorge.Shepherd@Sun.COM 		bzero((mp->b_rptr + len - pad), pad);
11638153SGeorge.Shepherd@Sun.COM 
11640Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
11650Sstevel@tonic-gate 	if (*errmp != NULL) {
11668153SGeorge.Shepherd@Sun.COM 		/*
11678549SGeorge.Shepherd@Sun.COM 		 * Update total length if an ERROR chunk, then link
11688549SGeorge.Shepherd@Sun.COM 		 * this CAUSE block to the possible chain of CAUSE
11698549SGeorge.Shepherd@Sun.COM 		 * blocks attached to the ERROR chunk or INIT_ACK
11708549SGeorge.Shepherd@Sun.COM 		 * being created.
11718153SGeorge.Shepherd@Sun.COM 		 */
11728549SGeorge.Shepherd@Sun.COM 		if (got_errchunk) {
11738549SGeorge.Shepherd@Sun.COM 			/* ERROR chunk already prepended */
11748549SGeorge.Shepherd@Sun.COM 			ecp = (sctp_chunk_hdr_t *)((*errmp)->b_rptr);
11758549SGeorge.Shepherd@Sun.COM 			ecp->sch_len = htons(ntohs(ecp->sch_len) + len);
11768549SGeorge.Shepherd@Sun.COM 		}
11770Sstevel@tonic-gate 		linkb(*errmp, mp);
11780Sstevel@tonic-gate 	} else {
11790Sstevel@tonic-gate 		*errmp = mp;
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate /*
11840Sstevel@tonic-gate  * o Bounds checking
11850Sstevel@tonic-gate  * o Updates remaining
11860Sstevel@tonic-gate  * o Checks alignment
11870Sstevel@tonic-gate  */
11880Sstevel@tonic-gate sctp_parm_hdr_t *
11890Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	int pad;
11920Sstevel@tonic-gate 	uint16_t len;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	len = ntohs(current->sph_len);
11950Sstevel@tonic-gate 	*remaining -= len;
11960Sstevel@tonic-gate 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
11970Sstevel@tonic-gate 		return (NULL);
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
12000Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
12010Sstevel@tonic-gate 		*remaining -= pad;
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
12040Sstevel@tonic-gate 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
12050Sstevel@tonic-gate 	return (current);
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate /*
12090Sstevel@tonic-gate  * Sets the address parameters given in the INIT chunk into sctp's
12100Sstevel@tonic-gate  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
12110Sstevel@tonic-gate  * no address parameters in the INIT chunk, a single faddr is created
12120Sstevel@tonic-gate  * from the ip hdr at the beginning of pkt.
12130Sstevel@tonic-gate  * If there already are existing addresses hanging from sctp, merge
12140Sstevel@tonic-gate  * them in, if the old info contains addresses which are not present
12150Sstevel@tonic-gate  * in this new info, get rid of them, and clean the pointers if there's
12160Sstevel@tonic-gate  * messages which have this as their target address.
12170Sstevel@tonic-gate  *
1218432Svi117747  * We also re-adjust the source address list here since the list may
1219432Svi117747  * contain more than what is actually part of the association. If
1220432Svi117747  * we get here from sctp_send_cookie_echo(), we are on the active
1221432Svi117747  * side and psctp will be NULL and ich will be the INIT-ACK chunk.
1222432Svi117747  * If we get here from sctp_accept_comm(), ich will be the INIT chunk
1223432Svi117747  * and psctp will the listening endpoint.
1224432Svi117747  *
1225432Svi117747  * INIT processing: When processing the INIT we inherit the src address
1226432Svi117747  * list from the listener. For a loopback or linklocal association, we
1227432Svi117747  * delete the list and just take the address from the IP header (since
1228432Svi117747  * that's how we created the INIT-ACK). Additionally, for loopback we
1229432Svi117747  * ignore the address params in the INIT. For determining which address
1230432Svi117747  * types were sent in the INIT-ACK we follow the same logic as in
1231432Svi117747  * creating the INIT-ACK. We delete addresses of the type that are not
1232432Svi117747  * supported by the peer.
1233432Svi117747  *
1234432Svi117747  * INIT-ACK processing: When processing the INIT-ACK since we had not
1235432Svi117747  * included addr params for loopback or linklocal addresses when creating
1236432Svi117747  * the INIT, we just use the address from the IP header. Further, for
1237432Svi117747  * loopback we ignore the addr param list. We mark addresses of the
1238432Svi117747  * type not supported by the peer as unconfirmed.
1239432Svi117747  *
1240432Svi117747  * In case of INIT processing we look for supported address types in the
1241432Svi117747  * supported address param, if present. In both cases the address type in
1242432Svi117747  * the IP header is supported as well as types for addresses in the param
1243432Svi117747  * list, if any.
1244432Svi117747  *
1245432Svi117747  * Once we have the supported address types sctp_check_saddr() runs through
1246432Svi117747  * the source address list and deletes or marks as unconfirmed address of
1247432Svi117747  * types not supported by the peer.
1248432Svi117747  *
12490Sstevel@tonic-gate  * Returns 0 on success, sys errno on failure
12500Sstevel@tonic-gate  */
12510Sstevel@tonic-gate int
12520Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
12530Sstevel@tonic-gate     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
12560Sstevel@tonic-gate 	ipha_t			*iph;
12570Sstevel@tonic-gate 	ip6_t			*ip6h;
1258432Svi117747 	in6_addr_t		hdrsaddr[1];
1259432Svi117747 	in6_addr_t		hdrdaddr[1];
12600Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
12610Sstevel@tonic-gate 	ssize_t			remaining;
12620Sstevel@tonic-gate 	int			isv4;
12630Sstevel@tonic-gate 	int			err;
12640Sstevel@tonic-gate 	sctp_faddr_t		*fp;
1265432Svi117747 	int			supp_af = 0;
1266432Svi117747 	boolean_t		check_saddr = B_TRUE;
1267852Svi117747 	in6_addr_t		curaddr;
12683448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
126911042SErik.Nordmark@Sun.COM 	conn_t			*connp = sctp->sctp_connp;
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	if (sctp_options != NULL)
12720Sstevel@tonic-gate 		*sctp_options = 0;
12730Sstevel@tonic-gate 
1274432Svi117747 	/* extract the address from the IP header */
1275432Svi117747 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1276432Svi117747 	if (isv4) {
1277432Svi117747 		iph = (ipha_t *)pkt->b_rptr;
1278432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr);
1279432Svi117747 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr);
1280432Svi117747 		supp_af |= PARM_SUPP_V4;
1281432Svi117747 	} else {
1282432Svi117747 		ip6h = (ip6_t *)pkt->b_rptr;
1283432Svi117747 		hdrsaddr[0] = ip6h->ip6_src;
1284432Svi117747 		hdrdaddr[0] = ip6h->ip6_dst;
1285432Svi117747 		supp_af |= PARM_SUPP_V6;
1286432Svi117747 	}
1287432Svi117747 
1288432Svi117747 	/*
1289432Svi117747 	 * Unfortunately, we can't delay this because adding an faddr
1290432Svi117747 	 * looks for the presence of the source address (from the ire
1291432Svi117747 	 * for the faddr) in the source address list. We could have
1292432Svi117747 	 * delayed this if, say, this was a loopback/linklocal connection.
1293432Svi117747 	 * Now, we just end up nuking this list and taking the addr from
1294432Svi117747 	 * the IP header for loopback/linklocal.
1295432Svi117747 	 */
12960Sstevel@tonic-gate 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
12970Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
13000Sstevel@tonic-gate 		if (err != 0)
13010Sstevel@tonic-gate 			return (err);
13020Sstevel@tonic-gate 	}
1303432Svi117747 	/*
1304432Svi117747 	 * We will add the faddr before parsing the address list as this
1305432Svi117747 	 * might be a loopback connection and we would not have to
1306432Svi117747 	 * go through the list.
1307432Svi117747 	 *
1308432Svi117747 	 * Make sure the header's addr is in the list
1309432Svi117747 	 */
1310432Svi117747 	fp = sctp_lookup_faddr(sctp, hdrsaddr);
1311432Svi117747 	if (fp == NULL) {
1312432Svi117747 		/* not included; add it now */
13131735Skcpoon 		err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE);
13141676Sjpk 		if (err != 0)
13151676Sjpk 			return (err);
13160Sstevel@tonic-gate 
1317432Svi117747 		/* sctp_faddrs will be the hdr addr */
1318432Svi117747 		fp = sctp->sctp_faddrs;
13190Sstevel@tonic-gate 	}
1320432Svi117747 	/* make the header addr the primary */
1321852Svi117747 
1322852Svi117747 	if (cl_sctp_assoc_change != NULL && psctp == NULL)
1323852Svi117747 		curaddr = sctp->sctp_current->faddr;
1324852Svi117747 
1325432Svi117747 	sctp->sctp_primary = fp;
1326432Svi117747 	sctp->sctp_current = fp;
1327432Svi117747 	sctp->sctp_mss = fp->sfa_pmss;
13280Sstevel@tonic-gate 
1329432Svi117747 	/* For loopback connections & linklocal get address from the header */
1330432Svi117747 	if (sctp->sctp_loopback || sctp->sctp_linklocal) {
1331432Svi117747 		if (sctp->sctp_nsaddrs != 0)
1332432Svi117747 			sctp_free_saddrs(sctp);
1333852Svi117747 		if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0)
1334432Svi117747 			return (err);
1335432Svi117747 		/* For loopback ignore address list */
1336432Svi117747 		if (sctp->sctp_loopback)
1337432Svi117747 			return (0);
1338432Svi117747 		check_saddr = B_FALSE;
1339432Svi117747 	}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
13420Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
13430Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
13440Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
1345432Svi117747 		if (check_saddr) {
1346432Svi117747 			sctp_check_saddr(sctp, supp_af, psctp == NULL ?
13474818Skcpoon 			    B_FALSE : B_TRUE, hdrdaddr);
1348432Svi117747 		}
1349852Svi117747 		ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1350432Svi117747 		return (0);
13510Sstevel@tonic-gate 	}
1352432Svi117747 
13530Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
13540Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
13550Sstevel@tonic-gate 
1356432Svi117747 	/* params will have already been byteordered when validating */
13570Sstevel@tonic-gate 	while (ph != NULL) {
1358432Svi117747 		if (ph->sph_type == htons(PARM_SUPP_ADDRS)) {
1359432Svi117747 			int		plen;
1360432Svi117747 			uint16_t	*p;
1361432Svi117747 			uint16_t	addrtype;
1362432Svi117747 
1363432Svi117747 			ASSERT(psctp != NULL);
1364432Svi117747 			plen = ntohs(ph->sph_len);
1365432Svi117747 			p = (uint16_t *)(ph + 1);
1366432Svi117747 			while (plen > 0) {
1367432Svi117747 				addrtype = ntohs(*p);
1368432Svi117747 				switch (addrtype) {
1369432Svi117747 					case PARM_ADDR6:
1370432Svi117747 						supp_af |= PARM_SUPP_V6;
1371432Svi117747 						break;
1372432Svi117747 					case PARM_ADDR4:
1373432Svi117747 						supp_af |= PARM_SUPP_V4;
1374432Svi117747 						break;
1375432Svi117747 					default:
1376432Svi117747 						break;
1377432Svi117747 				}
1378432Svi117747 				p++;
1379432Svi117747 				plen -= sizeof (*p);
1380432Svi117747 			}
1381432Svi117747 		} else if (ph->sph_type == htons(PARM_ADDR4)) {
13820Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
13830Sstevel@tonic-gate 				in6_addr_t addr;
13840Sstevel@tonic-gate 				ipaddr_t ta;
13850Sstevel@tonic-gate 
1386432Svi117747 				supp_af |= PARM_SUPP_V4;
13870Sstevel@tonic-gate 				/*
13880Sstevel@tonic-gate 				 * Screen out broad/multicasts & loopback.
13890Sstevel@tonic-gate 				 * If the endpoint only accepts v6 address,
13900Sstevel@tonic-gate 				 * go to the next one.
13914818Skcpoon 				 *
13924818Skcpoon 				 * Subnet broadcast check is done in
13934818Skcpoon 				 * sctp_add_faddr().  If the address is
13944818Skcpoon 				 * a broadcast address, it won't be added.
13950Sstevel@tonic-gate 				 */
13960Sstevel@tonic-gate 				bcopy(ph + 1, &ta, sizeof (ta));
13970Sstevel@tonic-gate 				if (ta == 0 ||
13980Sstevel@tonic-gate 				    ta == INADDR_BROADCAST ||
13990Sstevel@tonic-gate 				    ta == htonl(INADDR_LOOPBACK) ||
140011042SErik.Nordmark@Sun.COM 				    CLASSD(ta) || connp->conn_ipv6_v6only) {
14010Sstevel@tonic-gate 					goto next;
14020Sstevel@tonic-gate 				}
14030Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
14040Sstevel@tonic-gate 				    (ph + 1), &addr);
14054818Skcpoon 
14060Sstevel@tonic-gate 				/* Check for duplicate. */
14070Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
14080Sstevel@tonic-gate 					goto next;
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 				/* OK, add it to the faddr set */
14111735Skcpoon 				err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP,
14121735Skcpoon 				    B_FALSE);
14134818Skcpoon 				/* Something is wrong...  Try the next one. */
14141676Sjpk 				if (err != 0)
14154818Skcpoon 					goto next;
14160Sstevel@tonic-gate 			}
14170Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
141811042SErik.Nordmark@Sun.COM 		    connp->conn_family == AF_INET6) {
14190Sstevel@tonic-gate 			/* An v4 socket should not take v6 addresses. */
14200Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
14210Sstevel@tonic-gate 				in6_addr_t *addr6;
14220Sstevel@tonic-gate 
1423432Svi117747 				supp_af |= PARM_SUPP_V6;
14240Sstevel@tonic-gate 				addr6 = (in6_addr_t *)(ph + 1);
14250Sstevel@tonic-gate 				/*
14260Sstevel@tonic-gate 				 * Screen out link locals, mcast, loopback
14270Sstevel@tonic-gate 				 * and bogus v6 address.
14280Sstevel@tonic-gate 				 */
14290Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
14300Sstevel@tonic-gate 				    IN6_IS_ADDR_MULTICAST(addr6) ||
14310Sstevel@tonic-gate 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
14320Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
14330Sstevel@tonic-gate 					goto next;
14340Sstevel@tonic-gate 				}
14350Sstevel@tonic-gate 				/* Check for duplicate. */
14360Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
14370Sstevel@tonic-gate 					goto next;
14380Sstevel@tonic-gate 
14391676Sjpk 				err = sctp_add_faddr(sctp,
14401735Skcpoon 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP,
14411735Skcpoon 				    B_FALSE);
14424818Skcpoon 				/* Something is wrong...  Try the next one. */
14431676Sjpk 				if (err != 0)
14444818Skcpoon 					goto next;
14450Sstevel@tonic-gate 			}
14460Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
14470Sstevel@tonic-gate 			if (sctp_options != NULL)
14480Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
14490Sstevel@tonic-gate 		} /* else; skip */
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate next:
14520Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
14530Sstevel@tonic-gate 	}
1454432Svi117747 	if (check_saddr) {
1455432Svi117747 		sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE :
14564818Skcpoon 		    B_TRUE, hdrdaddr);
14570Sstevel@tonic-gate 	}
1458852Svi117747 	ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1459852Svi117747 	/*
1460852Svi117747 	 * We have the right address list now, update clustering's
1461852Svi117747 	 * knowledge because when we sent the INIT we had just added
1462852Svi117747 	 * the address the INIT was sent to.
1463852Svi117747 	 */
1464852Svi117747 	if (psctp == NULL && cl_sctp_assoc_change != NULL) {
1465852Svi117747 		uchar_t	*alist;
1466852Svi117747 		size_t	asize;
1467852Svi117747 		uchar_t	*dlist;
1468852Svi117747 		size_t	dsize;
1469852Svi117747 
1470852Svi117747 		asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
1471852Svi117747 		alist = kmem_alloc(asize, KM_NOSLEEP);
14721735Skcpoon 		if (alist == NULL) {
14733448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1474852Svi117747 			return (ENOMEM);
14751735Skcpoon 		}
1476852Svi117747 		/*
1477852Svi117747 		 * Just include the address the INIT was sent to in the
1478852Svi117747 		 * delete list and send the entire faddr list. We could
1479852Svi117747 		 * do it differently (i.e include all the addresses in the
1480852Svi117747 		 * add list even if it contains the original address OR
1481852Svi117747 		 * remove the original address from the add list etc.), but
1482852Svi117747 		 * this seems reasonable enough.
1483852Svi117747 		 */
1484852Svi117747 		dsize = sizeof (in6_addr_t);
1485852Svi117747 		dlist = kmem_alloc(dsize, KM_NOSLEEP);
1486852Svi117747 		if (dlist == NULL) {
1487852Svi117747 			kmem_free(alist, asize);
14883448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1489852Svi117747 			return (ENOMEM);
1490852Svi117747 		}
1491852Svi117747 		bcopy(&curaddr, dlist, sizeof (curaddr));
1492852Svi117747 		sctp_get_faddr_list(sctp, alist, asize);
149311042SErik.Nordmark@Sun.COM 		(*cl_sctp_assoc_change)(connp->conn_family, alist, asize,
1494852Svi117747 		    sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR,
1495852Svi117747 		    (cl_sctp_handle_t)sctp);
1496852Svi117747 		/* alist and dlist will be freed by the clustering module */
1497852Svi117747 	}
14980Sstevel@tonic-gate 	return (0);
14990Sstevel@tonic-gate }
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate /*
15020Sstevel@tonic-gate  * Returns 0 if the check failed and the restart should be refused,
15030Sstevel@tonic-gate  * 1 if the check succeeded.
15040Sstevel@tonic-gate  */
15050Sstevel@tonic-gate int
15060Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
150711042SErik.Nordmark@Sun.COM     int sleep, sctp_stack_t *sctps, ip_recv_attr_t *ira)
15080Sstevel@tonic-gate {
15094964Skcpoon 	sctp_faddr_t *fp, *fphead = NULL;
15100Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
15110Sstevel@tonic-gate 	ssize_t remaining;
15120Sstevel@tonic-gate 	int isv4;
15130Sstevel@tonic-gate 	ipha_t *iph;
15140Sstevel@tonic-gate 	ip6_t *ip6h;
15150Sstevel@tonic-gate 	in6_addr_t hdraddr[1];
15160Sstevel@tonic-gate 	int retval = 0;
15170Sstevel@tonic-gate 	sctp_tf_t *tf;
15180Sstevel@tonic-gate 	sctp_t *sctp;
15190Sstevel@tonic-gate 	int compres;
15200Sstevel@tonic-gate 	sctp_init_chunk_t *init;
15210Sstevel@tonic-gate 	int nadded = 0;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/* extract the address from the IP header */
15240Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
15250Sstevel@tonic-gate 	if (isv4) {
15260Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
15270Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
15280Sstevel@tonic-gate 	} else {
15290Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
15300Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
15310Sstevel@tonic-gate 	}
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
15340Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
15350Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
15360Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
15370Sstevel@tonic-gate 		/* no parameters; restart OK */
15380Sstevel@tonic-gate 		return (1);
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
15410Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	while (ph != NULL) {
15444964Skcpoon 		sctp_faddr_t *fpa = NULL;
15454964Skcpoon 
15460Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
15470Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
15480Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
15490Sstevel@tonic-gate 				in6_addr_t addr;
15500Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
15510Sstevel@tonic-gate 				    (ph + 1), &addr);
15520Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15530Sstevel@tonic-gate 				    sleep);
15544964Skcpoon 				if (fpa == NULL) {
15550Sstevel@tonic-gate 					goto done;
15560Sstevel@tonic-gate 				}
15570Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
15580Sstevel@tonic-gate 				fpa->faddr = addr;
15590Sstevel@tonic-gate 				fpa->next = NULL;
15600Sstevel@tonic-gate 			}
15610Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
15620Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
15630Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15640Sstevel@tonic-gate 				    sleep);
15654964Skcpoon 				if (fpa == NULL) {
15660Sstevel@tonic-gate 					goto done;
15670Sstevel@tonic-gate 				}
15680Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
15690Sstevel@tonic-gate 				bcopy(ph + 1, &fpa->faddr,
15700Sstevel@tonic-gate 				    sizeof (fpa->faddr));
15710Sstevel@tonic-gate 				fpa->next = NULL;
15720Sstevel@tonic-gate 			}
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 		/* link in the new addr, if it was an addr param */
15754964Skcpoon 		if (fpa != NULL) {
15764964Skcpoon 			if (fphead == NULL) {
15770Sstevel@tonic-gate 				fphead = fpa;
15780Sstevel@tonic-gate 			} else {
15794964Skcpoon 				fpa->next = fphead;
15804964Skcpoon 				fphead = fpa;
15810Sstevel@tonic-gate 			}
15820Sstevel@tonic-gate 		}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
15850Sstevel@tonic-gate 	}
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	if (fphead == NULL) {
15880Sstevel@tonic-gate 		/* no addr parameters; restart OK */
15890Sstevel@tonic-gate 		return (1);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	/*
15930Sstevel@tonic-gate 	 * got at least one; make sure the header's addr is
15940Sstevel@tonic-gate 	 * in the list
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
15974964Skcpoon 	if (fp == NULL) {
15980Sstevel@tonic-gate 		/* not included; add it now */
15990Sstevel@tonic-gate 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
16004964Skcpoon 		if (fp == NULL) {
16010Sstevel@tonic-gate 			goto done;
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 		bzero(fp, sizeof (*fp));
16040Sstevel@tonic-gate 		fp->faddr = *hdraddr;
16050Sstevel@tonic-gate 		fp->next = fphead;
16060Sstevel@tonic-gate 		fphead = fp;
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	/*
16100Sstevel@tonic-gate 	 * Now, we can finally do the check: For each sctp instance
16110Sstevel@tonic-gate 	 * on the hash line for ports, compare its faddr set against
16120Sstevel@tonic-gate 	 * the new one. If the new one is a strict subset of any
16130Sstevel@tonic-gate 	 * existing sctp's faddrs, the restart is OK. However, if there
16140Sstevel@tonic-gate 	 * is an overlap, this could be an attack, so return failure.
16150Sstevel@tonic-gate 	 * If all sctp's faddrs are disjoint, this is a legitimate new
16160Sstevel@tonic-gate 	 * association.
16170Sstevel@tonic-gate 	 */
16183448Sdh155122 	tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]);
16190Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
162211042SErik.Nordmark@Sun.COM 		if (ports != sctp->sctp_connp->conn_ports) {
16230Sstevel@tonic-gate 			continue;
16240Sstevel@tonic-gate 		}
16250Sstevel@tonic-gate 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
16260Sstevel@tonic-gate 		if (compres <= SCTP_ADDR_SUBSET) {
16270Sstevel@tonic-gate 			retval = 1;
16280Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16290Sstevel@tonic-gate 			goto done;
16300Sstevel@tonic-gate 		}
16310Sstevel@tonic-gate 		if (compres == SCTP_ADDR_OVERLAP) {
16320Sstevel@tonic-gate 			dprint(1,
16330Sstevel@tonic-gate 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
16341676Sjpk 			    SCTP_PRINTADDR(*hdraddr), (void *)sctp));
16350Sstevel@tonic-gate 			/*
16360Sstevel@tonic-gate 			 * While we still hold the lock, we need to
16370Sstevel@tonic-gate 			 * figure out which addresses have been
16380Sstevel@tonic-gate 			 * added so we can include them in the abort
16390Sstevel@tonic-gate 			 * we will send back. Since these faddrs will
16400Sstevel@tonic-gate 			 * never be used, we overload the rto field
16410Sstevel@tonic-gate 			 * here, setting it to 0 if the address was
16420Sstevel@tonic-gate 			 * not added, 1 if it was added.
16430Sstevel@tonic-gate 			 */
16440Sstevel@tonic-gate 			for (fp = fphead; fp; fp = fp->next) {
16450Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &fp->faddr)) {
16460Sstevel@tonic-gate 					fp->rto = 0;
16470Sstevel@tonic-gate 				} else {
16480Sstevel@tonic-gate 					fp->rto = 1;
16490Sstevel@tonic-gate 					nadded++;
16500Sstevel@tonic-gate 				}
16510Sstevel@tonic-gate 			}
16520Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16530Sstevel@tonic-gate 			goto done;
16540Sstevel@tonic-gate 		}
16550Sstevel@tonic-gate 	}
16560Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	/* All faddrs are disjoint; legit new association */
16590Sstevel@tonic-gate 	retval = 1;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate done:
16620Sstevel@tonic-gate 	/* If are attempted adds, send back an abort listing the addrs */
16630Sstevel@tonic-gate 	if (nadded > 0) {
16640Sstevel@tonic-gate 		void *dtail;
16650Sstevel@tonic-gate 		size_t dlen;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
16680Sstevel@tonic-gate 		if (dtail == NULL) {
16690Sstevel@tonic-gate 			goto cleanup;
16700Sstevel@tonic-gate 		}
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 		ph = dtail;
16730Sstevel@tonic-gate 		dlen = 0;
16740Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fp->next) {
16750Sstevel@tonic-gate 			if (fp->rto == 0) {
16760Sstevel@tonic-gate 				continue;
16770Sstevel@tonic-gate 			}
16780Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
16790Sstevel@tonic-gate 				ipaddr_t addr4;
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR4);
16820Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR4_LEN);
16830Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
16840Sstevel@tonic-gate 				ph++;
16850Sstevel@tonic-gate 				bcopy(&addr4, ph, sizeof (addr4));
16860Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
16870Sstevel@tonic-gate 				    ((char *)ph + sizeof (addr4));
16880Sstevel@tonic-gate 				dlen += PARM_ADDR4_LEN;
16890Sstevel@tonic-gate 			} else {
16900Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR6);
16910Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR6_LEN);
16920Sstevel@tonic-gate 				ph++;
16930Sstevel@tonic-gate 				bcopy(&fp->faddr, ph, sizeof (fp->faddr));
16940Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
16950Sstevel@tonic-gate 				    ((char *)ph + sizeof (fp->faddr));
16960Sstevel@tonic-gate 				dlen += PARM_ADDR6_LEN;
16970Sstevel@tonic-gate 			}
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 		/* Send off the abort */
17010Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ich),
170211042SErik.Nordmark@Sun.COM 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE,
170311042SErik.Nordmark@Sun.COM 		    ira);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
17060Sstevel@tonic-gate 	}
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate cleanup:
17090Sstevel@tonic-gate 	/* Clean up */
17100Sstevel@tonic-gate 	if (fphead) {
17110Sstevel@tonic-gate 		sctp_faddr_t *fpn;
17120Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fpn) {
17130Sstevel@tonic-gate 			fpn = fp->next;
171411042SErik.Nordmark@Sun.COM 			if (fp->ixa != NULL) {
171511042SErik.Nordmark@Sun.COM 				ixa_refrele(fp->ixa);
171611042SErik.Nordmark@Sun.COM 				fp->ixa = NULL;
171711042SErik.Nordmark@Sun.COM 			}
17180Sstevel@tonic-gate 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
17190Sstevel@tonic-gate 		}
17200Sstevel@tonic-gate 	}
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	return (retval);
17230Sstevel@tonic-gate }
17240Sstevel@tonic-gate 
17251932Svi117747 /*
17261932Svi117747  * Reset any state related to transmitted chunks.
17271932Svi117747  */
17280Sstevel@tonic-gate void
17290Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp)
17300Sstevel@tonic-gate {
17311932Svi117747 	sctp_faddr_t	*fp;
17323448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17331932Svi117747 	mblk_t		*mp;
17340Sstevel@tonic-gate 
17351932Svi117747 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
17363448Sdh155122 		fp->ssthresh = sctps->sctps_initial_mtu;
17373795Skcpoon 		SET_CWND(fp, fp->sfa_pmss, sctps->sctps_slow_start_initial);
17380Sstevel@tonic-gate 		fp->suna = 0;
17390Sstevel@tonic-gate 		fp->pba = 0;
17400Sstevel@tonic-gate 	}
17411932Svi117747 	/*
17421932Svi117747 	 * Clean up the transmit list as well since we have reset accounting
17431932Svi117747 	 * on all the fps. Send event upstream, if required.
17441932Svi117747 	 */
17451932Svi117747 	while ((mp = sctp->sctp_xmit_head) != NULL) {
17461932Svi117747 		sctp->sctp_xmit_head = mp->b_next;
17471932Svi117747 		mp->b_next = NULL;
17481932Svi117747 		if (sctp->sctp_xmit_head != NULL)
17491932Svi117747 			sctp->sctp_xmit_head->b_prev = NULL;
17501932Svi117747 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
17511932Svi117747 	}
17521932Svi117747 	sctp->sctp_xmit_head = NULL;
17531932Svi117747 	sctp->sctp_xmit_tail = NULL;
17541932Svi117747 	sctp->sctp_xmit_unacked = NULL;
17551932Svi117747 
17561932Svi117747 	sctp->sctp_unacked = 0;
17571932Svi117747 	/*
17581932Svi117747 	 * Any control message as well. We will clean-up this list as well.
17591932Svi117747 	 * This contains any pending ASCONF request that we have queued/sent.
17601932Svi117747 	 * If we do get an ACK we will just drop it. However, given that
17611932Svi117747 	 * we are restarting chances are we aren't going to get any.
17621932Svi117747 	 */
17631932Svi117747 	if (sctp->sctp_cxmit_list != NULL)
17641932Svi117747 		sctp_asconf_free_cxmit(sctp, NULL);
17651932Svi117747 	sctp->sctp_cxmit_list = NULL;
17661932Svi117747 	sctp->sctp_cchunk_pend = 0;
17671932Svi117747 
17681932Svi117747 	sctp->sctp_rexmitting = B_FALSE;
17691932Svi117747 	sctp->sctp_rxt_nxttsn = 0;
17701932Svi117747 	sctp->sctp_rxt_maxtsn = 0;
17711932Svi117747 
17721932Svi117747 	sctp->sctp_zero_win_probe = B_FALSE;
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate static void
17761735Skcpoon sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr,
17771735Skcpoon     mblk_t *timer_mp)
17780Sstevel@tonic-gate {
17793448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
17803448Sdh155122 
178111042SErik.Nordmark@Sun.COM 	ASSERT(fp->ixa != NULL);
178211042SErik.Nordmark@Sun.COM 
17830Sstevel@tonic-gate 	bcopy(addr, &fp->faddr, sizeof (*addr));
17840Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
17850Sstevel@tonic-gate 		fp->isv4 = 1;
17860Sstevel@tonic-gate 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
17874691Skcpoon 		fp->sfa_pmss =
17884691Skcpoon 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) &
17894691Skcpoon 		    ~(SCTP_ALIGN - 1);
179011042SErik.Nordmark@Sun.COM 		fp->ixa->ixa_flags |= IXAF_IS_IPV4;
17910Sstevel@tonic-gate 	} else {
17920Sstevel@tonic-gate 		fp->isv4 = 0;
17933448Sdh155122 		fp->sfa_pmss =
17943448Sdh155122 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) &
17953448Sdh155122 		    ~(SCTP_ALIGN - 1);
179611042SErik.Nordmark@Sun.COM 		fp->ixa->ixa_flags &= ~IXAF_IS_IPV4;
17970Sstevel@tonic-gate 	}
17983448Sdh155122 	fp->cwnd = sctps->sctps_slow_start_initial * fp->sfa_pmss;
179912474SGeorge.Shepherd@Sun.COM 	fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_rto_max_init);
180010212SGeorge.Shepherd@Sun.COM 	SCTP_MAX_RTO(sctp, fp);
18010Sstevel@tonic-gate 	fp->srtt = -1;
18020Sstevel@tonic-gate 	fp->rtt_updates = 0;
18030Sstevel@tonic-gate 	fp->strikes = 0;
18040Sstevel@tonic-gate 	fp->max_retr = sctp->sctp_pp_max_rxt;
18050Sstevel@tonic-gate 	/* Mark it as not confirmed. */
18060Sstevel@tonic-gate 	fp->state = SCTP_FADDRS_UNCONFIRMED;
18070Sstevel@tonic-gate 	fp->hb_interval = sctp->sctp_hb_interval;
18083448Sdh155122 	fp->ssthresh = sctps->sctps_initial_ssthresh;
18090Sstevel@tonic-gate 	fp->suna = 0;
18100Sstevel@tonic-gate 	fp->pba = 0;
18110Sstevel@tonic-gate 	fp->acked = 0;
181211066Srafael.vanoni@sun.com 	fp->lastactive = fp->hb_expiry = ddi_get_lbolt64();
18131735Skcpoon 	fp->timer_mp = timer_mp;
18140Sstevel@tonic-gate 	fp->hb_pending = B_FALSE;
18154818Skcpoon 	fp->hb_enabled = B_TRUE;
18160Sstevel@tonic-gate 	fp->df = 1;
18170Sstevel@tonic-gate 	fp->pmtu_discovered = 0;
18180Sstevel@tonic-gate 	fp->next = NULL;
18190Sstevel@tonic-gate 	fp->T3expire = 0;
18200Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret,
18210Sstevel@tonic-gate 	    sizeof (fp->hb_secret));
18223795Skcpoon 	fp->rxt_unacked = 0;
18230Sstevel@tonic-gate 
182411042SErik.Nordmark@Sun.COM 	sctp_get_dest(sctp, fp);
18250Sstevel@tonic-gate }
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate /*ARGSUSED*/
18286712Stomee static int
18296712Stomee faddr_constructor(void *buf, void *arg, int flags)
18306712Stomee {
18316712Stomee 	sctp_faddr_t *fp = buf;
18326712Stomee 
18336712Stomee 	fp->timer_mp = NULL;
18346712Stomee 	fp->timer_running = 0;
18356712Stomee 
18366712Stomee 	fp->rc_timer_mp = NULL;
18376712Stomee 	fp->rc_timer_running = 0;
18386712Stomee 
18396712Stomee 	return (0);
18406712Stomee }
18416712Stomee 
18426712Stomee /*ARGSUSED*/
18430Sstevel@tonic-gate static void
18446712Stomee faddr_destructor(void *buf, void *arg)
18450Sstevel@tonic-gate {
18460Sstevel@tonic-gate 	sctp_faddr_t *fp = buf;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	ASSERT(fp->timer_mp == NULL);
18490Sstevel@tonic-gate 	ASSERT(fp->timer_running == 0);
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	ASSERT(fp->rc_timer_mp == NULL);
18520Sstevel@tonic-gate 	ASSERT(fp->rc_timer_running == 0);
18530Sstevel@tonic-gate }
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate void
18561676Sjpk sctp_faddr_init(void)
18570Sstevel@tonic-gate {
18580Sstevel@tonic-gate 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
18596712Stomee 	    sizeof (sctp_faddr_t), 0, faddr_constructor, faddr_destructor,
18600Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
18610Sstevel@tonic-gate }
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate void
18641676Sjpk sctp_faddr_fini(void)
18650Sstevel@tonic-gate {
18660Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_faddr_cache);
18670Sstevel@tonic-gate }
1868