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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 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/md5.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/strsubr.h>
360Sstevel@tonic-gate #include <sys/random.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <netinet/in.h>
390Sstevel@tonic-gate #include <netinet/ip6.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <inet/common.h>
420Sstevel@tonic-gate #include <inet/ip.h>
430Sstevel@tonic-gate #include <inet/ip6.h>
440Sstevel@tonic-gate #include <inet/sctp_ip.h>
450Sstevel@tonic-gate #include <inet/ipclassifier.h>
460Sstevel@tonic-gate #include "sctp_impl.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * From RFC 2104. This should probably go into libmd5 (and while
500Sstevel@tonic-gate  * we're at it, maybe we should make a libdigest so we can later
510Sstevel@tonic-gate  * add SHA1 and others, esp. since some weaknesses have been found
520Sstevel@tonic-gate  * with MD5).
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * text		IN			pointer to data stream
550Sstevel@tonic-gate  * text_len	IN			length of data stream
560Sstevel@tonic-gate  * key		IN			pointer to authentication key
570Sstevel@tonic-gate  * key_len	IN			length of authentication key
580Sstevel@tonic-gate  * digest	OUT			caller digest to be filled in
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate static void
610Sstevel@tonic-gate hmac_md5(uchar_t *text, size_t text_len, uchar_t *key, size_t key_len,
620Sstevel@tonic-gate     uchar_t *digest)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	MD5_CTX context;
650Sstevel@tonic-gate 	uchar_t k_ipad[65];	/* inner padding - key XORd with ipad */
660Sstevel@tonic-gate 	uchar_t k_opad[65];	/* outer padding - key XORd with opad */
670Sstevel@tonic-gate 	uchar_t tk[16];
680Sstevel@tonic-gate 	int i;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/* if key is longer than 64 bytes reset it to key=MD5(key) */
710Sstevel@tonic-gate 	if (key_len > 64) {
720Sstevel@tonic-gate 		MD5_CTX tctx;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 		MD5Init(&tctx);
750Sstevel@tonic-gate 		MD5Update(&tctx, key, key_len);
760Sstevel@tonic-gate 		MD5Final(tk, &tctx);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 		key = tk;
790Sstevel@tonic-gate 		key_len = 16;
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 * the HMAC_MD5 transform looks like:
840Sstevel@tonic-gate 	 *
850Sstevel@tonic-gate 	 * MD5(K XOR opad, MD5(K XOR ipad, text))
860Sstevel@tonic-gate 	 *
870Sstevel@tonic-gate 	 * where K is an n byte key
880Sstevel@tonic-gate 	 * ipad is the byte 0x36 repeated 64 times
890Sstevel@tonic-gate 	 * opad is the byte 0x5c repeated 64 times
900Sstevel@tonic-gate 	 * and text is the data being protected
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* start out by storing key in pads */
940Sstevel@tonic-gate 	bzero(k_ipad, sizeof (k_ipad));
950Sstevel@tonic-gate 	bzero(k_opad, sizeof (k_opad));
960Sstevel@tonic-gate 	bcopy(key, k_ipad, key_len);
970Sstevel@tonic-gate 	bcopy(key, k_opad, key_len);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/* XOR key with ipad and opad values */
1000Sstevel@tonic-gate 	for (i = 0; i < 64; i++) {
1010Sstevel@tonic-gate 		k_ipad[i] ^= 0x36;
1020Sstevel@tonic-gate 		k_opad[i] ^= 0x5c;
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 * perform inner MD5
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	MD5Init(&context);			/* init context for 1st */
1080Sstevel@tonic-gate 						/* pass */
1090Sstevel@tonic-gate 	MD5Update(&context, k_ipad, 64);	/* start with inner pad */
1100Sstevel@tonic-gate 	MD5Update(&context, text, text_len);	/* then text of datagram */
1110Sstevel@tonic-gate 	MD5Final(digest, &context);		/* finish up 1st pass */
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 * perform outer MD5
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	MD5Init(&context);			/* init context for 2nd */
1160Sstevel@tonic-gate 						/* pass */
1170Sstevel@tonic-gate 	MD5Update(&context, k_opad, 64);	/* start with outer pad */
1180Sstevel@tonic-gate 	MD5Update(&context, digest, 16);	/* then results of 1st */
1190Sstevel@tonic-gate 						/* hash */
1200Sstevel@tonic-gate 	MD5Final(digest, &context);		/* finish up 2nd pass */
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * If inmp is non-NULL, and we need to abort, it will use the IP/SCTP
1250Sstevel@tonic-gate  * info in initmp to send the abort. Otherwise, no abort will be sent.
1260Sstevel@tonic-gate  * If errmp is non-NULL, a chain of unrecognized parameters will
1270Sstevel@tonic-gate  * be created and returned via *errmp.
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  * Returns 1 if the parameters are OK (or there are no parameters), or
1300Sstevel@tonic-gate  * 0 if not.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate static int
1330Sstevel@tonic-gate validate_init_params(sctp_t *sctp, sctp_chunk_hdr_t *ch,
1340Sstevel@tonic-gate     sctp_init_chunk_t *init, mblk_t *inmp, sctp_parm_hdr_t **want_cookie,
1350Sstevel@tonic-gate     mblk_t **errmp, int *supp_af, uint_t *sctp_options)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
1380Sstevel@tonic-gate 	sctp_init_chunk_t	*ic;
1390Sstevel@tonic-gate 	ssize_t			remaining;
1400Sstevel@tonic-gate 	uint16_t		serror = 0;
1410Sstevel@tonic-gate 	char			*details = NULL;
1420Sstevel@tonic-gate 	size_t			errlen = 0;
1430Sstevel@tonic-gate 	boolean_t		got_cookie = B_FALSE;
1440Sstevel@tonic-gate 	uint16_t		ptype;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	*supp_af = 0;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (sctp_options != NULL)
1490Sstevel@tonic-gate 		*sctp_options = 0;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* First validate stream parameters */
1520Sstevel@tonic-gate 	if (init->sic_instr == 0 || init->sic_outstr == 0) {
1530Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1540Sstevel@tonic-gate 		dprint(1,
1550Sstevel@tonic-gate 		    ("validate_init_params: bad sid, is=%d os=%d\n",
1560Sstevel@tonic-gate 			htons(init->sic_instr), htons(init->sic_outstr)));
1570Sstevel@tonic-gate 		goto abort;
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 	if (ntohl(init->sic_inittag) == 0) {
1600Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1610Sstevel@tonic-gate 		dprint(1, ("validate_init_params: inittag = 0\n"));
1620Sstevel@tonic-gate 		goto abort;
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	remaining = ntohs(ch->sch_len) - sizeof (*ch);
1660Sstevel@tonic-gate 	ic = (sctp_init_chunk_t *)(ch + 1);
1670Sstevel@tonic-gate 	remaining -= sizeof (*ic);
1680Sstevel@tonic-gate 	if (remaining < sizeof (*cph)) {
1690Sstevel@tonic-gate 		/* Nothing to validate */
1700Sstevel@tonic-gate 		if (want_cookie != NULL)
1710Sstevel@tonic-gate 			goto cookie_abort;
1720Sstevel@tonic-gate 		return (1);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	cph = (sctp_parm_hdr_t *)(ic + 1);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	while (cph != NULL) {
1780Sstevel@tonic-gate 		ptype = ntohs(cph->sph_type);
1790Sstevel@tonic-gate 		switch (ptype) {
1800Sstevel@tonic-gate 		case PARM_HBINFO:
1810Sstevel@tonic-gate 		case PARM_UNRECOGNIZED:
1820Sstevel@tonic-gate 		case PARM_ECN:
1830Sstevel@tonic-gate 			/* just ignore them */
1840Sstevel@tonic-gate 			break;
1850Sstevel@tonic-gate 		case PARM_FORWARD_TSN:
1860Sstevel@tonic-gate 			if (sctp_options != NULL)
1870Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 		case PARM_COOKIE:
1900Sstevel@tonic-gate 			got_cookie = B_TRUE;
1910Sstevel@tonic-gate 			if (want_cookie != NULL) {
1920Sstevel@tonic-gate 				*want_cookie = cph;
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 			break;
1950Sstevel@tonic-gate 		case PARM_ADDR4:
1960Sstevel@tonic-gate 		case PARM_ADDR6:
1970Sstevel@tonic-gate 		case PARM_COOKIE_PRESERVE:
1980Sstevel@tonic-gate 		case PARM_ADAPT_LAYER_IND:
1990Sstevel@tonic-gate 			/* These are OK */
2000Sstevel@tonic-gate 			break;
2010Sstevel@tonic-gate 		case PARM_ADDR_HOST_NAME:
2020Sstevel@tonic-gate 			/* Don't support this; abort the association */
2030Sstevel@tonic-gate 			serror = SCTP_ERR_BAD_ADDR;
2040Sstevel@tonic-gate 			details = (char *)cph;
2050Sstevel@tonic-gate 			errlen = ntohs(cph->sph_len);
2060Sstevel@tonic-gate 			dprint(1, ("sctp:validate_init_params: host addr\n"));
2070Sstevel@tonic-gate 			goto abort;
2080Sstevel@tonic-gate 		case PARM_SUPP_ADDRS: {
2090Sstevel@tonic-gate 			/* Make sure we have a supported addr intersection */
2100Sstevel@tonic-gate 			uint16_t *p, addrtype;
2110Sstevel@tonic-gate 			int plen;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 			plen = ntohs(cph->sph_len);
2140Sstevel@tonic-gate 			p = (uint16_t *)(cph + 1);
2150Sstevel@tonic-gate 			while (plen > 0) {
2160Sstevel@tonic-gate 				addrtype = ntohs(*p);
2170Sstevel@tonic-gate 				switch (addrtype) {
2180Sstevel@tonic-gate 				case PARM_ADDR6:
2190Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V6;
2200Sstevel@tonic-gate 					break;
2210Sstevel@tonic-gate 				case PARM_ADDR4:
2220Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V4;
2230Sstevel@tonic-gate 					break;
2240Sstevel@tonic-gate 				default:
2250Sstevel@tonic-gate 					/*
2260Sstevel@tonic-gate 					 * Do nothing, silently ignore hostname
2270Sstevel@tonic-gate 					 * address.
2280Sstevel@tonic-gate 					 */
2290Sstevel@tonic-gate 					break;
2300Sstevel@tonic-gate 				}
2310Sstevel@tonic-gate 				p++;
2320Sstevel@tonic-gate 				plen -= sizeof (*p);
2330Sstevel@tonic-gate 			}
2340Sstevel@tonic-gate 			/*
2350Sstevel@tonic-gate 			 * Some sanity checks.  The following should not
2360Sstevel@tonic-gate 			 * fail unless the other side is broken.
2370Sstevel@tonic-gate 			 *
2380Sstevel@tonic-gate 			 * 1. If there is no supported address type yet the
2390Sstevel@tonic-gate 			 * supported address parameter is present, abort.
2400Sstevel@tonic-gate 			 * 2. If this is a V4 endpoint but V4 address is not
2410Sstevel@tonic-gate 			 * supported, abort.
2420Sstevel@tonic-gate 			 * 3. If this is a V6 only endpoint but V6 address is
2430Sstevel@tonic-gate 			 * not supported, abort.  This assumes that a V6
2440Sstevel@tonic-gate 			 * endpoint can use both V4 and V6 addresses.
2450Sstevel@tonic-gate 			 */
2460Sstevel@tonic-gate 			if (*supp_af == 0 ||
2470Sstevel@tonic-gate 			    (sctp->sctp_family == AF_INET &&
2480Sstevel@tonic-gate 			    !(*supp_af & PARM_SUPP_V4)) ||
2490Sstevel@tonic-gate 			    (sctp->sctp_family == AF_INET6 &&
2500Sstevel@tonic-gate 			    !(*supp_af & PARM_SUPP_V6) &&
2510Sstevel@tonic-gate 			    sctp->sctp_connp->conn_ipv6_v6only)) {
2520Sstevel@tonic-gate 				dprint(1,
2530Sstevel@tonic-gate 				("sctp:validate_init_params: no supp addr\n"));
2540Sstevel@tonic-gate 				serror = SCTP_ERR_BAD_ADDR;
2550Sstevel@tonic-gate 				goto abort;
2560Sstevel@tonic-gate 			}
2570Sstevel@tonic-gate 			break;
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 		default:
2600Sstevel@tonic-gate 			/* Unrecognized param; check the high order bits */
2610Sstevel@tonic-gate 			if ((ptype & 0xc000) == 0xc000) {
2620Sstevel@tonic-gate 				/*
2630Sstevel@tonic-gate 				 * report unrecognized param, and
2640Sstevel@tonic-gate 				 * keep processing
2650Sstevel@tonic-gate 				 */
2660Sstevel@tonic-gate 				if (errmp != NULL) {
2670Sstevel@tonic-gate 					if (want_cookie != NULL) {
2680Sstevel@tonic-gate 						*errmp = sctp_make_err(sctp,
2690Sstevel@tonic-gate 						    PARM_UNRECOGNIZED,
2700Sstevel@tonic-gate 						    (void *)cph,
2710Sstevel@tonic-gate 						    ntohs(cph->sph_len));
2720Sstevel@tonic-gate 					} else {
2730Sstevel@tonic-gate 						sctp_add_unrec_parm(cph, errmp);
2740Sstevel@tonic-gate 					}
2750Sstevel@tonic-gate 				}
2760Sstevel@tonic-gate 				break;
2770Sstevel@tonic-gate 			}
2780Sstevel@tonic-gate 			if (ptype & 0x4000) {
2790Sstevel@tonic-gate 				/*
2800Sstevel@tonic-gate 				 * Stop processing and drop; report
2810Sstevel@tonic-gate 				 * unrecognized param
2820Sstevel@tonic-gate 				 */
2830Sstevel@tonic-gate 				serror = SCTP_ERR_UNREC_PARM;
2840Sstevel@tonic-gate 				details = (char *)cph;
2850Sstevel@tonic-gate 				errlen = ntohs(cph->sph_len);
2860Sstevel@tonic-gate 				goto abort;
2870Sstevel@tonic-gate 			}
2880Sstevel@tonic-gate 			if (ptype & 0x8000) {
2890Sstevel@tonic-gate 				/* skip and continue processing */
2900Sstevel@tonic-gate 				break;
2910Sstevel@tonic-gate 			}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 			/*
2940Sstevel@tonic-gate 			 * 2 high bits are clear; stop processing and
2950Sstevel@tonic-gate 			 * drop packet
2960Sstevel@tonic-gate 			 */
2970Sstevel@tonic-gate 			return (0);
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		cph = sctp_next_parm(cph, &remaining);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (want_cookie != NULL && !got_cookie) {
3040Sstevel@tonic-gate cookie_abort:
3050Sstevel@tonic-gate 		dprint(1, ("validate_init_params: cookie absent\n"));
3060Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch), SCTP_ERR_MISSING_PARM,
3070Sstevel@tonic-gate 		    details, errlen, inmp, 0, B_FALSE);
3080Sstevel@tonic-gate 		return (0);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/* OK */
3120Sstevel@tonic-gate 	return (1);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate abort:
3150Sstevel@tonic-gate 	if (want_cookie != NULL)
3160Sstevel@tonic-gate 		return (0);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	sctp_send_abort(sctp, sctp_init2vtag(ch), serror, details,
3190Sstevel@tonic-gate 	    errlen, inmp, 0, B_FALSE);
3200Sstevel@tonic-gate 	return (0);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate /*
3240Sstevel@tonic-gate  * Initialize params from the INIT and INIT-ACK when the assoc. is
3250Sstevel@tonic-gate  * established.
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate boolean_t
3280Sstevel@tonic-gate sctp_initialize_params(sctp_t *sctp, sctp_init_chunk_t *init,
3290Sstevel@tonic-gate     sctp_init_chunk_t *iack)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	/* Get initial TSN */
3320Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(init->sic_inittsn);
3330Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/* Serial number is initialized to the same value as the TSN */
3360Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/*
3390Sstevel@tonic-gate 	 * Get verification tags; no byteordering is necessary, since
3400Sstevel@tonic-gate 	 * verfication tags are never processed except for byte-by-byte
3410Sstevel@tonic-gate 	 * comparisons.
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	sctp->sctp_fvtag = init->sic_inittag;
3440Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = init->sic_inittag;
3450Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = init->sic_inittag;
3460Sstevel@tonic-gate 	sctp->sctp_lvtag = iack->sic_inittag;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/* Get the peer's rwnd */
3490Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	/* Allocate the in/out-stream counters */
3520Sstevel@tonic-gate 	sctp->sctp_num_ostr = iack->sic_outstr;
3530Sstevel@tonic-gate 	sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
3540Sstevel@tonic-gate 	    sctp->sctp_num_ostr, KM_NOSLEEP);
3550Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL)
3560Sstevel@tonic-gate 		return (B_FALSE);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	sctp->sctp_num_istr = iack->sic_instr;
3590Sstevel@tonic-gate 	sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
3600Sstevel@tonic-gate 	    sctp->sctp_num_istr, KM_NOSLEEP);
3610Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
3620Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
3630Sstevel@tonic-gate 		    sctp->sctp_num_ostr);
3640Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
3650Sstevel@tonic-gate 		return (B_FALSE);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 	return (B_TRUE);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate #define	SCTP_CALC_COOKIE_LEN(initcp) \
3710Sstevel@tonic-gate 	sizeof (int64_t) +		/* timestamp */			\
3720Sstevel@tonic-gate 	sizeof (uint32_t) +		/* cookie lifetime */		\
3730Sstevel@tonic-gate 	sizeof (sctp_init_chunk_t) +	/* INIT ACK */			\
3740Sstevel@tonic-gate 	sizeof (in6_addr_t) +		/* peer's original source */ 	\
3750Sstevel@tonic-gate 	ntohs((initcp)->sch_len) +	/* peer's INIT */		\
3760Sstevel@tonic-gate 	sizeof (uint32_t) +		/* local tie-tag */		\
3770Sstevel@tonic-gate 	sizeof (uint32_t) +		/* peer tie-tag */		\
3780Sstevel@tonic-gate 	sizeof (sctp_parm_hdr_t) +	/* param header */		\
3790Sstevel@tonic-gate 	16				/* MD5 hash */
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate void
3820Sstevel@tonic-gate sctp_send_initack(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *initmp)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	ipha_t			*initiph;
3850Sstevel@tonic-gate 	ip6_t			*initip6h;
3860Sstevel@tonic-gate 	ipha_t			*iackiph;
3870Sstevel@tonic-gate 	ip6_t			*iackip6h;
3880Sstevel@tonic-gate 	sctp_chunk_hdr_t	*iack_ch;
3890Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
3900Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
3910Sstevel@tonic-gate 	sctp_hdr_t		*iacksh;
3920Sstevel@tonic-gate 	sctp_hdr_t		*initsh;
3930Sstevel@tonic-gate 	size_t			cookielen;
3940Sstevel@tonic-gate 	size_t			iacklen;
3950Sstevel@tonic-gate 	size_t			ipsctplen;
3960Sstevel@tonic-gate 	size_t			errlen = 0;
3970Sstevel@tonic-gate 	sctp_parm_hdr_t		*cookieph;
3980Sstevel@tonic-gate 	mblk_t			*iackmp;
3990Sstevel@tonic-gate 	uint32_t		itag;
4000Sstevel@tonic-gate 	uint32_t		itsn;
4010Sstevel@tonic-gate 	int64_t			*now;
4020Sstevel@tonic-gate 	int64_t			nowt;
4030Sstevel@tonic-gate 	uint32_t		*lifetime;
4040Sstevel@tonic-gate 	char			*p;
4050Sstevel@tonic-gate 	boolean_t		 isv4;
4060Sstevel@tonic-gate 	int			supp_af;
4070Sstevel@tonic-gate 	uint_t			sctp_options;
4080Sstevel@tonic-gate 	uint32_t		*ttag;
4090Sstevel@tonic-gate 	int			pad;
4100Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
4110Sstevel@tonic-gate 	boolean_t		initcollision = B_FALSE;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
4140Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(initmp->b_rptr) == IPV4_VERSION);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/* Extract the INIT chunk */
4170Sstevel@tonic-gate 	if (isv4) {
4180Sstevel@tonic-gate 		initiph = (ipha_t *)initmp->b_rptr;
4190Sstevel@tonic-gate 		initsh = (sctp_hdr_t *)((char *)initiph +
4200Sstevel@tonic-gate 		    IPH_HDR_LENGTH(initmp->b_rptr));
4210Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr_len;
4220Sstevel@tonic-gate 	} else {
4230Sstevel@tonic-gate 		initip6h = (ip6_t *)initmp->b_rptr;
4240Sstevel@tonic-gate 		initsh = (sctp_hdr_t *)(initip6h + 1);
4250Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr6_len;
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	ASSERT(OK_32PTR(initsh));
4280Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)((char *)(initsh + 1) + sizeof (*iack_ch));
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/* Make sure we like the peer's parameters */
4310Sstevel@tonic-gate 	if (validate_init_params(sctp, ch, init, initmp, NULL, &errmp,
4320Sstevel@tonic-gate 		&supp_af, &sctp_options) == 0) {
4330Sstevel@tonic-gate 		return;
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 	if (errmp != NULL)
4360Sstevel@tonic-gate 		errlen = msgdsize(errmp);
4370Sstevel@tonic-gate 	if (sctp->sctp_family == AF_INET) {
4380Sstevel@tonic-gate 		/*
4390Sstevel@tonic-gate 		 * Irregardless of the supported address in the INIT, v4
4400Sstevel@tonic-gate 		 * must be supported.
4410Sstevel@tonic-gate 		 */
4420Sstevel@tonic-gate 		supp_af = PARM_SUPP_V4;
4430Sstevel@tonic-gate 	} else {
4440Sstevel@tonic-gate 		/*
4450Sstevel@tonic-gate 		 * No supported addresses parameter in INIT.  Assume
4460Sstevel@tonic-gate 		 * both v4 and v6 are supported.
4470Sstevel@tonic-gate 		 */
4480Sstevel@tonic-gate 		if (supp_af == 0) {
4490Sstevel@tonic-gate 			supp_af = PARM_SUPP_V6 | PARM_SUPP_V4;
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_LISTEN) {
4530Sstevel@tonic-gate 		/* normal, expected INIT: generate new vtag and itsn */
4540Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
4550Sstevel@tonic-gate 		if (itag == 0)
4560Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
4570Sstevel@tonic-gate 		itsn = itag + 1;
4580Sstevel@tonic-gate 		itag = htonl(itag);
4590Sstevel@tonic-gate 	} else if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
4600Sstevel@tonic-gate 	    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
4610Sstevel@tonic-gate 		/* init collision; copy vtag and itsn from sctp */
4620Sstevel@tonic-gate 		itag = sctp->sctp_lvtag;
4630Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
4640Sstevel@tonic-gate 		/*
4650Sstevel@tonic-gate 		 * In addition we need to send all the params that was sent
4660Sstevel@tonic-gate 		 * in our INIT chunk. Essentially, it is only the supported
4670Sstevel@tonic-gate 		 * address params that we need to add.
4680Sstevel@tonic-gate 		 */
4690Sstevel@tonic-gate 		initcollision = B_TRUE;
4700Sstevel@tonic-gate 	} else {
4710Sstevel@tonic-gate 		/* peer restart; generate new vtag but keep everything else */
4720Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
4730Sstevel@tonic-gate 		if (itag == 0)
4740Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
4750Sstevel@tonic-gate 		itag = htonl(itag);
4760Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Allocate a mblk for the INIT ACK, consisting of the link layer
4810Sstevel@tonic-gate 	 * header, the IP header, the SCTP common header, and INIT ACK chunk,
4820Sstevel@tonic-gate 	 * and finally the COOKIE parameter.
4830Sstevel@tonic-gate 	 */
4840Sstevel@tonic-gate 	cookielen = SCTP_CALC_COOKIE_LEN(ch);
4850Sstevel@tonic-gate 	iacklen = sizeof (*iack_ch) + sizeof (*iack) + cookielen;
4860Sstevel@tonic-gate 	if (sctp->sctp_send_adaption)
4870Sstevel@tonic-gate 		iacklen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
4880Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
4890Sstevel@tonic-gate 	    sctp->sctp_prsctp_aware && sctp_prsctp_enabled) {
4900Sstevel@tonic-gate 		iacklen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	if (initcollision)
4930Sstevel@tonic-gate 		iacklen += sctp_supaddr_param_len(sctp);
4940Sstevel@tonic-gate 	iacklen += sctp_addr_params_len(sctp, supp_af);
4950Sstevel@tonic-gate 	ipsctplen += sizeof (*iacksh) + iacklen;
4960Sstevel@tonic-gate 	iacklen += errlen;
4970Sstevel@tonic-gate 	if ((pad = ipsctplen % 4) != 0) {
4980Sstevel@tonic-gate 		pad = 4 - pad;
4990Sstevel@tonic-gate 		ipsctplen += pad;
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 	iackmp = allocb(ipsctplen + sctp_wroff_xtra, BPRI_MED);
5020Sstevel@tonic-gate 	if (iackmp == NULL) {
5030Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch),
5040Sstevel@tonic-gate 		    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE);
5050Sstevel@tonic-gate 		return;
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/* Copy in the [imcomplete] IP/SCTP composite header */
5090Sstevel@tonic-gate 	p = (char *)(iackmp->b_rptr + sctp_wroff_xtra);
5100Sstevel@tonic-gate 	iackmp->b_rptr = (uchar_t *)p;
5110Sstevel@tonic-gate 	if (isv4) {
5120Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, p, sctp->sctp_hdr_len);
5130Sstevel@tonic-gate 		iackiph = (ipha_t *)p;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		/* Copy the peer's IP addr */
5160Sstevel@tonic-gate 		iackiph->ipha_dst = initiph->ipha_src;
5170Sstevel@tonic-gate 		iackiph->ipha_src = initiph->ipha_dst;
5180Sstevel@tonic-gate 		iackiph->ipha_length = htons(ipsctplen + errlen);
5190Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr_len);
5200Sstevel@tonic-gate 	} else {
5210Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, p, sctp->sctp_hdr6_len);
5220Sstevel@tonic-gate 		iackip6h = (ip6_t *)p;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		/* Copy the peer's IP addr */
5250Sstevel@tonic-gate 		iackip6h->ip6_dst = initip6h->ip6_src;
5260Sstevel@tonic-gate 		iackip6h->ip6_src = initip6h->ip6_dst;
5270Sstevel@tonic-gate 		iackip6h->ip6_plen = htons(ipsctplen - sizeof (*iackip6h) +
5280Sstevel@tonic-gate 		    errlen);
5290Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr6_len);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 	ASSERT(OK_32PTR(iacksh));
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/* Fill in the holes in the SCTP common header */
5340Sstevel@tonic-gate 	iacksh->sh_sport = initsh->sh_dport;
5350Sstevel@tonic-gate 	iacksh->sh_dport = initsh->sh_sport;
5360Sstevel@tonic-gate 	iacksh->sh_verf = init->sic_inittag;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	/* INIT ACK chunk header */
5390Sstevel@tonic-gate 	iack_ch = (sctp_chunk_hdr_t *)(iacksh + 1);
5400Sstevel@tonic-gate 	iack_ch->sch_id = CHUNK_INIT_ACK;
5410Sstevel@tonic-gate 	iack_ch->sch_flags = 0;
5420Sstevel@tonic-gate 	iack_ch->sch_len = htons(iacklen);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/* The INIT ACK itself */
5450Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iack_ch + 1);
5460Sstevel@tonic-gate 	iack->sic_inittag = itag;	/* already in network byteorder */
5470Sstevel@tonic-gate 	iack->sic_inittsn = htonl(itsn);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	iack->sic_a_rwnd = htonl(sctp->sctp_rwnd);
5500Sstevel@tonic-gate 	/* Advertise what we would want to have as stream #'s */
5510Sstevel@tonic-gate 	iack->sic_outstr = htons(MIN(sctp->sctp_num_ostr,
5520Sstevel@tonic-gate 	    ntohs(init->sic_instr)));
5530Sstevel@tonic-gate 	iack->sic_instr = htons(sctp->sctp_num_istr);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	p = (char *)(iack + 1);
5560Sstevel@tonic-gate 	p += sctp_adaption_code_param(sctp, (uchar_t *)p);
5570Sstevel@tonic-gate 	if (initcollision)
5580Sstevel@tonic-gate 		p += sctp_supaddr_param(sctp, (uchar_t *)p);
5590Sstevel@tonic-gate 	p += sctp_addr_params(sctp, supp_af, (uchar_t *)p);
5600Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
5610Sstevel@tonic-gate 	    sctp->sctp_prsctp_aware && sctp_prsctp_enabled) {
5620Sstevel@tonic-gate 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * Generate and lay in the COOKIE parameter.
5660Sstevel@tonic-gate 	 *
5670Sstevel@tonic-gate 	 * The cookie consists of:
5680Sstevel@tonic-gate 	 * 1. The relative timestamp for the cookie (lbolt64)
5690Sstevel@tonic-gate 	 * 2. The cookie lifetime (uint32_t) in tick
5700Sstevel@tonic-gate 	 * 3. The local tie-tag
5710Sstevel@tonic-gate 	 * 4. The peer tie-tag
5720Sstevel@tonic-gate 	 * 5. Peer's original src, used to confirm the validity of address.
5730Sstevel@tonic-gate 	 * 6. Our INIT ACK chunk, less any parameters
5740Sstevel@tonic-gate 	 * 7. The INIT chunk (may contain parameters)
5750Sstevel@tonic-gate 	 * 8. 128-bit MD5 signature.
5760Sstevel@tonic-gate 	 *
5770Sstevel@tonic-gate 	 * Since the timestamp values will only be evaluated locally, we
5780Sstevel@tonic-gate 	 * don't need to worry about byte-ordering them.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	cookieph = (sctp_parm_hdr_t *)p;
5810Sstevel@tonic-gate 	cookieph->sph_type = htons(PARM_COOKIE);
5820Sstevel@tonic-gate 	cookieph->sph_len = htons(cookielen);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/* timestamp */
5850Sstevel@tonic-gate 	now = (int64_t *)(cookieph + 1);
5860Sstevel@tonic-gate 	nowt = lbolt64;
5870Sstevel@tonic-gate 	bcopy(&nowt, now, sizeof (*now));
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/* cookie lifetime -- need configuration */
5900Sstevel@tonic-gate 	lifetime = (uint32_t *)(now + 1);
5910Sstevel@tonic-gate 	*lifetime = sctp->sctp_cookie_lifetime;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	/* Set the tie-tags */
5940Sstevel@tonic-gate 	ttag = (uint32_t *)(lifetime + 1);
5950Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_COOKIE_WAIT) {
5960Sstevel@tonic-gate 		*ttag = 0;
5970Sstevel@tonic-gate 		ttag++;
5980Sstevel@tonic-gate 		*ttag = 0;
5990Sstevel@tonic-gate 		ttag++;
6000Sstevel@tonic-gate 	} else {
6010Sstevel@tonic-gate 		/* local tie-tag (network byte-order) */
6020Sstevel@tonic-gate 		*ttag = sctp->sctp_lvtag;
6030Sstevel@tonic-gate 		ttag++;
6040Sstevel@tonic-gate 		/* peer tie-tag (network byte-order) */
6050Sstevel@tonic-gate 		*ttag = sctp->sctp_fvtag;
6060Sstevel@tonic-gate 		ttag++;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	/*
6090Sstevel@tonic-gate 	 * Copy in peer's original source address so that we can confirm
6100Sstevel@tonic-gate 	 * the reachability later.
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	p = (char *)ttag;
6130Sstevel@tonic-gate 	if (isv4) {
6140Sstevel@tonic-gate 		in6_addr_t peer_addr;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iackiph->ipha_dst, &peer_addr);
6170Sstevel@tonic-gate 		bcopy(&peer_addr, p, sizeof (in6_addr_t));
6180Sstevel@tonic-gate 	} else {
6190Sstevel@tonic-gate 		bcopy(&iackip6h->ip6_dst, p, sizeof (in6_addr_t));
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 	p += sizeof (in6_addr_t);
6220Sstevel@tonic-gate 	/* Copy in our INIT ACK chunk */
6230Sstevel@tonic-gate 	bcopy(iack, p, sizeof (*iack));
6240Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)p;
6250Sstevel@tonic-gate 	/* Set the # of streams we'll end up using */
6260Sstevel@tonic-gate 	iack->sic_outstr = MIN(sctp->sctp_num_ostr, ntohs(init->sic_instr));
6270Sstevel@tonic-gate 	iack->sic_instr = MIN(sctp->sctp_num_istr, ntohs(init->sic_outstr));
6280Sstevel@tonic-gate 	p += sizeof (*iack);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/* Copy in the peer's INIT chunk */
6310Sstevel@tonic-gate 	bcopy(ch, p, ntohs(ch->sch_len));
6320Sstevel@tonic-gate 	p += ntohs(ch->sch_len);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/*
6350Sstevel@tonic-gate 	 * Calculate the HMAC ICV into the digest slot in buf.
6360Sstevel@tonic-gate 	 * First, generate a new secret if the current secret is
6370Sstevel@tonic-gate 	 * older than the new secret lifetime parameter permits,
6380Sstevel@tonic-gate 	 * copying the current secret to sctp_old_secret.
6390Sstevel@tonic-gate 	 */
6400Sstevel@tonic-gate 	if (sctp_new_secret_interval > 0 &&
6410Sstevel@tonic-gate 	    (sctp->sctp_last_secret_update +
6420Sstevel@tonic-gate 	    MSEC_TO_TICK(sctp_new_secret_interval)) <= nowt) {
6430Sstevel@tonic-gate 		bcopy(sctp->sctp_secret, sctp->sctp_old_secret,
6440Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
6450Sstevel@tonic-gate 		(void) random_get_pseudo_bytes(sctp->sctp_secret,
6460Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
6470Sstevel@tonic-gate 		sctp->sctp_last_secret_update = nowt;
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	hmac_md5((uchar_t *)now, cookielen - sizeof (*cookieph) - 16,
6510Sstevel@tonic-gate 	    (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN, (uchar_t *)p);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	iackmp->b_wptr = iackmp->b_rptr + ipsctplen;
6540Sstevel@tonic-gate 	iackmp->b_cont = errmp;		/*  OK if NULL */
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	/*
6570Sstevel@tonic-gate 	 * Stash the conn ptr info. for IP only as e don't have any
6580Sstevel@tonic-gate 	 * cached IRE.
6590Sstevel@tonic-gate 	 */
6600Sstevel@tonic-gate 	SCTP_STASH_IPINFO(iackmp, (ire_t *)NULL);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/* XXX sctp == sctp_g_q, so using its obchunks is valid */
6630Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_opkts);
6640Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	/* OK to call IP_PUT() here instead of sctp_add_sendq(). */
6670Sstevel@tonic-gate 	CONN_INC_REF(sctp->sctp_connp);
6680Sstevel@tonic-gate 	iackmp->b_flag |= MSGHASREF;
6690Sstevel@tonic-gate 	IP_PUT(iackmp, sctp->sctp_connp, isv4);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate void
6730Sstevel@tonic-gate sctp_send_cookie_ack(sctp_t *sctp)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	sctp_chunk_hdr_t *cach;
6760Sstevel@tonic-gate 	mblk_t *camp;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	camp = sctp_make_mp(sctp, NULL, sizeof (*cach));
6790Sstevel@tonic-gate 	if (camp == NULL) {
6800Sstevel@tonic-gate 		/* XXX should abort, but don't have the inmp anymore */
6810Sstevel@tonic-gate 		return;
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	cach = (sctp_chunk_hdr_t *)camp->b_wptr;
6850Sstevel@tonic-gate 	camp->b_wptr = (uchar_t *)(cach + 1);
6860Sstevel@tonic-gate 	cach->sch_id = CHUNK_COOKIE_ACK;
6870Sstevel@tonic-gate 	cach->sch_flags = 0;
6880Sstevel@tonic-gate 	cach->sch_len = htons(sizeof (*cach));
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	sctp_set_iplen(sctp, camp);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	sctp_add_sendq(sctp, camp);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate static int
6980Sstevel@tonic-gate sctp_find_al_ind(sctp_parm_hdr_t *sph, ssize_t len, uint32_t *adaption_code)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (len < sizeof (*sph))
7020Sstevel@tonic-gate 		return (-1);
7030Sstevel@tonic-gate 	while (sph != NULL) {
7040Sstevel@tonic-gate 		if (sph->sph_type == htons(PARM_ADAPT_LAYER_IND) &&
7050Sstevel@tonic-gate 		    ntohs(sph->sph_len) >= (sizeof (*sph) +
7060Sstevel@tonic-gate 			sizeof (uint32_t))) {
7070Sstevel@tonic-gate 			*adaption_code = *(uint32_t *)(sph + 1);
7080Sstevel@tonic-gate 			return (0);
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 		sph = sctp_next_parm(sph, &len);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	return (-1);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate void
7160Sstevel@tonic-gate sctp_send_cookie_echo(sctp_t *sctp, sctp_chunk_hdr_t *iackch, mblk_t *iackmp)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	mblk_t			*cemp;
7190Sstevel@tonic-gate 	mblk_t			*mp = NULL;
7200Sstevel@tonic-gate 	mblk_t			*head;
7210Sstevel@tonic-gate 	mblk_t			*meta;
7220Sstevel@tonic-gate 	sctp_faddr_t		*fp;
7230Sstevel@tonic-gate 	sctp_chunk_hdr_t	*cech;
7240Sstevel@tonic-gate 	sctp_init_chunk_t	 *iack;
7250Sstevel@tonic-gate 	int32_t			cansend;
7260Sstevel@tonic-gate 	int32_t			seglen;
7270Sstevel@tonic-gate 	size_t			ceclen;
7280Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
7290Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
7300Sstevel@tonic-gate 	sctp_tf_t		*tf;
7310Sstevel@tonic-gate 	int			pad;
7320Sstevel@tonic-gate 	int			hdrlen;
7330Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
7340Sstevel@tonic-gate 	uint_t			sctp_options;
7350Sstevel@tonic-gate 	int			error;
7360Sstevel@tonic-gate 	uint16_t		old_num_str;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iackch + 1);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	cph = NULL;
7410Sstevel@tonic-gate 	if (validate_init_params(sctp, iackch, iack, iackmp, &cph, &errmp,
7420Sstevel@tonic-gate 	    &pad, &sctp_options) == 0) { /* result in 'pad' ignored */
7430Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpAborted);
7440Sstevel@tonic-gate 		sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
7450Sstevel@tonic-gate 		sctp_clean_death(sctp, ECONNABORTED);
7460Sstevel@tonic-gate 		return;
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 	ASSERT(cph != NULL);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/* Got a cookie to echo back; allocate an mblk */
7530Sstevel@tonic-gate 	ceclen = sizeof (*cech) + ntohs(cph->sph_len) - sizeof (*cph);
7540Sstevel@tonic-gate 	if ((pad = ceclen & (SCTP_ALIGN - 1)) != 0)
7550Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if (IPH_HDR_VERSION(iackmp->b_rptr) == IPV4_VERSION)
7580Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
7590Sstevel@tonic-gate 	else
7600Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	cemp = allocb(sctp_wroff_xtra + hdrlen + ceclen + pad, BPRI_MED);
7630Sstevel@tonic-gate 	if (cemp == NULL) {
7640Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
7650Sstevel@tonic-gate 		    sctp->sctp_current->rto);
7660Sstevel@tonic-gate 		if (errmp != NULL)
7670Sstevel@tonic-gate 			freeb(errmp);
7680Sstevel@tonic-gate 		return;
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	cemp->b_rptr += (sctp_wroff_xtra + hdrlen);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	/* Process the INIT ACK */
7730Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = iack->sic_inittag;
7740Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = iack->sic_inittag;
7750Sstevel@tonic-gate 	sctp->sctp_fvtag = iack->sic_inittag;
7760Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(iack->sic_inittsn);
7770Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
7780Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
7790Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(iack->sic_a_rwnd);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Populate sctp with addresses given in the INIT ACK or IP header.
7830Sstevel@tonic-gate 	 * Need to set the df bit in the current fp as it has been cleared
7840Sstevel@tonic-gate 	 * in sctp_connect().
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	sctp->sctp_current->df = B_TRUE;
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * Since IP uses this info during the fanout process, we need to hold
7890Sstevel@tonic-gate 	 * the lock for this hash line while performing this operation.
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	/* XXX sctp_conn_fanout + SCTP_CONN_HASH(sctp->sctp_ports); */
7920Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
7930Sstevel@tonic-gate 	tf = sctp->sctp_conn_tfp;
7940Sstevel@tonic-gate 	/* sctp isn't a listener so only need to hold conn fanout lock */
7950Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
7960Sstevel@tonic-gate 	if (sctp_get_addrparams(sctp, NULL, iackmp, iackch, NULL) != 0) {
7970Sstevel@tonic-gate 		mutex_exit(&tf->tf_lock);
7980Sstevel@tonic-gate 		freeb(cemp);
7990Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
8000Sstevel@tonic-gate 		    sctp->sctp_current->rto);
8010Sstevel@tonic-gate 		if (errmp != NULL)
8020Sstevel@tonic-gate 			freeb(errmp);
8030Sstevel@tonic-gate 		return;
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	fp = sctp->sctp_current;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * There could be a case when we get an INIT-ACK again, if the INIT
8110Sstevel@tonic-gate 	 * is re-transmitted, for e.g., which means we would have already
8120Sstevel@tonic-gate 	 * allocated this resource earlier (also for sctp_instr). In this
8130Sstevel@tonic-gate 	 * case we check and re-allocate, if necessary.
8140Sstevel@tonic-gate 	 */
8150Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_ostr;
8160Sstevel@tonic-gate 	if (ntohs(iack->sic_instr) < sctp->sctp_num_ostr)
8170Sstevel@tonic-gate 		sctp->sctp_num_ostr = ntohs(iack->sic_instr);
8180Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
8190Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
8200Sstevel@tonic-gate 		    sctp->sctp_num_ostr, KM_NOSLEEP);
8210Sstevel@tonic-gate 	} else {
8220Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
8230Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_ostr) {
8240Sstevel@tonic-gate 			kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
8250Sstevel@tonic-gate 			    old_num_str);
8260Sstevel@tonic-gate 			sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
8270Sstevel@tonic-gate 			    sctp->sctp_num_ostr, KM_NOSLEEP);
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
8310Sstevel@tonic-gate 		freeb(cemp);
8320Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
8330Sstevel@tonic-gate 		if (errmp != NULL)
8340Sstevel@tonic-gate 			freeb(errmp);
8350Sstevel@tonic-gate 		return;
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	/*
8390Sstevel@tonic-gate 	 * Allocate the in stream tracking array. Comments for sctp_ostrcntrs
8400Sstevel@tonic-gate 	 * hold here too.
8410Sstevel@tonic-gate 	 */
8420Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_istr;
8430Sstevel@tonic-gate 	if (ntohs(iack->sic_outstr) < sctp->sctp_num_istr)
8440Sstevel@tonic-gate 		sctp->sctp_num_istr = ntohs(iack->sic_outstr);
8450Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
8460Sstevel@tonic-gate 		sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
8470Sstevel@tonic-gate 		    sctp->sctp_num_istr, KM_NOSLEEP);
8480Sstevel@tonic-gate 	} else {
8490Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
8500Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_istr) {
8510Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
8520Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * old_num_str);
8530Sstevel@tonic-gate 			sctp->sctp_instr = kmem_zalloc(
8540Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr,
8550Sstevel@tonic-gate 			    KM_NOSLEEP);
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
8590Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
8600Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
8610Sstevel@tonic-gate 		freeb(cemp);
8620Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
8630Sstevel@tonic-gate 		if (errmp != NULL)
8640Sstevel@tonic-gate 			freeb(errmp);
8650Sstevel@tonic-gate 		return;
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	if (!(sctp_options & SCTP_PRSCTP_OPTION) && sctp->sctp_prsctp_aware)
8690Sstevel@tonic-gate 		sctp->sctp_prsctp_aware = B_FALSE;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (sctp_find_al_ind((sctp_parm_hdr_t *)(iack + 1),
8720Sstevel@tonic-gate 		ntohs(iackch->sch_len) - (sizeof (*iackch) + sizeof (*iack)),
8730Sstevel@tonic-gate 		&sctp->sctp_rx_adaption_code) == 0) {
8740Sstevel@tonic-gate 		sctp->sctp_recv_adaption = 1;
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	cech = (sctp_chunk_hdr_t *)cemp->b_rptr;
8780Sstevel@tonic-gate 	ASSERT(OK_32PTR(cech));
8790Sstevel@tonic-gate 	cech->sch_id = CHUNK_COOKIE;
8800Sstevel@tonic-gate 	cech->sch_flags = 0;
8810Sstevel@tonic-gate 	cech->sch_len = htons(ceclen);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/* Copy the cookie (less the parm hdr) to the chunk */
8840Sstevel@tonic-gate 	bcopy(cph + 1, cech + 1, ceclen - sizeof (*cph));
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	cemp->b_wptr = cemp->b_rptr + ceclen;
8870Sstevel@tonic-gate 
888*252Svi117747 	if (sctp->sctp_unsent > 0) {
8890Sstevel@tonic-gate 		sctp_msg_hdr_t	*smh;
8900Sstevel@tonic-gate 		mblk_t		*prev = NULL;
8910Sstevel@tonic-gate 		uint32_t	unsent = 0;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 		mp = sctp->sctp_xmit_unsent;
8940Sstevel@tonic-gate 		do {
8950Sstevel@tonic-gate 			smh = (sctp_msg_hdr_t *)mp->b_rptr;
8960Sstevel@tonic-gate 			if (smh->smh_sid >= sctp->sctp_num_ostr) {
8970Sstevel@tonic-gate 				unsent += smh->smh_msglen;
8980Sstevel@tonic-gate 				if (prev != NULL)
8990Sstevel@tonic-gate 					prev->b_next = mp->b_next;
9000Sstevel@tonic-gate 				else
9010Sstevel@tonic-gate 					sctp->sctp_xmit_unsent = mp->b_next;
9020Sstevel@tonic-gate 				mp->b_next = NULL;
9030Sstevel@tonic-gate 				sctp_sendfail_event(sctp, mp, SCTP_ERR_BAD_SID,
9040Sstevel@tonic-gate 				    B_FALSE);
9050Sstevel@tonic-gate 				if (prev != NULL)
9060Sstevel@tonic-gate 					mp = prev->b_next;
9070Sstevel@tonic-gate 				else
9080Sstevel@tonic-gate 					mp = sctp->sctp_xmit_unsent;
9090Sstevel@tonic-gate 			} else {
9100Sstevel@tonic-gate 				prev = mp;
9110Sstevel@tonic-gate 				mp = mp->b_next;
9120Sstevel@tonic-gate 			}
9130Sstevel@tonic-gate 		} while (mp != NULL);
9140Sstevel@tonic-gate 		if (unsent > 0) {
9150Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= unsent);
9160Sstevel@tonic-gate 			sctp->sctp_unsent -= unsent;
9170Sstevel@tonic-gate 			/*
9180Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
9190Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
9200Sstevel@tonic-gate 			 * This is not necessary, but doesn't harm, we
9210Sstevel@tonic-gate 			 * just use unsent instead of sent-unack'ed +
9220Sstevel@tonic-gate 			 * unsent, since there won't be any sent-unack'ed
9230Sstevel@tonic-gate 			 * here.
9240Sstevel@tonic-gate 			 */
9250Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
9260Sstevel@tonic-gate 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
9270Sstevel@tonic-gate 				    sctp->sctp_unsent);
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 		if (sctp->sctp_xmit_unsent == NULL)
9310Sstevel@tonic-gate 			sctp->sctp_xmit_unsent_tail = NULL;
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 	ceclen += pad;
9340Sstevel@tonic-gate 	cansend = MIN(sctp->sctp_unsent, sctp->sctp_frwnd);
9350Sstevel@tonic-gate 	meta = sctp_get_msg_to_send(sctp, &mp, NULL, &error, ceclen,
9360Sstevel@tonic-gate 	    cansend,  NULL);
9370Sstevel@tonic-gate 	/*
9380Sstevel@tonic-gate 	 * The error cannot be anything else since we could have an non-zero
9390Sstevel@tonic-gate 	 * error only if sctp_get_msg_to_send() tries to send a Forward
9400Sstevel@tonic-gate 	 * TSN which will not happen here.
9410Sstevel@tonic-gate 	 */
9420Sstevel@tonic-gate 	ASSERT(error == 0);
9430Sstevel@tonic-gate 	if (meta == NULL)
9440Sstevel@tonic-gate 		goto sendcookie;
9450Sstevel@tonic-gate 	sctp->sctp_xmit_tail = meta;
9460Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
9470Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
9480Sstevel@tonic-gate 	if ((ceclen + seglen) > fp->sfa_pmss ||
9490Sstevel@tonic-gate 	    (seglen - sizeof (*sdc)) > cansend) {
9500Sstevel@tonic-gate 		goto sendcookie;
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 	/* OK, if this fails */
9530Sstevel@tonic-gate 	cemp->b_cont = dupmsg(mp);
9540Sstevel@tonic-gate sendcookie:
955*252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, cemp, 0, NULL);
956*252Svi117747 	if (head == NULL) {
957*252Svi117747 		freemsg(cemp);
958*252Svi117747 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
959*252Svi117747 		if (errmp != NULL)
960*252Svi117747 			freeb(errmp);
961*252Svi117747 		return;
962*252Svi117747 	}
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * Even if cookie-echo exceeds MTU for one of the hops, it'll
9650Sstevel@tonic-gate 	 * have a chance of getting there.
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	if (fp->isv4) {
9680Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
9690Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	sctp->sctp_cookie_mp = dupmsg(head);
9740Sstevel@tonic-gate 	/* Don't bundle, we will just resend init if this cookie is lost. */
9750Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp == NULL) {
9760Sstevel@tonic-gate 		if (cemp->b_cont != NULL) {
9770Sstevel@tonic-gate 			freemsg(cemp->b_cont);
9780Sstevel@tonic-gate 			cemp->b_cont = NULL;
9790Sstevel@tonic-gate 		}
9800Sstevel@tonic-gate 	} else if (cemp->b_cont != NULL) {
9810Sstevel@tonic-gate 		ASSERT(mp != NULL && mp == meta->b_cont);
9820Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(cemp->b_cont);
9830Sstevel@tonic-gate 		cemp->b_wptr += pad;
9840Sstevel@tonic-gate 		seglen -= sizeof (*sdc);
9850Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, seglen, meta);
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 	if (errmp != NULL)
9880Sstevel@tonic-gate 		linkb(head, errmp);
9890Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_COOKIE_ECHOED;
9900Sstevel@tonic-gate 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
9930Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
9940Sstevel@tonic-gate }
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate int
9970Sstevel@tonic-gate sctp_process_cookie(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *cmp,
9980Sstevel@tonic-gate     sctp_init_chunk_t **iackpp, sctp_hdr_t *insctph, int *recv_adaption,
9990Sstevel@tonic-gate     in6_addr_t *peer_addr)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	int32_t			clen;
10020Sstevel@tonic-gate 	size_t			initplen;
10030Sstevel@tonic-gate 	uchar_t			*p;
10040Sstevel@tonic-gate 	uchar_t			*given_hash;
10050Sstevel@tonic-gate 	uchar_t			needed_hash[16];
10060Sstevel@tonic-gate 	int64_t			ts;
10070Sstevel@tonic-gate 	int64_t			diff;
10080Sstevel@tonic-gate 	uint32_t		*lt;
10090Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
10100Sstevel@tonic-gate 	sctp_chunk_hdr_t	*initch;
10110Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
10120Sstevel@tonic-gate 	uint32_t		*lttag;
10130Sstevel@tonic-gate 	uint32_t		*fttag;
10140Sstevel@tonic-gate 	uint32_t		ports;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
10170Sstevel@tonic-gate 	/* Verify the ICV */
10180Sstevel@tonic-gate 	clen = ntohs(ch->sch_len) - sizeof (*ch) - 16;
10190Sstevel@tonic-gate 	if (clen < 0) {
10200Sstevel@tonic-gate 		dprint(1, ("invalid cookie chunk length %d\n",
10210Sstevel@tonic-gate 		    ntohs(ch->sch_len)));
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 		return (-1);
10240Sstevel@tonic-gate 	}
10250Sstevel@tonic-gate 	p = (uchar_t *)(ch + 1);
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	hmac_md5(p, clen, (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN,
10280Sstevel@tonic-gate 	    needed_hash);
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/* The given hash follows the cookie data */
10310Sstevel@tonic-gate 	given_hash = p + clen;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if (bcmp(given_hash, needed_hash, 16) != 0) {
10340Sstevel@tonic-gate 		/* The secret may have changed; try the old secret */
10350Sstevel@tonic-gate 		hmac_md5(p, clen, (uchar_t *)sctp->sctp_old_secret,
10360Sstevel@tonic-gate 		    SCTP_SECRET_LEN, needed_hash);
10370Sstevel@tonic-gate 		if (bcmp(given_hash, needed_hash, 16) != 0) {
10380Sstevel@tonic-gate 			return (-1);
10390Sstevel@tonic-gate 		}
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/* Timestamp is int64_t, and we only guarantee 32-bit alignment */
10430Sstevel@tonic-gate 	bcopy(p, &ts, sizeof (ts));
10440Sstevel@tonic-gate 	/* Cookie life time, int32_t */
10450Sstevel@tonic-gate 	lt = (uint32_t *)(p + sizeof (ts));
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10480Sstevel@tonic-gate 	 * To quote PRC, "this is our baby", so let's continue.
10490Sstevel@tonic-gate 	 * We need to pull out the encapsulated INIT ACK and
10500Sstevel@tonic-gate 	 * INIT chunks. Note that we don't process these until
10510Sstevel@tonic-gate 	 * we have verified the timestamp, but we need them before
10520Sstevel@tonic-gate 	 * processing the timestamp since if the time check fails,
10530Sstevel@tonic-gate 	 * we need to get the verification tag from the INIT in order
10540Sstevel@tonic-gate 	 * to send a stale cookie error.
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	lttag = (uint32_t *)(lt + 1);
10570Sstevel@tonic-gate 	fttag = lttag + 1;
10580Sstevel@tonic-gate 	if (peer_addr != NULL)
10590Sstevel@tonic-gate 		bcopy(fttag + 1, peer_addr, sizeof (in6_addr_t));
10600Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)((char *)(fttag + 1) + sizeof (in6_addr_t));
10610Sstevel@tonic-gate 	initch = (sctp_chunk_hdr_t *)(iack + 1);
10620Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(initch + 1);
10630Sstevel@tonic-gate 	initplen = ntohs(initch->sch_len) - (sizeof (*init) + sizeof (*initch));
10640Sstevel@tonic-gate 	*iackpp = iack;
10650Sstevel@tonic-gate 	*recv_adaption = 0;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	/* Check the timestamp */
10680Sstevel@tonic-gate 	diff = lbolt64 - ts;
10690Sstevel@tonic-gate 	if (diff > *lt && (init->sic_inittag != sctp->sctp_fvtag ||
10700Sstevel@tonic-gate 	    iack->sic_inittag != sctp->sctp_lvtag)) {
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 		uint32_t staleness;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 		staleness = TICK_TO_USEC(diff);
10750Sstevel@tonic-gate 		staleness = htonl(staleness);
10760Sstevel@tonic-gate 		sctp_send_abort(sctp, init->sic_inittag, SCTP_ERR_STALE_COOKIE,
10770Sstevel@tonic-gate 		    (char *)&staleness, sizeof (staleness), cmp, 1, B_FALSE);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		dprint(1, ("stale cookie %d\n", staleness));
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		return (-1);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/* Check for attack by adding addresses to a restart */
10850Sstevel@tonic-gate 	bcopy(insctph, &ports, sizeof (ports));
10860Sstevel@tonic-gate 	if (sctp_secure_restart_check(cmp, initch, ports, KM_NOSLEEP) != 1) {
10870Sstevel@tonic-gate 		return (-1);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	/* Look for adaptation code if there any parms in the INIT chunk */
10910Sstevel@tonic-gate 	if ((initplen >= sizeof (sctp_parm_hdr_t)) &&
10920Sstevel@tonic-gate 	    (sctp_find_al_ind((sctp_parm_hdr_t *)(init + 1), initplen,
10930Sstevel@tonic-gate 	    &sctp->sctp_rx_adaption_code) == 0)) {
10940Sstevel@tonic-gate 		*recv_adaption = 1;
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/* Examine tie-tags */
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_WAIT) {
11000Sstevel@tonic-gate 		if (sctp->sctp_state == SCTPS_ESTABLISHED &&
11010Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
11020Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag &&
11030Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 			dprint(1, ("duplicate cookie from %x:%x:%x:%x (%d)\n",
11060Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
11070Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
11080Sstevel@tonic-gate 			return (-1);
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		if (init->sic_inittag != sctp->sctp_fvtag &&
11120Sstevel@tonic-gate 		    iack->sic_inittag != sctp->sctp_lvtag &&
11130Sstevel@tonic-gate 		    *fttag == sctp->sctp_fvtag &&
11140Sstevel@tonic-gate 		    *lttag == sctp->sctp_lvtag) {
11150Sstevel@tonic-gate 			int i;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 			/* Section 5.2.4 case A: restart */
11180Sstevel@tonic-gate 			sctp->sctp_fvtag = init->sic_inittag;
11190Sstevel@tonic-gate 			sctp->sctp_lvtag = iack->sic_inittag;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 			sctp->sctp_sctph->sh_verf = init->sic_inittag;
11220Sstevel@tonic-gate 			sctp->sctp_sctph6->sh_verf = init->sic_inittag;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 			sctp->sctp_ftsn = ntohl(init->sic_inittsn);
11250Sstevel@tonic-gate 			sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
11260Sstevel@tonic-gate 			sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
11270Sstevel@tonic-gate 			sctp->sctp_fcsn = sctp->sctp_lastacked;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
11300Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
11310Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
11320Sstevel@tonic-gate 			}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 			dprint(1, ("sctp peer %x:%x:%x:%x (%d) restarted\n",
11350Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
11360Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
11370Sstevel@tonic-gate 			/* reset parameters */
11380Sstevel@tonic-gate 			sctp_congest_reset(sctp);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 			/* reset stream bookkeeping */
11410Sstevel@tonic-gate 			sctp_instream_cleanup(sctp, B_FALSE);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 			sctp->sctp_istr_nmsgs = 0;
11440Sstevel@tonic-gate 			sctp->sctp_rxqueued = 0;
11450Sstevel@tonic-gate 			for (i = 0; i < sctp->sctp_num_ostr; i++) {
11460Sstevel@tonic-gate 				sctp->sctp_ostrcntrs[i] = 0;
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 			/* XXX flush xmit_list? */
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 			return (0);
11510Sstevel@tonic-gate 		} else if (init->sic_inittag != sctp->sctp_fvtag &&
11520Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 			/* Section 5.2.4 case B: INIT collision */
11550Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
11560Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
11570Sstevel@tonic-gate 					return (-1);	/* Drop? */
11580Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
11590Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
11600Sstevel@tonic-gate 			}
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 			dprint(1, ("init collision with %x:%x:%x:%x (%d)\n",
11630Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
11640Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 			return (0);
11670Sstevel@tonic-gate 		} else if (iack->sic_inittag != sctp->sctp_lvtag &&
11680Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
11690Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 			/* Section 5.2.4 case C: late COOKIE */
11720Sstevel@tonic-gate 			dprint(1, ("late cookie from %x:%x:%x:%x (%d)\n",
11730Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
11740Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
11750Sstevel@tonic-gate 			return (-1);
11760Sstevel@tonic-gate 		} else if (init->sic_inittag == sctp->sctp_fvtag &&
11770Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			/*
11800Sstevel@tonic-gate 			 * Section 5.2.4 case D: COOKIE ECHO retransmit
11810Sstevel@tonic-gate 			 * Don't check cookie lifetime
11820Sstevel@tonic-gate 			 */
11830Sstevel@tonic-gate 			dprint(1, ("cookie tags match from %x:%x:%x:%x (%d)\n",
11840Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
11850Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
11860Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
11870Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
11880Sstevel@tonic-gate 					return (-1);	/* Drop? */
11890Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
11900Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
11910Sstevel@tonic-gate 			}
11920Sstevel@tonic-gate 			return (0);
11930Sstevel@tonic-gate 		} else {
11940Sstevel@tonic-gate 			/* unrecognized case -- silently drop it */
11950Sstevel@tonic-gate 			return (-1);
11960Sstevel@tonic-gate 		}
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	return (0);
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate /*
12030Sstevel@tonic-gate  * Similar to ip_fanout_sctp, except that the src addr(s) are drawn
12040Sstevel@tonic-gate  * from address parameters in an INIT ACK's address list. This
12050Sstevel@tonic-gate  * function is used when an INIT ACK is received but IP's fanout
12060Sstevel@tonic-gate  * function could not find a sctp via the normal lookup routine.
12070Sstevel@tonic-gate  * This can happen when a host sends an INIT ACK from a different
12080Sstevel@tonic-gate  * address than the INIT was sent to.
12090Sstevel@tonic-gate  *
12100Sstevel@tonic-gate  * Returns the sctp_t if found, or NULL if not found.
12110Sstevel@tonic-gate  */
12120Sstevel@tonic-gate sctp_t *
12130Sstevel@tonic-gate sctp_addrlist2sctp(mblk_t *mp, sctp_hdr_t *sctph, sctp_chunk_hdr_t *ich,
12140Sstevel@tonic-gate     uint_t ipif_seqid, zoneid_t zoneid)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	int isv4;
12170Sstevel@tonic-gate 	ipha_t *iph;
12180Sstevel@tonic-gate 	ip6_t *ip6h;
12190Sstevel@tonic-gate 	in6_addr_t dst;
12200Sstevel@tonic-gate 	in6_addr_t src;
12210Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
12220Sstevel@tonic-gate 	ssize_t remaining;
12230Sstevel@tonic-gate 	sctp_init_chunk_t *iack;
12240Sstevel@tonic-gate 	uint32_t ports;
12250Sstevel@tonic-gate 	sctp_t *sctp = NULL;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	ASSERT(ich->sch_id == CHUNK_INIT_ACK);
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
12300Sstevel@tonic-gate 	if (isv4) {
12310Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
12320Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, &dst);
12330Sstevel@tonic-gate 	} else {
12340Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
12350Sstevel@tonic-gate 		dst = ip6h->ip6_dst;
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	ports = *(uint32_t *)sctph;
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	dprint(1, ("sctp_addrlist2sctp: ports=%u, dst = %x:%x:%x:%x\n",
12410Sstevel@tonic-gate 	    ports, SCTP_PRINTADDR(dst)));
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/* pull out any address parameters */
12440Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) - sizeof (*iack);
12450Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
12460Sstevel@tonic-gate 		return (NULL);
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(ich + 1);
12500Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(iack + 1);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	while (ph != NULL) {
12530Sstevel@tonic-gate 		/*
12540Sstevel@tonic-gate 		 * params have been put in host byteorder by
12550Sstevel@tonic-gate 		 * sctp_check_input()
12560Sstevel@tonic-gate 		 */
12570Sstevel@tonic-gate 		if (ph->sph_type == PARM_ADDR4) {
12580Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED((struct in_addr *)(ph + 1),
12590Sstevel@tonic-gate 			    &src);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 			sctp = sctp_conn_match(&src, &dst, ports, ipif_seqid,
12620Sstevel@tonic-gate 			    zoneid);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 			dprint(1,
12650Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
12660Sstevel@tonic-gate 			    SCTP_PRINTADDR(src), sctp));
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 			if (sctp != NULL) {
12700Sstevel@tonic-gate 				return (sctp);
12710Sstevel@tonic-gate 			}
12720Sstevel@tonic-gate 		} else if (ph->sph_type == PARM_ADDR6) {
12730Sstevel@tonic-gate 			src = *(in6_addr_t *)(ph + 1);
12740Sstevel@tonic-gate 			sctp = sctp_conn_match(&src, &dst, ports, ipif_seqid,
12750Sstevel@tonic-gate 			    zoneid);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 			dprint(1,
12780Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
12790Sstevel@tonic-gate 			    SCTP_PRINTADDR(src), sctp));
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 			if (sctp != NULL) {
12820Sstevel@tonic-gate 				return (sctp);
12830Sstevel@tonic-gate 			}
12840Sstevel@tonic-gate 		}
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	return (NULL);
12900Sstevel@tonic-gate }
1291