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  */
21*77c67f2fSkcpoon 
227c478bd9Sstevel@tonic-gate /*
2345916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/stream.h>
327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
357c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
367c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
377c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
387c478bd9Sstevel@tonic-gate #include <sys/socket.h>
3945916cd2Sjpk #include <sys/tsol/tndb.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <inet/common.h>
457c478bd9Sstevel@tonic-gate #include <inet/ip.h>
467c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
477c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
487c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
517c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Common accept code.  Called by sctp_conn_request.
557c478bd9Sstevel@tonic-gate  * cr_pkt is the INIT / INIT ACK packet.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate static int
587c478bd9Sstevel@tonic-gate sctp_accept_comm(sctp_t *listener, sctp_t *acceptor, mblk_t *cr_pkt,
597c478bd9Sstevel@tonic-gate     uint_t ip_hdr_len, sctp_init_chunk_t *iack)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	sctp_hdr_t		*sctph;
637c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ich;
647c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
657c478bd9Sstevel@tonic-gate 	int			err;
667c478bd9Sstevel@tonic-gate 	uint_t			sctp_options;
6745916cd2Sjpk 	conn_t			*lconnp;
6845916cd2Sjpk 	cred_t			*cr;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(cr_pkt->b_rptr + ip_hdr_len);
717c478bd9Sstevel@tonic-gate 	ASSERT(OK_32PTR(sctph));
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	acceptor->sctp_lport = listener->sctp_lport;
747c478bd9Sstevel@tonic-gate 	acceptor->sctp_fport = sctph->sh_sport;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	ich = (sctp_chunk_hdr_t *)(iack + 1);
777c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/* acceptor isn't in any fanouts yet, so don't need to hold locks */
807c478bd9Sstevel@tonic-gate 	ASSERT(acceptor->sctp_faddrs == NULL);
817c478bd9Sstevel@tonic-gate 	err = sctp_get_addrparams(acceptor, listener, cr_pkt, ich,
827c478bd9Sstevel@tonic-gate 	    &sctp_options);
837c478bd9Sstevel@tonic-gate 	if (err != 0)
847c478bd9Sstevel@tonic-gate 		return (err);
857c478bd9Sstevel@tonic-gate 
8645916cd2Sjpk 	lconnp = listener->sctp_connp;
8745916cd2Sjpk 	if (lconnp->conn_mlp_type != mlptSingle) {
8845916cd2Sjpk 		cr = lconnp->conn_peercred = DB_CRED(cr_pkt);
8945916cd2Sjpk 		if (cr != NULL)
9045916cd2Sjpk 			crhold(cr);
9145916cd2Sjpk 		else
9245916cd2Sjpk 			cr = lconnp->conn_cred;
9345916cd2Sjpk 	} else {
9445916cd2Sjpk 		cr = lconnp->conn_cred;
9545916cd2Sjpk 	}
9645916cd2Sjpk 
97*77c67f2fSkcpoon 	if ((err = sctp_set_hdraddrs(acceptor)) != 0)
9845916cd2Sjpk 		return (err);
9945916cd2Sjpk 
1007c478bd9Sstevel@tonic-gate 	if ((sctp_options & SCTP_PRSCTP_OPTION) &&
1017c478bd9Sstevel@tonic-gate 	    listener->sctp_prsctp_aware && sctp_prsctp_enabled) {
1027c478bd9Sstevel@tonic-gate 		acceptor->sctp_prsctp_aware = B_TRUE;
1037c478bd9Sstevel@tonic-gate 	} else {
1047c478bd9Sstevel@tonic-gate 		acceptor->sctp_prsctp_aware = B_FALSE;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	/* The new sctp_t is fully bound now. */
1077c478bd9Sstevel@tonic-gate 	acceptor->sctp_connp->conn_fully_bound = B_TRUE;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* Get  initial TSNs */
1107c478bd9Sstevel@tonic-gate 	acceptor->sctp_ltsn = ntohl(iack->sic_inittsn);
1117c478bd9Sstevel@tonic-gate 	acceptor->sctp_recovery_tsn = acceptor->sctp_lastack_rxd =
1127c478bd9Sstevel@tonic-gate 	    acceptor->sctp_ltsn - 1;
1137c478bd9Sstevel@tonic-gate 	acceptor->sctp_adv_pap = acceptor->sctp_lastack_rxd;
1147c478bd9Sstevel@tonic-gate 	/* Serial numbers are initialized to the same value as the TSNs */
1157c478bd9Sstevel@tonic-gate 	acceptor->sctp_lcsn = acceptor->sctp_ltsn;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (!sctp_initialize_params(acceptor, init, iack))
1187c478bd9Sstevel@tonic-gate 		return (ENOMEM);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * Copy sctp_secret from the listener in case we need to validate
1227c478bd9Sstevel@tonic-gate 	 * a possibly delayed cookie.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	bcopy(listener->sctp_secret, acceptor->sctp_secret, SCTP_SECRET_LEN);
1257c478bd9Sstevel@tonic-gate 	bcopy(listener->sctp_old_secret, acceptor->sctp_old_secret,
1267c478bd9Sstevel@tonic-gate 	    SCTP_SECRET_LEN);
1277c478bd9Sstevel@tonic-gate 	acceptor->sctp_last_secret_update = lbolt64;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * After acceptor is inserted in the hash list, it can be found.
1317c478bd9Sstevel@tonic-gate 	 * So we need to lock it here.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	RUN_SCTP(acceptor);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	sctp_conn_hash_insert(&sctp_conn_fanout[
1367c478bd9Sstevel@tonic-gate 	    SCTP_CONN_HASH(acceptor->sctp_ports)], acceptor, 0);
1377c478bd9Sstevel@tonic-gate 	sctp_bind_hash_insert(&sctp_bind_fanout[
1387c478bd9Sstevel@tonic-gate 	    SCTP_BIND_HASH(ntohs(acceptor->sctp_lport))], acceptor, 0);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * No need to check for multicast destination since ip will only pass
1427c478bd9Sstevel@tonic-gate 	 * up multicasts to those that have expressed interest
1437c478bd9Sstevel@tonic-gate 	 * TODO: what about rejecting broadcasts?
1447c478bd9Sstevel@tonic-gate 	 * Also check that source is not a multicast or broadcast address.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	/* XXXSCTP */
1477c478bd9Sstevel@tonic-gate 	acceptor->sctp_state = SCTPS_ESTABLISHED;
1487c478bd9Sstevel@tonic-gate 	acceptor->sctp_assoc_start_time = (uint32_t)lbolt;
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * listener->sctp_rwnd should be the default window size or a
1517c478bd9Sstevel@tonic-gate 	 * window size changed via SO_RCVBUF option.
1527c478bd9Sstevel@tonic-gate 	 */
1531d8c4025Svi 	acceptor->sctp_rwnd = listener->sctp_rwnd;
1541d8c4025Svi 	acceptor->sctp_irwnd = acceptor->sctp_rwnd;
1557c478bd9Sstevel@tonic-gate 	bcopy(&listener->sctp_upcalls, &acceptor->sctp_upcalls,
1567c478bd9Sstevel@tonic-gate 	    sizeof (sctp_upcalls_t));
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	return (0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /* Process the COOKIE packet, mp, directed at the listener 'sctp' */
1627c478bd9Sstevel@tonic-gate sctp_t *
1637c478bd9Sstevel@tonic-gate sctp_conn_request(sctp_t *sctp, mblk_t *mp, uint_t ifindex, uint_t ip_hdr_len,
1647c478bd9Sstevel@tonic-gate     sctp_init_chunk_t *iack, mblk_t *ipsec_mp)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	sctp_t	*eager;
1677c478bd9Sstevel@tonic-gate 	uint_t	ipvers;
1687c478bd9Sstevel@tonic-gate 	ip6_t	*ip6h;
1697c478bd9Sstevel@tonic-gate 	int	err;
1707c478bd9Sstevel@tonic-gate 	conn_t	*connp, *econnp;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * No need to check for duplicate as this is the listener
1747c478bd9Sstevel@tonic-gate 	 * and we are holding the lock.  This means that no new
1757c478bd9Sstevel@tonic-gate 	 * connection can be created out of it.  And since the
1767c478bd9Sstevel@tonic-gate 	 * fanout already done cannot find a match, it means that
1777c478bd9Sstevel@tonic-gate 	 * there is no duplicate.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION(mp->b_rptr);
1807c478bd9Sstevel@tonic-gate 	ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION);
1817c478bd9Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_rptr));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if ((eager = sctp_create_eager(sctp)) == NULL) {
1847c478bd9Sstevel@tonic-gate 		return (NULL);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (ipvers != IPV4_VERSION) {
1887c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
189f551bb10Svi 		if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))
190f551bb10Svi 			eager->sctp_linklocal = 1;
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 		 * Record ifindex (might be zero) to tie this connection to
1937c478bd9Sstevel@tonic-gate 		 * that interface if either the listener was bound or
1947c478bd9Sstevel@tonic-gate 		 * if the connection is using link-local addresses.
1957c478bd9Sstevel@tonic-gate 		 */
1967c478bd9Sstevel@tonic-gate 		if (sctp->sctp_bound_if == ifindex ||
1977c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))
1987c478bd9Sstevel@tonic-gate 			eager->sctp_bound_if = ifindex;
1997c478bd9Sstevel@tonic-gate 		/*
2007c478bd9Sstevel@tonic-gate 		 * XXX broken. bound_if is always overwritten by statement
2017c478bd9Sstevel@tonic-gate 		 * below. What is the right thing to do here?
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		eager->sctp_bound_if = sctp->sctp_bound_if;
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	connp = sctp->sctp_connp;
2077c478bd9Sstevel@tonic-gate 	econnp = eager->sctp_connp;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (connp->conn_policy != NULL) {
2107c478bd9Sstevel@tonic-gate 		ipsec_in_t *ii;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp != NULL);
2137c478bd9Sstevel@tonic-gate 		ii = (ipsec_in_t *)(ipsec_mp->b_rptr);
2147c478bd9Sstevel@tonic-gate 		ASSERT(ii->ipsec_in_policy == NULL);
2157c478bd9Sstevel@tonic-gate 		IPPH_REFHOLD(connp->conn_policy);
2167c478bd9Sstevel@tonic-gate 		ii->ipsec_in_policy = connp->conn_policy;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		ipsec_mp->b_datap->db_type = IPSEC_POLICY_SET;
2197c478bd9Sstevel@tonic-gate 		if (!ip_bind_ipsec_policy_set(econnp, ipsec_mp)) {
2207c478bd9Sstevel@tonic-gate 			sctp_close_eager(eager);
2217c478bd9Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpListenDrop);
2227c478bd9Sstevel@tonic-gate 			return (NULL);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (ipsec_mp != NULL) {
2277c478bd9Sstevel@tonic-gate 		/*
2287c478bd9Sstevel@tonic-gate 		 * XXX need to fix the cached policy issue here.
2297c478bd9Sstevel@tonic-gate 		 * We temporarily set the conn_src/conn_rem here so
2307c478bd9Sstevel@tonic-gate 		 * that IPsec can use it for the latched policy
2317c478bd9Sstevel@tonic-gate 		 * selector.  This is obvioursly wrong as SCTP can
2327c478bd9Sstevel@tonic-gate 		 * use different addresses...
2337c478bd9Sstevel@tonic-gate 		 */
2347c478bd9Sstevel@tonic-gate 		if (ipvers == IPV4_VERSION) {
2357c478bd9Sstevel@tonic-gate 			ipha_t	*ipha;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
2387c478bd9Sstevel@tonic-gate 			econnp->conn_src = ipha->ipha_dst;
2397c478bd9Sstevel@tonic-gate 			econnp->conn_rem = ipha->ipha_src;
2407c478bd9Sstevel@tonic-gate 		} else {
2417c478bd9Sstevel@tonic-gate 			econnp->conn_srcv6 = ip6h->ip6_dst;
2427c478bd9Sstevel@tonic-gate 			econnp->conn_remv6 = ip6h->ip6_src;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) {
2467c478bd9Sstevel@tonic-gate 		sctp_close_eager(eager);
2477c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpListenDrop);
2487c478bd9Sstevel@tonic-gate 		return (NULL);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	err = sctp_accept_comm(sctp, eager, mp, ip_hdr_len, iack);
2527c478bd9Sstevel@tonic-gate 	if (err) {
2537c478bd9Sstevel@tonic-gate 		sctp_close_eager(eager);
2547c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpListenDrop);
2557c478bd9Sstevel@tonic-gate 		return (NULL);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2581d8c4025Svi 	/*
2591d8c4025Svi 	 * On a clustered note send this notification to the clustering
2601d8c4025Svi 	 * subsystem.
2611d8c4025Svi 	 */
2621d8c4025Svi 	if (cl_sctp_connect != NULL) {
2631d8c4025Svi 		uchar_t	*slist;
2641d8c4025Svi 		uchar_t	*flist;
2651d8c4025Svi 		size_t	fsize;
2661d8c4025Svi 		size_t	ssize;
2671d8c4025Svi 
2681d8c4025Svi 		fsize = sizeof (in6_addr_t) * eager->sctp_nfaddrs;
2691d8c4025Svi 		ssize = sizeof (in6_addr_t) * eager->sctp_nsaddrs;
2701d8c4025Svi 		slist = kmem_alloc(ssize, KM_NOSLEEP);
2711d8c4025Svi 		flist = kmem_alloc(fsize, KM_NOSLEEP);
2721d8c4025Svi 		if (slist == NULL || flist == NULL) {
2731d8c4025Svi 			if (slist != NULL)
2741d8c4025Svi 				kmem_free(slist, ssize);
2751d8c4025Svi 			if (flist != NULL)
2761d8c4025Svi 				kmem_free(flist, fsize);
2771d8c4025Svi 			sctp_close_eager(eager);
2781d8c4025Svi 			BUMP_MIB(&sctp_mib, sctpListenDrop);
279*77c67f2fSkcpoon 			SCTP_KSTAT(sctp_cl_connect);
2801d8c4025Svi 			return (NULL);
2811d8c4025Svi 		}
2821d8c4025Svi 		/* The clustering module frees these list */
2831d8c4025Svi 		sctp_get_saddr_list(eager, slist, ssize);
2841d8c4025Svi 		sctp_get_faddr_list(eager, flist, fsize);
2851d8c4025Svi 		(*cl_sctp_connect)(eager->sctp_family, slist,
2861d8c4025Svi 		    eager->sctp_nsaddrs, eager->sctp_lport, flist,
2871d8c4025Svi 		    eager->sctp_nfaddrs, eager->sctp_fport, B_FALSE,
2881d8c4025Svi 		    (cl_sctp_handle_t)eager);
2891d8c4025Svi 	}
2901d8c4025Svi 
2917c478bd9Sstevel@tonic-gate 	/* Connection established, so send up the conn_ind */
2927c478bd9Sstevel@tonic-gate 	if ((eager->sctp_ulpd = sctp->sctp_ulp_newconn(sctp->sctp_ulpd,
2937c478bd9Sstevel@tonic-gate 	    eager)) == NULL) {
2947c478bd9Sstevel@tonic-gate 		sctp_close_eager(eager);
2957c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpListenDrop);
2967c478bd9Sstevel@tonic-gate 		return (NULL);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	ASSERT(SCTP_IS_DETACHED(eager));
2997c478bd9Sstevel@tonic-gate 	eager->sctp_detached = B_FALSE;
3007c478bd9Sstevel@tonic-gate 	if (eager->sctp_family == AF_INET) {
3017c478bd9Sstevel@tonic-gate 		eager->sctp_ulp_prop(eager->sctp_ulpd,
3027c478bd9Sstevel@tonic-gate 		    sctp_wroff_xtra + sizeof (sctp_data_hdr_t) +
3037c478bd9Sstevel@tonic-gate 		    sctp->sctp_hdr_len, strmsgsz);
3047c478bd9Sstevel@tonic-gate 	} else {
3057c478bd9Sstevel@tonic-gate 		eager->sctp_ulp_prop(eager->sctp_ulpd,
3067c478bd9Sstevel@tonic-gate 		    sctp_wroff_xtra + sizeof (sctp_data_hdr_t) +
3077c478bd9Sstevel@tonic-gate 		    sctp->sctp_hdr6_len, strmsgsz);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	return (eager);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Connect to a peer - this function inserts the sctp in the
3147c478bd9Sstevel@tonic-gate  * bind and conn fanouts, sends the INIT, and replies to the client
3157c478bd9Sstevel@tonic-gate  * with an OK ack.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate int
3187c478bd9Sstevel@tonic-gate sctp_connect(sctp_t *sctp, const struct sockaddr *dst, uint32_t addrlen)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	sin_t		*sin;
3217c478bd9Sstevel@tonic-gate 	sin6_t		*sin6;
3227c478bd9Sstevel@tonic-gate 	in6_addr_t	dstaddr;
3237c478bd9Sstevel@tonic-gate 	in_port_t	dstport;
3247c478bd9Sstevel@tonic-gate 	mblk_t		*initmp;
3257c478bd9Sstevel@tonic-gate 	sctp_tf_t	*tbf;
3267c478bd9Sstevel@tonic-gate 	sctp_t		*lsctp;
3277c478bd9Sstevel@tonic-gate 	char		buf[INET6_ADDRSTRLEN];
3287c478bd9Sstevel@tonic-gate 	int		sleep = sctp->sctp_cansleep ? KM_SLEEP : KM_NOSLEEP;
3297c478bd9Sstevel@tonic-gate 	int 		hdrlen;
3307c478bd9Sstevel@tonic-gate 	ip6_rthdr_t	*rth;
33145916cd2Sjpk 	int		err;
3327c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*cur_fp;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * Determine packet type based on type of address passed in
3367c478bd9Sstevel@tonic-gate 	 * the request should contain an IPv4 or IPv6 address.
3377c478bd9Sstevel@tonic-gate 	 * Make sure that address family matches the type of
3387c478bd9Sstevel@tonic-gate 	 * family of the the address passed down
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	if (addrlen < sizeof (sin_t)) {
3417c478bd9Sstevel@tonic-gate 		return (EINVAL);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	switch (dst->sa_family) {
3447c478bd9Sstevel@tonic-gate 	case AF_INET:
3457c478bd9Sstevel@tonic-gate 		sin = (sin_t *)dst;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		/* Check for attempt to connect to non-unicast */
3487c478bd9Sstevel@tonic-gate 		if (IN_MULTICAST(sin->sin_addr.s_addr) ||
3497c478bd9Sstevel@tonic-gate 		    (sin->sin_addr.s_addr == INADDR_BROADCAST)) {
3507c478bd9Sstevel@tonic-gate 			ip0dbg(("sctp_connect: non-unicast\n"));
3517c478bd9Sstevel@tonic-gate 			return (EINVAL);
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only)
3547c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		/* convert to v6 mapped */
3577c478bd9Sstevel@tonic-gate 		/* Check for attempt to connect to INADDR_ANY */
3587c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr == INADDR_ANY)  {
3597c478bd9Sstevel@tonic-gate 			struct in_addr v4_addr;
3607c478bd9Sstevel@tonic-gate 			/*
3617c478bd9Sstevel@tonic-gate 			 * SunOS 4.x and 4.3 BSD allow an application
3627c478bd9Sstevel@tonic-gate 			 * to connect a TCP socket to INADDR_ANY.
3637c478bd9Sstevel@tonic-gate 			 * When they do this, the kernel picks the
3647c478bd9Sstevel@tonic-gate 			 * address of one interface and uses it
3657c478bd9Sstevel@tonic-gate 			 * instead.  The kernel usually ends up
3667c478bd9Sstevel@tonic-gate 			 * picking the address of the loopback
3677c478bd9Sstevel@tonic-gate 			 * interface.  This is an undocumented feature.
3687c478bd9Sstevel@tonic-gate 			 * However, we provide the same thing here
3697c478bd9Sstevel@tonic-gate 			 * in case any TCP apps that use this feature
3707c478bd9Sstevel@tonic-gate 			 * are being ported to SCTP...
3717c478bd9Sstevel@tonic-gate 			 */
3727c478bd9Sstevel@tonic-gate 			v4_addr.s_addr = htonl(INADDR_LOOPBACK);
3737c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&v4_addr, &dstaddr);
3747c478bd9Sstevel@tonic-gate 		} else {
3757c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &dstaddr);
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 		dstport = sin->sin_port;
3787c478bd9Sstevel@tonic-gate 		if (sin->sin_family == AF_INET) {
3797c478bd9Sstevel@tonic-gate 			hdrlen = sctp->sctp_hdr_len;
3807c478bd9Sstevel@tonic-gate 		} else {
3817c478bd9Sstevel@tonic-gate 			hdrlen = sctp->sctp_hdr6_len;
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 		break;
3847c478bd9Sstevel@tonic-gate 	case AF_INET6:
3857c478bd9Sstevel@tonic-gate 		sin6 = (sin6_t *)dst;
3867c478bd9Sstevel@tonic-gate 		/* Check for attempt to connect to non-unicast. */
3877c478bd9Sstevel@tonic-gate 		if ((addrlen < sizeof (sin6_t)) ||
3887c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
3897c478bd9Sstevel@tonic-gate 			ip0dbg(("sctp_connect: non-unicast\n"));
3907c478bd9Sstevel@tonic-gate 			return (EINVAL);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only &&
3937c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3947c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 		/* check for attempt to connect to unspec */
3977c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3987c478bd9Sstevel@tonic-gate 			dstaddr = ipv6_loopback;
3997c478bd9Sstevel@tonic-gate 		} else {
4007c478bd9Sstevel@tonic-gate 			dstaddr = sin6->sin6_addr;
401f551bb10Svi 			if (IN6_IS_ADDR_LINKLOCAL(&dstaddr))
402f551bb10Svi 				sctp->sctp_linklocal = 1;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 		dstport = sin6->sin6_port;
4057c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
4067c478bd9Sstevel@tonic-gate 		break;
4077c478bd9Sstevel@tonic-gate 	default:
4087c478bd9Sstevel@tonic-gate 		dprint(1, ("sctp_connect: unknown family %d\n",
4097c478bd9Sstevel@tonic-gate 			dst->sa_family));
4107c478bd9Sstevel@tonic-gate 		return (EAFNOSUPPORT);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, &dstaddr, buf, sizeof (buf));
4147c478bd9Sstevel@tonic-gate 	dprint(1, ("sctp_connect: attempting connect to %s...\n", buf));
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (sctp->sctp_family != dst->sa_family) {
4197c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4207c478bd9Sstevel@tonic-gate 		return (EINVAL);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_state) {
4247c478bd9Sstevel@tonic-gate 	case SCTPS_IDLE: {
4251d8c4025Svi 		struct sockaddr_storage	ss;
4261d8c4025Svi 
4277c478bd9Sstevel@tonic-gate 		/*
4287c478bd9Sstevel@tonic-gate 		 * We support a quick connect capability here, allowing
4297c478bd9Sstevel@tonic-gate 		 * clients to transition directly from IDLE to COOKIE_WAIT.
4307c478bd9Sstevel@tonic-gate 		 * sctp_bindi will pick an unused port, insert the connection
4317c478bd9Sstevel@tonic-gate 		 * in the bind hash and transition to BOUND state. SCTP
4327c478bd9Sstevel@tonic-gate 		 * picks and uses what it considers the optimal local address
4337c478bd9Sstevel@tonic-gate 		 * set (just like specifiying INADDR_ANY to bind()).
4347c478bd9Sstevel@tonic-gate 		 */
4357c478bd9Sstevel@tonic-gate 		dprint(1, ("sctp_connect: idle, attempting bind...\n"));
4367c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
4377c478bd9Sstevel@tonic-gate 
4381d8c4025Svi 		bzero(&ss, sizeof (ss));
4391d8c4025Svi 		ss.ss_family = sctp->sctp_family;
4401d8c4025Svi 		WAKE_SCTP(sctp);
4411d8c4025Svi 		if ((err = sctp_bind(sctp, (struct sockaddr *)&ss,
4421d8c4025Svi 		    sizeof (ss))) != 0) {
4437c478bd9Sstevel@tonic-gate 			return (err);
4447c478bd9Sstevel@tonic-gate 		}
4451d8c4025Svi 		RUN_SCTP(sctp);
4467c478bd9Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4477c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	case SCTPS_BOUND:
4517c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs > 0);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		/* do the connect */
4547c478bd9Sstevel@tonic-gate 		/* XXX check for attempt to connect to self */
4557c478bd9Sstevel@tonic-gate 		sctp->sctp_fport = dstport;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc);
4587c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc6);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * Don't allow this connection to completely duplicate
4627c478bd9Sstevel@tonic-gate 		 * an existing connection.
4637c478bd9Sstevel@tonic-gate 		 *
4647c478bd9Sstevel@tonic-gate 		 * Ensure that the duplicate check and insertion is atomic.
4657c478bd9Sstevel@tonic-gate 		 */
4667c478bd9Sstevel@tonic-gate 		sctp_conn_hash_remove(sctp);
4677c478bd9Sstevel@tonic-gate 		tbf = &sctp_conn_fanout[SCTP_CONN_HASH(sctp->sctp_ports)];
4687c478bd9Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
4697c478bd9Sstevel@tonic-gate 		lsctp = sctp_lookup(sctp, &dstaddr, tbf, &sctp->sctp_ports,
4707c478bd9Sstevel@tonic-gate 		    SCTPS_COOKIE_WAIT);
4717c478bd9Sstevel@tonic-gate 		if (lsctp != NULL) {
4727c478bd9Sstevel@tonic-gate 			/* found a duplicate connection */
4737c478bd9Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4747c478bd9Sstevel@tonic-gate 			SCTP_REFRELE(lsctp);
4757c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4767c478bd9Sstevel@tonic-gate 			return (EADDRINUSE);
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		/*
4797c478bd9Sstevel@tonic-gate 		 * OK; set up the peer addr (this may grow after we get
4807c478bd9Sstevel@tonic-gate 		 * the INIT ACK from the peer with additional addresses).
4817c478bd9Sstevel@tonic-gate 		 */
482*77c67f2fSkcpoon 		if ((err = sctp_add_faddr(sctp, &dstaddr, sleep,
483*77c67f2fSkcpoon 		    B_FALSE)) != 0) {
4847c478bd9Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4857c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
48645916cd2Sjpk 			return (err);
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 		/* No valid src addr, return. */
4897c478bd9Sstevel@tonic-gate 		if (sctp->sctp_faddrs->state == SCTP_FADDRS_UNREACH) {
4907c478bd9Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4917c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4927c478bd9Sstevel@tonic-gate 			return (EADDRNOTAVAIL);
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 		sctp->sctp_primary = sctp->sctp_faddrs;
4957c478bd9Sstevel@tonic-gate 		sctp->sctp_current = sctp->sctp_faddrs;
4967c478bd9Sstevel@tonic-gate 		cur_fp = sctp->sctp_current;
4977c478bd9Sstevel@tonic-gate 		sctp->sctp_mss = sctp->sctp_faddrs->sfa_pmss;
4987c478bd9Sstevel@tonic-gate 		sctp_conn_hash_insert(tbf, sctp, 1);
4997c478bd9Sstevel@tonic-gate 		mutex_exit(&tbf->tf_lock);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/* initialize composite headers */
502*77c67f2fSkcpoon 		if ((err = sctp_set_hdraddrs(sctp)) != 0) {
50345916cd2Sjpk 			sctp_conn_hash_remove(sctp);
50445916cd2Sjpk 			WAKE_SCTP(sctp);
50545916cd2Sjpk 			return (err);
50645916cd2Sjpk 		}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * Massage a routing header (if present) putting the first hop
5107c478bd9Sstevel@tonic-gate 		 * in ip6_dst.
5117c478bd9Sstevel@tonic-gate 		 */
5127c478bd9Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
5137c478bd9Sstevel@tonic-gate 		    (uint8_t *)sctp->sctp_sctph6);
5147c478bd9Sstevel@tonic-gate 		if (rth != NULL)
5157c478bd9Sstevel@tonic-gate 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		/*
5187c478bd9Sstevel@tonic-gate 		 * Turn off the don't fragment bit on the (only) faddr,
5197c478bd9Sstevel@tonic-gate 		 * so that if one of the messages exchanged during the
5207c478bd9Sstevel@tonic-gate 		 * initialization sequence exceeds the path mtu, it
5217c478bd9Sstevel@tonic-gate 		 * at least has a chance to get there. SCTP does no
5227c478bd9Sstevel@tonic-gate 		 * fragmentation of initialization messages.  The DF bit
5237c478bd9Sstevel@tonic-gate 		 * will be turned on again in sctp_send_cookie_echo()
5247c478bd9Sstevel@tonic-gate 		 * (but the cookie echo will still be sent with the df bit
5257c478bd9Sstevel@tonic-gate 		 * off).
5267c478bd9Sstevel@tonic-gate 		 */
5277c478bd9Sstevel@tonic-gate 		cur_fp->df = B_FALSE;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		/* Mark this address as alive */
5307c478bd9Sstevel@tonic-gate 		cur_fp->state = SCTP_FADDRS_ALIVE;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		/* This sctp_t is fully bound now. */
5337c478bd9Sstevel@tonic-gate 		sctp->sctp_connp->conn_fully_bound = B_TRUE;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		/* Send the INIT to the peer */
5367c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, cur_fp, cur_fp->rto);
537f551bb10Svi 		/*
538f551bb10Svi 		 * sctp_init_mp() could result in modifying the source
539f551bb10Svi 		 * address list, so take the hash lock.
540f551bb10Svi 		 */
541f551bb10Svi 		mutex_enter(&tbf->tf_lock);
5427c478bd9Sstevel@tonic-gate 		initmp = sctp_init_mp(sctp);
5437c478bd9Sstevel@tonic-gate 		if (initmp == NULL) {
544f551bb10Svi 			mutex_exit(&tbf->tf_lock);
5457c478bd9Sstevel@tonic-gate 			WAKE_SCTP(sctp);
5467c478bd9Sstevel@tonic-gate 			/* let timer retry */
5477c478bd9Sstevel@tonic-gate 			return (0);
5487c478bd9Sstevel@tonic-gate 		}
549f551bb10Svi 		mutex_exit(&tbf->tf_lock);
5507c478bd9Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_COOKIE_WAIT;
5511d8c4025Svi 		/*
5521d8c4025Svi 		 * On a clustered note send this notification to the clustering
5531d8c4025Svi 		 * subsystem.
5541d8c4025Svi 		 */
5551d8c4025Svi 		if (cl_sctp_connect != NULL) {
5561d8c4025Svi 			uchar_t		*slist;
5571d8c4025Svi 			uchar_t		*flist;
5581d8c4025Svi 			size_t		ssize;
5591d8c4025Svi 			size_t		fsize;
5601d8c4025Svi 
5611d8c4025Svi 			fsize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
5621d8c4025Svi 			ssize = sizeof (in6_addr_t) * sctp->sctp_nsaddrs;
5631d8c4025Svi 			slist = kmem_alloc(ssize, KM_SLEEP);
5641d8c4025Svi 			flist = kmem_alloc(fsize, KM_SLEEP);
5651d8c4025Svi 			/* The clustering module frees the lists */
5661d8c4025Svi 			sctp_get_saddr_list(sctp, slist, ssize);
5671d8c4025Svi 			sctp_get_faddr_list(sctp, flist, fsize);
5681d8c4025Svi 			(*cl_sctp_connect)(sctp->sctp_family, slist,
5691d8c4025Svi 			    sctp->sctp_nsaddrs, sctp->sctp_lport,
5701d8c4025Svi 			    flist, sctp->sctp_nfaddrs, sctp->sctp_fport,
5711d8c4025Svi 			    B_TRUE, (cl_sctp_handle_t)sctp);
5721d8c4025Svi 		}
5737c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
5747c478bd9Sstevel@tonic-gate 		/* OK to call IP_PUT() here instead of sctp_add_sendq(). */
5757c478bd9Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
5767c478bd9Sstevel@tonic-gate 		initmp->b_flag |= MSGHASREF;
5777c478bd9Sstevel@tonic-gate 		IP_PUT(initmp, sctp->sctp_connp, sctp->sctp_current->isv4);
5787c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		sctp->sctp_ulp_prop(sctp->sctp_ulpd,
5817c478bd9Sstevel@tonic-gate 		    sctp_wroff_xtra + hdrlen + sizeof (sctp_data_hdr_t), 0);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		return (0);
5847c478bd9Sstevel@tonic-gate 	default:
5857c478bd9Sstevel@tonic-gate 		ip0dbg(("sctp_connect: invalid state. %d\n", sctp->sctp_state));
5867c478bd9Sstevel@tonic-gate 		WAKE_SCTP(sctp);
5877c478bd9Sstevel@tonic-gate 		return (EINVAL);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate }
590