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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211735Skcpoon 
220Sstevel@tonic-gate /*
236596Skp158701  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/stream.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/md5.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/strsubr.h>
340Sstevel@tonic-gate #include <sys/random.h>
351676Sjpk #include <sys/tsol/tnet.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <netinet/in.h>
380Sstevel@tonic-gate #include <netinet/ip6.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <inet/common.h>
410Sstevel@tonic-gate #include <inet/ip.h>
420Sstevel@tonic-gate #include <inet/ip6.h>
430Sstevel@tonic-gate #include <inet/sctp_ip.h>
440Sstevel@tonic-gate #include <inet/ipclassifier.h>
450Sstevel@tonic-gate #include "sctp_impl.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
48852Svi117747  * Helper function for SunCluster (PSARC/2005/602) to get the original source
49852Svi117747  * address from the COOKIE
50852Svi117747  */
51852Svi117747 int cl_sctp_cookie_paddr(sctp_chunk_hdr_t *, in6_addr_t *);
52852Svi117747 
53852Svi117747 /*
540Sstevel@tonic-gate  * From RFC 2104. This should probably go into libmd5 (and while
550Sstevel@tonic-gate  * we're at it, maybe we should make a libdigest so we can later
560Sstevel@tonic-gate  * add SHA1 and others, esp. since some weaknesses have been found
570Sstevel@tonic-gate  * with MD5).
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * text		IN			pointer to data stream
600Sstevel@tonic-gate  * text_len	IN			length of data stream
610Sstevel@tonic-gate  * key		IN			pointer to authentication key
620Sstevel@tonic-gate  * key_len	IN			length of authentication key
630Sstevel@tonic-gate  * digest	OUT			caller digest to be filled in
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static void
660Sstevel@tonic-gate hmac_md5(uchar_t *text, size_t text_len, uchar_t *key, size_t key_len,
670Sstevel@tonic-gate     uchar_t *digest)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	MD5_CTX context;
700Sstevel@tonic-gate 	uchar_t k_ipad[65];	/* inner padding - key XORd with ipad */
710Sstevel@tonic-gate 	uchar_t k_opad[65];	/* outer padding - key XORd with opad */
720Sstevel@tonic-gate 	uchar_t tk[16];
730Sstevel@tonic-gate 	int i;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/* if key is longer than 64 bytes reset it to key=MD5(key) */
760Sstevel@tonic-gate 	if (key_len > 64) {
770Sstevel@tonic-gate 		MD5_CTX tctx;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 		MD5Init(&tctx);
800Sstevel@tonic-gate 		MD5Update(&tctx, key, key_len);
810Sstevel@tonic-gate 		MD5Final(tk, &tctx);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		key = tk;
840Sstevel@tonic-gate 		key_len = 16;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * the HMAC_MD5 transform looks like:
890Sstevel@tonic-gate 	 *
900Sstevel@tonic-gate 	 * MD5(K XOR opad, MD5(K XOR ipad, text))
910Sstevel@tonic-gate 	 *
920Sstevel@tonic-gate 	 * where K is an n byte key
930Sstevel@tonic-gate 	 * ipad is the byte 0x36 repeated 64 times
940Sstevel@tonic-gate 	 * opad is the byte 0x5c repeated 64 times
950Sstevel@tonic-gate 	 * and text is the data being protected
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* start out by storing key in pads */
990Sstevel@tonic-gate 	bzero(k_ipad, sizeof (k_ipad));
1000Sstevel@tonic-gate 	bzero(k_opad, sizeof (k_opad));
1010Sstevel@tonic-gate 	bcopy(key, k_ipad, key_len);
1020Sstevel@tonic-gate 	bcopy(key, k_opad, key_len);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/* XOR key with ipad and opad values */
1050Sstevel@tonic-gate 	for (i = 0; i < 64; i++) {
1060Sstevel@tonic-gate 		k_ipad[i] ^= 0x36;
1070Sstevel@tonic-gate 		k_opad[i] ^= 0x5c;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	/*
1100Sstevel@tonic-gate 	 * perform inner MD5
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	MD5Init(&context);			/* init context for 1st */
1130Sstevel@tonic-gate 						/* pass */
1140Sstevel@tonic-gate 	MD5Update(&context, k_ipad, 64);	/* start with inner pad */
1150Sstevel@tonic-gate 	MD5Update(&context, text, text_len);	/* then text of datagram */
1160Sstevel@tonic-gate 	MD5Final(digest, &context);		/* finish up 1st pass */
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * perform outer MD5
1190Sstevel@tonic-gate 	 */
1200Sstevel@tonic-gate 	MD5Init(&context);			/* init context for 2nd */
1210Sstevel@tonic-gate 						/* pass */
1220Sstevel@tonic-gate 	MD5Update(&context, k_opad, 64);	/* start with outer pad */
1230Sstevel@tonic-gate 	MD5Update(&context, digest, 16);	/* then results of 1st */
1240Sstevel@tonic-gate 						/* hash */
1250Sstevel@tonic-gate 	MD5Final(digest, &context);		/* finish up 2nd pass */
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * If inmp is non-NULL, and we need to abort, it will use the IP/SCTP
1300Sstevel@tonic-gate  * info in initmp to send the abort. Otherwise, no abort will be sent.
131*8153SGeorge.Shepherd@Sun.COM  *
132*8153SGeorge.Shepherd@Sun.COM  * An ERROR chunk and chain of one or more error cause blocks will be
133*8153SGeorge.Shepherd@Sun.COM  * created if unrecognized parameters marked by the sender as reportable
134*8153SGeorge.Shepherd@Sun.COM  * are found. This error chain is visible to the caller via *errmp.
135*8153SGeorge.Shepherd@Sun.COM  *
136*8153SGeorge.Shepherd@Sun.COM  * When called from stcp_send_init_ack() while processing parameters
137*8153SGeorge.Shepherd@Sun.COM  * from a received INIT_CHUNK want_cookie will be NULL.
1380Sstevel@tonic-gate  *
139*8153SGeorge.Shepherd@Sun.COM  * When called from sctp_send_cookie_echo() while processing an INIT_ACK,
140*8153SGeorge.Shepherd@Sun.COM  * want_cookie contains a pointer to a pointer of type *sctp_parm_hdr_t.
141*8153SGeorge.Shepherd@Sun.COM  * However, this last pointer will be NULL until the cookie is processed
142*8153SGeorge.Shepherd@Sun.COM  * at which time it will be set to point to a sctp_parm_hdr_t that contains
143*8153SGeorge.Shepherd@Sun.COM  * the cookie info.
144*8153SGeorge.Shepherd@Sun.COM  *
145*8153SGeorge.Shepherd@Sun.COM  * Note: an INIT_ACK is expected to contain a cookie.
146*8153SGeorge.Shepherd@Sun.COM  *
147*8153SGeorge.Shepherd@Sun.COM  * Returns 1 if the parameters are OK (or if there are no optional
148*8153SGeorge.Shepherd@Sun.COM  * parameters), returns 0 otherwise.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate static int
1510Sstevel@tonic-gate validate_init_params(sctp_t *sctp, sctp_chunk_hdr_t *ch,
1520Sstevel@tonic-gate     sctp_init_chunk_t *init, mblk_t *inmp, sctp_parm_hdr_t **want_cookie,
1530Sstevel@tonic-gate     mblk_t **errmp, int *supp_af, uint_t *sctp_options)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
1560Sstevel@tonic-gate 	sctp_init_chunk_t	*ic;
1570Sstevel@tonic-gate 	ssize_t			remaining;
1580Sstevel@tonic-gate 	uint16_t		serror = 0;
1590Sstevel@tonic-gate 	char			*details = NULL;
1600Sstevel@tonic-gate 	size_t			errlen = 0;
1610Sstevel@tonic-gate 	boolean_t		got_cookie = B_FALSE;
162*8153SGeorge.Shepherd@Sun.COM 	boolean_t		got_errchunk = B_FALSE;
1630Sstevel@tonic-gate 	uint16_t		ptype;
1640Sstevel@tonic-gate 
165*8153SGeorge.Shepherd@Sun.COM 	ASSERT(errmp != NULL);
166*8153SGeorge.Shepherd@Sun.COM 
1670Sstevel@tonic-gate 	if (sctp_options != NULL)
1680Sstevel@tonic-gate 		*sctp_options = 0;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/* First validate stream parameters */
1710Sstevel@tonic-gate 	if (init->sic_instr == 0 || init->sic_outstr == 0) {
1720Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1736596Skp158701 		dprint(1, ("validate_init_params: bad sid, is=%d os=%d\n",
1745586Skcpoon 		    htons(init->sic_instr), htons(init->sic_outstr)));
1750Sstevel@tonic-gate 		goto abort;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	if (ntohl(init->sic_inittag) == 0) {
1780Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1790Sstevel@tonic-gate 		dprint(1, ("validate_init_params: inittag = 0\n"));
1800Sstevel@tonic-gate 		goto abort;
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	remaining = ntohs(ch->sch_len) - sizeof (*ch);
1840Sstevel@tonic-gate 	ic = (sctp_init_chunk_t *)(ch + 1);
1850Sstevel@tonic-gate 	remaining -= sizeof (*ic);
1860Sstevel@tonic-gate 	if (remaining < sizeof (*cph)) {
187*8153SGeorge.Shepherd@Sun.COM 		/*
188*8153SGeorge.Shepherd@Sun.COM 		 * When processing a received INIT_ACK, a cookie is
189*8153SGeorge.Shepherd@Sun.COM 		 * expected, if missing there is nothing to validate.
190*8153SGeorge.Shepherd@Sun.COM 		 */
1910Sstevel@tonic-gate 		if (want_cookie != NULL)
1920Sstevel@tonic-gate 			goto cookie_abort;
1930Sstevel@tonic-gate 		return (1);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	cph = (sctp_parm_hdr_t *)(ic + 1);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	while (cph != NULL) {
1990Sstevel@tonic-gate 		ptype = ntohs(cph->sph_type);
2000Sstevel@tonic-gate 		switch (ptype) {
2010Sstevel@tonic-gate 		case PARM_HBINFO:
2020Sstevel@tonic-gate 		case PARM_UNRECOGNIZED:
2030Sstevel@tonic-gate 		case PARM_ECN:
2040Sstevel@tonic-gate 			/* just ignore them */
2050Sstevel@tonic-gate 			break;
2060Sstevel@tonic-gate 		case PARM_FORWARD_TSN:
2070Sstevel@tonic-gate 			if (sctp_options != NULL)
2080Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
2090Sstevel@tonic-gate 			break;
2100Sstevel@tonic-gate 		case PARM_COOKIE:
2110Sstevel@tonic-gate 			got_cookie = B_TRUE;
212*8153SGeorge.Shepherd@Sun.COM 			/*
213*8153SGeorge.Shepherd@Sun.COM 			 * Processing a received INIT_ACK, we have a cookie
214*8153SGeorge.Shepherd@Sun.COM 			 * and a valid pointer in our caller to attach it to.
215*8153SGeorge.Shepherd@Sun.COM 			 */
2160Sstevel@tonic-gate 			if (want_cookie != NULL) {
2170Sstevel@tonic-gate 				*want_cookie = cph;
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate 			break;
2200Sstevel@tonic-gate 		case PARM_ADDR4:
221432Svi117747 			*supp_af |= PARM_SUPP_V4;
222432Svi117747 			break;
2230Sstevel@tonic-gate 		case PARM_ADDR6:
224432Svi117747 			*supp_af |= PARM_SUPP_V6;
225432Svi117747 			break;
2260Sstevel@tonic-gate 		case PARM_COOKIE_PRESERVE:
2270Sstevel@tonic-gate 		case PARM_ADAPT_LAYER_IND:
2280Sstevel@tonic-gate 			/* These are OK */
2290Sstevel@tonic-gate 			break;
2300Sstevel@tonic-gate 		case PARM_ADDR_HOST_NAME:
2310Sstevel@tonic-gate 			/* Don't support this; abort the association */
2320Sstevel@tonic-gate 			serror = SCTP_ERR_BAD_ADDR;
2330Sstevel@tonic-gate 			details = (char *)cph;
2340Sstevel@tonic-gate 			errlen = ntohs(cph->sph_len);
2350Sstevel@tonic-gate 			dprint(1, ("sctp:validate_init_params: host addr\n"));
2360Sstevel@tonic-gate 			goto abort;
2370Sstevel@tonic-gate 		case PARM_SUPP_ADDRS: {
2380Sstevel@tonic-gate 			/* Make sure we have a supported addr intersection */
2390Sstevel@tonic-gate 			uint16_t *p, addrtype;
2400Sstevel@tonic-gate 			int plen;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 			plen = ntohs(cph->sph_len);
2430Sstevel@tonic-gate 			p = (uint16_t *)(cph + 1);
2440Sstevel@tonic-gate 			while (plen > 0) {
2450Sstevel@tonic-gate 				addrtype = ntohs(*p);
2460Sstevel@tonic-gate 				switch (addrtype) {
2470Sstevel@tonic-gate 				case PARM_ADDR6:
2480Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V6;
2490Sstevel@tonic-gate 					break;
2500Sstevel@tonic-gate 				case PARM_ADDR4:
2510Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V4;
2520Sstevel@tonic-gate 					break;
2530Sstevel@tonic-gate 				default:
2540Sstevel@tonic-gate 					/*
2550Sstevel@tonic-gate 					 * Do nothing, silently ignore hostname
2560Sstevel@tonic-gate 					 * address.
2570Sstevel@tonic-gate 					 */
2580Sstevel@tonic-gate 					break;
2590Sstevel@tonic-gate 				}
2600Sstevel@tonic-gate 				p++;
2610Sstevel@tonic-gate 				plen -= sizeof (*p);
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 			break;
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 		default:
266*8153SGeorge.Shepherd@Sun.COM 			/*
267*8153SGeorge.Shepherd@Sun.COM 			 * Handle any unrecognized params, the two high order
268*8153SGeorge.Shepherd@Sun.COM 			 * bits of ptype define how the remote wants them
269*8153SGeorge.Shepherd@Sun.COM 			 * handled.
270*8153SGeorge.Shepherd@Sun.COM 			 * Top bit:
271*8153SGeorge.Shepherd@Sun.COM 			 *    1. Continue processing other params in the chunk
272*8153SGeorge.Shepherd@Sun.COM 			 *    0. Stop processing params after this one.
273*8153SGeorge.Shepherd@Sun.COM 			 * 2nd bit:
274*8153SGeorge.Shepherd@Sun.COM 			 *    1. Must report this unrecognized param to remote
275*8153SGeorge.Shepherd@Sun.COM 			 *    0. Obey the top bit silently.
276*8153SGeorge.Shepherd@Sun.COM 			 */
277*8153SGeorge.Shepherd@Sun.COM 			if (ptype & SCTP_REPORT_THIS_PARAM) {
278*8153SGeorge.Shepherd@Sun.COM 				if (!got_errchunk) {
279*8153SGeorge.Shepherd@Sun.COM 					/*
280*8153SGeorge.Shepherd@Sun.COM 					 * First reportable param, create an
281*8153SGeorge.Shepherd@Sun.COM 					 * ERROR chunk and populate it with a
282*8153SGeorge.Shepherd@Sun.COM 					 * cause block for this parameter.
283*8153SGeorge.Shepherd@Sun.COM 					 */
284*8153SGeorge.Shepherd@Sun.COM 					*errmp = sctp_make_err(sctp,
285*8153SGeorge.Shepherd@Sun.COM 					    PARM_UNRECOGNIZED,
286*8153SGeorge.Shepherd@Sun.COM 					    (void *)cph,
287*8153SGeorge.Shepherd@Sun.COM 					    ntohs(cph->sph_len));
288*8153SGeorge.Shepherd@Sun.COM 					got_errchunk = B_TRUE;
289*8153SGeorge.Shepherd@Sun.COM 				} else {
290*8153SGeorge.Shepherd@Sun.COM 					/*
291*8153SGeorge.Shepherd@Sun.COM 					 * Add an additional cause block to
292*8153SGeorge.Shepherd@Sun.COM 					 * an existing ERROR chunk.
293*8153SGeorge.Shepherd@Sun.COM 					 */
294*8153SGeorge.Shepherd@Sun.COM 					sctp_add_unrec_parm(cph, errmp);
295*8153SGeorge.Shepherd@Sun.COM 				}
296*8153SGeorge.Shepherd@Sun.COM 			}
297*8153SGeorge.Shepherd@Sun.COM 			if (ptype & SCTP_CONT_PROC_PARAMS) {
2980Sstevel@tonic-gate 				/*
299*8153SGeorge.Shepherd@Sun.COM 				 * Continue processing params after this
300*8153SGeorge.Shepherd@Sun.COM 				 * parameter.
3010Sstevel@tonic-gate 				 */
3020Sstevel@tonic-gate 				break;
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 			/*
306*8153SGeorge.Shepherd@Sun.COM 			 * Stop processing params, report any reportable
307*8153SGeorge.Shepherd@Sun.COM 			 * unrecognized params found so far.
3080Sstevel@tonic-gate 			 */
309*8153SGeorge.Shepherd@Sun.COM 			goto done;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		cph = sctp_next_parm(cph, &remaining);
3130Sstevel@tonic-gate 	}
314*8153SGeorge.Shepherd@Sun.COM done:
315432Svi117747 	/*
316432Svi117747 	 * Some sanity checks.  The following should not fail unless the
317432Svi117747 	 * other side is broken.
318432Svi117747 	 *
319432Svi117747 	 * 1. If this is a V4 endpoint but V4 address is not
320432Svi117747 	 * supported, abort.
321432Svi117747 	 * 2. If this is a V6 only endpoint but V6 address is
322432Svi117747 	 * not supported, abort.  This assumes that a V6
323432Svi117747 	 * endpoint can use both V4 and V6 addresses.
324432Svi117747 	 * We only care about supp_af when processing INIT, i.e want_cookie
325432Svi117747 	 * is NULL.
326432Svi117747 	 */
327432Svi117747 	if (want_cookie == NULL &&
328432Svi117747 	    ((sctp->sctp_family == AF_INET && !(*supp_af & PARM_SUPP_V4)) ||
329432Svi117747 	    (sctp->sctp_family == AF_INET6 && !(*supp_af & PARM_SUPP_V6) &&
330432Svi117747 	    sctp->sctp_connp->conn_ipv6_v6only))) {
331432Svi117747 		dprint(1, ("sctp:validate_init_params: supp addr\n"));
332432Svi117747 		serror = SCTP_ERR_BAD_ADDR;
333432Svi117747 		goto abort;
334432Svi117747 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if (want_cookie != NULL && !got_cookie) {
3370Sstevel@tonic-gate cookie_abort:
3380Sstevel@tonic-gate 		dprint(1, ("validate_init_params: cookie absent\n"));
3390Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch), SCTP_ERR_MISSING_PARM,
3400Sstevel@tonic-gate 		    details, errlen, inmp, 0, B_FALSE);
3410Sstevel@tonic-gate 		return (0);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/* OK */
3450Sstevel@tonic-gate 	return (1);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate abort:
3480Sstevel@tonic-gate 	if (want_cookie != NULL)
3490Sstevel@tonic-gate 		return (0);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	sctp_send_abort(sctp, sctp_init2vtag(ch), serror, details,
3520Sstevel@tonic-gate 	    errlen, inmp, 0, B_FALSE);
3530Sstevel@tonic-gate 	return (0);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate  * Initialize params from the INIT and INIT-ACK when the assoc. is
3580Sstevel@tonic-gate  * established.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate boolean_t
3610Sstevel@tonic-gate sctp_initialize_params(sctp_t *sctp, sctp_init_chunk_t *init,
3620Sstevel@tonic-gate     sctp_init_chunk_t *iack)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	/* Get initial TSN */
3650Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(init->sic_inittsn);
3660Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/* Serial number is initialized to the same value as the TSN */
3690Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/*
3720Sstevel@tonic-gate 	 * Get verification tags; no byteordering is necessary, since
3730Sstevel@tonic-gate 	 * verfication tags are never processed except for byte-by-byte
3740Sstevel@tonic-gate 	 * comparisons.
3750Sstevel@tonic-gate 	 */
3760Sstevel@tonic-gate 	sctp->sctp_fvtag = init->sic_inittag;
3770Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = init->sic_inittag;
3780Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = init->sic_inittag;
3790Sstevel@tonic-gate 	sctp->sctp_lvtag = iack->sic_inittag;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/* Get the peer's rwnd */
3820Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/* Allocate the in/out-stream counters */
3850Sstevel@tonic-gate 	sctp->sctp_num_ostr = iack->sic_outstr;
3860Sstevel@tonic-gate 	sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
3870Sstevel@tonic-gate 	    sctp->sctp_num_ostr, KM_NOSLEEP);
3880Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL)
3890Sstevel@tonic-gate 		return (B_FALSE);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	sctp->sctp_num_istr = iack->sic_instr;
3920Sstevel@tonic-gate 	sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
3930Sstevel@tonic-gate 	    sctp->sctp_num_istr, KM_NOSLEEP);
3940Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
3950Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
3960Sstevel@tonic-gate 		    sctp->sctp_num_ostr);
3970Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
3980Sstevel@tonic-gate 		return (B_FALSE);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	return (B_TRUE);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
403852Svi117747 /*
404852Svi117747  * Copy the peer's original source address into addr. This relies on the
405852Svi117747  * following format (see sctp_send_initack() below):
406852Svi117747  * 	relative timestamp for the cookie (int64_t) +
407852Svi117747  * 	cookie lifetime (uint32_t) +
408852Svi117747  * 	local tie-tag (uint32_t) +  peer tie-tag (uint32_t) +
409852Svi117747  * 	Peer's original src ...
410852Svi117747  */
411852Svi117747 int
412852Svi117747 cl_sctp_cookie_paddr(sctp_chunk_hdr_t *ch, in6_addr_t *addr)
413852Svi117747 {
414852Svi117747 	uchar_t	*off;
415852Svi117747 
416852Svi117747 	ASSERT(addr != NULL);
417852Svi117747 
418852Svi117747 	if (ch->sch_id != CHUNK_COOKIE)
419852Svi117747 		return (EINVAL);
420852Svi117747 
421852Svi117747 	off = (uchar_t *)ch + sizeof (*ch) + sizeof (int64_t) +
422852Svi117747 	    sizeof (uint32_t) + sizeof (uint32_t) + sizeof (uint32_t);
423852Svi117747 
424852Svi117747 	bcopy(off, addr, sizeof (*addr));
425852Svi117747 
426852Svi117747 	return (0);
427852Svi117747 }
428852Svi117747 
4290Sstevel@tonic-gate #define	SCTP_CALC_COOKIE_LEN(initcp) \
4300Sstevel@tonic-gate 	sizeof (int64_t) +		/* timestamp */			\
4310Sstevel@tonic-gate 	sizeof (uint32_t) +		/* cookie lifetime */		\
4320Sstevel@tonic-gate 	sizeof (sctp_init_chunk_t) +	/* INIT ACK */			\
4330Sstevel@tonic-gate 	sizeof (in6_addr_t) +		/* peer's original source */ 	\
4340Sstevel@tonic-gate 	ntohs((initcp)->sch_len) +	/* peer's INIT */		\
4350Sstevel@tonic-gate 	sizeof (uint32_t) +		/* local tie-tag */		\
4360Sstevel@tonic-gate 	sizeof (uint32_t) +		/* peer tie-tag */		\
4370Sstevel@tonic-gate 	sizeof (sctp_parm_hdr_t) +	/* param header */		\
4380Sstevel@tonic-gate 	16				/* MD5 hash */
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate void
4412776Skp158701 sctp_send_initack(sctp_t *sctp, sctp_hdr_t *initsh, sctp_chunk_hdr_t *ch,
4422776Skp158701     mblk_t *initmp)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	ipha_t			*initiph;
4450Sstevel@tonic-gate 	ip6_t			*initip6h;
4460Sstevel@tonic-gate 	ipha_t			*iackiph;
4470Sstevel@tonic-gate 	ip6_t			*iackip6h;
4480Sstevel@tonic-gate 	sctp_chunk_hdr_t	*iack_ch;
4490Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
4500Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
4510Sstevel@tonic-gate 	sctp_hdr_t		*iacksh;
4520Sstevel@tonic-gate 	size_t			cookielen;
4530Sstevel@tonic-gate 	size_t			iacklen;
4540Sstevel@tonic-gate 	size_t			ipsctplen;
4550Sstevel@tonic-gate 	size_t			errlen = 0;
4560Sstevel@tonic-gate 	sctp_parm_hdr_t		*cookieph;
4570Sstevel@tonic-gate 	mblk_t			*iackmp;
4580Sstevel@tonic-gate 	uint32_t		itag;
4590Sstevel@tonic-gate 	uint32_t		itsn;
4600Sstevel@tonic-gate 	int64_t			*now;
4610Sstevel@tonic-gate 	int64_t			nowt;
4620Sstevel@tonic-gate 	uint32_t		*lifetime;
4630Sstevel@tonic-gate 	char			*p;
4640Sstevel@tonic-gate 	boolean_t		 isv4;
465432Svi117747 	int			supp_af = 0;
4660Sstevel@tonic-gate 	uint_t			sctp_options;
4670Sstevel@tonic-gate 	uint32_t		*ttag;
4680Sstevel@tonic-gate 	int			pad;
4690Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
4700Sstevel@tonic-gate 	boolean_t		initcollision = B_FALSE;
471432Svi117747 	boolean_t		linklocal = B_FALSE;
4721676Sjpk 	cred_t			*cr;
4732776Skp158701 	ts_label_t		*initlabel;
4743448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
4770Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(initmp->b_rptr) == IPV4_VERSION);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* Extract the INIT chunk */
4800Sstevel@tonic-gate 	if (isv4) {
4810Sstevel@tonic-gate 		initiph = (ipha_t *)initmp->b_rptr;
4820Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr_len;
483432Svi117747 		supp_af |= PARM_SUPP_V4;
4840Sstevel@tonic-gate 	} else {
4850Sstevel@tonic-gate 		initip6h = (ip6_t *)initmp->b_rptr;
4860Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr6_len;
487432Svi117747 		if (IN6_IS_ADDR_LINKLOCAL(&initip6h->ip6_src))
488432Svi117747 			linklocal = B_TRUE;
489432Svi117747 		supp_af |= PARM_SUPP_V6;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 	ASSERT(OK_32PTR(initsh));
4920Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)((char *)(initsh + 1) + sizeof (*iack_ch));
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/* Make sure we like the peer's parameters */
4950Sstevel@tonic-gate 	if (validate_init_params(sctp, ch, init, initmp, NULL, &errmp,
4965586Skcpoon 	    &supp_af, &sctp_options) == 0) {
4970Sstevel@tonic-gate 		return;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 	if (errmp != NULL)
5000Sstevel@tonic-gate 		errlen = msgdsize(errmp);
5010Sstevel@tonic-gate 	if (sctp->sctp_family == AF_INET) {
5020Sstevel@tonic-gate 		/*
5030Sstevel@tonic-gate 		 * Irregardless of the supported address in the INIT, v4
5040Sstevel@tonic-gate 		 * must be supported.
5050Sstevel@tonic-gate 		 */
5060Sstevel@tonic-gate 		supp_af = PARM_SUPP_V4;
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_LISTEN) {
5090Sstevel@tonic-gate 		/* normal, expected INIT: generate new vtag and itsn */
5100Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
5110Sstevel@tonic-gate 		if (itag == 0)
5120Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
5130Sstevel@tonic-gate 		itsn = itag + 1;
5140Sstevel@tonic-gate 		itag = htonl(itag);
5150Sstevel@tonic-gate 	} else if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
5160Sstevel@tonic-gate 	    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
5170Sstevel@tonic-gate 		/* init collision; copy vtag and itsn from sctp */
5180Sstevel@tonic-gate 		itag = sctp->sctp_lvtag;
5190Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
5200Sstevel@tonic-gate 		/*
5210Sstevel@tonic-gate 		 * In addition we need to send all the params that was sent
5220Sstevel@tonic-gate 		 * in our INIT chunk. Essentially, it is only the supported
5230Sstevel@tonic-gate 		 * address params that we need to add.
5240Sstevel@tonic-gate 		 */
5250Sstevel@tonic-gate 		initcollision = B_TRUE;
526432Svi117747 		/*
527432Svi117747 		 * When we sent the INIT, we should have set linklocal in
528432Svi117747 		 * the sctp which should be good enough.
529432Svi117747 		 */
530432Svi117747 		if (linklocal)
531432Svi117747 			linklocal = B_FALSE;
5320Sstevel@tonic-gate 	} else {
5330Sstevel@tonic-gate 		/* peer restart; generate new vtag but keep everything else */
5340Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
5350Sstevel@tonic-gate 		if (itag == 0)
5360Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
5370Sstevel@tonic-gate 		itag = htonl(itag);
5380Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/*
5420Sstevel@tonic-gate 	 * Allocate a mblk for the INIT ACK, consisting of the link layer
5430Sstevel@tonic-gate 	 * header, the IP header, the SCTP common header, and INIT ACK chunk,
5440Sstevel@tonic-gate 	 * and finally the COOKIE parameter.
5450Sstevel@tonic-gate 	 */
5460Sstevel@tonic-gate 	cookielen = SCTP_CALC_COOKIE_LEN(ch);
5470Sstevel@tonic-gate 	iacklen = sizeof (*iack_ch) + sizeof (*iack) + cookielen;
5485586Skcpoon 	if (sctp->sctp_send_adaptation)
5490Sstevel@tonic-gate 		iacklen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
5500Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
5513448Sdh155122 	    sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled) {
5520Sstevel@tonic-gate 		iacklen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	if (initcollision)
5550Sstevel@tonic-gate 		iacklen += sctp_supaddr_param_len(sctp);
556432Svi117747 	if (!linklocal)
5573795Skcpoon 		iacklen += sctp_addr_params(sctp, supp_af, NULL, B_FALSE);
5580Sstevel@tonic-gate 	ipsctplen += sizeof (*iacksh) + iacklen;
5590Sstevel@tonic-gate 	iacklen += errlen;
5600Sstevel@tonic-gate 	if ((pad = ipsctplen % 4) != 0) {
5610Sstevel@tonic-gate 		pad = 4 - pad;
5620Sstevel@tonic-gate 		ipsctplen += pad;
5630Sstevel@tonic-gate 	}
5642776Skp158701 
5652776Skp158701 	/*
5662776Skp158701 	 * If the listen socket is bound to a trusted extensions
5672776Skp158701 	 * multi-label port, attach a copy of the listener's cred
5682776Skp158701 	 * to the new INITACK mblk. Modify the cred to contain
5692776Skp158701 	 * the security label of the received INIT packet.
5702776Skp158701 	 * If not a multi-label port, attach the unmodified
5712776Skp158701 	 * listener's cred directly.
5722776Skp158701 	 *
5732776Skp158701 	 * We expect Sun developed kernel modules to properly set
5742776Skp158701 	 * cred labels for sctp connections. We can't be so sure this
5752776Skp158701 	 * will be done correctly when 3rd party kernel modules
5762776Skp158701 	 * directly use sctp. The initlabel panic guard logic was
5772776Skp158701 	 * added to cover this possibility.
5782776Skp158701 	 */
5792776Skp158701 	if (sctp->sctp_connp->conn_mlp_type != mlptSingle) {
5802776Skp158701 		initlabel = MBLK_GETLABEL(initmp);
5812776Skp158701 		if (initlabel == NULL) {
5822776Skp158701 			sctp_send_abort(sctp, sctp_init2vtag(ch),
5832776Skp158701 			    SCTP_ERR_UNKNOWN, NULL, 0, initmp, 0, B_FALSE);
5842776Skp158701 			return;
5852776Skp158701 		}
5862776Skp158701 		cr = copycred_from_bslabel(CONN_CRED(sctp->sctp_connp),
5872776Skp158701 		    &initlabel->tsl_label, initlabel->tsl_doi, KM_NOSLEEP);
5882776Skp158701 		if (cr == NULL) {
5892776Skp158701 			sctp_send_abort(sctp, sctp_init2vtag(ch),
5902776Skp158701 			    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE);
5912776Skp158701 			return;
5922776Skp158701 		}
5933448Sdh155122 		iackmp = allocb_cred(ipsctplen + sctps->sctps_wroff_xtra, cr);
5942776Skp158701 		crfree(cr);
5952776Skp158701 	} else {
5963448Sdh155122 		iackmp = allocb_cred(ipsctplen + sctps->sctps_wroff_xtra,
5972776Skp158701 		    CONN_CRED(sctp->sctp_connp));
5982776Skp158701 	}
5990Sstevel@tonic-gate 	if (iackmp == NULL) {
6000Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch),
6010Sstevel@tonic-gate 		    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE);
6020Sstevel@tonic-gate 		return;
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/* Copy in the [imcomplete] IP/SCTP composite header */
6063448Sdh155122 	p = (char *)(iackmp->b_rptr + sctps->sctps_wroff_xtra);
6070Sstevel@tonic-gate 	iackmp->b_rptr = (uchar_t *)p;
6080Sstevel@tonic-gate 	if (isv4) {
6090Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, p, sctp->sctp_hdr_len);
6100Sstevel@tonic-gate 		iackiph = (ipha_t *)p;
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 		/* Copy the peer's IP addr */
6130Sstevel@tonic-gate 		iackiph->ipha_dst = initiph->ipha_src;
6140Sstevel@tonic-gate 		iackiph->ipha_src = initiph->ipha_dst;
6150Sstevel@tonic-gate 		iackiph->ipha_length = htons(ipsctplen + errlen);
6160Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr_len);
6170Sstevel@tonic-gate 	} else {
6180Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, p, sctp->sctp_hdr6_len);
6190Sstevel@tonic-gate 		iackip6h = (ip6_t *)p;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		/* Copy the peer's IP addr */
6220Sstevel@tonic-gate 		iackip6h->ip6_dst = initip6h->ip6_src;
6230Sstevel@tonic-gate 		iackip6h->ip6_src = initip6h->ip6_dst;
6240Sstevel@tonic-gate 		iackip6h->ip6_plen = htons(ipsctplen - sizeof (*iackip6h) +
6250Sstevel@tonic-gate 		    errlen);
6260Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr6_len);
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 	ASSERT(OK_32PTR(iacksh));
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/* Fill in the holes in the SCTP common header */
6310Sstevel@tonic-gate 	iacksh->sh_sport = initsh->sh_dport;
6320Sstevel@tonic-gate 	iacksh->sh_dport = initsh->sh_sport;
6330Sstevel@tonic-gate 	iacksh->sh_verf = init->sic_inittag;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/* INIT ACK chunk header */
6360Sstevel@tonic-gate 	iack_ch = (sctp_chunk_hdr_t *)(iacksh + 1);
6370Sstevel@tonic-gate 	iack_ch->sch_id = CHUNK_INIT_ACK;
6380Sstevel@tonic-gate 	iack_ch->sch_flags = 0;
6390Sstevel@tonic-gate 	iack_ch->sch_len = htons(iacklen);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/* The INIT ACK itself */
6420Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iack_ch + 1);
6430Sstevel@tonic-gate 	iack->sic_inittag = itag;	/* already in network byteorder */
6440Sstevel@tonic-gate 	iack->sic_inittsn = htonl(itsn);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	iack->sic_a_rwnd = htonl(sctp->sctp_rwnd);
6470Sstevel@tonic-gate 	/* Advertise what we would want to have as stream #'s */
6480Sstevel@tonic-gate 	iack->sic_outstr = htons(MIN(sctp->sctp_num_ostr,
6490Sstevel@tonic-gate 	    ntohs(init->sic_instr)));
6500Sstevel@tonic-gate 	iack->sic_instr = htons(sctp->sctp_num_istr);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	p = (char *)(iack + 1);
6535586Skcpoon 	p += sctp_adaptation_code_param(sctp, (uchar_t *)p);
6540Sstevel@tonic-gate 	if (initcollision)
6550Sstevel@tonic-gate 		p += sctp_supaddr_param(sctp, (uchar_t *)p);
656432Svi117747 	if (!linklocal)
6573795Skcpoon 		p += sctp_addr_params(sctp, supp_af, (uchar_t *)p, B_FALSE);
6580Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
6593448Sdh155122 	    sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled) {
6600Sstevel@tonic-gate 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 	/*
6630Sstevel@tonic-gate 	 * Generate and lay in the COOKIE parameter.
6640Sstevel@tonic-gate 	 *
665852Svi117747 	 * Any change here that results in a change of location for
666852Svi117747 	 * the peer's orig source address must be propagated to the fn
667852Svi117747 	 * cl_sctp_cookie_paddr() above.
668852Svi117747 	 *
6690Sstevel@tonic-gate 	 * The cookie consists of:
6700Sstevel@tonic-gate 	 * 1. The relative timestamp for the cookie (lbolt64)
6710Sstevel@tonic-gate 	 * 2. The cookie lifetime (uint32_t) in tick
6720Sstevel@tonic-gate 	 * 3. The local tie-tag
6730Sstevel@tonic-gate 	 * 4. The peer tie-tag
6740Sstevel@tonic-gate 	 * 5. Peer's original src, used to confirm the validity of address.
6750Sstevel@tonic-gate 	 * 6. Our INIT ACK chunk, less any parameters
6760Sstevel@tonic-gate 	 * 7. The INIT chunk (may contain parameters)
6770Sstevel@tonic-gate 	 * 8. 128-bit MD5 signature.
6780Sstevel@tonic-gate 	 *
6790Sstevel@tonic-gate 	 * Since the timestamp values will only be evaluated locally, we
6800Sstevel@tonic-gate 	 * don't need to worry about byte-ordering them.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	cookieph = (sctp_parm_hdr_t *)p;
6830Sstevel@tonic-gate 	cookieph->sph_type = htons(PARM_COOKIE);
6840Sstevel@tonic-gate 	cookieph->sph_len = htons(cookielen);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	/* timestamp */
6870Sstevel@tonic-gate 	now = (int64_t *)(cookieph + 1);
6880Sstevel@tonic-gate 	nowt = lbolt64;
6890Sstevel@tonic-gate 	bcopy(&nowt, now, sizeof (*now));
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/* cookie lifetime -- need configuration */
6920Sstevel@tonic-gate 	lifetime = (uint32_t *)(now + 1);
6930Sstevel@tonic-gate 	*lifetime = sctp->sctp_cookie_lifetime;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/* Set the tie-tags */
6960Sstevel@tonic-gate 	ttag = (uint32_t *)(lifetime + 1);
6970Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_COOKIE_WAIT) {
6980Sstevel@tonic-gate 		*ttag = 0;
6990Sstevel@tonic-gate 		ttag++;
7000Sstevel@tonic-gate 		*ttag = 0;
7010Sstevel@tonic-gate 		ttag++;
7020Sstevel@tonic-gate 	} else {
7030Sstevel@tonic-gate 		/* local tie-tag (network byte-order) */
7040Sstevel@tonic-gate 		*ttag = sctp->sctp_lvtag;
7050Sstevel@tonic-gate 		ttag++;
7060Sstevel@tonic-gate 		/* peer tie-tag (network byte-order) */
7070Sstevel@tonic-gate 		*ttag = sctp->sctp_fvtag;
7080Sstevel@tonic-gate 		ttag++;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * Copy in peer's original source address so that we can confirm
7120Sstevel@tonic-gate 	 * the reachability later.
7130Sstevel@tonic-gate 	 */
7140Sstevel@tonic-gate 	p = (char *)ttag;
7150Sstevel@tonic-gate 	if (isv4) {
7160Sstevel@tonic-gate 		in6_addr_t peer_addr;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iackiph->ipha_dst, &peer_addr);
7190Sstevel@tonic-gate 		bcopy(&peer_addr, p, sizeof (in6_addr_t));
7200Sstevel@tonic-gate 	} else {
7210Sstevel@tonic-gate 		bcopy(&iackip6h->ip6_dst, p, sizeof (in6_addr_t));
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 	p += sizeof (in6_addr_t);
7240Sstevel@tonic-gate 	/* Copy in our INIT ACK chunk */
7250Sstevel@tonic-gate 	bcopy(iack, p, sizeof (*iack));
7260Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)p;
7270Sstevel@tonic-gate 	/* Set the # of streams we'll end up using */
7280Sstevel@tonic-gate 	iack->sic_outstr = MIN(sctp->sctp_num_ostr, ntohs(init->sic_instr));
7290Sstevel@tonic-gate 	iack->sic_instr = MIN(sctp->sctp_num_istr, ntohs(init->sic_outstr));
7300Sstevel@tonic-gate 	p += sizeof (*iack);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/* Copy in the peer's INIT chunk */
7330Sstevel@tonic-gate 	bcopy(ch, p, ntohs(ch->sch_len));
7340Sstevel@tonic-gate 	p += ntohs(ch->sch_len);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Calculate the HMAC ICV into the digest slot in buf.
7380Sstevel@tonic-gate 	 * First, generate a new secret if the current secret is
7390Sstevel@tonic-gate 	 * older than the new secret lifetime parameter permits,
7400Sstevel@tonic-gate 	 * copying the current secret to sctp_old_secret.
7410Sstevel@tonic-gate 	 */
7423448Sdh155122 	if (sctps->sctps_new_secret_interval > 0 &&
7430Sstevel@tonic-gate 	    (sctp->sctp_last_secret_update +
7443448Sdh155122 	    MSEC_TO_TICK(sctps->sctps_new_secret_interval)) <= nowt) {
7450Sstevel@tonic-gate 		bcopy(sctp->sctp_secret, sctp->sctp_old_secret,
7460Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
7470Sstevel@tonic-gate 		(void) random_get_pseudo_bytes(sctp->sctp_secret,
7480Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
7490Sstevel@tonic-gate 		sctp->sctp_last_secret_update = nowt;
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	hmac_md5((uchar_t *)now, cookielen - sizeof (*cookieph) - 16,
7530Sstevel@tonic-gate 	    (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN, (uchar_t *)p);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	iackmp->b_wptr = iackmp->b_rptr + ipsctplen;
7560Sstevel@tonic-gate 	iackmp->b_cont = errmp;		/*  OK if NULL */
7570Sstevel@tonic-gate 
7582776Skp158701 	if (is_system_labeled() && (cr = DB_CRED(iackmp)) != NULL &&
7591676Sjpk 	    crgetlabel(cr) != NULL) {
7601676Sjpk 		conn_t *connp = sctp->sctp_connp;
7616596Skp158701 		int err;
7621676Sjpk 
7631676Sjpk 		if (isv4)
7646596Skp158701 			err = tsol_check_label(cr, &iackmp,
7653448Sdh155122 			    connp->conn_mac_exempt,
7663448Sdh155122 			    sctps->sctps_netstack->netstack_ip);
7671676Sjpk 		else
7686596Skp158701 			err = tsol_check_label_v6(cr, &iackmp,
7693448Sdh155122 			    connp->conn_mac_exempt,
7703448Sdh155122 			    sctps->sctps_netstack->netstack_ip);
7711676Sjpk 		if (err != 0) {
7721676Sjpk 			sctp_send_abort(sctp, sctp_init2vtag(ch),
7731676Sjpk 			    SCTP_ERR_AUTH_ERR, NULL, 0, initmp, 0, B_FALSE);
7741676Sjpk 			freemsg(iackmp);
7751676Sjpk 			return;
7761676Sjpk 		}
7771676Sjpk 	}
7781676Sjpk 
7790Sstevel@tonic-gate 	/*
7800Sstevel@tonic-gate 	 * Stash the conn ptr info. for IP only as e don't have any
7810Sstevel@tonic-gate 	 * cached IRE.
7820Sstevel@tonic-gate 	 */
7830Sstevel@tonic-gate 	SCTP_STASH_IPINFO(iackmp, (ire_t *)NULL);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/* XXX sctp == sctp_g_q, so using its obchunks is valid */
7860Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_opkts);
7870Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/* OK to call IP_PUT() here instead of sctp_add_sendq(). */
7900Sstevel@tonic-gate 	CONN_INC_REF(sctp->sctp_connp);
7910Sstevel@tonic-gate 	iackmp->b_flag |= MSGHASREF;
7920Sstevel@tonic-gate 	IP_PUT(iackmp, sctp->sctp_connp, isv4);
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate void
7960Sstevel@tonic-gate sctp_send_cookie_ack(sctp_t *sctp)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	sctp_chunk_hdr_t *cach;
7990Sstevel@tonic-gate 	mblk_t *camp;
8003448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	camp = sctp_make_mp(sctp, NULL, sizeof (*cach));
8030Sstevel@tonic-gate 	if (camp == NULL) {
8040Sstevel@tonic-gate 		/* XXX should abort, but don't have the inmp anymore */
8053448Sdh155122 		SCTP_KSTAT(sctps, sctp_send_cookie_ack_failed);
8060Sstevel@tonic-gate 		return;
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	cach = (sctp_chunk_hdr_t *)camp->b_wptr;
8100Sstevel@tonic-gate 	camp->b_wptr = (uchar_t *)(cach + 1);
8110Sstevel@tonic-gate 	cach->sch_id = CHUNK_COOKIE_ACK;
8120Sstevel@tonic-gate 	cach->sch_flags = 0;
8130Sstevel@tonic-gate 	cach->sch_len = htons(sizeof (*cach));
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	sctp_set_iplen(sctp, camp);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	sctp_add_sendq(sctp, camp);
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate static int
8235586Skcpoon sctp_find_al_ind(sctp_parm_hdr_t *sph, ssize_t len, uint32_t *adaptation_code)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (len < sizeof (*sph))
8270Sstevel@tonic-gate 		return (-1);
8280Sstevel@tonic-gate 	while (sph != NULL) {
8290Sstevel@tonic-gate 		if (sph->sph_type == htons(PARM_ADAPT_LAYER_IND) &&
8300Sstevel@tonic-gate 		    ntohs(sph->sph_len) >= (sizeof (*sph) +
8315586Skcpoon 		    sizeof (uint32_t))) {
8325586Skcpoon 			*adaptation_code = *(uint32_t *)(sph + 1);
8330Sstevel@tonic-gate 			return (0);
8340Sstevel@tonic-gate 		}
8350Sstevel@tonic-gate 		sph = sctp_next_parm(sph, &len);
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 	return (-1);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate void
8410Sstevel@tonic-gate sctp_send_cookie_echo(sctp_t *sctp, sctp_chunk_hdr_t *iackch, mblk_t *iackmp)
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate 	mblk_t			*cemp;
8440Sstevel@tonic-gate 	mblk_t			*mp = NULL;
8450Sstevel@tonic-gate 	mblk_t			*head;
8460Sstevel@tonic-gate 	mblk_t			*meta;
8470Sstevel@tonic-gate 	sctp_faddr_t		*fp;
8480Sstevel@tonic-gate 	sctp_chunk_hdr_t	*cech;
8490Sstevel@tonic-gate 	sctp_init_chunk_t	 *iack;
8500Sstevel@tonic-gate 	int32_t			cansend;
8510Sstevel@tonic-gate 	int32_t			seglen;
8520Sstevel@tonic-gate 	size_t			ceclen;
8530Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
8540Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
8550Sstevel@tonic-gate 	sctp_tf_t		*tf;
856432Svi117747 	int			pad = 0;
8570Sstevel@tonic-gate 	int			hdrlen;
8580Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
8590Sstevel@tonic-gate 	uint_t			sctp_options;
8600Sstevel@tonic-gate 	int			error;
8610Sstevel@tonic-gate 	uint16_t		old_num_str;
8623448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iackch + 1);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	cph = NULL;
8670Sstevel@tonic-gate 	if (validate_init_params(sctp, iackch, iack, iackmp, &cph, &errmp,
8680Sstevel@tonic-gate 	    &pad, &sctp_options) == 0) { /* result in 'pad' ignored */
8693448Sdh155122 		BUMP_MIB(&sctps->sctps_mib, sctpAborted);
8700Sstevel@tonic-gate 		sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
8710Sstevel@tonic-gate 		sctp_clean_death(sctp, ECONNABORTED);
8720Sstevel@tonic-gate 		return;
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	ASSERT(cph != NULL);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	/* Got a cookie to echo back; allocate an mblk */
8790Sstevel@tonic-gate 	ceclen = sizeof (*cech) + ntohs(cph->sph_len) - sizeof (*cph);
8800Sstevel@tonic-gate 	if ((pad = ceclen & (SCTP_ALIGN - 1)) != 0)
8810Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (IPH_HDR_VERSION(iackmp->b_rptr) == IPV4_VERSION)
8840Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
8850Sstevel@tonic-gate 	else
8860Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
8870Sstevel@tonic-gate 
8883448Sdh155122 	cemp = allocb(sctps->sctps_wroff_xtra + hdrlen + ceclen + pad,
8893448Sdh155122 	    BPRI_MED);
8900Sstevel@tonic-gate 	if (cemp == NULL) {
8910Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
8920Sstevel@tonic-gate 		    sctp->sctp_current->rto);
8930Sstevel@tonic-gate 		if (errmp != NULL)
8940Sstevel@tonic-gate 			freeb(errmp);
8950Sstevel@tonic-gate 		return;
8960Sstevel@tonic-gate 	}
8973448Sdh155122 	cemp->b_rptr += (sctps->sctps_wroff_xtra + hdrlen);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	/* Process the INIT ACK */
9000Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = iack->sic_inittag;
9010Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = iack->sic_inittag;
9020Sstevel@tonic-gate 	sctp->sctp_fvtag = iack->sic_inittag;
9030Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(iack->sic_inittsn);
9040Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
9050Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
9060Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(iack->sic_a_rwnd);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * Populate sctp with addresses given in the INIT ACK or IP header.
9100Sstevel@tonic-gate 	 * Need to set the df bit in the current fp as it has been cleared
9110Sstevel@tonic-gate 	 * in sctp_connect().
9120Sstevel@tonic-gate 	 */
9130Sstevel@tonic-gate 	sctp->sctp_current->df = B_TRUE;
9140Sstevel@tonic-gate 	/*
9150Sstevel@tonic-gate 	 * Since IP uses this info during the fanout process, we need to hold
9160Sstevel@tonic-gate 	 * the lock for this hash line while performing this operation.
9170Sstevel@tonic-gate 	 */
9183448Sdh155122 	/* XXX sctp_conn_fanout + SCTP_CONN_HASH(sctps, sctp->sctp_ports); */
9190Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
9200Sstevel@tonic-gate 	tf = sctp->sctp_conn_tfp;
9210Sstevel@tonic-gate 	/* sctp isn't a listener so only need to hold conn fanout lock */
9220Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
9230Sstevel@tonic-gate 	if (sctp_get_addrparams(sctp, NULL, iackmp, iackch, NULL) != 0) {
9240Sstevel@tonic-gate 		mutex_exit(&tf->tf_lock);
9250Sstevel@tonic-gate 		freeb(cemp);
9260Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
9270Sstevel@tonic-gate 		    sctp->sctp_current->rto);
9280Sstevel@tonic-gate 		if (errmp != NULL)
9290Sstevel@tonic-gate 			freeb(errmp);
9300Sstevel@tonic-gate 		return;
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	fp = sctp->sctp_current;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	/*
9370Sstevel@tonic-gate 	 * There could be a case when we get an INIT-ACK again, if the INIT
9380Sstevel@tonic-gate 	 * is re-transmitted, for e.g., which means we would have already
9390Sstevel@tonic-gate 	 * allocated this resource earlier (also for sctp_instr). In this
9400Sstevel@tonic-gate 	 * case we check and re-allocate, if necessary.
9410Sstevel@tonic-gate 	 */
9420Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_ostr;
9430Sstevel@tonic-gate 	if (ntohs(iack->sic_instr) < sctp->sctp_num_ostr)
9440Sstevel@tonic-gate 		sctp->sctp_num_ostr = ntohs(iack->sic_instr);
9450Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
9460Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
9470Sstevel@tonic-gate 		    sctp->sctp_num_ostr, KM_NOSLEEP);
9480Sstevel@tonic-gate 	} else {
9490Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
9500Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_ostr) {
9510Sstevel@tonic-gate 			kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
9520Sstevel@tonic-gate 			    old_num_str);
9530Sstevel@tonic-gate 			sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
9540Sstevel@tonic-gate 			    sctp->sctp_num_ostr, KM_NOSLEEP);
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 	}
9570Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
9580Sstevel@tonic-gate 		freeb(cemp);
9590Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
9600Sstevel@tonic-gate 		if (errmp != NULL)
9610Sstevel@tonic-gate 			freeb(errmp);
9620Sstevel@tonic-gate 		return;
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 * Allocate the in stream tracking array. Comments for sctp_ostrcntrs
9670Sstevel@tonic-gate 	 * hold here too.
9680Sstevel@tonic-gate 	 */
9690Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_istr;
9700Sstevel@tonic-gate 	if (ntohs(iack->sic_outstr) < sctp->sctp_num_istr)
9710Sstevel@tonic-gate 		sctp->sctp_num_istr = ntohs(iack->sic_outstr);
9720Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
9730Sstevel@tonic-gate 		sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
9740Sstevel@tonic-gate 		    sctp->sctp_num_istr, KM_NOSLEEP);
9750Sstevel@tonic-gate 	} else {
9760Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
9770Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_istr) {
9780Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
9790Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * old_num_str);
9800Sstevel@tonic-gate 			sctp->sctp_instr = kmem_zalloc(
9810Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr,
9820Sstevel@tonic-gate 			    KM_NOSLEEP);
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
9860Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
9870Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
9880Sstevel@tonic-gate 		freeb(cemp);
9890Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
9900Sstevel@tonic-gate 		if (errmp != NULL)
9910Sstevel@tonic-gate 			freeb(errmp);
9920Sstevel@tonic-gate 		return;
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	if (!(sctp_options & SCTP_PRSCTP_OPTION) && sctp->sctp_prsctp_aware)
9960Sstevel@tonic-gate 		sctp->sctp_prsctp_aware = B_FALSE;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	if (sctp_find_al_ind((sctp_parm_hdr_t *)(iack + 1),
9995586Skcpoon 	    ntohs(iackch->sch_len) - (sizeof (*iackch) + sizeof (*iack)),
10005586Skcpoon 	    &sctp->sctp_rx_adaptation_code) == 0) {
10015586Skcpoon 		sctp->sctp_recv_adaptation = 1;
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	cech = (sctp_chunk_hdr_t *)cemp->b_rptr;
10050Sstevel@tonic-gate 	ASSERT(OK_32PTR(cech));
10060Sstevel@tonic-gate 	cech->sch_id = CHUNK_COOKIE;
10070Sstevel@tonic-gate 	cech->sch_flags = 0;
10080Sstevel@tonic-gate 	cech->sch_len = htons(ceclen);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/* Copy the cookie (less the parm hdr) to the chunk */
10110Sstevel@tonic-gate 	bcopy(cph + 1, cech + 1, ceclen - sizeof (*cph));
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	cemp->b_wptr = cemp->b_rptr + ceclen;
10140Sstevel@tonic-gate 
1015252Svi117747 	if (sctp->sctp_unsent > 0) {
10160Sstevel@tonic-gate 		sctp_msg_hdr_t	*smh;
10170Sstevel@tonic-gate 		mblk_t		*prev = NULL;
10180Sstevel@tonic-gate 		uint32_t	unsent = 0;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 		mp = sctp->sctp_xmit_unsent;
10210Sstevel@tonic-gate 		do {
10220Sstevel@tonic-gate 			smh = (sctp_msg_hdr_t *)mp->b_rptr;
10230Sstevel@tonic-gate 			if (smh->smh_sid >= sctp->sctp_num_ostr) {
10240Sstevel@tonic-gate 				unsent += smh->smh_msglen;
10250Sstevel@tonic-gate 				if (prev != NULL)
10260Sstevel@tonic-gate 					prev->b_next = mp->b_next;
10270Sstevel@tonic-gate 				else
10280Sstevel@tonic-gate 					sctp->sctp_xmit_unsent = mp->b_next;
10290Sstevel@tonic-gate 				mp->b_next = NULL;
10300Sstevel@tonic-gate 				sctp_sendfail_event(sctp, mp, SCTP_ERR_BAD_SID,
10310Sstevel@tonic-gate 				    B_FALSE);
10320Sstevel@tonic-gate 				if (prev != NULL)
10330Sstevel@tonic-gate 					mp = prev->b_next;
10340Sstevel@tonic-gate 				else
10350Sstevel@tonic-gate 					mp = sctp->sctp_xmit_unsent;
10360Sstevel@tonic-gate 			} else {
10370Sstevel@tonic-gate 				prev = mp;
10380Sstevel@tonic-gate 				mp = mp->b_next;
10390Sstevel@tonic-gate 			}
10400Sstevel@tonic-gate 		} while (mp != NULL);
10410Sstevel@tonic-gate 		if (unsent > 0) {
10420Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= unsent);
10430Sstevel@tonic-gate 			sctp->sctp_unsent -= unsent;
10440Sstevel@tonic-gate 			/*
10450Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
10460Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
10470Sstevel@tonic-gate 			 * This is not necessary, but doesn't harm, we
10480Sstevel@tonic-gate 			 * just use unsent instead of sent-unack'ed +
10490Sstevel@tonic-gate 			 * unsent, since there won't be any sent-unack'ed
10500Sstevel@tonic-gate 			 * here.
10510Sstevel@tonic-gate 			 */
10520Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
10530Sstevel@tonic-gate 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
10540Sstevel@tonic-gate 				    sctp->sctp_unsent);
10550Sstevel@tonic-gate 			}
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 		if (sctp->sctp_xmit_unsent == NULL)
10580Sstevel@tonic-gate 			sctp->sctp_xmit_unsent_tail = NULL;
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 	ceclen += pad;
10610Sstevel@tonic-gate 	cansend = MIN(sctp->sctp_unsent, sctp->sctp_frwnd);
10620Sstevel@tonic-gate 	meta = sctp_get_msg_to_send(sctp, &mp, NULL, &error, ceclen,
10630Sstevel@tonic-gate 	    cansend,  NULL);
10640Sstevel@tonic-gate 	/*
10650Sstevel@tonic-gate 	 * The error cannot be anything else since we could have an non-zero
10660Sstevel@tonic-gate 	 * error only if sctp_get_msg_to_send() tries to send a Forward
10670Sstevel@tonic-gate 	 * TSN which will not happen here.
10680Sstevel@tonic-gate 	 */
10690Sstevel@tonic-gate 	ASSERT(error == 0);
10700Sstevel@tonic-gate 	if (meta == NULL)
10710Sstevel@tonic-gate 		goto sendcookie;
10720Sstevel@tonic-gate 	sctp->sctp_xmit_tail = meta;
10730Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
10740Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
10750Sstevel@tonic-gate 	if ((ceclen + seglen) > fp->sfa_pmss ||
10760Sstevel@tonic-gate 	    (seglen - sizeof (*sdc)) > cansend) {
10770Sstevel@tonic-gate 		goto sendcookie;
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 	/* OK, if this fails */
10800Sstevel@tonic-gate 	cemp->b_cont = dupmsg(mp);
10810Sstevel@tonic-gate sendcookie:
1082252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, cemp, 0, NULL);
1083252Svi117747 	if (head == NULL) {
1084252Svi117747 		freemsg(cemp);
1085252Svi117747 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1086252Svi117747 		if (errmp != NULL)
1087252Svi117747 			freeb(errmp);
10883448Sdh155122 		SCTP_KSTAT(sctps, sctp_send_cookie_failed);
1089252Svi117747 		return;
1090252Svi117747 	}
10910Sstevel@tonic-gate 	/*
10920Sstevel@tonic-gate 	 * Even if cookie-echo exceeds MTU for one of the hops, it'll
10930Sstevel@tonic-gate 	 * have a chance of getting there.
10940Sstevel@tonic-gate 	 */
10950Sstevel@tonic-gate 	if (fp->isv4) {
10960Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
10970Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	sctp->sctp_cookie_mp = dupmsg(head);
11020Sstevel@tonic-gate 	/* Don't bundle, we will just resend init if this cookie is lost. */
11030Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp == NULL) {
11040Sstevel@tonic-gate 		if (cemp->b_cont != NULL) {
11050Sstevel@tonic-gate 			freemsg(cemp->b_cont);
11060Sstevel@tonic-gate 			cemp->b_cont = NULL;
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 	} else if (cemp->b_cont != NULL) {
11090Sstevel@tonic-gate 		ASSERT(mp != NULL && mp == meta->b_cont);
11100Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(cemp->b_cont);
11110Sstevel@tonic-gate 		cemp->b_wptr += pad;
11120Sstevel@tonic-gate 		seglen -= sizeof (*sdc);
11130Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, seglen, meta);
11140Sstevel@tonic-gate 	}
1115*8153SGeorge.Shepherd@Sun.COM 	if (errmp != NULL) {
1116*8153SGeorge.Shepherd@Sun.COM 		if (cemp->b_cont == NULL)
1117*8153SGeorge.Shepherd@Sun.COM 			cemp->b_wptr += pad;
11180Sstevel@tonic-gate 		linkb(head, errmp);
1119*8153SGeorge.Shepherd@Sun.COM 	}
11200Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_COOKIE_ECHOED;
11210Sstevel@tonic-gate 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
11240Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate int
11280Sstevel@tonic-gate sctp_process_cookie(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *cmp,
11295586Skcpoon     sctp_init_chunk_t **iackpp, sctp_hdr_t *insctph, int *recv_adaptation,
11300Sstevel@tonic-gate     in6_addr_t *peer_addr)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate 	int32_t			clen;
11330Sstevel@tonic-gate 	size_t			initplen;
11340Sstevel@tonic-gate 	uchar_t			*p;
11350Sstevel@tonic-gate 	uchar_t			*given_hash;
11360Sstevel@tonic-gate 	uchar_t			needed_hash[16];
11370Sstevel@tonic-gate 	int64_t			ts;
11380Sstevel@tonic-gate 	int64_t			diff;
11390Sstevel@tonic-gate 	uint32_t		*lt;
11400Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
11410Sstevel@tonic-gate 	sctp_chunk_hdr_t	*initch;
11420Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
11430Sstevel@tonic-gate 	uint32_t		*lttag;
11440Sstevel@tonic-gate 	uint32_t		*fttag;
11450Sstevel@tonic-gate 	uint32_t		ports;
11463448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
11490Sstevel@tonic-gate 	/* Verify the ICV */
11500Sstevel@tonic-gate 	clen = ntohs(ch->sch_len) - sizeof (*ch) - 16;
11510Sstevel@tonic-gate 	if (clen < 0) {
11520Sstevel@tonic-gate 		dprint(1, ("invalid cookie chunk length %d\n",
11530Sstevel@tonic-gate 		    ntohs(ch->sch_len)));
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		return (-1);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 	p = (uchar_t *)(ch + 1);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	hmac_md5(p, clen, (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN,
11600Sstevel@tonic-gate 	    needed_hash);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/* The given hash follows the cookie data */
11630Sstevel@tonic-gate 	given_hash = p + clen;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (bcmp(given_hash, needed_hash, 16) != 0) {
11660Sstevel@tonic-gate 		/* The secret may have changed; try the old secret */
11670Sstevel@tonic-gate 		hmac_md5(p, clen, (uchar_t *)sctp->sctp_old_secret,
11680Sstevel@tonic-gate 		    SCTP_SECRET_LEN, needed_hash);
11690Sstevel@tonic-gate 		if (bcmp(given_hash, needed_hash, 16) != 0) {
11700Sstevel@tonic-gate 			return (-1);
11710Sstevel@tonic-gate 		}
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	/* Timestamp is int64_t, and we only guarantee 32-bit alignment */
11750Sstevel@tonic-gate 	bcopy(p, &ts, sizeof (ts));
11763795Skcpoon 	/* Cookie life time, uint32_t */
11770Sstevel@tonic-gate 	lt = (uint32_t *)(p + sizeof (ts));
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/*
11800Sstevel@tonic-gate 	 * To quote PRC, "this is our baby", so let's continue.
11810Sstevel@tonic-gate 	 * We need to pull out the encapsulated INIT ACK and
11820Sstevel@tonic-gate 	 * INIT chunks. Note that we don't process these until
11830Sstevel@tonic-gate 	 * we have verified the timestamp, but we need them before
11840Sstevel@tonic-gate 	 * processing the timestamp since if the time check fails,
11850Sstevel@tonic-gate 	 * we need to get the verification tag from the INIT in order
11860Sstevel@tonic-gate 	 * to send a stale cookie error.
11870Sstevel@tonic-gate 	 */
11880Sstevel@tonic-gate 	lttag = (uint32_t *)(lt + 1);
11890Sstevel@tonic-gate 	fttag = lttag + 1;
11900Sstevel@tonic-gate 	if (peer_addr != NULL)
11910Sstevel@tonic-gate 		bcopy(fttag + 1, peer_addr, sizeof (in6_addr_t));
11920Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)((char *)(fttag + 1) + sizeof (in6_addr_t));
11930Sstevel@tonic-gate 	initch = (sctp_chunk_hdr_t *)(iack + 1);
11940Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(initch + 1);
11950Sstevel@tonic-gate 	initplen = ntohs(initch->sch_len) - (sizeof (*init) + sizeof (*initch));
11960Sstevel@tonic-gate 	*iackpp = iack;
11975586Skcpoon 	*recv_adaptation = 0;
11980Sstevel@tonic-gate 
11993795Skcpoon 	/*
12003795Skcpoon 	 * Check the staleness of the Cookie, specified in 3.3.10.3 of
12013795Skcpoon 	 * RFC 2960.
12023795Skcpoon 	 *
12033795Skcpoon 	 * The mesaure of staleness is the difference, in microseconds,
12043795Skcpoon 	 * between the current time and the time the State Cookie expires.
12053795Skcpoon 	 * So it is lbolt64 - (ts + *lt).  If it is positive, it means
12063795Skcpoon 	 * that the Cookie has expired.
12073795Skcpoon 	 */
12083795Skcpoon 	diff = lbolt64 - (ts + *lt);
12093795Skcpoon 	if (diff > 0 && (init->sic_inittag != sctp->sctp_fvtag ||
12100Sstevel@tonic-gate 	    iack->sic_inittag != sctp->sctp_lvtag)) {
12110Sstevel@tonic-gate 		uint32_t staleness;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 		staleness = TICK_TO_USEC(diff);
12140Sstevel@tonic-gate 		staleness = htonl(staleness);
12150Sstevel@tonic-gate 		sctp_send_abort(sctp, init->sic_inittag, SCTP_ERR_STALE_COOKIE,
12160Sstevel@tonic-gate 		    (char *)&staleness, sizeof (staleness), cmp, 1, B_FALSE);
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 		dprint(1, ("stale cookie %d\n", staleness));
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 		return (-1);
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	/* Check for attack by adding addresses to a restart */
12240Sstevel@tonic-gate 	bcopy(insctph, &ports, sizeof (ports));
12253448Sdh155122 	if (sctp_secure_restart_check(cmp, initch, ports, KM_NOSLEEP,
12263448Sdh155122 	    sctps) != 1) {
12270Sstevel@tonic-gate 		return (-1);
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	/* Look for adaptation code if there any parms in the INIT chunk */
12310Sstevel@tonic-gate 	if ((initplen >= sizeof (sctp_parm_hdr_t)) &&
12320Sstevel@tonic-gate 	    (sctp_find_al_ind((sctp_parm_hdr_t *)(init + 1), initplen,
12335586Skcpoon 	    &sctp->sctp_rx_adaptation_code) == 0)) {
12345586Skcpoon 		*recv_adaptation = 1;
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	/* Examine tie-tags */
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_WAIT) {
12400Sstevel@tonic-gate 		if (sctp->sctp_state == SCTPS_ESTABLISHED &&
12410Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
12420Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag &&
12430Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 			dprint(1, ("duplicate cookie from %x:%x:%x:%x (%d)\n",
12460Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
12470Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
12480Sstevel@tonic-gate 			return (-1);
12490Sstevel@tonic-gate 		}
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 		if (init->sic_inittag != sctp->sctp_fvtag &&
12520Sstevel@tonic-gate 		    iack->sic_inittag != sctp->sctp_lvtag &&
12530Sstevel@tonic-gate 		    *fttag == sctp->sctp_fvtag &&
12540Sstevel@tonic-gate 		    *lttag == sctp->sctp_lvtag) {
12550Sstevel@tonic-gate 			int i;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 			/* Section 5.2.4 case A: restart */
12580Sstevel@tonic-gate 			sctp->sctp_fvtag = init->sic_inittag;
12590Sstevel@tonic-gate 			sctp->sctp_lvtag = iack->sic_inittag;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 			sctp->sctp_sctph->sh_verf = init->sic_inittag;
12620Sstevel@tonic-gate 			sctp->sctp_sctph6->sh_verf = init->sic_inittag;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 			sctp->sctp_ftsn = ntohl(init->sic_inittsn);
12650Sstevel@tonic-gate 			sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
12660Sstevel@tonic-gate 			sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
12670Sstevel@tonic-gate 			sctp->sctp_fcsn = sctp->sctp_lastacked;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
12700Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
12710Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
12720Sstevel@tonic-gate 			}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 			dprint(1, ("sctp peer %x:%x:%x:%x (%d) restarted\n",
12750Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
12760Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
12770Sstevel@tonic-gate 			/* reset parameters */
12780Sstevel@tonic-gate 			sctp_congest_reset(sctp);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 			/* reset stream bookkeeping */
12810Sstevel@tonic-gate 			sctp_instream_cleanup(sctp, B_FALSE);
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 			sctp->sctp_istr_nmsgs = 0;
12840Sstevel@tonic-gate 			sctp->sctp_rxqueued = 0;
12850Sstevel@tonic-gate 			for (i = 0; i < sctp->sctp_num_ostr; i++) {
12860Sstevel@tonic-gate 				sctp->sctp_ostrcntrs[i] = 0;
12870Sstevel@tonic-gate 			}
12880Sstevel@tonic-gate 			/* XXX flush xmit_list? */
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			return (0);
12910Sstevel@tonic-gate 		} else if (init->sic_inittag != sctp->sctp_fvtag &&
12920Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 			/* Section 5.2.4 case B: INIT collision */
12950Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
12960Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
12970Sstevel@tonic-gate 					return (-1);	/* Drop? */
12980Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
12990Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
13000Sstevel@tonic-gate 			}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 			dprint(1, ("init collision with %x:%x:%x:%x (%d)\n",
13030Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
13040Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 			return (0);
13070Sstevel@tonic-gate 		} else if (iack->sic_inittag != sctp->sctp_lvtag &&
13080Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
13090Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 			/* Section 5.2.4 case C: late COOKIE */
13120Sstevel@tonic-gate 			dprint(1, ("late cookie from %x:%x:%x:%x (%d)\n",
13130Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
13140Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
13150Sstevel@tonic-gate 			return (-1);
13160Sstevel@tonic-gate 		} else if (init->sic_inittag == sctp->sctp_fvtag &&
13170Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 			/*
13200Sstevel@tonic-gate 			 * Section 5.2.4 case D: COOKIE ECHO retransmit
13210Sstevel@tonic-gate 			 * Don't check cookie lifetime
13220Sstevel@tonic-gate 			 */
13230Sstevel@tonic-gate 			dprint(1, ("cookie tags match from %x:%x:%x:%x (%d)\n",
13240Sstevel@tonic-gate 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
13250Sstevel@tonic-gate 			    (int)(sctp->sctp_fport)));
13260Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
13270Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
13280Sstevel@tonic-gate 					return (-1);	/* Drop? */
13290Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
13300Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
13310Sstevel@tonic-gate 			}
13320Sstevel@tonic-gate 			return (0);
13330Sstevel@tonic-gate 		} else {
13340Sstevel@tonic-gate 			/* unrecognized case -- silently drop it */
13350Sstevel@tonic-gate 			return (-1);
13360Sstevel@tonic-gate 		}
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	return (0);
13400Sstevel@tonic-gate }
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate /*
13430Sstevel@tonic-gate  * Similar to ip_fanout_sctp, except that the src addr(s) are drawn
13440Sstevel@tonic-gate  * from address parameters in an INIT ACK's address list. This
13450Sstevel@tonic-gate  * function is used when an INIT ACK is received but IP's fanout
13460Sstevel@tonic-gate  * function could not find a sctp via the normal lookup routine.
13470Sstevel@tonic-gate  * This can happen when a host sends an INIT ACK from a different
13480Sstevel@tonic-gate  * address than the INIT was sent to.
13490Sstevel@tonic-gate  *
13500Sstevel@tonic-gate  * Returns the sctp_t if found, or NULL if not found.
13510Sstevel@tonic-gate  */
13520Sstevel@tonic-gate sctp_t *
13530Sstevel@tonic-gate sctp_addrlist2sctp(mblk_t *mp, sctp_hdr_t *sctph, sctp_chunk_hdr_t *ich,
13543510Svi117747     zoneid_t zoneid, sctp_stack_t *sctps)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate 	int isv4;
13570Sstevel@tonic-gate 	ipha_t *iph;
13580Sstevel@tonic-gate 	ip6_t *ip6h;
13590Sstevel@tonic-gate 	in6_addr_t dst;
13600Sstevel@tonic-gate 	in6_addr_t src;
13610Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
13620Sstevel@tonic-gate 	ssize_t remaining;
13630Sstevel@tonic-gate 	sctp_init_chunk_t *iack;
13640Sstevel@tonic-gate 	uint32_t ports;
13650Sstevel@tonic-gate 	sctp_t *sctp = NULL;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	ASSERT(ich->sch_id == CHUNK_INIT_ACK);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
13700Sstevel@tonic-gate 	if (isv4) {
13710Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
13720Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, &dst);
13730Sstevel@tonic-gate 	} else {
13740Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
13750Sstevel@tonic-gate 		dst = ip6h->ip6_dst;
13760Sstevel@tonic-gate 	}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	ports = *(uint32_t *)sctph;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	dprint(1, ("sctp_addrlist2sctp: ports=%u, dst = %x:%x:%x:%x\n",
13810Sstevel@tonic-gate 	    ports, SCTP_PRINTADDR(dst)));
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	/* pull out any address parameters */
13840Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) - sizeof (*iack);
13850Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
13860Sstevel@tonic-gate 		return (NULL);
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(ich + 1);
13900Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(iack + 1);
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	while (ph != NULL) {
13930Sstevel@tonic-gate 		/*
13940Sstevel@tonic-gate 		 * params have been put in host byteorder by
13950Sstevel@tonic-gate 		 * sctp_check_input()
13960Sstevel@tonic-gate 		 */
13970Sstevel@tonic-gate 		if (ph->sph_type == PARM_ADDR4) {
13980Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED((struct in_addr *)(ph + 1),
13990Sstevel@tonic-gate 			    &src);
14000Sstevel@tonic-gate 
14013510Svi117747 			sctp = sctp_conn_match(&src, &dst, ports, zoneid,
14023510Svi117747 			    sctps);
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 			dprint(1,
14050Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
14061676Sjpk 			    SCTP_PRINTADDR(src), (void *)sctp));
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 			if (sctp != NULL) {
14100Sstevel@tonic-gate 				return (sctp);
14110Sstevel@tonic-gate 			}
14120Sstevel@tonic-gate 		} else if (ph->sph_type == PARM_ADDR6) {
14130Sstevel@tonic-gate 			src = *(in6_addr_t *)(ph + 1);
14143510Svi117747 			sctp = sctp_conn_match(&src, &dst, ports, zoneid,
14153510Svi117747 			    sctps);
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 			dprint(1,
14180Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
14191676Sjpk 			    SCTP_PRINTADDR(src), (void *)sctp));
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 			if (sctp != NULL) {
14220Sstevel@tonic-gate 				return (sctp);
14230Sstevel@tonic-gate 			}
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	return (NULL);
14300Sstevel@tonic-gate }
1431