10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211735Skcpoon 
220Sstevel@tonic-gate /*
231676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/cmn_err.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
350Sstevel@tonic-gate #include <sys/tihdr.h>
360Sstevel@tonic-gate #include <sys/stropts.h>
370Sstevel@tonic-gate #include <sys/strsubr.h>
380Sstevel@tonic-gate #include <sys/socket.h>
391676Sjpk #include <sys/tsol/tndb.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <netinet/ip6.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <inet/common.h>
450Sstevel@tonic-gate #include <inet/ip.h>
460Sstevel@tonic-gate #include <inet/ip6.h>
470Sstevel@tonic-gate #include <inet/ipclassifier.h>
480Sstevel@tonic-gate #include <inet/ipsec_impl.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "sctp_impl.h"
510Sstevel@tonic-gate #include "sctp_addr.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Common accept code.  Called by sctp_conn_request.
550Sstevel@tonic-gate  * cr_pkt is the INIT / INIT ACK packet.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static int
580Sstevel@tonic-gate sctp_accept_comm(sctp_t *listener, sctp_t *acceptor, mblk_t *cr_pkt,
590Sstevel@tonic-gate     uint_t ip_hdr_len, sctp_init_chunk_t *iack)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	sctp_hdr_t		*sctph;
630Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ich;
640Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
650Sstevel@tonic-gate 	int			err;
660Sstevel@tonic-gate 	uint_t			sctp_options;
67*2776Skp158701 	conn_t			*aconnp;
681676Sjpk 	conn_t			*lconnp;
691676Sjpk 	cred_t			*cr;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(cr_pkt->b_rptr + ip_hdr_len);
720Sstevel@tonic-gate 	ASSERT(OK_32PTR(sctph));
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	acceptor->sctp_lport = listener->sctp_lport;
750Sstevel@tonic-gate 	acceptor->sctp_fport = sctph->sh_sport;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	ich = (sctp_chunk_hdr_t *)(iack + 1);
780Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/* acceptor isn't in any fanouts yet, so don't need to hold locks */
810Sstevel@tonic-gate 	ASSERT(acceptor->sctp_faddrs == NULL);
820Sstevel@tonic-gate 	err = sctp_get_addrparams(acceptor, listener, cr_pkt, ich,
830Sstevel@tonic-gate 	    &sctp_options);
840Sstevel@tonic-gate 	if (err != 0)
850Sstevel@tonic-gate 		return (err);
860Sstevel@tonic-gate 
87*2776Skp158701 	aconnp = acceptor->sctp_connp;
881676Sjpk 	lconnp = listener->sctp_connp;
891676Sjpk 	if (lconnp->conn_mlp_type != mlptSingle) {
90*2776Skp158701 		cr = aconnp->conn_peercred = DB_CRED(cr_pkt);
911676Sjpk 		if (cr != NULL)
921676Sjpk 			crhold(cr);
931676Sjpk 	}
941676Sjpk 
951735Skcpoon 	if ((err = sctp_set_hdraddrs(acceptor)) != 0)
961676Sjpk 		return (err);
971676Sjpk 
980Sstevel@tonic-gate 	if ((sctp_options & SCTP_PRSCTP_OPTION) &&
990Sstevel@tonic-gate 	    listener->sctp_prsctp_aware && sctp_prsctp_enabled) {
1000Sstevel@tonic-gate 		acceptor->sctp_prsctp_aware = B_TRUE;
1010Sstevel@tonic-gate 	} else {
1020Sstevel@tonic-gate 		acceptor->sctp_prsctp_aware = B_FALSE;
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	/* The new sctp_t is fully bound now. */
1050Sstevel@tonic-gate 	acceptor->sctp_connp->conn_fully_bound = B_TRUE;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/* Get  initial TSNs */
1080Sstevel@tonic-gate 	acceptor->sctp_ltsn = ntohl(iack->sic_inittsn);
1090Sstevel@tonic-gate 	acceptor->sctp_recovery_tsn = acceptor->sctp_lastack_rxd =
1100Sstevel@tonic-gate 	    acceptor->sctp_ltsn - 1;
1110Sstevel@tonic-gate 	acceptor->sctp_adv_pap = acceptor->sctp_lastack_rxd;
1120Sstevel@tonic-gate 	/* Serial numbers are initialized to the same value as the TSNs */
1130Sstevel@tonic-gate 	acceptor->sctp_lcsn = acceptor->sctp_ltsn;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (!sctp_initialize_params(acceptor, init, iack))
1160Sstevel@tonic-gate 		return (ENOMEM);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/*
1190Sstevel@tonic-gate 	 * Copy sctp_secret from the listener in case we need to validate
1200Sstevel@tonic-gate 	 * a possibly delayed cookie.
1210Sstevel@tonic-gate 	 */
1220Sstevel@tonic-gate 	bcopy(listener->sctp_secret, acceptor->sctp_secret, SCTP_SECRET_LEN);
1230Sstevel@tonic-gate 	bcopy(listener->sctp_old_secret, acceptor->sctp_old_secret,
1240Sstevel@tonic-gate 	    SCTP_SECRET_LEN);
1250Sstevel@tonic-gate 	acceptor->sctp_last_secret_update = lbolt64;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/*
1280Sstevel@tonic-gate 	 * After acceptor is inserted in the hash list, it can be found.
1290Sstevel@tonic-gate 	 * So we need to lock it here.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	RUN_SCTP(acceptor);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	sctp_conn_hash_insert(&sctp_conn_fanout[
1340Sstevel@tonic-gate 	    SCTP_CONN_HASH(acceptor->sctp_ports)], acceptor, 0);
1350Sstevel@tonic-gate 	sctp_bind_hash_insert(&sctp_bind_fanout[
1360Sstevel@tonic-gate 	    SCTP_BIND_HASH(ntohs(acceptor->sctp_lport))], acceptor, 0);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * No need to check for multicast destination since ip will only pass
1400Sstevel@tonic-gate 	 * up multicasts to those that have expressed interest
1410Sstevel@tonic-gate 	 * TODO: what about rejecting broadcasts?
1420Sstevel@tonic-gate 	 * Also check that source is not a multicast or broadcast address.
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	/* XXXSCTP */
1450Sstevel@tonic-gate 	acceptor->sctp_state = SCTPS_ESTABLISHED;
1460Sstevel@tonic-gate 	acceptor->sctp_assoc_start_time = (uint32_t)lbolt;
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * listener->sctp_rwnd should be the default window size or a
1490Sstevel@tonic-gate 	 * window size changed via SO_RCVBUF option.
1500Sstevel@tonic-gate 	 */
151852Svi117747 	acceptor->sctp_rwnd = listener->sctp_rwnd;
152852Svi117747 	acceptor->sctp_irwnd = acceptor->sctp_rwnd;
1530Sstevel@tonic-gate 	bcopy(&listener->sctp_upcalls, &acceptor->sctp_upcalls,
1540Sstevel@tonic-gate 	    sizeof (sctp_upcalls_t));
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (0);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /* Process the COOKIE packet, mp, directed at the listener 'sctp' */
1600Sstevel@tonic-gate sctp_t *
1610Sstevel@tonic-gate sctp_conn_request(sctp_t *sctp, mblk_t *mp, uint_t ifindex, uint_t ip_hdr_len,
1620Sstevel@tonic-gate     sctp_init_chunk_t *iack, mblk_t *ipsec_mp)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	sctp_t	*eager;
1650Sstevel@tonic-gate 	uint_t	ipvers;
1660Sstevel@tonic-gate 	ip6_t	*ip6h;
1670Sstevel@tonic-gate 	int	err;
1680Sstevel@tonic-gate 	conn_t	*connp, *econnp;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * No need to check for duplicate as this is the listener
1720Sstevel@tonic-gate 	 * and we are holding the lock.  This means that no new
1730Sstevel@tonic-gate 	 * connection can be created out of it.  And since the
1740Sstevel@tonic-gate 	 * fanout already done cannot find a match, it means that
1750Sstevel@tonic-gate 	 * there is no duplicate.
1760Sstevel@tonic-gate 	 */
1770Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION(mp->b_rptr);
1780Sstevel@tonic-gate 	ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION);
1790Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_rptr));
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if ((eager = sctp_create_eager(sctp)) == NULL) {
1820Sstevel@tonic-gate 		return (NULL);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (ipvers != IPV4_VERSION) {
1860Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
187432Svi117747 		if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))
188432Svi117747 			eager->sctp_linklocal = 1;
1890Sstevel@tonic-gate 		/*
1900Sstevel@tonic-gate 		 * Record ifindex (might be zero) to tie this connection to
1910Sstevel@tonic-gate 		 * that interface if either the listener was bound or
1920Sstevel@tonic-gate 		 * if the connection is using link-local addresses.
1930Sstevel@tonic-gate 		 */
1940Sstevel@tonic-gate 		if (sctp->sctp_bound_if == ifindex ||
1950Sstevel@tonic-gate 		    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))
1960Sstevel@tonic-gate 			eager->sctp_bound_if = ifindex;
1970Sstevel@tonic-gate 		/*
1980Sstevel@tonic-gate 		 * XXX broken. bound_if is always overwritten by statement
1990Sstevel@tonic-gate 		 * below. What is the right thing to do here?
2000Sstevel@tonic-gate 		 */
2010Sstevel@tonic-gate 		eager->sctp_bound_if = sctp->sctp_bound_if;
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	connp = sctp->sctp_connp;
2050Sstevel@tonic-gate 	econnp = eager->sctp_connp;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if (connp->conn_policy != NULL) {
2080Sstevel@tonic-gate 		ipsec_in_t *ii;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		ASSERT(ipsec_mp != NULL);
2110Sstevel@tonic-gate 		ii = (ipsec_in_t *)(ipsec_mp->b_rptr);
2120Sstevel@tonic-gate 		ASSERT(ii->ipsec_in_policy == NULL);
2130Sstevel@tonic-gate 		IPPH_REFHOLD(connp->conn_policy);
2140Sstevel@tonic-gate 		ii->ipsec_in_policy = connp->conn_policy;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		ipsec_mp->b_datap->db_type = IPSEC_POLICY_SET;
2170Sstevel@tonic-gate 		if (!ip_bind_ipsec_policy_set(econnp, ipsec_mp)) {
2180Sstevel@tonic-gate 			sctp_close_eager(eager);
2190Sstevel@tonic-gate 			BUMP_MIB(&sctp_mib, sctpListenDrop);
2200Sstevel@tonic-gate 			return (NULL);
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (ipsec_mp != NULL) {
2250Sstevel@tonic-gate 		/*
2260Sstevel@tonic-gate 		 * XXX need to fix the cached policy issue here.
2270Sstevel@tonic-gate 		 * We temporarily set the conn_src/conn_rem here so
2280Sstevel@tonic-gate 		 * that IPsec can use it for the latched policy
2290Sstevel@tonic-gate 		 * selector.  This is obvioursly wrong as SCTP can
2300Sstevel@tonic-gate 		 * use different addresses...
2310Sstevel@tonic-gate 		 */
2320Sstevel@tonic-gate 		if (ipvers == IPV4_VERSION) {
2330Sstevel@tonic-gate 			ipha_t	*ipha;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
2360Sstevel@tonic-gate 			econnp->conn_src = ipha->ipha_dst;
2370Sstevel@tonic-gate 			econnp->conn_rem = ipha->ipha_src;
2380Sstevel@tonic-gate 		} else {
2390Sstevel@tonic-gate 			econnp->conn_srcv6 = ip6h->ip6_dst;
2400Sstevel@tonic-gate 			econnp->conn_remv6 = ip6h->ip6_src;
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 	if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) {
2440Sstevel@tonic-gate 		sctp_close_eager(eager);
2450Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpListenDrop);
2460Sstevel@tonic-gate 		return (NULL);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	err = sctp_accept_comm(sctp, eager, mp, ip_hdr_len, iack);
2500Sstevel@tonic-gate 	if (err) {
2510Sstevel@tonic-gate 		sctp_close_eager(eager);
2520Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpListenDrop);
2530Sstevel@tonic-gate 		return (NULL);
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
256852Svi117747 	/*
257852Svi117747 	 * On a clustered note send this notification to the clustering
258852Svi117747 	 * subsystem.
259852Svi117747 	 */
260852Svi117747 	if (cl_sctp_connect != NULL) {
261852Svi117747 		uchar_t	*slist;
262852Svi117747 		uchar_t	*flist;
263852Svi117747 		size_t	fsize;
264852Svi117747 		size_t	ssize;
265852Svi117747 
266852Svi117747 		fsize = sizeof (in6_addr_t) * eager->sctp_nfaddrs;
267852Svi117747 		ssize = sizeof (in6_addr_t) * eager->sctp_nsaddrs;
268852Svi117747 		slist = kmem_alloc(ssize, KM_NOSLEEP);
269852Svi117747 		flist = kmem_alloc(fsize, KM_NOSLEEP);
270852Svi117747 		if (slist == NULL || flist == NULL) {
271852Svi117747 			if (slist != NULL)
272852Svi117747 				kmem_free(slist, ssize);
273852Svi117747 			if (flist != NULL)
274852Svi117747 				kmem_free(flist, fsize);
275852Svi117747 			sctp_close_eager(eager);
276852Svi117747 			BUMP_MIB(&sctp_mib, sctpListenDrop);
2771735Skcpoon 			SCTP_KSTAT(sctp_cl_connect);
278852Svi117747 			return (NULL);
279852Svi117747 		}
280852Svi117747 		/* The clustering module frees these list */
281852Svi117747 		sctp_get_saddr_list(eager, slist, ssize);
282852Svi117747 		sctp_get_faddr_list(eager, flist, fsize);
283852Svi117747 		(*cl_sctp_connect)(eager->sctp_family, slist,
284852Svi117747 		    eager->sctp_nsaddrs, eager->sctp_lport, flist,
285852Svi117747 		    eager->sctp_nfaddrs, eager->sctp_fport, B_FALSE,
286852Svi117747 		    (cl_sctp_handle_t)eager);
287852Svi117747 	}
288852Svi117747 
2890Sstevel@tonic-gate 	/* Connection established, so send up the conn_ind */
2900Sstevel@tonic-gate 	if ((eager->sctp_ulpd = sctp->sctp_ulp_newconn(sctp->sctp_ulpd,
2910Sstevel@tonic-gate 	    eager)) == NULL) {
2920Sstevel@tonic-gate 		sctp_close_eager(eager);
2930Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpListenDrop);
2940Sstevel@tonic-gate 		return (NULL);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 	ASSERT(SCTP_IS_DETACHED(eager));
2970Sstevel@tonic-gate 	eager->sctp_detached = B_FALSE;
2980Sstevel@tonic-gate 	if (eager->sctp_family == AF_INET) {
2990Sstevel@tonic-gate 		eager->sctp_ulp_prop(eager->sctp_ulpd,
3000Sstevel@tonic-gate 		    sctp_wroff_xtra + sizeof (sctp_data_hdr_t) +
3010Sstevel@tonic-gate 		    sctp->sctp_hdr_len, strmsgsz);
3020Sstevel@tonic-gate 	} else {
3030Sstevel@tonic-gate 		eager->sctp_ulp_prop(eager->sctp_ulpd,
3040Sstevel@tonic-gate 		    sctp_wroff_xtra + sizeof (sctp_data_hdr_t) +
3050Sstevel@tonic-gate 		    sctp->sctp_hdr6_len, strmsgsz);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 	return (eager);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Connect to a peer - this function inserts the sctp in the
3120Sstevel@tonic-gate  * bind and conn fanouts, sends the INIT, and replies to the client
3130Sstevel@tonic-gate  * with an OK ack.
3140Sstevel@tonic-gate  */
3150Sstevel@tonic-gate int
3160Sstevel@tonic-gate sctp_connect(sctp_t *sctp, const struct sockaddr *dst, uint32_t addrlen)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	sin_t		*sin;
3190Sstevel@tonic-gate 	sin6_t		*sin6;
3200Sstevel@tonic-gate 	in6_addr_t	dstaddr;
3210Sstevel@tonic-gate 	in_port_t	dstport;
3220Sstevel@tonic-gate 	mblk_t		*initmp;
3230Sstevel@tonic-gate 	sctp_tf_t	*tbf;
3240Sstevel@tonic-gate 	sctp_t		*lsctp;
3250Sstevel@tonic-gate 	char		buf[INET6_ADDRSTRLEN];
3260Sstevel@tonic-gate 	int		sleep = sctp->sctp_cansleep ? KM_SLEEP : KM_NOSLEEP;
3270Sstevel@tonic-gate 	int 		hdrlen;
3280Sstevel@tonic-gate 	ip6_rthdr_t	*rth;
3291676Sjpk 	int		err;
3300Sstevel@tonic-gate 	sctp_faddr_t	*cur_fp;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * Determine packet type based on type of address passed in
3340Sstevel@tonic-gate 	 * the request should contain an IPv4 or IPv6 address.
3350Sstevel@tonic-gate 	 * Make sure that address family matches the type of
3360Sstevel@tonic-gate 	 * family of the the address passed down
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	if (addrlen < sizeof (sin_t)) {
3390Sstevel@tonic-gate 		return (EINVAL);
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 	switch (dst->sa_family) {
3420Sstevel@tonic-gate 	case AF_INET:
3430Sstevel@tonic-gate 		sin = (sin_t *)dst;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		/* Check for attempt to connect to non-unicast */
3460Sstevel@tonic-gate 		if (IN_MULTICAST(sin->sin_addr.s_addr) ||
3470Sstevel@tonic-gate 		    (sin->sin_addr.s_addr == INADDR_BROADCAST)) {
3480Sstevel@tonic-gate 			ip0dbg(("sctp_connect: non-unicast\n"));
3490Sstevel@tonic-gate 			return (EINVAL);
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only)
3520Sstevel@tonic-gate 			return (EAFNOSUPPORT);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		/* convert to v6 mapped */
3550Sstevel@tonic-gate 		/* Check for attempt to connect to INADDR_ANY */
3560Sstevel@tonic-gate 		if (sin->sin_addr.s_addr == INADDR_ANY)  {
3570Sstevel@tonic-gate 			struct in_addr v4_addr;
3580Sstevel@tonic-gate 			/*
3590Sstevel@tonic-gate 			 * SunOS 4.x and 4.3 BSD allow an application
3600Sstevel@tonic-gate 			 * to connect a TCP socket to INADDR_ANY.
3610Sstevel@tonic-gate 			 * When they do this, the kernel picks the
3620Sstevel@tonic-gate 			 * address of one interface and uses it
3630Sstevel@tonic-gate 			 * instead.  The kernel usually ends up
3640Sstevel@tonic-gate 			 * picking the address of the loopback
3650Sstevel@tonic-gate 			 * interface.  This is an undocumented feature.
3660Sstevel@tonic-gate 			 * However, we provide the same thing here
3670Sstevel@tonic-gate 			 * in case any TCP apps that use this feature
3680Sstevel@tonic-gate 			 * are being ported to SCTP...
3690Sstevel@tonic-gate 			 */
3700Sstevel@tonic-gate 			v4_addr.s_addr = htonl(INADDR_LOOPBACK);
3710Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&v4_addr, &dstaddr);
3720Sstevel@tonic-gate 		} else {
3730Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &dstaddr);
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 		dstport = sin->sin_port;
3760Sstevel@tonic-gate 		if (sin->sin_family == AF_INET) {
3770Sstevel@tonic-gate 			hdrlen = sctp->sctp_hdr_len;
3780Sstevel@tonic-gate 		} else {
3790Sstevel@tonic-gate 			hdrlen = sctp->sctp_hdr6_len;
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 		break;
3820Sstevel@tonic-gate 	case AF_INET6:
3830Sstevel@tonic-gate 		sin6 = (sin6_t *)dst;
3840Sstevel@tonic-gate 		/* Check for attempt to connect to non-unicast. */
3850Sstevel@tonic-gate 		if ((addrlen < sizeof (sin6_t)) ||
3860Sstevel@tonic-gate 		    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
3870Sstevel@tonic-gate 			ip0dbg(("sctp_connect: non-unicast\n"));
3880Sstevel@tonic-gate 			return (EINVAL);
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only &&
3910Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3920Sstevel@tonic-gate 			return (EAFNOSUPPORT);
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 		/* check for attempt to connect to unspec */
3950Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3960Sstevel@tonic-gate 			dstaddr = ipv6_loopback;
3970Sstevel@tonic-gate 		} else {
3980Sstevel@tonic-gate 			dstaddr = sin6->sin6_addr;
399432Svi117747 			if (IN6_IS_ADDR_LINKLOCAL(&dstaddr))
400432Svi117747 				sctp->sctp_linklocal = 1;
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 		dstport = sin6->sin6_port;
4030Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
4040Sstevel@tonic-gate 		break;
4050Sstevel@tonic-gate 	default:
4060Sstevel@tonic-gate 		dprint(1, ("sctp_connect: unknown family %d\n",
4070Sstevel@tonic-gate 			dst->sa_family));
4080Sstevel@tonic-gate 		return (EAFNOSUPPORT);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, &dstaddr, buf, sizeof (buf));
4120Sstevel@tonic-gate 	dprint(1, ("sctp_connect: attempting connect to %s...\n", buf));
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	RUN_SCTP(sctp);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if (sctp->sctp_family != dst->sa_family) {
4170Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4180Sstevel@tonic-gate 		return (EINVAL);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	switch (sctp->sctp_state) {
4220Sstevel@tonic-gate 	case SCTPS_IDLE: {
423852Svi117747 		struct sockaddr_storage	ss;
424852Svi117747 
4250Sstevel@tonic-gate 		/*
4260Sstevel@tonic-gate 		 * We support a quick connect capability here, allowing
4270Sstevel@tonic-gate 		 * clients to transition directly from IDLE to COOKIE_WAIT.
4280Sstevel@tonic-gate 		 * sctp_bindi will pick an unused port, insert the connection
4290Sstevel@tonic-gate 		 * in the bind hash and transition to BOUND state. SCTP
4300Sstevel@tonic-gate 		 * picks and uses what it considers the optimal local address
4310Sstevel@tonic-gate 		 * set (just like specifiying INADDR_ANY to bind()).
4320Sstevel@tonic-gate 		 */
4330Sstevel@tonic-gate 		dprint(1, ("sctp_connect: idle, attempting bind...\n"));
4340Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
4350Sstevel@tonic-gate 
436852Svi117747 		bzero(&ss, sizeof (ss));
437852Svi117747 		ss.ss_family = sctp->sctp_family;
438852Svi117747 		WAKE_SCTP(sctp);
439852Svi117747 		if ((err = sctp_bind(sctp, (struct sockaddr *)&ss,
440852Svi117747 		    sizeof (ss))) != 0) {
4410Sstevel@tonic-gate 			return (err);
4420Sstevel@tonic-gate 		}
443852Svi117747 		RUN_SCTP(sctp);
4440Sstevel@tonic-gate 		sctp->sctp_bound_to_all = 1;
4450Sstevel@tonic-gate 		/* FALLTHRU */
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	case SCTPS_BOUND:
4490Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs > 0);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		/* do the connect */
4520Sstevel@tonic-gate 		/* XXX check for attempt to connect to self */
4530Sstevel@tonic-gate 		sctp->sctp_fport = dstport;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc);
4560Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc6);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		/*
4590Sstevel@tonic-gate 		 * Don't allow this connection to completely duplicate
4600Sstevel@tonic-gate 		 * an existing connection.
4610Sstevel@tonic-gate 		 *
4620Sstevel@tonic-gate 		 * Ensure that the duplicate check and insertion is atomic.
4630Sstevel@tonic-gate 		 */
4640Sstevel@tonic-gate 		sctp_conn_hash_remove(sctp);
4650Sstevel@tonic-gate 		tbf = &sctp_conn_fanout[SCTP_CONN_HASH(sctp->sctp_ports)];
4660Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
4670Sstevel@tonic-gate 		lsctp = sctp_lookup(sctp, &dstaddr, tbf, &sctp->sctp_ports,
4680Sstevel@tonic-gate 		    SCTPS_COOKIE_WAIT);
4690Sstevel@tonic-gate 		if (lsctp != NULL) {
4700Sstevel@tonic-gate 			/* found a duplicate connection */
4710Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4720Sstevel@tonic-gate 			SCTP_REFRELE(lsctp);
4730Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4740Sstevel@tonic-gate 			return (EADDRINUSE);
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 		/*
4770Sstevel@tonic-gate 		 * OK; set up the peer addr (this may grow after we get
4780Sstevel@tonic-gate 		 * the INIT ACK from the peer with additional addresses).
4790Sstevel@tonic-gate 		 */
4801735Skcpoon 		if ((err = sctp_add_faddr(sctp, &dstaddr, sleep,
4811735Skcpoon 		    B_FALSE)) != 0) {
4820Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4830Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4841676Sjpk 			return (err);
4850Sstevel@tonic-gate 		}
4860Sstevel@tonic-gate 		/* No valid src addr, return. */
4870Sstevel@tonic-gate 		if (sctp->sctp_faddrs->state == SCTP_FADDRS_UNREACH) {
4880Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4890Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4900Sstevel@tonic-gate 			return (EADDRNOTAVAIL);
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 		sctp->sctp_primary = sctp->sctp_faddrs;
4930Sstevel@tonic-gate 		sctp->sctp_current = sctp->sctp_faddrs;
4940Sstevel@tonic-gate 		cur_fp = sctp->sctp_current;
4950Sstevel@tonic-gate 		sctp->sctp_mss = sctp->sctp_faddrs->sfa_pmss;
4960Sstevel@tonic-gate 		sctp_conn_hash_insert(tbf, sctp, 1);
4970Sstevel@tonic-gate 		mutex_exit(&tbf->tf_lock);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		/* initialize composite headers */
5001735Skcpoon 		if ((err = sctp_set_hdraddrs(sctp)) != 0) {
5011676Sjpk 			sctp_conn_hash_remove(sctp);
5021676Sjpk 			WAKE_SCTP(sctp);
5031676Sjpk 			return (err);
5041676Sjpk 		}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		/*
5070Sstevel@tonic-gate 		 * Massage a routing header (if present) putting the first hop
5080Sstevel@tonic-gate 		 * in ip6_dst.
5090Sstevel@tonic-gate 		 */
5100Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
5110Sstevel@tonic-gate 		    (uint8_t *)sctp->sctp_sctph6);
5120Sstevel@tonic-gate 		if (rth != NULL)
5130Sstevel@tonic-gate 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		/*
5160Sstevel@tonic-gate 		 * Turn off the don't fragment bit on the (only) faddr,
5170Sstevel@tonic-gate 		 * so that if one of the messages exchanged during the
5180Sstevel@tonic-gate 		 * initialization sequence exceeds the path mtu, it
5190Sstevel@tonic-gate 		 * at least has a chance to get there. SCTP does no
5200Sstevel@tonic-gate 		 * fragmentation of initialization messages.  The DF bit
5210Sstevel@tonic-gate 		 * will be turned on again in sctp_send_cookie_echo()
5220Sstevel@tonic-gate 		 * (but the cookie echo will still be sent with the df bit
5230Sstevel@tonic-gate 		 * off).
5240Sstevel@tonic-gate 		 */
5250Sstevel@tonic-gate 		cur_fp->df = B_FALSE;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 		/* Mark this address as alive */
5280Sstevel@tonic-gate 		cur_fp->state = SCTP_FADDRS_ALIVE;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		/* This sctp_t is fully bound now. */
5310Sstevel@tonic-gate 		sctp->sctp_connp->conn_fully_bound = B_TRUE;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		/* Send the INIT to the peer */
5340Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, cur_fp, cur_fp->rto);
535432Svi117747 		/*
536432Svi117747 		 * sctp_init_mp() could result in modifying the source
537432Svi117747 		 * address list, so take the hash lock.
538432Svi117747 		 */
539432Svi117747 		mutex_enter(&tbf->tf_lock);
5400Sstevel@tonic-gate 		initmp = sctp_init_mp(sctp);
5410Sstevel@tonic-gate 		if (initmp == NULL) {
542432Svi117747 			mutex_exit(&tbf->tf_lock);
5430Sstevel@tonic-gate 			WAKE_SCTP(sctp);
5440Sstevel@tonic-gate 			/* let timer retry */
5450Sstevel@tonic-gate 			return (0);
5460Sstevel@tonic-gate 		}
547432Svi117747 		mutex_exit(&tbf->tf_lock);
5480Sstevel@tonic-gate 		sctp->sctp_state = SCTPS_COOKIE_WAIT;
549852Svi117747 		/*
550852Svi117747 		 * On a clustered note send this notification to the clustering
551852Svi117747 		 * subsystem.
552852Svi117747 		 */
553852Svi117747 		if (cl_sctp_connect != NULL) {
554852Svi117747 			uchar_t		*slist;
555852Svi117747 			uchar_t		*flist;
556852Svi117747 			size_t		ssize;
557852Svi117747 			size_t		fsize;
558852Svi117747 
559852Svi117747 			fsize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
560852Svi117747 			ssize = sizeof (in6_addr_t) * sctp->sctp_nsaddrs;
561852Svi117747 			slist = kmem_alloc(ssize, KM_SLEEP);
562852Svi117747 			flist = kmem_alloc(fsize, KM_SLEEP);
563852Svi117747 			/* The clustering module frees the lists */
564852Svi117747 			sctp_get_saddr_list(sctp, slist, ssize);
565852Svi117747 			sctp_get_faddr_list(sctp, flist, fsize);
566852Svi117747 			(*cl_sctp_connect)(sctp->sctp_family, slist,
567852Svi117747 			    sctp->sctp_nsaddrs, sctp->sctp_lport,
568852Svi117747 			    flist, sctp->sctp_nfaddrs, sctp->sctp_fport,
569852Svi117747 			    B_TRUE, (cl_sctp_handle_t)sctp);
570852Svi117747 		}
5710Sstevel@tonic-gate 		WAKE_SCTP(sctp);
5720Sstevel@tonic-gate 		/* OK to call IP_PUT() here instead of sctp_add_sendq(). */
5730Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
5740Sstevel@tonic-gate 		initmp->b_flag |= MSGHASREF;
5750Sstevel@tonic-gate 		IP_PUT(initmp, sctp->sctp_connp, sctp->sctp_current->isv4);
5760Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		sctp->sctp_ulp_prop(sctp->sctp_ulpd,
5790Sstevel@tonic-gate 		    sctp_wroff_xtra + hdrlen + sizeof (sctp_data_hdr_t), 0);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		return (0);
5820Sstevel@tonic-gate 	default:
5830Sstevel@tonic-gate 		ip0dbg(("sctp_connect: invalid state. %d\n", sctp->sctp_state));
5840Sstevel@tonic-gate 		WAKE_SCTP(sctp);
5850Sstevel@tonic-gate 		return (EINVAL);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate }
588