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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 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/stream.h>
317c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <netinet/in.h>
357c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <inet/common.h>
387c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
397c478bd9Sstevel@tonic-gate #include <inet/ip.h>
407c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
417c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
427c478bd9Sstevel@tonic-gate #include <inet/nd.h>
437c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
447c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
457c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * This will compute the checksum over the SCTP packet, so this
497c478bd9Sstevel@tonic-gate  * function should only be called after the whole packet has been
507c478bd9Sstevel@tonic-gate  * built.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * rptr should point to the IP / SCTP composite header.
537c478bd9Sstevel@tonic-gate  * len should be the length of the entire packet, including the IP
547c478bd9Sstevel@tonic-gate  *     header.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate void
577c478bd9Sstevel@tonic-gate sctp_add_hdr(sctp_t *sctp, uchar_t *rptr, size_t len)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	ipha_t *iphdr;
607c478bd9Sstevel@tonic-gate 	short iplen;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	ASSERT(len >= sctp->sctp_hdr_len);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* Copy the common header from the template */
657c478bd9Sstevel@tonic-gate 	bcopy(sctp->sctp_iphc, rptr, sctp->sctp_hdr_len);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/* Set the total length in the IP hdr */
687c478bd9Sstevel@tonic-gate 	iplen = (short)len;
697c478bd9Sstevel@tonic-gate 	iphdr = (ipha_t *)rptr;
707c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(iplen, &iphdr->ipha_length);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
747c478bd9Sstevel@tonic-gate size_t
757c478bd9Sstevel@tonic-gate sctp_supaddr_param_len(sctp_t *sctp)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	return (sizeof (sctp_parm_hdr_t) + sizeof (int32_t));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate size_t
817c478bd9Sstevel@tonic-gate sctp_supaddr_param(sctp_t *sctp, uchar_t *p)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *sph;
847c478bd9Sstevel@tonic-gate 	uint16_t *addrtype;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	sph = (sctp_parm_hdr_t *)p;
877c478bd9Sstevel@tonic-gate 	sph->sph_type = htons(PARM_SUPP_ADDRS);
887c478bd9Sstevel@tonic-gate 	addrtype = (uint16_t *)(sph + 1);
897c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_ipversion) {
907c478bd9Sstevel@tonic-gate 	case IPV4_VERSION:
917c478bd9Sstevel@tonic-gate 		*addrtype++ = htons(PARM_ADDR4);
927c478bd9Sstevel@tonic-gate 		*addrtype = 0;
937c478bd9Sstevel@tonic-gate 		sph->sph_len = htons(sizeof (*sph) + sizeof (*addrtype));
947c478bd9Sstevel@tonic-gate 		break;
957c478bd9Sstevel@tonic-gate 	case IPV6_VERSION:
967c478bd9Sstevel@tonic-gate 		*addrtype++ = htons(PARM_ADDR6);
977c478bd9Sstevel@tonic-gate 		if (!sctp->sctp_connp->conn_ipv6_v6only) {
987c478bd9Sstevel@tonic-gate 			*addrtype = htons(PARM_ADDR4);
997c478bd9Sstevel@tonic-gate 			sph->sph_len = htons(sizeof (*sph) +
1007c478bd9Sstevel@tonic-gate 			    sizeof (*addrtype) * 2);
1017c478bd9Sstevel@tonic-gate 		} else {
1027c478bd9Sstevel@tonic-gate 			*addrtype = 0;
1037c478bd9Sstevel@tonic-gate 			sph->sph_len = htons(sizeof (*sph) +
1047c478bd9Sstevel@tonic-gate 			    sizeof (*addrtype));
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 		break;
1077c478bd9Sstevel@tonic-gate 	default:
1087c478bd9Sstevel@tonic-gate 		break;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	return (sizeof (*sph) + (sizeof (*addrtype) * 2));
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Currently, we support on PRSCTP option, there is more to come.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1177c478bd9Sstevel@tonic-gate size_t
1187c478bd9Sstevel@tonic-gate sctp_options_param_len(const sctp_t *sctp, int option)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	size_t	optlen;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	switch (option) {
1237c478bd9Sstevel@tonic-gate 	case SCTP_PRSCTP_OPTION:
1247c478bd9Sstevel@tonic-gate 		optlen = sizeof (sctp_parm_hdr_t);
1257c478bd9Sstevel@tonic-gate 		break;
1267c478bd9Sstevel@tonic-gate 	default:
1277c478bd9Sstevel@tonic-gate 		ASSERT(0);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	return (optlen);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1347c478bd9Sstevel@tonic-gate size_t
1357c478bd9Sstevel@tonic-gate sctp_options_param(const sctp_t *sctp, void *p, int option)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t	*sph = (sctp_parm_hdr_t *)p;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	switch (option) {
1407c478bd9Sstevel@tonic-gate 	case SCTP_PRSCTP_OPTION:
1417c478bd9Sstevel@tonic-gate 		sph->sph_type = htons(PARM_FORWARD_TSN);
1427c478bd9Sstevel@tonic-gate 		sph->sph_len = htons(sizeof (*sph));
1437c478bd9Sstevel@tonic-gate 		break;
1447c478bd9Sstevel@tonic-gate 	default:
1457c478bd9Sstevel@tonic-gate 		ASSERT(0);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	return (sizeof (*sph));
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate size_t
1537c478bd9Sstevel@tonic-gate sctp_adaption_code_param(sctp_t *sctp, uchar_t *p)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *sph;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_send_adaption) {
1587c478bd9Sstevel@tonic-gate 		return (0);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	sph = (sctp_parm_hdr_t *)p;
1617c478bd9Sstevel@tonic-gate 	sph->sph_type = htons(PARM_ADAPT_LAYER_IND);
1627c478bd9Sstevel@tonic-gate 	sph->sph_len = htons(sizeof (*sph) + sizeof (uint32_t));
1637c478bd9Sstevel@tonic-gate 	*(uint32_t *)(sph + 1) = htonl(sctp->sctp_tx_adaption_code);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (sizeof (*sph) + sizeof (uint32_t));
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate mblk_t *
1697c478bd9Sstevel@tonic-gate sctp_init_mp(sctp_t *sctp)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
1727c478bd9Sstevel@tonic-gate 	uchar_t			*p;
1737c478bd9Sstevel@tonic-gate 	size_t			initlen;
1747c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*icp;
1757c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*chp;
1767c478bd9Sstevel@tonic-gate 	uint16_t		schlen;
1777c478bd9Sstevel@tonic-gate 	int			supp_af;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (sctp->sctp_family == AF_INET) {
1807c478bd9Sstevel@tonic-gate 		supp_af = PARM_SUPP_V4;
1817c478bd9Sstevel@tonic-gate 	} else {
1827c478bd9Sstevel@tonic-gate 		/* Assume here that a v6 endpoint supports v4 address. */
1837c478bd9Sstevel@tonic-gate 		if (sctp->sctp_connp->conn_ipv6_v6only)
1847c478bd9Sstevel@tonic-gate 			supp_af = PARM_SUPP_V6;
1857c478bd9Sstevel@tonic-gate 		else
1867c478bd9Sstevel@tonic-gate 			supp_af = PARM_SUPP_V6 | PARM_SUPP_V4;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	initlen = sizeof (*chp) + sizeof (*icp);
1897c478bd9Sstevel@tonic-gate 	if (sctp->sctp_send_adaption) {
1907c478bd9Sstevel@tonic-gate 		initlen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	initlen += sctp_supaddr_param_len(sctp);
193*f551bb10Svi 	initlen += sctp_addr_params_len(sctp, supp_af, B_TRUE);
1947c478bd9Sstevel@tonic-gate 	if (sctp->sctp_prsctp_aware && sctp_prsctp_enabled)
1957c478bd9Sstevel@tonic-gate 		initlen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * This could be a INIT retransmission in which case sh_verf may
1997c478bd9Sstevel@tonic-gate 	 * be non-zero, zero it out just to be sure.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = 0;
2027c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = 0;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	mp = sctp_make_mp(sctp, NULL, initlen);
2057c478bd9Sstevel@tonic-gate 	if (mp == NULL)
2067c478bd9Sstevel@tonic-gate 		return (NULL);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* Lay in a new INIT chunk, starting with the chunk header */
2097c478bd9Sstevel@tonic-gate 	chp = (sctp_chunk_hdr_t *)mp->b_wptr;
2107c478bd9Sstevel@tonic-gate 	chp->sch_id = CHUNK_INIT;
2117c478bd9Sstevel@tonic-gate 	chp->sch_flags = 0;
2127c478bd9Sstevel@tonic-gate 	schlen = (uint16_t)initlen;
2137c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(schlen, &(chp->sch_len));
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	mp->b_wptr += initlen;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	icp = (sctp_init_chunk_t *)(chp + 1);
2187c478bd9Sstevel@tonic-gate 	icp->sic_inittag = sctp->sctp_lvtag;
2197c478bd9Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_rwnd, &(icp->sic_a_rwnd));
2207c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(sctp->sctp_num_ostr, &(icp->sic_outstr));
2217c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(sctp->sctp_num_istr, &(icp->sic_instr));
2227c478bd9Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_ltsn, &(icp->sic_inittsn));
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	p = (uchar_t *)(icp + 1);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/* Adaption layer param */
2277c478bd9Sstevel@tonic-gate 	p += sctp_adaption_code_param(sctp, p);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* Add supported address types parameter */
2307c478bd9Sstevel@tonic-gate 	p += sctp_supaddr_param(sctp, p);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* Add address parameters */
2337c478bd9Sstevel@tonic-gate 	p += sctp_addr_params(sctp, supp_af, p);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* Add Forward-TSN-Supported param */
2367c478bd9Sstevel@tonic-gate 	if (sctp->sctp_prsctp_aware && sctp_prsctp_enabled)
2377c478bd9Sstevel@tonic-gate 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	sctp_set_iplen(sctp, mp);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (mp);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * Extracts the verification tag from an INIT chunk. If the INIT
2487c478bd9Sstevel@tonic-gate  * chunk is truncated or malformed, returns 0.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate uint32_t
2517c478bd9Sstevel@tonic-gate sctp_init2vtag(sctp_chunk_hdr_t *initch)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t *init;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(initch + 1);
2567c478bd9Sstevel@tonic-gate 	return (init->sic_inittag);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate size_t
260*f551bb10Svi sctp_addr_params_len(sctp_t *sctp, int af, boolean_t modify)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_nsaddrs > 0);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
265*f551bb10Svi 	 * If we have only one local address or it is a loopback or linklocal
266*f551bb10Svi 	 * association, we let the peer pull the address from the IP header.
2677c478bd9Sstevel@tonic-gate 	 */
268*f551bb10Svi 	if (sctp->sctp_nsaddrs == 1 || sctp->sctp_loopback ||
269*f551bb10Svi 	    sctp->sctp_linklocal) {
2707c478bd9Sstevel@tonic-gate 		return (0);
271*f551bb10Svi 	}
272*f551bb10Svi 
273*f551bb10Svi 	return (sctp_saddr_info(sctp, af, NULL, modify));
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate size_t
2777c478bd9Sstevel@tonic-gate sctp_addr_params(sctp_t *sctp, int af, uchar_t *p)
2787c478bd9Sstevel@tonic-gate {
279*f551bb10Svi 	/*
280*f551bb10Svi 	 * If we have only one local address or it is a loopback or linklocal
281*f551bb10Svi 	 * association, we let the peer pull the address from the IP header.
282*f551bb10Svi 	 */
283*f551bb10Svi 	if (sctp->sctp_nsaddrs == 1 || sctp->sctp_loopback ||
284*f551bb10Svi 	    sctp->sctp_linklocal) {
2857c478bd9Sstevel@tonic-gate 		return (0);
286*f551bb10Svi 	}
287*f551bb10Svi 	return (sctp_saddr_info(sctp, af, p, B_FALSE));
2887c478bd9Sstevel@tonic-gate }
289