xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_cookie.c (revision 13009:29fc56bd19a6)
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 /*
2312869SKacheong.Poon@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/systm.h>
280Sstevel@tonic-gate #include <sys/stream.h>
290Sstevel@tonic-gate #include <sys/cmn_err.h>
300Sstevel@tonic-gate #include <sys/md5.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #include <sys/strsubr.h>
330Sstevel@tonic-gate #include <sys/random.h>
341676Sjpk #include <sys/tsol/tnet.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <netinet/in.h>
370Sstevel@tonic-gate #include <netinet/ip6.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <inet/common.h>
400Sstevel@tonic-gate #include <inet/ip.h>
410Sstevel@tonic-gate #include <inet/ip6.h>
4211042SErik.Nordmark@Sun.COM #include <inet/ipsec_impl.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
hmac_md5(uchar_t * text,size_t text_len,uchar_t * key,size_t key_len,uchar_t * digest)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.
1318153SGeorge.Shepherd@Sun.COM  *
1328549SGeorge.Shepherd@Sun.COM  * When called from stcp_send_initack() while processing parameters
1338153SGeorge.Shepherd@Sun.COM  * from a received INIT_CHUNK want_cookie will be NULL.
1340Sstevel@tonic-gate  *
1358153SGeorge.Shepherd@Sun.COM  * When called from sctp_send_cookie_echo() while processing an INIT_ACK,
1368153SGeorge.Shepherd@Sun.COM  * want_cookie contains a pointer to a pointer of type *sctp_parm_hdr_t.
1378153SGeorge.Shepherd@Sun.COM  * However, this last pointer will be NULL until the cookie is processed
1388153SGeorge.Shepherd@Sun.COM  * at which time it will be set to point to a sctp_parm_hdr_t that contains
1398153SGeorge.Shepherd@Sun.COM  * the cookie info.
1408153SGeorge.Shepherd@Sun.COM  *
1418153SGeorge.Shepherd@Sun.COM  * Note: an INIT_ACK is expected to contain a cookie.
1428153SGeorge.Shepherd@Sun.COM  *
1438549SGeorge.Shepherd@Sun.COM  * When processing an INIT_ACK, an ERROR chunk and chain of one or more
1448549SGeorge.Shepherd@Sun.COM  * error CAUSE blocks will be created if unrecognized parameters marked by
1458549SGeorge.Shepherd@Sun.COM  * the sender as reportable are found.
1468549SGeorge.Shepherd@Sun.COM  *
1478549SGeorge.Shepherd@Sun.COM  * When processing an INIT chunk, a chain of one or more error CAUSE blocks
1488549SGeorge.Shepherd@Sun.COM  * will be created if unrecognized parameters marked by the sender as
1498549SGeorge.Shepherd@Sun.COM  * reportable are found. These are appended directly to the INIT_ACK chunk.
1508549SGeorge.Shepherd@Sun.COM  *
1518549SGeorge.Shepherd@Sun.COM  * In both cases the error chain is visible to the caller via *errmp.
1528549SGeorge.Shepherd@Sun.COM  *
1538153SGeorge.Shepherd@Sun.COM  * Returns 1 if the parameters are OK (or if there are no optional
1548153SGeorge.Shepherd@Sun.COM  * parameters), returns 0 otherwise.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate static int
validate_init_params(sctp_t * sctp,sctp_chunk_hdr_t * ch,sctp_init_chunk_t * init,mblk_t * inmp,sctp_parm_hdr_t ** want_cookie,mblk_t ** errmp,int * supp_af,uint_t * sctp_options,ip_recv_attr_t * ira)1570Sstevel@tonic-gate validate_init_params(sctp_t *sctp, sctp_chunk_hdr_t *ch,
1580Sstevel@tonic-gate     sctp_init_chunk_t *init, mblk_t *inmp, sctp_parm_hdr_t **want_cookie,
15911042SErik.Nordmark@Sun.COM     mblk_t **errmp, int *supp_af, uint_t *sctp_options, ip_recv_attr_t *ira)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
1620Sstevel@tonic-gate 	sctp_init_chunk_t	*ic;
1630Sstevel@tonic-gate 	ssize_t			remaining;
1640Sstevel@tonic-gate 	uint16_t		serror = 0;
1650Sstevel@tonic-gate 	char			*details = NULL;
1660Sstevel@tonic-gate 	size_t			errlen = 0;
1670Sstevel@tonic-gate 	boolean_t		got_cookie = B_FALSE;
1688153SGeorge.Shepherd@Sun.COM 	boolean_t		got_errchunk = B_FALSE;
1690Sstevel@tonic-gate 	uint16_t		ptype;
1709451SGeorge.Shepherd@Sun.COM 	sctp_mpc_t		mpc;
17111042SErik.Nordmark@Sun.COM 	conn_t			*connp = sctp->sctp_connp;
1729451SGeorge.Shepherd@Sun.COM 
1730Sstevel@tonic-gate 
1748153SGeorge.Shepherd@Sun.COM 	ASSERT(errmp != NULL);
1758153SGeorge.Shepherd@Sun.COM 
1760Sstevel@tonic-gate 	if (sctp_options != NULL)
1770Sstevel@tonic-gate 		*sctp_options = 0;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* First validate stream parameters */
1800Sstevel@tonic-gate 	if (init->sic_instr == 0 || init->sic_outstr == 0) {
1810Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1826596Skp158701 		dprint(1, ("validate_init_params: bad sid, is=%d os=%d\n",
1835586Skcpoon 		    htons(init->sic_instr), htons(init->sic_outstr)));
1840Sstevel@tonic-gate 		goto abort;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	if (ntohl(init->sic_inittag) == 0) {
1870Sstevel@tonic-gate 		serror = SCTP_ERR_BAD_MANDPARM;
1880Sstevel@tonic-gate 		dprint(1, ("validate_init_params: inittag = 0\n"));
1890Sstevel@tonic-gate 		goto abort;
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	remaining = ntohs(ch->sch_len) - sizeof (*ch);
1930Sstevel@tonic-gate 	ic = (sctp_init_chunk_t *)(ch + 1);
1940Sstevel@tonic-gate 	remaining -= sizeof (*ic);
1950Sstevel@tonic-gate 	if (remaining < sizeof (*cph)) {
1968153SGeorge.Shepherd@Sun.COM 		/*
1978153SGeorge.Shepherd@Sun.COM 		 * When processing a received INIT_ACK, a cookie is
1988153SGeorge.Shepherd@Sun.COM 		 * expected, if missing there is nothing to validate.
1998153SGeorge.Shepherd@Sun.COM 		 */
2000Sstevel@tonic-gate 		if (want_cookie != NULL)
2010Sstevel@tonic-gate 			goto cookie_abort;
2020Sstevel@tonic-gate 		return (1);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	cph = (sctp_parm_hdr_t *)(ic + 1);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	while (cph != NULL) {
2080Sstevel@tonic-gate 		ptype = ntohs(cph->sph_type);
2090Sstevel@tonic-gate 		switch (ptype) {
2100Sstevel@tonic-gate 		case PARM_HBINFO:
2110Sstevel@tonic-gate 		case PARM_UNRECOGNIZED:
2120Sstevel@tonic-gate 		case PARM_ECN:
2130Sstevel@tonic-gate 			/* just ignore them */
2140Sstevel@tonic-gate 			break;
2150Sstevel@tonic-gate 		case PARM_FORWARD_TSN:
2160Sstevel@tonic-gate 			if (sctp_options != NULL)
2170Sstevel@tonic-gate 				*sctp_options |= SCTP_PRSCTP_OPTION;
2180Sstevel@tonic-gate 			break;
2190Sstevel@tonic-gate 		case PARM_COOKIE:
2200Sstevel@tonic-gate 			got_cookie = B_TRUE;
2218153SGeorge.Shepherd@Sun.COM 			/*
2228153SGeorge.Shepherd@Sun.COM 			 * Processing a received INIT_ACK, we have a cookie
2238153SGeorge.Shepherd@Sun.COM 			 * and a valid pointer in our caller to attach it to.
2248153SGeorge.Shepherd@Sun.COM 			 */
2250Sstevel@tonic-gate 			if (want_cookie != NULL) {
2260Sstevel@tonic-gate 				*want_cookie = cph;
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 			break;
2290Sstevel@tonic-gate 		case PARM_ADDR4:
230432Svi117747 			*supp_af |= PARM_SUPP_V4;
231432Svi117747 			break;
2320Sstevel@tonic-gate 		case PARM_ADDR6:
233432Svi117747 			*supp_af |= PARM_SUPP_V6;
234432Svi117747 			break;
2350Sstevel@tonic-gate 		case PARM_COOKIE_PRESERVE:
2360Sstevel@tonic-gate 		case PARM_ADAPT_LAYER_IND:
2370Sstevel@tonic-gate 			/* These are OK */
2380Sstevel@tonic-gate 			break;
2390Sstevel@tonic-gate 		case PARM_ADDR_HOST_NAME:
2400Sstevel@tonic-gate 			/* Don't support this; abort the association */
2410Sstevel@tonic-gate 			serror = SCTP_ERR_BAD_ADDR;
2420Sstevel@tonic-gate 			details = (char *)cph;
2430Sstevel@tonic-gate 			errlen = ntohs(cph->sph_len);
2440Sstevel@tonic-gate 			dprint(1, ("sctp:validate_init_params: host addr\n"));
2450Sstevel@tonic-gate 			goto abort;
2460Sstevel@tonic-gate 		case PARM_SUPP_ADDRS: {
2470Sstevel@tonic-gate 			/* Make sure we have a supported addr intersection */
2480Sstevel@tonic-gate 			uint16_t *p, addrtype;
2490Sstevel@tonic-gate 			int plen;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 			plen = ntohs(cph->sph_len);
2520Sstevel@tonic-gate 			p = (uint16_t *)(cph + 1);
2530Sstevel@tonic-gate 			while (plen > 0) {
2540Sstevel@tonic-gate 				addrtype = ntohs(*p);
2550Sstevel@tonic-gate 				switch (addrtype) {
2560Sstevel@tonic-gate 				case PARM_ADDR6:
2570Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V6;
2580Sstevel@tonic-gate 					break;
2590Sstevel@tonic-gate 				case PARM_ADDR4:
2600Sstevel@tonic-gate 					*supp_af |= PARM_SUPP_V4;
2610Sstevel@tonic-gate 					break;
2620Sstevel@tonic-gate 				default:
2630Sstevel@tonic-gate 					/*
2640Sstevel@tonic-gate 					 * Do nothing, silently ignore hostname
2650Sstevel@tonic-gate 					 * address.
2660Sstevel@tonic-gate 					 */
2670Sstevel@tonic-gate 					break;
2680Sstevel@tonic-gate 				}
2690Sstevel@tonic-gate 				p++;
2700Sstevel@tonic-gate 				plen -= sizeof (*p);
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 			break;
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 		default:
2758153SGeorge.Shepherd@Sun.COM 			/*
2768153SGeorge.Shepherd@Sun.COM 			 * Handle any unrecognized params, the two high order
2778153SGeorge.Shepherd@Sun.COM 			 * bits of ptype define how the remote wants them
2788153SGeorge.Shepherd@Sun.COM 			 * handled.
2798153SGeorge.Shepherd@Sun.COM 			 * Top bit:
2808153SGeorge.Shepherd@Sun.COM 			 *    1. Continue processing other params in the chunk
2818153SGeorge.Shepherd@Sun.COM 			 *    0. Stop processing params after this one.
2828153SGeorge.Shepherd@Sun.COM 			 * 2nd bit:
2838153SGeorge.Shepherd@Sun.COM 			 *    1. Must report this unrecognized param to remote
2848153SGeorge.Shepherd@Sun.COM 			 *    0. Obey the top bit silently.
2858153SGeorge.Shepherd@Sun.COM 			 */
2868153SGeorge.Shepherd@Sun.COM 			if (ptype & SCTP_REPORT_THIS_PARAM) {
2878549SGeorge.Shepherd@Sun.COM 				if (!got_errchunk && want_cookie != NULL) {
2888153SGeorge.Shepherd@Sun.COM 					/*
28911943SGeorge.Shepherd@Sun.COM 					 * The incoming pointer want_cookie is
29011943SGeorge.Shepherd@Sun.COM 					 * NULL so processing an INIT_ACK.
29111943SGeorge.Shepherd@Sun.COM 					 * This is the first reportable param,
29211943SGeorge.Shepherd@Sun.COM 					 * create an ERROR chunk and populate
29311943SGeorge.Shepherd@Sun.COM 					 * it with a CAUSE block for this parm.
2948153SGeorge.Shepherd@Sun.COM 					 */
2958153SGeorge.Shepherd@Sun.COM 					*errmp = sctp_make_err(sctp,
2968153SGeorge.Shepherd@Sun.COM 					    PARM_UNRECOGNIZED,
2978153SGeorge.Shepherd@Sun.COM 					    (void *)cph,
2988153SGeorge.Shepherd@Sun.COM 					    ntohs(cph->sph_len));
2998153SGeorge.Shepherd@Sun.COM 					got_errchunk = B_TRUE;
3008153SGeorge.Shepherd@Sun.COM 				} else {
3018153SGeorge.Shepherd@Sun.COM 					/*
30211943SGeorge.Shepherd@Sun.COM 					 * If processing an INIT_ACK, we already
30311943SGeorge.Shepherd@Sun.COM 					 * have an ERROR chunk, just add a new
30411943SGeorge.Shepherd@Sun.COM 					 * CAUSE block and update ERROR chunk
30511943SGeorge.Shepherd@Sun.COM 					 * length.
30611943SGeorge.Shepherd@Sun.COM 					 * If processing an INIT chunk add a new
30711943SGeorge.Shepherd@Sun.COM 					 * CAUSE block to the INIT_ACK, in this
30811943SGeorge.Shepherd@Sun.COM 					 * case there is no ERROR chunk thus
30911943SGeorge.Shepherd@Sun.COM 					 * got_errchunk will be B_FALSE. Chunk
31011943SGeorge.Shepherd@Sun.COM 					 * length is computed by our caller.
3118153SGeorge.Shepherd@Sun.COM 					 */
3128549SGeorge.Shepherd@Sun.COM 					sctp_add_unrec_parm(cph, errmp,
3138549SGeorge.Shepherd@Sun.COM 					    got_errchunk);
3148153SGeorge.Shepherd@Sun.COM 				}
3158153SGeorge.Shepherd@Sun.COM 			}
3168153SGeorge.Shepherd@Sun.COM 			if (ptype & SCTP_CONT_PROC_PARAMS) {
3170Sstevel@tonic-gate 				/*
3188153SGeorge.Shepherd@Sun.COM 				 * Continue processing params after this
3198153SGeorge.Shepherd@Sun.COM 				 * parameter.
3200Sstevel@tonic-gate 				 */
3210Sstevel@tonic-gate 				break;
3220Sstevel@tonic-gate 			}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 			/*
3258153SGeorge.Shepherd@Sun.COM 			 * Stop processing params, report any reportable
3268153SGeorge.Shepherd@Sun.COM 			 * unrecognized params found so far.
3270Sstevel@tonic-gate 			 */
3288153SGeorge.Shepherd@Sun.COM 			goto done;
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		cph = sctp_next_parm(cph, &remaining);
3320Sstevel@tonic-gate 	}
3338153SGeorge.Shepherd@Sun.COM done:
334432Svi117747 	/*
335432Svi117747 	 * Some sanity checks.  The following should not fail unless the
336432Svi117747 	 * other side is broken.
337432Svi117747 	 *
338432Svi117747 	 * 1. If this is a V4 endpoint but V4 address is not
339432Svi117747 	 * supported, abort.
340432Svi117747 	 * 2. If this is a V6 only endpoint but V6 address is
341432Svi117747 	 * not supported, abort.  This assumes that a V6
342432Svi117747 	 * endpoint can use both V4 and V6 addresses.
343432Svi117747 	 * We only care about supp_af when processing INIT, i.e want_cookie
344432Svi117747 	 * is NULL.
345432Svi117747 	 */
346432Svi117747 	if (want_cookie == NULL &&
34711042SErik.Nordmark@Sun.COM 	    ((connp->conn_family == AF_INET && !(*supp_af & PARM_SUPP_V4)) ||
34811042SErik.Nordmark@Sun.COM 	    (connp->conn_family == AF_INET6 && !(*supp_af & PARM_SUPP_V6) &&
349432Svi117747 	    sctp->sctp_connp->conn_ipv6_v6only))) {
350432Svi117747 		dprint(1, ("sctp:validate_init_params: supp addr\n"));
351432Svi117747 		serror = SCTP_ERR_BAD_ADDR;
352432Svi117747 		goto abort;
353432Svi117747 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if (want_cookie != NULL && !got_cookie) {
3560Sstevel@tonic-gate cookie_abort:
3579451SGeorge.Shepherd@Sun.COM 		/* Will populate the CAUSE block in the ABORT chunk. */
3589451SGeorge.Shepherd@Sun.COM 		mpc.mpc_num =  htons(1);
3599451SGeorge.Shepherd@Sun.COM 		mpc.mpc_param = htons(PARM_COOKIE);
3609451SGeorge.Shepherd@Sun.COM 		mpc.mpc_pad = 0;
3619451SGeorge.Shepherd@Sun.COM 
3620Sstevel@tonic-gate 		dprint(1, ("validate_init_params: cookie absent\n"));
3630Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch), SCTP_ERR_MISSING_PARM,
36411042SErik.Nordmark@Sun.COM 		    (char *)&mpc, sizeof (sctp_mpc_t), inmp, 0, B_FALSE, ira);
3650Sstevel@tonic-gate 		return (0);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/* OK */
3690Sstevel@tonic-gate 	return (1);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate abort:
3720Sstevel@tonic-gate 	if (want_cookie != NULL)
3730Sstevel@tonic-gate 		return (0);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	sctp_send_abort(sctp, sctp_init2vtag(ch), serror, details,
37611042SErik.Nordmark@Sun.COM 	    errlen, inmp, 0, B_FALSE, ira);
3770Sstevel@tonic-gate 	return (0);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * Initialize params from the INIT and INIT-ACK when the assoc. is
3820Sstevel@tonic-gate  * established.
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate boolean_t
sctp_initialize_params(sctp_t * sctp,sctp_init_chunk_t * init,sctp_init_chunk_t * iack)3850Sstevel@tonic-gate sctp_initialize_params(sctp_t *sctp, sctp_init_chunk_t *init,
3860Sstevel@tonic-gate     sctp_init_chunk_t *iack)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	/* Get initial TSN */
3890Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(init->sic_inittsn);
3900Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/* Serial number is initialized to the same value as the TSN */
3930Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Get verification tags; no byteordering is necessary, since
3970Sstevel@tonic-gate 	 * verfication tags are never processed except for byte-by-byte
3980Sstevel@tonic-gate 	 * comparisons.
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	sctp->sctp_fvtag = init->sic_inittag;
4010Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = init->sic_inittag;
4020Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = init->sic_inittag;
4030Sstevel@tonic-gate 	sctp->sctp_lvtag = iack->sic_inittag;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/* Get the peer's rwnd */
4060Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/* Allocate the in/out-stream counters */
4090Sstevel@tonic-gate 	sctp->sctp_num_ostr = iack->sic_outstr;
4100Sstevel@tonic-gate 	sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
4110Sstevel@tonic-gate 	    sctp->sctp_num_ostr, KM_NOSLEEP);
4120Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL)
4130Sstevel@tonic-gate 		return (B_FALSE);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	sctp->sctp_num_istr = iack->sic_instr;
4160Sstevel@tonic-gate 	sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
4170Sstevel@tonic-gate 	    sctp->sctp_num_istr, KM_NOSLEEP);
4180Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
4190Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
4200Sstevel@tonic-gate 		    sctp->sctp_num_ostr);
4210Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = NULL;
4220Sstevel@tonic-gate 		return (B_FALSE);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	return (B_TRUE);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
427852Svi117747 /*
428852Svi117747  * Copy the peer's original source address into addr. This relies on the
429852Svi117747  * following format (see sctp_send_initack() below):
430852Svi117747  * 	relative timestamp for the cookie (int64_t) +
431852Svi117747  * 	cookie lifetime (uint32_t) +
432852Svi117747  * 	local tie-tag (uint32_t) +  peer tie-tag (uint32_t) +
433852Svi117747  * 	Peer's original src ...
434852Svi117747  */
435852Svi117747 int
cl_sctp_cookie_paddr(sctp_chunk_hdr_t * ch,in6_addr_t * addr)436852Svi117747 cl_sctp_cookie_paddr(sctp_chunk_hdr_t *ch, in6_addr_t *addr)
437852Svi117747 {
438852Svi117747 	uchar_t	*off;
439852Svi117747 
440852Svi117747 	ASSERT(addr != NULL);
441852Svi117747 
442852Svi117747 	if (ch->sch_id != CHUNK_COOKIE)
443852Svi117747 		return (EINVAL);
444852Svi117747 
445852Svi117747 	off = (uchar_t *)ch + sizeof (*ch) + sizeof (int64_t) +
446852Svi117747 	    sizeof (uint32_t) + sizeof (uint32_t) + sizeof (uint32_t);
447852Svi117747 
448852Svi117747 	bcopy(off, addr, sizeof (*addr));
449852Svi117747 
450852Svi117747 	return (0);
451852Svi117747 }
452852Svi117747 
4530Sstevel@tonic-gate #define	SCTP_CALC_COOKIE_LEN(initcp) \
4540Sstevel@tonic-gate 	sizeof (int64_t) +		/* timestamp */			\
4550Sstevel@tonic-gate 	sizeof (uint32_t) +		/* cookie lifetime */		\
4560Sstevel@tonic-gate 	sizeof (sctp_init_chunk_t) +	/* INIT ACK */			\
4570Sstevel@tonic-gate 	sizeof (in6_addr_t) +		/* peer's original source */ 	\
4580Sstevel@tonic-gate 	ntohs((initcp)->sch_len) +	/* peer's INIT */		\
4590Sstevel@tonic-gate 	sizeof (uint32_t) +		/* local tie-tag */		\
4600Sstevel@tonic-gate 	sizeof (uint32_t) +		/* peer tie-tag */		\
4610Sstevel@tonic-gate 	sizeof (sctp_parm_hdr_t) +	/* param header */		\
4620Sstevel@tonic-gate 	16				/* MD5 hash */
4630Sstevel@tonic-gate 
46411042SErik.Nordmark@Sun.COM /*
46511042SErik.Nordmark@Sun.COM  * Note that sctp is the listener, hence we shouldn't modify it.
46611042SErik.Nordmark@Sun.COM  */
4670Sstevel@tonic-gate void
sctp_send_initack(sctp_t * sctp,sctp_hdr_t * initsh,sctp_chunk_hdr_t * ch,mblk_t * initmp,ip_recv_attr_t * ira)4682776Skp158701 sctp_send_initack(sctp_t *sctp, sctp_hdr_t *initsh, sctp_chunk_hdr_t *ch,
46911042SErik.Nordmark@Sun.COM     mblk_t *initmp, ip_recv_attr_t *ira)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	ipha_t			*initiph;
4720Sstevel@tonic-gate 	ip6_t			*initip6h;
47311042SErik.Nordmark@Sun.COM 	ipha_t			*iackiph = NULL;
47411042SErik.Nordmark@Sun.COM 	ip6_t			*iackip6h = NULL;
4750Sstevel@tonic-gate 	sctp_chunk_hdr_t	*iack_ch;
4760Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
4770Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
4780Sstevel@tonic-gate 	sctp_hdr_t		*iacksh;
4790Sstevel@tonic-gate 	size_t			cookielen;
4800Sstevel@tonic-gate 	size_t			iacklen;
4810Sstevel@tonic-gate 	size_t			ipsctplen;
4820Sstevel@tonic-gate 	size_t			errlen = 0;
4830Sstevel@tonic-gate 	sctp_parm_hdr_t		*cookieph;
4840Sstevel@tonic-gate 	mblk_t			*iackmp;
4850Sstevel@tonic-gate 	uint32_t		itag;
4860Sstevel@tonic-gate 	uint32_t		itsn;
4870Sstevel@tonic-gate 	int64_t			*now;
4880Sstevel@tonic-gate 	int64_t			nowt;
4890Sstevel@tonic-gate 	uint32_t		*lifetime;
4900Sstevel@tonic-gate 	char			*p;
4910Sstevel@tonic-gate 	boolean_t		 isv4;
492432Svi117747 	int			supp_af = 0;
4930Sstevel@tonic-gate 	uint_t			sctp_options;
4940Sstevel@tonic-gate 	uint32_t		*ttag;
4950Sstevel@tonic-gate 	int			pad;
4960Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
4970Sstevel@tonic-gate 	boolean_t		initcollision = B_FALSE;
498432Svi117747 	boolean_t		linklocal = B_FALSE;
4993448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
50011042SErik.Nordmark@Sun.COM 	conn_t			*connp = sctp->sctp_connp;
50111042SErik.Nordmark@Sun.COM 	int			err;
50211042SErik.Nordmark@Sun.COM 	ip_xmit_attr_t		*ixa = NULL;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
5050Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(initmp->b_rptr) == IPV4_VERSION);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/* Extract the INIT chunk */
5080Sstevel@tonic-gate 	if (isv4) {
5090Sstevel@tonic-gate 		initiph = (ipha_t *)initmp->b_rptr;
5100Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr_len;
511432Svi117747 		supp_af |= PARM_SUPP_V4;
5120Sstevel@tonic-gate 	} else {
5130Sstevel@tonic-gate 		initip6h = (ip6_t *)initmp->b_rptr;
5140Sstevel@tonic-gate 		ipsctplen = sctp->sctp_ip_hdr6_len;
51511042SErik.Nordmark@Sun.COM 		if (IN6_IS_ADDR_LINKLOCAL(&initip6h->ip6_src) ||
51611042SErik.Nordmark@Sun.COM 		    IN6_IS_ADDR_LINKLOCAL(&initip6h->ip6_dst))
517432Svi117747 			linklocal = B_TRUE;
518432Svi117747 		supp_af |= PARM_SUPP_V6;
51911042SErik.Nordmark@Sun.COM 		if (!sctp->sctp_connp->conn_ipv6_v6only)
52011042SErik.Nordmark@Sun.COM 			supp_af |= PARM_SUPP_V4;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 	ASSERT(OK_32PTR(initsh));
5230Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)((char *)(initsh + 1) + sizeof (*iack_ch));
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/* Make sure we like the peer's parameters */
5260Sstevel@tonic-gate 	if (validate_init_params(sctp, ch, init, initmp, NULL, &errmp,
52711042SErik.Nordmark@Sun.COM 	    &supp_af, &sctp_options, ira) == 0) {
5280Sstevel@tonic-gate 		return;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	if (errmp != NULL)
5310Sstevel@tonic-gate 		errlen = msgdsize(errmp);
53211042SErik.Nordmark@Sun.COM 	if (connp->conn_family == AF_INET) {
5330Sstevel@tonic-gate 		/*
53411943SGeorge.Shepherd@Sun.COM 		 * Regardless of the supported address in the INIT, v4
5350Sstevel@tonic-gate 		 * must be supported.
5360Sstevel@tonic-gate 		 */
5370Sstevel@tonic-gate 		supp_af = PARM_SUPP_V4;
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_LISTEN) {
5400Sstevel@tonic-gate 		/* normal, expected INIT: generate new vtag and itsn */
5410Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
5420Sstevel@tonic-gate 		if (itag == 0)
5430Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
5440Sstevel@tonic-gate 		itsn = itag + 1;
5450Sstevel@tonic-gate 		itag = htonl(itag);
5460Sstevel@tonic-gate 	} else if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
5470Sstevel@tonic-gate 	    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
5480Sstevel@tonic-gate 		/* init collision; copy vtag and itsn from sctp */
5490Sstevel@tonic-gate 		itag = sctp->sctp_lvtag;
5500Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * In addition we need to send all the params that was sent
5530Sstevel@tonic-gate 		 * in our INIT chunk. Essentially, it is only the supported
5540Sstevel@tonic-gate 		 * address params that we need to add.
5550Sstevel@tonic-gate 		 */
5560Sstevel@tonic-gate 		initcollision = B_TRUE;
557432Svi117747 		/*
558432Svi117747 		 * When we sent the INIT, we should have set linklocal in
559432Svi117747 		 * the sctp which should be good enough.
560432Svi117747 		 */
561432Svi117747 		if (linklocal)
562432Svi117747 			linklocal = B_FALSE;
5630Sstevel@tonic-gate 	} else {
5640Sstevel@tonic-gate 		/* peer restart; generate new vtag but keep everything else */
5650Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
5660Sstevel@tonic-gate 		if (itag == 0)
5670Sstevel@tonic-gate 			itag = (uint32_t)gethrtime();
5680Sstevel@tonic-gate 		itag = htonl(itag);
5690Sstevel@tonic-gate 		itsn = sctp->sctp_ltsn;
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/*
5730Sstevel@tonic-gate 	 * Allocate a mblk for the INIT ACK, consisting of the link layer
5740Sstevel@tonic-gate 	 * header, the IP header, the SCTP common header, and INIT ACK chunk,
5750Sstevel@tonic-gate 	 * and finally the COOKIE parameter.
5760Sstevel@tonic-gate 	 */
5770Sstevel@tonic-gate 	cookielen = SCTP_CALC_COOKIE_LEN(ch);
5780Sstevel@tonic-gate 	iacklen = sizeof (*iack_ch) + sizeof (*iack) + cookielen;
5795586Skcpoon 	if (sctp->sctp_send_adaptation)
5800Sstevel@tonic-gate 		iacklen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
5810Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
5823448Sdh155122 	    sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled) {
5830Sstevel@tonic-gate 		iacklen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 	if (initcollision)
5860Sstevel@tonic-gate 		iacklen += sctp_supaddr_param_len(sctp);
587432Svi117747 	if (!linklocal)
5883795Skcpoon 		iacklen += sctp_addr_params(sctp, supp_af, NULL, B_FALSE);
5890Sstevel@tonic-gate 	ipsctplen += sizeof (*iacksh) + iacklen;
5900Sstevel@tonic-gate 	iacklen += errlen;
59111943SGeorge.Shepherd@Sun.COM 	/*
59211943SGeorge.Shepherd@Sun.COM 	 * Padding is applied after the cookie which is the end of chunk
59311943SGeorge.Shepherd@Sun.COM 	 * unless CAUSE blocks are appended when the pad must also be
59411943SGeorge.Shepherd@Sun.COM 	 * accounted for in iacklen.
59511943SGeorge.Shepherd@Sun.COM 	 */
59611943SGeorge.Shepherd@Sun.COM 	if ((pad = ipsctplen % SCTP_ALIGN) != 0) {
59711943SGeorge.Shepherd@Sun.COM 		pad = SCTP_ALIGN - pad;
5980Sstevel@tonic-gate 		ipsctplen += pad;
59911943SGeorge.Shepherd@Sun.COM 		if (errmp != NULL)
60011943SGeorge.Shepherd@Sun.COM 			iacklen += pad;
6010Sstevel@tonic-gate 	}
6022776Skp158701 
6032776Skp158701 	/*
60411042SErik.Nordmark@Sun.COM 	 * Base the transmission on any routing-related socket options
60511042SErik.Nordmark@Sun.COM 	 * that have been set on the listener.
60611042SErik.Nordmark@Sun.COM 	 */
60711042SErik.Nordmark@Sun.COM 	ixa = conn_get_ixa_exclusive(connp);
60811042SErik.Nordmark@Sun.COM 	if (ixa == NULL) {
60911042SErik.Nordmark@Sun.COM 		sctp_send_abort(sctp, sctp_init2vtag(ch),
61011042SErik.Nordmark@Sun.COM 		    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE, ira);
61111042SErik.Nordmark@Sun.COM 		return;
61211042SErik.Nordmark@Sun.COM 	}
61311042SErik.Nordmark@Sun.COM 	ixa->ixa_flags &= ~IXAF_VERIFY_PMTU;
61411042SErik.Nordmark@Sun.COM 
61511042SErik.Nordmark@Sun.COM 	if (isv4)
61611042SErik.Nordmark@Sun.COM 		ixa->ixa_flags |= IXAF_IS_IPV4;
61711042SErik.Nordmark@Sun.COM 	else
61811042SErik.Nordmark@Sun.COM 		ixa->ixa_flags &= ~IXAF_IS_IPV4;
61911042SErik.Nordmark@Sun.COM 
62011042SErik.Nordmark@Sun.COM 	/*
62111042SErik.Nordmark@Sun.COM 	 * If the listen socket is bound to a trusted extensions multi-label
62211042SErik.Nordmark@Sun.COM 	 * port, a MAC-Exempt connection with an unlabeled node, we use the
6232776Skp158701 	 * the security label of the received INIT packet.
6242776Skp158701 	 * If not a multi-label port, attach the unmodified
62511042SErik.Nordmark@Sun.COM 	 * listener's label directly.
6262776Skp158701 	 *
6272776Skp158701 	 * We expect Sun developed kernel modules to properly set
6282776Skp158701 	 * cred labels for sctp connections. We can't be so sure this
6292776Skp158701 	 * will be done correctly when 3rd party kernel modules
63011042SErik.Nordmark@Sun.COM 	 * directly use sctp. We check for a NULL ira_tsl to cover this
63111042SErik.Nordmark@Sun.COM 	 * possibility.
6322776Skp158701 	 */
63311042SErik.Nordmark@Sun.COM 	if (is_system_labeled()) {
63411042SErik.Nordmark@Sun.COM 		/* Discard any old label */
63511042SErik.Nordmark@Sun.COM 		if (ixa->ixa_free_flags & IXA_FREE_TSL) {
63611042SErik.Nordmark@Sun.COM 			ASSERT(ixa->ixa_tsl != NULL);
63711042SErik.Nordmark@Sun.COM 			label_rele(ixa->ixa_tsl);
63811042SErik.Nordmark@Sun.COM 			ixa->ixa_free_flags &= ~IXA_FREE_TSL;
63911042SErik.Nordmark@Sun.COM 			ixa->ixa_tsl = NULL;
6402776Skp158701 		}
64111042SErik.Nordmark@Sun.COM 
64211042SErik.Nordmark@Sun.COM 		if (connp->conn_mlp_type != mlptSingle ||
64311042SErik.Nordmark@Sun.COM 		    connp->conn_mac_mode != CONN_MAC_DEFAULT) {
64411042SErik.Nordmark@Sun.COM 			if (ira->ira_tsl == NULL) {
64511042SErik.Nordmark@Sun.COM 				sctp_send_abort(sctp, sctp_init2vtag(ch),
64611042SErik.Nordmark@Sun.COM 				    SCTP_ERR_UNKNOWN, NULL, 0, initmp, 0,
64711042SErik.Nordmark@Sun.COM 				    B_FALSE, ira);
64811042SErik.Nordmark@Sun.COM 				ixa_refrele(ixa);
64911042SErik.Nordmark@Sun.COM 				return;
65011042SErik.Nordmark@Sun.COM 			}
65111042SErik.Nordmark@Sun.COM 			label_hold(ira->ira_tsl);
65211042SErik.Nordmark@Sun.COM 			ip_xmit_attr_replace_tsl(ixa, ira->ira_tsl);
65311042SErik.Nordmark@Sun.COM 		} else {
65411042SErik.Nordmark@Sun.COM 			ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6552776Skp158701 		}
6562776Skp158701 	}
65711042SErik.Nordmark@Sun.COM 
65811042SErik.Nordmark@Sun.COM 	iackmp = allocb(ipsctplen + sctps->sctps_wroff_xtra, BPRI_MED);
6590Sstevel@tonic-gate 	if (iackmp == NULL) {
6600Sstevel@tonic-gate 		sctp_send_abort(sctp, sctp_init2vtag(ch),
66111042SErik.Nordmark@Sun.COM 		    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE, ira);
66211042SErik.Nordmark@Sun.COM 		ixa_refrele(ixa);
6630Sstevel@tonic-gate 		return;
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	/* Copy in the [imcomplete] IP/SCTP composite header */
6673448Sdh155122 	p = (char *)(iackmp->b_rptr + sctps->sctps_wroff_xtra);
6680Sstevel@tonic-gate 	iackmp->b_rptr = (uchar_t *)p;
6690Sstevel@tonic-gate 	if (isv4) {
6700Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc, p, sctp->sctp_hdr_len);
6710Sstevel@tonic-gate 		iackiph = (ipha_t *)p;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		/* Copy the peer's IP addr */
6740Sstevel@tonic-gate 		iackiph->ipha_dst = initiph->ipha_src;
6750Sstevel@tonic-gate 		iackiph->ipha_src = initiph->ipha_dst;
6760Sstevel@tonic-gate 		iackiph->ipha_length = htons(ipsctplen + errlen);
6770Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr_len);
67811042SErik.Nordmark@Sun.COM 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr_len;
6790Sstevel@tonic-gate 	} else {
6800Sstevel@tonic-gate 		bcopy(sctp->sctp_iphc6, p, sctp->sctp_hdr6_len);
6810Sstevel@tonic-gate 		iackip6h = (ip6_t *)p;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		/* Copy the peer's IP addr */
6840Sstevel@tonic-gate 		iackip6h->ip6_dst = initip6h->ip6_src;
6850Sstevel@tonic-gate 		iackip6h->ip6_src = initip6h->ip6_dst;
68611042SErik.Nordmark@Sun.COM 		iackip6h->ip6_plen = htons(ipsctplen + errlen - IPV6_HDR_LEN);
6870Sstevel@tonic-gate 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr6_len);
68811042SErik.Nordmark@Sun.COM 		ixa->ixa_ip_hdr_length = sctp->sctp_ip_hdr6_len;
6890Sstevel@tonic-gate 	}
69011042SErik.Nordmark@Sun.COM 	ixa->ixa_pktlen = ipsctplen + errlen;
69111042SErik.Nordmark@Sun.COM 
6920Sstevel@tonic-gate 	ASSERT(OK_32PTR(iacksh));
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	/* Fill in the holes in the SCTP common header */
6950Sstevel@tonic-gate 	iacksh->sh_sport = initsh->sh_dport;
6960Sstevel@tonic-gate 	iacksh->sh_dport = initsh->sh_sport;
6970Sstevel@tonic-gate 	iacksh->sh_verf = init->sic_inittag;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* INIT ACK chunk header */
7000Sstevel@tonic-gate 	iack_ch = (sctp_chunk_hdr_t *)(iacksh + 1);
7010Sstevel@tonic-gate 	iack_ch->sch_id = CHUNK_INIT_ACK;
7020Sstevel@tonic-gate 	iack_ch->sch_flags = 0;
7030Sstevel@tonic-gate 	iack_ch->sch_len = htons(iacklen);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/* The INIT ACK itself */
7060Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iack_ch + 1);
7070Sstevel@tonic-gate 	iack->sic_inittag = itag;	/* already in network byteorder */
7080Sstevel@tonic-gate 	iack->sic_inittsn = htonl(itsn);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	iack->sic_a_rwnd = htonl(sctp->sctp_rwnd);
7110Sstevel@tonic-gate 	/* Advertise what we would want to have as stream #'s */
7120Sstevel@tonic-gate 	iack->sic_outstr = htons(MIN(sctp->sctp_num_ostr,
7130Sstevel@tonic-gate 	    ntohs(init->sic_instr)));
7140Sstevel@tonic-gate 	iack->sic_instr = htons(sctp->sctp_num_istr);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	p = (char *)(iack + 1);
7175586Skcpoon 	p += sctp_adaptation_code_param(sctp, (uchar_t *)p);
7180Sstevel@tonic-gate 	if (initcollision)
7190Sstevel@tonic-gate 		p += sctp_supaddr_param(sctp, (uchar_t *)p);
720432Svi117747 	if (!linklocal)
7213795Skcpoon 		p += sctp_addr_params(sctp, supp_af, (uchar_t *)p, B_FALSE);
7220Sstevel@tonic-gate 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
7233448Sdh155122 	    sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled) {
7240Sstevel@tonic-gate 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 	/*
7270Sstevel@tonic-gate 	 * Generate and lay in the COOKIE parameter.
7280Sstevel@tonic-gate 	 *
729852Svi117747 	 * Any change here that results in a change of location for
730852Svi117747 	 * the peer's orig source address must be propagated to the fn
731852Svi117747 	 * cl_sctp_cookie_paddr() above.
732852Svi117747 	 *
7330Sstevel@tonic-gate 	 * The cookie consists of:
7340Sstevel@tonic-gate 	 * 1. The relative timestamp for the cookie (lbolt64)
7350Sstevel@tonic-gate 	 * 2. The cookie lifetime (uint32_t) in tick
7360Sstevel@tonic-gate 	 * 3. The local tie-tag
7370Sstevel@tonic-gate 	 * 4. The peer tie-tag
7380Sstevel@tonic-gate 	 * 5. Peer's original src, used to confirm the validity of address.
7390Sstevel@tonic-gate 	 * 6. Our INIT ACK chunk, less any parameters
7400Sstevel@tonic-gate 	 * 7. The INIT chunk (may contain parameters)
7410Sstevel@tonic-gate 	 * 8. 128-bit MD5 signature.
7420Sstevel@tonic-gate 	 *
7430Sstevel@tonic-gate 	 * Since the timestamp values will only be evaluated locally, we
7440Sstevel@tonic-gate 	 * don't need to worry about byte-ordering them.
7450Sstevel@tonic-gate 	 */
7460Sstevel@tonic-gate 	cookieph = (sctp_parm_hdr_t *)p;
7470Sstevel@tonic-gate 	cookieph->sph_type = htons(PARM_COOKIE);
7480Sstevel@tonic-gate 	cookieph->sph_len = htons(cookielen);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/* timestamp */
7510Sstevel@tonic-gate 	now = (int64_t *)(cookieph + 1);
75212869SKacheong.Poon@Sun.COM 	nowt = LBOLT_FASTPATH64;
7530Sstevel@tonic-gate 	bcopy(&nowt, now, sizeof (*now));
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/* cookie lifetime -- need configuration */
7560Sstevel@tonic-gate 	lifetime = (uint32_t *)(now + 1);
7570Sstevel@tonic-gate 	*lifetime = sctp->sctp_cookie_lifetime;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/* Set the tie-tags */
7600Sstevel@tonic-gate 	ttag = (uint32_t *)(lifetime + 1);
7610Sstevel@tonic-gate 	if (sctp->sctp_state <= SCTPS_COOKIE_WAIT) {
7620Sstevel@tonic-gate 		*ttag = 0;
7630Sstevel@tonic-gate 		ttag++;
7640Sstevel@tonic-gate 		*ttag = 0;
7650Sstevel@tonic-gate 		ttag++;
7660Sstevel@tonic-gate 	} else {
7670Sstevel@tonic-gate 		/* local tie-tag (network byte-order) */
7680Sstevel@tonic-gate 		*ttag = sctp->sctp_lvtag;
7690Sstevel@tonic-gate 		ttag++;
7700Sstevel@tonic-gate 		/* peer tie-tag (network byte-order) */
7710Sstevel@tonic-gate 		*ttag = sctp->sctp_fvtag;
7720Sstevel@tonic-gate 		ttag++;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	/*
7750Sstevel@tonic-gate 	 * Copy in peer's original source address so that we can confirm
7760Sstevel@tonic-gate 	 * the reachability later.
7770Sstevel@tonic-gate 	 */
7780Sstevel@tonic-gate 	p = (char *)ttag;
7790Sstevel@tonic-gate 	if (isv4) {
7800Sstevel@tonic-gate 		in6_addr_t peer_addr;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iackiph->ipha_dst, &peer_addr);
7830Sstevel@tonic-gate 		bcopy(&peer_addr, p, sizeof (in6_addr_t));
7840Sstevel@tonic-gate 	} else {
7850Sstevel@tonic-gate 		bcopy(&iackip6h->ip6_dst, p, sizeof (in6_addr_t));
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	p += sizeof (in6_addr_t);
7880Sstevel@tonic-gate 	/* Copy in our INIT ACK chunk */
7890Sstevel@tonic-gate 	bcopy(iack, p, sizeof (*iack));
7900Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)p;
7910Sstevel@tonic-gate 	/* Set the # of streams we'll end up using */
7920Sstevel@tonic-gate 	iack->sic_outstr = MIN(sctp->sctp_num_ostr, ntohs(init->sic_instr));
7930Sstevel@tonic-gate 	iack->sic_instr = MIN(sctp->sctp_num_istr, ntohs(init->sic_outstr));
7940Sstevel@tonic-gate 	p += sizeof (*iack);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/* Copy in the peer's INIT chunk */
7970Sstevel@tonic-gate 	bcopy(ch, p, ntohs(ch->sch_len));
7980Sstevel@tonic-gate 	p += ntohs(ch->sch_len);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	/*
8010Sstevel@tonic-gate 	 * Calculate the HMAC ICV into the digest slot in buf.
8020Sstevel@tonic-gate 	 * First, generate a new secret if the current secret is
8030Sstevel@tonic-gate 	 * older than the new secret lifetime parameter permits,
8040Sstevel@tonic-gate 	 * copying the current secret to sctp_old_secret.
8050Sstevel@tonic-gate 	 */
8063448Sdh155122 	if (sctps->sctps_new_secret_interval > 0 &&
8070Sstevel@tonic-gate 	    (sctp->sctp_last_secret_update +
8083448Sdh155122 	    MSEC_TO_TICK(sctps->sctps_new_secret_interval)) <= nowt) {
8090Sstevel@tonic-gate 		bcopy(sctp->sctp_secret, sctp->sctp_old_secret,
8100Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
8110Sstevel@tonic-gate 		(void) random_get_pseudo_bytes(sctp->sctp_secret,
8120Sstevel@tonic-gate 		    SCTP_SECRET_LEN);
8130Sstevel@tonic-gate 		sctp->sctp_last_secret_update = nowt;
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	hmac_md5((uchar_t *)now, cookielen - sizeof (*cookieph) - 16,
8170Sstevel@tonic-gate 	    (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN, (uchar_t *)p);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	iackmp->b_wptr = iackmp->b_rptr + ipsctplen;
8208549SGeorge.Shepherd@Sun.COM 	if (pad != 0)
8218549SGeorge.Shepherd@Sun.COM 		bzero((iackmp->b_wptr - pad), pad);
8228549SGeorge.Shepherd@Sun.COM 
8230Sstevel@tonic-gate 	iackmp->b_cont = errmp;		/*  OK if NULL */
8240Sstevel@tonic-gate 
82511042SErik.Nordmark@Sun.COM 	if (is_system_labeled()) {
82611042SErik.Nordmark@Sun.COM 		ts_label_t *effective_tsl = NULL;
82711042SErik.Nordmark@Sun.COM 
82811042SErik.Nordmark@Sun.COM 		ASSERT(ira->ira_tsl != NULL);
82911042SErik.Nordmark@Sun.COM 
83011042SErik.Nordmark@Sun.COM 		/* Discard any old label */
83111042SErik.Nordmark@Sun.COM 		if (ixa->ixa_free_flags & IXA_FREE_TSL) {
83211042SErik.Nordmark@Sun.COM 			ASSERT(ixa->ixa_tsl != NULL);
83311042SErik.Nordmark@Sun.COM 			label_rele(ixa->ixa_tsl);
83411042SErik.Nordmark@Sun.COM 			ixa->ixa_free_flags &= ~IXA_FREE_TSL;
83511042SErik.Nordmark@Sun.COM 		}
83611042SErik.Nordmark@Sun.COM 		ixa->ixa_tsl = ira->ira_tsl;	/* A multi-level responder */
8371676Sjpk 
83811042SErik.Nordmark@Sun.COM 		/*
83911042SErik.Nordmark@Sun.COM 		 * We need to check for label-related failures which implies
84011042SErik.Nordmark@Sun.COM 		 * an extra call to tsol_check_dest (as ip_output_simple
84111042SErik.Nordmark@Sun.COM 		 * also does a tsol_check_dest as part of computing the
84211042SErik.Nordmark@Sun.COM 		 * label for the packet, but ip_output_simple doesn't return
84311042SErik.Nordmark@Sun.COM 		 * a specific errno for that case so we can't rely on its
84411042SErik.Nordmark@Sun.COM 		 * check.)
84511042SErik.Nordmark@Sun.COM 		 */
84611042SErik.Nordmark@Sun.COM 		if (isv4) {
84711042SErik.Nordmark@Sun.COM 			err = tsol_check_dest(ixa->ixa_tsl, &iackiph->ipha_dst,
84811042SErik.Nordmark@Sun.COM 			    IPV4_VERSION, connp->conn_mac_mode,
84911042SErik.Nordmark@Sun.COM 			    connp->conn_zone_is_global, &effective_tsl);
85011042SErik.Nordmark@Sun.COM 		} else {
85111042SErik.Nordmark@Sun.COM 			err = tsol_check_dest(ixa->ixa_tsl, &iackip6h->ip6_dst,
85211042SErik.Nordmark@Sun.COM 			    IPV6_VERSION, connp->conn_mac_mode,
85311042SErik.Nordmark@Sun.COM 			    connp->conn_zone_is_global, &effective_tsl);
85411042SErik.Nordmark@Sun.COM 		}
8551676Sjpk 		if (err != 0) {
8561676Sjpk 			sctp_send_abort(sctp, sctp_init2vtag(ch),
85711042SErik.Nordmark@Sun.COM 			    SCTP_ERR_AUTH_ERR, NULL, 0, initmp, 0, B_FALSE,
85811042SErik.Nordmark@Sun.COM 			    ira);
85911042SErik.Nordmark@Sun.COM 			ixa_refrele(ixa);
8601676Sjpk 			freemsg(iackmp);
8611676Sjpk 			return;
8621676Sjpk 		}
86311042SErik.Nordmark@Sun.COM 		if (effective_tsl != NULL) {
86411042SErik.Nordmark@Sun.COM 			/*
86511042SErik.Nordmark@Sun.COM 			 * Since ip_output_simple will redo the
86611042SErik.Nordmark@Sun.COM 			 * tsol_check_dest, we just drop the ref.
86711042SErik.Nordmark@Sun.COM 			 */
86811042SErik.Nordmark@Sun.COM 			label_rele(effective_tsl);
86911042SErik.Nordmark@Sun.COM 		}
8701676Sjpk 	}
8711676Sjpk 
8720Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_opkts);
8730Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
8740Sstevel@tonic-gate 
87511042SErik.Nordmark@Sun.COM 	(void) ip_output_simple(iackmp, ixa);
87611042SErik.Nordmark@Sun.COM 	ixa_refrele(ixa);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate void
sctp_send_cookie_ack(sctp_t * sctp)8800Sstevel@tonic-gate sctp_send_cookie_ack(sctp_t *sctp)
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate 	sctp_chunk_hdr_t *cach;
8830Sstevel@tonic-gate 	mblk_t *camp;
8843448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
8850Sstevel@tonic-gate 
88611042SErik.Nordmark@Sun.COM 	camp = sctp_make_mp(sctp, sctp->sctp_current, sizeof (*cach));
8870Sstevel@tonic-gate 	if (camp == NULL) {
8880Sstevel@tonic-gate 		/* XXX should abort, but don't have the inmp anymore */
8893448Sdh155122 		SCTP_KSTAT(sctps, sctp_send_cookie_ack_failed);
8900Sstevel@tonic-gate 		return;
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	cach = (sctp_chunk_hdr_t *)camp->b_wptr;
8940Sstevel@tonic-gate 	camp->b_wptr = (uchar_t *)(cach + 1);
8950Sstevel@tonic-gate 	cach->sch_id = CHUNK_COOKIE_ACK;
8960Sstevel@tonic-gate 	cach->sch_flags = 0;
8970Sstevel@tonic-gate 	cach->sch_len = htons(sizeof (*cach));
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
9000Sstevel@tonic-gate 
901*13009SChandrasekar.Marimuthu@Sun.COM 	sctp_set_iplen(sctp, camp, sctp->sctp_current->sf_ixa);
902*13009SChandrasekar.Marimuthu@Sun.COM 	(void) conn_ip_output(camp, sctp->sctp_current->sf_ixa);
90311042SErik.Nordmark@Sun.COM 	BUMP_LOCAL(sctp->sctp_opkts);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate static int
sctp_find_al_ind(sctp_parm_hdr_t * sph,ssize_t len,uint32_t * adaptation_code)9075586Skcpoon sctp_find_al_ind(sctp_parm_hdr_t *sph, ssize_t len, uint32_t *adaptation_code)
9080Sstevel@tonic-gate {
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	if (len < sizeof (*sph))
9110Sstevel@tonic-gate 		return (-1);
9120Sstevel@tonic-gate 	while (sph != NULL) {
9130Sstevel@tonic-gate 		if (sph->sph_type == htons(PARM_ADAPT_LAYER_IND) &&
9140Sstevel@tonic-gate 		    ntohs(sph->sph_len) >= (sizeof (*sph) +
9155586Skcpoon 		    sizeof (uint32_t))) {
9165586Skcpoon 			*adaptation_code = *(uint32_t *)(sph + 1);
9170Sstevel@tonic-gate 			return (0);
9180Sstevel@tonic-gate 		}
9190Sstevel@tonic-gate 		sph = sctp_next_parm(sph, &len);
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 	return (-1);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate void
sctp_send_cookie_echo(sctp_t * sctp,sctp_chunk_hdr_t * iackch,mblk_t * iackmp,ip_recv_attr_t * ira)92511042SErik.Nordmark@Sun.COM sctp_send_cookie_echo(sctp_t *sctp, sctp_chunk_hdr_t *iackch, mblk_t *iackmp,
92611042SErik.Nordmark@Sun.COM     ip_recv_attr_t *ira)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	mblk_t			*cemp;
9290Sstevel@tonic-gate 	mblk_t			*mp = NULL;
9300Sstevel@tonic-gate 	mblk_t			*head;
9310Sstevel@tonic-gate 	mblk_t			*meta;
9320Sstevel@tonic-gate 	sctp_faddr_t		*fp;
9330Sstevel@tonic-gate 	sctp_chunk_hdr_t	*cech;
9340Sstevel@tonic-gate 	sctp_init_chunk_t	 *iack;
9350Sstevel@tonic-gate 	int32_t			cansend;
9360Sstevel@tonic-gate 	int32_t			seglen;
9370Sstevel@tonic-gate 	size_t			ceclen;
9380Sstevel@tonic-gate 	sctp_parm_hdr_t		*cph;
9390Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
9400Sstevel@tonic-gate 	sctp_tf_t		*tf;
941432Svi117747 	int			pad = 0;
9420Sstevel@tonic-gate 	int			hdrlen;
9430Sstevel@tonic-gate 	mblk_t			*errmp = NULL;
9440Sstevel@tonic-gate 	uint_t			sctp_options;
9450Sstevel@tonic-gate 	int			error;
9460Sstevel@tonic-gate 	uint16_t		old_num_str;
9473448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(iackch + 1);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	cph = NULL;
9520Sstevel@tonic-gate 	if (validate_init_params(sctp, iackch, iack, iackmp, &cph, &errmp,
95311042SErik.Nordmark@Sun.COM 	    &pad, &sctp_options, ira) == 0) { /* result in 'pad' ignored */
95412869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpAborted);
9550Sstevel@tonic-gate 		sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
9560Sstevel@tonic-gate 		sctp_clean_death(sctp, ECONNABORTED);
9570Sstevel@tonic-gate 		return;
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 	ASSERT(cph != NULL);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	ASSERT(sctp->sctp_cookie_mp == NULL);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/* Got a cookie to echo back; allocate an mblk */
9640Sstevel@tonic-gate 	ceclen = sizeof (*cech) + ntohs(cph->sph_len) - sizeof (*cph);
9650Sstevel@tonic-gate 	if ((pad = ceclen & (SCTP_ALIGN - 1)) != 0)
9660Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	if (IPH_HDR_VERSION(iackmp->b_rptr) == IPV4_VERSION)
9690Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
9700Sstevel@tonic-gate 	else
9710Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
9720Sstevel@tonic-gate 
97311042SErik.Nordmark@Sun.COM 	cemp = allocb(sctps->sctps_wroff_xtra + hdrlen + ceclen + pad,
97411042SErik.Nordmark@Sun.COM 	    BPRI_MED);
9750Sstevel@tonic-gate 	if (cemp == NULL) {
9760Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
977*13009SChandrasekar.Marimuthu@Sun.COM 		    sctp->sctp_current->sf_rto);
9780Sstevel@tonic-gate 		if (errmp != NULL)
9790Sstevel@tonic-gate 			freeb(errmp);
9800Sstevel@tonic-gate 		return;
9810Sstevel@tonic-gate 	}
9823448Sdh155122 	cemp->b_rptr += (sctps->sctps_wroff_xtra + hdrlen);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/* Process the INIT ACK */
9850Sstevel@tonic-gate 	sctp->sctp_sctph->sh_verf = iack->sic_inittag;
9860Sstevel@tonic-gate 	sctp->sctp_sctph6->sh_verf = iack->sic_inittag;
9870Sstevel@tonic-gate 	sctp->sctp_fvtag = iack->sic_inittag;
9880Sstevel@tonic-gate 	sctp->sctp_ftsn = ntohl(iack->sic_inittsn);
9890Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
9900Sstevel@tonic-gate 	sctp->sctp_fcsn = sctp->sctp_lastacked;
9910Sstevel@tonic-gate 	sctp->sctp_frwnd = ntohl(iack->sic_a_rwnd);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * Populate sctp with addresses given in the INIT ACK or IP header.
9950Sstevel@tonic-gate 	 * Need to set the df bit in the current fp as it has been cleared
9960Sstevel@tonic-gate 	 * in sctp_connect().
9970Sstevel@tonic-gate 	 */
998*13009SChandrasekar.Marimuthu@Sun.COM 	sctp->sctp_current->sf_df = B_TRUE;
99911042SErik.Nordmark@Sun.COM 	sctp->sctp_ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
100011042SErik.Nordmark@Sun.COM 
10010Sstevel@tonic-gate 	/*
10020Sstevel@tonic-gate 	 * Since IP uses this info during the fanout process, we need to hold
10030Sstevel@tonic-gate 	 * the lock for this hash line while performing this operation.
10040Sstevel@tonic-gate 	 */
100511042SErik.Nordmark@Sun.COM 	/* XXX sctp_conn_fanout + SCTP_CONN_HASH(sctps, connp->conn_ports); */
10060Sstevel@tonic-gate 	ASSERT(sctp->sctp_conn_tfp != NULL);
10070Sstevel@tonic-gate 	tf = sctp->sctp_conn_tfp;
10080Sstevel@tonic-gate 	/* sctp isn't a listener so only need to hold conn fanout lock */
10090Sstevel@tonic-gate 	mutex_enter(&tf->tf_lock);
10100Sstevel@tonic-gate 	if (sctp_get_addrparams(sctp, NULL, iackmp, iackch, NULL) != 0) {
10110Sstevel@tonic-gate 		mutex_exit(&tf->tf_lock);
10120Sstevel@tonic-gate 		freeb(cemp);
10130Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
1014*13009SChandrasekar.Marimuthu@Sun.COM 		    sctp->sctp_current->sf_rto);
10150Sstevel@tonic-gate 		if (errmp != NULL)
10160Sstevel@tonic-gate 			freeb(errmp);
10170Sstevel@tonic-gate 		return;
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 	mutex_exit(&tf->tf_lock);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	fp = sctp->sctp_current;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	/*
10240Sstevel@tonic-gate 	 * There could be a case when we get an INIT-ACK again, if the INIT
10250Sstevel@tonic-gate 	 * is re-transmitted, for e.g., which means we would have already
10260Sstevel@tonic-gate 	 * allocated this resource earlier (also for sctp_instr). In this
10270Sstevel@tonic-gate 	 * case we check and re-allocate, if necessary.
10280Sstevel@tonic-gate 	 */
10290Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_ostr;
10300Sstevel@tonic-gate 	if (ntohs(iack->sic_instr) < sctp->sctp_num_ostr)
10310Sstevel@tonic-gate 		sctp->sctp_num_ostr = ntohs(iack->sic_instr);
10320Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
10330Sstevel@tonic-gate 		sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
10340Sstevel@tonic-gate 		    sctp->sctp_num_ostr, KM_NOSLEEP);
10350Sstevel@tonic-gate 	} else {
10360Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
10370Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_ostr) {
10380Sstevel@tonic-gate 			kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
10390Sstevel@tonic-gate 			    old_num_str);
10400Sstevel@tonic-gate 			sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
10410Sstevel@tonic-gate 			    sctp->sctp_num_ostr, KM_NOSLEEP);
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 	if (sctp->sctp_ostrcntrs == NULL) {
10450Sstevel@tonic-gate 		freeb(cemp);
1046*13009SChandrasekar.Marimuthu@Sun.COM 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
10470Sstevel@tonic-gate 		if (errmp != NULL)
10480Sstevel@tonic-gate 			freeb(errmp);
10490Sstevel@tonic-gate 		return;
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * Allocate the in stream tracking array. Comments for sctp_ostrcntrs
10540Sstevel@tonic-gate 	 * hold here too.
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	old_num_str = sctp->sctp_num_istr;
10570Sstevel@tonic-gate 	if (ntohs(iack->sic_outstr) < sctp->sctp_num_istr)
10580Sstevel@tonic-gate 		sctp->sctp_num_istr = ntohs(iack->sic_outstr);
10590Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
10600Sstevel@tonic-gate 		sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
10610Sstevel@tonic-gate 		    sctp->sctp_num_istr, KM_NOSLEEP);
10620Sstevel@tonic-gate 	} else {
10630Sstevel@tonic-gate 		ASSERT(old_num_str > 0);
10640Sstevel@tonic-gate 		if (old_num_str != sctp->sctp_num_istr) {
10650Sstevel@tonic-gate 			kmem_free(sctp->sctp_instr,
10660Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * old_num_str);
10670Sstevel@tonic-gate 			sctp->sctp_instr = kmem_zalloc(
10680Sstevel@tonic-gate 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr,
10690Sstevel@tonic-gate 			    KM_NOSLEEP);
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 	if (sctp->sctp_instr == NULL) {
10730Sstevel@tonic-gate 		kmem_free(sctp->sctp_ostrcntrs,
10740Sstevel@tonic-gate 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
10750Sstevel@tonic-gate 		freeb(cemp);
1076*13009SChandrasekar.Marimuthu@Sun.COM 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
10770Sstevel@tonic-gate 		if (errmp != NULL)
10780Sstevel@tonic-gate 			freeb(errmp);
10790Sstevel@tonic-gate 		return;
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (!(sctp_options & SCTP_PRSCTP_OPTION) && sctp->sctp_prsctp_aware)
10830Sstevel@tonic-gate 		sctp->sctp_prsctp_aware = B_FALSE;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	if (sctp_find_al_ind((sctp_parm_hdr_t *)(iack + 1),
10865586Skcpoon 	    ntohs(iackch->sch_len) - (sizeof (*iackch) + sizeof (*iack)),
10875586Skcpoon 	    &sctp->sctp_rx_adaptation_code) == 0) {
10885586Skcpoon 		sctp->sctp_recv_adaptation = 1;
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	cech = (sctp_chunk_hdr_t *)cemp->b_rptr;
10920Sstevel@tonic-gate 	ASSERT(OK_32PTR(cech));
10930Sstevel@tonic-gate 	cech->sch_id = CHUNK_COOKIE;
10940Sstevel@tonic-gate 	cech->sch_flags = 0;
10950Sstevel@tonic-gate 	cech->sch_len = htons(ceclen);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/* Copy the cookie (less the parm hdr) to the chunk */
10980Sstevel@tonic-gate 	bcopy(cph + 1, cech + 1, ceclen - sizeof (*cph));
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	cemp->b_wptr = cemp->b_rptr + ceclen;
11010Sstevel@tonic-gate 
1102252Svi117747 	if (sctp->sctp_unsent > 0) {
11030Sstevel@tonic-gate 		sctp_msg_hdr_t	*smh;
11040Sstevel@tonic-gate 		mblk_t		*prev = NULL;
11050Sstevel@tonic-gate 		uint32_t	unsent = 0;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		mp = sctp->sctp_xmit_unsent;
11080Sstevel@tonic-gate 		do {
11090Sstevel@tonic-gate 			smh = (sctp_msg_hdr_t *)mp->b_rptr;
11100Sstevel@tonic-gate 			if (smh->smh_sid >= sctp->sctp_num_ostr) {
11110Sstevel@tonic-gate 				unsent += smh->smh_msglen;
11120Sstevel@tonic-gate 				if (prev != NULL)
11130Sstevel@tonic-gate 					prev->b_next = mp->b_next;
11140Sstevel@tonic-gate 				else
11150Sstevel@tonic-gate 					sctp->sctp_xmit_unsent = mp->b_next;
11160Sstevel@tonic-gate 				mp->b_next = NULL;
11170Sstevel@tonic-gate 				sctp_sendfail_event(sctp, mp, SCTP_ERR_BAD_SID,
11180Sstevel@tonic-gate 				    B_FALSE);
11190Sstevel@tonic-gate 				if (prev != NULL)
11200Sstevel@tonic-gate 					mp = prev->b_next;
11210Sstevel@tonic-gate 				else
11220Sstevel@tonic-gate 					mp = sctp->sctp_xmit_unsent;
11230Sstevel@tonic-gate 			} else {
11240Sstevel@tonic-gate 				prev = mp;
11250Sstevel@tonic-gate 				mp = mp->b_next;
11260Sstevel@tonic-gate 			}
11270Sstevel@tonic-gate 		} while (mp != NULL);
11280Sstevel@tonic-gate 		if (unsent > 0) {
11290Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= unsent);
11300Sstevel@tonic-gate 			sctp->sctp_unsent -= unsent;
11310Sstevel@tonic-gate 			/*
11320Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
11330Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
11340Sstevel@tonic-gate 			 * This is not necessary, but doesn't harm, we
11350Sstevel@tonic-gate 			 * just use unsent instead of sent-unack'ed +
11360Sstevel@tonic-gate 			 * unsent, since there won't be any sent-unack'ed
11370Sstevel@tonic-gate 			 * here.
11380Sstevel@tonic-gate 			 */
11398348SEric.Yu@Sun.COM 			if (!SCTP_IS_DETACHED(sctp))
11408348SEric.Yu@Sun.COM 				SCTP_TXQ_UPDATE(sctp);
11410Sstevel@tonic-gate 		}
11420Sstevel@tonic-gate 		if (sctp->sctp_xmit_unsent == NULL)
11430Sstevel@tonic-gate 			sctp->sctp_xmit_unsent_tail = NULL;
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 	ceclen += pad;
11460Sstevel@tonic-gate 	cansend = MIN(sctp->sctp_unsent, sctp->sctp_frwnd);
11470Sstevel@tonic-gate 	meta = sctp_get_msg_to_send(sctp, &mp, NULL, &error, ceclen,
11480Sstevel@tonic-gate 	    cansend,  NULL);
11490Sstevel@tonic-gate 	/*
11500Sstevel@tonic-gate 	 * The error cannot be anything else since we could have an non-zero
11510Sstevel@tonic-gate 	 * error only if sctp_get_msg_to_send() tries to send a Forward
11520Sstevel@tonic-gate 	 * TSN which will not happen here.
11530Sstevel@tonic-gate 	 */
11540Sstevel@tonic-gate 	ASSERT(error == 0);
11550Sstevel@tonic-gate 	if (meta == NULL)
11560Sstevel@tonic-gate 		goto sendcookie;
11570Sstevel@tonic-gate 	sctp->sctp_xmit_tail = meta;
11580Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
11590Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
1160*13009SChandrasekar.Marimuthu@Sun.COM 	if ((ceclen + seglen) > fp->sf_pmss ||
11610Sstevel@tonic-gate 	    (seglen - sizeof (*sdc)) > cansend) {
11620Sstevel@tonic-gate 		goto sendcookie;
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 	/* OK, if this fails */
11650Sstevel@tonic-gate 	cemp->b_cont = dupmsg(mp);
11660Sstevel@tonic-gate sendcookie:
1167252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, cemp, 0, NULL);
1168252Svi117747 	if (head == NULL) {
1169252Svi117747 		freemsg(cemp);
1170*13009SChandrasekar.Marimuthu@Sun.COM 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
1171252Svi117747 		if (errmp != NULL)
1172252Svi117747 			freeb(errmp);
11733448Sdh155122 		SCTP_KSTAT(sctps, sctp_send_cookie_failed);
1174252Svi117747 		return;
1175252Svi117747 	}
11760Sstevel@tonic-gate 	/*
11770Sstevel@tonic-gate 	 * Even if cookie-echo exceeds MTU for one of the hops, it'll
11780Sstevel@tonic-gate 	 * have a chance of getting there.
11790Sstevel@tonic-gate 	 */
1180*13009SChandrasekar.Marimuthu@Sun.COM 	if (fp->sf_isv4) {
11810Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
11820Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	sctp->sctp_cookie_mp = dupmsg(head);
11870Sstevel@tonic-gate 	/* Don't bundle, we will just resend init if this cookie is lost. */
11880Sstevel@tonic-gate 	if (sctp->sctp_cookie_mp == NULL) {
11890Sstevel@tonic-gate 		if (cemp->b_cont != NULL) {
11900Sstevel@tonic-gate 			freemsg(cemp->b_cont);
11910Sstevel@tonic-gate 			cemp->b_cont = NULL;
11920Sstevel@tonic-gate 		}
11930Sstevel@tonic-gate 	} else if (cemp->b_cont != NULL) {
11940Sstevel@tonic-gate 		ASSERT(mp != NULL && mp == meta->b_cont);
11950Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(cemp->b_cont);
11960Sstevel@tonic-gate 		cemp->b_wptr += pad;
11970Sstevel@tonic-gate 		seglen -= sizeof (*sdc);
11980Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, seglen, meta);
11990Sstevel@tonic-gate 	}
12008153SGeorge.Shepherd@Sun.COM 	if (errmp != NULL) {
12018153SGeorge.Shepherd@Sun.COM 		if (cemp->b_cont == NULL)
12028153SGeorge.Shepherd@Sun.COM 			cemp->b_wptr += pad;
12030Sstevel@tonic-gate 		linkb(head, errmp);
12048153SGeorge.Shepherd@Sun.COM 	}
12050Sstevel@tonic-gate 	sctp->sctp_state = SCTPS_COOKIE_ECHOED;
1206*13009SChandrasekar.Marimuthu@Sun.COM 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
12070Sstevel@tonic-gate 
1208*13009SChandrasekar.Marimuthu@Sun.COM 	sctp_set_iplen(sctp, head, fp->sf_ixa);
1209*13009SChandrasekar.Marimuthu@Sun.COM 	(void) conn_ip_output(head, fp->sf_ixa);
121011042SErik.Nordmark@Sun.COM 	BUMP_LOCAL(sctp->sctp_opkts);
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate int
sctp_process_cookie(sctp_t * sctp,sctp_chunk_hdr_t * ch,mblk_t * cmp,sctp_init_chunk_t ** iackpp,sctp_hdr_t * insctph,int * recv_adaptation,in6_addr_t * peer_addr,ip_recv_attr_t * ira)12140Sstevel@tonic-gate sctp_process_cookie(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *cmp,
12155586Skcpoon     sctp_init_chunk_t **iackpp, sctp_hdr_t *insctph, int *recv_adaptation,
121611042SErik.Nordmark@Sun.COM     in6_addr_t *peer_addr, ip_recv_attr_t *ira)
12170Sstevel@tonic-gate {
12180Sstevel@tonic-gate 	int32_t			clen;
12190Sstevel@tonic-gate 	size_t			initplen;
12200Sstevel@tonic-gate 	uchar_t			*p;
12210Sstevel@tonic-gate 	uchar_t			*given_hash;
12220Sstevel@tonic-gate 	uchar_t			needed_hash[16];
12230Sstevel@tonic-gate 	int64_t			ts;
12240Sstevel@tonic-gate 	int64_t			diff;
12250Sstevel@tonic-gate 	uint32_t		*lt;
12260Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
12270Sstevel@tonic-gate 	sctp_chunk_hdr_t	*initch;
12280Sstevel@tonic-gate 	sctp_init_chunk_t	*init;
12290Sstevel@tonic-gate 	uint32_t		*lttag;
12300Sstevel@tonic-gate 	uint32_t		*fttag;
12310Sstevel@tonic-gate 	uint32_t		ports;
12323448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
123311042SErik.Nordmark@Sun.COM 	conn_t			*connp = sctp->sctp_connp;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
12360Sstevel@tonic-gate 	/* Verify the ICV */
12370Sstevel@tonic-gate 	clen = ntohs(ch->sch_len) - sizeof (*ch) - 16;
12380Sstevel@tonic-gate 	if (clen < 0) {
12390Sstevel@tonic-gate 		dprint(1, ("invalid cookie chunk length %d\n",
12400Sstevel@tonic-gate 		    ntohs(ch->sch_len)));
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		return (-1);
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 	p = (uchar_t *)(ch + 1);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	hmac_md5(p, clen, (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN,
12470Sstevel@tonic-gate 	    needed_hash);
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	/* The given hash follows the cookie data */
12500Sstevel@tonic-gate 	given_hash = p + clen;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	if (bcmp(given_hash, needed_hash, 16) != 0) {
12530Sstevel@tonic-gate 		/* The secret may have changed; try the old secret */
12540Sstevel@tonic-gate 		hmac_md5(p, clen, (uchar_t *)sctp->sctp_old_secret,
12550Sstevel@tonic-gate 		    SCTP_SECRET_LEN, needed_hash);
12560Sstevel@tonic-gate 		if (bcmp(given_hash, needed_hash, 16) != 0) {
12570Sstevel@tonic-gate 			return (-1);
12580Sstevel@tonic-gate 		}
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	/* Timestamp is int64_t, and we only guarantee 32-bit alignment */
12620Sstevel@tonic-gate 	bcopy(p, &ts, sizeof (ts));
12633795Skcpoon 	/* Cookie life time, uint32_t */
12640Sstevel@tonic-gate 	lt = (uint32_t *)(p + sizeof (ts));
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	/*
12670Sstevel@tonic-gate 	 * To quote PRC, "this is our baby", so let's continue.
12680Sstevel@tonic-gate 	 * We need to pull out the encapsulated INIT ACK and
12690Sstevel@tonic-gate 	 * INIT chunks. Note that we don't process these until
12700Sstevel@tonic-gate 	 * we have verified the timestamp, but we need them before
12710Sstevel@tonic-gate 	 * processing the timestamp since if the time check fails,
12720Sstevel@tonic-gate 	 * we need to get the verification tag from the INIT in order
12730Sstevel@tonic-gate 	 * to send a stale cookie error.
12740Sstevel@tonic-gate 	 */
12750Sstevel@tonic-gate 	lttag = (uint32_t *)(lt + 1);
12760Sstevel@tonic-gate 	fttag = lttag + 1;
12770Sstevel@tonic-gate 	if (peer_addr != NULL)
12780Sstevel@tonic-gate 		bcopy(fttag + 1, peer_addr, sizeof (in6_addr_t));
12790Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)((char *)(fttag + 1) + sizeof (in6_addr_t));
12800Sstevel@tonic-gate 	initch = (sctp_chunk_hdr_t *)(iack + 1);
12810Sstevel@tonic-gate 	init = (sctp_init_chunk_t *)(initch + 1);
12820Sstevel@tonic-gate 	initplen = ntohs(initch->sch_len) - (sizeof (*init) + sizeof (*initch));
12830Sstevel@tonic-gate 	*iackpp = iack;
12845586Skcpoon 	*recv_adaptation = 0;
12850Sstevel@tonic-gate 
12863795Skcpoon 	/*
12873795Skcpoon 	 * Check the staleness of the Cookie, specified in 3.3.10.3 of
12883795Skcpoon 	 * RFC 2960.
12893795Skcpoon 	 *
12903795Skcpoon 	 * The mesaure of staleness is the difference, in microseconds,
12913795Skcpoon 	 * between the current time and the time the State Cookie expires.
12923795Skcpoon 	 * So it is lbolt64 - (ts + *lt).  If it is positive, it means
12933795Skcpoon 	 * that the Cookie has expired.
12943795Skcpoon 	 */
129512869SKacheong.Poon@Sun.COM 	diff = LBOLT_FASTPATH64 - (ts + *lt);
12963795Skcpoon 	if (diff > 0 && (init->sic_inittag != sctp->sctp_fvtag ||
12970Sstevel@tonic-gate 	    iack->sic_inittag != sctp->sctp_lvtag)) {
12980Sstevel@tonic-gate 		uint32_t staleness;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 		staleness = TICK_TO_USEC(diff);
13010Sstevel@tonic-gate 		staleness = htonl(staleness);
13020Sstevel@tonic-gate 		sctp_send_abort(sctp, init->sic_inittag, SCTP_ERR_STALE_COOKIE,
130311042SErik.Nordmark@Sun.COM 		    (char *)&staleness, sizeof (staleness), cmp, 1, B_FALSE,
130411042SErik.Nordmark@Sun.COM 		    ira);
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 		dprint(1, ("stale cookie %d\n", staleness));
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 		return (-1);
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/* Check for attack by adding addresses to a restart */
13120Sstevel@tonic-gate 	bcopy(insctph, &ports, sizeof (ports));
13133448Sdh155122 	if (sctp_secure_restart_check(cmp, initch, ports, KM_NOSLEEP,
131411042SErik.Nordmark@Sun.COM 	    sctps, ira) != 1) {
13150Sstevel@tonic-gate 		return (-1);
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	/* Look for adaptation code if there any parms in the INIT chunk */
13190Sstevel@tonic-gate 	if ((initplen >= sizeof (sctp_parm_hdr_t)) &&
13200Sstevel@tonic-gate 	    (sctp_find_al_ind((sctp_parm_hdr_t *)(init + 1), initplen,
13215586Skcpoon 	    &sctp->sctp_rx_adaptation_code) == 0)) {
13225586Skcpoon 		*recv_adaptation = 1;
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/* Examine tie-tags */
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_WAIT) {
13280Sstevel@tonic-gate 		if (sctp->sctp_state == SCTPS_ESTABLISHED &&
13290Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
13300Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag &&
13310Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 			dprint(1, ("duplicate cookie from %x:%x:%x:%x (%d)\n",
1334*13009SChandrasekar.Marimuthu@Sun.COM 			    SCTP_PRINTADDR(sctp->sctp_current->sf_faddr),
133511042SErik.Nordmark@Sun.COM 			    (int)(connp->conn_fport)));
13360Sstevel@tonic-gate 			return (-1);
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		if (init->sic_inittag != sctp->sctp_fvtag &&
13400Sstevel@tonic-gate 		    iack->sic_inittag != sctp->sctp_lvtag &&
13410Sstevel@tonic-gate 		    *fttag == sctp->sctp_fvtag &&
13420Sstevel@tonic-gate 		    *lttag == sctp->sctp_lvtag) {
13430Sstevel@tonic-gate 			int i;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 			/* Section 5.2.4 case A: restart */
13460Sstevel@tonic-gate 			sctp->sctp_fvtag = init->sic_inittag;
13470Sstevel@tonic-gate 			sctp->sctp_lvtag = iack->sic_inittag;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 			sctp->sctp_sctph->sh_verf = init->sic_inittag;
13500Sstevel@tonic-gate 			sctp->sctp_sctph6->sh_verf = init->sic_inittag;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 			sctp->sctp_ftsn = ntohl(init->sic_inittsn);
13530Sstevel@tonic-gate 			sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
13540Sstevel@tonic-gate 			sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
13550Sstevel@tonic-gate 			sctp->sctp_fcsn = sctp->sctp_lastacked;
13560Sstevel@tonic-gate 
135712869SKacheong.Poon@Sun.COM 			if (sctp->sctp_state < SCTPS_ESTABLISHED)
135812869SKacheong.Poon@Sun.COM 				SCTP_ASSOC_EST(sctps, sctp);
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 			dprint(1, ("sctp peer %x:%x:%x:%x (%d) restarted\n",
1361*13009SChandrasekar.Marimuthu@Sun.COM 			    SCTP_PRINTADDR(sctp->sctp_current->sf_faddr),
136211042SErik.Nordmark@Sun.COM 			    (int)(connp->conn_fport)));
13630Sstevel@tonic-gate 			/* reset parameters */
13640Sstevel@tonic-gate 			sctp_congest_reset(sctp);
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 			/* reset stream bookkeeping */
13670Sstevel@tonic-gate 			sctp_instream_cleanup(sctp, B_FALSE);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 			sctp->sctp_istr_nmsgs = 0;
13700Sstevel@tonic-gate 			sctp->sctp_rxqueued = 0;
13710Sstevel@tonic-gate 			for (i = 0; i < sctp->sctp_num_ostr; i++) {
13720Sstevel@tonic-gate 				sctp->sctp_ostrcntrs[i] = 0;
13730Sstevel@tonic-gate 			}
13740Sstevel@tonic-gate 			/* XXX flush xmit_list? */
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 			return (0);
13770Sstevel@tonic-gate 		} else if (init->sic_inittag != sctp->sctp_fvtag &&
13780Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 			/* Section 5.2.4 case B: INIT collision */
13810Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
13820Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
13830Sstevel@tonic-gate 					return (-1);	/* Drop? */
138412869SKacheong.Poon@Sun.COM 				SCTP_ASSOC_EST(sctps, sctp);
13850Sstevel@tonic-gate 			}
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 			dprint(1, ("init collision with %x:%x:%x:%x (%d)\n",
1388*13009SChandrasekar.Marimuthu@Sun.COM 			    SCTP_PRINTADDR(sctp->sctp_current->sf_faddr),
138911042SErik.Nordmark@Sun.COM 			    (int)(connp->conn_fport)));
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 			return (0);
13920Sstevel@tonic-gate 		} else if (iack->sic_inittag != sctp->sctp_lvtag &&
13930Sstevel@tonic-gate 		    init->sic_inittag == sctp->sctp_fvtag &&
13940Sstevel@tonic-gate 		    *fttag == 0 && *lttag == 0) {
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 			/* Section 5.2.4 case C: late COOKIE */
13970Sstevel@tonic-gate 			dprint(1, ("late cookie from %x:%x:%x:%x (%d)\n",
1398*13009SChandrasekar.Marimuthu@Sun.COM 			    SCTP_PRINTADDR(sctp->sctp_current->sf_faddr),
139911042SErik.Nordmark@Sun.COM 			    (int)(connp->conn_fport)));
14000Sstevel@tonic-gate 			return (-1);
14010Sstevel@tonic-gate 		} else if (init->sic_inittag == sctp->sctp_fvtag &&
14020Sstevel@tonic-gate 		    iack->sic_inittag == sctp->sctp_lvtag) {
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 			/*
14050Sstevel@tonic-gate 			 * Section 5.2.4 case D: COOKIE ECHO retransmit
14060Sstevel@tonic-gate 			 * Don't check cookie lifetime
14070Sstevel@tonic-gate 			 */
14080Sstevel@tonic-gate 			dprint(1, ("cookie tags match from %x:%x:%x:%x (%d)\n",
1409*13009SChandrasekar.Marimuthu@Sun.COM 			    SCTP_PRINTADDR(sctp->sctp_current->sf_faddr),
141011042SErik.Nordmark@Sun.COM 			    (int)(connp->conn_fport)));
14110Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
14120Sstevel@tonic-gate 				if (!sctp_initialize_params(sctp, init, iack))
14130Sstevel@tonic-gate 					return (-1);	/* Drop? */
141412869SKacheong.Poon@Sun.COM 				SCTP_ASSOC_EST(sctps, sctp);
14150Sstevel@tonic-gate 			}
14160Sstevel@tonic-gate 			return (0);
14170Sstevel@tonic-gate 		} else {
14180Sstevel@tonic-gate 			/* unrecognized case -- silently drop it */
14190Sstevel@tonic-gate 			return (-1);
14200Sstevel@tonic-gate 		}
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	return (0);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate /*
14270Sstevel@tonic-gate  * Similar to ip_fanout_sctp, except that the src addr(s) are drawn
14280Sstevel@tonic-gate  * from address parameters in an INIT ACK's address list. This
14290Sstevel@tonic-gate  * function is used when an INIT ACK is received but IP's fanout
14300Sstevel@tonic-gate  * function could not find a sctp via the normal lookup routine.
14310Sstevel@tonic-gate  * This can happen when a host sends an INIT ACK from a different
14320Sstevel@tonic-gate  * address than the INIT was sent to.
14330Sstevel@tonic-gate  *
14340Sstevel@tonic-gate  * Returns the sctp_t if found, or NULL if not found.
14350Sstevel@tonic-gate  */
14360Sstevel@tonic-gate sctp_t *
sctp_addrlist2sctp(mblk_t * mp,sctp_hdr_t * sctph,sctp_chunk_hdr_t * ich,zoneid_t zoneid,sctp_stack_t * sctps)14370Sstevel@tonic-gate sctp_addrlist2sctp(mblk_t *mp, sctp_hdr_t *sctph, sctp_chunk_hdr_t *ich,
14383510Svi117747     zoneid_t zoneid, sctp_stack_t *sctps)
14390Sstevel@tonic-gate {
14400Sstevel@tonic-gate 	int isv4;
14410Sstevel@tonic-gate 	ipha_t *iph;
14420Sstevel@tonic-gate 	ip6_t *ip6h;
14430Sstevel@tonic-gate 	in6_addr_t dst;
144412339Sanil.udupa@sun.com 	in6_addr_t src, *srcp = &src;
14450Sstevel@tonic-gate 	sctp_parm_hdr_t *ph;
14460Sstevel@tonic-gate 	ssize_t remaining;
14470Sstevel@tonic-gate 	sctp_init_chunk_t *iack;
14480Sstevel@tonic-gate 	uint32_t ports;
14490Sstevel@tonic-gate 	sctp_t *sctp = NULL;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	ASSERT(ich->sch_id == CHUNK_INIT_ACK);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
14540Sstevel@tonic-gate 	if (isv4) {
14550Sstevel@tonic-gate 		iph = (ipha_t *)mp->b_rptr;
14560Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, &dst);
14570Sstevel@tonic-gate 	} else {
14580Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
14590Sstevel@tonic-gate 		dst = ip6h->ip6_dst;
14600Sstevel@tonic-gate 	}
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	ports = *(uint32_t *)sctph;
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	dprint(1, ("sctp_addrlist2sctp: ports=%u, dst = %x:%x:%x:%x\n",
14650Sstevel@tonic-gate 	    ports, SCTP_PRINTADDR(dst)));
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	/* pull out any address parameters */
14680Sstevel@tonic-gate 	remaining = ntohs(ich->sch_len) - sizeof (*ich) - sizeof (*iack);
14690Sstevel@tonic-gate 	if (remaining < sizeof (*ph)) {
14700Sstevel@tonic-gate 		return (NULL);
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	iack = (sctp_init_chunk_t *)(ich + 1);
14740Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(iack + 1);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	while (ph != NULL) {
14770Sstevel@tonic-gate 		/*
147812339Sanil.udupa@sun.com 		 * params have been verified in sctp_check_input(),
147912339Sanil.udupa@sun.com 		 * so no need to do it again here.
148011042SErik.Nordmark@Sun.COM 		 *
148111042SErik.Nordmark@Sun.COM 		 * For labeled systems, there's no need to check the
148211042SErik.Nordmark@Sun.COM 		 * label here.  It's known to be good as we checked
148311042SErik.Nordmark@Sun.COM 		 * before allowing the connection to become bound.
148412339Sanil.udupa@sun.com 		 *
148512339Sanil.udupa@sun.com 		 * According to RFC4960 :
148612339Sanil.udupa@sun.com 		 * All integer fields in an SCTP packet MUST be transmitted
148712339Sanil.udupa@sun.com 		 * in network byte order, unless otherwise stated.
148812339Sanil.udupa@sun.com 		 * Therefore convert the param type to network byte order.
14890Sstevel@tonic-gate 		 */
149012339Sanil.udupa@sun.com 		if (ph->sph_type == htons(PARM_ADDR4)) {
14910Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED((struct in_addr *)(ph + 1),
149212339Sanil.udupa@sun.com 			    srcp);
14930Sstevel@tonic-gate 
149412339Sanil.udupa@sun.com 			sctp = sctp_conn_match(&srcp, 1, &dst, ports, zoneid,
149511042SErik.Nordmark@Sun.COM 			    0, sctps);
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 			dprint(1,
14980Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
14991676Sjpk 			    SCTP_PRINTADDR(src), (void *)sctp));
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 			if (sctp != NULL) {
15030Sstevel@tonic-gate 				return (sctp);
15040Sstevel@tonic-gate 			}
150512339Sanil.udupa@sun.com 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
150612339Sanil.udupa@sun.com 			srcp = (in6_addr_t *)(ph + 1);
150712339Sanil.udupa@sun.com 			sctp = sctp_conn_match(&srcp, 1, &dst, ports, zoneid,
150811042SErik.Nordmark@Sun.COM 			    0, sctps);
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 			dprint(1,
15110Sstevel@tonic-gate 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
15121676Sjpk 			    SCTP_PRINTADDR(src), (void *)sctp));
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 			if (sctp != NULL) {
15150Sstevel@tonic-gate 				return (sctp);
15160Sstevel@tonic-gate 			}
15170Sstevel@tonic-gate 		}
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &remaining);
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	return (NULL);
15230Sstevel@tonic-gate }
1524