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
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/stream.h>
317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
327c478bd9Sstevel@tonic-gate #include <sys/md5.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
357c478bd9Sstevel@tonic-gate #include <sys/random.h>
36*45916cd2Sjpk #include <sys/tsol/tnet.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <netinet/in.h>
397c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <inet/common.h>
427c478bd9Sstevel@tonic-gate #include <inet/ip.h>
437c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
447c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
457c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
467c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
477c478bd9Sstevel@tonic-gate 
481d8c4025Svi /*
491d8c4025Svi  * Helper function for SunCluster (PSARC/2005/602) to get the original source
501d8c4025Svi  * address from the COOKIE
511d8c4025Svi  */
521d8c4025Svi int cl_sctp_cookie_paddr(sctp_chunk_hdr_t *, in6_addr_t *);
531d8c4025Svi 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * From RFC 2104. This should probably go into libmd5 (and while
567c478bd9Sstevel@tonic-gate  * we're at it, maybe we should make a libdigest so we can later
577c478bd9Sstevel@tonic-gate  * add SHA1 and others, esp. since some weaknesses have been found
587c478bd9Sstevel@tonic-gate  * with MD5).
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * text		IN			pointer to data stream
617c478bd9Sstevel@tonic-gate  * text_len	IN			length of data stream
627c478bd9Sstevel@tonic-gate  * key		IN			pointer to authentication key
637c478bd9Sstevel@tonic-gate  * key_len	IN			length of authentication key
647c478bd9Sstevel@tonic-gate  * digest	OUT			caller digest to be filled in
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static void
677c478bd9Sstevel@tonic-gate hmac_md5(uchar_t *text, size_t text_len, uchar_t *key, size_t key_len,
687c478bd9Sstevel@tonic-gate     uchar_t *digest)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	MD5_CTX context;
717c478bd9Sstevel@tonic-gate 	uchar_t k_ipad[65];	/* inner padding - key XORd with ipad */
727c478bd9Sstevel@tonic-gate 	uchar_t k_opad[65];	/* outer padding - key XORd with opad */
737c478bd9Sstevel@tonic-gate 	uchar_t tk[16];
747c478bd9Sstevel@tonic-gate 	int i;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* if key is longer than 64 bytes reset it to key=MD5(key) */
777c478bd9Sstevel@tonic-gate 	if (key_len > 64) {
787c478bd9Sstevel@tonic-gate 		MD5_CTX tctx;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		MD5Init(&tctx);
817c478bd9Sstevel@tonic-gate 		MD5Update(&tctx, key, key_len);
827c478bd9Sstevel@tonic-gate 		MD5Final(tk, &tctx);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		key = tk;
857c478bd9Sstevel@tonic-gate 		key_len = 16;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * the HMAC_MD5 transform looks like:
907c478bd9Sstevel@tonic-gate 	 *
917c478bd9Sstevel@tonic-gate 	 * MD5(K XOR opad, MD5(K XOR ipad, text))
927c478bd9Sstevel@tonic-gate 	 *
937c478bd9Sstevel@tonic-gate 	 * where K is an n byte key
947c478bd9Sstevel@tonic-gate 	 * ipad is the byte 0x36 repeated 64 times
957c478bd9Sstevel@tonic-gate 	 * opad is the byte 0x5c repeated 64 times
967c478bd9Sstevel@tonic-gate 	 * and text is the data being protected
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/* start out by storing key in pads */
1007c478bd9Sstevel@tonic-gate 	bzero(k_ipad, sizeof (k_ipad));
1017c478bd9Sstevel@tonic-gate 	bzero(k_opad, sizeof (k_opad));
1027c478bd9Sstevel@tonic-gate 	bcopy(key, k_ipad, key_len);
1037c478bd9Sstevel@tonic-gate 	bcopy(key, k_opad, key_len);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* XOR key with ipad and opad values */
1067c478bd9Sstevel@tonic-gate 	for (i = 0; i < 64; i++) {
1077c478bd9Sstevel@tonic-gate 		k_ipad[i] ^= 0x36;
1087c478bd9Sstevel@tonic-gate 		k_opad[i] ^= 0x5c;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * perform inner MD5
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	MD5Init(&context);			/* init context for 1st */
1147c478bd9Sstevel@tonic-gate 						/* pass */
1157c478bd9Sstevel@tonic-gate 	MD5Update(&context, k_ipad, 64);	/* start with inner pad */
1167c478bd9Sstevel@tonic-gate 	MD5Update(&context, text, text_len);	/* then text of datagram */
1177c478bd9Sstevel@tonic-gate 	MD5Final(digest, &context);		/* finish up 1st pass */
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * perform outer MD5
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	MD5Init(&context);			/* init context for 2nd */
1227c478bd9Sstevel@tonic-gate 						/* pass */
1237c478bd9Sstevel@tonic-gate 	MD5Update(&context, k_opad, 64);	/* start with outer pad */
1247c478bd9Sstevel@tonic-gate 	MD5Update(&context, digest, 16);	/* then results of 1st */
1257c478bd9Sstevel@tonic-gate 						/* hash */
1267c478bd9Sstevel@tonic-gate 	MD5Final(digest, &context);		/* finish up 2nd pass */
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * If inmp is non-NULL, and we need to abort, it will use the IP/SCTP
1317c478bd9Sstevel@tonic-gate  * info in initmp to send the abort. Otherwise, no abort will be sent.
1327c478bd9Sstevel@tonic-gate  * If errmp is non-NULL, a chain of unrecognized parameters will
1337c478bd9Sstevel@tonic-gate  * be created and returned via *errmp.
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  * Returns 1 if the parameters are OK (or there are no parameters), or
1367c478bd9Sstevel@tonic-gate  * 0 if not.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static int
1397c478bd9Sstevel@tonic-gate validate_init_params(sctp_t *sctp, sctp_chunk_hdr_t *ch,
1407c478bd9Sstevel@tonic-gate     sctp_init_chunk_t *init, mblk_t *inmp, sctp_parm_hdr_t **want_cookie,
1417c478bd9Sstevel@tonic-gate     mblk_t **errmp, int *supp_af, uint_t *sctp_options)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
1447c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*ic;
1457c478bd9Sstevel@tonic-gate 	ssize_t			remaining;
1467c478bd9Sstevel@tonic-gate 	uint16_t		serror = 0;
1477c478bd9Sstevel@tonic-gate 	char			*details = NULL;
1487c478bd9Sstevel@tonic-gate 	size_t			errlen = 0;
1497c478bd9Sstevel@tonic-gate 	boolean_t		got_cookie = B_FALSE;
1507c478bd9Sstevel@tonic-gate 	uint16_t		ptype;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (sctp_options != NULL)
1537c478bd9Sstevel@tonic-gate 		*sctp_options = 0;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* First validate stream parameters */
1567c478bd9Sstevel@tonic-gate 	if (init->sic_instr == 0 || init->sic_outstr == 0) {
1577c478bd9Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1587c478bd9Sstevel@tonic-gate 		dprint(1,
1597c478bd9Sstevel@tonic-gate 		    ("validate_init_params: bad sid, is=%d os=%d\n",
1607c478bd9Sstevel@tonic-gate 			htons(init->sic_instr), htons(init->sic_outstr)));
1617c478bd9Sstevel@tonic-gate 		goto abort;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	if (ntohl(init->sic_inittag) == 0) {
1647c478bd9Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1657c478bd9Sstevel@tonic-gate 		dprint(1, ("validate_init_params: inittag = 0\n"));
1667c478bd9Sstevel@tonic-gate 		goto abort;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	remaining = ntohs(ch->sch_len) - sizeof (*ch);
1707c478bd9Sstevel@tonic-gate 	ic = (sctp_init_chunk_t *)(ch + 1);
1717c478bd9Sstevel@tonic-gate 	remaining -= sizeof (*ic);
1727c478bd9Sstevel@tonic-gate 	if (remaining < sizeof (*cph)) {
1737c478bd9Sstevel@tonic-gate 		/* Nothing to validate */
1747c478bd9Sstevel@tonic-gate 		if (want_cookie != NULL)
1757c478bd9Sstevel@tonic-gate 			goto cookie_abort;
1767c478bd9Sstevel@tonic-gate 		return (1);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	cph = (sctp_parm_hdr_t *)(ic + 1);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	while (cph != NULL) {
1827c478bd9Sstevel@tonic-gate 		ptype = ntohs(cph->sph_type);
1837c478bd9Sstevel@tonic-gate 		switch (ptype) {
1847c478bd9Sstevel@tonic-gate 		case PARM_HBINFO:
1857c478bd9Sstevel@tonic-gate 		case PARM_UNRECOGNIZED:
1867c478bd9Sstevel@tonic-gate 		case PARM_ECN:
1877c478bd9Sstevel@tonic-gate 			/* just ignore them */
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 		case PARM_FORWARD_TSN:
1907c478bd9Sstevel@tonic-gate 			if (sctp_options != NULL)
1917c478bd9Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		case PARM_COOKIE:
1947c478bd9Sstevel@tonic-gate 			got_cookie = B_TRUE;
1957c478bd9Sstevel@tonic-gate 			if (want_cookie != NULL) {
1967c478bd9Sstevel@tonic-gate 				*want_cookie = cph;
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		case PARM_ADDR4:
200f551bb10Svi 			*supp_af |= PARM_SUPP_V4;
201f551bb10Svi 			break;
2027c478bd9Sstevel@tonic-gate 		case PARM_ADDR6:
203f551bb10Svi 			*supp_af |= PARM_SUPP_V6;
204f551bb10Svi 			break;
2057c478bd9Sstevel@tonic-gate 		case PARM_COOKIE_PRESERVE:
2067c478bd9Sstevel@tonic-gate 		case PARM_ADAPT_LAYER_IND:
2077c478bd9Sstevel@tonic-gate 			/* These are OK */
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 		case PARM_ADDR_HOST_NAME:
2107c478bd9Sstevel@tonic-gate 			/* Don't support this; abort the association */
2117c478bd9Sstevel@tonic-gate 			serror = SCTP_ERR_BAD_ADDR;
2127c478bd9Sstevel@tonic-gate 			details = (char *)cph;
2137c478bd9Sstevel@tonic-gate 			errlen = ntohs(cph->sph_len);
2147c478bd9Sstevel@tonic-gate 			dprint(1, ("sctp:validate_init_params: host addr\n"));
2157c478bd9Sstevel@tonic-gate 			goto abort;
2167c478bd9Sstevel@tonic-gate 		case PARM_SUPP_ADDRS: {
2177c478bd9Sstevel@tonic-gate 			/* Make sure we have a supported addr intersection */
2187c478bd9Sstevel@tonic-gate 			uint16_t *p, addrtype;
2197c478bd9Sstevel@tonic-gate 			int plen;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 			plen = ntohs(cph->sph_len);
2227c478bd9Sstevel@tonic-gate 			p = (uint16_t *)(cph + 1);
2237c478bd9Sstevel@tonic-gate 			while (plen > 0) {
2247c478bd9Sstevel@tonic-gate 				addrtype = ntohs(*p);
2257c478bd9Sstevel@tonic-gate 				switch (addrtype) {
2267c478bd9Sstevel@tonic-gate 				case PARM_ADDR6:
2277c478bd9Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V6;
2287c478bd9Sstevel@tonic-gate 					break;
2297c478bd9Sstevel@tonic-gate 				case PARM_ADDR4:
2307c478bd9Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V4;
2317c478bd9Sstevel@tonic-gate 					break;
2327c478bd9Sstevel@tonic-gate 				default:
2337c478bd9Sstevel@tonic-gate 					/*
2347c478bd9Sstevel@tonic-gate 					 * Do nothing, silently ignore hostname
2357c478bd9Sstevel@tonic-gate 					 * address.
2367c478bd9Sstevel@tonic-gate 					 */
2377c478bd9Sstevel@tonic-gate 					break;
2387c478bd9Sstevel@tonic-gate 				}
2397c478bd9Sstevel@tonic-gate 				p++;
2407c478bd9Sstevel@tonic-gate 				plen -= sizeof (*p);
2417c478bd9Sstevel@tonic-gate 			}
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		default:
2457c478bd9Sstevel@tonic-gate 			/* Unrecognized param; check the high order bits */
2467c478bd9Sstevel@tonic-gate 			if ((ptype & 0xc000) == 0xc000) {
2477c478bd9Sstevel@tonic-gate 				/*
2487c478bd9Sstevel@tonic-gate 				 * report unrecognized param, and
2497c478bd9Sstevel@tonic-gate 				 * keep processing
2507c478bd9Sstevel@tonic-gate 				 */
2517c478bd9Sstevel@tonic-gate 				if (errmp != NULL) {
2527c478bd9Sstevel@tonic-gate 					if (want_cookie != NULL) {
2537c478bd9Sstevel@tonic-gate 						*errmp = sctp_make_err(sctp,
2547c478bd9Sstevel@tonic-gate 						    PARM_UNRECOGNIZED,
2557c478bd9Sstevel@tonic-gate 						    (void *)cph,
2567c478bd9Sstevel@tonic-gate 						    ntohs(cph->sph_len));
2577c478bd9Sstevel@tonic-gate 					} else {
2587c478bd9Sstevel@tonic-gate 						sctp_add_unrec_parm(cph, errmp);
2597c478bd9Sstevel@tonic-gate 					}
2607c478bd9Sstevel@tonic-gate 				}
2617c478bd9Sstevel@tonic-gate 				break;
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 			if (ptype & 0x4000) {
2647c478bd9Sstevel@tonic-gate 				/*
2657c478bd9Sstevel@tonic-gate 				 * Stop processing and drop; report
2667c478bd9Sstevel@tonic-gate 				 * unrecognized param
2677c478bd9Sstevel@tonic-gate 				 */
2687c478bd9Sstevel@tonic-gate 				serror = SCTP_ERR_UNREC_PARM;
2697c478bd9Sstevel@tonic-gate 				details = (char *)cph;
2707c478bd9Sstevel@tonic-gate 				errlen = ntohs(cph->sph_len);
2717c478bd9Sstevel@tonic-gate 				goto abort;
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 			if (ptype & 0x8000) {
2747c478bd9Sstevel@tonic-gate 				/* skip and continue processing */
2757c478bd9Sstevel@tonic-gate 				break;
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 			/*
2797c478bd9Sstevel@tonic-gate 			 * 2 high bits are clear; stop processing and
2807c478bd9Sstevel@tonic-gate 			 * drop packet
2817c478bd9Sstevel@tonic-gate 			 */
2827c478bd9Sstevel@tonic-gate 			return (0);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		cph = sctp_next_parm(cph, &remaining);
2867c478bd9Sstevel@tonic-gate 	}
287f551bb10Svi 	/*
288f551bb10Svi 	 * Some sanity checks.  The following should not fail unless the
289f551bb10Svi 	 * other side is broken.
290f551bb10Svi 	 *
291f551bb10Svi 	 * 1. If this is a V4 endpoint but V4 address is not
292f551bb10Svi 	 * supported, abort.
293f551bb10Svi 	 * 2. If this is a V6 only endpoint but V6 address is
294f551bb10Svi 	 * not supported, abort.  This assumes that a V6
295f551bb10Svi 	 * endpoint can use both V4 and V6 addresses.
296f551bb10Svi 	 * We only care about supp_af when processing INIT, i.e want_cookie
297f551bb10Svi 	 * is NULL.
298f551bb10Svi 	 */
299f551bb10Svi 	if (want_cookie == NULL &&
300f551bb10Svi 	    ((sctp->sctp_family == AF_INET && !(*supp_af & PARM_SUPP_V4)) ||
301f551bb10Svi 	    (sctp->sctp_family == AF_INET6 && !(*supp_af & PARM_SUPP_V6) &&
302f551bb10Svi 	    sctp->sctp_connp->conn_ipv6_v6only))) {
303f551bb10Svi 		dprint(1, ("sctp:validate_init_params: supp addr\n"));
304f551bb10Svi 		serror = SCTP_ERR_BAD_ADDR;
305f551bb10Svi 		goto abort;
306f551bb10Svi 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (want_cookie != NULL && !got_cookie) {
3097c478bd9Sstevel@tonic-gate cookie_abort:
3107c478bd9Sstevel@tonic-gate 		dprint(1, ("validate_init_params: cookie absent\n"));
3117c478bd9Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch), SCTP_ERR_MISSING_PARM,
3127c478bd9Sstevel@tonic-gate 		    details, errlen, inmp, 0, B_FALSE);
3137c478bd9Sstevel@tonic-gate 		return (0);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* OK */
3177c478bd9Sstevel@tonic-gate 	return (1);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate abort:
3207c478bd9Sstevel@tonic-gate 	if (want_cookie != NULL)
3217c478bd9Sstevel@tonic-gate 		return (0);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	sctp_send_abort(sctp, sctp_init2vtag(ch), serror, details,
3247c478bd9Sstevel@tonic-gate 	    errlen, inmp, 0, B_FALSE);
3257c478bd9Sstevel@tonic-gate 	return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * Initialize params from the INIT and INIT-ACK when the assoc. is
3307c478bd9Sstevel@tonic-gate  * established.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate boolean_t
3337c478bd9Sstevel@tonic-gate sctp_initialize_params(sctp_t *sctp, sctp_init_chunk_t *init,
3347c478bd9Sstevel@tonic-gate     sctp_init_chunk_t *iack)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	/* Get initial TSN */
3377c478bd9Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(init->sic_inittsn);
3387c478bd9Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/* Serial number is initialized to the same value as the TSN */
3417c478bd9Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * Get verification tags; no byteordering is necessary, since
3457c478bd9Sstevel@tonic-gate 	 * verfication tags are never processed except for byte-by-byte
3467c478bd9Sstevel@tonic-gate 	 * comparisons.
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	sctp->sctp_fvtag = init->sic_inittag;
3497c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = init->sic_inittag;
3507c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = init->sic_inittag;
3517c478bd9Sstevel@tonic-gate 	sctp->sctp_lvtag = iack->sic_inittag;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* Get the peer's rwnd */
3547c478bd9Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* Allocate the in/out-stream counters */
3577c478bd9Sstevel@tonic-gate 	sctp->sctp_num_ostr = iack->sic_outstr;
3587c478bd9Sstevel@tonic-gate 	sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
3597c478bd9Sstevel@tonic-gate 	    sctp->sctp_num_ostr, KM_NOSLEEP);
3607c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL)
3617c478bd9Sstevel@tonic-gate 		return (B_FALSE);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	sctp->sctp_num_istr = iack->sic_instr;
3647c478bd9Sstevel@tonic-gate 	sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
3657c478bd9Sstevel@tonic-gate 	    sctp->sctp_num_istr, KM_NOSLEEP);
3667c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
3677c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
3687c478bd9Sstevel@tonic-gate 		    sctp->sctp_num_ostr);
3697c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
3707c478bd9Sstevel@tonic-gate 		return (B_FALSE);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	return (B_TRUE);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3751d8c4025Svi /*
3761d8c4025Svi  * Copy the peer's original source address into addr. This relies on the
3771d8c4025Svi  * following format (see sctp_send_initack() below):
3781d8c4025Svi  * 	relative timestamp for the cookie (int64_t) +
3791d8c4025Svi  * 	cookie lifetime (uint32_t) +
3801d8c4025Svi  * 	local tie-tag (uint32_t) +  peer tie-tag (uint32_t) +
3811d8c4025Svi  * 	Peer's original src ...
3821d8c4025Svi  */
3831d8c4025Svi int
3841d8c4025Svi cl_sctp_cookie_paddr(sctp_chunk_hdr_t *ch, in6_addr_t *addr)
3851d8c4025Svi {
3861d8c4025Svi 	uchar_t	*off;
3871d8c4025Svi 
3881d8c4025Svi 	ASSERT(addr != NULL);
3891d8c4025Svi 
3901d8c4025Svi 	if (ch->sch_id != CHUNK_COOKIE)
3911d8c4025Svi 		return (EINVAL);
3921d8c4025Svi 
3931d8c4025Svi 	off = (uchar_t *)ch + sizeof (*ch) + sizeof (int64_t) +
3941d8c4025Svi 	    sizeof (uint32_t) + sizeof (uint32_t) + sizeof (uint32_t);
3951d8c4025Svi 
3961d8c4025Svi 	bcopy(off, addr, sizeof (*addr));
3971d8c4025Svi 
3981d8c4025Svi 	return (0);
3991d8c4025Svi }
4001d8c4025Svi 
4017c478bd9Sstevel@tonic-gate #define	SCTP_CALC_COOKIE_LEN(initcp) \
4027c478bd9Sstevel@tonic-gate 	sizeof (int64_t) +		/* timestamp */			\
4037c478bd9Sstevel@tonic-gate 	sizeof (uint32_t) +		/* cookie lifetime */		\
4047c478bd9Sstevel@tonic-gate 	sizeof (sctp_init_chunk_t) +	/* INIT ACK */			\
4057c478bd9Sstevel@tonic-gate 	sizeof (in6_addr_t) +		/* peer's original source */ 	\
4067c478bd9Sstevel@tonic-gate 	ntohs((initcp)->sch_len) +	/* peer's INIT */		\
4077c478bd9Sstevel@tonic-gate 	sizeof (uint32_t) +		/* local tie-tag */		\
4087c478bd9Sstevel@tonic-gate 	sizeof (uint32_t) +		/* peer tie-tag */		\
4097c478bd9Sstevel@tonic-gate 	sizeof (sctp_parm_hdr_t) +	/* param header */		\
4107c478bd9Sstevel@tonic-gate 	16				/* MD5 hash */
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate void
4137c478bd9Sstevel@tonic-gate sctp_send_initack(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *initmp)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	ipha_t			*initiph;
4167c478bd9Sstevel@tonic-gate 	ip6_t			*initip6h;
4177c478bd9Sstevel@tonic-gate 	ipha_t			*iackiph;
4187c478bd9Sstevel@tonic-gate 	ip6_t			*iackip6h;
4197c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*iack_ch;
4207c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
4217c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
4227c478bd9Sstevel@tonic-gate 	sctp_hdr_t		*iacksh;
4237c478bd9Sstevel@tonic-gate 	sctp_hdr_t		*initsh;
4247c478bd9Sstevel@tonic-gate 	size_t			cookielen;
4257c478bd9Sstevel@tonic-gate 	size_t			iacklen;
4267c478bd9Sstevel@tonic-gate 	size_t			ipsctplen;
4277c478bd9Sstevel@tonic-gate 	size_t			errlen = 0;
4287c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*cookieph;
4297c478bd9Sstevel@tonic-gate 	mblk_t			*iackmp;
4307c478bd9Sstevel@tonic-gate 	uint32_t		itag;
4317c478bd9Sstevel@tonic-gate 	uint32_t		itsn;
4327c478bd9Sstevel@tonic-gate 	int64_t			*now;
4337c478bd9Sstevel@tonic-gate 	int64_t			nowt;
4347c478bd9Sstevel@tonic-gate 	uint32_t		*lifetime;
4357c478bd9Sstevel@tonic-gate 	char			*p;
4367c478bd9Sstevel@tonic-gate 	boolean_t		 isv4;
437f551bb10Svi 	int			supp_af = 0;
4387c478bd9Sstevel@tonic-gate 	uint_t			sctp_options;
4397c478bd9Sstevel@tonic-gate 	uint32_t		*ttag;
4407c478bd9Sstevel@tonic-gate 	int			pad;
4417c478bd9Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
4427c478bd9Sstevel@tonic-gate 	boolean_t		initcollision = B_FALSE;
443f551bb10Svi 	boolean_t		linklocal = B_FALSE;
444*45916cd2Sjpk 	cred_t			*cr;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
4477c478bd9Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(initmp->b_rptr) == IPV4_VERSION);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/* Extract the INIT chunk */
4507c478bd9Sstevel@tonic-gate 	if (isv4) {
4517c478bd9Sstevel@tonic-gate 		initiph = (ipha_t *)initmp->b_rptr;
4527c478bd9Sstevel@tonic-gate 		initsh = (sctp_hdr_t *)((char *)initiph +
4537c478bd9Sstevel@tonic-gate 		    IPH_HDR_LENGTH(initmp->b_rptr));
4547c478bd9Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr_len;
455f551bb10Svi 		supp_af |= PARM_SUPP_V4;
4567c478bd9Sstevel@tonic-gate 	} else {
4577c478bd9Sstevel@tonic-gate 		initip6h = (ip6_t *)initmp->b_rptr;
4587c478bd9Sstevel@tonic-gate 		initsh = (sctp_hdr_t *)(initip6h + 1);
4597c478bd9Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr6_len;
460f551bb10Svi 		if (IN6_IS_ADDR_LINKLOCAL(&initip6h->ip6_src))
461f551bb10Svi 			linklocal = B_TRUE;
462f551bb10Svi 		supp_af |= PARM_SUPP_V6;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	ASSERT(OK_32PTR(initsh));
4657c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)((char *)(initsh + 1) + sizeof (*iack_ch));
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/* Make sure we like the peer's parameters */
4687c478bd9Sstevel@tonic-gate 	if (validate_init_params(sctp, ch, init, initmp, NULL, &errmp,
4697c478bd9Sstevel@tonic-gate 		&supp_af, &sctp_options) == 0) {
4707c478bd9Sstevel@tonic-gate 		return;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 	if (errmp != NULL)
4737c478bd9Sstevel@tonic-gate 		errlen = msgdsize(errmp);
4747c478bd9Sstevel@tonic-gate 	if (sctp->sctp_family == AF_INET) {
4757c478bd9Sstevel@tonic-gate 		/*
4767c478bd9Sstevel@tonic-gate 		 * Irregardless of the supported address in the INIT, v4
4777c478bd9Sstevel@tonic-gate 		 * must be supported.
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		supp_af = PARM_SUPP_V4;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_LISTEN) {
4827c478bd9Sstevel@tonic-gate 		/* normal, expected INIT: generate new vtag and itsn */
4837c478bd9Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
4847c478bd9Sstevel@tonic-gate 		if (itag == 0)
4857c478bd9Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
4867c478bd9Sstevel@tonic-gate 		itsn = itag + 1;
4877c478bd9Sstevel@tonic-gate 		itag = htonl(itag);
4887c478bd9Sstevel@tonic-gate 	} else if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
4897c478bd9Sstevel@tonic-gate 	    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
4907c478bd9Sstevel@tonic-gate 		/* init collision; copy vtag and itsn from sctp */
4917c478bd9Sstevel@tonic-gate 		itag = sctp->sctp_lvtag;
4927c478bd9Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * In addition we need to send all the params that was sent
4957c478bd9Sstevel@tonic-gate 		 * in our INIT chunk. Essentially, it is only the supported
4967c478bd9Sstevel@tonic-gate 		 * address params that we need to add.
4977c478bd9Sstevel@tonic-gate 		 */
4987c478bd9Sstevel@tonic-gate 		initcollision = B_TRUE;
499f551bb10Svi 		/*
500f551bb10Svi 		 * When we sent the INIT, we should have set linklocal in
501f551bb10Svi 		 * the sctp which should be good enough.
502f551bb10Svi 		 */
503f551bb10Svi 		if (linklocal)
504f551bb10Svi 			linklocal = B_FALSE;
5057c478bd9Sstevel@tonic-gate 	} else {
5067c478bd9Sstevel@tonic-gate 		/* peer restart; generate new vtag but keep everything else */
5077c478bd9Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
5087c478bd9Sstevel@tonic-gate 		if (itag == 0)
5097c478bd9Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
5107c478bd9Sstevel@tonic-gate 		itag = htonl(itag);
5117c478bd9Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/*
5157c478bd9Sstevel@tonic-gate 	 * Allocate a mblk for the INIT ACK, consisting of the link layer
5167c478bd9Sstevel@tonic-gate 	 * header, the IP header, the SCTP common header, and INIT ACK chunk,
5177c478bd9Sstevel@tonic-gate 	 * and finally the COOKIE parameter.
5187c478bd9Sstevel@tonic-gate 	 */
5197c478bd9Sstevel@tonic-gate 	cookielen = SCTP_CALC_COOKIE_LEN(ch);
5207c478bd9Sstevel@tonic-gate 	iacklen = sizeof (*iack_ch) + sizeof (*iack) + cookielen;
5217c478bd9Sstevel@tonic-gate 	if (sctp->sctp_send_adaption)
5227c478bd9Sstevel@tonic-gate 		iacklen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
5237c478bd9Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
5247c478bd9Sstevel@tonic-gate 	    sctp->sctp_prsctp_aware && sctp_prsctp_enabled) {
5257c478bd9Sstevel@tonic-gate 		iacklen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 	if (initcollision)
5287c478bd9Sstevel@tonic-gate 		iacklen += sctp_supaddr_param_len(sctp);
529f551bb10Svi 	if (!linklocal)
530f551bb10Svi 		iacklen += sctp_addr_params_len(sctp, supp_af, B_FALSE);
5317c478bd9Sstevel@tonic-gate 	ipsctplen += sizeof (*iacksh) + iacklen;
5327c478bd9Sstevel@tonic-gate 	iacklen += errlen;
5337c478bd9Sstevel@tonic-gate 	if ((pad = ipsctplen % 4) != 0) {
5347c478bd9Sstevel@tonic-gate 		pad = 4 - pad;
5357c478bd9Sstevel@tonic-gate 		ipsctplen += pad;
5367c478bd9Sstevel@tonic-gate 	}
537*45916cd2Sjpk 	iackmp = allocb_cred(ipsctplen + sctp_wroff_xtra,
538*45916cd2Sjpk 	    CONN_CRED(sctp->sctp_connp));
5397c478bd9Sstevel@tonic-gate 	if (iackmp == NULL) {
5407c478bd9Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch),
5417c478bd9Sstevel@tonic-gate 		    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE);
5427c478bd9Sstevel@tonic-gate 		return;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* Copy in the [imcomplete] IP/SCTP composite header */
5467c478bd9Sstevel@tonic-gate 	p = (char *)(iackmp->b_rptr + sctp_wroff_xtra);
5477c478bd9Sstevel@tonic-gate 	iackmp->b_rptr = (uchar_t *)p;
5487c478bd9Sstevel@tonic-gate 	if (isv4) {
5497c478bd9Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, p, sctp->sctp_hdr_len);
5507c478bd9Sstevel@tonic-gate 		iackiph = (ipha_t *)p;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		/* Copy the peer's IP addr */
5537c478bd9Sstevel@tonic-gate 		iackiph->ipha_dst = initiph->ipha_src;
5547c478bd9Sstevel@tonic-gate 		iackiph->ipha_src = initiph->ipha_dst;
5557c478bd9Sstevel@tonic-gate 		iackiph->ipha_length = htons(ipsctplen + errlen);
5567c478bd9Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr_len);
5577c478bd9Sstevel@tonic-gate 	} else {
5587c478bd9Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, p, sctp->sctp_hdr6_len);
5597c478bd9Sstevel@tonic-gate 		iackip6h = (ip6_t *)p;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		/* Copy the peer's IP addr */
5627c478bd9Sstevel@tonic-gate 		iackip6h->ip6_dst = initip6h->ip6_src;
5637c478bd9Sstevel@tonic-gate 		iackip6h->ip6_src = initip6h->ip6_dst;
5647c478bd9Sstevel@tonic-gate 		iackip6h->ip6_plen = htons(ipsctplen - sizeof (*iackip6h) +
5657c478bd9Sstevel@tonic-gate 		    errlen);
5667c478bd9Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr6_len);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	ASSERT(OK_32PTR(iacksh));
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/* Fill in the holes in the SCTP common header */
5717c478bd9Sstevel@tonic-gate 	iacksh->sh_sport = initsh->sh_dport;
5727c478bd9Sstevel@tonic-gate 	iacksh->sh_dport = initsh->sh_sport;
5737c478bd9Sstevel@tonic-gate 	iacksh->sh_verf = init->sic_inittag;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	/* INIT ACK chunk header */
5767c478bd9Sstevel@tonic-gate 	iack_ch = (sctp_chunk_hdr_t *)(iacksh + 1);
5777c478bd9Sstevel@tonic-gate 	iack_ch->sch_id = CHUNK_INIT_ACK;
5787c478bd9Sstevel@tonic-gate 	iack_ch->sch_flags = 0;
5797c478bd9Sstevel@tonic-gate 	iack_ch->sch_len = htons(iacklen);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/* The INIT ACK itself */
5827c478bd9Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iack_ch + 1);
5837c478bd9Sstevel@tonic-gate 	iack->sic_inittag = itag;	/* already in network byteorder */
5847c478bd9Sstevel@tonic-gate 	iack->sic_inittsn = htonl(itsn);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	iack->sic_a_rwnd = htonl(sctp->sctp_rwnd);
5877c478bd9Sstevel@tonic-gate 	/* Advertise what we would want to have as stream #'s */
5887c478bd9Sstevel@tonic-gate 	iack->sic_outstr = htons(MIN(sctp->sctp_num_ostr,
5897c478bd9Sstevel@tonic-gate 	    ntohs(init->sic_instr)));
5907c478bd9Sstevel@tonic-gate 	iack->sic_instr = htons(sctp->sctp_num_istr);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	p = (char *)(iack + 1);
5937c478bd9Sstevel@tonic-gate 	p += sctp_adaption_code_param(sctp, (uchar_t *)p);
5947c478bd9Sstevel@tonic-gate 	if (initcollision)
5957c478bd9Sstevel@tonic-gate 		p += sctp_supaddr_param(sctp, (uchar_t *)p);
596f551bb10Svi 	if (!linklocal)
597f551bb10Svi 		p += sctp_addr_params(sctp, supp_af, (uchar_t *)p);
5987c478bd9Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
5997c478bd9Sstevel@tonic-gate 	    sctp->sctp_prsctp_aware && sctp_prsctp_enabled) {
6007c478bd9Sstevel@tonic-gate 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * Generate and lay in the COOKIE parameter.
6047c478bd9Sstevel@tonic-gate 	 *
6051d8c4025Svi 	 * Any change here that results in a change of location for
6061d8c4025Svi 	 * the peer's orig source address must be propagated to the fn
6071d8c4025Svi 	 * cl_sctp_cookie_paddr() above.
6081d8c4025Svi 	 *
6097c478bd9Sstevel@tonic-gate 	 * The cookie consists of:
6107c478bd9Sstevel@tonic-gate 	 * 1. The relative timestamp for the cookie (lbolt64)
6117c478bd9Sstevel@tonic-gate 	 * 2. The cookie lifetime (uint32_t) in tick
6127c478bd9Sstevel@tonic-gate 	 * 3. The local tie-tag
6137c478bd9Sstevel@tonic-gate 	 * 4. The peer tie-tag
6147c478bd9Sstevel@tonic-gate 	 * 5. Peer's original src, used to confirm the validity of address.
6157c478bd9Sstevel@tonic-gate 	 * 6. Our INIT ACK chunk, less any parameters
6167c478bd9Sstevel@tonic-gate 	 * 7. The INIT chunk (may contain parameters)
6177c478bd9Sstevel@tonic-gate 	 * 8. 128-bit MD5 signature.
6187c478bd9Sstevel@tonic-gate 	 *
6197c478bd9Sstevel@tonic-gate 	 * Since the timestamp values will only be evaluated locally, we
6207c478bd9Sstevel@tonic-gate 	 * don't need to worry about byte-ordering them.
6217c478bd9Sstevel@tonic-gate 	 */
6227c478bd9Sstevel@tonic-gate 	cookieph = (sctp_parm_hdr_t *)p;
6237c478bd9Sstevel@tonic-gate 	cookieph->sph_type = htons(PARM_COOKIE);
6247c478bd9Sstevel@tonic-gate 	cookieph->sph_len = htons(cookielen);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/* timestamp */
6277c478bd9Sstevel@tonic-gate 	now = (int64_t *)(cookieph + 1);
6287c478bd9Sstevel@tonic-gate 	nowt = lbolt64;
6297c478bd9Sstevel@tonic-gate 	bcopy(&nowt, now, sizeof (*now));
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/* cookie lifetime -- need configuration */
6327c478bd9Sstevel@tonic-gate 	lifetime = (uint32_t *)(now + 1);
6337c478bd9Sstevel@tonic-gate 	*lifetime = sctp->sctp_cookie_lifetime;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/* Set the tie-tags */
6367c478bd9Sstevel@tonic-gate 	ttag = (uint32_t *)(lifetime + 1);
6377c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_COOKIE_WAIT) {
6387c478bd9Sstevel@tonic-gate 		*ttag = 0;
6397c478bd9Sstevel@tonic-gate 		ttag++;
6407c478bd9Sstevel@tonic-gate 		*ttag = 0;
6417c478bd9Sstevel@tonic-gate 		ttag++;
6427c478bd9Sstevel@tonic-gate 	} else {
6437c478bd9Sstevel@tonic-gate 		/* local tie-tag (network byte-order) */
6447c478bd9Sstevel@tonic-gate 		*ttag = sctp->sctp_lvtag;
6457c478bd9Sstevel@tonic-gate 		ttag++;
6467c478bd9Sstevel@tonic-gate 		/* peer tie-tag (network byte-order) */
6477c478bd9Sstevel@tonic-gate 		*ttag = sctp->sctp_fvtag;
6487c478bd9Sstevel@tonic-gate 		ttag++;
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * Copy in peer's original source address so that we can confirm
6527c478bd9Sstevel@tonic-gate 	 * the reachability later.
6537c478bd9Sstevel@tonic-gate 	 */
6547c478bd9Sstevel@tonic-gate 	p = (char *)ttag;
6557c478bd9Sstevel@tonic-gate 	if (isv4) {
6567c478bd9Sstevel@tonic-gate 		in6_addr_t peer_addr;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iackiph->ipha_dst, &peer_addr);
6597c478bd9Sstevel@tonic-gate 		bcopy(&peer_addr, p, sizeof (in6_addr_t));
6607c478bd9Sstevel@tonic-gate 	} else {
6617c478bd9Sstevel@tonic-gate 		bcopy(&iackip6h->ip6_dst, p, sizeof (in6_addr_t));
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 	p += sizeof (in6_addr_t);
6647c478bd9Sstevel@tonic-gate 	/* Copy in our INIT ACK chunk */
6657c478bd9Sstevel@tonic-gate 	bcopy(iack, p, sizeof (*iack));
6667c478bd9Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)p;
6677c478bd9Sstevel@tonic-gate 	/* Set the # of streams we'll end up using */
6687c478bd9Sstevel@tonic-gate 	iack->sic_outstr = MIN(sctp->sctp_num_ostr, ntohs(init->sic_instr));
6697c478bd9Sstevel@tonic-gate 	iack->sic_instr = MIN(sctp->sctp_num_istr, ntohs(init->sic_outstr));
6707c478bd9Sstevel@tonic-gate 	p += sizeof (*iack);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/* Copy in the peer's INIT chunk */
6737c478bd9Sstevel@tonic-gate 	bcopy(ch, p, ntohs(ch->sch_len));
6747c478bd9Sstevel@tonic-gate 	p += ntohs(ch->sch_len);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * Calculate the HMAC ICV into the digest slot in buf.
6787c478bd9Sstevel@tonic-gate 	 * First, generate a new secret if the current secret is
6797c478bd9Sstevel@tonic-gate 	 * older than the new secret lifetime parameter permits,
6807c478bd9Sstevel@tonic-gate 	 * copying the current secret to sctp_old_secret.
6817c478bd9Sstevel@tonic-gate 	 */
6827c478bd9Sstevel@tonic-gate 	if (sctp_new_secret_interval > 0 &&
6837c478bd9Sstevel@tonic-gate 	    (sctp->sctp_last_secret_update +
6847c478bd9Sstevel@tonic-gate 	    MSEC_TO_TICK(sctp_new_secret_interval)) <= nowt) {
6857c478bd9Sstevel@tonic-gate 		bcopy(sctp->sctp_secret, sctp->sctp_old_secret,
6867c478bd9Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
6877c478bd9Sstevel@tonic-gate 		(void) random_get_pseudo_bytes(sctp->sctp_secret,
6887c478bd9Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
6897c478bd9Sstevel@tonic-gate 		sctp->sctp_last_secret_update = nowt;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	hmac_md5((uchar_t *)now, cookielen - sizeof (*cookieph) - 16,
6937c478bd9Sstevel@tonic-gate 	    (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN, (uchar_t *)p);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	iackmp->b_wptr = iackmp->b_rptr + ipsctplen;
6967c478bd9Sstevel@tonic-gate 	iackmp->b_cont = errmp;		/*  OK if NULL */
6977c478bd9Sstevel@tonic-gate 
698*45916cd2Sjpk 	if (is_system_labeled() && (cr = DB_CRED(initmp)) != NULL &&
699*45916cd2Sjpk 	    crgetlabel(cr) != NULL) {
700*45916cd2Sjpk 		conn_t *connp = sctp->sctp_connp;
701*45916cd2Sjpk 		int err, adjust;
702*45916cd2Sjpk 
703*45916cd2Sjpk 		if (isv4)
704*45916cd2Sjpk 			err = tsol_check_label(cr, &iackmp, &adjust,
705*45916cd2Sjpk 			    connp->conn_mac_exempt);
706*45916cd2Sjpk 		else
707*45916cd2Sjpk 			err = tsol_check_label_v6(cr, &iackmp, &adjust,
708*45916cd2Sjpk 			    connp->conn_mac_exempt);
709*45916cd2Sjpk 		if (err != 0) {
710*45916cd2Sjpk 			sctp_send_abort(sctp, sctp_init2vtag(ch),
711*45916cd2Sjpk 			    SCTP_ERR_AUTH_ERR, NULL, 0, initmp, 0, B_FALSE);
712*45916cd2Sjpk 			freemsg(iackmp);
713*45916cd2Sjpk 			return;
714*45916cd2Sjpk 		}
715*45916cd2Sjpk 		if (isv4) {
716*45916cd2Sjpk 			iackiph = (ipha_t *)iackmp->b_rptr;
717*45916cd2Sjpk 			adjust += ntohs(iackiph->ipha_length);
718*45916cd2Sjpk 			iackiph->ipha_length = htons(adjust);
719*45916cd2Sjpk 		}
720*45916cd2Sjpk 	}
721*45916cd2Sjpk 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * Stash the conn ptr info. for IP only as e don't have any
7247c478bd9Sstevel@tonic-gate 	 * cached IRE.
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	SCTP_STASH_IPINFO(iackmp, (ire_t *)NULL);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/* XXX sctp == sctp_g_q, so using its obchunks is valid */
7297c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_opkts);
7307c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/* OK to call IP_PUT() here instead of sctp_add_sendq(). */
7337c478bd9Sstevel@tonic-gate 	CONN_INC_REF(sctp->sctp_connp);
7347c478bd9Sstevel@tonic-gate 	iackmp->b_flag |= MSGHASREF;
7357c478bd9Sstevel@tonic-gate 	IP_PUT(iackmp, sctp->sctp_connp, isv4);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate void
7397c478bd9Sstevel@tonic-gate sctp_send_cookie_ack(sctp_t *sctp)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t *cach;
7427c478bd9Sstevel@tonic-gate 	mblk_t *camp;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	camp = sctp_make_mp(sctp, NULL, sizeof (*cach));
7457c478bd9Sstevel@tonic-gate 	if (camp == NULL) {
7467c478bd9Sstevel@tonic-gate 		/* XXX should abort, but don't have the inmp anymore */
7477c478bd9Sstevel@tonic-gate 		return;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	cach = (sctp_chunk_hdr_t *)camp->b_wptr;
7517c478bd9Sstevel@tonic-gate 	camp->b_wptr = (uchar_t *)(cach + 1);
7527c478bd9Sstevel@tonic-gate 	cach->sch_id = CHUNK_COOKIE_ACK;
7537c478bd9Sstevel@tonic-gate 	cach->sch_flags = 0;
7547c478bd9Sstevel@tonic-gate 	cach->sch_len = htons(sizeof (*cach));
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	sctp_set_iplen(sctp, camp);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	sctp_add_sendq(sctp, camp);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate static int
7647c478bd9Sstevel@tonic-gate sctp_find_al_ind(sctp_parm_hdr_t *sph, ssize_t len, uint32_t *adaption_code)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (len < sizeof (*sph))
7687c478bd9Sstevel@tonic-gate 		return (-1);
7697c478bd9Sstevel@tonic-gate 	while (sph != NULL) {
7707c478bd9Sstevel@tonic-gate 		if (sph->sph_type == htons(PARM_ADAPT_LAYER_IND) &&
7717c478bd9Sstevel@tonic-gate 		    ntohs(sph->sph_len) >= (sizeof (*sph) +
7727c478bd9Sstevel@tonic-gate 			sizeof (uint32_t))) {
7737c478bd9Sstevel@tonic-gate 			*adaption_code = *(uint32_t *)(sph + 1);
7747c478bd9Sstevel@tonic-gate 			return (0);
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 		sph = sctp_next_parm(sph, &len);
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 	return (-1);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate void
7827c478bd9Sstevel@tonic-gate sctp_send_cookie_echo(sctp_t *sctp, sctp_chunk_hdr_t *iackch, mblk_t *iackmp)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	mblk_t			*cemp;
7857c478bd9Sstevel@tonic-gate 	mblk_t			*mp = NULL;
7867c478bd9Sstevel@tonic-gate 	mblk_t			*head;
7877c478bd9Sstevel@tonic-gate 	mblk_t			*meta;
7887c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*fp;
7897c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*cech;
7907c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	 *iack;
7917c478bd9Sstevel@tonic-gate 	int32_t			cansend;
7927c478bd9Sstevel@tonic-gate 	int32_t			seglen;
7937c478bd9Sstevel@tonic-gate 	size_t			ceclen;
7947c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
7957c478bd9Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
7967c478bd9Sstevel@tonic-gate 	sctp_tf_t		*tf;
797f551bb10Svi 	int			pad = 0;
7987c478bd9Sstevel@tonic-gate 	int			hdrlen;
7997c478bd9Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
8007c478bd9Sstevel@tonic-gate 	uint_t			sctp_options;
8017c478bd9Sstevel@tonic-gate 	int			error;
8027c478bd9Sstevel@tonic-gate 	uint16_t		old_num_str;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iackch + 1);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	cph = NULL;
8077c478bd9Sstevel@tonic-gate 	if (validate_init_params(sctp, iackch, iack, iackmp, &cph, &errmp,
8087c478bd9Sstevel@tonic-gate 	    &pad, &sctp_options) == 0) { /* result in 'pad' ignored */
8097c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpAborted);
8107c478bd9Sstevel@tonic-gate 		sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
8117c478bd9Sstevel@tonic-gate 		sctp_clean_death(sctp, ECONNABORTED);
8127c478bd9Sstevel@tonic-gate 		return;
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 	ASSERT(cph != NULL);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/* Got a cookie to echo back; allocate an mblk */
8197c478bd9Sstevel@tonic-gate 	ceclen = sizeof (*cech) + ntohs(cph->sph_len) - sizeof (*cph);
8207c478bd9Sstevel@tonic-gate 	if ((pad = ceclen & (SCTP_ALIGN - 1)) != 0)
8217c478bd9Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (IPH_HDR_VERSION(iackmp->b_rptr) == IPV4_VERSION)
8247c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
8257c478bd9Sstevel@tonic-gate 	else
8267c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	cemp = allocb(sctp_wroff_xtra + hdrlen + ceclen + pad, BPRI_MED);
8297c478bd9Sstevel@tonic-gate 	if (cemp == NULL) {
8307c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
8317c478bd9Sstevel@tonic-gate 		    sctp->sctp_current->rto);
8327c478bd9Sstevel@tonic-gate 		if (errmp != NULL)
8337c478bd9Sstevel@tonic-gate 			freeb(errmp);
8347c478bd9Sstevel@tonic-gate 		return;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	cemp->b_rptr += (sctp_wroff_xtra + hdrlen);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/* Process the INIT ACK */
8397c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = iack->sic_inittag;
8407c478bd9Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = iack->sic_inittag;
8417c478bd9Sstevel@tonic-gate 	sctp->sctp_fvtag = iack->sic_inittag;
8427c478bd9Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(iack->sic_inittsn);
8437c478bd9Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
8447c478bd9Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
8457c478bd9Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(iack->sic_a_rwnd);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/*
8487c478bd9Sstevel@tonic-gate 	 * Populate sctp with addresses given in the INIT ACK or IP header.
8497c478bd9Sstevel@tonic-gate 	 * Need to set the df bit in the current fp as it has been cleared
8507c478bd9Sstevel@tonic-gate 	 * in sctp_connect().
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 	sctp->sctp_current->df = B_TRUE;
8537c478bd9Sstevel@tonic-gate 	/*
8547c478bd9Sstevel@tonic-gate 	 * Since IP uses this info during the fanout process, we need to hold
8557c478bd9Sstevel@tonic-gate 	 * the lock for this hash line while performing this operation.
8567c478bd9Sstevel@tonic-gate 	 */
8577c478bd9Sstevel@tonic-gate 	/* XXX sctp_conn_fanout + SCTP_CONN_HASH(sctp->sctp_ports); */
8587c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
8597c478bd9Sstevel@tonic-gate 	tf = sctp->sctp_conn_tfp;
8607c478bd9Sstevel@tonic-gate 	/* sctp isn't a listener so only need to hold conn fanout lock */
8617c478bd9Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
8627c478bd9Sstevel@tonic-gate 	if (sctp_get_addrparams(sctp, NULL, iackmp, iackch, NULL) != 0) {
8637c478bd9Sstevel@tonic-gate 		mutex_exit(&tf->tf_lock);
8647c478bd9Sstevel@tonic-gate 		freeb(cemp);
8657c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
8667c478bd9Sstevel@tonic-gate 		    sctp->sctp_current->rto);
8677c478bd9Sstevel@tonic-gate 		if (errmp != NULL)
8687c478bd9Sstevel@tonic-gate 			freeb(errmp);
8697c478bd9Sstevel@tonic-gate 		return;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	fp = sctp->sctp_current;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/*
8767c478bd9Sstevel@tonic-gate 	 * There could be a case when we get an INIT-ACK again, if the INIT
8777c478bd9Sstevel@tonic-gate 	 * is re-transmitted, for e.g., which means we would have already
8787c478bd9Sstevel@tonic-gate 	 * allocated this resource earlier (also for sctp_instr). In this
8797c478bd9Sstevel@tonic-gate 	 * case we check and re-allocate, if necessary.
8807c478bd9Sstevel@tonic-gate 	 */
8817c478bd9Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_ostr;
8827c478bd9Sstevel@tonic-gate 	if (ntohs(iack->sic_instr) < sctp->sctp_num_ostr)
8837c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = ntohs(iack->sic_instr);
8847c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
8857c478bd9Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
8867c478bd9Sstevel@tonic-gate 		    sctp->sctp_num_ostr, KM_NOSLEEP);
8877c478bd9Sstevel@tonic-gate 	} else {
8887c478bd9Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
8897c478bd9Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_ostr) {
8907c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
8917c478bd9Sstevel@tonic-gate 			    old_num_str);
8927c478bd9Sstevel@tonic-gate 			sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
8937c478bd9Sstevel@tonic-gate 			    sctp->sctp_num_ostr, KM_NOSLEEP);
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
8977c478bd9Sstevel@tonic-gate 		freeb(cemp);
8987c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
8997c478bd9Sstevel@tonic-gate 		if (errmp != NULL)
9007c478bd9Sstevel@tonic-gate 			freeb(errmp);
9017c478bd9Sstevel@tonic-gate 		return;
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * Allocate the in stream tracking array. Comments for sctp_ostrcntrs
9067c478bd9Sstevel@tonic-gate 	 * hold here too.
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_istr;
9097c478bd9Sstevel@tonic-gate 	if (ntohs(iack->sic_outstr) < sctp->sctp_num_istr)
9107c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = ntohs(iack->sic_outstr);
9117c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
9127c478bd9Sstevel@tonic-gate 		sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
9137c478bd9Sstevel@tonic-gate 		    sctp->sctp_num_istr, KM_NOSLEEP);
9147c478bd9Sstevel@tonic-gate 	} else {
9157c478bd9Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
9167c478bd9Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_istr) {
9177c478bd9Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
9187c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * old_num_str);
9197c478bd9Sstevel@tonic-gate 			sctp->sctp_instr = kmem_zalloc(
9207c478bd9Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr,
9217c478bd9Sstevel@tonic-gate 			    KM_NOSLEEP);
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
9257c478bd9Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
9267c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
9277c478bd9Sstevel@tonic-gate 		freeb(cemp);
9287c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
9297c478bd9Sstevel@tonic-gate 		if (errmp != NULL)
9307c478bd9Sstevel@tonic-gate 			freeb(errmp);
9317c478bd9Sstevel@tonic-gate 		return;
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if (!(sctp_options & SCTP_PRSCTP_OPTION) && sctp->sctp_prsctp_aware)
9357c478bd9Sstevel@tonic-gate 		sctp->sctp_prsctp_aware = B_FALSE;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (sctp_find_al_ind((sctp_parm_hdr_t *)(iack + 1),
9387c478bd9Sstevel@tonic-gate 		ntohs(iackch->sch_len) - (sizeof (*iackch) + sizeof (*iack)),
9397c478bd9Sstevel@tonic-gate 		&sctp->sctp_rx_adaption_code) == 0) {
9407c478bd9Sstevel@tonic-gate 		sctp->sctp_recv_adaption = 1;
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	cech = (sctp_chunk_hdr_t *)cemp->b_rptr;
9447c478bd9Sstevel@tonic-gate 	ASSERT(OK_32PTR(cech));
9457c478bd9Sstevel@tonic-gate 	cech->sch_id = CHUNK_COOKIE;
9467c478bd9Sstevel@tonic-gate 	cech->sch_flags = 0;
9477c478bd9Sstevel@tonic-gate 	cech->sch_len = htons(ceclen);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	/* Copy the cookie (less the parm hdr) to the chunk */
9507c478bd9Sstevel@tonic-gate 	bcopy(cph + 1, cech + 1, ceclen - sizeof (*cph));
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	cemp->b_wptr = cemp->b_rptr + ceclen;
9537c478bd9Sstevel@tonic-gate 
954df19b344Svi 	if (sctp->sctp_unsent > 0) {
9557c478bd9Sstevel@tonic-gate 		sctp_msg_hdr_t	*smh;
9567c478bd9Sstevel@tonic-gate 		mblk_t		*prev = NULL;
9577c478bd9Sstevel@tonic-gate 		uint32_t	unsent = 0;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		mp = sctp->sctp_xmit_unsent;
9607c478bd9Sstevel@tonic-gate 		do {
9617c478bd9Sstevel@tonic-gate 			smh = (sctp_msg_hdr_t *)mp->b_rptr;
9627c478bd9Sstevel@tonic-gate 			if (smh->smh_sid >= sctp->sctp_num_ostr) {
9637c478bd9Sstevel@tonic-gate 				unsent += smh->smh_msglen;
9647c478bd9Sstevel@tonic-gate 				if (prev != NULL)
9657c478bd9Sstevel@tonic-gate 					prev->b_next = mp->b_next;
9667c478bd9Sstevel@tonic-gate 				else
9677c478bd9Sstevel@tonic-gate 					sctp->sctp_xmit_unsent = mp->b_next;
9687c478bd9Sstevel@tonic-gate 				mp->b_next = NULL;
9697c478bd9Sstevel@tonic-gate 				sctp_sendfail_event(sctp, mp, SCTP_ERR_BAD_SID,
9707c478bd9Sstevel@tonic-gate 				    B_FALSE);
9717c478bd9Sstevel@tonic-gate 				if (prev != NULL)
9727c478bd9Sstevel@tonic-gate 					mp = prev->b_next;
9737c478bd9Sstevel@tonic-gate 				else
9747c478bd9Sstevel@tonic-gate 					mp = sctp->sctp_xmit_unsent;
9757c478bd9Sstevel@tonic-gate 			} else {
9767c478bd9Sstevel@tonic-gate 				prev = mp;
9777c478bd9Sstevel@tonic-gate 				mp = mp->b_next;
9787c478bd9Sstevel@tonic-gate 			}
9797c478bd9Sstevel@tonic-gate 		} while (mp != NULL);
9807c478bd9Sstevel@tonic-gate 		if (unsent > 0) {
9817c478bd9Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= unsent);
9827c478bd9Sstevel@tonic-gate 			sctp->sctp_unsent -= unsent;
9837c478bd9Sstevel@tonic-gate 			/*
9847c478bd9Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
9857c478bd9Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
9867c478bd9Sstevel@tonic-gate 			 * This is not necessary, but doesn't harm, we
9877c478bd9Sstevel@tonic-gate 			 * just use unsent instead of sent-unack'ed +
9887c478bd9Sstevel@tonic-gate 			 * unsent, since there won't be any sent-unack'ed
9897c478bd9Sstevel@tonic-gate 			 * here.
9907c478bd9Sstevel@tonic-gate 			 */
9917c478bd9Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
9927c478bd9Sstevel@tonic-gate 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
9937c478bd9Sstevel@tonic-gate 				    sctp->sctp_unsent);
9947c478bd9Sstevel@tonic-gate 			}
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_unsent == NULL)
9977c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_unsent_tail = NULL;
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 	ceclen += pad;
10007c478bd9Sstevel@tonic-gate 	cansend = MIN(sctp->sctp_unsent, sctp->sctp_frwnd);
10017c478bd9Sstevel@tonic-gate 	meta = sctp_get_msg_to_send(sctp, &mp, NULL, &error, ceclen,
10027c478bd9Sstevel@tonic-gate 	    cansend,  NULL);
10037c478bd9Sstevel@tonic-gate 	/*
10047c478bd9Sstevel@tonic-gate 	 * The error cannot be anything else since we could have an non-zero
10057c478bd9Sstevel@tonic-gate 	 * error only if sctp_get_msg_to_send() tries to send a Forward
10067c478bd9Sstevel@tonic-gate 	 * TSN which will not happen here.
10077c478bd9Sstevel@tonic-gate 	 */
10087c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
10097c478bd9Sstevel@tonic-gate 	if (meta == NULL)
10107c478bd9Sstevel@tonic-gate 		goto sendcookie;
10117c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = meta;
10127c478bd9Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
10137c478bd9Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
10147c478bd9Sstevel@tonic-gate 	if ((ceclen + seglen) > fp->sfa_pmss ||
10157c478bd9Sstevel@tonic-gate 	    (seglen - sizeof (*sdc)) > cansend) {
10167c478bd9Sstevel@tonic-gate 		goto sendcookie;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	/* OK, if this fails */
10197c478bd9Sstevel@tonic-gate 	cemp->b_cont = dupmsg(mp);
10207c478bd9Sstevel@tonic-gate sendcookie:
1021df19b344Svi 	head = sctp_add_proto_hdr(sctp, fp, cemp, 0, NULL);
1022df19b344Svi 	if (head == NULL) {
1023df19b344Svi 		freemsg(cemp);
1024df19b344Svi 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1025df19b344Svi 		if (errmp != NULL)
1026df19b344Svi 			freeb(errmp);
1027df19b344Svi 		return;
1028df19b344Svi 	}
10297c478bd9Sstevel@tonic-gate 	/*
10307c478bd9Sstevel@tonic-gate 	 * Even if cookie-echo exceeds MTU for one of the hops, it'll
10317c478bd9Sstevel@tonic-gate 	 * have a chance of getting there.
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	if (fp->isv4) {
10347c478bd9Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
10357c478bd9Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	sctp->sctp_cookie_mp = dupmsg(head);
10407c478bd9Sstevel@tonic-gate 	/* Don't bundle, we will just resend init if this cookie is lost. */
10417c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp == NULL) {
10427c478bd9Sstevel@tonic-gate 		if (cemp->b_cont != NULL) {
10437c478bd9Sstevel@tonic-gate 			freemsg(cemp->b_cont);
10447c478bd9Sstevel@tonic-gate 			cemp->b_cont = NULL;
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 	} else if (cemp->b_cont != NULL) {
10477c478bd9Sstevel@tonic-gate 		ASSERT(mp != NULL && mp == meta->b_cont);
10487c478bd9Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(cemp->b_cont);
10497c478bd9Sstevel@tonic-gate 		cemp->b_wptr += pad;
10507c478bd9Sstevel@tonic-gate 		seglen -= sizeof (*sdc);
10517c478bd9Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, seglen, meta);
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 	if (errmp != NULL)
10547c478bd9Sstevel@tonic-gate 		linkb(head, errmp);
10557c478bd9Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_COOKIE_ECHOED;
10567c478bd9Sstevel@tonic-gate 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
10597c478bd9Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate int
10637c478bd9Sstevel@tonic-gate sctp_process_cookie(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *cmp,
10647c478bd9Sstevel@tonic-gate     sctp_init_chunk_t **iackpp, sctp_hdr_t *insctph, int *recv_adaption,
10657c478bd9Sstevel@tonic-gate     in6_addr_t *peer_addr)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	int32_t			clen;
10687c478bd9Sstevel@tonic-gate 	size_t			initplen;
10697c478bd9Sstevel@tonic-gate 	uchar_t			*p;
10707c478bd9Sstevel@tonic-gate 	uchar_t			*given_hash;
10717c478bd9Sstevel@tonic-gate 	uchar_t			needed_hash[16];
10727c478bd9Sstevel@tonic-gate 	int64_t			ts;
10737c478bd9Sstevel@tonic-gate 	int64_t			diff;
10747c478bd9Sstevel@tonic-gate 	uint32_t		*lt;
10757c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
10767c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*initch;
10777c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
10787c478bd9Sstevel@tonic-gate 	uint32_t		*lttag;
10797c478bd9Sstevel@tonic-gate 	uint32_t		*fttag;
10807c478bd9Sstevel@tonic-gate 	uint32_t		ports;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
10837c478bd9Sstevel@tonic-gate 	/* Verify the ICV */
10847c478bd9Sstevel@tonic-gate 	clen = ntohs(ch->sch_len) - sizeof (*ch) - 16;
10857c478bd9Sstevel@tonic-gate 	if (clen < 0) {
10867c478bd9Sstevel@tonic-gate 		dprint(1, ("invalid cookie chunk length %d\n",
10877c478bd9Sstevel@tonic-gate 		    ntohs(ch->sch_len)));
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		return (-1);
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 	p = (uchar_t *)(ch + 1);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	hmac_md5(p, clen, (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN,
10947c478bd9Sstevel@tonic-gate 	    needed_hash);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/* The given hash follows the cookie data */
10977c478bd9Sstevel@tonic-gate 	given_hash = p + clen;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (bcmp(given_hash, needed_hash, 16) != 0) {
11007c478bd9Sstevel@tonic-gate 		/* The secret may have changed; try the old secret */
11017c478bd9Sstevel@tonic-gate 		hmac_md5(p, clen, (uchar_t *)sctp->sctp_old_secret,
11027c478bd9Sstevel@tonic-gate 		    SCTP_SECRET_LEN, needed_hash);
11037c478bd9Sstevel@tonic-gate 		if (bcmp(given_hash, needed_hash, 16) != 0) {
11047c478bd9Sstevel@tonic-gate 			return (-1);
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/* Timestamp is int64_t, and we only guarantee 32-bit alignment */
11097c478bd9Sstevel@tonic-gate 	bcopy(p, &ts, sizeof (ts));
11107c478bd9Sstevel@tonic-gate 	/* Cookie life time, int32_t */
11117c478bd9Sstevel@tonic-gate 	lt = (uint32_t *)(p + sizeof (ts));
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * To quote PRC, "this is our baby", so let's continue.
11157c478bd9Sstevel@tonic-gate 	 * We need to pull out the encapsulated INIT ACK and
11167c478bd9Sstevel@tonic-gate 	 * INIT chunks. Note that we don't process these until
11177c478bd9Sstevel@tonic-gate 	 * we have verified the timestamp, but we need them before
11187c478bd9Sstevel@tonic-gate 	 * processing the timestamp since if the time check fails,
11197c478bd9Sstevel@tonic-gate 	 * we need to get the verification tag from the INIT in order
11207c478bd9Sstevel@tonic-gate 	 * to send a stale cookie error.
11217c478bd9Sstevel@tonic-gate 	 */
11227c478bd9Sstevel@tonic-gate 	lttag = (uint32_t *)(lt + 1);
11237c478bd9Sstevel@tonic-gate 	fttag = lttag + 1;
11247c478bd9Sstevel@tonic-gate 	if (peer_addr != NULL)
11257c478bd9Sstevel@tonic-gate 		bcopy(fttag + 1, peer_addr, sizeof (in6_addr_t));
11267c478bd9Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)((char *)(fttag + 1) + sizeof (in6_addr_t));
11277c478bd9Sstevel@tonic-gate 	initch = (sctp_chunk_hdr_t *)(iack + 1);
11287c478bd9Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(initch + 1);
11297c478bd9Sstevel@tonic-gate 	initplen = ntohs(initch->sch_len) - (sizeof (*init) + sizeof (*initch));
11307c478bd9Sstevel@tonic-gate 	*iackpp = iack;
11317c478bd9Sstevel@tonic-gate 	*recv_adaption = 0;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	/* Check the timestamp */
11347c478bd9Sstevel@tonic-gate 	diff = lbolt64 - ts;
11357c478bd9Sstevel@tonic-gate 	if (diff > *lt && (init->sic_inittag != sctp->sctp_fvtag ||
11367c478bd9Sstevel@tonic-gate 	    iack->sic_inittag != sctp->sctp_lvtag)) {
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		uint32_t staleness;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		staleness = TICK_TO_USEC(diff);
11417c478bd9Sstevel@tonic-gate 		staleness = htonl(staleness);
11427c478bd9Sstevel@tonic-gate 		sctp_send_abort(sctp, init->sic_inittag, SCTP_ERR_STALE_COOKIE,
11437c478bd9Sstevel@tonic-gate 		    (char *)&staleness, sizeof (staleness), cmp, 1, B_FALSE);
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		dprint(1, ("stale cookie %d\n", staleness));
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 		return (-1);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/* Check for attack by adding addresses to a restart */
11517c478bd9Sstevel@tonic-gate 	bcopy(insctph, &ports, sizeof (ports));
11527c478bd9Sstevel@tonic-gate 	if (sctp_secure_restart_check(cmp, initch, ports, KM_NOSLEEP) != 1) {
11537c478bd9Sstevel@tonic-gate 		return (-1);
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/* Look for adaptation code if there any parms in the INIT chunk */
11577c478bd9Sstevel@tonic-gate 	if ((initplen >= sizeof (sctp_parm_hdr_t)) &&
11587c478bd9Sstevel@tonic-gate 	    (sctp_find_al_ind((sctp_parm_hdr_t *)(init + 1), initplen,
11597c478bd9Sstevel@tonic-gate 	    &sctp->sctp_rx_adaption_code) == 0)) {
11607c478bd9Sstevel@tonic-gate 		*recv_adaption = 1;
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/* Examine tie-tags */
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_WAIT) {
11667c478bd9Sstevel@tonic-gate 		if (sctp->sctp_state == SCTPS_ESTABLISHED &&
11677c478bd9Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
11687c478bd9Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag &&
11697c478bd9Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 			dprint(1, ("duplicate cookie from %x:%x:%x:%x (%d)\n",
11727c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
11737c478bd9Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
11747c478bd9Sstevel@tonic-gate 			return (-1);
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		if (init->sic_inittag != sctp->sctp_fvtag &&
11787c478bd9Sstevel@tonic-gate 		    iack->sic_inittag != sctp->sctp_lvtag &&
11797c478bd9Sstevel@tonic-gate 		    *fttag == sctp->sctp_fvtag &&
11807c478bd9Sstevel@tonic-gate 		    *lttag == sctp->sctp_lvtag) {
11817c478bd9Sstevel@tonic-gate 			int i;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 			/* Section 5.2.4 case A: restart */
11847c478bd9Sstevel@tonic-gate 			sctp->sctp_fvtag = init->sic_inittag;
11857c478bd9Sstevel@tonic-gate 			sctp->sctp_lvtag = iack->sic_inittag;
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 			sctp->sctp_sctph->sh_verf = init->sic_inittag;
11887c478bd9Sstevel@tonic-gate 			sctp->sctp_sctph6->sh_verf = init->sic_inittag;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 			sctp->sctp_ftsn = ntohl(init->sic_inittsn);
11917c478bd9Sstevel@tonic-gate 			sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
11927c478bd9Sstevel@tonic-gate 			sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
11937c478bd9Sstevel@tonic-gate 			sctp->sctp_fcsn = sctp->sctp_lastacked;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
11967c478bd9Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
11977c478bd9Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
11987c478bd9Sstevel@tonic-gate 			}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 			dprint(1, ("sctp peer %x:%x:%x:%x (%d) restarted\n",
12017c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
12027c478bd9Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
12037c478bd9Sstevel@tonic-gate 			/* reset parameters */
12047c478bd9Sstevel@tonic-gate 			sctp_congest_reset(sctp);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 			/* reset stream bookkeeping */
12077c478bd9Sstevel@tonic-gate 			sctp_instream_cleanup(sctp, B_FALSE);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			sctp->sctp_istr_nmsgs = 0;
12107c478bd9Sstevel@tonic-gate 			sctp->sctp_rxqueued = 0;
12117c478bd9Sstevel@tonic-gate 			for (i = 0; i < sctp->sctp_num_ostr; i++) {
12127c478bd9Sstevel@tonic-gate 				sctp->sctp_ostrcntrs[i] = 0;
12137c478bd9Sstevel@tonic-gate 			}
12147c478bd9Sstevel@tonic-gate 			/* XXX flush xmit_list? */
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 			return (0);
12177c478bd9Sstevel@tonic-gate 		} else if (init->sic_inittag != sctp->sctp_fvtag &&
12187c478bd9Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 			/* Section 5.2.4 case B: INIT collision */
12217c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
12227c478bd9Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
12237c478bd9Sstevel@tonic-gate 					return (-1);	/* Drop? */
12247c478bd9Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
12257c478bd9Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
12267c478bd9Sstevel@tonic-gate 			}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 			dprint(1, ("init collision with %x:%x:%x:%x (%d)\n",
12297c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
12307c478bd9Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			return (0);
12337c478bd9Sstevel@tonic-gate 		} else if (iack->sic_inittag != sctp->sctp_lvtag &&
12347c478bd9Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
12357c478bd9Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 			/* Section 5.2.4 case C: late COOKIE */
12387c478bd9Sstevel@tonic-gate 			dprint(1, ("late cookie from %x:%x:%x:%x (%d)\n",
12397c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
12407c478bd9Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
12417c478bd9Sstevel@tonic-gate 			return (-1);
12427c478bd9Sstevel@tonic-gate 		} else if (init->sic_inittag == sctp->sctp_fvtag &&
12437c478bd9Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 			/*
12467c478bd9Sstevel@tonic-gate 			 * Section 5.2.4 case D: COOKIE ECHO retransmit
12477c478bd9Sstevel@tonic-gate 			 * Don't check cookie lifetime
12487c478bd9Sstevel@tonic-gate 			 */
12497c478bd9Sstevel@tonic-gate 			dprint(1, ("cookie tags match from %x:%x:%x:%x (%d)\n",
12507c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
12517c478bd9Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
12527c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
12537c478bd9Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
12547c478bd9Sstevel@tonic-gate 					return (-1);	/* Drop? */
12557c478bd9Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
12567c478bd9Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
12577c478bd9Sstevel@tonic-gate 			}
12587c478bd9Sstevel@tonic-gate 			return (0);
12597c478bd9Sstevel@tonic-gate 		} else {
12607c478bd9Sstevel@tonic-gate 			/* unrecognized case -- silently drop it */
12617c478bd9Sstevel@tonic-gate 			return (-1);
12627c478bd9Sstevel@tonic-gate 		}
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	return (0);
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate /*
12697c478bd9Sstevel@tonic-gate  * Similar to ip_fanout_sctp, except that the src addr(s) are drawn
12707c478bd9Sstevel@tonic-gate  * from address parameters in an INIT ACK's address list. This
12717c478bd9Sstevel@tonic-gate  * function is used when an INIT ACK is received but IP's fanout
12727c478bd9Sstevel@tonic-gate  * function could not find a sctp via the normal lookup routine.
12737c478bd9Sstevel@tonic-gate  * This can happen when a host sends an INIT ACK from a different
12747c478bd9Sstevel@tonic-gate  * address than the INIT was sent to.
12757c478bd9Sstevel@tonic-gate  *
12767c478bd9Sstevel@tonic-gate  * Returns the sctp_t if found, or NULL if not found.
12777c478bd9Sstevel@tonic-gate  */
12787c478bd9Sstevel@tonic-gate sctp_t *
12797c478bd9Sstevel@tonic-gate sctp_addrlist2sctp(mblk_t *mp, sctp_hdr_t *sctph, sctp_chunk_hdr_t *ich,
12807c478bd9Sstevel@tonic-gate     uint_t ipif_seqid, zoneid_t zoneid)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	int isv4;
12837c478bd9Sstevel@tonic-gate 	ipha_t *iph;
12847c478bd9Sstevel@tonic-gate 	ip6_t *ip6h;
12857c478bd9Sstevel@tonic-gate 	in6_addr_t dst;
12867c478bd9Sstevel@tonic-gate 	in6_addr_t src;
12877c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
12887c478bd9Sstevel@tonic-gate 	ssize_t remaining;
12897c478bd9Sstevel@tonic-gate 	sctp_init_chunk_t *iack;
12907c478bd9Sstevel@tonic-gate 	uint32_t ports;
12917c478bd9Sstevel@tonic-gate 	sctp_t *sctp = NULL;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	ASSERT(ich->sch_id == CHUNK_INIT_ACK);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
12967c478bd9Sstevel@tonic-gate 	if (isv4) {
12977c478bd9Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
12987c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, &dst);
12997c478bd9Sstevel@tonic-gate 	} else {
13007c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
13017c478bd9Sstevel@tonic-gate 		dst = ip6h->ip6_dst;
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	ports = *(uint32_t *)sctph;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	dprint(1, ("sctp_addrlist2sctp: ports=%u, dst = %x:%x:%x:%x\n",
13077c478bd9Sstevel@tonic-gate 	    ports, SCTP_PRINTADDR(dst)));
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	/* pull out any address parameters */
13107c478bd9Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) - sizeof (*iack);
13117c478bd9Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
13127c478bd9Sstevel@tonic-gate 		return (NULL);
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(ich + 1);
13167c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(iack + 1);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	while (ph != NULL) {
13197c478bd9Sstevel@tonic-gate 		/*
13207c478bd9Sstevel@tonic-gate 		 * params have been put in host byteorder by
13217c478bd9Sstevel@tonic-gate 		 * sctp_check_input()
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		if (ph->sph_type == PARM_ADDR4) {
13247c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED((struct in_addr *)(ph + 1),
13257c478bd9Sstevel@tonic-gate 			    &src);
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 			sctp = sctp_conn_match(&src, &dst, ports, ipif_seqid,
13287c478bd9Sstevel@tonic-gate 			    zoneid);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 			dprint(1,
13317c478bd9Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
1332*45916cd2Sjpk 			    SCTP_PRINTADDR(src), (void *)sctp));
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 			if (sctp != NULL) {
13367c478bd9Sstevel@tonic-gate 				return (sctp);
13377c478bd9Sstevel@tonic-gate 			}
13387c478bd9Sstevel@tonic-gate 		} else if (ph->sph_type == PARM_ADDR6) {
13397c478bd9Sstevel@tonic-gate 			src = *(in6_addr_t *)(ph + 1);
13407c478bd9Sstevel@tonic-gate 			sctp = sctp_conn_match(&src, &dst, ports, ipif_seqid,
13417c478bd9Sstevel@tonic-gate 			    zoneid);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 			dprint(1,
13447c478bd9Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
1345*45916cd2Sjpk 			    SCTP_PRINTADDR(src), (void *)sctp));
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 			if (sctp != NULL) {
13487c478bd9Sstevel@tonic-gate 				return (sctp);
13497c478bd9Sstevel@tonic-gate 			}
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	return (NULL);
13567c478bd9Sstevel@tonic-gate }
1357