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*6be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/stream.h>
287c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
297c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <netinet/in.h>
327c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <inet/common.h>
357c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
367c478bd9Sstevel@tonic-gate #include <inet/ip.h>
377c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
387c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
397c478bd9Sstevel@tonic-gate #include <inet/nd.h>
407c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
41f4b3ec61Sdh #include <inet/ipclassifier.h>
427c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
437c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
467c478bd9Sstevel@tonic-gate size_t
sctp_supaddr_param_len(sctp_t * sctp)477c478bd9Sstevel@tonic-gate sctp_supaddr_param_len(sctp_t *sctp)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	return (sizeof (sctp_parm_hdr_t) + sizeof (int32_t));
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate size_t
sctp_supaddr_param(sctp_t * sctp,uchar_t * p)537c478bd9Sstevel@tonic-gate sctp_supaddr_param(sctp_t *sctp, uchar_t *p)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *sph;
567c478bd9Sstevel@tonic-gate 	uint16_t *addrtype;
57bd670b35SErik Nordmark 	conn_t		*connp = sctp->sctp_connp;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	sph = (sctp_parm_hdr_t *)p;
607c478bd9Sstevel@tonic-gate 	sph->sph_type = htons(PARM_SUPP_ADDRS);
617c478bd9Sstevel@tonic-gate 	addrtype = (uint16_t *)(sph + 1);
62bd670b35SErik Nordmark 	switch (connp->conn_family) {
63bd670b35SErik Nordmark 	case AF_INET:
647c478bd9Sstevel@tonic-gate 		*addrtype++ = htons(PARM_ADDR4);
657c478bd9Sstevel@tonic-gate 		*addrtype = 0;
667c478bd9Sstevel@tonic-gate 		sph->sph_len = htons(sizeof (*sph) + sizeof (*addrtype));
677c478bd9Sstevel@tonic-gate 		break;
68bd670b35SErik Nordmark 	case AF_INET6:
697c478bd9Sstevel@tonic-gate 		*addrtype++ = htons(PARM_ADDR6);
707c478bd9Sstevel@tonic-gate 		if (!sctp->sctp_connp->conn_ipv6_v6only) {
717c478bd9Sstevel@tonic-gate 			*addrtype = htons(PARM_ADDR4);
727c478bd9Sstevel@tonic-gate 			sph->sph_len = htons(sizeof (*sph) +
737c478bd9Sstevel@tonic-gate 			    sizeof (*addrtype) * 2);
747c478bd9Sstevel@tonic-gate 		} else {
757c478bd9Sstevel@tonic-gate 			*addrtype = 0;
767c478bd9Sstevel@tonic-gate 			sph->sph_len = htons(sizeof (*sph) +
777c478bd9Sstevel@tonic-gate 			    sizeof (*addrtype));
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 		break;
807c478bd9Sstevel@tonic-gate 	default:
817c478bd9Sstevel@tonic-gate 		break;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	return (sizeof (*sph) + (sizeof (*addrtype) * 2));
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Currently, we support on PRSCTP option, there is more to come.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
907c478bd9Sstevel@tonic-gate size_t
sctp_options_param_len(const sctp_t * sctp,int option)917c478bd9Sstevel@tonic-gate sctp_options_param_len(const sctp_t *sctp, int option)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	size_t	optlen;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	switch (option) {
967c478bd9Sstevel@tonic-gate 	case SCTP_PRSCTP_OPTION:
977c478bd9Sstevel@tonic-gate 		optlen = sizeof (sctp_parm_hdr_t);
987c478bd9Sstevel@tonic-gate 		break;
997c478bd9Sstevel@tonic-gate 	default:
1007c478bd9Sstevel@tonic-gate 		ASSERT(0);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return (optlen);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1077c478bd9Sstevel@tonic-gate size_t
sctp_options_param(const sctp_t * sctp,void * p,int option)1087c478bd9Sstevel@tonic-gate sctp_options_param(const sctp_t *sctp, void *p, int option)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t	*sph = (sctp_parm_hdr_t *)p;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	switch (option) {
1137c478bd9Sstevel@tonic-gate 	case SCTP_PRSCTP_OPTION:
1147c478bd9Sstevel@tonic-gate 		sph->sph_type = htons(PARM_FORWARD_TSN);
1157c478bd9Sstevel@tonic-gate 		sph->sph_len = htons(sizeof (*sph));
1167c478bd9Sstevel@tonic-gate 		break;
1177c478bd9Sstevel@tonic-gate 	default:
1187c478bd9Sstevel@tonic-gate 		ASSERT(0);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	return (sizeof (*sph));
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate size_t
sctp_adaptation_code_param(sctp_t * sctp,uchar_t * p)126558fbd03Skcpoon sctp_adaptation_code_param(sctp_t *sctp, uchar_t *p)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *sph;
1297c478bd9Sstevel@tonic-gate 
130558fbd03Skcpoon 	if (!sctp->sctp_send_adaptation) {
1317c478bd9Sstevel@tonic-gate 		return (0);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	sph = (sctp_parm_hdr_t *)p;
1347c478bd9Sstevel@tonic-gate 	sph->sph_type = htons(PARM_ADAPT_LAYER_IND);
1357c478bd9Sstevel@tonic-gate 	sph->sph_len = htons(sizeof (*sph) + sizeof (uint32_t));
136558fbd03Skcpoon 	*(uint32_t *)(sph + 1) = htonl(sctp->sctp_tx_adaptation_code);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	return (sizeof (*sph) + sizeof (uint32_t));
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate mblk_t *
sctp_init_mp(sctp_t * sctp,sctp_faddr_t * fp)142bd670b35SErik Nordmark sctp_init_mp(sctp_t *sctp, sctp_faddr_t *fp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
1457c478bd9Sstevel@tonic-gate 	uchar_t			*p;
1467c478bd9Sstevel@tonic-gate 	size_t			initlen;
1477c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*icp;
1487c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*chp;
1497c478bd9Sstevel@tonic-gate 	uint16_t		schlen;
1507c478bd9Sstevel@tonic-gate 	int			supp_af;
151bd670b35SErik Nordmark 	sctp_stack_t		*sctps = sctp->sctp_sctps;
152bd670b35SErik Nordmark 	conn_t			*connp = sctp->sctp_connp;
1537c478bd9Sstevel@tonic-gate 
154bd670b35SErik Nordmark 	if (connp->conn_family == AF_INET) {
1557c478bd9Sstevel@tonic-gate 		supp_af = PARM_SUPP_V4;
1567c478bd9Sstevel@tonic-gate 	} else {
1577c478bd9Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only)
1587c478bd9Sstevel@tonic-gate 			supp_af = PARM_SUPP_V6;
1597c478bd9Sstevel@tonic-gate 		else
1607c478bd9Sstevel@tonic-gate 			supp_af = PARM_SUPP_V6 | PARM_SUPP_V4;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	initlen = sizeof (*chp) + sizeof (*icp);
163558fbd03Skcpoon 	if (sctp->sctp_send_adaptation) {
1647c478bd9Sstevel@tonic-gate 		initlen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	initlen += sctp_supaddr_param_len(sctp);
16712f47623Skcpoon 	initlen += sctp_addr_params(sctp, supp_af, NULL, B_TRUE);
168f4b3ec61Sdh 	if (sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled)
1697c478bd9Sstevel@tonic-gate 		initlen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * This could be a INIT retransmission in which case sh_verf may
1737c478bd9Sstevel@tonic-gate 	 * be non-zero, zero it out just to be sure.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = 0;
1767c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = 0;
1777c478bd9Sstevel@tonic-gate 
178bd670b35SErik Nordmark 	mp = sctp_make_mp(sctp, fp, initlen);
17977c67f2fSkcpoon 	if (mp == NULL) {
180f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_send_init_failed);
1817c478bd9Sstevel@tonic-gate 		return (NULL);
18277c67f2fSkcpoon 	}
183bd670b35SErik Nordmark 	/* sctp_make_mp could have discovered we have no usable sources */
184bd670b35SErik Nordmark 	if (sctp->sctp_nsaddrs == 0) {
185bd670b35SErik Nordmark 		freemsg(mp);
186bd670b35SErik Nordmark 		SCTP_KSTAT(sctps, sctp_send_init_failed);
187bd670b35SErik Nordmark 		return (NULL);
188bd670b35SErik Nordmark 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/* Lay in a new INIT chunk, starting with the chunk header */
1917c478bd9Sstevel@tonic-gate 	chp = (sctp_chunk_hdr_t *)mp->b_wptr;
1927c478bd9Sstevel@tonic-gate 	chp->sch_id = CHUNK_INIT;
1937c478bd9Sstevel@tonic-gate 	chp->sch_flags = 0;
1947c478bd9Sstevel@tonic-gate 	schlen = (uint16_t)initlen;
1957c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(schlen, &(chp->sch_len));
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	mp->b_wptr += initlen;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	icp = (sctp_init_chunk_t *)(chp + 1);
2007c478bd9Sstevel@tonic-gate 	icp->sic_inittag = sctp->sctp_lvtag;
2017c478bd9Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_rwnd, &(icp->sic_a_rwnd));
2027c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(sctp->sctp_num_ostr, &(icp->sic_outstr));
2037c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(sctp->sctp_num_istr, &(icp->sic_instr));
2047c478bd9Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_ltsn, &(icp->sic_inittsn));
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	p = (uchar_t *)(icp + 1);
2077c478bd9Sstevel@tonic-gate 
208558fbd03Skcpoon 	/* Adaptation layer param */
209558fbd03Skcpoon 	p += sctp_adaptation_code_param(sctp, p);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* Add supported address types parameter */
2127c478bd9Sstevel@tonic-gate 	p += sctp_supaddr_param(sctp, p);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* Add address parameters */
21512f47623Skcpoon 	p += sctp_addr_params(sctp, supp_af, p, B_FALSE);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* Add Forward-TSN-Supported param */
218f4b3ec61Sdh 	if (sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled)
2197c478bd9Sstevel@tonic-gate 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
2227c478bd9Sstevel@tonic-gate 
223*6be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	sctp_set_iplen(sctp, mp, fp->sf_ixa);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	return (mp);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Extracts the verification tag from an INIT chunk. If the INIT
2307c478bd9Sstevel@tonic-gate  * chunk is truncated or malformed, returns 0.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate uint32_t
sctp_init2vtag(sctp_chunk_hdr_t * initch)2337c478bd9Sstevel@tonic-gate sctp_init2vtag(sctp_chunk_hdr_t *initch)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t *init;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(initch + 1);
2387c478bd9Sstevel@tonic-gate 	return (init->sic_inittag);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate size_t
sctp_addr_params(sctp_t * sctp,int af,uchar_t * p,boolean_t modify)24212f47623Skcpoon sctp_addr_params(sctp_t *sctp, int af, uchar_t *p, boolean_t modify)
2437c478bd9Sstevel@tonic-gate {
24412f47623Skcpoon 	size_t	param_len;
24512f47623Skcpoon 
2467c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
249f551bb10Svi 	 * If we have only one local address or it is a loopback or linklocal
250f551bb10Svi 	 * association, we let the peer pull the address from the IP header.
2517c478bd9Sstevel@tonic-gate 	 */
25212f47623Skcpoon 	if ((!modify && sctp->sctp_nsaddrs == 1) || sctp->sctp_loopback ||
253f551bb10Svi 	    sctp->sctp_linklocal) {
2547c478bd9Sstevel@tonic-gate 		return (0);
255f551bb10Svi 	}
256f551bb10Svi 
25712f47623Skcpoon 	param_len = sctp_saddr_info(sctp, af, p, modify);
25812f47623Skcpoon 	return ((sctp->sctp_nsaddrs == 1) ? 0 : param_len);
2597c478bd9Sstevel@tonic-gate }
260