xref: /titanic_44/usr/src/uts/common/inet/sctp/sctp_cookie.c (revision 9dd0f810214fdc8e1af881a9a5c4b6927629ff9e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/stream.h>
32 #include <sys/cmn_err.h>
33 #include <sys/md5.h>
34 #include <sys/kmem.h>
35 #include <sys/strsubr.h>
36 #include <sys/random.h>
37 #include <sys/tsol/tnet.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/ip6.h>
41 
42 #include <inet/common.h>
43 #include <inet/ip.h>
44 #include <inet/ip6.h>
45 #include <inet/sctp_ip.h>
46 #include <inet/ipclassifier.h>
47 #include "sctp_impl.h"
48 
49 /*
50  * Helper function for SunCluster (PSARC/2005/602) to get the original source
51  * address from the COOKIE
52  */
53 int cl_sctp_cookie_paddr(sctp_chunk_hdr_t *, in6_addr_t *);
54 
55 /*
56  * From RFC 2104. This should probably go into libmd5 (and while
57  * we're at it, maybe we should make a libdigest so we can later
58  * add SHA1 and others, esp. since some weaknesses have been found
59  * with MD5).
60  *
61  * text		IN			pointer to data stream
62  * text_len	IN			length of data stream
63  * key		IN			pointer to authentication key
64  * key_len	IN			length of authentication key
65  * digest	OUT			caller digest to be filled in
66  */
67 static void
68 hmac_md5(uchar_t *text, size_t text_len, uchar_t *key, size_t key_len,
69     uchar_t *digest)
70 {
71 	MD5_CTX context;
72 	uchar_t k_ipad[65];	/* inner padding - key XORd with ipad */
73 	uchar_t k_opad[65];	/* outer padding - key XORd with opad */
74 	uchar_t tk[16];
75 	int i;
76 
77 	/* if key is longer than 64 bytes reset it to key=MD5(key) */
78 	if (key_len > 64) {
79 		MD5_CTX tctx;
80 
81 		MD5Init(&tctx);
82 		MD5Update(&tctx, key, key_len);
83 		MD5Final(tk, &tctx);
84 
85 		key = tk;
86 		key_len = 16;
87 	}
88 
89 	/*
90 	 * the HMAC_MD5 transform looks like:
91 	 *
92 	 * MD5(K XOR opad, MD5(K XOR ipad, text))
93 	 *
94 	 * where K is an n byte key
95 	 * ipad is the byte 0x36 repeated 64 times
96 	 * opad is the byte 0x5c repeated 64 times
97 	 * and text is the data being protected
98 	 */
99 
100 	/* start out by storing key in pads */
101 	bzero(k_ipad, sizeof (k_ipad));
102 	bzero(k_opad, sizeof (k_opad));
103 	bcopy(key, k_ipad, key_len);
104 	bcopy(key, k_opad, key_len);
105 
106 	/* XOR key with ipad and opad values */
107 	for (i = 0; i < 64; i++) {
108 		k_ipad[i] ^= 0x36;
109 		k_opad[i] ^= 0x5c;
110 	}
111 	/*
112 	 * perform inner MD5
113 	 */
114 	MD5Init(&context);			/* init context for 1st */
115 						/* pass */
116 	MD5Update(&context, k_ipad, 64);	/* start with inner pad */
117 	MD5Update(&context, text, text_len);	/* then text of datagram */
118 	MD5Final(digest, &context);		/* finish up 1st pass */
119 	/*
120 	 * perform outer MD5
121 	 */
122 	MD5Init(&context);			/* init context for 2nd */
123 						/* pass */
124 	MD5Update(&context, k_opad, 64);	/* start with outer pad */
125 	MD5Update(&context, digest, 16);	/* then results of 1st */
126 						/* hash */
127 	MD5Final(digest, &context);		/* finish up 2nd pass */
128 }
129 
130 /*
131  * If inmp is non-NULL, and we need to abort, it will use the IP/SCTP
132  * info in initmp to send the abort. Otherwise, no abort will be sent.
133  * If errmp is non-NULL, a chain of unrecognized parameters will
134  * be created and returned via *errmp.
135  *
136  * Returns 1 if the parameters are OK (or there are no parameters), or
137  * 0 if not.
138  */
139 static int
140 validate_init_params(sctp_t *sctp, sctp_chunk_hdr_t *ch,
141     sctp_init_chunk_t *init, mblk_t *inmp, sctp_parm_hdr_t **want_cookie,
142     mblk_t **errmp, int *supp_af, uint_t *sctp_options)
143 {
144 	sctp_parm_hdr_t		*cph;
145 	sctp_init_chunk_t	*ic;
146 	ssize_t			remaining;
147 	uint16_t		serror = 0;
148 	char			*details = NULL;
149 	size_t			errlen = 0;
150 	boolean_t		got_cookie = B_FALSE;
151 	uint16_t		ptype;
152 
153 	if (sctp_options != NULL)
154 		*sctp_options = 0;
155 
156 	/* First validate stream parameters */
157 	if (init->sic_instr == 0 || init->sic_outstr == 0) {
158 		serror = SCTP_ERR_BAD_MANDPARM;
159 		dprint(1,
160 		    ("validate_init_params: bad sid, is=%d os=%d\n",
161 			htons(init->sic_instr), htons(init->sic_outstr)));
162 		goto abort;
163 	}
164 	if (ntohl(init->sic_inittag) == 0) {
165 		serror = SCTP_ERR_BAD_MANDPARM;
166 		dprint(1, ("validate_init_params: inittag = 0\n"));
167 		goto abort;
168 	}
169 
170 	remaining = ntohs(ch->sch_len) - sizeof (*ch);
171 	ic = (sctp_init_chunk_t *)(ch + 1);
172 	remaining -= sizeof (*ic);
173 	if (remaining < sizeof (*cph)) {
174 		/* Nothing to validate */
175 		if (want_cookie != NULL)
176 			goto cookie_abort;
177 		return (1);
178 	}
179 
180 	cph = (sctp_parm_hdr_t *)(ic + 1);
181 
182 	while (cph != NULL) {
183 		ptype = ntohs(cph->sph_type);
184 		switch (ptype) {
185 		case PARM_HBINFO:
186 		case PARM_UNRECOGNIZED:
187 		case PARM_ECN:
188 			/* just ignore them */
189 			break;
190 		case PARM_FORWARD_TSN:
191 			if (sctp_options != NULL)
192 				*sctp_options |= SCTP_PRSCTP_OPTION;
193 			break;
194 		case PARM_COOKIE:
195 			got_cookie = B_TRUE;
196 			if (want_cookie != NULL) {
197 				*want_cookie = cph;
198 			}
199 			break;
200 		case PARM_ADDR4:
201 			*supp_af |= PARM_SUPP_V4;
202 			break;
203 		case PARM_ADDR6:
204 			*supp_af |= PARM_SUPP_V6;
205 			break;
206 		case PARM_COOKIE_PRESERVE:
207 		case PARM_ADAPT_LAYER_IND:
208 			/* These are OK */
209 			break;
210 		case PARM_ADDR_HOST_NAME:
211 			/* Don't support this; abort the association */
212 			serror = SCTP_ERR_BAD_ADDR;
213 			details = (char *)cph;
214 			errlen = ntohs(cph->sph_len);
215 			dprint(1, ("sctp:validate_init_params: host addr\n"));
216 			goto abort;
217 		case PARM_SUPP_ADDRS: {
218 			/* Make sure we have a supported addr intersection */
219 			uint16_t *p, addrtype;
220 			int plen;
221 
222 			plen = ntohs(cph->sph_len);
223 			p = (uint16_t *)(cph + 1);
224 			while (plen > 0) {
225 				addrtype = ntohs(*p);
226 				switch (addrtype) {
227 				case PARM_ADDR6:
228 					*supp_af |= PARM_SUPP_V6;
229 					break;
230 				case PARM_ADDR4:
231 					*supp_af |= PARM_SUPP_V4;
232 					break;
233 				default:
234 					/*
235 					 * Do nothing, silently ignore hostname
236 					 * address.
237 					 */
238 					break;
239 				}
240 				p++;
241 				plen -= sizeof (*p);
242 			}
243 			break;
244 		}
245 		default:
246 			/* Unrecognized param; check the high order bits */
247 			if ((ptype & 0xc000) == 0xc000) {
248 				/*
249 				 * report unrecognized param, and
250 				 * keep processing
251 				 */
252 				if (errmp != NULL) {
253 					if (want_cookie != NULL) {
254 						*errmp = sctp_make_err(sctp,
255 						    PARM_UNRECOGNIZED,
256 						    (void *)cph,
257 						    ntohs(cph->sph_len));
258 					} else {
259 						sctp_add_unrec_parm(cph, errmp);
260 					}
261 				}
262 				break;
263 			}
264 			if (ptype & 0x4000) {
265 				/*
266 				 * Stop processing and drop; report
267 				 * unrecognized param
268 				 */
269 				serror = SCTP_ERR_UNREC_PARM;
270 				details = (char *)cph;
271 				errlen = ntohs(cph->sph_len);
272 				goto abort;
273 			}
274 			if (ptype & 0x8000) {
275 				/* skip and continue processing */
276 				break;
277 			}
278 
279 			/*
280 			 * 2 high bits are clear; stop processing and
281 			 * drop packet
282 			 */
283 			return (0);
284 		}
285 
286 		cph = sctp_next_parm(cph, &remaining);
287 	}
288 	/*
289 	 * Some sanity checks.  The following should not fail unless the
290 	 * other side is broken.
291 	 *
292 	 * 1. If this is a V4 endpoint but V4 address is not
293 	 * supported, abort.
294 	 * 2. If this is a V6 only endpoint but V6 address is
295 	 * not supported, abort.  This assumes that a V6
296 	 * endpoint can use both V4 and V6 addresses.
297 	 * We only care about supp_af when processing INIT, i.e want_cookie
298 	 * is NULL.
299 	 */
300 	if (want_cookie == NULL &&
301 	    ((sctp->sctp_family == AF_INET && !(*supp_af & PARM_SUPP_V4)) ||
302 	    (sctp->sctp_family == AF_INET6 && !(*supp_af & PARM_SUPP_V6) &&
303 	    sctp->sctp_connp->conn_ipv6_v6only))) {
304 		dprint(1, ("sctp:validate_init_params: supp addr\n"));
305 		serror = SCTP_ERR_BAD_ADDR;
306 		goto abort;
307 	}
308 
309 	if (want_cookie != NULL && !got_cookie) {
310 cookie_abort:
311 		dprint(1, ("validate_init_params: cookie absent\n"));
312 		sctp_send_abort(sctp, sctp_init2vtag(ch), SCTP_ERR_MISSING_PARM,
313 		    details, errlen, inmp, 0, B_FALSE);
314 		return (0);
315 	}
316 
317 	/* OK */
318 	return (1);
319 
320 abort:
321 	if (want_cookie != NULL)
322 		return (0);
323 
324 	sctp_send_abort(sctp, sctp_init2vtag(ch), serror, details,
325 	    errlen, inmp, 0, B_FALSE);
326 	return (0);
327 }
328 
329 /*
330  * Initialize params from the INIT and INIT-ACK when the assoc. is
331  * established.
332  */
333 boolean_t
334 sctp_initialize_params(sctp_t *sctp, sctp_init_chunk_t *init,
335     sctp_init_chunk_t *iack)
336 {
337 	/* Get initial TSN */
338 	sctp->sctp_ftsn = ntohl(init->sic_inittsn);
339 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
340 
341 	/* Serial number is initialized to the same value as the TSN */
342 	sctp->sctp_fcsn = sctp->sctp_lastacked;
343 
344 	/*
345 	 * Get verification tags; no byteordering is necessary, since
346 	 * verfication tags are never processed except for byte-by-byte
347 	 * comparisons.
348 	 */
349 	sctp->sctp_fvtag = init->sic_inittag;
350 	sctp->sctp_sctph->sh_verf = init->sic_inittag;
351 	sctp->sctp_sctph6->sh_verf = init->sic_inittag;
352 	sctp->sctp_lvtag = iack->sic_inittag;
353 
354 	/* Get the peer's rwnd */
355 	sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
356 
357 	/* Allocate the in/out-stream counters */
358 	sctp->sctp_num_ostr = iack->sic_outstr;
359 	sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
360 	    sctp->sctp_num_ostr, KM_NOSLEEP);
361 	if (sctp->sctp_ostrcntrs == NULL)
362 		return (B_FALSE);
363 
364 	sctp->sctp_num_istr = iack->sic_instr;
365 	sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
366 	    sctp->sctp_num_istr, KM_NOSLEEP);
367 	if (sctp->sctp_instr == NULL) {
368 		kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
369 		    sctp->sctp_num_ostr);
370 		sctp->sctp_ostrcntrs = NULL;
371 		return (B_FALSE);
372 	}
373 	return (B_TRUE);
374 }
375 
376 /*
377  * Copy the peer's original source address into addr. This relies on the
378  * following format (see sctp_send_initack() below):
379  * 	relative timestamp for the cookie (int64_t) +
380  * 	cookie lifetime (uint32_t) +
381  * 	local tie-tag (uint32_t) +  peer tie-tag (uint32_t) +
382  * 	Peer's original src ...
383  */
384 int
385 cl_sctp_cookie_paddr(sctp_chunk_hdr_t *ch, in6_addr_t *addr)
386 {
387 	uchar_t	*off;
388 
389 	ASSERT(addr != NULL);
390 
391 	if (ch->sch_id != CHUNK_COOKIE)
392 		return (EINVAL);
393 
394 	off = (uchar_t *)ch + sizeof (*ch) + sizeof (int64_t) +
395 	    sizeof (uint32_t) + sizeof (uint32_t) + sizeof (uint32_t);
396 
397 	bcopy(off, addr, sizeof (*addr));
398 
399 	return (0);
400 }
401 
402 #define	SCTP_CALC_COOKIE_LEN(initcp) \
403 	sizeof (int64_t) +		/* timestamp */			\
404 	sizeof (uint32_t) +		/* cookie lifetime */		\
405 	sizeof (sctp_init_chunk_t) +	/* INIT ACK */			\
406 	sizeof (in6_addr_t) +		/* peer's original source */ 	\
407 	ntohs((initcp)->sch_len) +	/* peer's INIT */		\
408 	sizeof (uint32_t) +		/* local tie-tag */		\
409 	sizeof (uint32_t) +		/* peer tie-tag */		\
410 	sizeof (sctp_parm_hdr_t) +	/* param header */		\
411 	16				/* MD5 hash */
412 
413 void
414 sctp_send_initack(sctp_t *sctp, sctp_hdr_t *initsh, sctp_chunk_hdr_t *ch,
415     mblk_t *initmp)
416 {
417 	ipha_t			*initiph;
418 	ip6_t			*initip6h;
419 	ipha_t			*iackiph;
420 	ip6_t			*iackip6h;
421 	sctp_chunk_hdr_t	*iack_ch;
422 	sctp_init_chunk_t	*iack;
423 	sctp_init_chunk_t	*init;
424 	sctp_hdr_t		*iacksh;
425 	size_t			cookielen;
426 	size_t			iacklen;
427 	size_t			ipsctplen;
428 	size_t			errlen = 0;
429 	sctp_parm_hdr_t		*cookieph;
430 	mblk_t			*iackmp;
431 	uint32_t		itag;
432 	uint32_t		itsn;
433 	int64_t			*now;
434 	int64_t			nowt;
435 	uint32_t		*lifetime;
436 	char			*p;
437 	boolean_t		 isv4;
438 	int			supp_af = 0;
439 	uint_t			sctp_options;
440 	uint32_t		*ttag;
441 	int			pad;
442 	mblk_t			*errmp = NULL;
443 	boolean_t		initcollision = B_FALSE;
444 	boolean_t		linklocal = B_FALSE;
445 	cred_t			*cr;
446 	ts_label_t		*initlabel;
447 
448 	BUMP_LOCAL(sctp->sctp_ibchunks);
449 	isv4 = (IPH_HDR_VERSION(initmp->b_rptr) == IPV4_VERSION);
450 
451 	/* Extract the INIT chunk */
452 	if (isv4) {
453 		initiph = (ipha_t *)initmp->b_rptr;
454 		ipsctplen = sctp->sctp_ip_hdr_len;
455 		supp_af |= PARM_SUPP_V4;
456 	} else {
457 		initip6h = (ip6_t *)initmp->b_rptr;
458 		ipsctplen = sctp->sctp_ip_hdr6_len;
459 		if (IN6_IS_ADDR_LINKLOCAL(&initip6h->ip6_src))
460 			linklocal = B_TRUE;
461 		supp_af |= PARM_SUPP_V6;
462 	}
463 	ASSERT(OK_32PTR(initsh));
464 	init = (sctp_init_chunk_t *)((char *)(initsh + 1) + sizeof (*iack_ch));
465 
466 	/* Make sure we like the peer's parameters */
467 	if (validate_init_params(sctp, ch, init, initmp, NULL, &errmp,
468 		&supp_af, &sctp_options) == 0) {
469 		return;
470 	}
471 	if (errmp != NULL)
472 		errlen = msgdsize(errmp);
473 	if (sctp->sctp_family == AF_INET) {
474 		/*
475 		 * Irregardless of the supported address in the INIT, v4
476 		 * must be supported.
477 		 */
478 		supp_af = PARM_SUPP_V4;
479 	}
480 	if (sctp->sctp_state <= SCTPS_LISTEN) {
481 		/* normal, expected INIT: generate new vtag and itsn */
482 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
483 		if (itag == 0)
484 			itag = (uint32_t)gethrtime();
485 		itsn = itag + 1;
486 		itag = htonl(itag);
487 	} else if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
488 	    sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
489 		/* init collision; copy vtag and itsn from sctp */
490 		itag = sctp->sctp_lvtag;
491 		itsn = sctp->sctp_ltsn;
492 		/*
493 		 * In addition we need to send all the params that was sent
494 		 * in our INIT chunk. Essentially, it is only the supported
495 		 * address params that we need to add.
496 		 */
497 		initcollision = B_TRUE;
498 		/*
499 		 * When we sent the INIT, we should have set linklocal in
500 		 * the sctp which should be good enough.
501 		 */
502 		if (linklocal)
503 			linklocal = B_FALSE;
504 	} else {
505 		/* peer restart; generate new vtag but keep everything else */
506 		(void) random_get_pseudo_bytes((uint8_t *)&itag, sizeof (itag));
507 		if (itag == 0)
508 			itag = (uint32_t)gethrtime();
509 		itag = htonl(itag);
510 		itsn = sctp->sctp_ltsn;
511 	}
512 
513 	/*
514 	 * Allocate a mblk for the INIT ACK, consisting of the link layer
515 	 * header, the IP header, the SCTP common header, and INIT ACK chunk,
516 	 * and finally the COOKIE parameter.
517 	 */
518 	cookielen = SCTP_CALC_COOKIE_LEN(ch);
519 	iacklen = sizeof (*iack_ch) + sizeof (*iack) + cookielen;
520 	if (sctp->sctp_send_adaption)
521 		iacklen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
522 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
523 	    sctp->sctp_prsctp_aware && sctp_prsctp_enabled) {
524 		iacklen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);
525 	}
526 	if (initcollision)
527 		iacklen += sctp_supaddr_param_len(sctp);
528 	if (!linklocal)
529 		iacklen += sctp_addr_params_len(sctp, supp_af, B_FALSE);
530 	ipsctplen += sizeof (*iacksh) + iacklen;
531 	iacklen += errlen;
532 	if ((pad = ipsctplen % 4) != 0) {
533 		pad = 4 - pad;
534 		ipsctplen += pad;
535 	}
536 
537 	/*
538 	 * If the listen socket is bound to a trusted extensions
539 	 * multi-label port, attach a copy of the listener's cred
540 	 * to the new INITACK mblk. Modify the cred to contain
541 	 * the security label of the received INIT packet.
542 	 * If not a multi-label port, attach the unmodified
543 	 * listener's cred directly.
544 	 *
545 	 * We expect Sun developed kernel modules to properly set
546 	 * cred labels for sctp connections. We can't be so sure this
547 	 * will be done correctly when 3rd party kernel modules
548 	 * directly use sctp. The initlabel panic guard logic was
549 	 * added to cover this possibility.
550 	 */
551 	if (sctp->sctp_connp->conn_mlp_type != mlptSingle) {
552 		initlabel = MBLK_GETLABEL(initmp);
553 		if (initlabel == NULL) {
554 			sctp_send_abort(sctp, sctp_init2vtag(ch),
555 			    SCTP_ERR_UNKNOWN, NULL, 0, initmp, 0, B_FALSE);
556 			return;
557 		}
558 		cr = copycred_from_bslabel(CONN_CRED(sctp->sctp_connp),
559 		    &initlabel->tsl_label, initlabel->tsl_doi, KM_NOSLEEP);
560 		if (cr == NULL) {
561 			sctp_send_abort(sctp, sctp_init2vtag(ch),
562 			    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE);
563 			return;
564 		}
565 		iackmp = allocb_cred(ipsctplen + sctp_wroff_xtra, cr);
566 		crfree(cr);
567 	} else {
568 		iackmp = allocb_cred(ipsctplen + sctp_wroff_xtra,
569 		    CONN_CRED(sctp->sctp_connp));
570 	}
571 	if (iackmp == NULL) {
572 		sctp_send_abort(sctp, sctp_init2vtag(ch),
573 		    SCTP_ERR_NO_RESOURCES, NULL, 0, initmp, 0, B_FALSE);
574 		return;
575 	}
576 
577 	/* Copy in the [imcomplete] IP/SCTP composite header */
578 	p = (char *)(iackmp->b_rptr + sctp_wroff_xtra);
579 	iackmp->b_rptr = (uchar_t *)p;
580 	if (isv4) {
581 		bcopy(sctp->sctp_iphc, p, sctp->sctp_hdr_len);
582 		iackiph = (ipha_t *)p;
583 
584 		/* Copy the peer's IP addr */
585 		iackiph->ipha_dst = initiph->ipha_src;
586 		iackiph->ipha_src = initiph->ipha_dst;
587 		iackiph->ipha_length = htons(ipsctplen + errlen);
588 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr_len);
589 	} else {
590 		bcopy(sctp->sctp_iphc6, p, sctp->sctp_hdr6_len);
591 		iackip6h = (ip6_t *)p;
592 
593 		/* Copy the peer's IP addr */
594 		iackip6h->ip6_dst = initip6h->ip6_src;
595 		iackip6h->ip6_src = initip6h->ip6_dst;
596 		iackip6h->ip6_plen = htons(ipsctplen - sizeof (*iackip6h) +
597 		    errlen);
598 		iacksh = (sctp_hdr_t *)(p + sctp->sctp_ip_hdr6_len);
599 	}
600 	ASSERT(OK_32PTR(iacksh));
601 
602 	/* Fill in the holes in the SCTP common header */
603 	iacksh->sh_sport = initsh->sh_dport;
604 	iacksh->sh_dport = initsh->sh_sport;
605 	iacksh->sh_verf = init->sic_inittag;
606 
607 	/* INIT ACK chunk header */
608 	iack_ch = (sctp_chunk_hdr_t *)(iacksh + 1);
609 	iack_ch->sch_id = CHUNK_INIT_ACK;
610 	iack_ch->sch_flags = 0;
611 	iack_ch->sch_len = htons(iacklen);
612 
613 	/* The INIT ACK itself */
614 	iack = (sctp_init_chunk_t *)(iack_ch + 1);
615 	iack->sic_inittag = itag;	/* already in network byteorder */
616 	iack->sic_inittsn = htonl(itsn);
617 
618 	iack->sic_a_rwnd = htonl(sctp->sctp_rwnd);
619 	/* Advertise what we would want to have as stream #'s */
620 	iack->sic_outstr = htons(MIN(sctp->sctp_num_ostr,
621 	    ntohs(init->sic_instr)));
622 	iack->sic_instr = htons(sctp->sctp_num_istr);
623 
624 	p = (char *)(iack + 1);
625 	p += sctp_adaption_code_param(sctp, (uchar_t *)p);
626 	if (initcollision)
627 		p += sctp_supaddr_param(sctp, (uchar_t *)p);
628 	if (!linklocal)
629 		p += sctp_addr_params(sctp, supp_af, (uchar_t *)p);
630 	if (((sctp_options & SCTP_PRSCTP_OPTION) || initcollision) &&
631 	    sctp->sctp_prsctp_aware && sctp_prsctp_enabled) {
632 		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);
633 	}
634 	/*
635 	 * Generate and lay in the COOKIE parameter.
636 	 *
637 	 * Any change here that results in a change of location for
638 	 * the peer's orig source address must be propagated to the fn
639 	 * cl_sctp_cookie_paddr() above.
640 	 *
641 	 * The cookie consists of:
642 	 * 1. The relative timestamp for the cookie (lbolt64)
643 	 * 2. The cookie lifetime (uint32_t) in tick
644 	 * 3. The local tie-tag
645 	 * 4. The peer tie-tag
646 	 * 5. Peer's original src, used to confirm the validity of address.
647 	 * 6. Our INIT ACK chunk, less any parameters
648 	 * 7. The INIT chunk (may contain parameters)
649 	 * 8. 128-bit MD5 signature.
650 	 *
651 	 * Since the timestamp values will only be evaluated locally, we
652 	 * don't need to worry about byte-ordering them.
653 	 */
654 	cookieph = (sctp_parm_hdr_t *)p;
655 	cookieph->sph_type = htons(PARM_COOKIE);
656 	cookieph->sph_len = htons(cookielen);
657 
658 	/* timestamp */
659 	now = (int64_t *)(cookieph + 1);
660 	nowt = lbolt64;
661 	bcopy(&nowt, now, sizeof (*now));
662 
663 	/* cookie lifetime -- need configuration */
664 	lifetime = (uint32_t *)(now + 1);
665 	*lifetime = sctp->sctp_cookie_lifetime;
666 
667 	/* Set the tie-tags */
668 	ttag = (uint32_t *)(lifetime + 1);
669 	if (sctp->sctp_state <= SCTPS_COOKIE_WAIT) {
670 		*ttag = 0;
671 		ttag++;
672 		*ttag = 0;
673 		ttag++;
674 	} else {
675 		/* local tie-tag (network byte-order) */
676 		*ttag = sctp->sctp_lvtag;
677 		ttag++;
678 		/* peer tie-tag (network byte-order) */
679 		*ttag = sctp->sctp_fvtag;
680 		ttag++;
681 	}
682 	/*
683 	 * Copy in peer's original source address so that we can confirm
684 	 * the reachability later.
685 	 */
686 	p = (char *)ttag;
687 	if (isv4) {
688 		in6_addr_t peer_addr;
689 
690 		IN6_IPADDR_TO_V4MAPPED(iackiph->ipha_dst, &peer_addr);
691 		bcopy(&peer_addr, p, sizeof (in6_addr_t));
692 	} else {
693 		bcopy(&iackip6h->ip6_dst, p, sizeof (in6_addr_t));
694 	}
695 	p += sizeof (in6_addr_t);
696 	/* Copy in our INIT ACK chunk */
697 	bcopy(iack, p, sizeof (*iack));
698 	iack = (sctp_init_chunk_t *)p;
699 	/* Set the # of streams we'll end up using */
700 	iack->sic_outstr = MIN(sctp->sctp_num_ostr, ntohs(init->sic_instr));
701 	iack->sic_instr = MIN(sctp->sctp_num_istr, ntohs(init->sic_outstr));
702 	p += sizeof (*iack);
703 
704 	/* Copy in the peer's INIT chunk */
705 	bcopy(ch, p, ntohs(ch->sch_len));
706 	p += ntohs(ch->sch_len);
707 
708 	/*
709 	 * Calculate the HMAC ICV into the digest slot in buf.
710 	 * First, generate a new secret if the current secret is
711 	 * older than the new secret lifetime parameter permits,
712 	 * copying the current secret to sctp_old_secret.
713 	 */
714 	if (sctp_new_secret_interval > 0 &&
715 	    (sctp->sctp_last_secret_update +
716 	    MSEC_TO_TICK(sctp_new_secret_interval)) <= nowt) {
717 		bcopy(sctp->sctp_secret, sctp->sctp_old_secret,
718 		    SCTP_SECRET_LEN);
719 		(void) random_get_pseudo_bytes(sctp->sctp_secret,
720 		    SCTP_SECRET_LEN);
721 		sctp->sctp_last_secret_update = nowt;
722 	}
723 
724 	hmac_md5((uchar_t *)now, cookielen - sizeof (*cookieph) - 16,
725 	    (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN, (uchar_t *)p);
726 
727 	iackmp->b_wptr = iackmp->b_rptr + ipsctplen;
728 	iackmp->b_cont = errmp;		/*  OK if NULL */
729 
730 	if (is_system_labeled() && (cr = DB_CRED(iackmp)) != NULL &&
731 	    crgetlabel(cr) != NULL) {
732 		conn_t *connp = sctp->sctp_connp;
733 		int err, adjust;
734 
735 		if (isv4)
736 			err = tsol_check_label(cr, &iackmp, &adjust,
737 			    connp->conn_mac_exempt);
738 		else
739 			err = tsol_check_label_v6(cr, &iackmp, &adjust,
740 			    connp->conn_mac_exempt);
741 		if (err != 0) {
742 			sctp_send_abort(sctp, sctp_init2vtag(ch),
743 			    SCTP_ERR_AUTH_ERR, NULL, 0, initmp, 0, B_FALSE);
744 			freemsg(iackmp);
745 			return;
746 		}
747 		if (isv4) {
748 			iackiph = (ipha_t *)iackmp->b_rptr;
749 			adjust += ntohs(iackiph->ipha_length);
750 			iackiph->ipha_length = htons(adjust);
751 		}
752 	}
753 
754 	/*
755 	 * Stash the conn ptr info. for IP only as e don't have any
756 	 * cached IRE.
757 	 */
758 	SCTP_STASH_IPINFO(iackmp, (ire_t *)NULL);
759 
760 	/* XXX sctp == sctp_g_q, so using its obchunks is valid */
761 	BUMP_LOCAL(sctp->sctp_opkts);
762 	BUMP_LOCAL(sctp->sctp_obchunks);
763 
764 	/* OK to call IP_PUT() here instead of sctp_add_sendq(). */
765 	CONN_INC_REF(sctp->sctp_connp);
766 	iackmp->b_flag |= MSGHASREF;
767 	IP_PUT(iackmp, sctp->sctp_connp, isv4);
768 }
769 
770 void
771 sctp_send_cookie_ack(sctp_t *sctp)
772 {
773 	sctp_chunk_hdr_t *cach;
774 	mblk_t *camp;
775 
776 	camp = sctp_make_mp(sctp, NULL, sizeof (*cach));
777 	if (camp == NULL) {
778 		/* XXX should abort, but don't have the inmp anymore */
779 		SCTP_KSTAT(sctp_send_cookie_ack_failed);
780 		return;
781 	}
782 
783 	cach = (sctp_chunk_hdr_t *)camp->b_wptr;
784 	camp->b_wptr = (uchar_t *)(cach + 1);
785 	cach->sch_id = CHUNK_COOKIE_ACK;
786 	cach->sch_flags = 0;
787 	cach->sch_len = htons(sizeof (*cach));
788 
789 	sctp_set_iplen(sctp, camp);
790 
791 	BUMP_LOCAL(sctp->sctp_obchunks);
792 
793 	sctp_add_sendq(sctp, camp);
794 }
795 
796 static int
797 sctp_find_al_ind(sctp_parm_hdr_t *sph, ssize_t len, uint32_t *adaption_code)
798 {
799 
800 	if (len < sizeof (*sph))
801 		return (-1);
802 	while (sph != NULL) {
803 		if (sph->sph_type == htons(PARM_ADAPT_LAYER_IND) &&
804 		    ntohs(sph->sph_len) >= (sizeof (*sph) +
805 			sizeof (uint32_t))) {
806 			*adaption_code = *(uint32_t *)(sph + 1);
807 			return (0);
808 		}
809 		sph = sctp_next_parm(sph, &len);
810 	}
811 	return (-1);
812 }
813 
814 void
815 sctp_send_cookie_echo(sctp_t *sctp, sctp_chunk_hdr_t *iackch, mblk_t *iackmp)
816 {
817 	mblk_t			*cemp;
818 	mblk_t			*mp = NULL;
819 	mblk_t			*head;
820 	mblk_t			*meta;
821 	sctp_faddr_t		*fp;
822 	sctp_chunk_hdr_t	*cech;
823 	sctp_init_chunk_t	 *iack;
824 	int32_t			cansend;
825 	int32_t			seglen;
826 	size_t			ceclen;
827 	sctp_parm_hdr_t		*cph;
828 	sctp_data_hdr_t		*sdc;
829 	sctp_tf_t		*tf;
830 	int			pad = 0;
831 	int			hdrlen;
832 	mblk_t			*errmp = NULL;
833 	uint_t			sctp_options;
834 	int			error;
835 	uint16_t		old_num_str;
836 
837 	iack = (sctp_init_chunk_t *)(iackch + 1);
838 
839 	cph = NULL;
840 	if (validate_init_params(sctp, iackch, iack, iackmp, &cph, &errmp,
841 	    &pad, &sctp_options) == 0) { /* result in 'pad' ignored */
842 		BUMP_MIB(&sctp_mib, sctpAborted);
843 		sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, NULL);
844 		sctp_clean_death(sctp, ECONNABORTED);
845 		return;
846 	}
847 	ASSERT(cph != NULL);
848 
849 	ASSERT(sctp->sctp_cookie_mp == NULL);
850 
851 	/* Got a cookie to echo back; allocate an mblk */
852 	ceclen = sizeof (*cech) + ntohs(cph->sph_len) - sizeof (*cph);
853 	if ((pad = ceclen & (SCTP_ALIGN - 1)) != 0)
854 		pad = SCTP_ALIGN - pad;
855 
856 	if (IPH_HDR_VERSION(iackmp->b_rptr) == IPV4_VERSION)
857 		hdrlen = sctp->sctp_hdr_len;
858 	else
859 		hdrlen = sctp->sctp_hdr6_len;
860 
861 	cemp = allocb(sctp_wroff_xtra + hdrlen + ceclen + pad, BPRI_MED);
862 	if (cemp == NULL) {
863 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
864 		    sctp->sctp_current->rto);
865 		if (errmp != NULL)
866 			freeb(errmp);
867 		return;
868 	}
869 	cemp->b_rptr += (sctp_wroff_xtra + hdrlen);
870 
871 	/* Process the INIT ACK */
872 	sctp->sctp_sctph->sh_verf = iack->sic_inittag;
873 	sctp->sctp_sctph6->sh_verf = iack->sic_inittag;
874 	sctp->sctp_fvtag = iack->sic_inittag;
875 	sctp->sctp_ftsn = ntohl(iack->sic_inittsn);
876 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
877 	sctp->sctp_fcsn = sctp->sctp_lastacked;
878 	sctp->sctp_frwnd = ntohl(iack->sic_a_rwnd);
879 
880 	/*
881 	 * Populate sctp with addresses given in the INIT ACK or IP header.
882 	 * Need to set the df bit in the current fp as it has been cleared
883 	 * in sctp_connect().
884 	 */
885 	sctp->sctp_current->df = B_TRUE;
886 	/*
887 	 * Since IP uses this info during the fanout process, we need to hold
888 	 * the lock for this hash line while performing this operation.
889 	 */
890 	/* XXX sctp_conn_fanout + SCTP_CONN_HASH(sctp->sctp_ports); */
891 	ASSERT(sctp->sctp_conn_tfp != NULL);
892 	tf = sctp->sctp_conn_tfp;
893 	/* sctp isn't a listener so only need to hold conn fanout lock */
894 	mutex_enter(&tf->tf_lock);
895 	if (sctp_get_addrparams(sctp, NULL, iackmp, iackch, NULL) != 0) {
896 		mutex_exit(&tf->tf_lock);
897 		freeb(cemp);
898 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
899 		    sctp->sctp_current->rto);
900 		if (errmp != NULL)
901 			freeb(errmp);
902 		return;
903 	}
904 	mutex_exit(&tf->tf_lock);
905 
906 	fp = sctp->sctp_current;
907 
908 	/*
909 	 * There could be a case when we get an INIT-ACK again, if the INIT
910 	 * is re-transmitted, for e.g., which means we would have already
911 	 * allocated this resource earlier (also for sctp_instr). In this
912 	 * case we check and re-allocate, if necessary.
913 	 */
914 	old_num_str = sctp->sctp_num_ostr;
915 	if (ntohs(iack->sic_instr) < sctp->sctp_num_ostr)
916 		sctp->sctp_num_ostr = ntohs(iack->sic_instr);
917 	if (sctp->sctp_ostrcntrs == NULL) {
918 		sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
919 		    sctp->sctp_num_ostr, KM_NOSLEEP);
920 	} else {
921 		ASSERT(old_num_str > 0);
922 		if (old_num_str != sctp->sctp_num_ostr) {
923 			kmem_free(sctp->sctp_ostrcntrs, sizeof (uint16_t) *
924 			    old_num_str);
925 			sctp->sctp_ostrcntrs = kmem_zalloc(sizeof (uint16_t) *
926 			    sctp->sctp_num_ostr, KM_NOSLEEP);
927 		}
928 	}
929 	if (sctp->sctp_ostrcntrs == NULL) {
930 		freeb(cemp);
931 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
932 		if (errmp != NULL)
933 			freeb(errmp);
934 		return;
935 	}
936 
937 	/*
938 	 * Allocate the in stream tracking array. Comments for sctp_ostrcntrs
939 	 * hold here too.
940 	 */
941 	old_num_str = sctp->sctp_num_istr;
942 	if (ntohs(iack->sic_outstr) < sctp->sctp_num_istr)
943 		sctp->sctp_num_istr = ntohs(iack->sic_outstr);
944 	if (sctp->sctp_instr == NULL) {
945 		sctp->sctp_instr = kmem_zalloc(sizeof (*sctp->sctp_instr) *
946 		    sctp->sctp_num_istr, KM_NOSLEEP);
947 	} else {
948 		ASSERT(old_num_str > 0);
949 		if (old_num_str != sctp->sctp_num_istr) {
950 			kmem_free(sctp->sctp_instr,
951 			    sizeof (*sctp->sctp_instr) * old_num_str);
952 			sctp->sctp_instr = kmem_zalloc(
953 			    sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr,
954 			    KM_NOSLEEP);
955 		}
956 	}
957 	if (sctp->sctp_instr == NULL) {
958 		kmem_free(sctp->sctp_ostrcntrs,
959 		    sizeof (uint16_t) * sctp->sctp_num_ostr);
960 		freeb(cemp);
961 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
962 		if (errmp != NULL)
963 			freeb(errmp);
964 		return;
965 	}
966 
967 	if (!(sctp_options & SCTP_PRSCTP_OPTION) && sctp->sctp_prsctp_aware)
968 		sctp->sctp_prsctp_aware = B_FALSE;
969 
970 	if (sctp_find_al_ind((sctp_parm_hdr_t *)(iack + 1),
971 		ntohs(iackch->sch_len) - (sizeof (*iackch) + sizeof (*iack)),
972 		&sctp->sctp_rx_adaption_code) == 0) {
973 		sctp->sctp_recv_adaption = 1;
974 	}
975 
976 	cech = (sctp_chunk_hdr_t *)cemp->b_rptr;
977 	ASSERT(OK_32PTR(cech));
978 	cech->sch_id = CHUNK_COOKIE;
979 	cech->sch_flags = 0;
980 	cech->sch_len = htons(ceclen);
981 
982 	/* Copy the cookie (less the parm hdr) to the chunk */
983 	bcopy(cph + 1, cech + 1, ceclen - sizeof (*cph));
984 
985 	cemp->b_wptr = cemp->b_rptr + ceclen;
986 
987 	if (sctp->sctp_unsent > 0) {
988 		sctp_msg_hdr_t	*smh;
989 		mblk_t		*prev = NULL;
990 		uint32_t	unsent = 0;
991 
992 		mp = sctp->sctp_xmit_unsent;
993 		do {
994 			smh = (sctp_msg_hdr_t *)mp->b_rptr;
995 			if (smh->smh_sid >= sctp->sctp_num_ostr) {
996 				unsent += smh->smh_msglen;
997 				if (prev != NULL)
998 					prev->b_next = mp->b_next;
999 				else
1000 					sctp->sctp_xmit_unsent = mp->b_next;
1001 				mp->b_next = NULL;
1002 				sctp_sendfail_event(sctp, mp, SCTP_ERR_BAD_SID,
1003 				    B_FALSE);
1004 				if (prev != NULL)
1005 					mp = prev->b_next;
1006 				else
1007 					mp = sctp->sctp_xmit_unsent;
1008 			} else {
1009 				prev = mp;
1010 				mp = mp->b_next;
1011 			}
1012 		} while (mp != NULL);
1013 		if (unsent > 0) {
1014 			ASSERT(sctp->sctp_unsent >= unsent);
1015 			sctp->sctp_unsent -= unsent;
1016 			/*
1017 			 * Update ULP the amount of queued data, which is
1018 			 * sent-unack'ed + unsent.
1019 			 * This is not necessary, but doesn't harm, we
1020 			 * just use unsent instead of sent-unack'ed +
1021 			 * unsent, since there won't be any sent-unack'ed
1022 			 * here.
1023 			 */
1024 			if (!SCTP_IS_DETACHED(sctp)) {
1025 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
1026 				    sctp->sctp_unsent);
1027 			}
1028 		}
1029 		if (sctp->sctp_xmit_unsent == NULL)
1030 			sctp->sctp_xmit_unsent_tail = NULL;
1031 	}
1032 	ceclen += pad;
1033 	cansend = MIN(sctp->sctp_unsent, sctp->sctp_frwnd);
1034 	meta = sctp_get_msg_to_send(sctp, &mp, NULL, &error, ceclen,
1035 	    cansend,  NULL);
1036 	/*
1037 	 * The error cannot be anything else since we could have an non-zero
1038 	 * error only if sctp_get_msg_to_send() tries to send a Forward
1039 	 * TSN which will not happen here.
1040 	 */
1041 	ASSERT(error == 0);
1042 	if (meta == NULL)
1043 		goto sendcookie;
1044 	sctp->sctp_xmit_tail = meta;
1045 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
1046 	seglen = ntohs(sdc->sdh_len);
1047 	if ((ceclen + seglen) > fp->sfa_pmss ||
1048 	    (seglen - sizeof (*sdc)) > cansend) {
1049 		goto sendcookie;
1050 	}
1051 	/* OK, if this fails */
1052 	cemp->b_cont = dupmsg(mp);
1053 sendcookie:
1054 	head = sctp_add_proto_hdr(sctp, fp, cemp, 0, NULL);
1055 	if (head == NULL) {
1056 		freemsg(cemp);
1057 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1058 		if (errmp != NULL)
1059 			freeb(errmp);
1060 		SCTP_KSTAT(sctp_send_cookie_failed);
1061 		return;
1062 	}
1063 	/*
1064 	 * Even if cookie-echo exceeds MTU for one of the hops, it'll
1065 	 * have a chance of getting there.
1066 	 */
1067 	if (fp->isv4) {
1068 		ipha_t *iph = (ipha_t *)head->b_rptr;
1069 		iph->ipha_fragment_offset_and_flags = 0;
1070 	}
1071 	BUMP_LOCAL(sctp->sctp_obchunks);
1072 
1073 	sctp->sctp_cookie_mp = dupmsg(head);
1074 	/* Don't bundle, we will just resend init if this cookie is lost. */
1075 	if (sctp->sctp_cookie_mp == NULL) {
1076 		if (cemp->b_cont != NULL) {
1077 			freemsg(cemp->b_cont);
1078 			cemp->b_cont = NULL;
1079 		}
1080 	} else if (cemp->b_cont != NULL) {
1081 		ASSERT(mp != NULL && mp == meta->b_cont);
1082 		SCTP_CHUNK_CLEAR_FLAGS(cemp->b_cont);
1083 		cemp->b_wptr += pad;
1084 		seglen -= sizeof (*sdc);
1085 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, seglen, meta);
1086 	}
1087 	if (errmp != NULL)
1088 		linkb(head, errmp);
1089 	sctp->sctp_state = SCTPS_COOKIE_ECHOED;
1090 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1091 
1092 	sctp_set_iplen(sctp, head);
1093 	sctp_add_sendq(sctp, head);
1094 }
1095 
1096 int
1097 sctp_process_cookie(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *cmp,
1098     sctp_init_chunk_t **iackpp, sctp_hdr_t *insctph, int *recv_adaption,
1099     in6_addr_t *peer_addr)
1100 {
1101 	int32_t			clen;
1102 	size_t			initplen;
1103 	uchar_t			*p;
1104 	uchar_t			*given_hash;
1105 	uchar_t			needed_hash[16];
1106 	int64_t			ts;
1107 	int64_t			diff;
1108 	uint32_t		*lt;
1109 	sctp_init_chunk_t	*iack;
1110 	sctp_chunk_hdr_t	*initch;
1111 	sctp_init_chunk_t	*init;
1112 	uint32_t		*lttag;
1113 	uint32_t		*fttag;
1114 	uint32_t		ports;
1115 
1116 	BUMP_LOCAL(sctp->sctp_ibchunks);
1117 	/* Verify the ICV */
1118 	clen = ntohs(ch->sch_len) - sizeof (*ch) - 16;
1119 	if (clen < 0) {
1120 		dprint(1, ("invalid cookie chunk length %d\n",
1121 		    ntohs(ch->sch_len)));
1122 
1123 		return (-1);
1124 	}
1125 	p = (uchar_t *)(ch + 1);
1126 
1127 	hmac_md5(p, clen, (uchar_t *)sctp->sctp_secret, SCTP_SECRET_LEN,
1128 	    needed_hash);
1129 
1130 	/* The given hash follows the cookie data */
1131 	given_hash = p + clen;
1132 
1133 	if (bcmp(given_hash, needed_hash, 16) != 0) {
1134 		/* The secret may have changed; try the old secret */
1135 		hmac_md5(p, clen, (uchar_t *)sctp->sctp_old_secret,
1136 		    SCTP_SECRET_LEN, needed_hash);
1137 		if (bcmp(given_hash, needed_hash, 16) != 0) {
1138 			return (-1);
1139 		}
1140 	}
1141 
1142 	/* Timestamp is int64_t, and we only guarantee 32-bit alignment */
1143 	bcopy(p, &ts, sizeof (ts));
1144 	/* Cookie life time, int32_t */
1145 	lt = (uint32_t *)(p + sizeof (ts));
1146 
1147 	/*
1148 	 * To quote PRC, "this is our baby", so let's continue.
1149 	 * We need to pull out the encapsulated INIT ACK and
1150 	 * INIT chunks. Note that we don't process these until
1151 	 * we have verified the timestamp, but we need them before
1152 	 * processing the timestamp since if the time check fails,
1153 	 * we need to get the verification tag from the INIT in order
1154 	 * to send a stale cookie error.
1155 	 */
1156 	lttag = (uint32_t *)(lt + 1);
1157 	fttag = lttag + 1;
1158 	if (peer_addr != NULL)
1159 		bcopy(fttag + 1, peer_addr, sizeof (in6_addr_t));
1160 	iack = (sctp_init_chunk_t *)((char *)(fttag + 1) + sizeof (in6_addr_t));
1161 	initch = (sctp_chunk_hdr_t *)(iack + 1);
1162 	init = (sctp_init_chunk_t *)(initch + 1);
1163 	initplen = ntohs(initch->sch_len) - (sizeof (*init) + sizeof (*initch));
1164 	*iackpp = iack;
1165 	*recv_adaption = 0;
1166 
1167 	/* Check the timestamp */
1168 	diff = lbolt64 - ts;
1169 	if (diff > *lt && (init->sic_inittag != sctp->sctp_fvtag ||
1170 	    iack->sic_inittag != sctp->sctp_lvtag)) {
1171 
1172 		uint32_t staleness;
1173 
1174 		staleness = TICK_TO_USEC(diff);
1175 		staleness = htonl(staleness);
1176 		sctp_send_abort(sctp, init->sic_inittag, SCTP_ERR_STALE_COOKIE,
1177 		    (char *)&staleness, sizeof (staleness), cmp, 1, B_FALSE);
1178 
1179 		dprint(1, ("stale cookie %d\n", staleness));
1180 
1181 		return (-1);
1182 	}
1183 
1184 	/* Check for attack by adding addresses to a restart */
1185 	bcopy(insctph, &ports, sizeof (ports));
1186 	if (sctp_secure_restart_check(cmp, initch, ports, KM_NOSLEEP) != 1) {
1187 		return (-1);
1188 	}
1189 
1190 	/* Look for adaptation code if there any parms in the INIT chunk */
1191 	if ((initplen >= sizeof (sctp_parm_hdr_t)) &&
1192 	    (sctp_find_al_ind((sctp_parm_hdr_t *)(init + 1), initplen,
1193 	    &sctp->sctp_rx_adaption_code) == 0)) {
1194 		*recv_adaption = 1;
1195 	}
1196 
1197 	/* Examine tie-tags */
1198 
1199 	if (sctp->sctp_state >= SCTPS_COOKIE_WAIT) {
1200 		if (sctp->sctp_state == SCTPS_ESTABLISHED &&
1201 		    init->sic_inittag == sctp->sctp_fvtag &&
1202 		    iack->sic_inittag == sctp->sctp_lvtag &&
1203 		    *fttag == 0 && *lttag == 0) {
1204 
1205 			dprint(1, ("duplicate cookie from %x:%x:%x:%x (%d)\n",
1206 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
1207 			    (int)(sctp->sctp_fport)));
1208 			return (-1);
1209 		}
1210 
1211 		if (init->sic_inittag != sctp->sctp_fvtag &&
1212 		    iack->sic_inittag != sctp->sctp_lvtag &&
1213 		    *fttag == sctp->sctp_fvtag &&
1214 		    *lttag == sctp->sctp_lvtag) {
1215 			int i;
1216 
1217 			/* Section 5.2.4 case A: restart */
1218 			sctp->sctp_fvtag = init->sic_inittag;
1219 			sctp->sctp_lvtag = iack->sic_inittag;
1220 
1221 			sctp->sctp_sctph->sh_verf = init->sic_inittag;
1222 			sctp->sctp_sctph6->sh_verf = init->sic_inittag;
1223 
1224 			sctp->sctp_ftsn = ntohl(init->sic_inittsn);
1225 			sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
1226 			sctp->sctp_frwnd = ntohl(init->sic_a_rwnd);
1227 			sctp->sctp_fcsn = sctp->sctp_lastacked;
1228 
1229 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
1230 				sctp->sctp_state = SCTPS_ESTABLISHED;
1231 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
1232 			}
1233 
1234 			dprint(1, ("sctp peer %x:%x:%x:%x (%d) restarted\n",
1235 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
1236 			    (int)(sctp->sctp_fport)));
1237 			/* reset parameters */
1238 			sctp_congest_reset(sctp);
1239 
1240 			/* reset stream bookkeeping */
1241 			sctp_instream_cleanup(sctp, B_FALSE);
1242 
1243 			sctp->sctp_istr_nmsgs = 0;
1244 			sctp->sctp_rxqueued = 0;
1245 			for (i = 0; i < sctp->sctp_num_ostr; i++) {
1246 				sctp->sctp_ostrcntrs[i] = 0;
1247 			}
1248 			/* XXX flush xmit_list? */
1249 
1250 			return (0);
1251 		} else if (init->sic_inittag != sctp->sctp_fvtag &&
1252 		    iack->sic_inittag == sctp->sctp_lvtag) {
1253 
1254 			/* Section 5.2.4 case B: INIT collision */
1255 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
1256 				if (!sctp_initialize_params(sctp, init, iack))
1257 					return (-1);	/* Drop? */
1258 				sctp->sctp_state = SCTPS_ESTABLISHED;
1259 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
1260 			}
1261 
1262 			dprint(1, ("init collision with %x:%x:%x:%x (%d)\n",
1263 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
1264 			    (int)(sctp->sctp_fport)));
1265 
1266 			return (0);
1267 		} else if (iack->sic_inittag != sctp->sctp_lvtag &&
1268 		    init->sic_inittag == sctp->sctp_fvtag &&
1269 		    *fttag == 0 && *lttag == 0) {
1270 
1271 			/* Section 5.2.4 case C: late COOKIE */
1272 			dprint(1, ("late cookie from %x:%x:%x:%x (%d)\n",
1273 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
1274 			    (int)(sctp->sctp_fport)));
1275 			return (-1);
1276 		} else if (init->sic_inittag == sctp->sctp_fvtag &&
1277 		    iack->sic_inittag == sctp->sctp_lvtag) {
1278 
1279 			/*
1280 			 * Section 5.2.4 case D: COOKIE ECHO retransmit
1281 			 * Don't check cookie lifetime
1282 			 */
1283 			dprint(1, ("cookie tags match from %x:%x:%x:%x (%d)\n",
1284 			    SCTP_PRINTADDR(sctp->sctp_current->faddr),
1285 			    (int)(sctp->sctp_fport)));
1286 			if (sctp->sctp_state < SCTPS_ESTABLISHED) {
1287 				if (!sctp_initialize_params(sctp, init, iack))
1288 					return (-1);	/* Drop? */
1289 				sctp->sctp_state = SCTPS_ESTABLISHED;
1290 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
1291 			}
1292 			return (0);
1293 		} else {
1294 			/* unrecognized case -- silently drop it */
1295 			return (-1);
1296 		}
1297 	}
1298 
1299 	return (0);
1300 }
1301 
1302 /*
1303  * Similar to ip_fanout_sctp, except that the src addr(s) are drawn
1304  * from address parameters in an INIT ACK's address list. This
1305  * function is used when an INIT ACK is received but IP's fanout
1306  * function could not find a sctp via the normal lookup routine.
1307  * This can happen when a host sends an INIT ACK from a different
1308  * address than the INIT was sent to.
1309  *
1310  * Returns the sctp_t if found, or NULL if not found.
1311  */
1312 sctp_t *
1313 sctp_addrlist2sctp(mblk_t *mp, sctp_hdr_t *sctph, sctp_chunk_hdr_t *ich,
1314     uint_t ipif_seqid, zoneid_t zoneid)
1315 {
1316 	int isv4;
1317 	ipha_t *iph;
1318 	ip6_t *ip6h;
1319 	in6_addr_t dst;
1320 	in6_addr_t src;
1321 	sctp_parm_hdr_t *ph;
1322 	ssize_t remaining;
1323 	sctp_init_chunk_t *iack;
1324 	uint32_t ports;
1325 	sctp_t *sctp = NULL;
1326 
1327 	ASSERT(ich->sch_id == CHUNK_INIT_ACK);
1328 
1329 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
1330 	if (isv4) {
1331 		iph = (ipha_t *)mp->b_rptr;
1332 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, &dst);
1333 	} else {
1334 		ip6h = (ip6_t *)mp->b_rptr;
1335 		dst = ip6h->ip6_dst;
1336 	}
1337 
1338 	ports = *(uint32_t *)sctph;
1339 
1340 	dprint(1, ("sctp_addrlist2sctp: ports=%u, dst = %x:%x:%x:%x\n",
1341 	    ports, SCTP_PRINTADDR(dst)));
1342 
1343 	/* pull out any address parameters */
1344 	remaining = ntohs(ich->sch_len) - sizeof (*ich) - sizeof (*iack);
1345 	if (remaining < sizeof (*ph)) {
1346 		return (NULL);
1347 	}
1348 
1349 	iack = (sctp_init_chunk_t *)(ich + 1);
1350 	ph = (sctp_parm_hdr_t *)(iack + 1);
1351 
1352 	while (ph != NULL) {
1353 		/*
1354 		 * params have been put in host byteorder by
1355 		 * sctp_check_input()
1356 		 */
1357 		if (ph->sph_type == PARM_ADDR4) {
1358 			IN6_INADDR_TO_V4MAPPED((struct in_addr *)(ph + 1),
1359 			    &src);
1360 
1361 			sctp = sctp_conn_match(&src, &dst, ports, ipif_seqid,
1362 			    zoneid);
1363 
1364 			dprint(1,
1365 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
1366 			    SCTP_PRINTADDR(src), (void *)sctp));
1367 
1368 
1369 			if (sctp != NULL) {
1370 				return (sctp);
1371 			}
1372 		} else if (ph->sph_type == PARM_ADDR6) {
1373 			src = *(in6_addr_t *)(ph + 1);
1374 			sctp = sctp_conn_match(&src, &dst, ports, ipif_seqid,
1375 			    zoneid);
1376 
1377 			dprint(1,
1378 			    ("sctp_addrlist2sctp: src=%x:%x:%x:%x, sctp=%p\n",
1379 			    SCTP_PRINTADDR(src), (void *)sctp));
1380 
1381 			if (sctp != NULL) {
1382 				return (sctp);
1383 			}
1384 		}
1385 
1386 		ph = sctp_next_parm(ph, &remaining);
1387 	}
1388 
1389 	return (NULL);
1390 }
1391