10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211735Skcpoon 
220Sstevel@tonic-gate /*
23*8778SErik.Nordmark@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/stream.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
330Sstevel@tonic-gate #include <sys/tihdr.h>
340Sstevel@tonic-gate #include <sys/stropts.h>
350Sstevel@tonic-gate #include <sys/strsubr.h>
360Sstevel@tonic-gate #include <sys/socket.h>
371676Sjpk #include <sys/tsol/tndb.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <netinet/in.h>
400Sstevel@tonic-gate #include <netinet/ip6.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <inet/common.h>
430Sstevel@tonic-gate #include <inet/ip.h>
440Sstevel@tonic-gate #include <inet/ip6.h>
450Sstevel@tonic-gate #include <inet/ipclassifier.h>
460Sstevel@tonic-gate #include <inet/ipsec_impl.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include "sctp_impl.h"
490Sstevel@tonic-gate #include "sctp_addr.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Common accept code.  Called by sctp_conn_request.
530Sstevel@tonic-gate  * cr_pkt is the INIT / INIT ACK packet.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate static int
560Sstevel@tonic-gate sctp_accept_comm(sctp_t *listener, sctp_t *acceptor, mblk_t *cr_pkt,
570Sstevel@tonic-gate     uint_t ip_hdr_len, sctp_init_chunk_t *iack)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	sctp_hdr_t		*sctph;
610Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ich;
620Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
630Sstevel@tonic-gate 	int			err;
640Sstevel@tonic-gate 	uint_t			sctp_options;
652776Skp158701 	conn_t			*aconnp;
661676Sjpk 	conn_t			*lconnp;
671676Sjpk 	cred_t			*cr;
683448Sdh155122 	sctp_stack_t	*sctps = listener->sctp_sctps;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	sctph = (sctp_hdr_t *)(cr_pkt->b_rptr + ip_hdr_len);
710Sstevel@tonic-gate 	ASSERT(OK_32PTR(sctph));
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	acceptor->sctp_lport = listener->sctp_lport;
740Sstevel@tonic-gate 	acceptor->sctp_fport = sctph->sh_sport;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	ich = (sctp_chunk_hdr_t *)(iack + 1);
770Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(ich + 1);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/* acceptor isn't in any fanouts yet, so don't need to hold locks */
800Sstevel@tonic-gate 	ASSERT(acceptor->sctp_faddrs == NULL);
810Sstevel@tonic-gate 	err = sctp_get_addrparams(acceptor, listener, cr_pkt, ich,
820Sstevel@tonic-gate 	    &sctp_options);
830Sstevel@tonic-gate 	if (err != 0)
840Sstevel@tonic-gate 		return (err);
850Sstevel@tonic-gate 
862776Skp158701 	aconnp = acceptor->sctp_connp;
871676Sjpk 	lconnp = listener->sctp_connp;
881676Sjpk 	if (lconnp->conn_mlp_type != mlptSingle) {
89*8778SErik.Nordmark@Sun.COM 		cr = aconnp->conn_peercred = msg_getcred(cr_pkt, NULL);
901676Sjpk 		if (cr != NULL)
911676Sjpk 			crhold(cr);
921676Sjpk 	}
931676Sjpk 
941735Skcpoon 	if ((err = sctp_set_hdraddrs(acceptor)) != 0)
951676Sjpk 		return (err);
961676Sjpk 
970Sstevel@tonic-gate 	if ((sctp_options & SCTP_PRSCTP_OPTION) &&
983448Sdh155122 	    listener->sctp_prsctp_aware && sctps->sctps_prsctp_enabled) {
990Sstevel@tonic-gate 		acceptor->sctp_prsctp_aware = B_TRUE;
1000Sstevel@tonic-gate 	} else {
1010Sstevel@tonic-gate 		acceptor->sctp_prsctp_aware = B_FALSE;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	/* The new sctp_t is fully bound now. */
1040Sstevel@tonic-gate 	acceptor->sctp_connp->conn_fully_bound = B_TRUE;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/* Get  initial TSNs */
1070Sstevel@tonic-gate 	acceptor->sctp_ltsn = ntohl(iack->sic_inittsn);
1080Sstevel@tonic-gate 	acceptor->sctp_recovery_tsn = acceptor->sctp_lastack_rxd =
1090Sstevel@tonic-gate 	    acceptor->sctp_ltsn - 1;
1100Sstevel@tonic-gate 	acceptor->sctp_adv_pap = acceptor->sctp_lastack_rxd;
1110Sstevel@tonic-gate 	/* Serial numbers are initialized to the same value as the TSNs */
1120Sstevel@tonic-gate 	acceptor->sctp_lcsn = acceptor->sctp_ltsn;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (!sctp_initialize_params(acceptor, init, iack))
1150Sstevel@tonic-gate 		return (ENOMEM);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * Copy sctp_secret from the listener in case we need to validate
1190Sstevel@tonic-gate 	 * a possibly delayed cookie.
1200Sstevel@tonic-gate 	 */
1210Sstevel@tonic-gate 	bcopy(listener->sctp_secret, acceptor->sctp_secret, SCTP_SECRET_LEN);
1220Sstevel@tonic-gate 	bcopy(listener->sctp_old_secret, acceptor->sctp_old_secret,
1230Sstevel@tonic-gate 	    SCTP_SECRET_LEN);
1240Sstevel@tonic-gate 	acceptor->sctp_last_secret_update = lbolt64;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * After acceptor is inserted in the hash list, it can be found.
1280Sstevel@tonic-gate 	 * So we need to lock it here.
1290Sstevel@tonic-gate 	 */
1300Sstevel@tonic-gate 	RUN_SCTP(acceptor);
1310Sstevel@tonic-gate 
1323448Sdh155122 	sctp_conn_hash_insert(&sctps->sctps_conn_fanout[
1333448Sdh155122 	    SCTP_CONN_HASH(sctps, acceptor->sctp_ports)], acceptor, 0);
1343448Sdh155122 	sctp_bind_hash_insert(&sctps->sctps_bind_fanout[
1350Sstevel@tonic-gate 	    SCTP_BIND_HASH(ntohs(acceptor->sctp_lport))], acceptor, 0);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/*
1380Sstevel@tonic-gate 	 * No need to check for multicast destination since ip will only pass
1390Sstevel@tonic-gate 	 * up multicasts to those that have expressed interest
1400Sstevel@tonic-gate 	 * TODO: what about rejecting broadcasts?
1410Sstevel@tonic-gate 	 * Also check that source is not a multicast or broadcast address.
1420Sstevel@tonic-gate 	 */
1430Sstevel@tonic-gate 	/* XXXSCTP */
1440Sstevel@tonic-gate 	acceptor->sctp_state = SCTPS_ESTABLISHED;
1450Sstevel@tonic-gate 	acceptor->sctp_assoc_start_time = (uint32_t)lbolt;
1460Sstevel@tonic-gate 	/*
1470Sstevel@tonic-gate 	 * listener->sctp_rwnd should be the default window size or a
1480Sstevel@tonic-gate 	 * window size changed via SO_RCVBUF option.
1490Sstevel@tonic-gate 	 */
150852Svi117747 	acceptor->sctp_rwnd = listener->sctp_rwnd;
151852Svi117747 	acceptor->sctp_irwnd = acceptor->sctp_rwnd;
1523845Svi117747 	acceptor->sctp_pd_point = acceptor->sctp_rwnd;
1538348SEric.Yu@Sun.COM 	acceptor->sctp_upcalls = listener->sctp_upcalls;
1548348SEric.Yu@Sun.COM #if 0
1550Sstevel@tonic-gate 	bcopy(&listener->sctp_upcalls, &acceptor->sctp_upcalls,
1560Sstevel@tonic-gate 	    sizeof (sctp_upcalls_t));
1578348SEric.Yu@Sun.COM #endif
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return (0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /* Process the COOKIE packet, mp, directed at the listener 'sctp' */
1630Sstevel@tonic-gate sctp_t *
1640Sstevel@tonic-gate sctp_conn_request(sctp_t *sctp, mblk_t *mp, uint_t ifindex, uint_t ip_hdr_len,
1650Sstevel@tonic-gate     sctp_init_chunk_t *iack, mblk_t *ipsec_mp)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	sctp_t	*eager;
1680Sstevel@tonic-gate 	uint_t	ipvers;
1690Sstevel@tonic-gate 	ip6_t	*ip6h;
1700Sstevel@tonic-gate 	int	err;
1710Sstevel@tonic-gate 	conn_t	*connp, *econnp;
1723448Sdh155122 	sctp_stack_t	*sctps;
1738348SEric.Yu@Sun.COM 	struct sock_proto_props sopp;
174*8778SErik.Nordmark@Sun.COM 	cred_t		*cr;
175*8778SErik.Nordmark@Sun.COM 	pid_t		cpid;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * No need to check for duplicate as this is the listener
1790Sstevel@tonic-gate 	 * and we are holding the lock.  This means that no new
1800Sstevel@tonic-gate 	 * connection can be created out of it.  And since the
1810Sstevel@tonic-gate 	 * fanout already done cannot find a match, it means that
1820Sstevel@tonic-gate 	 * there is no duplicate.
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	ipvers = IPH_HDR_VERSION(mp->b_rptr);
1850Sstevel@tonic-gate 	ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION);
1860Sstevel@tonic-gate 	ASSERT(OK_32PTR(mp->b_rptr));
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if ((eager = sctp_create_eager(sctp)) == NULL) {
1890Sstevel@tonic-gate 		return (NULL);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (ipvers != IPV4_VERSION) {
1930Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
194432Svi117747 		if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))
195432Svi117747 			eager->sctp_linklocal = 1;
1960Sstevel@tonic-gate 		/*
1970Sstevel@tonic-gate 		 * Record ifindex (might be zero) to tie this connection to
1980Sstevel@tonic-gate 		 * that interface if either the listener was bound or
1990Sstevel@tonic-gate 		 * if the connection is using link-local addresses.
2000Sstevel@tonic-gate 		 */
2010Sstevel@tonic-gate 		if (sctp->sctp_bound_if == ifindex ||
2020Sstevel@tonic-gate 		    IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))
2030Sstevel@tonic-gate 			eager->sctp_bound_if = ifindex;
2040Sstevel@tonic-gate 		/*
2050Sstevel@tonic-gate 		 * XXX broken. bound_if is always overwritten by statement
2060Sstevel@tonic-gate 		 * below. What is the right thing to do here?
2070Sstevel@tonic-gate 		 */
2080Sstevel@tonic-gate 		eager->sctp_bound_if = sctp->sctp_bound_if;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	connp = sctp->sctp_connp;
2123448Sdh155122 	sctps = sctp->sctp_sctps;
2130Sstevel@tonic-gate 	econnp = eager->sctp_connp;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (connp->conn_policy != NULL) {
2160Sstevel@tonic-gate 		ipsec_in_t *ii;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		ASSERT(ipsec_mp != NULL);
2190Sstevel@tonic-gate 		ii = (ipsec_in_t *)(ipsec_mp->b_rptr);
2200Sstevel@tonic-gate 		ASSERT(ii->ipsec_in_policy == NULL);
2210Sstevel@tonic-gate 		IPPH_REFHOLD(connp->conn_policy);
2220Sstevel@tonic-gate 		ii->ipsec_in_policy = connp->conn_policy;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		ipsec_mp->b_datap->db_type = IPSEC_POLICY_SET;
2250Sstevel@tonic-gate 		if (!ip_bind_ipsec_policy_set(econnp, ipsec_mp)) {
2260Sstevel@tonic-gate 			sctp_close_eager(eager);
2273448Sdh155122 			BUMP_MIB(&sctps->sctps_mib, sctpListenDrop);
2280Sstevel@tonic-gate 			return (NULL);
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (ipsec_mp != NULL) {
2330Sstevel@tonic-gate 		/*
2340Sstevel@tonic-gate 		 * XXX need to fix the cached policy issue here.
2350Sstevel@tonic-gate 		 * We temporarily set the conn_src/conn_rem here so
2360Sstevel@tonic-gate 		 * that IPsec can use it for the latched policy
2370Sstevel@tonic-gate 		 * selector.  This is obvioursly wrong as SCTP can
2380Sstevel@tonic-gate 		 * use different addresses...
2390Sstevel@tonic-gate 		 */
2400Sstevel@tonic-gate 		if (ipvers == IPV4_VERSION) {
2410Sstevel@tonic-gate 			ipha_t	*ipha;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
2440Sstevel@tonic-gate 			econnp->conn_src = ipha->ipha_dst;
2450Sstevel@tonic-gate 			econnp->conn_rem = ipha->ipha_src;
2460Sstevel@tonic-gate 		} else {
2470Sstevel@tonic-gate 			econnp->conn_srcv6 = ip6h->ip6_dst;
2480Sstevel@tonic-gate 			econnp->conn_remv6 = ip6h->ip6_src;
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) {
2520Sstevel@tonic-gate 		sctp_close_eager(eager);
2533448Sdh155122 		BUMP_MIB(&sctps->sctps_mib, sctpListenDrop);
2540Sstevel@tonic-gate 		return (NULL);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
257*8778SErik.Nordmark@Sun.COM 	/* Save for getpeerucred */
258*8778SErik.Nordmark@Sun.COM 	cr = msg_getcred(mp, &cpid);
259*8778SErik.Nordmark@Sun.COM 
2600Sstevel@tonic-gate 	err = sctp_accept_comm(sctp, eager, mp, ip_hdr_len, iack);
2610Sstevel@tonic-gate 	if (err) {
2620Sstevel@tonic-gate 		sctp_close_eager(eager);
2633448Sdh155122 		BUMP_MIB(&sctps->sctps_mib, sctpListenDrop);
2640Sstevel@tonic-gate 		return (NULL);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
267852Svi117747 	/*
268852Svi117747 	 * On a clustered note send this notification to the clustering
269852Svi117747 	 * subsystem.
270852Svi117747 	 */
271852Svi117747 	if (cl_sctp_connect != NULL) {
272852Svi117747 		uchar_t	*slist;
273852Svi117747 		uchar_t	*flist;
274852Svi117747 		size_t	fsize;
275852Svi117747 		size_t	ssize;
276852Svi117747 
277852Svi117747 		fsize = sizeof (in6_addr_t) * eager->sctp_nfaddrs;
278852Svi117747 		ssize = sizeof (in6_addr_t) * eager->sctp_nsaddrs;
279852Svi117747 		slist = kmem_alloc(ssize, KM_NOSLEEP);
280852Svi117747 		flist = kmem_alloc(fsize, KM_NOSLEEP);
281852Svi117747 		if (slist == NULL || flist == NULL) {
282852Svi117747 			if (slist != NULL)
283852Svi117747 				kmem_free(slist, ssize);
284852Svi117747 			if (flist != NULL)
285852Svi117747 				kmem_free(flist, fsize);
286852Svi117747 			sctp_close_eager(eager);
2873448Sdh155122 			BUMP_MIB(&sctps->sctps_mib, sctpListenDrop);
2883448Sdh155122 			SCTP_KSTAT(sctps, sctp_cl_connect);
289852Svi117747 			return (NULL);
290852Svi117747 		}
291852Svi117747 		/* The clustering module frees these list */
292852Svi117747 		sctp_get_saddr_list(eager, slist, ssize);
293852Svi117747 		sctp_get_faddr_list(eager, flist, fsize);
294852Svi117747 		(*cl_sctp_connect)(eager->sctp_family, slist,
295852Svi117747 		    eager->sctp_nsaddrs, eager->sctp_lport, flist,
296852Svi117747 		    eager->sctp_nfaddrs, eager->sctp_fport, B_FALSE,
297852Svi117747 		    (cl_sctp_handle_t)eager);
298852Svi117747 	}
299852Svi117747 
3000Sstevel@tonic-gate 	/* Connection established, so send up the conn_ind */
3010Sstevel@tonic-gate 	if ((eager->sctp_ulpd = sctp->sctp_ulp_newconn(sctp->sctp_ulpd,
302*8778SErik.Nordmark@Sun.COM 	    (sock_lower_handle_t)eager, NULL, cr, cpid,
3038348SEric.Yu@Sun.COM 	    &eager->sctp_upcalls)) == NULL) {
3040Sstevel@tonic-gate 		sctp_close_eager(eager);
3053448Sdh155122 		BUMP_MIB(&sctps->sctps_mib, sctpListenDrop);
3060Sstevel@tonic-gate 		return (NULL);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	ASSERT(SCTP_IS_DETACHED(eager));
3090Sstevel@tonic-gate 	eager->sctp_detached = B_FALSE;
3108348SEric.Yu@Sun.COM 	bzero(&sopp, sizeof (sopp));
3118348SEric.Yu@Sun.COM 	sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF;
3128348SEric.Yu@Sun.COM 	sopp.sopp_maxblk = strmsgsz;
3130Sstevel@tonic-gate 	if (eager->sctp_family == AF_INET) {
3148348SEric.Yu@Sun.COM 		sopp.sopp_wroff = sctps->sctps_wroff_xtra +
3158348SEric.Yu@Sun.COM 		    sizeof (sctp_data_hdr_t) + sctp->sctp_hdr_len;
3160Sstevel@tonic-gate 	} else {
3178348SEric.Yu@Sun.COM 		sopp.sopp_wroff = sctps->sctps_wroff_xtra +
3188348SEric.Yu@Sun.COM 		    sizeof (sctp_data_hdr_t) + sctp->sctp_hdr6_len;
3190Sstevel@tonic-gate 	}
3208348SEric.Yu@Sun.COM 	eager->sctp_ulp_prop(eager->sctp_ulpd, &sopp);
3210Sstevel@tonic-gate 	return (eager);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * Connect to a peer - this function inserts the sctp in the
3260Sstevel@tonic-gate  * bind and conn fanouts, sends the INIT, and replies to the client
3270Sstevel@tonic-gate  * with an OK ack.
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate int
3300Sstevel@tonic-gate sctp_connect(sctp_t *sctp, const struct sockaddr *dst, uint32_t addrlen)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	sin_t		*sin;
3330Sstevel@tonic-gate 	sin6_t		*sin6;
3340Sstevel@tonic-gate 	in6_addr_t	dstaddr;
3350Sstevel@tonic-gate 	in_port_t	dstport;
3360Sstevel@tonic-gate 	mblk_t		*initmp;
3370Sstevel@tonic-gate 	sctp_tf_t	*tbf;
3380Sstevel@tonic-gate 	sctp_t		*lsctp;
3390Sstevel@tonic-gate 	char		buf[INET6_ADDRSTRLEN];
3400Sstevel@tonic-gate 	int		sleep = sctp->sctp_cansleep ? KM_SLEEP : KM_NOSLEEP;
3410Sstevel@tonic-gate 	int 		hdrlen;
3420Sstevel@tonic-gate 	ip6_rthdr_t	*rth;
3431676Sjpk 	int		err;
3440Sstevel@tonic-gate 	sctp_faddr_t	*cur_fp;
3453448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
3468348SEric.Yu@Sun.COM 	struct sock_proto_props sopp;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * Determine packet type based on type of address passed in
3500Sstevel@tonic-gate 	 * the request should contain an IPv4 or IPv6 address.
3510Sstevel@tonic-gate 	 * Make sure that address family matches the type of
3520Sstevel@tonic-gate 	 * family of the the address passed down
3530Sstevel@tonic-gate 	 */
3540Sstevel@tonic-gate 	if (addrlen < sizeof (sin_t)) {
3550Sstevel@tonic-gate 		return (EINVAL);
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 	switch (dst->sa_family) {
3580Sstevel@tonic-gate 	case AF_INET:
3590Sstevel@tonic-gate 		sin = (sin_t *)dst;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 		/* Check for attempt to connect to non-unicast */
3625215Skcpoon 		if (CLASSD(sin->sin_addr.s_addr) ||
3630Sstevel@tonic-gate 		    (sin->sin_addr.s_addr == INADDR_BROADCAST)) {
3640Sstevel@tonic-gate 			ip0dbg(("sctp_connect: non-unicast\n"));
3650Sstevel@tonic-gate 			return (EINVAL);
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only)
3680Sstevel@tonic-gate 			return (EAFNOSUPPORT);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		/* convert to v6 mapped */
3710Sstevel@tonic-gate 		/* Check for attempt to connect to INADDR_ANY */
3720Sstevel@tonic-gate 		if (sin->sin_addr.s_addr == INADDR_ANY)  {
3730Sstevel@tonic-gate 			struct in_addr v4_addr;
3740Sstevel@tonic-gate 			/*
3750Sstevel@tonic-gate 			 * SunOS 4.x and 4.3 BSD allow an application
3760Sstevel@tonic-gate 			 * to connect a TCP socket to INADDR_ANY.
3770Sstevel@tonic-gate 			 * When they do this, the kernel picks the
3780Sstevel@tonic-gate 			 * address of one interface and uses it
3790Sstevel@tonic-gate 			 * instead.  The kernel usually ends up
3800Sstevel@tonic-gate 			 * picking the address of the loopback
3810Sstevel@tonic-gate 			 * interface.  This is an undocumented feature.
3820Sstevel@tonic-gate 			 * However, we provide the same thing here
3830Sstevel@tonic-gate 			 * in case any TCP apps that use this feature
3840Sstevel@tonic-gate 			 * are being ported to SCTP...
3850Sstevel@tonic-gate 			 */
3860Sstevel@tonic-gate 			v4_addr.s_addr = htonl(INADDR_LOOPBACK);
3870Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&v4_addr, &dstaddr);
3880Sstevel@tonic-gate 		} else {
3890Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &dstaddr);
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 		dstport = sin->sin_port;
3920Sstevel@tonic-gate 		if (sin->sin_family == AF_INET) {
3930Sstevel@tonic-gate 			hdrlen = sctp->sctp_hdr_len;
3940Sstevel@tonic-gate 		} else {
3950Sstevel@tonic-gate 			hdrlen = sctp->sctp_hdr6_len;
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 		break;
3980Sstevel@tonic-gate 	case AF_INET6:
3990Sstevel@tonic-gate 		sin6 = (sin6_t *)dst;
4000Sstevel@tonic-gate 		/* Check for attempt to connect to non-unicast. */
4010Sstevel@tonic-gate 		if ((addrlen < sizeof (sin6_t)) ||
4020Sstevel@tonic-gate 		    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4030Sstevel@tonic-gate 			ip0dbg(("sctp_connect: non-unicast\n"));
4040Sstevel@tonic-gate 			return (EINVAL);
4050Sstevel@tonic-gate 		}
4060Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only &&
4070Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4080Sstevel@tonic-gate 			return (EAFNOSUPPORT);
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 		/* check for attempt to connect to unspec */
4110Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4120Sstevel@tonic-gate 			dstaddr = ipv6_loopback;
4130Sstevel@tonic-gate 		} else {
4140Sstevel@tonic-gate 			dstaddr = sin6->sin6_addr;
415432Svi117747 			if (IN6_IS_ADDR_LINKLOCAL(&dstaddr))
416432Svi117747 				sctp->sctp_linklocal = 1;
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 		dstport = sin6->sin6_port;
4190Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
4200Sstevel@tonic-gate 		break;
4210Sstevel@tonic-gate 	default:
4220Sstevel@tonic-gate 		dprint(1, ("sctp_connect: unknown family %d\n",
4234505Skcpoon 		    dst->sa_family));
4240Sstevel@tonic-gate 		return (EAFNOSUPPORT);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	(void) inet_ntop(AF_INET6, &dstaddr, buf, sizeof (buf));
4280Sstevel@tonic-gate 	dprint(1, ("sctp_connect: attempting connect to %s...\n", buf));
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	RUN_SCTP(sctp);
4310Sstevel@tonic-gate 
4324505Skcpoon 	if (sctp->sctp_family != dst->sa_family ||
4334505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
4340Sstevel@tonic-gate 		WAKE_SCTP(sctp);
4350Sstevel@tonic-gate 		return (EINVAL);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	switch (sctp->sctp_state) {
4390Sstevel@tonic-gate 	case SCTPS_IDLE: {
440852Svi117747 		struct sockaddr_storage	ss;
441852Svi117747 
4420Sstevel@tonic-gate 		/*
4430Sstevel@tonic-gate 		 * We support a quick connect capability here, allowing
4440Sstevel@tonic-gate 		 * clients to transition directly from IDLE to COOKIE_WAIT.
4450Sstevel@tonic-gate 		 * sctp_bindi will pick an unused port, insert the connection
4460Sstevel@tonic-gate 		 * in the bind hash and transition to BOUND state. SCTP
4470Sstevel@tonic-gate 		 * picks and uses what it considers the optimal local address
4480Sstevel@tonic-gate 		 * set (just like specifiying INADDR_ANY to bind()).
4490Sstevel@tonic-gate 		 */
4500Sstevel@tonic-gate 		dprint(1, ("sctp_connect: idle, attempting bind...\n"));
4510Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs == 0);
4520Sstevel@tonic-gate 
453852Svi117747 		bzero(&ss, sizeof (ss));
454852Svi117747 		ss.ss_family = sctp->sctp_family;
455852Svi117747 		WAKE_SCTP(sctp);
456852Svi117747 		if ((err = sctp_bind(sctp, (struct sockaddr *)&ss,
457852Svi117747 		    sizeof (ss))) != 0) {
4580Sstevel@tonic-gate 			return (err);
4590Sstevel@tonic-gate 		}
460852Svi117747 		RUN_SCTP(sctp);
4610Sstevel@tonic-gate 		/* FALLTHRU */
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	case SCTPS_BOUND:
4650Sstevel@tonic-gate 		ASSERT(sctp->sctp_nsaddrs > 0);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		/* do the connect */
4680Sstevel@tonic-gate 		/* XXX check for attempt to connect to self */
4690Sstevel@tonic-gate 		sctp->sctp_fport = dstport;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc);
4720Sstevel@tonic-gate 		ASSERT(sctp->sctp_iphc6);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		/*
4750Sstevel@tonic-gate 		 * Don't allow this connection to completely duplicate
4760Sstevel@tonic-gate 		 * an existing connection.
4770Sstevel@tonic-gate 		 *
4780Sstevel@tonic-gate 		 * Ensure that the duplicate check and insertion is atomic.
4790Sstevel@tonic-gate 		 */
4800Sstevel@tonic-gate 		sctp_conn_hash_remove(sctp);
4813448Sdh155122 		tbf = &sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps,
4824505Skcpoon 		    sctp->sctp_ports)];
4830Sstevel@tonic-gate 		mutex_enter(&tbf->tf_lock);
4840Sstevel@tonic-gate 		lsctp = sctp_lookup(sctp, &dstaddr, tbf, &sctp->sctp_ports,
4850Sstevel@tonic-gate 		    SCTPS_COOKIE_WAIT);
4860Sstevel@tonic-gate 		if (lsctp != NULL) {
4870Sstevel@tonic-gate 			/* found a duplicate connection */
4880Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
4890Sstevel@tonic-gate 			SCTP_REFRELE(lsctp);
4900Sstevel@tonic-gate 			WAKE_SCTP(sctp);
4910Sstevel@tonic-gate 			return (EADDRINUSE);
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 		/*
4940Sstevel@tonic-gate 		 * OK; set up the peer addr (this may grow after we get
4950Sstevel@tonic-gate 		 * the INIT ACK from the peer with additional addresses).
4960Sstevel@tonic-gate 		 */
4971735Skcpoon 		if ((err = sctp_add_faddr(sctp, &dstaddr, sleep,
4981735Skcpoon 		    B_FALSE)) != 0) {
4990Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
5000Sstevel@tonic-gate 			WAKE_SCTP(sctp);
5011676Sjpk 			return (err);
5020Sstevel@tonic-gate 		}
5034818Skcpoon 		cur_fp = sctp->sctp_faddrs;
5044818Skcpoon 
5050Sstevel@tonic-gate 		/* No valid src addr, return. */
5064818Skcpoon 		if (cur_fp->state == SCTP_FADDRS_UNREACH) {
5070Sstevel@tonic-gate 			mutex_exit(&tbf->tf_lock);
5080Sstevel@tonic-gate 			WAKE_SCTP(sctp);
5090Sstevel@tonic-gate 			return (EADDRNOTAVAIL);
5100Sstevel@tonic-gate 		}
5114818Skcpoon 
5124818Skcpoon 		sctp->sctp_primary = cur_fp;
5134818Skcpoon 		sctp->sctp_current = cur_fp;
5144818Skcpoon 		sctp->sctp_mss = cur_fp->sfa_pmss;
5150Sstevel@tonic-gate 		sctp_conn_hash_insert(tbf, sctp, 1);
5160Sstevel@tonic-gate 		mutex_exit(&tbf->tf_lock);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		/* initialize composite headers */
5191735Skcpoon 		if ((err = sctp_set_hdraddrs(sctp)) != 0) {
5201676Sjpk 			sctp_conn_hash_remove(sctp);
5211676Sjpk 			WAKE_SCTP(sctp);
5221676Sjpk 			return (err);
5231676Sjpk 		}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		/*
5260Sstevel@tonic-gate 		 * Massage a routing header (if present) putting the first hop
5270Sstevel@tonic-gate 		 * in ip6_dst.
5280Sstevel@tonic-gate 		 */
5290Sstevel@tonic-gate 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
5300Sstevel@tonic-gate 		    (uint8_t *)sctp->sctp_sctph6);
5313448Sdh155122 		if (rth != NULL) {
5323448Sdh155122 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth,
5333448Sdh155122 			    sctps->sctps_netstack);
5343448Sdh155122 		}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		/*
5370Sstevel@tonic-gate 		 * Turn off the don't fragment bit on the (only) faddr,
5380Sstevel@tonic-gate 		 * so that if one of the messages exchanged during the
5390Sstevel@tonic-gate 		 * initialization sequence exceeds the path mtu, it
5400Sstevel@tonic-gate 		 * at least has a chance to get there. SCTP does no
5410Sstevel@tonic-gate 		 * fragmentation of initialization messages.  The DF bit
5420Sstevel@tonic-gate 		 * will be turned on again in sctp_send_cookie_echo()
5430Sstevel@tonic-gate 		 * (but the cookie echo will still be sent with the df bit
5440Sstevel@tonic-gate 		 * off).
5450Sstevel@tonic-gate 		 */
5460Sstevel@tonic-gate 		cur_fp->df = B_FALSE;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		/* Mark this address as alive */
5490Sstevel@tonic-gate 		cur_fp->state = SCTP_FADDRS_ALIVE;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/* This sctp_t is fully bound now. */
5520Sstevel@tonic-gate 		sctp->sctp_connp->conn_fully_bound = B_TRUE;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		/* Send the INIT to the peer */
5550Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, cur_fp, cur_fp->rto);
5564818Skcpoon 		sctp->sctp_state = SCTPS_COOKIE_WAIT;
557432Svi117747 		/*
558432Svi117747 		 * sctp_init_mp() could result in modifying the source
559432Svi117747 		 * address list, so take the hash lock.
560432Svi117747 		 */
561432Svi117747 		mutex_enter(&tbf->tf_lock);
5620Sstevel@tonic-gate 		initmp = sctp_init_mp(sctp);
5630Sstevel@tonic-gate 		if (initmp == NULL) {
564432Svi117747 			mutex_exit(&tbf->tf_lock);
5654818Skcpoon 			/*
5664818Skcpoon 			 * It may happen that all the source addresses
5674818Skcpoon 			 * (loopback/link local) are removed.  In that case,
5684818Skcpoon 			 * faile the connect.
5694818Skcpoon 			 */
5704818Skcpoon 			if (sctp->sctp_nsaddrs == 0) {
5714818Skcpoon 				sctp_conn_hash_remove(sctp);
5724818Skcpoon 				SCTP_FADDR_TIMER_STOP(cur_fp);
5734818Skcpoon 				WAKE_SCTP(sctp);
5744818Skcpoon 				return (EADDRNOTAVAIL);
5754818Skcpoon 			}
5764818Skcpoon 
5774818Skcpoon 			/* Otherwise, let the retransmission timer retry */
5780Sstevel@tonic-gate 			WAKE_SCTP(sctp);
5794818Skcpoon 			goto notify_ulp;
5800Sstevel@tonic-gate 		}
581432Svi117747 		mutex_exit(&tbf->tf_lock);
5824818Skcpoon 
583852Svi117747 		/*
584852Svi117747 		 * On a clustered note send this notification to the clustering
585852Svi117747 		 * subsystem.
586852Svi117747 		 */
587852Svi117747 		if (cl_sctp_connect != NULL) {
588852Svi117747 			uchar_t		*slist;
589852Svi117747 			uchar_t		*flist;
590852Svi117747 			size_t		ssize;
591852Svi117747 			size_t		fsize;
592852Svi117747 
593852Svi117747 			fsize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
594852Svi117747 			ssize = sizeof (in6_addr_t) * sctp->sctp_nsaddrs;
595852Svi117747 			slist = kmem_alloc(ssize, KM_SLEEP);
596852Svi117747 			flist = kmem_alloc(fsize, KM_SLEEP);
597852Svi117747 			/* The clustering module frees the lists */
598852Svi117747 			sctp_get_saddr_list(sctp, slist, ssize);
599852Svi117747 			sctp_get_faddr_list(sctp, flist, fsize);
600852Svi117747 			(*cl_sctp_connect)(sctp->sctp_family, slist,
601852Svi117747 			    sctp->sctp_nsaddrs, sctp->sctp_lport,
602852Svi117747 			    flist, sctp->sctp_nfaddrs, sctp->sctp_fport,
603852Svi117747 			    B_TRUE, (cl_sctp_handle_t)sctp);
604852Svi117747 		}
6050Sstevel@tonic-gate 		WAKE_SCTP(sctp);
6060Sstevel@tonic-gate 		/* OK to call IP_PUT() here instead of sctp_add_sendq(). */
6070Sstevel@tonic-gate 		CONN_INC_REF(sctp->sctp_connp);
6080Sstevel@tonic-gate 		initmp->b_flag |= MSGHASREF;
6090Sstevel@tonic-gate 		IP_PUT(initmp, sctp->sctp_connp, sctp->sctp_current->isv4);
6100Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_opkts);
6110Sstevel@tonic-gate 
6124818Skcpoon notify_ulp:
6138348SEric.Yu@Sun.COM 		bzero(&sopp, sizeof (sopp));
6148348SEric.Yu@Sun.COM 		sopp.sopp_flags = SOCKOPT_WROFF;
6158348SEric.Yu@Sun.COM 		sopp.sopp_wroff = sctps->sctps_wroff_xtra + hdrlen +
6168348SEric.Yu@Sun.COM 		    sizeof (sctp_data_hdr_t);
6178348SEric.Yu@Sun.COM 		sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		return (0);
6200Sstevel@tonic-gate 	default:
6210Sstevel@tonic-gate 		ip0dbg(("sctp_connect: invalid state. %d\n", sctp->sctp_state));
6220Sstevel@tonic-gate 		WAKE_SCTP(sctp);
6230Sstevel@tonic-gate 		return (EINVAL);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate }
626