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
577c67f2fSkcpoon  * Common Development and Distribution License (the "License").
677c67f2fSkcpoon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2177c67f2fSkcpoon 
227c478bd9Sstevel@tonic-gate /*
23*bd670b35SErik Nordmark  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/stream.h>
297c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <netinet/in.h>
337c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <inet/common.h>
367c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
377c478bd9Sstevel@tonic-gate #include <inet/ip.h>
387c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
397c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
407c478bd9Sstevel@tonic-gate #include <inet/nd.h>
417c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
42f4b3ec61Sdh #include <inet/ipclassifier.h>
437c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
447c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
477c478bd9Sstevel@tonic-gate size_t
487c478bd9Sstevel@tonic-gate sctp_supaddr_param_len(sctp_t *sctp)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	return (sizeof (sctp_parm_hdr_t) + sizeof (int32_t));
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate size_t
547c478bd9Sstevel@tonic-gate sctp_supaddr_param(sctp_t *sctp, uchar_t *p)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *sph;
577c478bd9Sstevel@tonic-gate 	uint16_t *addrtype;
58*bd670b35SErik Nordmark 	conn_t		*connp = sctp->sctp_connp;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	sph = (sctp_parm_hdr_t *)p;
617c478bd9Sstevel@tonic-gate 	sph->sph_type = htons(PARM_SUPP_ADDRS);
627c478bd9Sstevel@tonic-gate 	addrtype = (uint16_t *)(sph + 1);
63*bd670b35SErik Nordmark 	switch (connp->conn_family) {
64*bd670b35SErik Nordmark 	case AF_INET:
657c478bd9Sstevel@tonic-gate 		*addrtype++ = htons(PARM_ADDR4);
667c478bd9Sstevel@tonic-gate 		*addrtype = 0;
677c478bd9Sstevel@tonic-gate 		sph->sph_len = htons(sizeof (*sph) + sizeof (*addrtype));
687c478bd9Sstevel@tonic-gate 		break;
69*bd670b35SErik Nordmark 	case AF_INET6:
707c478bd9Sstevel@tonic-gate 		*addrtype++ = htons(PARM_ADDR6);
717c478bd9Sstevel@tonic-gate 		if (!sctp->sctp_connp->conn_ipv6_v6only) {
727c478bd9Sstevel@tonic-gate 			*addrtype = htons(PARM_ADDR4);
737c478bd9Sstevel@tonic-gate 			sph->sph_len = htons(sizeof (*sph) +
747c478bd9Sstevel@tonic-gate 			    sizeof (*addrtype) * 2);
757c478bd9Sstevel@tonic-gate 		} else {
767c478bd9Sstevel@tonic-gate 			*addrtype = 0;
777c478bd9Sstevel@tonic-gate 			sph->sph_len = htons(sizeof (*sph) +
787c478bd9Sstevel@tonic-gate 			    sizeof (*addrtype));
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 		break;
817c478bd9Sstevel@tonic-gate 	default:
827c478bd9Sstevel@tonic-gate 		break;
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	return (sizeof (*sph) + (sizeof (*addrtype) * 2));
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Currently, we support on PRSCTP option, there is more to come.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
917c478bd9Sstevel@tonic-gate size_t
927c478bd9Sstevel@tonic-gate sctp_options_param_len(const sctp_t *sctp, int option)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	size_t	optlen;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	switch (option) {
977c478bd9Sstevel@tonic-gate 	case SCTP_PRSCTP_OPTION:
987c478bd9Sstevel@tonic-gate 		optlen = sizeof (sctp_parm_hdr_t);
997c478bd9Sstevel@tonic-gate 		break;
1007c478bd9Sstevel@tonic-gate 	default:
1017c478bd9Sstevel@tonic-gate 		ASSERT(0);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	return (optlen);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1087c478bd9Sstevel@tonic-gate size_t
1097c478bd9Sstevel@tonic-gate sctp_options_param(const sctp_t *sctp, void *p, int option)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t	*sph = (sctp_parm_hdr_t *)p;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	switch (option) {
1147c478bd9Sstevel@tonic-gate 	case SCTP_PRSCTP_OPTION:
1157c478bd9Sstevel@tonic-gate 		sph->sph_type = htons(PARM_FORWARD_TSN);
1167c478bd9Sstevel@tonic-gate 		sph->sph_len = htons(sizeof (*sph));
1177c478bd9Sstevel@tonic-gate 		break;
1187c478bd9Sstevel@tonic-gate 	default:
1197c478bd9Sstevel@tonic-gate 		ASSERT(0);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	return (sizeof (*sph));
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate size_t
127558fbd03Skcpoon sctp_adaptation_code_param(sctp_t *sctp, uchar_t *p)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *sph;
1307c478bd9Sstevel@tonic-gate 
131558fbd03Skcpoon 	if (!sctp->sctp_send_adaptation) {
1327c478bd9Sstevel@tonic-gate 		return (0);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	sph = (sctp_parm_hdr_t *)p;
1357c478bd9Sstevel@tonic-gate 	sph->sph_type = htons(PARM_ADAPT_LAYER_IND);
1367c478bd9Sstevel@tonic-gate 	sph->sph_len = htons(sizeof (*sph) + sizeof (uint32_t));
137558fbd03Skcpoon 	*(uint32_t *)(sph + 1) = htonl(sctp->sctp_tx_adaptation_code);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (sizeof (*sph) + sizeof (uint32_t));
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate mblk_t *
143*bd670b35SErik Nordmark sctp_init_mp(sctp_t *sctp, sctp_faddr_t *fp)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
1467c478bd9Sstevel@tonic-gate 	uchar_t			*p;
1477c478bd9Sstevel@tonic-gate 	size_t			initlen;
1487c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*icp;
1497c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*chp;
1507c478bd9Sstevel@tonic-gate 	uint16_t		schlen;
1517c478bd9Sstevel@tonic-gate 	int			supp_af;
152*bd670b35SErik Nordmark 	sctp_stack_t		*sctps = sctp->sctp_sctps;
153*bd670b35SErik Nordmark 	conn_t			*connp = sctp->sctp_connp;
1547c478bd9Sstevel@tonic-gate 
155*bd670b35SErik Nordmark 	if (connp->conn_family == AF_INET) {
1567c478bd9Sstevel@tonic-gate 		supp_af = PARM_SUPP_V4;
1577c478bd9Sstevel@tonic-gate 	} else {
1587c478bd9Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only)
1597c478bd9Sstevel@tonic-gate 			supp_af = PARM_SUPP_V6;
1607c478bd9Sstevel@tonic-gate 		else
1617c478bd9Sstevel@tonic-gate 			supp_af = PARM_SUPP_V6 | PARM_SUPP_V4;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	initlen = sizeof (*chp) + sizeof (*icp);
164558fbd03Skcpoon 	if (sctp->sctp_send_adaptation) {
1657c478bd9Sstevel@tonic-gate 		initlen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	initlen += sctp_supaddr_param_len(sctp);
16812f47623Skcpoon 	initlen += sctp_addr_params(sctp, supp_af, NULL, B_TRUE);
169f4b3ec61Sdh 	if (sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled)
1707c478bd9Sstevel@tonic-gate 		initlen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * This could be a INIT retransmission in which case sh_verf may
1747c478bd9Sstevel@tonic-gate 	 * be non-zero, zero it out just to be sure.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = 0;
1777c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = 0;
1787c478bd9Sstevel@tonic-gate 
179*bd670b35SErik Nordmark 	mp = sctp_make_mp(sctp, fp, initlen);
18077c67f2fSkcpoon 	if (mp == NULL) {
181f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_send_init_failed);
1827c478bd9Sstevel@tonic-gate 		return (NULL);
18377c67f2fSkcpoon 	}
184*bd670b35SErik Nordmark 	/* sctp_make_mp could have discovered we have no usable sources */
185*bd670b35SErik Nordmark 	if (sctp->sctp_nsaddrs == 0) {
186*bd670b35SErik Nordmark 		freemsg(mp);
187*bd670b35SErik Nordmark 		SCTP_KSTAT(sctps, sctp_send_init_failed);
188*bd670b35SErik Nordmark 		return (NULL);
189*bd670b35SErik Nordmark 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* Lay in a new INIT chunk, starting with the chunk header */
1927c478bd9Sstevel@tonic-gate 	chp = (sctp_chunk_hdr_t *)mp->b_wptr;
1937c478bd9Sstevel@tonic-gate 	chp->sch_id = CHUNK_INIT;
1947c478bd9Sstevel@tonic-gate 	chp->sch_flags = 0;
1957c478bd9Sstevel@tonic-gate 	schlen = (uint16_t)initlen;
1967c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(schlen, &(chp->sch_len));
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	mp->b_wptr += initlen;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	icp = (sctp_init_chunk_t *)(chp + 1);
2017c478bd9Sstevel@tonic-gate 	icp->sic_inittag = sctp->sctp_lvtag;
2027c478bd9Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_rwnd, &(icp->sic_a_rwnd));
2037c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(sctp->sctp_num_ostr, &(icp->sic_outstr));
2047c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(sctp->sctp_num_istr, &(icp->sic_instr));
2057c478bd9Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_ltsn, &(icp->sic_inittsn));
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	p = (uchar_t *)(icp + 1);
2087c478bd9Sstevel@tonic-gate 
209558fbd03Skcpoon 	/* Adaptation layer param */
210558fbd03Skcpoon 	p += sctp_adaptation_code_param(sctp, p);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* Add supported address types parameter */
2137c478bd9Sstevel@tonic-gate 	p += sctp_supaddr_param(sctp, p);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/* Add address parameters */
21612f47623Skcpoon 	p += sctp_addr_params(sctp, supp_af, p, B_FALSE);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/* Add Forward-TSN-Supported param */
219f4b3ec61Sdh 	if (sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled)
2207c478bd9Sstevel@tonic-gate 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
2237c478bd9Sstevel@tonic-gate 
224*bd670b35SErik Nordmark 	sctp_set_iplen(sctp, mp, fp->ixa);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	return (mp);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Extracts the verification tag from an INIT chunk. If the INIT
2317c478bd9Sstevel@tonic-gate  * chunk is truncated or malformed, returns 0.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate uint32_t
2347c478bd9Sstevel@tonic-gate sctp_init2vtag(sctp_chunk_hdr_t *initch)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t *init;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(initch + 1);
2397c478bd9Sstevel@tonic-gate 	return (init->sic_inittag);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate size_t
24312f47623Skcpoon sctp_addr_params(sctp_t *sctp, int af, uchar_t *p, boolean_t modify)
2447c478bd9Sstevel@tonic-gate {
24512f47623Skcpoon 	size_t	param_len;
24612f47623Skcpoon 
2477c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
250f551bb10Svi 	 * If we have only one local address or it is a loopback or linklocal
251f551bb10Svi 	 * association, we let the peer pull the address from the IP header.
2527c478bd9Sstevel@tonic-gate 	 */
25312f47623Skcpoon 	if ((!modify && sctp->sctp_nsaddrs == 1) || sctp->sctp_loopback ||
254f551bb10Svi 	    sctp->sctp_linklocal) {
2557c478bd9Sstevel@tonic-gate 		return (0);
256f551bb10Svi 	}
257f551bb10Svi 
25812f47623Skcpoon 	param_len = sctp_saddr_info(sctp, af, p, modify);
25912f47623Skcpoon 	return ((sctp->sctp_nsaddrs == 1) ? 0 : param_len);
2607c478bd9Sstevel@tonic-gate }
261