17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2177c67f2fSkcpoon 
227c478bd9Sstevel@tonic-gate /*
23fd7b5aedSGeorge Shepherd  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/systm.h>
287c478bd9Sstevel@tonic-gate #include <sys/stream.h>
2945916cd2Sjpk #include <sys/strsubr.h>
307c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/socket.h>
347c478bd9Sstevel@tonic-gate #include <sys/random.h>
3545916cd2Sjpk #include <sys/tsol/tndb.h>
3645916cd2Sjpk #include <sys/tsol/tnet.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <netinet/in.h>
397c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
407c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <inet/common.h>
437c478bd9Sstevel@tonic-gate #include <inet/ip.h>
447c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
457c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
46bd670b35SErik Nordmark #include <inet/ip_if.h>
47bd670b35SErik Nordmark #include <inet/ip_ndp.h>
487c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
497c478bd9Sstevel@tonic-gate #include <inet/nd.h>
507c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
517c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
527c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
5345916cd2Sjpk 
547c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
557c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
56769b977dSvi #include "sctp_asconf.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_faddr_cache;
5977c67f2fSkcpoon static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *);
607c478bd9Sstevel@tonic-gate 
61bd670b35SErik Nordmark /* Set the source address.  Refer to comments in sctp_get_dest(). */
62769b977dSvi void
sctp_set_saddr(sctp_t * sctp,sctp_faddr_t * fp)63769b977dSvi sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp)
647c478bd9Sstevel@tonic-gate {
656be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	boolean_t v6 = !fp->sf_isv4;
66c31292eeSkcpoon 	boolean_t addr_set;
67b46e5978Sjarrett 
686be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_saddr = sctp_get_valid_addr(sctp, v6, &addr_set);
69c31292eeSkcpoon 	/*
70c31292eeSkcpoon 	 * If there is no source address avaialble, mark this peer address
71c31292eeSkcpoon 	 * as unreachable for now.  When the heartbeat timer fires, it will
72bd670b35SErik Nordmark 	 * call sctp_get_dest() to re-check if there is any source address
73c31292eeSkcpoon 	 * available.
74c31292eeSkcpoon 	 */
75c31292eeSkcpoon 	if (!addr_set)
766be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_state = SCTP_FADDRS_UNREACH;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
80bd670b35SErik Nordmark  * Call this function to get information about a peer addr fp.
81bd670b35SErik Nordmark  *
82bd670b35SErik Nordmark  * Uses ip_attr_connect to avoid explicit use of ire and source address
83bd670b35SErik Nordmark  * selection.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate void
sctp_get_dest(sctp_t * sctp,sctp_faddr_t * fp)86bd670b35SErik Nordmark sctp_get_dest(sctp_t *sctp, sctp_faddr_t *fp)
877c478bd9Sstevel@tonic-gate {
8877c67f2fSkcpoon 	in6_addr_t	laddr;
89bd670b35SErik Nordmark 	in6_addr_t	nexthop;
907c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t *sp;
9177c67f2fSkcpoon 	int		hdrlen;
92f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
93bd670b35SErik Nordmark 	conn_t		*connp = sctp->sctp_connp;
94bd670b35SErik Nordmark 	iulp_t		uinfo;
95bd670b35SErik Nordmark 	uint_t		pmtu;
96bd670b35SErik Nordmark 	int		error;
97bd670b35SErik Nordmark 	uint32_t	flags = IPDF_VERIFY_DST | IPDF_IPSEC |
98bd670b35SErik Nordmark 	    IPDF_SELECT_SRC | IPDF_UNIQUE_DCE;
997c478bd9Sstevel@tonic-gate 
100bd670b35SErik Nordmark 	/*
101bd670b35SErik Nordmark 	 * Tell sctp_make_mp it needs to call us again should we not
102bd670b35SErik Nordmark 	 * complete and set the saddr.
103bd670b35SErik Nordmark 	 */
1046be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_saddr = ipv6_all_zeros;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/*
1077c478bd9Sstevel@tonic-gate 	 * If this addr is not reachable, mark it as unconfirmed for now, the
1087c478bd9Sstevel@tonic-gate 	 * state will be changed back to unreachable later in this function
1097c478bd9Sstevel@tonic-gate 	 * if it is still the case.
1107c478bd9Sstevel@tonic-gate 	 */
1116be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_state == SCTP_FADDRS_UNREACH) {
1126be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_state = SCTP_FADDRS_UNCONFIRMED;
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
115bd670b35SErik Nordmark 	/*
116bd670b35SErik Nordmark 	 * Socket is connected - enable PMTU discovery.
117bd670b35SErik Nordmark 	 */
118bd670b35SErik Nordmark 	if (!sctps->sctps_ignore_path_mtu)
1196be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
12045916cd2Sjpk 
1216be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ip_attr_nexthop(&connp->conn_xmit_ipp, fp->sf_ixa, &fp->sf_faddr,
122bd670b35SErik Nordmark 	    &nexthop);
1237c478bd9Sstevel@tonic-gate 
1246be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	laddr = fp->sf_saddr;
1256be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	error = ip_attr_connect(connp, fp->sf_ixa, &laddr, &fp->sf_faddr,
1266be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	    &nexthop, connp->conn_fport, &laddr, &uinfo, flags);
127bd670b35SErik Nordmark 
128bd670b35SErik Nordmark 	if (error != 0) {
129bd670b35SErik Nordmark 		dprint(3, ("sctp_get_dest: no ire for %x:%x:%x:%x\n",
1306be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		    SCTP_PRINTADDR(fp->sf_faddr)));
13145916cd2Sjpk 		/*
13245916cd2Sjpk 		 * It is tempting to just leave the src addr
13345916cd2Sjpk 		 * unspecified and let IP figure it out, but we
13445916cd2Sjpk 		 * *cannot* do this, since IP may choose a src addr
13545916cd2Sjpk 		 * that is not part of this association... unless
136bd670b35SErik Nordmark 		 * this sctp has bound to all addrs.  So if the dest
13745916cd2Sjpk 		 * lookup fails, try to find one in our src addr
13845916cd2Sjpk 		 * list, unless the sctp has bound to all addrs, in
13945916cd2Sjpk 		 * which case we change the src addr to unspec.
14045916cd2Sjpk 		 *
14145916cd2Sjpk 		 * Note that if this is a v6 endpoint but it does
14245916cd2Sjpk 		 * not have any v4 address at this point (e.g. may
14345916cd2Sjpk 		 * have been  deleted), sctp_get_valid_addr() will
14445916cd2Sjpk 		 * return mapped INADDR_ANY.  In this case, this
14545916cd2Sjpk 		 * address should be marked not reachable so that
14645916cd2Sjpk 		 * it won't be used to send data.
14745916cd2Sjpk 		 */
148769b977dSvi 		sctp_set_saddr(sctp, fp);
1496be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_state == SCTP_FADDRS_UNREACH)
15077c67f2fSkcpoon 			return;
15177c67f2fSkcpoon 		goto check_current;
15245916cd2Sjpk 	}
1536be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_ixa->ixa_ire != NULL);
1546be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(!(fp->sf_ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)));
155bd670b35SErik Nordmark 
156bd670b35SErik Nordmark 	if (!sctp->sctp_loopback)
157bd670b35SErik Nordmark 		sctp->sctp_loopback = uinfo.iulp_loopback;
15845916cd2Sjpk 
15977c67f2fSkcpoon 	/* Make sure the laddr is part of this association */
160bd670b35SErik Nordmark 	if ((sp = sctp_saddr_lookup(sctp, &laddr, 0)) != NULL &&
161bd670b35SErik Nordmark 	    !sp->saddr_ipif_dontsrc) {
16245916cd2Sjpk 		if (sp->saddr_ipif_unconfirmed == 1)
16345916cd2Sjpk 			sp->saddr_ipif_unconfirmed = 0;
164bd670b35SErik Nordmark 		/* We did IPsec policy lookup for laddr already */
1656be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_saddr = laddr;
16645916cd2Sjpk 	} else {
167bd670b35SErik Nordmark 		dprint(2, ("sctp_get_dest: src addr is not part of assoc "
168bd670b35SErik Nordmark 		    "%x:%x:%x:%x\n", SCTP_PRINTADDR(laddr)));
16977c67f2fSkcpoon 
17077c67f2fSkcpoon 		/*
17177c67f2fSkcpoon 		 * Set the src to the first saddr and hope for the best.
172bd670b35SErik Nordmark 		 * Note that this case should very seldomly
17377c67f2fSkcpoon 		 * happen.  One scenario this can happen is an app
17477c67f2fSkcpoon 		 * explicitly bind() to an address.  But that address is
17577c67f2fSkcpoon 		 * not the preferred source address to send to the peer.
17677c67f2fSkcpoon 		 */
177769b977dSvi 		sctp_set_saddr(sctp, fp);
1786be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_state == SCTP_FADDRS_UNREACH) {
17977c67f2fSkcpoon 			return;
18077c67f2fSkcpoon 		}
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Pull out RTO information for this faddr and use it if we don't
1857c478bd9Sstevel@tonic-gate 	 * have any yet.
1867c478bd9Sstevel@tonic-gate 	 */
1876be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_srtt == -1 && uinfo.iulp_rtt != 0) {
188d1d9f9adSkcpoon 		/* The cached value is in ms. */
1896be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_srtt = MSEC_TO_TICK(uinfo.iulp_rtt);
1906be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_rttvar = MSEC_TO_TICK(uinfo.iulp_rtt_sd);
1916be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_rto = 3 * fp->sf_srtt;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		/* Bound the RTO by configured min and max values */
1946be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_rto < sctp->sctp_rto_min) {
1956be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			fp->sf_rto = sctp->sctp_rto_min;
1967c478bd9Sstevel@tonic-gate 		}
1976be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_rto > sctp->sctp_rto_max) {
1986be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			fp->sf_rto = sctp->sctp_rto_max;
1997c478bd9Sstevel@tonic-gate 		}
2009f13099eSGeorge Shepherd 		SCTP_MAX_RTO(sctp, fp);
2017c478bd9Sstevel@tonic-gate 	}
202bd670b35SErik Nordmark 	pmtu = uinfo.iulp_mtu;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Record the MTU for this faddr. If the MTU for this faddr has
2067c478bd9Sstevel@tonic-gate 	 * changed, check if the assc MTU will also change.
2077c478bd9Sstevel@tonic-gate 	 */
2086be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_isv4) {
2097c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
2107c478bd9Sstevel@tonic-gate 	} else {
2117c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
2127c478bd9Sstevel@tonic-gate 	}
2136be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if ((fp->sf_pmss + hdrlen) != pmtu) {
2146be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		/* Make sure that sf_pmss is a multiple of SCTP_ALIGN. */
2156be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_pmss = (pmtu - hdrlen) & ~(SCTP_ALIGN - 1);
2166be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_cwnd < (fp->sf_pmss * 2)) {
2176be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			SET_CWND(fp, fp->sf_pmss,
21812f47623Skcpoon 			    sctps->sctps_slow_start_initial);
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
22277c67f2fSkcpoon check_current:
22377c67f2fSkcpoon 	if (fp == sctp->sctp_current)
22477c67f2fSkcpoon 		sctp_set_faddr_current(sctp, fp);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate void
sctp_update_dce(sctp_t * sctp)228bd670b35SErik Nordmark sctp_update_dce(sctp_t *sctp)
2297c478bd9Sstevel@tonic-gate {
23077c67f2fSkcpoon 	sctp_faddr_t	*fp;
231f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
232bd670b35SErik Nordmark 	iulp_t		uinfo;
233bd670b35SErik Nordmark 	ip_stack_t	*ipst = sctps->sctps_netstack->netstack_ip;
234bd670b35SErik Nordmark 	uint_t		ifindex;
2357c478bd9Sstevel@tonic-gate 
2366be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
237bd670b35SErik Nordmark 		bzero(&uinfo, sizeof (uinfo));
2387c478bd9Sstevel@tonic-gate 		/*
23977c67f2fSkcpoon 		 * Only record the PMTU for this faddr if we actually have
24077c67f2fSkcpoon 		 * done discovery. This prevents initialized default from
24177c67f2fSkcpoon 		 * clobbering any real info that IP may have.
2427c478bd9Sstevel@tonic-gate 		 */
2436be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_pmtu_discovered) {
2446be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (fp->sf_isv4) {
2456be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				uinfo.iulp_mtu = fp->sf_pmss +
24677c67f2fSkcpoon 				    sctp->sctp_hdr_len;
24777c67f2fSkcpoon 			} else {
2486be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				uinfo.iulp_mtu = fp->sf_pmss +
24977c67f2fSkcpoon 				    sctp->sctp_hdr6_len;
25077c67f2fSkcpoon 			}
2517c478bd9Sstevel@tonic-gate 		}
252f4b3ec61Sdh 		if (sctps->sctps_rtt_updates != 0 &&
2536be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		    fp->sf_rtt_updates >= sctps->sctps_rtt_updates) {
25477c67f2fSkcpoon 			/*
255bd670b35SErik Nordmark 			 * dce_update_uinfo() merges these values with the
256bd670b35SErik Nordmark 			 * old values.
25777c67f2fSkcpoon 			 */
2586be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			uinfo.iulp_rtt = TICK_TO_MSEC(fp->sf_srtt);
2596be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			uinfo.iulp_rtt_sd = TICK_TO_MSEC(fp->sf_rttvar);
2606be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			fp->sf_rtt_updates = 0;
261bd670b35SErik Nordmark 		}
262bd670b35SErik Nordmark 		ifindex = 0;
2636be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (IN6_IS_ADDR_LINKSCOPE(&fp->sf_faddr)) {
264bd670b35SErik Nordmark 			/*
265bd670b35SErik Nordmark 			 * If we are going to create a DCE we'd better have
266bd670b35SErik Nordmark 			 * an ifindex
267bd670b35SErik Nordmark 			 */
2686be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (fp->sf_ixa->ixa_nce != NULL) {
2696be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				ifindex = fp->sf_ixa->ixa_nce->nce_common->
270bd670b35SErik Nordmark 				    ncec_ill->ill_phyint->phyint_ifindex;
27177c67f2fSkcpoon 			} else {
272bd670b35SErik Nordmark 				continue;
27377c67f2fSkcpoon 			}
2747c478bd9Sstevel@tonic-gate 		}
275bd670b35SErik Nordmark 
2766be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		(void) dce_update_uinfo(&fp->sf_faddr, ifindex, &uinfo, ipst);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
281bd670b35SErik Nordmark  * The sender must later set the total length in the IP header.
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate mblk_t *
sctp_make_mp(sctp_t * sctp,sctp_faddr_t * fp,int trailer)284bd670b35SErik Nordmark sctp_make_mp(sctp_t *sctp, sctp_faddr_t *fp, int trailer)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	mblk_t *mp;
2877c478bd9Sstevel@tonic-gate 	size_t ipsctplen;
2887c478bd9Sstevel@tonic-gate 	int isv4;
289f4b3ec61Sdh 	sctp_stack_t *sctps = sctp->sctp_sctps;
290c31292eeSkcpoon 	boolean_t src_changed = B_FALSE;
2917c478bd9Sstevel@tonic-gate 
292bd670b35SErik Nordmark 	ASSERT(fp != NULL);
2936be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	isv4 = fp->sf_isv4;
2947c478bd9Sstevel@tonic-gate 
2956be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (SCTP_IS_ADDR_UNSPEC(isv4, fp->sf_saddr) ||
2966be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	    (fp->sf_ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
297bd670b35SErik Nordmark 		/* Need to pick a source */
298bd670b35SErik Nordmark 		sctp_get_dest(sctp, fp);
299c31292eeSkcpoon 		/*
300c31292eeSkcpoon 		 * Although we still may not get an IRE, the source address
301c31292eeSkcpoon 		 * may be changed in sctp_get_ire().  Set src_changed to
302c31292eeSkcpoon 		 * true so that the source address is copied again.
303c31292eeSkcpoon 		 */
304c31292eeSkcpoon 		src_changed = B_TRUE;
305c31292eeSkcpoon 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/* There is no suitable source address to use, return. */
3086be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_state == SCTP_FADDRS_UNREACH)
3097c478bd9Sstevel@tonic-gate 		return (NULL);
310bd670b35SErik Nordmark 
3116be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_ixa->ixa_ire != NULL);
3126be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(!SCTP_IS_ADDR_UNSPEC(isv4, fp->sf_saddr));
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (isv4) {
3157c478bd9Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr_len;
3167c478bd9Sstevel@tonic-gate 	} else {
3177c478bd9Sstevel@tonic-gate 		ipsctplen = sctp->sctp_hdr6_len;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
320bd670b35SErik Nordmark 	mp = allocb(ipsctplen + sctps->sctps_wroff_xtra + trailer, BPRI_MED);
3217c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
32245916cd2Sjpk 		ip1dbg(("sctp_make_mp: error making mp..\n"));
3237c478bd9Sstevel@tonic-gate 		return (NULL);
3247c478bd9Sstevel@tonic-gate 	}
325f4b3ec61Sdh 	mp->b_rptr += sctps->sctps_wroff_xtra;
3267c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + ipsctplen;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_wptr));
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (isv4) {
3317c478bd9Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)mp->b_rptr;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
334c31292eeSkcpoon 		if (fp != sctp->sctp_current || src_changed) {
335c31292eeSkcpoon 			/* Fix the source and destination addresses. */
3366be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			IN6_V4MAPPED_TO_IPADDR(&fp->sf_faddr, iph->ipha_dst);
3376be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			IN6_V4MAPPED_TO_IPADDR(&fp->sf_saddr, iph->ipha_src);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		/* set or clear the don't fragment bit */
3406be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_df) {
3417c478bd9Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
3427c478bd9Sstevel@tonic-gate 		} else {
3437c478bd9Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 	} else {
3467c478bd9Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
347c31292eeSkcpoon 		if (fp != sctp->sctp_current || src_changed) {
348c31292eeSkcpoon 			/* Fix the source and destination addresses. */
3496be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->sf_faddr;
3506be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			((ip6_t *)(mp->b_rptr))->ip6_src = fp->sf_saddr;
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_connp != NULL);
3547c478bd9Sstevel@tonic-gate 	return (mp);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * Notify upper layers about preferred write offset, write size.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate void
sctp_set_ulp_prop(sctp_t * sctp)3617c478bd9Sstevel@tonic-gate sctp_set_ulp_prop(sctp_t *sctp)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	int hdrlen;
3640f1702c5SYu Xiangning 	struct sock_proto_props sopp;
3650f1702c5SYu Xiangning 
366f4b3ec61Sdh 	sctp_stack_t *sctps = sctp->sctp_sctps;
3677c478bd9Sstevel@tonic-gate 
3686be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (sctp->sctp_current->sf_isv4) {
3697c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
3707c478bd9Sstevel@tonic-gate 	} else {
3717c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_ulpd);
3747c478bd9Sstevel@tonic-gate 
375bd670b35SErik Nordmark 	sctp->sctp_connp->conn_wroff = sctps->sctps_wroff_xtra + hdrlen +
376bd670b35SErik Nordmark 	    sizeof (sctp_data_hdr_t);
377bd670b35SErik Nordmark 
3786be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(sctp->sctp_current->sf_pmss == sctp->sctp_mss);
3790f1702c5SYu Xiangning 	bzero(&sopp, sizeof (sopp));
3800f1702c5SYu Xiangning 	sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF;
381bd670b35SErik Nordmark 	sopp.sopp_wroff = sctp->sctp_connp->conn_wroff;
3820f1702c5SYu Xiangning 	sopp.sopp_maxblk = sctp->sctp_mss - sizeof (sctp_data_hdr_t);
3830f1702c5SYu Xiangning 	sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
386bd670b35SErik Nordmark /*
387bd670b35SErik Nordmark  * Set the lengths in the packet and the transmit attributes.
388bd670b35SErik Nordmark  */
3897c478bd9Sstevel@tonic-gate void
sctp_set_iplen(sctp_t * sctp,mblk_t * mp,ip_xmit_attr_t * ixa)390bd670b35SErik Nordmark sctp_set_iplen(sctp_t *sctp, mblk_t *mp, ip_xmit_attr_t *ixa)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	uint16_t	sum = 0;
3937c478bd9Sstevel@tonic-gate 	ipha_t		*iph;
3947c478bd9Sstevel@tonic-gate 	ip6_t		*ip6h;
3957c478bd9Sstevel@tonic-gate 	mblk_t		*pmp = mp;
3967c478bd9Sstevel@tonic-gate 	boolean_t	isv4;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
3997c478bd9Sstevel@tonic-gate 	for (; pmp; pmp = pmp->b_cont)
4007c478bd9Sstevel@tonic-gate 		sum += pmp->b_wptr - pmp->b_rptr;
4017c478bd9Sstevel@tonic-gate 
402bd670b35SErik Nordmark 	ixa->ixa_pktlen = sum;
4037c478bd9Sstevel@tonic-gate 	if (isv4) {
4047c478bd9Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
4057c478bd9Sstevel@tonic-gate 		iph->ipha_length = htons(sum);
406bd670b35SErik Nordmark 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr_len;
4077c478bd9Sstevel@tonic-gate 	} else {
4087c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
409bd670b35SErik Nordmark 		ip6h->ip6_plen = htons(sum - IPV6_HDR_LEN);
410bd670b35SErik Nordmark 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr6_len;
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate int
sctp_compare_faddrsets(sctp_faddr_t * a1,sctp_faddr_t * a2)4157c478bd9Sstevel@tonic-gate sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	int na1 = 0;
4187c478bd9Sstevel@tonic-gate 	int overlap = 0;
4197c478bd9Sstevel@tonic-gate 	int equal = 1;
4207c478bd9Sstevel@tonic-gate 	int onematch;
4217c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp1, *fp2;
4227c478bd9Sstevel@tonic-gate 
4236be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp1 = a1; fp1; fp1 = fp1->sf_next) {
4247c478bd9Sstevel@tonic-gate 		onematch = 0;
4256be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		for (fp2 = a2; fp2; fp2 = fp2->sf_next) {
4266be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (IN6_ARE_ADDR_EQUAL(&fp1->sf_faddr,
4276be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			    &fp2->sf_faddr)) {
4287c478bd9Sstevel@tonic-gate 				overlap++;
4297c478bd9Sstevel@tonic-gate 				onematch = 1;
4307c478bd9Sstevel@tonic-gate 				break;
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			if (!onematch) {
4337c478bd9Sstevel@tonic-gate 				equal = 0;
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		na1++;
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (equal) {
4407c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_EQUAL);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	if (overlap == na1) {
4437c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_SUBSET);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	if (overlap) {
4467c478bd9Sstevel@tonic-gate 		return (SCTP_ADDR_OVERLAP);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	return (SCTP_ADDR_DISJOINT);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4525f9878b0Sken Powell - Sun Microsystem  * Returns 0 on success, ENOMEM on memory allocation failure, EHOSTUNREACH
4535f9878b0Sken Powell - Sun Microsystem  * if the connection credentials fail remote host accreditation or
4545f9878b0Sken Powell - Sun Microsystem  * if the new destination does not support the previously established
4555f9878b0Sken Powell - Sun Microsystem  * connection security label. If sleep is true, this function should
4565f9878b0Sken Powell - Sun Microsystem  * never fail for a memory allocation failure. The boolean parameter
4575f9878b0Sken Powell - Sun Microsystem  * "first" decides whether the newly created faddr structure should be
45877c67f2fSkcpoon  * added at the beginning of the list or at the end.
45977c67f2fSkcpoon  *
46077c67f2fSkcpoon  * Note: caller must hold conn fanout lock.
4617c478bd9Sstevel@tonic-gate  */
46277c67f2fSkcpoon int
sctp_add_faddr(sctp_t * sctp,in6_addr_t * addr,int sleep,boolean_t first)46377c67f2fSkcpoon sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first)
4647c478bd9Sstevel@tonic-gate {
46577c67f2fSkcpoon 	sctp_faddr_t	*faddr;
46677c67f2fSkcpoon 	mblk_t		*timer_mp;
4675f9878b0Sken Powell - Sun Microsystem 	int		err;
468bd670b35SErik Nordmark 	conn_t		*connp = sctp->sctp_connp;
4697c478bd9Sstevel@tonic-gate 
47045916cd2Sjpk 	if (is_system_labeled()) {
471bd670b35SErik Nordmark 		ip_xmit_attr_t	*ixa = connp->conn_ixa;
472bd670b35SErik Nordmark 		ts_label_t	*effective_tsl = NULL;
473bd670b35SErik Nordmark 
474bd670b35SErik Nordmark 		ASSERT(ixa->ixa_tsl != NULL);
47545916cd2Sjpk 
4765f9878b0Sken Powell - Sun Microsystem 		/*
4775f9878b0Sken Powell - Sun Microsystem 		 * Verify the destination is allowed to receive packets
4785f9878b0Sken Powell - Sun Microsystem 		 * at the security label of the connection we are initiating.
4795f9878b0Sken Powell - Sun Microsystem 		 *
480bd670b35SErik Nordmark 		 * tsol_check_dest() will create a new effective label for
4815f9878b0Sken Powell - Sun Microsystem 		 * this connection with a modified label or label flags only
482bd670b35SErik Nordmark 		 * if there are changes from the original label.
4835f9878b0Sken Powell - Sun Microsystem 		 *
4845f9878b0Sken Powell - Sun Microsystem 		 * Accept whatever label we get if this is the first
4855f9878b0Sken Powell - Sun Microsystem 		 * destination address for this connection. The security
4865f9878b0Sken Powell - Sun Microsystem 		 * label and label flags must match any previuous settings
4875f9878b0Sken Powell - Sun Microsystem 		 * for all subsequent destination addresses.
4885f9878b0Sken Powell - Sun Microsystem 		 */
4895f9878b0Sken Powell - Sun Microsystem 		if (IN6_IS_ADDR_V4MAPPED(addr)) {
4905f9878b0Sken Powell - Sun Microsystem 			uint32_t dst;
4915f9878b0Sken Powell - Sun Microsystem 			IN6_V4MAPPED_TO_IPADDR(addr, dst);
492bd670b35SErik Nordmark 			err = tsol_check_dest(ixa->ixa_tsl,
493bd670b35SErik Nordmark 			    &dst, IPV4_VERSION, connp->conn_mac_mode,
494bd670b35SErik Nordmark 			    connp->conn_zone_is_global, &effective_tsl);
4955f9878b0Sken Powell - Sun Microsystem 		} else {
496bd670b35SErik Nordmark 			err = tsol_check_dest(ixa->ixa_tsl,
497bd670b35SErik Nordmark 			    addr, IPV6_VERSION, connp->conn_mac_mode,
498bd670b35SErik Nordmark 			    connp->conn_zone_is_global, &effective_tsl);
4995f9878b0Sken Powell - Sun Microsystem 		}
5005f9878b0Sken Powell - Sun Microsystem 		if (err != 0)
5015f9878b0Sken Powell - Sun Microsystem 			return (err);
502bd670b35SErik Nordmark 
503bd670b35SErik Nordmark 		if (sctp->sctp_faddrs == NULL && effective_tsl != NULL) {
504bd670b35SErik Nordmark 			ip_xmit_attr_replace_tsl(ixa, effective_tsl);
505bd670b35SErik Nordmark 		} else if (effective_tsl != NULL) {
506bd670b35SErik Nordmark 			label_rele(effective_tsl);
5075f9878b0Sken Powell - Sun Microsystem 			return (EHOSTUNREACH);
50845916cd2Sjpk 		}
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
51145916cd2Sjpk 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL)
51245916cd2Sjpk 		return (ENOMEM);
513bd670b35SErik Nordmark 	bzero(faddr, sizeof (*faddr));
514121e5416Skcpoon 	timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep);
51577c67f2fSkcpoon 	if (timer_mp == NULL) {
51677c67f2fSkcpoon 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
51777c67f2fSkcpoon 		return (ENOMEM);
51877c67f2fSkcpoon 	}
51977c67f2fSkcpoon 	((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr;
52045916cd2Sjpk 
521bd670b35SErik Nordmark 	/* Start with any options set on the conn */
5226be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	faddr->sf_ixa = conn_get_ixa_exclusive(connp);
5236be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (faddr->sf_ixa == NULL) {
524bd670b35SErik Nordmark 		freemsg(timer_mp);
525c31292eeSkcpoon 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
526bd670b35SErik Nordmark 		return (ENOMEM);
527c31292eeSkcpoon 	}
5286be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	faddr->sf_ixa->ixa_notify_cookie = connp->conn_sctp;
529bd670b35SErik Nordmark 
530bd670b35SErik Nordmark 	sctp_init_faddr(sctp, faddr, addr, timer_mp);
5316be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(faddr->sf_ixa->ixa_cred != NULL);
532bd670b35SErik Nordmark 
533bd670b35SErik Nordmark 	/* ip_attr_connect didn't allow broadcats/multicast dest */
5346be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(faddr->sf_next == NULL);
5357c478bd9Sstevel@tonic-gate 
53645916cd2Sjpk 	if (sctp->sctp_faddrs == NULL) {
53745916cd2Sjpk 		ASSERT(sctp->sctp_lastfaddr == NULL);
53845916cd2Sjpk 		/* only element on list; first and last are same */
53945916cd2Sjpk 		sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr;
54045916cd2Sjpk 	} else if (first) {
54145916cd2Sjpk 		ASSERT(sctp->sctp_lastfaddr != NULL);
5426be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		faddr->sf_next = sctp->sctp_faddrs;
5437c478bd9Sstevel@tonic-gate 		sctp->sctp_faddrs = faddr;
54445916cd2Sjpk 	} else {
5456be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp->sctp_lastfaddr->sf_next = faddr;
54645916cd2Sjpk 		sctp->sctp_lastfaddr = faddr;
5477c478bd9Sstevel@tonic-gate 	}
5481d8c4025Svi 	sctp->sctp_nfaddrs++;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	return (0);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate sctp_faddr_t *
sctp_lookup_faddr(sctp_t * sctp,in6_addr_t * addr)5547c478bd9Sstevel@tonic-gate sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
5577c478bd9Sstevel@tonic-gate 
5586be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
5596be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (IN6_ARE_ADDR_EQUAL(&fp->sf_faddr, addr))
5607c478bd9Sstevel@tonic-gate 			break;
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return (fp);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate sctp_faddr_t *
sctp_lookup_faddr_nosctp(sctp_faddr_t * fp,in6_addr_t * addr)5677c478bd9Sstevel@tonic-gate sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
5687c478bd9Sstevel@tonic-gate {
5696be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (; fp; fp = fp->sf_next) {
5706be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (IN6_ARE_ADDR_EQUAL(&fp->sf_faddr, addr)) {
5717c478bd9Sstevel@tonic-gate 			break;
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	return (fp);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
57877c67f2fSkcpoon /*
57977c67f2fSkcpoon  * To change the currently used peer address to the specified one.
58077c67f2fSkcpoon  */
5817c478bd9Sstevel@tonic-gate void
sctp_set_faddr_current(sctp_t * sctp,sctp_faddr_t * fp)58277c67f2fSkcpoon sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp)
5837c478bd9Sstevel@tonic-gate {
58477c67f2fSkcpoon 	/* Now setup the composite header. */
5856be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_isv4) {
5866be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		IN6_V4MAPPED_TO_IPADDR(&fp->sf_faddr,
5877c478bd9Sstevel@tonic-gate 		    sctp->sctp_ipha->ipha_dst);
5886be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		IN6_V4MAPPED_TO_IPADDR(&fp->sf_saddr,
5896be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		    sctp->sctp_ipha->ipha_src);
5907c478bd9Sstevel@tonic-gate 		/* update don't fragment bit */
5916be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_df) {
5927c478bd9Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
5937c478bd9Sstevel@tonic-gate 			    htons(IPH_DF);
5947c478bd9Sstevel@tonic-gate 		} else {
5957c478bd9Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 	} else {
5986be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp->sctp_ip6h->ip6_dst = fp->sf_faddr;
5996be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp->sctp_ip6h->ip6_src = fp->sf_saddr;
6007c478bd9Sstevel@tonic-gate 	}
60177c67f2fSkcpoon 
60277c67f2fSkcpoon 	sctp->sctp_current = fp;
6036be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	sctp->sctp_mss = fp->sf_pmss;
60477c67f2fSkcpoon 
60577c67f2fSkcpoon 	/* Update the uppper layer for the change. */
60677c67f2fSkcpoon 	if (!SCTP_IS_DETACHED(sctp))
60777c67f2fSkcpoon 		sctp_set_ulp_prop(sctp);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate void
sctp_redo_faddr_srcs(sctp_t * sctp)6117c478bd9Sstevel@tonic-gate sctp_redo_faddr_srcs(sctp_t *sctp)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
6147c478bd9Sstevel@tonic-gate 
6156be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
616bd670b35SErik Nordmark 		sctp_get_dest(sctp, fp);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate void
sctp_faddr_alive(sctp_t * sctp,sctp_faddr_t * fp)6217c478bd9Sstevel@tonic-gate sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
6227c478bd9Sstevel@tonic-gate {
6235dd46ab5SKacheong Poon 	int64_t now = LBOLT_FASTPATH64;
6247c478bd9Sstevel@tonic-gate 
6255dd46ab5SKacheong Poon 	/*
6265dd46ab5SKacheong Poon 	 * If we are under memory pressure, we abort association waiting
6275dd46ab5SKacheong Poon 	 * in zero window probing state for too long.  We do this by not
6285dd46ab5SKacheong Poon 	 * resetting sctp_strikes.  So if sctp_zero_win_probe continues
6295dd46ab5SKacheong Poon 	 * while under memory pressure, this association will eventually
6305dd46ab5SKacheong Poon 	 * time out.
6315dd46ab5SKacheong Poon 	 */
6325dd46ab5SKacheong Poon 	if (!sctp->sctp_zero_win_probe || !sctp->sctp_sctps->sctps_reclaim) {
6335dd46ab5SKacheong Poon 		sctp->sctp_strikes = 0;
6345dd46ab5SKacheong Poon 	}
6356be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_strikes = 0;
6366be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_lastactive = now;
6376be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_hb_expiry = now + SET_HB_INTVL(fp);
6386be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_hb_pending = B_FALSE;
6396be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_state != SCTP_FADDRS_ALIVE) {
6406be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_state = SCTP_FADDRS_ALIVE;
6416be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp_intf_event(sctp, fp->sf_faddr, SCTP_ADDR_AVAILABLE, 0);
642c31292eeSkcpoon 		/* Should have a full IRE now */
643bd670b35SErik Nordmark 		sctp_get_dest(sctp, fp);
6447c478bd9Sstevel@tonic-gate 
64577c67f2fSkcpoon 		/*
64677c67f2fSkcpoon 		 * If this is the primary, switch back to it now.  And
64777c67f2fSkcpoon 		 * we probably want to reset the source addr used to reach
64877c67f2fSkcpoon 		 * it.
649bd670b35SErik Nordmark 		 * Note that if we didn't find a source in sctp_get_dest
650bd670b35SErik Nordmark 		 * then we'd be unreachable at this point in time.
65177c67f2fSkcpoon 		 */
652bd670b35SErik Nordmark 		if (fp == sctp->sctp_primary &&
6536be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		    fp->sf_state != SCTP_FADDRS_UNREACH) {
65477c67f2fSkcpoon 			sctp_set_faddr_current(sctp, fp);
65577c67f2fSkcpoon 			return;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6605dd46ab5SKacheong Poon /*
6615dd46ab5SKacheong Poon  * Return B_TRUE if there is still an active peer address with zero strikes;
6625dd46ab5SKacheong Poon  * otherwise rturn B_FALSE.
6635dd46ab5SKacheong Poon  */
6645dd46ab5SKacheong Poon boolean_t
sctp_is_a_faddr_clean(sctp_t * sctp)6657c478bd9Sstevel@tonic-gate sctp_is_a_faddr_clean(sctp_t *sctp)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
6687c478bd9Sstevel@tonic-gate 
6696be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = sctp->sctp_faddrs; fp; fp = fp->sf_next) {
6706be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_state == SCTP_FADDRS_ALIVE && fp->sf_strikes == 0) {
6715dd46ab5SKacheong Poon 			return (B_TRUE);
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6755dd46ab5SKacheong Poon 	return (B_FALSE);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate /*
6797c478bd9Sstevel@tonic-gate  * Returns 0 if there is at leave one other active faddr, -1 if there
6807c478bd9Sstevel@tonic-gate  * are none. If there are none left, faddr_dead() will start killing the
6817c478bd9Sstevel@tonic-gate  * association.
6827c478bd9Sstevel@tonic-gate  * If the downed faddr was the current faddr, a new current faddr
6837c478bd9Sstevel@tonic-gate  * will be chosen.
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate int
sctp_faddr_dead(sctp_t * sctp,sctp_faddr_t * fp,int newstate)6867c478bd9Sstevel@tonic-gate sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	sctp_faddr_t *ofp;
689f4b3ec61Sdh 	sctp_stack_t *sctps = sctp->sctp_sctps;
6907c478bd9Sstevel@tonic-gate 
6916be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_state == SCTP_FADDRS_ALIVE) {
6926be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp_intf_event(sctp, fp->sf_faddr, SCTP_ADDR_UNREACHABLE, 0);
6937c478bd9Sstevel@tonic-gate 	}
6946be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_state = newstate;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
6976be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	    SCTP_PRINTADDR(fp->sf_faddr), newstate));
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (fp == sctp->sctp_current) {
7007c478bd9Sstevel@tonic-gate 		/* Current faddr down; need to switch it */
7017c478bd9Sstevel@tonic-gate 		sctp->sctp_current = NULL;
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/* Find next alive faddr */
7057c478bd9Sstevel@tonic-gate 	ofp = fp;
7066be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = fp->sf_next; fp != NULL; fp = fp->sf_next) {
7076be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_state == SCTP_FADDRS_ALIVE) {
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
7137c478bd9Sstevel@tonic-gate 		/* Continue from beginning of list */
7146be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->sf_next) {
7156be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (fp->sf_state == SCTP_FADDRS_ALIVE) {
7167c478bd9Sstevel@tonic-gate 				break;
7177c478bd9Sstevel@tonic-gate 			}
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
72177c67f2fSkcpoon 	/*
72277c67f2fSkcpoon 	 * Find a new fp, so if the current faddr is dead, use the new fp
72377c67f2fSkcpoon 	 * as the current one.
72477c67f2fSkcpoon 	 */
7257c478bd9Sstevel@tonic-gate 	if (fp != ofp) {
7267c478bd9Sstevel@tonic-gate 		if (sctp->sctp_current == NULL) {
7277c478bd9Sstevel@tonic-gate 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
7286be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			    SCTP_PRINTADDR(fp->sf_faddr)));
72977c67f2fSkcpoon 			/*
73077c67f2fSkcpoon 			 * Note that we don't need to reset the source addr
73177c67f2fSkcpoon 			 * of the new fp.
73277c67f2fSkcpoon 			 */
73377c67f2fSkcpoon 			sctp_set_faddr_current(sctp, fp);
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 		return (0);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	/* All faddrs are down; kill the association */
7407c478bd9Sstevel@tonic-gate 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
7415dd46ab5SKacheong Poon 	SCTPS_BUMP_MIB(sctps, sctpAborted);
7427c478bd9Sstevel@tonic-gate 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
7437c478bd9Sstevel@tonic-gate 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
7447c478bd9Sstevel@tonic-gate 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
7457c478bd9Sstevel@tonic-gate 	    sctp->sctp_client_errno : ETIMEDOUT);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	return (-1);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate sctp_faddr_t *
sctp_rotate_faddr(sctp_t * sctp,sctp_faddr_t * ofp)7517c478bd9Sstevel@tonic-gate sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	sctp_faddr_t *nfp = NULL;
75437d5888bSGeorge Shepherd 	sctp_faddr_t *saved_fp = NULL;
75537d5888bSGeorge Shepherd 	int min_strikes;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (ofp == NULL) {
7587c478bd9Sstevel@tonic-gate 		ofp = sctp->sctp_current;
7597c478bd9Sstevel@tonic-gate 	}
76037d5888bSGeorge Shepherd 	/* Nothing to do */
76137d5888bSGeorge Shepherd 	if (sctp->sctp_nfaddrs < 2)
76237d5888bSGeorge Shepherd 		return (ofp);
7637c478bd9Sstevel@tonic-gate 
76437d5888bSGeorge Shepherd 	/*
76537d5888bSGeorge Shepherd 	 * Find the next live peer address with zero strikes. In case
76637d5888bSGeorge Shepherd 	 * there is none, find the one with the lowest number of strikes.
76737d5888bSGeorge Shepherd 	 */
7686be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	min_strikes = ofp->sf_strikes;
7696be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	nfp = ofp->sf_next;
7706dee1faeSGeorge Shepherd 	while (nfp != ofp) {
77137d5888bSGeorge Shepherd 		/* If reached end of list, continue scan from the head */
77237d5888bSGeorge Shepherd 		if (nfp == NULL) {
77337d5888bSGeorge Shepherd 			nfp = sctp->sctp_faddrs;
77437d5888bSGeorge Shepherd 			continue;
77537d5888bSGeorge Shepherd 		}
7766be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (nfp->sf_state == SCTP_FADDRS_ALIVE) {
7776be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (nfp->sf_strikes == 0)
7787c478bd9Sstevel@tonic-gate 				break;
7796be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (nfp->sf_strikes < min_strikes) {
7806be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				min_strikes = nfp->sf_strikes;
78137d5888bSGeorge Shepherd 				saved_fp = nfp;
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 		}
7846be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		nfp = nfp->sf_next;
7857c478bd9Sstevel@tonic-gate 	}
78637d5888bSGeorge Shepherd 	/* If reached the old address, there is no zero strike path */
78737d5888bSGeorge Shepherd 	if (nfp == ofp)
78837d5888bSGeorge Shepherd 		nfp = NULL;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/*
79137d5888bSGeorge Shepherd 	 * If there is a peer address with zero strikes  we use that, if not
79237d5888bSGeorge Shepherd 	 * return a peer address with fewer strikes than the one last used,
79337d5888bSGeorge Shepherd 	 * if neither exist we may as well stay with the old one.
7947c478bd9Sstevel@tonic-gate 	 */
79537d5888bSGeorge Shepherd 	if (nfp != NULL)
79637d5888bSGeorge Shepherd 		return (nfp);
79737d5888bSGeorge Shepherd 	if (saved_fp != NULL)
79837d5888bSGeorge Shepherd 		return (saved_fp);
79937d5888bSGeorge Shepherd 	return (ofp);
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate void
sctp_unlink_faddr(sctp_t * sctp,sctp_faddr_t * fp)8037c478bd9Sstevel@tonic-gate sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fpp;
8067c478bd9Sstevel@tonic-gate 
807*ab82c29bSToomas Soome 	fpp = NULL;
808*ab82c29bSToomas Soome 
8097c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_faddrs) {
8107c478bd9Sstevel@tonic-gate 		return;
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8136be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_timer_mp != NULL) {
8146be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp_timer_free(fp->sf_timer_mp);
8156be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_timer_mp = NULL;
8166be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_timer_running = 0;
8177c478bd9Sstevel@tonic-gate 	}
8186be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_rc_timer_mp != NULL) {
8196be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp_timer_free(fp->sf_rc_timer_mp);
8206be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_rc_timer_mp = NULL;
8216be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_rc_timer_running = 0;
8227c478bd9Sstevel@tonic-gate 	}
8236be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (fp->sf_ixa != NULL) {
8246be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		ixa_refrele(fp->sf_ixa);
8256be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_ixa = NULL;
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8297c478bd9Sstevel@tonic-gate 		goto gotit;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8326be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fpp = sctp->sctp_faddrs; fpp->sf_next != fp; fpp = fpp->sf_next)
8337c478bd9Sstevel@tonic-gate 		;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate gotit:
8367c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
8377c478bd9Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8387c478bd9Sstevel@tonic-gate 	if (fp == sctp->sctp_faddrs) {
8396be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sctp->sctp_faddrs = fp->sf_next;
8407c478bd9Sstevel@tonic-gate 	} else {
8416be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fpp->sf_next = fp->sf_next;
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8447c478bd9Sstevel@tonic-gate 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
8451d8c4025Svi 	sctp->sctp_nfaddrs--;
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate void
sctp_zap_faddrs(sctp_t * sctp,int caller_holds_lock)8497c478bd9Sstevel@tonic-gate sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp, *fpn;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL) {
8547c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_lastfaddr == NULL);
8557c478bd9Sstevel@tonic-gate 		return;
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastfaddr != NULL);
8597c478bd9Sstevel@tonic-gate 	sctp->sctp_lastfaddr = NULL;
8607c478bd9Sstevel@tonic-gate 	sctp->sctp_current = NULL;
8617c478bd9Sstevel@tonic-gate 	sctp->sctp_primary = NULL;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	sctp_free_faddr_timers(sctp);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8667c478bd9Sstevel@tonic-gate 		/* in conn fanout; need to hold lock */
8677c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
8716be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fpn = fp->sf_next;
8726be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (fp->sf_ixa != NULL) {
8736be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			ixa_refrele(fp->sf_ixa);
8746be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			fp->sf_ixa = NULL;
875bd670b35SErik Nordmark 		}
8767c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
8771d8c4025Svi 		sctp->sctp_nfaddrs--;
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	sctp->sctp_faddrs = NULL;
8811d8c4025Svi 	ASSERT(sctp->sctp_nfaddrs == 0);
8827c478bd9Sstevel@tonic-gate 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
8837c478bd9Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate void
sctp_zap_addrs(sctp_t * sctp)8897c478bd9Sstevel@tonic-gate sctp_zap_addrs(sctp_t *sctp)
8907c478bd9Sstevel@tonic-gate {
8917c478bd9Sstevel@tonic-gate 	sctp_zap_faddrs(sctp, 0);
8927c478bd9Sstevel@tonic-gate 	sctp_free_saddrs(sctp);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate /*
896bd670b35SErik Nordmark  * Build two SCTP header templates; one for IPv4 and one for IPv6.
897bd670b35SErik Nordmark  * Store them in sctp_iphc and sctp_iphc6 respectively (and related fields).
898bd670b35SErik Nordmark  * There are no IP addresses in the templates, but the port numbers and
899bd670b35SErik Nordmark  * verifier are field in from the conn_t and sctp_t.
900bd670b35SErik Nordmark  *
901bd670b35SErik Nordmark  * Returns failure if can't allocate memory, or if there is a problem
902bd670b35SErik Nordmark  * with a routing header/option.
903bd670b35SErik Nordmark  *
904bd670b35SErik Nordmark  * We allocate space for the minimum sctp header (sctp_hdr_t).
905bd670b35SErik Nordmark  *
906bd670b35SErik Nordmark  * We massage an routing option/header. There is no checksum implication
907bd670b35SErik Nordmark  * for a routing header for sctp.
908bd670b35SErik Nordmark  *
909bd670b35SErik Nordmark  * Caller needs to update conn_wroff if desired.
910bd670b35SErik Nordmark  *
911bd670b35SErik Nordmark  * TSol notes: This assumes that a SCTP association has a single peer label
912bd670b35SErik Nordmark  * since we only track a single pair of ipp_label_v4/v6 and not a separate one
913bd670b35SErik Nordmark  * for each faddr.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate int
sctp_build_hdrs(sctp_t * sctp,int sleep)916bd670b35SErik Nordmark sctp_build_hdrs(sctp_t *sctp, int sleep)
9177c478bd9Sstevel@tonic-gate {
918bd670b35SErik Nordmark 	conn_t		*connp = sctp->sctp_connp;
919bd670b35SErik Nordmark 	ip_pkt_t	*ipp = &connp->conn_xmit_ipp;
920bd670b35SErik Nordmark 	uint_t		ip_hdr_length;
921bd670b35SErik Nordmark 	uchar_t		*hdrs;
922bd670b35SErik Nordmark 	uint_t		hdrs_len;
923bd670b35SErik Nordmark 	uint_t		ulp_hdr_length = sizeof (sctp_hdr_t);
924bd670b35SErik Nordmark 	ipha_t		*ipha;
925bd670b35SErik Nordmark 	ip6_t		*ip6h;
9267c478bd9Sstevel@tonic-gate 	sctp_hdr_t	*sctph;
927bd670b35SErik Nordmark 	in6_addr_t	v6src, v6dst;
928bd670b35SErik Nordmark 	ipaddr_t	v4src, v4dst;
9297c478bd9Sstevel@tonic-gate 
930bd670b35SErik Nordmark 	v4src = connp->conn_saddr_v4;
931bd670b35SErik Nordmark 	v4dst = connp->conn_faddr_v4;
932bd670b35SErik Nordmark 	v6src = connp->conn_saddr_v6;
933bd670b35SErik Nordmark 	v6dst = connp->conn_faddr_v6;
9347c478bd9Sstevel@tonic-gate 
935bd670b35SErik Nordmark 	/* First do IPv4 header */
936bd670b35SErik Nordmark 	ip_hdr_length = ip_total_hdrs_len_v4(ipp);
9377c478bd9Sstevel@tonic-gate 
938bd670b35SErik Nordmark 	/* In case of TX label and IP options it can be too much */
939bd670b35SErik Nordmark 	if (ip_hdr_length > IP_MAX_HDR_LENGTH) {
940bd670b35SErik Nordmark 		/* Preserves existing TX errno for this */
941bd670b35SErik Nordmark 		return (EHOSTUNREACH);
942bd670b35SErik Nordmark 	}
943bd670b35SErik Nordmark 	hdrs_len = ip_hdr_length + ulp_hdr_length;
944bd670b35SErik Nordmark 	ASSERT(hdrs_len != 0);
9457c478bd9Sstevel@tonic-gate 
946bd670b35SErik Nordmark 	if (hdrs_len != sctp->sctp_iphc_len) {
947bd670b35SErik Nordmark 		/* Allocate new before we free any old */
948bd670b35SErik Nordmark 		hdrs = kmem_alloc(hdrs_len, sleep);
949bd670b35SErik Nordmark 		if (hdrs == NULL)
950bd670b35SErik Nordmark 			return (ENOMEM);
9517c478bd9Sstevel@tonic-gate 
952bd670b35SErik Nordmark 		if (sctp->sctp_iphc != NULL)
953bd670b35SErik Nordmark 			kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
954bd670b35SErik Nordmark 		sctp->sctp_iphc = hdrs;
955bd670b35SErik Nordmark 		sctp->sctp_iphc_len = hdrs_len;
956bd670b35SErik Nordmark 	} else {
957bd670b35SErik Nordmark 		hdrs = sctp->sctp_iphc;
958bd670b35SErik Nordmark 	}
959bd670b35SErik Nordmark 	sctp->sctp_hdr_len = sctp->sctp_iphc_len;
960bd670b35SErik Nordmark 	sctp->sctp_ip_hdr_len = ip_hdr_length;
9617c478bd9Sstevel@tonic-gate 
962bd670b35SErik Nordmark 	sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length);
9637c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph = sctph;
964bd670b35SErik Nordmark 	sctph->sh_sport = connp->conn_lport;
965bd670b35SErik Nordmark 	sctph->sh_dport = connp->conn_fport;
966bd670b35SErik Nordmark 	sctph->sh_verf = sctp->sctp_fvtag;
967bd670b35SErik Nordmark 	sctph->sh_chksum = 0;
968bd670b35SErik Nordmark 
969bd670b35SErik Nordmark 	ipha = (ipha_t *)hdrs;
970bd670b35SErik Nordmark 	sctp->sctp_ipha = ipha;
971bd670b35SErik Nordmark 
972bd670b35SErik Nordmark 	ipha->ipha_src = v4src;
973bd670b35SErik Nordmark 	ipha->ipha_dst = v4dst;
974bd670b35SErik Nordmark 	ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, connp->conn_proto);
975bd670b35SErik Nordmark 	ipha->ipha_length = htons(hdrs_len);
976bd670b35SErik Nordmark 	ipha->ipha_fragment_offset_and_flags = 0;
977bd670b35SErik Nordmark 
978bd670b35SErik Nordmark 	if (ipp->ipp_fields & IPPF_IPV4_OPTIONS)
979bd670b35SErik Nordmark 		(void) ip_massage_options(ipha, connp->conn_netstack);
980bd670b35SErik Nordmark 
981bd670b35SErik Nordmark 	/* Now IPv6 */
982bd670b35SErik Nordmark 	ip_hdr_length = ip_total_hdrs_len_v6(ipp);
983bd670b35SErik Nordmark 	hdrs_len = ip_hdr_length + ulp_hdr_length;
9847c478bd9Sstevel@tonic-gate 	ASSERT(hdrs_len != 0);
985bd670b35SErik Nordmark 
986bd670b35SErik Nordmark 	if (hdrs_len != sctp->sctp_iphc6_len) {
987bd670b35SErik Nordmark 		/* Allocate new before we free any old */
988bd670b35SErik Nordmark 		hdrs = kmem_alloc(hdrs_len, sleep);
9897c478bd9Sstevel@tonic-gate 		if (hdrs == NULL)
9907c478bd9Sstevel@tonic-gate 			return (ENOMEM);
9917c478bd9Sstevel@tonic-gate 
992bd670b35SErik Nordmark 		if (sctp->sctp_iphc6 != NULL)
9937c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
9947c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6 = hdrs;
9957c478bd9Sstevel@tonic-gate 		sctp->sctp_iphc6_len = hdrs_len;
9967c478bd9Sstevel@tonic-gate 	} else {
997bd670b35SErik Nordmark 		hdrs = sctp->sctp_iphc6;
9987c478bd9Sstevel@tonic-gate 	}
999bd670b35SErik Nordmark 	sctp->sctp_hdr6_len = sctp->sctp_iphc6_len;
1000bd670b35SErik Nordmark 	sctp->sctp_ip_hdr6_len = ip_hdr_length;
10017c478bd9Sstevel@tonic-gate 
1002bd670b35SErik Nordmark 	sctph = (sctp_hdr_t *)(hdrs + ip_hdr_length);
1003bd670b35SErik Nordmark 	sctp->sctp_sctph6 = sctph;
1004bd670b35SErik Nordmark 	sctph->sh_sport = connp->conn_lport;
1005bd670b35SErik Nordmark 	sctph->sh_dport = connp->conn_fport;
1006bd670b35SErik Nordmark 	sctph->sh_verf = sctp->sctp_fvtag;
1007bd670b35SErik Nordmark 	sctph->sh_chksum = 0;
1008bd670b35SErik Nordmark 
1009bd670b35SErik Nordmark 	ip6h = (ip6_t *)hdrs;
1010bd670b35SErik Nordmark 	sctp->sctp_ip6h = ip6h;
1011bd670b35SErik Nordmark 
1012bd670b35SErik Nordmark 	ip6h->ip6_src = v6src;
1013bd670b35SErik Nordmark 	ip6h->ip6_dst = v6dst;
1014bd670b35SErik Nordmark 	ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, connp->conn_proto,
1015bd670b35SErik Nordmark 	    connp->conn_flowinfo);
1016bd670b35SErik Nordmark 	ip6h->ip6_plen = htons(hdrs_len - IPV6_HDR_LEN);
1017bd670b35SErik Nordmark 
1018bd670b35SErik Nordmark 	if (ipp->ipp_fields & IPPF_RTHDR) {
1019bd670b35SErik Nordmark 		uint8_t		*end;
1020bd670b35SErik Nordmark 		ip6_rthdr_t	*rth;
1021bd670b35SErik Nordmark 
1022bd670b35SErik Nordmark 		end = (uint8_t *)ip6h + ip_hdr_length;
1023bd670b35SErik Nordmark 		rth = ip_find_rthdr_v6(ip6h, end);
1024f4b3ec61Sdh 		if (rth != NULL) {
1025bd670b35SErik Nordmark 			(void) ip_massage_options_v6(ip6h, rth,
1026bd670b35SErik Nordmark 			    connp->conn_netstack);
1027f4b3ec61Sdh 		}
10287c478bd9Sstevel@tonic-gate 
1029bd670b35SErik Nordmark 		/*
1030bd670b35SErik Nordmark 		 * Verify that the first hop isn't a mapped address.
1031bd670b35SErik Nordmark 		 * Routers along the path need to do this verification
1032bd670b35SErik Nordmark 		 * for subsequent hops.
1033bd670b35SErik Nordmark 		 */
1034bd670b35SErik Nordmark 		if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst))
1035bd670b35SErik Nordmark 			return (EADDRNOTAVAIL);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	return (0);
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate 
104045916cd2Sjpk static int
sctp_v4_label(sctp_t * sctp,sctp_faddr_t * fp)1041bd670b35SErik Nordmark sctp_v4_label(sctp_t *sctp, sctp_faddr_t *fp)
104245916cd2Sjpk {
1043bd670b35SErik Nordmark 	conn_t *connp = sctp->sctp_connp;
104445916cd2Sjpk 
10456be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_ixa->ixa_flags & IXAF_IS_IPV4);
10466be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	return (conn_update_label(connp, fp->sf_ixa, &fp->sf_faddr,
1047bd670b35SErik Nordmark 	    &connp->conn_xmit_ipp));
104845916cd2Sjpk }
104945916cd2Sjpk 
105045916cd2Sjpk static int
sctp_v6_label(sctp_t * sctp,sctp_faddr_t * fp)1051bd670b35SErik Nordmark sctp_v6_label(sctp_t *sctp, sctp_faddr_t *fp)
105245916cd2Sjpk {
1053bd670b35SErik Nordmark 	conn_t *connp = sctp->sctp_connp;
105445916cd2Sjpk 
10556be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(!(fp->sf_ixa->ixa_flags & IXAF_IS_IPV4));
10566be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	return (conn_update_label(connp, fp->sf_ixa, &fp->sf_faddr,
1057bd670b35SErik Nordmark 	    &connp->conn_xmit_ipp));
105845916cd2Sjpk }
105945916cd2Sjpk 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * XXX implement more sophisticated logic
1062bd670b35SErik Nordmark  *
1063bd670b35SErik Nordmark  * Tsol note: We have already verified the addresses using tsol_check_dest
1064bd670b35SErik Nordmark  * in sctp_add_faddr, thus no need to redo that here.
1065bd670b35SErik Nordmark  * We do setup ipp_label_v4 and ipp_label_v6 based on which addresses
1066bd670b35SErik Nordmark  * we have.
10677c478bd9Sstevel@tonic-gate  */
106845916cd2Sjpk int
sctp_set_hdraddrs(sctp_t * sctp)106977c67f2fSkcpoon sctp_set_hdraddrs(sctp_t *sctp)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
10727c478bd9Sstevel@tonic-gate 	int gotv4 = 0;
10737c478bd9Sstevel@tonic-gate 	int gotv6 = 0;
1074bd670b35SErik Nordmark 	conn_t *connp = sctp->sctp_connp;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_faddrs != NULL);
10777c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	/* Set up using the primary first */
10806be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	connp->conn_faddr_v6 = sctp->sctp_primary->sf_faddr;
1081bd670b35SErik Nordmark 	/* saddr may be unspec; make_mp() will handle this */
10826be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	connp->conn_saddr_v6 = sctp->sctp_primary->sf_saddr;
1083bd670b35SErik Nordmark 	connp->conn_laddr_v6 = connp->conn_saddr_v6;
10846be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->sf_faddr)) {
1085bd670b35SErik Nordmark 		if (!is_system_labeled() ||
1086bd670b35SErik Nordmark 		    sctp_v4_label(sctp, sctp->sctp_primary) == 0) {
108745916cd2Sjpk 			gotv4 = 1;
1088bd670b35SErik Nordmark 			if (connp->conn_family == AF_INET) {
1089bd670b35SErik Nordmark 				goto done;
109045916cd2Sjpk 			}
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 	} else {
1093bd670b35SErik Nordmark 		if (!is_system_labeled() ||
1094bd670b35SErik Nordmark 		    sctp_v6_label(sctp, sctp->sctp_primary) == 0) {
109545916cd2Sjpk 			gotv6 = 1;
1096bd670b35SErik Nordmark 		}
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10996be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = sctp->sctp_faddrs; fp; fp = fp->sf_next) {
11006be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->sf_faddr)) {
1101bd670b35SErik Nordmark 			if (!is_system_labeled() ||
1102bd670b35SErik Nordmark 			    sctp_v4_label(sctp, fp) == 0) {
110345916cd2Sjpk 				gotv4 = 1;
1104bd670b35SErik Nordmark 				if (connp->conn_family == AF_INET || gotv6) {
110545916cd2Sjpk 					break;
110645916cd2Sjpk 				}
11077c478bd9Sstevel@tonic-gate 			}
11086be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		} else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->sf_faddr)) {
1109bd670b35SErik Nordmark 			if (!is_system_labeled() ||
1110bd670b35SErik Nordmark 			    sctp_v6_label(sctp, fp) == 0) {
111145916cd2Sjpk 				gotv6 = 1;
111245916cd2Sjpk 				if (gotv4)
111345916cd2Sjpk 					break;
11147c478bd9Sstevel@tonic-gate 			}
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
1118bd670b35SErik Nordmark done:
111945916cd2Sjpk 	if (!gotv4 && !gotv6)
112045916cd2Sjpk 		return (EACCES);
112145916cd2Sjpk 
112245916cd2Sjpk 	return (0);
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
1125c513743fSGeorge Shepherd /*
1126c513743fSGeorge Shepherd  * got_errchunk is set B_TRUE only if called from validate_init_params(), when
1127c513743fSGeorge Shepherd  * an ERROR chunk is already prepended the size of which needs updating for
1128c513743fSGeorge Shepherd  * additional unrecognized parameters. Other callers either prepend the ERROR
1129c513743fSGeorge Shepherd  * chunk with the correct size after calling this function, or they are calling
1130c513743fSGeorge Shepherd  * to add an invalid parameter to an INIT_ACK chunk, in that case no ERROR chunk
1131c513743fSGeorge Shepherd  * exists, the CAUSE blocks go into the INIT_ACK directly.
1132c513743fSGeorge Shepherd  *
1133c513743fSGeorge Shepherd  * *errmp will be non-NULL both when adding an additional CAUSE block to an
1134c513743fSGeorge Shepherd  * existing prepended COOKIE ERROR chunk (processing params of an INIT_ACK),
1135c513743fSGeorge Shepherd  * and when adding unrecognized parameters after the first, to an INIT_ACK
1136c513743fSGeorge Shepherd  * (processing params of an INIT chunk).
1137c513743fSGeorge Shepherd  */
11387c478bd9Sstevel@tonic-gate void
sctp_add_unrec_parm(sctp_parm_hdr_t * uph,mblk_t ** errmp,boolean_t got_errchunk)1139c513743fSGeorge Shepherd sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp,
1140c513743fSGeorge Shepherd     boolean_t got_errchunk)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11437c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
11447c478bd9Sstevel@tonic-gate 	size_t len;
11457c478bd9Sstevel@tonic-gate 	int pad;
114677ebe684SGeorge Shepherd 	sctp_chunk_hdr_t *ecp;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	len = sizeof (*ph) + ntohs(uph->sph_len);
114977ebe684SGeorge Shepherd 	if ((pad = len % SCTP_ALIGN) != 0) {
115077ebe684SGeorge Shepherd 		pad = SCTP_ALIGN - pad;
11517c478bd9Sstevel@tonic-gate 		len += pad;
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
11547c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
11557c478bd9Sstevel@tonic-gate 		return;
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
11597c478bd9Sstevel@tonic-gate 	ph->sph_type = htons(PARM_UNRECOGNIZED);
11607c478bd9Sstevel@tonic-gate 	ph->sph_len = htons(len - pad);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	/* copy in the unrecognized parameter */
11637c478bd9Sstevel@tonic-gate 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
11647c478bd9Sstevel@tonic-gate 
116577ebe684SGeorge Shepherd 	if (pad != 0)
116677ebe684SGeorge Shepherd 		bzero((mp->b_rptr + len - pad), pad);
116777ebe684SGeorge Shepherd 
11687c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
11697c478bd9Sstevel@tonic-gate 	if (*errmp != NULL) {
117077ebe684SGeorge Shepherd 		/*
1171c513743fSGeorge Shepherd 		 * Update total length if an ERROR chunk, then link
1172c513743fSGeorge Shepherd 		 * this CAUSE block to the possible chain of CAUSE
1173c513743fSGeorge Shepherd 		 * blocks attached to the ERROR chunk or INIT_ACK
1174c513743fSGeorge Shepherd 		 * being created.
117577ebe684SGeorge Shepherd 		 */
1176c513743fSGeorge Shepherd 		if (got_errchunk) {
1177c513743fSGeorge Shepherd 			/* ERROR chunk already prepended */
1178c513743fSGeorge Shepherd 			ecp = (sctp_chunk_hdr_t *)((*errmp)->b_rptr);
1179c513743fSGeorge Shepherd 			ecp->sch_len = htons(ntohs(ecp->sch_len) + len);
1180c513743fSGeorge Shepherd 		}
11817c478bd9Sstevel@tonic-gate 		linkb(*errmp, mp);
11827c478bd9Sstevel@tonic-gate 	} else {
11837c478bd9Sstevel@tonic-gate 		*errmp = mp;
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
11887c478bd9Sstevel@tonic-gate  * o Bounds checking
11897c478bd9Sstevel@tonic-gate  * o Updates remaining
11907c478bd9Sstevel@tonic-gate  * o Checks alignment
11917c478bd9Sstevel@tonic-gate  */
11927c478bd9Sstevel@tonic-gate sctp_parm_hdr_t *
sctp_next_parm(sctp_parm_hdr_t * current,ssize_t * remaining)11937c478bd9Sstevel@tonic-gate sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
11947c478bd9Sstevel@tonic-gate {
11957c478bd9Sstevel@tonic-gate 	int pad;
11967c478bd9Sstevel@tonic-gate 	uint16_t len;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	len = ntohs(current->sph_len);
11997c478bd9Sstevel@tonic-gate 	*remaining -= len;
12007c478bd9Sstevel@tonic-gate 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
12017c478bd9Sstevel@tonic-gate 		return (NULL);
12027c478bd9Sstevel@tonic-gate 	}
12037c478bd9Sstevel@tonic-gate 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
12047c478bd9Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
12057c478bd9Sstevel@tonic-gate 		*remaining -= pad;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 	/*LINTED pointer cast may result in improper alignment*/
12087c478bd9Sstevel@tonic-gate 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
12097c478bd9Sstevel@tonic-gate 	return (current);
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate /*
12137c478bd9Sstevel@tonic-gate  * Sets the address parameters given in the INIT chunk into sctp's
12147c478bd9Sstevel@tonic-gate  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
12157c478bd9Sstevel@tonic-gate  * no address parameters in the INIT chunk, a single faddr is created
12167c478bd9Sstevel@tonic-gate  * from the ip hdr at the beginning of pkt.
12177c478bd9Sstevel@tonic-gate  * If there already are existing addresses hanging from sctp, merge
12187c478bd9Sstevel@tonic-gate  * them in, if the old info contains addresses which are not present
12197c478bd9Sstevel@tonic-gate  * in this new info, get rid of them, and clean the pointers if there's
12207c478bd9Sstevel@tonic-gate  * messages which have this as their target address.
12217c478bd9Sstevel@tonic-gate  *
1222f551bb10Svi  * We also re-adjust the source address list here since the list may
1223f551bb10Svi  * contain more than what is actually part of the association. If
1224f551bb10Svi  * we get here from sctp_send_cookie_echo(), we are on the active
1225f551bb10Svi  * side and psctp will be NULL and ich will be the INIT-ACK chunk.
1226f551bb10Svi  * If we get here from sctp_accept_comm(), ich will be the INIT chunk
1227f551bb10Svi  * and psctp will the listening endpoint.
1228f551bb10Svi  *
1229f551bb10Svi  * INIT processing: When processing the INIT we inherit the src address
1230f551bb10Svi  * list from the listener. For a loopback or linklocal association, we
1231f551bb10Svi  * delete the list and just take the address from the IP header (since
1232f551bb10Svi  * that's how we created the INIT-ACK). Additionally, for loopback we
1233f551bb10Svi  * ignore the address params in the INIT. For determining which address
1234f551bb10Svi  * types were sent in the INIT-ACK we follow the same logic as in
1235f551bb10Svi  * creating the INIT-ACK. We delete addresses of the type that are not
1236f551bb10Svi  * supported by the peer.
1237f551bb10Svi  *
1238f551bb10Svi  * INIT-ACK processing: When processing the INIT-ACK since we had not
1239f551bb10Svi  * included addr params for loopback or linklocal addresses when creating
1240f551bb10Svi  * the INIT, we just use the address from the IP header. Further, for
1241f551bb10Svi  * loopback we ignore the addr param list. We mark addresses of the
1242f551bb10Svi  * type not supported by the peer as unconfirmed.
1243f551bb10Svi  *
1244f551bb10Svi  * In case of INIT processing we look for supported address types in the
1245f551bb10Svi  * supported address param, if present. In both cases the address type in
1246f551bb10Svi  * the IP header is supported as well as types for addresses in the param
1247f551bb10Svi  * list, if any.
1248f551bb10Svi  *
1249f551bb10Svi  * Once we have the supported address types sctp_check_saddr() runs through
1250f551bb10Svi  * the source address list and deletes or marks as unconfirmed address of
1251f551bb10Svi  * types not supported by the peer.
1252f551bb10Svi  *
12537c478bd9Sstevel@tonic-gate  * Returns 0 on success, sys errno on failure
12547c478bd9Sstevel@tonic-gate  */
12557c478bd9Sstevel@tonic-gate int
sctp_get_addrparams(sctp_t * sctp,sctp_t * psctp,mblk_t * pkt,sctp_chunk_hdr_t * ich,uint_t * sctp_options)12567c478bd9Sstevel@tonic-gate sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
12577c478bd9Sstevel@tonic-gate     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
12607c478bd9Sstevel@tonic-gate 	ipha_t			*iph;
12617c478bd9Sstevel@tonic-gate 	ip6_t			*ip6h;
1262f551bb10Svi 	in6_addr_t		hdrsaddr[1];
1263f551bb10Svi 	in6_addr_t		hdrdaddr[1];
12647c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
12657c478bd9Sstevel@tonic-gate 	ssize_t			remaining;
12667c478bd9Sstevel@tonic-gate 	int			isv4;
12677c478bd9Sstevel@tonic-gate 	int			err;
12687c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*fp;
1269f551bb10Svi 	int			supp_af = 0;
1270f551bb10Svi 	boolean_t		check_saddr = B_TRUE;
12711d8c4025Svi 	in6_addr_t		curaddr;
1272f4b3ec61Sdh 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1273bd670b35SErik Nordmark 	conn_t			*connp = sctp->sctp_connp;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	if (sctp_options != NULL)
12767c478bd9Sstevel@tonic-gate 		*sctp_options = 0;
12777c478bd9Sstevel@tonic-gate 
1278f551bb10Svi 	/* extract the address from the IP header */
1279f551bb10Svi 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1280f551bb10Svi 	if (isv4) {
1281f551bb10Svi 		iph = (ipha_t *)pkt->b_rptr;
1282f551bb10Svi 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr);
1283f551bb10Svi 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr);
1284f551bb10Svi 		supp_af |= PARM_SUPP_V4;
1285f551bb10Svi 	} else {
1286f551bb10Svi 		ip6h = (ip6_t *)pkt->b_rptr;
1287f551bb10Svi 		hdrsaddr[0] = ip6h->ip6_src;
1288f551bb10Svi 		hdrdaddr[0] = ip6h->ip6_dst;
1289f551bb10Svi 		supp_af |= PARM_SUPP_V6;
1290f551bb10Svi 	}
1291f551bb10Svi 
1292f551bb10Svi 	/*
1293f551bb10Svi 	 * Unfortunately, we can't delay this because adding an faddr
1294f551bb10Svi 	 * looks for the presence of the source address (from the ire
1295f551bb10Svi 	 * for the faddr) in the source address list. We could have
1296f551bb10Svi 	 * delayed this if, say, this was a loopback/linklocal connection.
1297f551bb10Svi 	 * Now, we just end up nuking this list and taking the addr from
1298f551bb10Svi 	 * the IP header for loopback/linklocal.
1299f551bb10Svi 	 */
13007c478bd9Sstevel@tonic-gate 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
13017c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
13047c478bd9Sstevel@tonic-gate 		if (err != 0)
13057c478bd9Sstevel@tonic-gate 			return (err);
13067c478bd9Sstevel@tonic-gate 	}
1307f551bb10Svi 	/*
1308f551bb10Svi 	 * We will add the faddr before parsing the address list as this
1309f551bb10Svi 	 * might be a loopback connection and we would not have to
1310f551bb10Svi 	 * go through the list.
1311f551bb10Svi 	 *
1312f551bb10Svi 	 * Make sure the header's addr is in the list
1313f551bb10Svi 	 */
1314f551bb10Svi 	fp = sctp_lookup_faddr(sctp, hdrsaddr);
1315f551bb10Svi 	if (fp == NULL) {
1316f551bb10Svi 		/* not included; add it now */
131777c67f2fSkcpoon 		err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE);
131845916cd2Sjpk 		if (err != 0)
131945916cd2Sjpk 			return (err);
13207c478bd9Sstevel@tonic-gate 
1321f551bb10Svi 		/* sctp_faddrs will be the hdr addr */
1322f551bb10Svi 		fp = sctp->sctp_faddrs;
13237c478bd9Sstevel@tonic-gate 	}
1324f551bb10Svi 	/* make the header addr the primary */
13251d8c4025Svi 
13261d8c4025Svi 	if (cl_sctp_assoc_change != NULL && psctp == NULL)
13276be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		curaddr = sctp->sctp_current->sf_faddr;
13281d8c4025Svi 
1329f551bb10Svi 	sctp->sctp_primary = fp;
1330f551bb10Svi 	sctp->sctp_current = fp;
13316be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	sctp->sctp_mss = fp->sf_pmss;
13327c478bd9Sstevel@tonic-gate 
1333f551bb10Svi 	/* For loopback connections & linklocal get address from the header */
1334f551bb10Svi 	if (sctp->sctp_loopback || sctp->sctp_linklocal) {
1335f551bb10Svi 		if (sctp->sctp_nsaddrs != 0)
1336f551bb10Svi 			sctp_free_saddrs(sctp);
13371d8c4025Svi 		if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0)
1338f551bb10Svi 			return (err);
1339f551bb10Svi 		/* For loopback ignore address list */
1340f551bb10Svi 		if (sctp->sctp_loopback)
1341f551bb10Svi 			return (0);
1342f551bb10Svi 		check_saddr = B_FALSE;
1343f551bb10Svi 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
13467c478bd9Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
13477c478bd9Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
13487c478bd9Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
1349f551bb10Svi 		if (check_saddr) {
1350f551bb10Svi 			sctp_check_saddr(sctp, supp_af, psctp == NULL ?
1351c31292eeSkcpoon 			    B_FALSE : B_TRUE, hdrdaddr);
1352f551bb10Svi 		}
13531d8c4025Svi 		ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1354f551bb10Svi 		return (0);
13557c478bd9Sstevel@tonic-gate 	}
1356f551bb10Svi 
13577c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
13587c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
13597c478bd9Sstevel@tonic-gate 
1360f551bb10Svi 	/* params will have already been byteordered when validating */
13617c478bd9Sstevel@tonic-gate 	while (ph != NULL) {
1362f551bb10Svi 		if (ph->sph_type == htons(PARM_SUPP_ADDRS)) {
1363f551bb10Svi 			int		plen;
1364f551bb10Svi 			uint16_t	*p;
1365f551bb10Svi 			uint16_t	addrtype;
1366f551bb10Svi 
1367f551bb10Svi 			ASSERT(psctp != NULL);
1368f551bb10Svi 			plen = ntohs(ph->sph_len);
1369f551bb10Svi 			p = (uint16_t *)(ph + 1);
1370f551bb10Svi 			while (plen > 0) {
1371f551bb10Svi 				addrtype = ntohs(*p);
1372f551bb10Svi 				switch (addrtype) {
1373f551bb10Svi 					case PARM_ADDR6:
1374f551bb10Svi 						supp_af |= PARM_SUPP_V6;
1375f551bb10Svi 						break;
1376f551bb10Svi 					case PARM_ADDR4:
1377f551bb10Svi 						supp_af |= PARM_SUPP_V4;
1378f551bb10Svi 						break;
1379f551bb10Svi 					default:
1380f551bb10Svi 						break;
1381f551bb10Svi 				}
1382f551bb10Svi 				p++;
1383f551bb10Svi 				plen -= sizeof (*p);
1384f551bb10Svi 			}
1385f551bb10Svi 		} else if (ph->sph_type == htons(PARM_ADDR4)) {
13867c478bd9Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
13877c478bd9Sstevel@tonic-gate 				in6_addr_t addr;
13887c478bd9Sstevel@tonic-gate 				ipaddr_t ta;
13897c478bd9Sstevel@tonic-gate 
1390f551bb10Svi 				supp_af |= PARM_SUPP_V4;
13917c478bd9Sstevel@tonic-gate 				/*
13927c478bd9Sstevel@tonic-gate 				 * Screen out broad/multicasts & loopback.
13937c478bd9Sstevel@tonic-gate 				 * If the endpoint only accepts v6 address,
13947c478bd9Sstevel@tonic-gate 				 * go to the next one.
1395c31292eeSkcpoon 				 *
1396c31292eeSkcpoon 				 * Subnet broadcast check is done in
1397c31292eeSkcpoon 				 * sctp_add_faddr().  If the address is
1398c31292eeSkcpoon 				 * a broadcast address, it won't be added.
13997c478bd9Sstevel@tonic-gate 				 */
14007c478bd9Sstevel@tonic-gate 				bcopy(ph + 1, &ta, sizeof (ta));
14017c478bd9Sstevel@tonic-gate 				if (ta == 0 ||
14027c478bd9Sstevel@tonic-gate 				    ta == INADDR_BROADCAST ||
14037c478bd9Sstevel@tonic-gate 				    ta == htonl(INADDR_LOOPBACK) ||
1404bd670b35SErik Nordmark 				    CLASSD(ta) || connp->conn_ipv6_v6only) {
14057c478bd9Sstevel@tonic-gate 					goto next;
14067c478bd9Sstevel@tonic-gate 				}
14077c478bd9Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
14087c478bd9Sstevel@tonic-gate 				    (ph + 1), &addr);
1409c31292eeSkcpoon 
14107c478bd9Sstevel@tonic-gate 				/* Check for duplicate. */
14117c478bd9Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
14127c478bd9Sstevel@tonic-gate 					goto next;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 				/* OK, add it to the faddr set */
141577c67f2fSkcpoon 				err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP,
141677c67f2fSkcpoon 				    B_FALSE);
1417c31292eeSkcpoon 				/* Something is wrong...  Try the next one. */
141845916cd2Sjpk 				if (err != 0)
1419c31292eeSkcpoon 					goto next;
14207c478bd9Sstevel@tonic-gate 			}
14217c478bd9Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
1422bd670b35SErik Nordmark 		    connp->conn_family == AF_INET6) {
14237c478bd9Sstevel@tonic-gate 			/* An v4 socket should not take v6 addresses. */
14247c478bd9Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
14257c478bd9Sstevel@tonic-gate 				in6_addr_t *addr6;
14267c478bd9Sstevel@tonic-gate 
1427f551bb10Svi 				supp_af |= PARM_SUPP_V6;
14287c478bd9Sstevel@tonic-gate 				addr6 = (in6_addr_t *)(ph + 1);
14297c478bd9Sstevel@tonic-gate 				/*
14307c478bd9Sstevel@tonic-gate 				 * Screen out link locals, mcast, loopback
14317c478bd9Sstevel@tonic-gate 				 * and bogus v6 address.
14327c478bd9Sstevel@tonic-gate 				 */
14337c478bd9Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
14347c478bd9Sstevel@tonic-gate 				    IN6_IS_ADDR_MULTICAST(addr6) ||
14357c478bd9Sstevel@tonic-gate 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
14367c478bd9Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
14377c478bd9Sstevel@tonic-gate 					goto next;
14387c478bd9Sstevel@tonic-gate 				}
14397c478bd9Sstevel@tonic-gate 				/* Check for duplicate. */
14407c478bd9Sstevel@tonic-gate 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
14417c478bd9Sstevel@tonic-gate 					goto next;
14427c478bd9Sstevel@tonic-gate 
144345916cd2Sjpk 				err = sctp_add_faddr(sctp,
144477c67f2fSkcpoon 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP,
144577c67f2fSkcpoon 				    B_FALSE);
1446c31292eeSkcpoon 				/* Something is wrong...  Try the next one. */
144745916cd2Sjpk 				if (err != 0)
1448c31292eeSkcpoon 					goto next;
14497c478bd9Sstevel@tonic-gate 			}
14507c478bd9Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
14517c478bd9Sstevel@tonic-gate 			if (sctp_options != NULL)
14527c478bd9Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
14537c478bd9Sstevel@tonic-gate 		} /* else; skip */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate next:
14567c478bd9Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
14577c478bd9Sstevel@tonic-gate 	}
1458f551bb10Svi 	if (check_saddr) {
1459f551bb10Svi 		sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE :
1460c31292eeSkcpoon 		    B_TRUE, hdrdaddr);
14617c478bd9Sstevel@tonic-gate 	}
14621d8c4025Svi 	ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
14631d8c4025Svi 	/*
14641d8c4025Svi 	 * We have the right address list now, update clustering's
14651d8c4025Svi 	 * knowledge because when we sent the INIT we had just added
14661d8c4025Svi 	 * the address the INIT was sent to.
14671d8c4025Svi 	 */
14681d8c4025Svi 	if (psctp == NULL && cl_sctp_assoc_change != NULL) {
14691d8c4025Svi 		uchar_t	*alist;
14701d8c4025Svi 		size_t	asize;
14711d8c4025Svi 		uchar_t	*dlist;
14721d8c4025Svi 		size_t	dsize;
14731d8c4025Svi 
14741d8c4025Svi 		asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
14751d8c4025Svi 		alist = kmem_alloc(asize, KM_NOSLEEP);
147677c67f2fSkcpoon 		if (alist == NULL) {
1477f4b3ec61Sdh 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
14781d8c4025Svi 			return (ENOMEM);
147977c67f2fSkcpoon 		}
14801d8c4025Svi 		/*
14811d8c4025Svi 		 * Just include the address the INIT was sent to in the
14821d8c4025Svi 		 * delete list and send the entire faddr list. We could
14831d8c4025Svi 		 * do it differently (i.e include all the addresses in the
14841d8c4025Svi 		 * add list even if it contains the original address OR
14851d8c4025Svi 		 * remove the original address from the add list etc.), but
14861d8c4025Svi 		 * this seems reasonable enough.
14871d8c4025Svi 		 */
14881d8c4025Svi 		dsize = sizeof (in6_addr_t);
14891d8c4025Svi 		dlist = kmem_alloc(dsize, KM_NOSLEEP);
14901d8c4025Svi 		if (dlist == NULL) {
14911d8c4025Svi 			kmem_free(alist, asize);
1492f4b3ec61Sdh 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
14931d8c4025Svi 			return (ENOMEM);
14941d8c4025Svi 		}
14951d8c4025Svi 		bcopy(&curaddr, dlist, sizeof (curaddr));
14961d8c4025Svi 		sctp_get_faddr_list(sctp, alist, asize);
1497bd670b35SErik Nordmark 		(*cl_sctp_assoc_change)(connp->conn_family, alist, asize,
14981d8c4025Svi 		    sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR,
14991d8c4025Svi 		    (cl_sctp_handle_t)sctp);
15001d8c4025Svi 		/* alist and dlist will be freed by the clustering module */
15011d8c4025Svi 	}
15027c478bd9Sstevel@tonic-gate 	return (0);
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate /*
15067c478bd9Sstevel@tonic-gate  * Returns 0 if the check failed and the restart should be refused,
15077c478bd9Sstevel@tonic-gate  * 1 if the check succeeded.
15087c478bd9Sstevel@tonic-gate  */
15097c478bd9Sstevel@tonic-gate int
sctp_secure_restart_check(mblk_t * pkt,sctp_chunk_hdr_t * ich,uint32_t ports,int sleep,sctp_stack_t * sctps,ip_recv_attr_t * ira)15107c478bd9Sstevel@tonic-gate sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
1511bd670b35SErik Nordmark     int sleep, sctp_stack_t *sctps, ip_recv_attr_t *ira)
15127c478bd9Sstevel@tonic-gate {
15137f093707Skcpoon 	sctp_faddr_t *fp, *fphead = NULL;
15147c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
15157c478bd9Sstevel@tonic-gate 	ssize_t remaining;
15167c478bd9Sstevel@tonic-gate 	int isv4;
15177c478bd9Sstevel@tonic-gate 	ipha_t *iph;
15187c478bd9Sstevel@tonic-gate 	ip6_t *ip6h;
15197c478bd9Sstevel@tonic-gate 	in6_addr_t hdraddr[1];
15207c478bd9Sstevel@tonic-gate 	int retval = 0;
15217c478bd9Sstevel@tonic-gate 	sctp_tf_t *tf;
15227c478bd9Sstevel@tonic-gate 	sctp_t *sctp;
15237c478bd9Sstevel@tonic-gate 	int compres;
15247c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t *init;
15257c478bd9Sstevel@tonic-gate 	int nadded = 0;
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/* extract the address from the IP header */
15287c478bd9Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
15297c478bd9Sstevel@tonic-gate 	if (isv4) {
15307c478bd9Sstevel@tonic-gate 		iph = (ipha_t *)pkt->b_rptr;
15317c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
15327c478bd9Sstevel@tonic-gate 	} else {
15337c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)pkt->b_rptr;
15347c478bd9Sstevel@tonic-gate 		hdraddr[0] = ip6h->ip6_src;
15357c478bd9Sstevel@tonic-gate 	}
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	/* Walk the params in the INIT [ACK], pulling out addr params */
15387c478bd9Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
15397c478bd9Sstevel@tonic-gate 	    sizeof (sctp_init_chunk_t);
15407c478bd9Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
15417c478bd9Sstevel@tonic-gate 		/* no parameters; restart OK */
15427c478bd9Sstevel@tonic-gate 		return (1);
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
15457c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(init + 1);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	while (ph != NULL) {
15487f093707Skcpoon 		sctp_faddr_t *fpa = NULL;
15497f093707Skcpoon 
15507c478bd9Sstevel@tonic-gate 		/* params will have already been byteordered when validating */
15517c478bd9Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_ADDR4)) {
15527c478bd9Sstevel@tonic-gate 			if (remaining >= PARM_ADDR4_LEN) {
15537c478bd9Sstevel@tonic-gate 				in6_addr_t addr;
15547c478bd9Sstevel@tonic-gate 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
15557c478bd9Sstevel@tonic-gate 				    (ph + 1), &addr);
15567c478bd9Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15577c478bd9Sstevel@tonic-gate 				    sleep);
15587f093707Skcpoon 				if (fpa == NULL) {
15597c478bd9Sstevel@tonic-gate 					goto done;
15607c478bd9Sstevel@tonic-gate 				}
15617c478bd9Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
15626be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				fpa->sf_faddr = addr;
15636be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				fpa->sf_next = NULL;
15647c478bd9Sstevel@tonic-gate 			}
15657c478bd9Sstevel@tonic-gate 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
15667c478bd9Sstevel@tonic-gate 			if (remaining >= PARM_ADDR6_LEN) {
15677c478bd9Sstevel@tonic-gate 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
15687c478bd9Sstevel@tonic-gate 				    sleep);
15697f093707Skcpoon 				if (fpa == NULL) {
15707c478bd9Sstevel@tonic-gate 					goto done;
15717c478bd9Sstevel@tonic-gate 				}
15727c478bd9Sstevel@tonic-gate 				bzero(fpa, sizeof (*fpa));
15736be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				bcopy(ph + 1, &fpa->sf_faddr,
15746be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				    sizeof (fpa->sf_faddr));
15756be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				fpa->sf_next = NULL;
15767c478bd9Sstevel@tonic-gate 			}
15777c478bd9Sstevel@tonic-gate 		}
15787c478bd9Sstevel@tonic-gate 		/* link in the new addr, if it was an addr param */
15797f093707Skcpoon 		if (fpa != NULL) {
15807f093707Skcpoon 			if (fphead == NULL) {
15817c478bd9Sstevel@tonic-gate 				fphead = fpa;
15827c478bd9Sstevel@tonic-gate 			} else {
15836be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				fpa->sf_next = fphead;
15847f093707Skcpoon 				fphead = fpa;
15857c478bd9Sstevel@tonic-gate 			}
15867c478bd9Sstevel@tonic-gate 		}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	if (fphead == NULL) {
15927c478bd9Sstevel@tonic-gate 		/* no addr parameters; restart OK */
15937c478bd9Sstevel@tonic-gate 		return (1);
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/*
15977c478bd9Sstevel@tonic-gate 	 * got at least one; make sure the header's addr is
15987c478bd9Sstevel@tonic-gate 	 * in the list
15997c478bd9Sstevel@tonic-gate 	 */
16007c478bd9Sstevel@tonic-gate 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
16017f093707Skcpoon 	if (fp == NULL) {
16027c478bd9Sstevel@tonic-gate 		/* not included; add it now */
16037c478bd9Sstevel@tonic-gate 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
16047f093707Skcpoon 		if (fp == NULL) {
16057c478bd9Sstevel@tonic-gate 			goto done;
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 		bzero(fp, sizeof (*fp));
16086be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_faddr = *hdraddr;
16096be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_next = fphead;
16107c478bd9Sstevel@tonic-gate 		fphead = fp;
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	/*
16147c478bd9Sstevel@tonic-gate 	 * Now, we can finally do the check: For each sctp instance
16157c478bd9Sstevel@tonic-gate 	 * on the hash line for ports, compare its faddr set against
16167c478bd9Sstevel@tonic-gate 	 * the new one. If the new one is a strict subset of any
16177c478bd9Sstevel@tonic-gate 	 * existing sctp's faddrs, the restart is OK. However, if there
16187c478bd9Sstevel@tonic-gate 	 * is an overlap, this could be an attack, so return failure.
16197c478bd9Sstevel@tonic-gate 	 * If all sctp's faddrs are disjoint, this is a legitimate new
16207c478bd9Sstevel@tonic-gate 	 * association.
16217c478bd9Sstevel@tonic-gate 	 */
1622f4b3ec61Sdh 	tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]);
16237c478bd9Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
1626bd670b35SErik Nordmark 		if (ports != sctp->sctp_connp->conn_ports) {
16277c478bd9Sstevel@tonic-gate 			continue;
16287c478bd9Sstevel@tonic-gate 		}
16297c478bd9Sstevel@tonic-gate 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
16307c478bd9Sstevel@tonic-gate 		if (compres <= SCTP_ADDR_SUBSET) {
16317c478bd9Sstevel@tonic-gate 			retval = 1;
16327c478bd9Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16337c478bd9Sstevel@tonic-gate 			goto done;
16347c478bd9Sstevel@tonic-gate 		}
16357c478bd9Sstevel@tonic-gate 		if (compres == SCTP_ADDR_OVERLAP) {
16367c478bd9Sstevel@tonic-gate 			dprint(1,
16377c478bd9Sstevel@tonic-gate 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
163845916cd2Sjpk 			    SCTP_PRINTADDR(*hdraddr), (void *)sctp));
16397c478bd9Sstevel@tonic-gate 			/*
16407c478bd9Sstevel@tonic-gate 			 * While we still hold the lock, we need to
16417c478bd9Sstevel@tonic-gate 			 * figure out which addresses have been
16427c478bd9Sstevel@tonic-gate 			 * added so we can include them in the abort
16437c478bd9Sstevel@tonic-gate 			 * we will send back. Since these faddrs will
16447c478bd9Sstevel@tonic-gate 			 * never be used, we overload the rto field
16457c478bd9Sstevel@tonic-gate 			 * here, setting it to 0 if the address was
16467c478bd9Sstevel@tonic-gate 			 * not added, 1 if it was added.
16477c478bd9Sstevel@tonic-gate 			 */
16486be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			for (fp = fphead; fp; fp = fp->sf_next) {
16496be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				if (sctp_lookup_faddr(sctp, &fp->sf_faddr)) {
16506be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 					fp->sf_rto = 0;
16517c478bd9Sstevel@tonic-gate 				} else {
16526be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 					fp->sf_rto = 1;
16537c478bd9Sstevel@tonic-gate 					nadded++;
16547c478bd9Sstevel@tonic-gate 				}
16557c478bd9Sstevel@tonic-gate 			}
16567c478bd9Sstevel@tonic-gate 			mutex_exit(&tf->tf_lock);
16577c478bd9Sstevel@tonic-gate 			goto done;
16587c478bd9Sstevel@tonic-gate 		}
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/* All faddrs are disjoint; legit new association */
16637c478bd9Sstevel@tonic-gate 	retval = 1;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate done:
16667c478bd9Sstevel@tonic-gate 	/* If are attempted adds, send back an abort listing the addrs */
16677c478bd9Sstevel@tonic-gate 	if (nadded > 0) {
16687c478bd9Sstevel@tonic-gate 		void *dtail;
16697c478bd9Sstevel@tonic-gate 		size_t dlen;
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
16727c478bd9Sstevel@tonic-gate 		if (dtail == NULL) {
16737c478bd9Sstevel@tonic-gate 			goto cleanup;
16747c478bd9Sstevel@tonic-gate 		}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		ph = dtail;
16777c478bd9Sstevel@tonic-gate 		dlen = 0;
16786be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		for (fp = fphead; fp; fp = fp->sf_next) {
16796be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (fp->sf_rto == 0) {
16807c478bd9Sstevel@tonic-gate 				continue;
16817c478bd9Sstevel@tonic-gate 			}
16826be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (IN6_IS_ADDR_V4MAPPED(&fp->sf_faddr)) {
16837c478bd9Sstevel@tonic-gate 				ipaddr_t addr4;
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR4);
16867c478bd9Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR4_LEN);
16876be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				IN6_V4MAPPED_TO_IPADDR(&fp->sf_faddr, addr4);
16887c478bd9Sstevel@tonic-gate 				ph++;
16897c478bd9Sstevel@tonic-gate 				bcopy(&addr4, ph, sizeof (addr4));
16907c478bd9Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
16917c478bd9Sstevel@tonic-gate 				    ((char *)ph + sizeof (addr4));
16927c478bd9Sstevel@tonic-gate 				dlen += PARM_ADDR4_LEN;
16937c478bd9Sstevel@tonic-gate 			} else {
16947c478bd9Sstevel@tonic-gate 				ph->sph_type = htons(PARM_ADDR6);
16957c478bd9Sstevel@tonic-gate 				ph->sph_len = htons(PARM_ADDR6_LEN);
16967c478bd9Sstevel@tonic-gate 				ph++;
16976be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				bcopy(&fp->sf_faddr, ph, sizeof (fp->sf_faddr));
16987c478bd9Sstevel@tonic-gate 				ph = (sctp_parm_hdr_t *)
16996be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				    ((char *)ph + sizeof (fp->sf_faddr));
17007c478bd9Sstevel@tonic-gate 				dlen += PARM_ADDR6_LEN;
17017c478bd9Sstevel@tonic-gate 			}
17027c478bd9Sstevel@tonic-gate 		}
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 		/* Send off the abort */
17057c478bd9Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ich),
1706bd670b35SErik Nordmark 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE,
1707bd670b35SErik Nordmark 		    ira);
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate cleanup:
17137c478bd9Sstevel@tonic-gate 	/* Clean up */
17147c478bd9Sstevel@tonic-gate 	if (fphead) {
17157c478bd9Sstevel@tonic-gate 		sctp_faddr_t *fpn;
17167c478bd9Sstevel@tonic-gate 		for (fp = fphead; fp; fp = fpn) {
17176be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			fpn = fp->sf_next;
17186be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 			if (fp->sf_ixa != NULL) {
17196be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				ixa_refrele(fp->sf_ixa);
17206be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 				fp->sf_ixa = NULL;
1721bd670b35SErik Nordmark 			}
17227c478bd9Sstevel@tonic-gate 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
17237c478bd9Sstevel@tonic-gate 		}
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	return (retval);
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate 
1729769b977dSvi /*
1730769b977dSvi  * Reset any state related to transmitted chunks.
1731769b977dSvi  */
17327c478bd9Sstevel@tonic-gate void
sctp_congest_reset(sctp_t * sctp)17337c478bd9Sstevel@tonic-gate sctp_congest_reset(sctp_t *sctp)
17347c478bd9Sstevel@tonic-gate {
1735769b977dSvi 	sctp_faddr_t	*fp;
1736f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1737769b977dSvi 	mblk_t		*mp;
17387c478bd9Sstevel@tonic-gate 
17396be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
17406be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_ssthresh = sctps->sctps_initial_mtu;
17416be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		SET_CWND(fp, fp->sf_pmss, sctps->sctps_slow_start_initial);
17426be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_suna = 0;
17436be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_pba = 0;
17447c478bd9Sstevel@tonic-gate 	}
1745769b977dSvi 	/*
1746769b977dSvi 	 * Clean up the transmit list as well since we have reset accounting
1747769b977dSvi 	 * on all the fps. Send event upstream, if required.
1748769b977dSvi 	 */
1749769b977dSvi 	while ((mp = sctp->sctp_xmit_head) != NULL) {
1750769b977dSvi 		sctp->sctp_xmit_head = mp->b_next;
1751769b977dSvi 		mp->b_next = NULL;
1752769b977dSvi 		if (sctp->sctp_xmit_head != NULL)
1753769b977dSvi 			sctp->sctp_xmit_head->b_prev = NULL;
1754769b977dSvi 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
1755769b977dSvi 	}
1756769b977dSvi 	sctp->sctp_xmit_head = NULL;
1757769b977dSvi 	sctp->sctp_xmit_tail = NULL;
1758769b977dSvi 	sctp->sctp_xmit_unacked = NULL;
1759769b977dSvi 
1760769b977dSvi 	sctp->sctp_unacked = 0;
1761769b977dSvi 	/*
1762769b977dSvi 	 * Any control message as well. We will clean-up this list as well.
1763769b977dSvi 	 * This contains any pending ASCONF request that we have queued/sent.
1764769b977dSvi 	 * If we do get an ACK we will just drop it. However, given that
1765769b977dSvi 	 * we are restarting chances are we aren't going to get any.
1766769b977dSvi 	 */
1767769b977dSvi 	if (sctp->sctp_cxmit_list != NULL)
1768769b977dSvi 		sctp_asconf_free_cxmit(sctp, NULL);
1769769b977dSvi 	sctp->sctp_cxmit_list = NULL;
1770769b977dSvi 	sctp->sctp_cchunk_pend = 0;
1771769b977dSvi 
1772769b977dSvi 	sctp->sctp_rexmitting = B_FALSE;
1773769b977dSvi 	sctp->sctp_rxt_nxttsn = 0;
1774769b977dSvi 	sctp->sctp_rxt_maxtsn = 0;
1775769b977dSvi 
1776769b977dSvi 	sctp->sctp_zero_win_probe = B_FALSE;
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate static void
sctp_init_faddr(sctp_t * sctp,sctp_faddr_t * fp,in6_addr_t * addr,mblk_t * timer_mp)178077c67f2fSkcpoon sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr,
178177c67f2fSkcpoon     mblk_t *timer_mp)
17827c478bd9Sstevel@tonic-gate {
1783f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1784f4b3ec61Sdh 
17856be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_ixa != NULL);
1786bd670b35SErik Nordmark 
17876be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	bcopy(addr, &fp->sf_faddr, sizeof (*addr));
17887c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
17896be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_isv4 = 1;
17906be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		/* Make sure that sf_pmss is a multiple of SCTP_ALIGN. */
17916be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_pmss =
1792121e5416Skcpoon 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) &
1793121e5416Skcpoon 		    ~(SCTP_ALIGN - 1);
17946be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_ixa->ixa_flags |= IXAF_IS_IPV4;
17957c478bd9Sstevel@tonic-gate 	} else {
17966be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_isv4 = 0;
17976be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_pmss =
1798f4b3ec61Sdh 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) &
1799f4b3ec61Sdh 		    ~(SCTP_ALIGN - 1);
18006be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		fp->sf_ixa->ixa_flags &= ~IXAF_IS_IPV4;
18017c478bd9Sstevel@tonic-gate 	}
18026be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_cwnd = sctps->sctps_slow_start_initial * fp->sf_pmss;
18036be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_rto = MIN(sctp->sctp_rto_initial, sctp->sctp_rto_max_init);
18049f13099eSGeorge Shepherd 	SCTP_MAX_RTO(sctp, fp);
18056be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_srtt = -1;
18066be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_rtt_updates = 0;
18076be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_strikes = 0;
18086be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_max_retr = sctp->sctp_pp_max_rxt;
18097c478bd9Sstevel@tonic-gate 	/* Mark it as not confirmed. */
18106be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_state = SCTP_FADDRS_UNCONFIRMED;
18116be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_hb_interval = sctp->sctp_hb_interval;
18126be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_ssthresh = sctps->sctps_initial_ssthresh;
18136be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_suna = 0;
18146be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_pba = 0;
18156be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_acked = 0;
18166be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_lastactive = fp->sf_hb_expiry = ddi_get_lbolt64();
18176be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_timer_mp = timer_mp;
18186be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_hb_pending = B_FALSE;
18196be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_hb_enabled = B_TRUE;
18206be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_df = 1;
18216be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_pmtu_discovered = 0;
18226be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_next = NULL;
18236be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_T3expire = 0;
18246be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	(void) random_get_pseudo_bytes((uint8_t *)&fp->sf_hb_secret,
18256be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	    sizeof (fp->sf_hb_secret));
18266be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_rxt_unacked = 0;
18277c478bd9Sstevel@tonic-gate 
1828bd670b35SErik Nordmark 	sctp_get_dest(sctp, fp);
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
1831b5fca8f8Stomee /*ARGSUSED*/
1832b5fca8f8Stomee static int
faddr_constructor(void * buf,void * arg,int flags)1833b5fca8f8Stomee faddr_constructor(void *buf, void *arg, int flags)
1834b5fca8f8Stomee {
1835b5fca8f8Stomee 	sctp_faddr_t *fp = buf;
1836b5fca8f8Stomee 
18376be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_timer_mp = NULL;
18386be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_timer_running = 0;
1839b5fca8f8Stomee 
18406be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_rc_timer_mp = NULL;
18416be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	fp->sf_rc_timer_running = 0;
1842b5fca8f8Stomee 
1843b5fca8f8Stomee 	return (0);
1844b5fca8f8Stomee }
1845b5fca8f8Stomee 
18467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18477c478bd9Sstevel@tonic-gate static void
faddr_destructor(void * buf,void * arg)1848b5fca8f8Stomee faddr_destructor(void *buf, void *arg)
18497c478bd9Sstevel@tonic-gate {
18507c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp = buf;
18517c478bd9Sstevel@tonic-gate 
18526be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_timer_mp == NULL);
18536be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_timer_running == 0);
18547c478bd9Sstevel@tonic-gate 
18556be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_rc_timer_mp == NULL);
18566be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	ASSERT(fp->sf_rc_timer_running == 0);
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate void
sctp_faddr_init(void)186045916cd2Sjpk sctp_faddr_init(void)
18617c478bd9Sstevel@tonic-gate {
18627c478bd9Sstevel@tonic-gate 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
1863b5fca8f8Stomee 	    sizeof (sctp_faddr_t), 0, faddr_constructor, faddr_destructor,
18647c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate void
sctp_faddr_fini(void)186845916cd2Sjpk sctp_faddr_fini(void)
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_faddr_cache);
18717c478bd9Sstevel@tonic-gate }
1872