17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2177c67f2fSkcpoon 
227c478bd9Sstevel@tonic-gate /*
23*8dfac042SAnil udupa  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/stream.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
347c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
377c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
387c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <inet/common.h>
417c478bd9Sstevel@tonic-gate #include <inet/ip.h>
427c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
437c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
44f4b3ec61Sdh #include <inet/ipclassifier.h>
457c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
467c478bd9Sstevel@tonic-gate #include "sctp_asconf.h"
477c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate typedef struct sctp_asconf_s {
501d8c4025Svi 	mblk_t		*head;
511d8c4025Svi 	uint32_t 	cid;
527c478bd9Sstevel@tonic-gate } sctp_asconf_t;
537c478bd9Sstevel@tonic-gate 
541d8c4025Svi /*
551d8c4025Svi  * This is only used on a clustered node to maintain pre-allocated buffer info.
561d8c4025Svi  * before sending an ASCONF chunk. The reason for pre-allocation is we don't
571d8c4025Svi  * want to fail allocating memory when we get then ASCONF-ACK in order to
581d8c4025Svi  * update the clustering subsystem's state for this assoc.
591d8c4025Svi  */
601d8c4025Svi typedef struct sctp_cl_ainfo_s {
611d8c4025Svi 	uchar_t	*sctp_cl_alist;
621d8c4025Svi 	size_t	sctp_cl_asize;
631d8c4025Svi 	uchar_t	*sctp_cl_dlist;
641d8c4025Svi 	size_t	sctp_cl_dsize;
651d8c4025Svi } sctp_cl_ainfo_t;
661d8c4025Svi 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * The ASCONF chunk per-parameter request interface. ph is the
697c478bd9Sstevel@tonic-gate  * parameter header for the parameter in the request, and cid
707c478bd9Sstevel@tonic-gate  * is the parameters correlation ID. cont should be set to 1
717c478bd9Sstevel@tonic-gate  * if the ASCONF framework should continue processing request
727c478bd9Sstevel@tonic-gate  * parameters following this one, or 0 if it should stop. If
737c478bd9Sstevel@tonic-gate  * cont is -1, this indicates complete memory depletion, which
747c478bd9Sstevel@tonic-gate  * will cause the ASCONF framework to abort building a reply. If
757c478bd9Sstevel@tonic-gate  * act is 1, the callback should take whatever action it needs
767c478bd9Sstevel@tonic-gate  * to fulfil this request. If act is 0, this request has already
777c478bd9Sstevel@tonic-gate  * been processed, so the callback should only verify and pass
787c478bd9Sstevel@tonic-gate  * back error parameters, and not take any action.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * The callback should return an mblk with any reply enclosed,
817c478bd9Sstevel@tonic-gate  * with the correlation ID in the first four bytes of the
827c478bd9Sstevel@tonic-gate  * message. A NULL return implies implicit success to the
837c478bd9Sstevel@tonic-gate  * requestor.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate typedef mblk_t *sctp_asconf_func_t(sctp_t *, sctp_parm_hdr_t *ph, uint32_t cid,
861d8c4025Svi     sctp_faddr_t *, int *cont, int act, in6_addr_t *addr);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * The ASCONF chunk per-parameter ACK interface. ph is the parameter
907c478bd9Sstevel@tonic-gate  * header for the parameter returned in the ACK, and oph is the
917c478bd9Sstevel@tonic-gate  * original parameter sent out in the ASCONF request.
927c478bd9Sstevel@tonic-gate  * If the peer implicitly responded OK (by not including an
937c478bd9Sstevel@tonic-gate  * explicit OK for the request), ph will be NULL.
947c478bd9Sstevel@tonic-gate  * ph can also point to an Unrecognized Parameter parameter,
957c478bd9Sstevel@tonic-gate  * in which case the peer did not understand the request
967c478bd9Sstevel@tonic-gate  * parameter.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * ph and oph parameter headers are in host byte order. Encapsulated
997c478bd9Sstevel@tonic-gate  * parameters will still be in network byte order.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate typedef void sctp_asconf_ack_func_t(sctp_t *, sctp_parm_hdr_t *ph,
1021d8c4025Svi     sctp_parm_hdr_t *oph, sctp_faddr_t *, in6_addr_t *addr);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate typedef struct {
1057c478bd9Sstevel@tonic-gate 	uint16_t id;
1067c478bd9Sstevel@tonic-gate 	sctp_asconf_func_t *asconf;
1077c478bd9Sstevel@tonic-gate 	sctp_asconf_ack_func_t *asconf_ack;
1087c478bd9Sstevel@tonic-gate } dispatch_t;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static sctp_asconf_func_t sctp_addip_req, sctp_setprim_req,
1117c478bd9Sstevel@tonic-gate     sctp_asconf_unrec_parm;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static sctp_asconf_ack_func_t sctp_addip_ack, sctp_setprim_ack,
1147c478bd9Sstevel@tonic-gate     sctp_asconf_ack_unrec_parm;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static const dispatch_t sctp_asconf_dispatch_tbl[] = {
1177c478bd9Sstevel@tonic-gate /*	ID			ASCONF			ASCONF_ACK */
1187c478bd9Sstevel@tonic-gate 	{ PARM_ADD_IP,		sctp_addip_req,		sctp_addip_ack },
1197c478bd9Sstevel@tonic-gate 	{ PARM_DEL_IP,		sctp_addip_req,		sctp_addip_ack },
1207c478bd9Sstevel@tonic-gate 	{ PARM_SET_PRIMARY,	sctp_setprim_req,	sctp_setprim_ack }
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static const dispatch_t sctp_asconf_default_dispatch = {
1247c478bd9Sstevel@tonic-gate 	0, sctp_asconf_unrec_parm, sctp_asconf_ack_unrec_parm
1257c478bd9Sstevel@tonic-gate };
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * ASCONF framework
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static const dispatch_t *
1327c478bd9Sstevel@tonic-gate sctp_lookup_asconf_dispatch(int id)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	int i;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	for (i = 0; i < A_CNT(sctp_asconf_dispatch_tbl); i++) {
1377c478bd9Sstevel@tonic-gate 		if (sctp_asconf_dispatch_tbl[i].id == id) {
1387c478bd9Sstevel@tonic-gate 			return (sctp_asconf_dispatch_tbl + i);
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	return (&sctp_asconf_default_dispatch);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Frees mp on failure
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate static mblk_t *
1497c478bd9Sstevel@tonic-gate sctp_asconf_prepend_errwrap(mblk_t *mp, uint32_t cid)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	mblk_t		*wmp;
1527c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t	*wph;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* Prepend a wrapper err cause ind param */
1557c478bd9Sstevel@tonic-gate 	wmp = allocb(sizeof (*wph) + sizeof (cid), BPRI_MED);
1567c478bd9Sstevel@tonic-gate 	if (wmp == NULL) {
1577c478bd9Sstevel@tonic-gate 		freemsg(mp);
1587c478bd9Sstevel@tonic-gate 		return (NULL);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	wmp->b_wptr += sizeof (*wph) + sizeof (cid);
1617c478bd9Sstevel@tonic-gate 	wph = (sctp_parm_hdr_t *)wmp->b_rptr;
1627c478bd9Sstevel@tonic-gate 	wph->sph_type = htons(PARM_ERROR_IND);
1637c478bd9Sstevel@tonic-gate 	wph->sph_len = htons(msgdsize(mp) + sizeof (*wph) + sizeof (cid));
1647c478bd9Sstevel@tonic-gate 	bcopy(&cid, wph + 1, sizeof (uint32_t));
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	wmp->b_cont = mp;
1677c478bd9Sstevel@tonic-gate 	return (wmp);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1717c478bd9Sstevel@tonic-gate static mblk_t *
1727c478bd9Sstevel@tonic-gate sctp_asconf_unrec_parm(sctp_t *sctp, sctp_parm_hdr_t *ph, uint32_t cid,
1731d8c4025Svi     sctp_faddr_t *fp, int *cont, int act, in6_addr_t *addr)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	mblk_t *mp = NULL;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* Unrecognized param; check the high order bits */
17877ebe684SGeorge Shepherd 	if ((ph->sph_type & SCTP_UNREC_PARAM_MASK) ==
17977ebe684SGeorge Shepherd 	    (SCTP_CONT_PROC_PARAMS | SCTP_REPORT_THIS_PARAM)) {
1807c478bd9Sstevel@tonic-gate 		/* report unrecognized param, and keep processing */
181c513743fSGeorge Shepherd 		sctp_add_unrec_parm(ph, &mp, B_FALSE);
1827c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
1837c478bd9Sstevel@tonic-gate 			*cont = -1;
1847c478bd9Sstevel@tonic-gate 			return (NULL);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		/* Prepend a the CID and a wrapper err cause ind param */
1877c478bd9Sstevel@tonic-gate 		mp = sctp_asconf_prepend_errwrap(mp, cid);
1887c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
1897c478bd9Sstevel@tonic-gate 			*cont = -1;
1907c478bd9Sstevel@tonic-gate 			return (NULL);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		*cont = 1;
1947c478bd9Sstevel@tonic-gate 		return (mp);
1957c478bd9Sstevel@tonic-gate 	}
19677ebe684SGeorge Shepherd 	if (ph->sph_type & SCTP_REPORT_THIS_PARAM) {
1977c478bd9Sstevel@tonic-gate 		/* Stop processing and drop; report unrecognized param */
198c513743fSGeorge Shepherd 		sctp_add_unrec_parm(ph, &mp, B_FALSE);
1997c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
2007c478bd9Sstevel@tonic-gate 			*cont = -1;
2017c478bd9Sstevel@tonic-gate 			return (NULL);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		/* Prepend a the CID and a wrapper err cause ind param */
2047c478bd9Sstevel@tonic-gate 		mp = sctp_asconf_prepend_errwrap(mp, cid);
2057c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
2067c478bd9Sstevel@tonic-gate 			*cont = -1;
2077c478bd9Sstevel@tonic-gate 			return (NULL);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		*cont = 0;
2117c478bd9Sstevel@tonic-gate 		return (mp);
2127c478bd9Sstevel@tonic-gate 	}
21377ebe684SGeorge Shepherd 	if (ph->sph_type & SCTP_CONT_PROC_PARAMS) {
2147c478bd9Sstevel@tonic-gate 		/* skip and continue processing */
2157c478bd9Sstevel@tonic-gate 		*cont = 1;
2167c478bd9Sstevel@tonic-gate 		return (NULL);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* 2 high bits are clear; stop processing and drop packet */
2207c478bd9Sstevel@tonic-gate 	*cont = 0;
2217c478bd9Sstevel@tonic-gate 	return (NULL);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2257c478bd9Sstevel@tonic-gate static void
2267c478bd9Sstevel@tonic-gate sctp_asconf_ack_unrec_parm(sctp_t *sctp, sctp_parm_hdr_t *ph,
2271d8c4025Svi     sctp_parm_hdr_t *oph, sctp_faddr_t *fp, in6_addr_t *laddr)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	ASSERT(ph);
230*8dfac042SAnil udupa 	sctp_error_event(sctp, (sctp_chunk_hdr_t *)ph, B_TRUE);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate sctp_asconf_init(sctp_asconf_t *asc)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	ASSERT(asc != NULL);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	asc->head = NULL;
2397c478bd9Sstevel@tonic-gate 	asc->cid = 0;
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static int
2437c478bd9Sstevel@tonic-gate sctp_asconf_add(sctp_asconf_t *asc, mblk_t *mp)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	uint32_t *cp;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/* XXX can't exceed MTU */
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	cp = (uint32_t *)(mp->b_rptr + sizeof (sctp_parm_hdr_t));
2507c478bd9Sstevel@tonic-gate 	*cp = asc->cid++;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (asc->head == NULL)
2537c478bd9Sstevel@tonic-gate 		asc->head = mp;
2547c478bd9Sstevel@tonic-gate 	else
2557c478bd9Sstevel@tonic-gate 		linkb(asc->head, mp);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (0);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static void
2617c478bd9Sstevel@tonic-gate sctp_asconf_destroy(sctp_asconf_t *asc)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	if (asc->head != NULL) {
2647c478bd9Sstevel@tonic-gate 		freemsg(asc->head);
2657c478bd9Sstevel@tonic-gate 		asc->head = NULL;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	asc->cid = 0;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static int
2711d8c4025Svi sctp_asconf_send(sctp_t *sctp, sctp_asconf_t *asc, sctp_faddr_t *fp,
2721d8c4025Svi     sctp_cl_ainfo_t *ainfo)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	mblk_t			*mp, *nmp;
2757c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch;
2767c478bd9Sstevel@tonic-gate 	boolean_t		isv4;
2777c478bd9Sstevel@tonic-gate 	size_t			msgsize;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	ASSERT(asc != NULL && asc->head != NULL);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	isv4 = (fp != NULL) ? fp->isv4 : sctp->sctp_current->isv4;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/* SCTP chunk header + Serial Number + Address Param TLV */
2847c478bd9Sstevel@tonic-gate 	msgsize = sizeof (*ch) + sizeof (uint32_t) +
2857c478bd9Sstevel@tonic-gate 	    (isv4 ? PARM_ADDR4_LEN : PARM_ADDR6_LEN);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	mp = allocb(msgsize, BPRI_MED);
2887c478bd9Sstevel@tonic-gate 	if (mp == NULL)
2897c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	mp->b_wptr += msgsize;
2927c478bd9Sstevel@tonic-gate 	mp->b_cont = asc->head;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	ch = (sctp_chunk_hdr_t *)mp->b_rptr;
2957c478bd9Sstevel@tonic-gate 	ch->sch_id = CHUNK_ASCONF;
2967c478bd9Sstevel@tonic-gate 	ch->sch_flags = 0;
2977c478bd9Sstevel@tonic-gate 	ch->sch_len = htons(msgdsize(mp));
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	nmp = msgpullup(mp, -1);
3007c478bd9Sstevel@tonic-gate 	if (nmp == NULL) {
3017c478bd9Sstevel@tonic-gate 		freeb(mp);
3027c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3051d8c4025Svi 	/*
3061d8c4025Svi 	 * Stash the address list and the count so that when the operation
3071d8c4025Svi 	 * completes, i.e. when as get an ACK, we can update the clustering's
3081d8c4025Svi 	 * state for this association.
3091d8c4025Svi 	 */
3101d8c4025Svi 	if (ainfo != NULL) {
3111d8c4025Svi 		ASSERT(cl_sctp_assoc_change != NULL);
3121d8c4025Svi 		ASSERT(nmp->b_prev == NULL);
3131d8c4025Svi 		nmp->b_prev = (mblk_t *)ainfo;
3141d8c4025Svi 	}
3157c478bd9Sstevel@tonic-gate 	/* Clean up the temporary mblk chain */
3167c478bd9Sstevel@tonic-gate 	freemsg(mp);
3177c478bd9Sstevel@tonic-gate 	asc->head = NULL;
3187c478bd9Sstevel@tonic-gate 	asc->cid = 0;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* Queue it ... */
3217c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cxmit_list == NULL) {
3227c478bd9Sstevel@tonic-gate 		sctp->sctp_cxmit_list = nmp;
3237c478bd9Sstevel@tonic-gate 	} else {
3247c478bd9Sstevel@tonic-gate 		linkb(sctp->sctp_cxmit_list, nmp);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/* And try to send it. */
3307c478bd9Sstevel@tonic-gate 	sctp_wput_asconf(sctp, fp);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	return (0);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * If the peer does not understand an ASCONF chunk, we simply
3377c478bd9Sstevel@tonic-gate  * clear out the cxmit_list, since we can send nothing further
3387c478bd9Sstevel@tonic-gate  * that the peer will understand.
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  * Assumes chunk length has already been checked.
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3437c478bd9Sstevel@tonic-gate void
3441d8c4025Svi sctp_asconf_free_cxmit(sctp_t *sctp, sctp_chunk_hdr_t *ch)
3457c478bd9Sstevel@tonic-gate {
3461d8c4025Svi 	mblk_t		*mp;
3471d8c4025Svi 	mblk_t		*mp1;
3481d8c4025Svi 	sctp_cl_ainfo_t	*ainfo;
3491d8c4025Svi 
3507c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cxmit_list == NULL) {
3517c478bd9Sstevel@tonic-gate 		/* Nothing pending */
3527c478bd9Sstevel@tonic-gate 		return;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3551d8c4025Svi 	mp = sctp->sctp_cxmit_list;
3561d8c4025Svi 	while (mp != NULL) {
3571d8c4025Svi 		mp1 = mp->b_cont;
3581d8c4025Svi 		mp->b_cont = NULL;
3591d8c4025Svi 		if (mp->b_prev != NULL) {
3601d8c4025Svi 			ainfo = (sctp_cl_ainfo_t *)mp->b_prev;
3611d8c4025Svi 			mp->b_prev = NULL;
3621d8c4025Svi 			kmem_free(ainfo->sctp_cl_alist, ainfo->sctp_cl_asize);
3631d8c4025Svi 			kmem_free(ainfo->sctp_cl_dlist, ainfo->sctp_cl_dsize);
3641d8c4025Svi 			kmem_free(ainfo, sizeof (*ainfo));
3651d8c4025Svi 		}
3661d8c4025Svi 		freeb(mp);
3671d8c4025Svi 		mp = mp1;
3681d8c4025Svi 	}
3697c478bd9Sstevel@tonic-gate 	sctp->sctp_cxmit_list = NULL;
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate void
3737c478bd9Sstevel@tonic-gate sctp_input_asconf(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	const dispatch_t	*dp;
3767c478bd9Sstevel@tonic-gate 	mblk_t			*hmp;
3777c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
3787c478bd9Sstevel@tonic-gate 	uint32_t		*idp;
3797c478bd9Sstevel@tonic-gate 	uint32_t		*hidp;
3807c478bd9Sstevel@tonic-gate 	ssize_t			rlen;
3817c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
3827c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ach;
3837c478bd9Sstevel@tonic-gate 	int			cont;
3847c478bd9Sstevel@tonic-gate 	int			act;
3857c478bd9Sstevel@tonic-gate 	uint16_t		plen;
3861d8c4025Svi 	uchar_t			*alist = NULL;
3871d8c4025Svi 	size_t			asize = 0;
3881d8c4025Svi 	uchar_t			*dlist = NULL;
3891d8c4025Svi 	size_t			dsize = 0;
3901d8c4025Svi 	uchar_t			*aptr = NULL;
3911d8c4025Svi 	uchar_t			*dptr = NULL;
3921d8c4025Svi 	int			acount = 0;
3931d8c4025Svi 	int			dcount = 0;
394f4b3ec61Sdh 	sctp_stack_t		*sctps = sctp->sctp_sctps;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	ASSERT(ch->sch_id == CHUNK_ASCONF);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	idp = (uint32_t *)(ch + 1);
3997c478bd9Sstevel@tonic-gate 	rlen = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*idp);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (rlen < 0 || rlen < sizeof (*idp)) {
4027c478bd9Sstevel@tonic-gate 		/* nothing there; bail out */
4037c478bd9Sstevel@tonic-gate 		return;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* Check for duplicates */
4077c478bd9Sstevel@tonic-gate 	*idp = ntohl(*idp);
4087c478bd9Sstevel@tonic-gate 	if (*idp == (sctp->sctp_fcsn + 1)) {
4097c478bd9Sstevel@tonic-gate 		act = 1;
4107c478bd9Sstevel@tonic-gate 	} else if (*idp == sctp->sctp_fcsn) {
4117c478bd9Sstevel@tonic-gate 		act = 0;
4127c478bd9Sstevel@tonic-gate 	} else {
4137c478bd9Sstevel@tonic-gate 		/* stale or malicious packet; drop */
4147c478bd9Sstevel@tonic-gate 		return;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/* Create the ASCONF_ACK header */
4187c478bd9Sstevel@tonic-gate 	hmp = sctp_make_mp(sctp, fp, sizeof (*ach) + sizeof (*idp));
4197c478bd9Sstevel@tonic-gate 	if (hmp == NULL) {
4207c478bd9Sstevel@tonic-gate 		/* Let the peer retransmit */
421f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_send_asconf_ack_failed);
4227c478bd9Sstevel@tonic-gate 		return;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	ach = (sctp_chunk_hdr_t *)hmp->b_wptr;
4257c478bd9Sstevel@tonic-gate 	ach->sch_id = CHUNK_ASCONF_ACK;
4267c478bd9Sstevel@tonic-gate 	ach->sch_flags = 0;
4277c478bd9Sstevel@tonic-gate 	/* Set the length later */
4287c478bd9Sstevel@tonic-gate 	hidp = (uint32_t *)(ach + 1);
4297c478bd9Sstevel@tonic-gate 	*hidp = htonl(*idp);
4307c478bd9Sstevel@tonic-gate 	hmp->b_wptr = (uchar_t *)(hidp + 1);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/* Move to the Address Parameter */
4337c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(idp + 1);
4347c478bd9Sstevel@tonic-gate 	if (rlen <= ntohs(ph->sph_len)) {
4357c478bd9Sstevel@tonic-gate 		freeb(hmp);
4367c478bd9Sstevel@tonic-gate 		return;
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * We already have the association here, so this address parameter
4417c478bd9Sstevel@tonic-gate 	 * doesn't seem to be very useful, should we make sure this is part
4427c478bd9Sstevel@tonic-gate 	 * of the association and send an error, if not?
4437c478bd9Sstevel@tonic-gate 	 * Ignore it for now.
4447c478bd9Sstevel@tonic-gate 	 */
4457c478bd9Sstevel@tonic-gate 	rlen -= ntohs(ph->sph_len);
4467c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)((char *)ph + ntohs(ph->sph_len));
4471d8c4025Svi 
4481d8c4025Svi 	/*
4491d8c4025Svi 	 * We need to pre-allocate buffer before processing the ASCONF
4501d8c4025Svi 	 * chunk. We don't want to fail allocating buffers after processing
4511d8c4025Svi 	 * the ASCONF chunk. So, we walk the list and get the number of
4521d8c4025Svi 	 * addresses added and/or deleted.
4531d8c4025Svi 	 */
4541d8c4025Svi 	if (cl_sctp_assoc_change != NULL) {
4551d8c4025Svi 		sctp_parm_hdr_t	*oph = ph;
4561d8c4025Svi 		ssize_t		orlen = rlen;
4571d8c4025Svi 
4581d8c4025Svi 		/*
4591d8c4025Svi 		 * This not very efficient, but there is no better way of
4601d8c4025Svi 		 * doing it.  It should be fine since normally the param list
4611d8c4025Svi 		 * will not be very long.
4621d8c4025Svi 		 */
4631d8c4025Svi 		while (orlen > 0) {
4641d8c4025Svi 			/* Sanity checks */
4651d8c4025Svi 			if (orlen < sizeof (*oph))
4661d8c4025Svi 				break;
4671d8c4025Svi 			plen = ntohs(oph->sph_len);
4681d8c4025Svi 			if (plen < sizeof (*oph) || plen > orlen)
4691d8c4025Svi 				break;
4701d8c4025Svi 			if (oph->sph_type == htons(PARM_ADD_IP))
4711d8c4025Svi 				acount++;
4721d8c4025Svi 			if (oph->sph_type == htons(PARM_DEL_IP))
4731d8c4025Svi 				dcount++;
4741d8c4025Svi 			oph = sctp_next_parm(oph, &orlen);
4751d8c4025Svi 			if (oph == NULL)
4761d8c4025Svi 				break;
4771d8c4025Svi 		}
4781d8c4025Svi 		if (acount > 0 || dcount > 0) {
4791d8c4025Svi 			if (acount > 0) {
4801d8c4025Svi 				asize = sizeof (in6_addr_t) * acount;
4811d8c4025Svi 				alist = kmem_alloc(asize, KM_NOSLEEP);
4821d8c4025Svi 				if (alist == NULL) {
4831d8c4025Svi 					freeb(hmp);
484f4b3ec61Sdh 					SCTP_KSTAT(sctps, sctp_cl_assoc_change);
4851d8c4025Svi 					return;
4861d8c4025Svi 				}
4871d8c4025Svi 			}
4881d8c4025Svi 			if (dcount > 0) {
4891d8c4025Svi 				dsize = sizeof (in6_addr_t) * dcount;
4901d8c4025Svi 				dlist = kmem_alloc(dsize, KM_NOSLEEP);
4911d8c4025Svi 				if (dlist == NULL) {
4921d8c4025Svi 					if (acount > 0)
4931d8c4025Svi 						kmem_free(alist, asize);
4941d8c4025Svi 					freeb(hmp);
495f4b3ec61Sdh 					SCTP_KSTAT(sctps, sctp_cl_assoc_change);
4961d8c4025Svi 					return;
4971d8c4025Svi 				}
4981d8c4025Svi 			}
4991d8c4025Svi 			aptr = alist;
5001d8c4025Svi 			dptr = dlist;
5011d8c4025Svi 			/*
5021d8c4025Svi 			 * We will get the actual count when we process
5031d8c4025Svi 			 * the chunk.
5041d8c4025Svi 			 */
5051d8c4025Svi 			acount = 0;
5061d8c4025Svi 			dcount = 0;
5071d8c4025Svi 		}
5081d8c4025Svi 	}
5097c478bd9Sstevel@tonic-gate 	cont = 1;
5107c478bd9Sstevel@tonic-gate 	while (rlen > 0 && cont) {
5111d8c4025Svi 		in6_addr_t	addr;
5121d8c4025Svi 
5137c478bd9Sstevel@tonic-gate 		/* Sanity checks */
5147c478bd9Sstevel@tonic-gate 		if (rlen < sizeof (*ph))
5157c478bd9Sstevel@tonic-gate 			break;
5167c478bd9Sstevel@tonic-gate 		plen = ntohs(ph->sph_len);
5177c478bd9Sstevel@tonic-gate 		if (plen < sizeof (*ph) || plen > rlen) {
5187c478bd9Sstevel@tonic-gate 			break;
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		idp = (uint32_t *)(ph + 1);
5217c478bd9Sstevel@tonic-gate 		dp = sctp_lookup_asconf_dispatch(ntohs(ph->sph_type));
5227c478bd9Sstevel@tonic-gate 		ASSERT(dp);
5237c478bd9Sstevel@tonic-gate 		if (dp->asconf) {
5241d8c4025Svi 			mp = dp->asconf(sctp, ph, *idp, fp, &cont, act, &addr);
5257c478bd9Sstevel@tonic-gate 			if (cont == -1) {
5267c478bd9Sstevel@tonic-gate 				/*
5277c478bd9Sstevel@tonic-gate 				 * Not even enough memory to create
5287c478bd9Sstevel@tonic-gate 				 * an out-of-resources error. Free
5297c478bd9Sstevel@tonic-gate 				 * everything and return; the peer
5307c478bd9Sstevel@tonic-gate 				 * should retransmit.
5317c478bd9Sstevel@tonic-gate 				 */
5327c478bd9Sstevel@tonic-gate 				freemsg(hmp);
5331d8c4025Svi 				if (alist != NULL)
5341d8c4025Svi 					kmem_free(alist, asize);
5351d8c4025Svi 				if (dlist != NULL)
5361d8c4025Svi 					kmem_free(dlist, dsize);
5377c478bd9Sstevel@tonic-gate 				return;
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 			if (mp != NULL) {
5407c478bd9Sstevel@tonic-gate 				linkb(hmp, mp);
5411d8c4025Svi 			} else if (act != 0) {
5421d8c4025Svi 				/* update the add/delete list */
5431d8c4025Svi 				if (cl_sctp_assoc_change != NULL) {
5441d8c4025Svi 					if (ph->sph_type ==
5451d8c4025Svi 					    htons(PARM_ADD_IP)) {
5461d8c4025Svi 						ASSERT(alist != NULL);
5471d8c4025Svi 						bcopy(&addr, aptr,
5481d8c4025Svi 						    sizeof (addr));
5491d8c4025Svi 						aptr += sizeof (addr);
5501d8c4025Svi 						acount++;
5511d8c4025Svi 					} else if (ph->sph_type ==
5521d8c4025Svi 					    htons(PARM_DEL_IP)) {
5531d8c4025Svi 						ASSERT(dlist != NULL);
5541d8c4025Svi 						bcopy(&addr, dptr,
5551d8c4025Svi 						    sizeof (addr));
5561d8c4025Svi 						dptr += sizeof (addr);
5571d8c4025Svi 						dcount++;
5581d8c4025Svi 					}
5591d8c4025Svi 				}
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &rlen);
5637c478bd9Sstevel@tonic-gate 		if (ph == NULL)
5647c478bd9Sstevel@tonic-gate 			break;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5671d8c4025Svi 	/*
5681d8c4025Svi 	 * Update clustering's state for this assoc. Note acount/dcount
5691d8c4025Svi 	 * could be zero (i.e. if the add/delete address(es) were not
5701d8c4025Svi 	 * processed successfully). Regardless, if the ?size is > 0,
5711d8c4025Svi 	 * it is the clustering module's responsibility to free the lists.
5721d8c4025Svi 	 */
5731d8c4025Svi 	if (cl_sctp_assoc_change != NULL) {
574bd670b35SErik Nordmark 		(*cl_sctp_assoc_change)(sctp->sctp_connp->conn_family,
575bd670b35SErik Nordmark 		    alist, asize,
5761d8c4025Svi 		    acount, dlist, dsize, dcount, SCTP_CL_PADDR,
5771d8c4025Svi 		    (cl_sctp_handle_t)sctp);
5781d8c4025Svi 		/* alist and dlist will be freed by the clustering module */
5791d8c4025Svi 	}
5807c478bd9Sstevel@tonic-gate 	/* Now that the params have been processed, increment the fcsn */
5817c478bd9Sstevel@tonic-gate 	if (act) {
5827c478bd9Sstevel@tonic-gate 		sctp->sctp_fcsn++;
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (fp->isv4)
5877c478bd9Sstevel@tonic-gate 		ach->sch_len = htons(msgdsize(hmp) - sctp->sctp_hdr_len);
5887c478bd9Sstevel@tonic-gate 	else
5897c478bd9Sstevel@tonic-gate 		ach->sch_len = htons(msgdsize(hmp) - sctp->sctp_hdr6_len);
5907c478bd9Sstevel@tonic-gate 
591bd670b35SErik Nordmark 	sctp_set_iplen(sctp, hmp, fp->ixa);
592bd670b35SErik Nordmark 	(void) conn_ip_output(hmp, fp->ixa);
593bd670b35SErik Nordmark 	BUMP_LOCAL(sctp->sctp_opkts);
5947c478bd9Sstevel@tonic-gate 	sctp_validate_peer(sctp);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate static sctp_parm_hdr_t *
5987c478bd9Sstevel@tonic-gate sctp_lookup_asconf_param(sctp_parm_hdr_t *ph, uint32_t cid, ssize_t rlen)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	uint32_t *idp;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	while (rlen > 0) {
6037c478bd9Sstevel@tonic-gate 		idp = (uint32_t *)(ph + 1);
6047c478bd9Sstevel@tonic-gate 		if (*idp == cid) {
6057c478bd9Sstevel@tonic-gate 			return (ph);
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &rlen);
6087c478bd9Sstevel@tonic-gate 		if (ph == NULL)
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 	return (NULL);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate void
6157c478bd9Sstevel@tonic-gate sctp_input_asconf_ack(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	const dispatch_t	*dp;
6187c478bd9Sstevel@tonic-gate 	uint32_t		*idp;
6197c478bd9Sstevel@tonic-gate 	uint32_t		*snp;
6207c478bd9Sstevel@tonic-gate 	ssize_t			rlen;
6217c478bd9Sstevel@tonic-gate 	ssize_t			plen;
6227c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
6237c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*oph;
6247c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*fph;
6257c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
6267c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*och;
6277c478bd9Sstevel@tonic-gate 	int			redosrcs = 0;
6287c478bd9Sstevel@tonic-gate 	uint16_t		param_len;
6291d8c4025Svi 	uchar_t			*alist;
6301d8c4025Svi 	uchar_t			*dlist;
6311d8c4025Svi 	uint_t			acount = 0;
6321d8c4025Svi 	uint_t			dcount = 0;
6331d8c4025Svi 	uchar_t			*aptr;
6341d8c4025Svi 	uchar_t			*dptr;
6351d8c4025Svi 	sctp_cl_ainfo_t		*ainfo;
6361d8c4025Svi 	in6_addr_t		addr;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	ASSERT(ch->sch_id == CHUNK_ASCONF_ACK);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	snp = (uint32_t *)(ch + 1);
6417c478bd9Sstevel@tonic-gate 	rlen = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*snp);
6427c478bd9Sstevel@tonic-gate 	if (rlen < 0) {
6437c478bd9Sstevel@tonic-gate 		return;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/* Accept only an ACK for the current serial number */
6477c478bd9Sstevel@tonic-gate 	*snp = ntohl(*snp);
6487c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cxmit_list == NULL || *snp != (sctp->sctp_lcsn - 1)) {
6497c478bd9Sstevel@tonic-gate 		/* Need to send an abort */
6507c478bd9Sstevel@tonic-gate 		return;
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 	sctp->sctp_cchunk_pend = 0;
6537c478bd9Sstevel@tonic-gate 	SCTP_FADDR_RC_TIMER_STOP(fp);
6547c478bd9Sstevel@tonic-gate 
6551d8c4025Svi 	mp = sctp->sctp_cxmit_list;
6561d8c4025Svi 	/*
6571d8c4025Svi 	 * We fill in the addresses here to update the clustering's state for
6581d8c4025Svi 	 * this assoc.
6591d8c4025Svi 	 */
6601d8c4025Svi 	if (mp != NULL && cl_sctp_assoc_change != NULL) {
6611d8c4025Svi 		ASSERT(mp->b_prev != NULL);
6621d8c4025Svi 		ainfo = (sctp_cl_ainfo_t *)mp->b_prev;
6631d8c4025Svi 		alist = ainfo->sctp_cl_alist;
6641d8c4025Svi 		dlist = ainfo->sctp_cl_dlist;
6651d8c4025Svi 		aptr = alist;
6661d8c4025Svi 		dptr = dlist;
6671d8c4025Svi 	}
6681d8c4025Svi 
6697c478bd9Sstevel@tonic-gate 	/*
6707c478bd9Sstevel@tonic-gate 	 * Pass explicit replies to callbacks:
6717c478bd9Sstevel@tonic-gate 	 * For each reply in the ACK, look up the corresponding
6727c478bd9Sstevel@tonic-gate 	 * original parameter in the request using the correlation
6737c478bd9Sstevel@tonic-gate 	 * ID, and pass it to the right callback.
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 	och = (sctp_chunk_hdr_t *)sctp->sctp_cxmit_list->b_rptr;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	plen = ntohs(och->sch_len) - sizeof (*och) - sizeof (*idp);
6787c478bd9Sstevel@tonic-gate 	idp = (uint32_t *)(och + 1);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/* Get to the 1st ASCONF param, need to skip Address TLV parm */
6817c478bd9Sstevel@tonic-gate 	fph = (sctp_parm_hdr_t *)(idp + 1);
6827c478bd9Sstevel@tonic-gate 	plen -= ntohs(fph->sph_len);
6837c478bd9Sstevel@tonic-gate 	fph = (sctp_parm_hdr_t *)((char *)fph + ntohs(fph->sph_len));
6847c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(snp + 1);
6857c478bd9Sstevel@tonic-gate 	while (rlen > 0) {
6867c478bd9Sstevel@tonic-gate 		/* Sanity checks */
6877c478bd9Sstevel@tonic-gate 		if (rlen < sizeof (*ph)) {
6887c478bd9Sstevel@tonic-gate 			break;
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 		param_len = ntohs(ph->sph_len);
6917c478bd9Sstevel@tonic-gate 		if (param_len < sizeof (*ph) || param_len > rlen) {
6927c478bd9Sstevel@tonic-gate 			break;
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 		idp = (uint32_t *)(ph + 1);
6957c478bd9Sstevel@tonic-gate 		oph = sctp_lookup_asconf_param(fph, *idp, plen);
6967c478bd9Sstevel@tonic-gate 		if (oph != NULL) {
6977c478bd9Sstevel@tonic-gate 			dp = sctp_lookup_asconf_dispatch(ntohs(oph->sph_type));
6987c478bd9Sstevel@tonic-gate 			ASSERT(dp);
6997c478bd9Sstevel@tonic-gate 			if (dp->asconf_ack) {
7001d8c4025Svi 				dp->asconf_ack(sctp, ph, oph, fp, &addr);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 				/* hack. see below */
7037c478bd9Sstevel@tonic-gate 				if (oph->sph_type == htons(PARM_ADD_IP) ||
7047c478bd9Sstevel@tonic-gate 				    oph->sph_type == htons(PARM_DEL_IP)) {
7057c478bd9Sstevel@tonic-gate 					redosrcs = 1;
7061d8c4025Svi 					/*
7071d8c4025Svi 					 * If the address was sucessfully
7081d8c4025Svi 					 * processed, add it to the add/delete
7091d8c4025Svi 					 * list to send to the clustering
7101d8c4025Svi 					 * module.
7111d8c4025Svi 					 */
7121d8c4025Svi 					if (cl_sctp_assoc_change != NULL &&
713a22dfb13SVenugopal Iyer 					    !SCTP_IS_ADDR_UNSPEC(
714a22dfb13SVenugopal Iyer 					    IN6_IS_ADDR_V4MAPPED(&addr),
715a22dfb13SVenugopal Iyer 					    addr)) {
7161d8c4025Svi 						if (oph->sph_type ==
7171d8c4025Svi 						    htons(PARM_ADD_IP)) {
7181d8c4025Svi 							bcopy(&addr, aptr,
7191d8c4025Svi 							    sizeof (addr));
7201d8c4025Svi 							aptr += sizeof (addr);
7211d8c4025Svi 							acount++;
7221d8c4025Svi 						} else {
7231d8c4025Svi 							bcopy(&addr, dptr,
7241d8c4025Svi 							    sizeof (addr));
7251d8c4025Svi 							dptr += sizeof (addr);
7261d8c4025Svi 							dcount++;
7271d8c4025Svi 						}
7281d8c4025Svi 					}
7297c478bd9Sstevel@tonic-gate 				}
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &rlen);
7347c478bd9Sstevel@tonic-gate 		if (ph == NULL)
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	/*
7397c478bd9Sstevel@tonic-gate 	 * Pass implicit replies to callbacks:
7407c478bd9Sstevel@tonic-gate 	 * For each original request, look up its parameter
7417c478bd9Sstevel@tonic-gate 	 * in the ACK. If there is no corresponding reply,
7427c478bd9Sstevel@tonic-gate 	 * call the callback with a NULL parameter, indicating
7437c478bd9Sstevel@tonic-gate 	 * success.
7447c478bd9Sstevel@tonic-gate 	 */
7457c478bd9Sstevel@tonic-gate 	rlen = plen;
7467c478bd9Sstevel@tonic-gate 	plen = ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*idp);
7477c478bd9Sstevel@tonic-gate 	oph = fph;
7487c478bd9Sstevel@tonic-gate 	fph = (sctp_parm_hdr_t *)((char *)ch + sizeof (sctp_chunk_hdr_t) +
7497c478bd9Sstevel@tonic-gate 	    sizeof (uint32_t));
7507c478bd9Sstevel@tonic-gate 	while (rlen > 0) {
7517c478bd9Sstevel@tonic-gate 		idp = (uint32_t *)(oph + 1);
7527c478bd9Sstevel@tonic-gate 		ph = sctp_lookup_asconf_param(fph, *idp, plen);
7537c478bd9Sstevel@tonic-gate 		if (ph == NULL) {
7547c478bd9Sstevel@tonic-gate 			dp = sctp_lookup_asconf_dispatch(ntohs(oph->sph_type));
7557c478bd9Sstevel@tonic-gate 			ASSERT(dp);
7567c478bd9Sstevel@tonic-gate 			if (dp->asconf_ack) {
7571d8c4025Svi 				dp->asconf_ack(sctp, NULL, oph, fp, &addr);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 				/* hack. see below */
7607c478bd9Sstevel@tonic-gate 				if (oph->sph_type == htons(PARM_ADD_IP) ||
7617c478bd9Sstevel@tonic-gate 				    oph->sph_type == htons(PARM_DEL_IP)) {
7627c478bd9Sstevel@tonic-gate 					redosrcs = 1;
7631d8c4025Svi 					/*
7641d8c4025Svi 					 * If the address was sucessfully
7651d8c4025Svi 					 * processed, add it to the add/delete
7661d8c4025Svi 					 * list to send to the clustering
7671d8c4025Svi 					 * module.
7681d8c4025Svi 					 */
7691d8c4025Svi 					if (cl_sctp_assoc_change != NULL &&
770a22dfb13SVenugopal Iyer 					    !SCTP_IS_ADDR_UNSPEC(
771a22dfb13SVenugopal Iyer 					    IN6_IS_ADDR_V4MAPPED(&addr),
772a22dfb13SVenugopal Iyer 					    addr)) {
7731d8c4025Svi 						if (oph->sph_type ==
7741d8c4025Svi 						    htons(PARM_ADD_IP)) {
7751d8c4025Svi 							bcopy(&addr, aptr,
7761d8c4025Svi 							    sizeof (addr));
7771d8c4025Svi 							aptr += sizeof (addr);
7781d8c4025Svi 							acount++;
7791d8c4025Svi 						} else {
7801d8c4025Svi 							bcopy(&addr, dptr,
7811d8c4025Svi 							    sizeof (addr));
7821d8c4025Svi 							dptr += sizeof (addr);
7831d8c4025Svi 							dcount++;
7841d8c4025Svi 						}
7851d8c4025Svi 					}
7867c478bd9Sstevel@tonic-gate 				}
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		oph = sctp_next_parm(oph, &rlen);
7907c478bd9Sstevel@tonic-gate 		if (oph == NULL) {
7917c478bd9Sstevel@tonic-gate 			break;
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/* We can now free up the first chunk in the cxmit list */
7967c478bd9Sstevel@tonic-gate 	sctp->sctp_cxmit_list = mp->b_cont;
7977c478bd9Sstevel@tonic-gate 	mp->b_cont = NULL;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	fp = SCTP_CHUNK_DEST(mp);
8007c478bd9Sstevel@tonic-gate 	ASSERT(fp != NULL && fp->suna >= MBLKL(mp));
8017c478bd9Sstevel@tonic-gate 	fp->suna -= MBLKL(mp);
8021d8c4025Svi 
8031d8c4025Svi 	/*
8041d8c4025Svi 	 * Update clustering's state for this assoc. Note acount/dcount
8051d8c4025Svi 	 * could be zero (i.e. if the add/delete address(es) did not
8061d8c4025Svi 	 * succeed). Regardless, if the ?size is > 0, it is the clustering
8071d8c4025Svi 	 * module's responsibility to free the lists.
8081d8c4025Svi 	 */
8091d8c4025Svi 	if (cl_sctp_assoc_change != NULL) {
8101d8c4025Svi 		ASSERT(mp->b_prev != NULL);
8111d8c4025Svi 		mp->b_prev = NULL;
8121d8c4025Svi 		ainfo->sctp_cl_alist = NULL;
8131d8c4025Svi 		ainfo->sctp_cl_dlist = NULL;
814bd670b35SErik Nordmark 		(*cl_sctp_assoc_change)(sctp->sctp_connp->conn_family, alist,
8151d8c4025Svi 		    ainfo->sctp_cl_asize, acount, dlist, ainfo->sctp_cl_dsize,
8161d8c4025Svi 		    dcount, SCTP_CL_LADDR, (cl_sctp_handle_t)sctp);
8171d8c4025Svi 		/* alist and dlist will be freed by the clustering module */
8181d8c4025Svi 		ainfo->sctp_cl_asize = 0;
8191d8c4025Svi 		ainfo->sctp_cl_dsize = 0;
8201d8c4025Svi 		kmem_free(ainfo, sizeof (*ainfo));
8211d8c4025Svi 	}
8227c478bd9Sstevel@tonic-gate 	freeb(mp);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/* can now send the next control chunk */
8257c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cxmit_list != NULL)
8267c478bd9Sstevel@tonic-gate 		sctp_wput_asconf(sctp, NULL);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/*
8297c478bd9Sstevel@tonic-gate 	 * If an add-ip or del-ip has completed (successfully or
8307c478bd9Sstevel@tonic-gate 	 * unsuccessfully), the pool of available source addresses
8317c478bd9Sstevel@tonic-gate 	 * may have changed, so we need to redo faddr source
8327c478bd9Sstevel@tonic-gate 	 * address selections. This is a bit of a hack since
8337c478bd9Sstevel@tonic-gate 	 * this really belongs in the add/del-ip code. However,
8347c478bd9Sstevel@tonic-gate 	 * that code consists of callbacks called for *each*
8357c478bd9Sstevel@tonic-gate 	 * add/del-ip parameter, and sctp_redo_faddr_srcs() is
8367c478bd9Sstevel@tonic-gate 	 * expensive enough that we really don't want to be
8377c478bd9Sstevel@tonic-gate 	 * doing it for each one. So we do it once here.
8387c478bd9Sstevel@tonic-gate 	 */
8397c478bd9Sstevel@tonic-gate 	if (redosrcs)
8407c478bd9Sstevel@tonic-gate 		sctp_redo_faddr_srcs(sctp);
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate static void
8447c478bd9Sstevel@tonic-gate sctp_rc_timer(sctp_t *sctp, sctp_faddr_t *fp)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate #define	SCTP_CLR_SENT_FLAG(mp)	((mp)->b_flag &= ~SCTP_CHUNK_FLAG_SENT)
8477c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*nfp;
8487c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*ofp;
849f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	ASSERT(fp != NULL);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	fp->rc_timer_running = 0;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_ESTABLISHED ||
8567c478bd9Sstevel@tonic-gate 	    sctp->sctp_cxmit_list == NULL) {
8577c478bd9Sstevel@tonic-gate 		return;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	/*
8607c478bd9Sstevel@tonic-gate 	 * Not a retransmission, this was deferred due to some error
8617c478bd9Sstevel@tonic-gate 	 * condition
8627c478bd9Sstevel@tonic-gate 	 */
8637c478bd9Sstevel@tonic-gate 	if (!SCTP_CHUNK_ISSENT(sctp->sctp_cxmit_list)) {
8647c478bd9Sstevel@tonic-gate 		sctp_wput_asconf(sctp, fp);
8657c478bd9Sstevel@tonic-gate 		return;
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * The sent flag indicates if the msg has been sent on this fp.
8697c478bd9Sstevel@tonic-gate 	 */
8707c478bd9Sstevel@tonic-gate 	SCTP_CLR_SENT_FLAG(sctp->sctp_cxmit_list);
8717c478bd9Sstevel@tonic-gate 	/* Retransmission */
8727c478bd9Sstevel@tonic-gate 	if (sctp->sctp_strikes >= sctp->sctp_pa_max_rxt) {
8737c478bd9Sstevel@tonic-gate 		/* time to give up */
874f4b3ec61Sdh 		BUMP_MIB(&sctps->sctps_mib, sctpAborted);
8757c478bd9Sstevel@tonic-gate 		sctp_assoc_event(sctp, SCTP_COMM_LOST, 0, NULL);
8767c478bd9Sstevel@tonic-gate 		sctp_clean_death(sctp, ETIMEDOUT);
8777c478bd9Sstevel@tonic-gate 		return;
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 	if (fp->strikes >= fp->max_retr) {
8807c478bd9Sstevel@tonic-gate 		if (sctp_faddr_dead(sctp, fp, SCTP_FADDRS_DOWN) == -1)
8817c478bd9Sstevel@tonic-gate 			return;
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	fp->strikes++;
8857c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes++;
8869f13099eSGeorge Shepherd 	SCTP_CALC_RXT(sctp, fp);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	nfp = sctp_rotate_faddr(sctp, fp);
8897c478bd9Sstevel@tonic-gate 	sctp->sctp_cchunk_pend = 0;
8907c478bd9Sstevel@tonic-gate 	ofp = SCTP_CHUNK_DEST(sctp->sctp_cxmit_list);
8917c478bd9Sstevel@tonic-gate 	SCTP_SET_CHUNK_DEST(sctp->sctp_cxmit_list, NULL);
8927c478bd9Sstevel@tonic-gate 	ASSERT(ofp != NULL && ofp == fp);
8937c478bd9Sstevel@tonic-gate 	ASSERT(ofp->suna >= MBLKL(sctp->sctp_cxmit_list));
8947c478bd9Sstevel@tonic-gate 	/*
8957c478bd9Sstevel@tonic-gate 	 * Enter slow start for this destination.
8967c478bd9Sstevel@tonic-gate 	 * XXX anything in the data path that needs to be considered?
8977c478bd9Sstevel@tonic-gate 	 */
8987c478bd9Sstevel@tonic-gate 	ofp->ssthresh = ofp->cwnd / 2;
8997c478bd9Sstevel@tonic-gate 	if (ofp->ssthresh < 2 * ofp->sfa_pmss)
9007c478bd9Sstevel@tonic-gate 		ofp->ssthresh = 2 * ofp->sfa_pmss;
9017c478bd9Sstevel@tonic-gate 	ofp->cwnd = ofp->sfa_pmss;
9027c478bd9Sstevel@tonic-gate 	ofp->pba = 0;
9037c478bd9Sstevel@tonic-gate 	ofp->suna -= MBLKL(sctp->sctp_cxmit_list);
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * The rexmit flags is used to determine if a serial number needs to
9067c478bd9Sstevel@tonic-gate 	 * be assigned or not, so once set we leave it there.
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	if (!SCTP_CHUNK_WANT_REXMIT(sctp->sctp_cxmit_list))
9097c478bd9Sstevel@tonic-gate 		SCTP_CHUNK_REXMIT(sctp->sctp_cxmit_list);
9107c478bd9Sstevel@tonic-gate 	sctp_wput_asconf(sctp, nfp);
9117c478bd9Sstevel@tonic-gate #undef	SCTP_CLR_SENT_FLAG
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate void
9157c478bd9Sstevel@tonic-gate sctp_wput_asconf(sctp_t *sctp, sctp_faddr_t *fp)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate #define	SCTP_SET_SENT_FLAG(mp)	((mp)->b_flag = SCTP_CHUNK_FLAG_SENT)
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	mblk_t 			*mp;
9207c478bd9Sstevel@tonic-gate 	mblk_t			*ipmp;
9217c478bd9Sstevel@tonic-gate 	uint32_t 		*snp;
9227c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t		*ph;
9237c478bd9Sstevel@tonic-gate 	boolean_t		isv4;
924f4b3ec61Sdh 	sctp_stack_t		*sctps = sctp->sctp_sctps;
925c31292eeSkcpoon 	boolean_t		saddr_set;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	if (sctp->sctp_cchunk_pend || sctp->sctp_cxmit_list == NULL ||
9287c478bd9Sstevel@tonic-gate 	    /* Queue it for later transmission if not yet established */
9297c478bd9Sstevel@tonic-gate 	    sctp->sctp_state < SCTPS_ESTABLISHED) {
9307c478bd9Sstevel@tonic-gate 		ip2dbg(("sctp_wput_asconf: cchunk pending? (%d) or null "\
9317c478bd9Sstevel@tonic-gate 		    "sctp_cxmit_list? (%s) or incorrect state? (%x)\n",
9327c478bd9Sstevel@tonic-gate 		    sctp->sctp_cchunk_pend, sctp->sctp_cxmit_list == NULL ?
9337c478bd9Sstevel@tonic-gate 		    "yes" : "no", sctp->sctp_state));
9347c478bd9Sstevel@tonic-gate 		return;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (fp == NULL)
9387c478bd9Sstevel@tonic-gate 		fp = sctp->sctp_current;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/* OK to send */
9417c478bd9Sstevel@tonic-gate 	ipmp = sctp_make_mp(sctp, fp, 0);
9427c478bd9Sstevel@tonic-gate 	if (ipmp == NULL) {
9437c478bd9Sstevel@tonic-gate 		SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto);
944f4b3ec61Sdh 		SCTP_KSTAT(sctps, sctp_send_asconf_failed);
9457c478bd9Sstevel@tonic-gate 		return;
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	mp = sctp->sctp_cxmit_list;
9487c478bd9Sstevel@tonic-gate 	/* Fill in the mandatory  Address Parameter TLV */
9497c478bd9Sstevel@tonic-gate 	isv4 = (fp != NULL) ? fp->isv4 : sctp->sctp_current->isv4;
9507c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)(mp->b_rptr + sizeof (sctp_chunk_hdr_t) +
9517c478bd9Sstevel@tonic-gate 	    sizeof (uint32_t));
9527c478bd9Sstevel@tonic-gate 	if (isv4) {
9537c478bd9Sstevel@tonic-gate 		ipha_t		*ipha = (ipha_t *)ipmp->b_rptr;
9547c478bd9Sstevel@tonic-gate 		in6_addr_t	ipaddr;
9557c478bd9Sstevel@tonic-gate 		ipaddr_t	addr4;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		ph->sph_type = htons(PARM_ADDR4);
9587c478bd9Sstevel@tonic-gate 		ph->sph_len = htons(PARM_ADDR4_LEN);
9597c478bd9Sstevel@tonic-gate 		if (ipha->ipha_src != INADDR_ANY) {
9607c478bd9Sstevel@tonic-gate 			bcopy(&ipha->ipha_src, ph + 1, IP_ADDR_LEN);
9617c478bd9Sstevel@tonic-gate 		} else {
962c31292eeSkcpoon 			ipaddr = sctp_get_valid_addr(sctp, B_FALSE, &saddr_set);
963df19b344Svi 			/*
964df19b344Svi 			 * All the addresses are down.
965df19b344Svi 			 * Maybe we might have better luck next time.
966df19b344Svi 			 */
967c31292eeSkcpoon 			if (!saddr_set) {
968df19b344Svi 				SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto);
969df19b344Svi 				freeb(ipmp);
970df19b344Svi 				return;
971df19b344Svi 			}
9727c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&ipaddr, addr4);
9737c478bd9Sstevel@tonic-gate 			bcopy(&addr4, ph + 1, IP_ADDR_LEN);
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 	} else {
9767c478bd9Sstevel@tonic-gate 		ip6_t		*ip6 = (ip6_t *)ipmp->b_rptr;
9777c478bd9Sstevel@tonic-gate 		in6_addr_t	ipaddr;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		ph->sph_type = htons(PARM_ADDR6);
9807c478bd9Sstevel@tonic-gate 		ph->sph_len = htons(PARM_ADDR6_LEN);
9817c478bd9Sstevel@tonic-gate 		if (!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
9827c478bd9Sstevel@tonic-gate 			bcopy(&ip6->ip6_src, ph + 1, IPV6_ADDR_LEN);
9837c478bd9Sstevel@tonic-gate 		} else {
984c31292eeSkcpoon 			ipaddr = sctp_get_valid_addr(sctp, B_TRUE, &saddr_set);
985df19b344Svi 			/*
986df19b344Svi 			 * All the addresses are down.
987df19b344Svi 			 * Maybe we might have better luck next time.
988df19b344Svi 			 */
989c31292eeSkcpoon 			if (!saddr_set) {
990df19b344Svi 				SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto);
991df19b344Svi 				freeb(ipmp);
992df19b344Svi 				return;
993df19b344Svi 			}
9947c478bd9Sstevel@tonic-gate 			bcopy(&ipaddr, ph + 1, IPV6_ADDR_LEN);
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/* Don't exceed CWND */
9997c478bd9Sstevel@tonic-gate 	if ((MBLKL(mp) > (fp->cwnd - fp->suna)) ||
10007c478bd9Sstevel@tonic-gate 	    ((mp = dupb(sctp->sctp_cxmit_list)) == NULL)) {
10017c478bd9Sstevel@tonic-gate 		SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto);
10027c478bd9Sstevel@tonic-gate 		freeb(ipmp);
10037c478bd9Sstevel@tonic-gate 		return;
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	/* Set the serial number now, if sending for the first time */
10077c478bd9Sstevel@tonic-gate 	if (!SCTP_CHUNK_WANT_REXMIT(mp)) {
10087c478bd9Sstevel@tonic-gate 		snp = (uint32_t *)(mp->b_rptr + sizeof (sctp_chunk_hdr_t));
10097c478bd9Sstevel@tonic-gate 		*snp = htonl(sctp->sctp_lcsn++);
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	SCTP_CHUNK_CLEAR_FLAGS(mp);
10127c478bd9Sstevel@tonic-gate 	fp->suna += MBLKL(mp);
10137c478bd9Sstevel@tonic-gate 	/* Attach the header and send the chunk */
10147c478bd9Sstevel@tonic-gate 	ipmp->b_cont = mp;
10157c478bd9Sstevel@tonic-gate 	sctp->sctp_cchunk_pend = 1;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	SCTP_SET_SENT_FLAG(sctp->sctp_cxmit_list);
10187c478bd9Sstevel@tonic-gate 	SCTP_SET_CHUNK_DEST(sctp->sctp_cxmit_list, fp);
1019bd670b35SErik Nordmark 	sctp_set_iplen(sctp, ipmp, fp->ixa);
1020bd670b35SErik Nordmark 	(void) conn_ip_output(ipmp, fp->ixa);
1021bd670b35SErik Nordmark 	BUMP_LOCAL(sctp->sctp_opkts);
10227c478bd9Sstevel@tonic-gate 	SCTP_FADDR_RC_TIMER_RESTART(sctp, fp, fp->rto);
10237c478bd9Sstevel@tonic-gate #undef	SCTP_SET_SENT_FLAG
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate  * Generate ASCONF error param, include errph, if present.
10287c478bd9Sstevel@tonic-gate  */
10297c478bd9Sstevel@tonic-gate static mblk_t *
10307c478bd9Sstevel@tonic-gate sctp_asconf_adderr(int err, sctp_parm_hdr_t *errph, uint32_t cid)
10317c478bd9Sstevel@tonic-gate {
10327c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
10337c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t	*eph;
10347c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t	*wph;
10357c478bd9Sstevel@tonic-gate 	size_t		len;
10367c478bd9Sstevel@tonic-gate 	size_t		elen = 0;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	len = sizeof (*wph) + sizeof (*eph) + sizeof (cid);
10397c478bd9Sstevel@tonic-gate 	if (errph != NULL) {
10407c478bd9Sstevel@tonic-gate 		elen = ntohs(errph->sph_len);
10417c478bd9Sstevel@tonic-gate 		len += elen;
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 	mp = allocb(len, BPRI_MED);
10447c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
10457c478bd9Sstevel@tonic-gate 		return (NULL);
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 	wph = (sctp_parm_hdr_t *)mp->b_rptr;
10487c478bd9Sstevel@tonic-gate 	/* error cause wrapper */
10497c478bd9Sstevel@tonic-gate 	wph->sph_type = htons(PARM_ERROR_IND);
10507c478bd9Sstevel@tonic-gate 	wph->sph_len = htons(len);
10517c478bd9Sstevel@tonic-gate 	bcopy(&cid, wph + 1, sizeof (uint32_t));
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	/* error cause */
10547c478bd9Sstevel@tonic-gate 	eph = (sctp_parm_hdr_t *)((char *)wph + sizeof (sctp_parm_hdr_t) +
10557c478bd9Sstevel@tonic-gate 	    sizeof (cid));
10567c478bd9Sstevel@tonic-gate 	eph->sph_type = htons(err);
10577c478bd9Sstevel@tonic-gate 	eph->sph_len = htons(len - sizeof (*wph) - sizeof (cid));
10587c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(eph + 1);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* details */
10617c478bd9Sstevel@tonic-gate 	if (elen > 0) {
10627c478bd9Sstevel@tonic-gate 		bcopy(errph, mp->b_wptr, elen);
10637c478bd9Sstevel@tonic-gate 		mp->b_wptr += elen;
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 	return (mp);
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate static mblk_t *
10697c478bd9Sstevel@tonic-gate sctp_check_addip_addr(sctp_parm_hdr_t *ph, sctp_parm_hdr_t *oph, int *cont,
10707c478bd9Sstevel@tonic-gate     uint32_t cid, in6_addr_t *raddr)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	uint16_t	atype;
10737c478bd9Sstevel@tonic-gate 	uint16_t	alen;
10747c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
10757c478bd9Sstevel@tonic-gate 	in6_addr_t	addr;
10767c478bd9Sstevel@tonic-gate 	ipaddr_t	*addr4;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	atype = ntohs(ph->sph_type);
10797c478bd9Sstevel@tonic-gate 	alen = ntohs(ph->sph_len);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (atype != PARM_ADDR4 && atype != PARM_ADDR6) {
10827c478bd9Sstevel@tonic-gate 		mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, cid);
10837c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
10847c478bd9Sstevel@tonic-gate 			*cont = -1;
10857c478bd9Sstevel@tonic-gate 		}
10867c478bd9Sstevel@tonic-gate 		return (mp);
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 	if ((atype == PARM_ADDR4 && alen < PARM_ADDR4_LEN) ||
10897c478bd9Sstevel@tonic-gate 	    (atype == PARM_ADDR6 && alen < PARM_ADDR6_LEN)) {
10907c478bd9Sstevel@tonic-gate 		mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, cid);
10917c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
10927c478bd9Sstevel@tonic-gate 			*cont = -1;
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 		return (mp);
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	/* Address parameter is present; extract and screen it */
10987c478bd9Sstevel@tonic-gate 	if (atype == PARM_ADDR4) {
10997c478bd9Sstevel@tonic-gate 		addr4 = (ipaddr_t *)(ph + 1);
11007c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(*addr4, &addr);
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		/* screen XXX loopback to scoping */
11037c478bd9Sstevel@tonic-gate 		if (*addr4 == 0 || *addr4 == INADDR_BROADCAST ||
1104c4f4b3c8Skcpoon 		    *addr4 == htonl(INADDR_LOOPBACK) || CLASSD(*addr4)) {
11057c478bd9Sstevel@tonic-gate 			dprint(1, ("addip: addr not unicast: %x:%x:%x:%x\n",
11067c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(addr)));
11077c478bd9Sstevel@tonic-gate 			mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph,
11087c478bd9Sstevel@tonic-gate 			    cid);
11097c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
11107c478bd9Sstevel@tonic-gate 				*cont = -1;
11117c478bd9Sstevel@tonic-gate 			}
11127c478bd9Sstevel@tonic-gate 			return (mp);
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 		/*
11157c478bd9Sstevel@tonic-gate 		 * XXX also need to check for subnet
11167c478bd9Sstevel@tonic-gate 		 * broadcasts. This should probably
11177c478bd9Sstevel@tonic-gate 		 * wait until we have full access
11187c478bd9Sstevel@tonic-gate 		 * to the ILL tables.
11197c478bd9Sstevel@tonic-gate 		 */
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	} else {
11227c478bd9Sstevel@tonic-gate 		bcopy(ph + 1, &addr, sizeof (addr));
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 		/* screen XXX loopback to scoping */
11257c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_LINKLOCAL(&addr) ||
11267c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_MULTICAST(&addr) ||
11277c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_LOOPBACK(&addr)) {
11287c478bd9Sstevel@tonic-gate 			dprint(1, ("addip: addr not unicast: %x:%x:%x:%x\n",
11297c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(addr)));
11307c478bd9Sstevel@tonic-gate 			mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph,
11317c478bd9Sstevel@tonic-gate 			    cid);
11327c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
11337c478bd9Sstevel@tonic-gate 				*cont = -1;
11347c478bd9Sstevel@tonic-gate 			}
11357c478bd9Sstevel@tonic-gate 			return (mp);
11367c478bd9Sstevel@tonic-gate 		}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	/* OK */
11417c478bd9Sstevel@tonic-gate 	*raddr = addr;
11427c478bd9Sstevel@tonic-gate 	return (NULL);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate /*
11467c478bd9Sstevel@tonic-gate  * Handles both add and delete address requests.
11477c478bd9Sstevel@tonic-gate  */
11487c478bd9Sstevel@tonic-gate static mblk_t *
11497c478bd9Sstevel@tonic-gate sctp_addip_req(sctp_t *sctp, sctp_parm_hdr_t *ph, uint32_t cid,
11501d8c4025Svi     sctp_faddr_t *fp, int *cont, int act, in6_addr_t *raddr)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate 	in6_addr_t	addr;
11537c478bd9Sstevel@tonic-gate 	uint16_t	type;
11547c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
11557c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*nfp;
115645916cd2Sjpk 	sctp_parm_hdr_t	*oph = ph;
115745916cd2Sjpk 	int		err;
1158f4b3ec61Sdh 	sctp_stack_t	*sctps = sctp->sctp_sctps;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	*cont = 1;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	/* Send back an authorization error if addip is disabled */
1163f4b3ec61Sdh 	if (!sctps->sctps_addip_enabled) {
116445916cd2Sjpk 		err = SCTP_ERR_UNAUTHORIZED;
116545916cd2Sjpk 		goto error_handler;
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	/* Check input */
11687c478bd9Sstevel@tonic-gate 	if (ntohs(ph->sph_len) < (sizeof (*ph) * 2)) {
116945916cd2Sjpk 		err = SCTP_ERR_BAD_MANDPARM;
117045916cd2Sjpk 		goto error_handler;
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	type = ntohs(ph->sph_type);
11747c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)((char *)ph + sizeof (sctp_parm_hdr_t) +
11757c478bd9Sstevel@tonic-gate 	    sizeof (cid));
11767c478bd9Sstevel@tonic-gate 	mp = sctp_check_addip_addr(ph, oph, cont, cid, &addr);
11777c478bd9Sstevel@tonic-gate 	if (mp != NULL)
11787c478bd9Sstevel@tonic-gate 		return (mp);
11791d8c4025Svi 	if (raddr != NULL)
11801d8c4025Svi 		*raddr = addr;
11817c478bd9Sstevel@tonic-gate 	if (type == PARM_ADD_IP) {
11827c478bd9Sstevel@tonic-gate 		if (sctp_lookup_faddr(sctp, &addr) != NULL) {
11837c478bd9Sstevel@tonic-gate 			/* Address is already part of association */
11847c478bd9Sstevel@tonic-gate 			dprint(1, ("addip: addr already here: %x:%x:%x:%x\n",
11857c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(addr)));
118645916cd2Sjpk 			err = SCTP_ERR_BAD_MANDPARM;
118745916cd2Sjpk 			goto error_handler;
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 		if (!act) {
11917c478bd9Sstevel@tonic-gate 			return (NULL);
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 		/* Add the new address */
11947c478bd9Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
119577c67f2fSkcpoon 		err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP, B_FALSE);
119645916cd2Sjpk 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
119745916cd2Sjpk 		if (err == ENOMEM) {
11987c478bd9Sstevel@tonic-gate 			/* no memory */
11997c478bd9Sstevel@tonic-gate 			*cont = -1;
12007c478bd9Sstevel@tonic-gate 			return (NULL);
12017c478bd9Sstevel@tonic-gate 		}
120245916cd2Sjpk 		if (err != 0) {
120345916cd2Sjpk 			err = SCTP_ERR_BAD_MANDPARM;
120445916cd2Sjpk 			goto error_handler;
120545916cd2Sjpk 		}
12067c478bd9Sstevel@tonic-gate 		sctp_intf_event(sctp, addr, SCTP_ADDR_ADDED, 0);
12077c478bd9Sstevel@tonic-gate 	} else if (type == PARM_DEL_IP) {
12087c478bd9Sstevel@tonic-gate 		nfp = sctp_lookup_faddr(sctp, &addr);
12097c478bd9Sstevel@tonic-gate 		if (nfp == NULL) {
12107c478bd9Sstevel@tonic-gate 			/*
12117c478bd9Sstevel@tonic-gate 			 * Peer is trying to delete an address that is not
12127c478bd9Sstevel@tonic-gate 			 * part of the association.
12137c478bd9Sstevel@tonic-gate 			 */
12147c478bd9Sstevel@tonic-gate 			dprint(1, ("delip: addr not here: %x:%x:%x:%x\n",
12157c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(addr)));
121645916cd2Sjpk 			err = SCTP_ERR_BAD_MANDPARM;
121745916cd2Sjpk 			goto error_handler;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 		if (sctp->sctp_faddrs == nfp && nfp->next == NULL) {
12207c478bd9Sstevel@tonic-gate 			/* Peer is trying to delete last address */
12217c478bd9Sstevel@tonic-gate 			dprint(1, ("delip: del last addr: %x:%x:%x:%x\n",
12227c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(addr)));
122345916cd2Sjpk 			err = SCTP_ERR_DEL_LAST_ADDR;
122445916cd2Sjpk 			goto error_handler;
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 		if (nfp == fp) {
12277c478bd9Sstevel@tonic-gate 			/* Peer is trying to delete source address */
12287c478bd9Sstevel@tonic-gate 			dprint(1, ("delip: del src addr: %x:%x:%x:%x\n",
12297c478bd9Sstevel@tonic-gate 			    SCTP_PRINTADDR(addr)));
123045916cd2Sjpk 			err = SCTP_ERR_DEL_SRC_ADDR;
123145916cd2Sjpk 			goto error_handler;
12327c478bd9Sstevel@tonic-gate 		}
12337c478bd9Sstevel@tonic-gate 		if (!act) {
12347c478bd9Sstevel@tonic-gate 			return (NULL);
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		sctp_unlink_faddr(sctp, nfp);
12387c478bd9Sstevel@tonic-gate 		/* Update all references to the deleted faddr */
12397c478bd9Sstevel@tonic-gate 		if (sctp->sctp_primary == nfp) {
12407c478bd9Sstevel@tonic-gate 			sctp->sctp_primary = fp;
12417c478bd9Sstevel@tonic-gate 		}
12427c478bd9Sstevel@tonic-gate 		if (sctp->sctp_current == nfp) {
124377c67f2fSkcpoon 			sctp_set_faddr_current(sctp, fp);
12447c478bd9Sstevel@tonic-gate 		}
12457c478bd9Sstevel@tonic-gate 		if (sctp->sctp_lastdata == nfp) {
12467c478bd9Sstevel@tonic-gate 			sctp->sctp_lastdata = fp;
12477c478bd9Sstevel@tonic-gate 		}
12487c478bd9Sstevel@tonic-gate 		if (sctp->sctp_shutdown_faddr == nfp) {
12497c478bd9Sstevel@tonic-gate 			sctp->sctp_shutdown_faddr = nfp;
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 		if (sctp->sctp_lastfaddr == nfp) {
12527c478bd9Sstevel@tonic-gate 			for (fp = sctp->sctp_faddrs; fp->next; fp = fp->next)
12537c478bd9Sstevel@tonic-gate 				;
12547c478bd9Sstevel@tonic-gate 			sctp->sctp_lastfaddr = fp;
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 		sctp_intf_event(sctp, addr, SCTP_ADDR_REMOVED, 0);
12577c478bd9Sstevel@tonic-gate 	} else {
12587c478bd9Sstevel@tonic-gate 		ASSERT(0);
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/* Successful, don't need to return anything. */
12627c478bd9Sstevel@tonic-gate 	return (NULL);
126345916cd2Sjpk 
126445916cd2Sjpk error_handler:
126545916cd2Sjpk 	mp = sctp_asconf_adderr(err, oph, cid);
126645916cd2Sjpk 	if (mp == NULL)
126745916cd2Sjpk 		*cont = -1;
126845916cd2Sjpk 	return (mp);
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate /*
12727c478bd9Sstevel@tonic-gate  * Handles both add and delete IP ACKs.
12737c478bd9Sstevel@tonic-gate  */
12747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12757c478bd9Sstevel@tonic-gate static void
12767c478bd9Sstevel@tonic-gate sctp_addip_ack(sctp_t *sctp, sctp_parm_hdr_t *ph, sctp_parm_hdr_t *oph,
12771d8c4025Svi     sctp_faddr_t *fp, in6_addr_t *laddr)
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
12807c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*sp;
12817c478bd9Sstevel@tonic-gate 	ipaddr_t		*addr4;
12827c478bd9Sstevel@tonic-gate 	boolean_t		backout = B_FALSE;
12837c478bd9Sstevel@tonic-gate 	uint16_t		type;
12847c478bd9Sstevel@tonic-gate 	uint32_t		*cid;
12857c478bd9Sstevel@tonic-gate 
12861d8c4025Svi 	/* could be an ASSERT */
12871d8c4025Svi 	if (laddr != NULL)
12881d8c4025Svi 		IN6_IPADDR_TO_V4MAPPED(0, laddr);
12891d8c4025Svi 
12907c478bd9Sstevel@tonic-gate 	/* If the peer doesn't understand Add-IP, remember it */
12917c478bd9Sstevel@tonic-gate 	if (ph != NULL && ph->sph_type == htons(PARM_UNRECOGNIZED)) {
12927c478bd9Sstevel@tonic-gate 		sctp->sctp_understands_addip = B_FALSE;
12937c478bd9Sstevel@tonic-gate 		backout = B_TRUE;
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	/*
12977c478bd9Sstevel@tonic-gate 	 * If OK, continue with the add / delete action, otherwise
12987c478bd9Sstevel@tonic-gate 	 * back out the action.
12997c478bd9Sstevel@tonic-gate 	 */
13007c478bd9Sstevel@tonic-gate 	if (ph != NULL && ph->sph_type != htons(PARM_SUCCESS)) {
13017c478bd9Sstevel@tonic-gate 		backout = B_TRUE;
1302*8dfac042SAnil udupa 		sctp_error_event(sctp, (sctp_chunk_hdr_t *)ph, B_TRUE);
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	type = ntohs(oph->sph_type);
13067c478bd9Sstevel@tonic-gate 	cid = (uint32_t *)(oph + 1);
13077c478bd9Sstevel@tonic-gate 	oph = (sctp_parm_hdr_t *)(cid + 1);
13087c478bd9Sstevel@tonic-gate 	if (oph->sph_type == htons(PARM_ADDR4)) {
13097c478bd9Sstevel@tonic-gate 		addr4 = (ipaddr_t *)(oph + 1);
13107c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(*addr4, &addr);
13117c478bd9Sstevel@tonic-gate 	} else {
13127c478bd9Sstevel@tonic-gate 		bcopy(oph + 1, &addr, sizeof (addr));
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate 
13151d8c4025Svi 	/* Signifies that the address was sucessfully processed */
13161d8c4025Svi 	if (!backout && laddr != NULL)
13171d8c4025Svi 		*laddr = addr;
13181d8c4025Svi 
13191d8c4025Svi 	sp = sctp_saddr_lookup(sctp, &addr, 0);
13207c478bd9Sstevel@tonic-gate 	ASSERT(sp != NULL);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	if (type == PARM_ADD_IP) {
13237c478bd9Sstevel@tonic-gate 		if (backout) {
13247c478bd9Sstevel@tonic-gate 			sctp_del_saddr(sctp, sp);
13257c478bd9Sstevel@tonic-gate 		} else {
13267c478bd9Sstevel@tonic-gate 			sp->saddr_ipif_dontsrc = 0;
13277c478bd9Sstevel@tonic-gate 		}
13287c478bd9Sstevel@tonic-gate 	} else if (type == PARM_DEL_IP) {
13297c478bd9Sstevel@tonic-gate 		if (backout) {
13307c478bd9Sstevel@tonic-gate 			sp->saddr_ipif_delete_pending = 0;
13317c478bd9Sstevel@tonic-gate 			sp->saddr_ipif_dontsrc = 0;
13327c478bd9Sstevel@tonic-gate 		} else {
13337c478bd9Sstevel@tonic-gate 			sctp_del_saddr(sctp, sp);
13347c478bd9Sstevel@tonic-gate 		}
13357c478bd9Sstevel@tonic-gate 	} else {
13367c478bd9Sstevel@tonic-gate 		/* Must be either PARM_ADD_IP or PARM_DEL_IP */
13377c478bd9Sstevel@tonic-gate 		ASSERT(0);
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13427c478bd9Sstevel@tonic-gate static mblk_t *
13437c478bd9Sstevel@tonic-gate sctp_setprim_req(sctp_t *sctp, sctp_parm_hdr_t *ph, uint32_t cid,
13441d8c4025Svi     sctp_faddr_t *fp, int *cont, int act, in6_addr_t *raddr)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	mblk_t *mp;
13477c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *oph;
13487c478bd9Sstevel@tonic-gate 	sctp_faddr_t *nfp;
13497c478bd9Sstevel@tonic-gate 	in6_addr_t addr;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	*cont = 1;
13527c478bd9Sstevel@tonic-gate 
1353*8dfac042SAnil udupa 	/* Does the peer understand ASCONF and Add-IP? */
1354*8dfac042SAnil udupa 	if (!sctp->sctp_understands_asconf || !sctp->sctp_understands_addip) {
1355*8dfac042SAnil udupa 		mp = sctp_asconf_adderr(SCTP_ERR_UNAUTHORIZED, ph, cid);
1356*8dfac042SAnil udupa 		if (mp == NULL) {
1357*8dfac042SAnil udupa 			*cont = -1;
1358*8dfac042SAnil udupa 		}
1359*8dfac042SAnil udupa 		return (mp);
1360*8dfac042SAnil udupa 	}
1361*8dfac042SAnil udupa 
13627c478bd9Sstevel@tonic-gate 	/* Check input */
13637c478bd9Sstevel@tonic-gate 	if (ntohs(ph->sph_len) < (sizeof (*ph) * 2)) {
13647c478bd9Sstevel@tonic-gate 		mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, ph, cid);
13657c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
13667c478bd9Sstevel@tonic-gate 			*cont = -1;
13677c478bd9Sstevel@tonic-gate 		}
13687c478bd9Sstevel@tonic-gate 		return (mp);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	oph = ph;
13727c478bd9Sstevel@tonic-gate 	ph = (sctp_parm_hdr_t *)((char *)ph + sizeof (sctp_parm_hdr_t) +
13737c478bd9Sstevel@tonic-gate 	    sizeof (cid));
13747c478bd9Sstevel@tonic-gate 	mp = sctp_check_addip_addr(ph, oph, cont, cid, &addr);
13757c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
13767c478bd9Sstevel@tonic-gate 		return (mp);
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	nfp = sctp_lookup_faddr(sctp, &addr);
13807c478bd9Sstevel@tonic-gate 	if (nfp == NULL) {
13817c478bd9Sstevel@tonic-gate 		/*
13827c478bd9Sstevel@tonic-gate 		 * Peer is trying to set an address that is not
13837c478bd9Sstevel@tonic-gate 		 * part of the association.
13847c478bd9Sstevel@tonic-gate 		 */
13857c478bd9Sstevel@tonic-gate 		dprint(1, ("setprim: addr not here: %x:%x:%x:%x\n",
13867c478bd9Sstevel@tonic-gate 		    SCTP_PRINTADDR(addr)));
13877c478bd9Sstevel@tonic-gate 		mp = sctp_asconf_adderr(SCTP_ERR_BAD_MANDPARM, oph, cid);
13887c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
13897c478bd9Sstevel@tonic-gate 			*cont = -1;
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 		return (mp);
13927c478bd9Sstevel@tonic-gate 	}
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	sctp_intf_event(sctp, addr, SCTP_ADDR_MADE_PRIM, 0);
13957c478bd9Sstevel@tonic-gate 	sctp->sctp_primary = nfp;
13967c478bd9Sstevel@tonic-gate 	if (nfp->state != SCTP_FADDRS_ALIVE || nfp == sctp->sctp_current) {
13977c478bd9Sstevel@tonic-gate 		return (NULL);
13987c478bd9Sstevel@tonic-gate 	}
139977c67f2fSkcpoon 	sctp_set_faddr_current(sctp, nfp);
14007c478bd9Sstevel@tonic-gate 	return (NULL);
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14047c478bd9Sstevel@tonic-gate static void
14057c478bd9Sstevel@tonic-gate sctp_setprim_ack(sctp_t *sctp, sctp_parm_hdr_t *ph, sctp_parm_hdr_t *oph,
14061d8c4025Svi     sctp_faddr_t *fp, in6_addr_t *laddr)
14077c478bd9Sstevel@tonic-gate {
14087c478bd9Sstevel@tonic-gate 	if (ph != NULL && ph->sph_type != htons(PARM_SUCCESS)) {
14097c478bd9Sstevel@tonic-gate 		/* If the peer doesn't understand Add-IP, remember it */
14107c478bd9Sstevel@tonic-gate 		if (ph->sph_type == htons(PARM_UNRECOGNIZED)) {
14117c478bd9Sstevel@tonic-gate 			sctp->sctp_understands_addip = B_FALSE;
14127c478bd9Sstevel@tonic-gate 		}
1413*8dfac042SAnil udupa 		sctp_error_event(sctp, (sctp_chunk_hdr_t *)ph, B_TRUE);
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	/* On success we do nothing */
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate int
14207c478bd9Sstevel@tonic-gate sctp_add_ip(sctp_t *sctp, const void *addrs, uint32_t cnt)
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
14237c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
14247c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
14257c478bd9Sstevel@tonic-gate 	int			error = 0;
14267c478bd9Sstevel@tonic-gate 	int			i;
14277c478bd9Sstevel@tonic-gate 	sctp_addip4_t		*ad4;
14287c478bd9Sstevel@tonic-gate 	sctp_addip6_t		*ad6;
14297c478bd9Sstevel@tonic-gate 	sctp_asconf_t		asc[1];
14307c478bd9Sstevel@tonic-gate 	uint16_t		type = htons(PARM_ADD_IP);
14317c478bd9Sstevel@tonic-gate 	boolean_t		v4mapped = B_FALSE;
14321d8c4025Svi 	sctp_cl_ainfo_t		*ainfo = NULL;
1433bd670b35SErik Nordmark 	conn_t			*connp = sctp->sctp_connp;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/* Does the peer understand ASCONF and Add-IP? */
14367c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_understands_asconf || !sctp->sctp_understands_addip)
14377c478bd9Sstevel@tonic-gate 		return (EOPNOTSUPP);
14387c478bd9Sstevel@tonic-gate 
14391d8c4025Svi 	/*
14401d8c4025Svi 	 * On a clustered node, we need to pass this list when
14411d8c4025Svi 	 * we get an ASCONF-ACK. We only pre-allocate memory for the
14421d8c4025Svi 	 * list, but fill in the addresses when it is processed
14431d8c4025Svi 	 * successfully after we get an ASCONF-ACK.
14441d8c4025Svi 	 */
14451d8c4025Svi 	if (cl_sctp_assoc_change != NULL) {
14461d8c4025Svi 		ainfo = kmem_zalloc(sizeof (*ainfo), KM_SLEEP);
14471d8c4025Svi 		/*
14481d8c4025Svi 		 * Reserve space for the list of new addresses
14491d8c4025Svi 		 */
14501d8c4025Svi 		ainfo->sctp_cl_asize = sizeof (in6_addr_t) * cnt;
14511d8c4025Svi 		ainfo->sctp_cl_alist = kmem_alloc(ainfo->sctp_cl_asize,
14521d8c4025Svi 		    KM_SLEEP);
14531d8c4025Svi 	}
14541d8c4025Svi 
14557c478bd9Sstevel@tonic-gate 	sctp_asconf_init(asc);
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	/*
14587c478bd9Sstevel@tonic-gate 	 * Screen addresses:
14597c478bd9Sstevel@tonic-gate 	 * If adding:
14607c478bd9Sstevel@tonic-gate 	 *   o Must not already be a part of the association
14617c478bd9Sstevel@tonic-gate 	 *   o Must be AF_INET or AF_INET6
14627c478bd9Sstevel@tonic-gate 	 *   o XXX Must be valid source address for this node
14637c478bd9Sstevel@tonic-gate 	 *   o Must be unicast
14647c478bd9Sstevel@tonic-gate 	 *   o XXX Must fit scoping rules
14657c478bd9Sstevel@tonic-gate 	 * If deleting:
14667c478bd9Sstevel@tonic-gate 	 *   o Must be part of the association
14677c478bd9Sstevel@tonic-gate 	 */
14687c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
1469bd670b35SErik Nordmark 		switch (connp->conn_family) {
14707c478bd9Sstevel@tonic-gate 		case AF_INET:
14717c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + i;
14727c478bd9Sstevel@tonic-gate 			v4mapped = B_TRUE;
14737c478bd9Sstevel@tonic-gate 			break;
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 		case AF_INET6:
14767c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + i;
14777c478bd9Sstevel@tonic-gate 			break;
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 		if (v4mapped) {
14817c478bd9Sstevel@tonic-gate 			mp = allocb(sizeof (*ad4), BPRI_MED);
14827c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
14837c478bd9Sstevel@tonic-gate 				error = ENOMEM;
14847c478bd9Sstevel@tonic-gate 				goto fail;
14857c478bd9Sstevel@tonic-gate 			}
14867c478bd9Sstevel@tonic-gate 			mp->b_wptr += sizeof (*ad4);
14877c478bd9Sstevel@tonic-gate 			ad4 = (sctp_addip4_t *)mp->b_rptr;
14887c478bd9Sstevel@tonic-gate 			ad4->sad4_addip_ph.sph_type = type;
14897c478bd9Sstevel@tonic-gate 			ad4->sad4_addip_ph.sph_len =
14907c478bd9Sstevel@tonic-gate 			    htons(sizeof (sctp_parm_hdr_t) +
14917c478bd9Sstevel@tonic-gate 			    PARM_ADDR4_LEN + sizeof (ad4->asconf_req_cid));
14927c478bd9Sstevel@tonic-gate 			ad4->sad4_addr4_ph.sph_type = htons(PARM_ADDR4);
14937c478bd9Sstevel@tonic-gate 			ad4->sad4_addr4_ph.sph_len = htons(PARM_ADDR4_LEN);
14947c478bd9Sstevel@tonic-gate 			ad4->sad4_addr = sin4->sin_addr.s_addr;
14957c478bd9Sstevel@tonic-gate 		} else {
14967c478bd9Sstevel@tonic-gate 			mp = allocb(sizeof (*ad6), BPRI_MED);
14977c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
14987c478bd9Sstevel@tonic-gate 				error = ENOMEM;
14997c478bd9Sstevel@tonic-gate 				goto fail;
15007c478bd9Sstevel@tonic-gate 			}
15017c478bd9Sstevel@tonic-gate 			mp->b_wptr += sizeof (*ad6);
15027c478bd9Sstevel@tonic-gate 			ad6 = (sctp_addip6_t *)mp->b_rptr;
15037c478bd9Sstevel@tonic-gate 			ad6->sad6_addip_ph.sph_type = type;
15047c478bd9Sstevel@tonic-gate 			ad6->sad6_addip_ph.sph_len =
15057c478bd9Sstevel@tonic-gate 			    htons(sizeof (sctp_parm_hdr_t) +
15067c478bd9Sstevel@tonic-gate 			    PARM_ADDR6_LEN + sizeof (ad6->asconf_req_cid));
15077c478bd9Sstevel@tonic-gate 			ad6->sad6_addr6_ph.sph_type = htons(PARM_ADDR6);
15087c478bd9Sstevel@tonic-gate 			ad6->sad6_addr6_ph.sph_len = htons(PARM_ADDR6_LEN);
15097c478bd9Sstevel@tonic-gate 			ad6->sad6_addr = sin6->sin6_addr;
15107c478bd9Sstevel@tonic-gate 		}
15117c478bd9Sstevel@tonic-gate 		error = sctp_asconf_add(asc, mp);
15127c478bd9Sstevel@tonic-gate 		if (error != 0)
15137c478bd9Sstevel@tonic-gate 			goto fail;
15147c478bd9Sstevel@tonic-gate 	}
15151d8c4025Svi 	error = sctp_asconf_send(sctp, asc, sctp->sctp_current, ainfo);
15167c478bd9Sstevel@tonic-gate 	if (error != 0)
15177c478bd9Sstevel@tonic-gate 		goto fail;
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	return (0);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate fail:
15221d8c4025Svi 	if (ainfo != NULL) {
15231d8c4025Svi 		kmem_free(ainfo->sctp_cl_alist, ainfo->sctp_cl_asize);
15241d8c4025Svi 		ainfo->sctp_cl_asize = 0;
15251d8c4025Svi 		kmem_free(ainfo, sizeof (*ainfo));
15261d8c4025Svi 	}
15277c478bd9Sstevel@tonic-gate 	sctp_asconf_destroy(asc);
15287c478bd9Sstevel@tonic-gate 	return (error);
15297c478bd9Sstevel@tonic-gate }
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate int
15321d8c4025Svi sctp_del_ip(sctp_t *sctp, const void *addrs, uint32_t cnt, uchar_t *ulist,
15331d8c4025Svi     size_t usize)
15347c478bd9Sstevel@tonic-gate {
15357c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
15367c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
15377c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
15387c478bd9Sstevel@tonic-gate 	int			error = 0;
15397c478bd9Sstevel@tonic-gate 	int			i;
15407c478bd9Sstevel@tonic-gate 	int			addrcnt = 0;
15417c478bd9Sstevel@tonic-gate 	sctp_addip4_t		*ad4;
15427c478bd9Sstevel@tonic-gate 	sctp_addip6_t		*ad6;
15437c478bd9Sstevel@tonic-gate 	sctp_asconf_t		asc[1];
15447c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t	*nsp;
15457c478bd9Sstevel@tonic-gate 	uint16_t		type = htons(PARM_DEL_IP);
15467c478bd9Sstevel@tonic-gate 	boolean_t		v4mapped = B_FALSE;
15477c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
15487c478bd9Sstevel@tonic-gate 	boolean_t		asconf = B_TRUE;
15491d8c4025Svi 	uint_t			ifindex;
15501d8c4025Svi 	sctp_cl_ainfo_t		*ainfo = NULL;
15511d8c4025Svi 	uchar_t			*p = ulist;
15521d8c4025Svi 	boolean_t		check_lport = B_FALSE;
1553f4b3ec61Sdh 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1554bd670b35SErik Nordmark 	conn_t			*connp = sctp->sctp_connp;
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/* Does the peer understand ASCONF and Add-IP? */
1557f4b3ec61Sdh 	if (sctp->sctp_state <= SCTPS_LISTEN || !sctps->sctps_addip_enabled ||
15587c478bd9Sstevel@tonic-gate 	    !sctp->sctp_understands_asconf || !sctp->sctp_understands_addip) {
15597c478bd9Sstevel@tonic-gate 		asconf = B_FALSE;
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 
15621d8c4025Svi 	if (sctp->sctp_state > SCTPS_BOUND)
15631d8c4025Svi 		check_lport = B_TRUE;
15641d8c4025Svi 
15651d8c4025Svi 	if (asconf) {
15661d8c4025Svi 		/*
15671d8c4025Svi 		 * On a clustered node, we need to pass this list when
15681d8c4025Svi 		 * we get an ASCONF-ACK. We only pre-allocate memory for the
15691d8c4025Svi 		 * list, but fill in the addresses when it is processed
15701d8c4025Svi 		 * successfully after we get an ASCONF-ACK.
15711d8c4025Svi 		 */
15721d8c4025Svi 		if (cl_sctp_assoc_change != NULL) {
15731d8c4025Svi 			ainfo = kmem_alloc(sizeof (*ainfo), KM_SLEEP);
15741d8c4025Svi 			ainfo->sctp_cl_dsize = sizeof (in6_addr_t) * cnt;
15751d8c4025Svi 			ainfo->sctp_cl_dlist = kmem_alloc(ainfo->sctp_cl_dsize,
15761d8c4025Svi 			    KM_SLEEP);
15771d8c4025Svi 		}
15787c478bd9Sstevel@tonic-gate 		sctp_asconf_init(asc);
15791d8c4025Svi 	}
15807c478bd9Sstevel@tonic-gate 	/*
15817c478bd9Sstevel@tonic-gate 	 * Screen addresses:
15827c478bd9Sstevel@tonic-gate 	 * If adding:
15837c478bd9Sstevel@tonic-gate 	 *   o Must not already be a part of the association
15847c478bd9Sstevel@tonic-gate 	 *   o Must be AF_INET or AF_INET6
15857c478bd9Sstevel@tonic-gate 	 *   o XXX Must be valid source address for this node
15867c478bd9Sstevel@tonic-gate 	 *   o Must be unicast
15877c478bd9Sstevel@tonic-gate 	 *   o XXX Must fit scoping rules
15887c478bd9Sstevel@tonic-gate 	 * If deleting:
15897c478bd9Sstevel@tonic-gate 	 *   o Must be part of the association
15907c478bd9Sstevel@tonic-gate 	 */
15917c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
15921d8c4025Svi 		ifindex = 0;
15931d8c4025Svi 
1594bd670b35SErik Nordmark 		switch (connp->conn_family) {
15957c478bd9Sstevel@tonic-gate 		case AF_INET:
15967c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + i;
1597bd670b35SErik Nordmark 			if (check_lport &&
1598bd670b35SErik Nordmark 			    sin4->sin_port != connp->conn_lport) {
15991d8c4025Svi 				error = EINVAL;
16001d8c4025Svi 				goto fail;
16011d8c4025Svi 			}
16027c478bd9Sstevel@tonic-gate 			v4mapped = B_TRUE;
16037c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(sin4->sin_addr.s_addr, &addr);
16047c478bd9Sstevel@tonic-gate 			break;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 		case AF_INET6:
16077c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + i;
16081d8c4025Svi 			if (check_lport &&
1609bd670b35SErik Nordmark 			    sin6->sin6_port != connp->conn_lport) {
16101d8c4025Svi 				error = EINVAL;
16111d8c4025Svi 				goto fail;
16121d8c4025Svi 			}
16137c478bd9Sstevel@tonic-gate 			addr = sin6->sin6_addr;
16141d8c4025Svi 			ifindex = sin6->sin6_scope_id;
16157c478bd9Sstevel@tonic-gate 			break;
16167c478bd9Sstevel@tonic-gate 		}
16171d8c4025Svi 		nsp = sctp_saddr_lookup(sctp, &addr, ifindex);
16187c478bd9Sstevel@tonic-gate 		if (nsp == NULL) {
16197c478bd9Sstevel@tonic-gate 			error = EADDRNOTAVAIL;
16207c478bd9Sstevel@tonic-gate 			goto fail;
16217c478bd9Sstevel@tonic-gate 		}
16227c478bd9Sstevel@tonic-gate 
16231d8c4025Svi 		/* Collect the list of addresses, if required */
16241d8c4025Svi 		if (usize >= sizeof (addr)) {
16251d8c4025Svi 			bcopy(&addr, p, sizeof (addr));
16261d8c4025Svi 			p += sizeof (addr);
16271d8c4025Svi 			usize -= sizeof (addr);
16281d8c4025Svi 		}
16297c478bd9Sstevel@tonic-gate 		if (!asconf)
16307c478bd9Sstevel@tonic-gate 			continue;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 		nsp->saddr_ipif_delete_pending = 1;
16337c478bd9Sstevel@tonic-gate 		nsp->saddr_ipif_dontsrc = 1;
16347c478bd9Sstevel@tonic-gate 		addrcnt++;
16357c478bd9Sstevel@tonic-gate 		if (v4mapped) {
16367c478bd9Sstevel@tonic-gate 			mp = allocb(sizeof (*ad4), BPRI_MED);
16377c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
16387c478bd9Sstevel@tonic-gate 				error = ENOMEM;
16397c478bd9Sstevel@tonic-gate 				goto fail;
16407c478bd9Sstevel@tonic-gate 			}
16417c478bd9Sstevel@tonic-gate 			mp->b_wptr += sizeof (*ad4);
16427c478bd9Sstevel@tonic-gate 			ad4 = (sctp_addip4_t *)mp->b_rptr;
16437c478bd9Sstevel@tonic-gate 			ad4->sad4_addip_ph.sph_type = type;
16447c478bd9Sstevel@tonic-gate 			ad4->sad4_addip_ph.sph_len =
16457c478bd9Sstevel@tonic-gate 			    htons(sizeof (sctp_parm_hdr_t) +
16467c478bd9Sstevel@tonic-gate 			    PARM_ADDR4_LEN + sizeof (ad4->asconf_req_cid));
16477c478bd9Sstevel@tonic-gate 			ad4->sad4_addr4_ph.sph_type = htons(PARM_ADDR4);
16487c478bd9Sstevel@tonic-gate 			ad4->sad4_addr4_ph.sph_len = htons(PARM_ADDR4_LEN);
16497c478bd9Sstevel@tonic-gate 			ad4->sad4_addr = sin4->sin_addr.s_addr;
16507c478bd9Sstevel@tonic-gate 		} else {
16517c478bd9Sstevel@tonic-gate 			mp = allocb(sizeof (*ad6), BPRI_MED);
16527c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
16537c478bd9Sstevel@tonic-gate 				error = ENOMEM;
16547c478bd9Sstevel@tonic-gate 				goto fail;
16557c478bd9Sstevel@tonic-gate 			}
16567c478bd9Sstevel@tonic-gate 			mp->b_wptr += sizeof (*ad6);
16577c478bd9Sstevel@tonic-gate 			ad6 = (sctp_addip6_t *)mp->b_rptr;
16587c478bd9Sstevel@tonic-gate 			ad6->sad6_addip_ph.sph_type = type;
16597c478bd9Sstevel@tonic-gate 			ad6->sad6_addip_ph.sph_len =
16607c478bd9Sstevel@tonic-gate 			    htons(sizeof (sctp_parm_hdr_t) + PARM_ADDR6_LEN +
16617c478bd9Sstevel@tonic-gate 			    sizeof (ad6->asconf_req_cid));
16627c478bd9Sstevel@tonic-gate 			ad6->sad6_addr6_ph.sph_type = htons(PARM_ADDR6);
16637c478bd9Sstevel@tonic-gate 			ad6->sad6_addr6_ph.sph_len = htons(PARM_ADDR6_LEN);
16647c478bd9Sstevel@tonic-gate 			ad6->sad6_addr = addr;
16657c478bd9Sstevel@tonic-gate 		}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 		error = sctp_asconf_add(asc, mp);
16687c478bd9Sstevel@tonic-gate 		if (error != 0)
16697c478bd9Sstevel@tonic-gate 			goto fail;
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	if (!asconf) {
16737c478bd9Sstevel@tonic-gate 		sctp_del_saddr_list(sctp, addrs, cnt, B_FALSE);
16747c478bd9Sstevel@tonic-gate 		return (0);
16757c478bd9Sstevel@tonic-gate 	}
16761d8c4025Svi 	error = sctp_asconf_send(sctp, asc, sctp->sctp_current, ainfo);
16777c478bd9Sstevel@tonic-gate 	if (error != 0)
16787c478bd9Sstevel@tonic-gate 		goto fail;
16797c478bd9Sstevel@tonic-gate 	sctp_redo_faddr_srcs(sctp);
16807c478bd9Sstevel@tonic-gate 	return (0);
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate fail:
16831d8c4025Svi 	if (ainfo != NULL) {
16841d8c4025Svi 		kmem_free(ainfo->sctp_cl_dlist, ainfo->sctp_cl_dsize);
16851d8c4025Svi 		ainfo->sctp_cl_dsize = 0;
16861d8c4025Svi 		kmem_free(ainfo, sizeof (*ainfo));
16871d8c4025Svi 	}
16887c478bd9Sstevel@tonic-gate 	if (!asconf)
16897c478bd9Sstevel@tonic-gate 		return (error);
16907c478bd9Sstevel@tonic-gate 	for (i = 0; i < addrcnt; i++) {
16911d8c4025Svi 		ifindex = 0;
16921d8c4025Svi 
1693bd670b35SErik Nordmark 		switch (connp->conn_family) {
16947c478bd9Sstevel@tonic-gate 		case AF_INET:
16957c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)addrs + i;
16967c478bd9Sstevel@tonic-gate 			IN6_INADDR_TO_V4MAPPED(&(sin4->sin_addr), &addr);
16977c478bd9Sstevel@tonic-gate 			break;
16987c478bd9Sstevel@tonic-gate 		case AF_INET6:
16997c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addrs + i;
17007c478bd9Sstevel@tonic-gate 			addr = sin6->sin6_addr;
17011d8c4025Svi 			ifindex = sin6->sin6_scope_id;
17027c478bd9Sstevel@tonic-gate 			break;
17037c478bd9Sstevel@tonic-gate 		}
17041d8c4025Svi 		nsp = sctp_saddr_lookup(sctp, &addr, ifindex);
17057c478bd9Sstevel@tonic-gate 		ASSERT(nsp != NULL);
17067c478bd9Sstevel@tonic-gate 		nsp->saddr_ipif_delete_pending = 0;
17077c478bd9Sstevel@tonic-gate 		nsp->saddr_ipif_dontsrc = 0;
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 	sctp_asconf_destroy(asc);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	return (error);
17127c478bd9Sstevel@tonic-gate }
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate int
1715bd670b35SErik Nordmark sctp_set_peerprim(sctp_t *sctp, const void *inp)
17167c478bd9Sstevel@tonic-gate {
17177c478bd9Sstevel@tonic-gate 	const struct sctp_setprim	*prim = inp;
17187c478bd9Sstevel@tonic-gate 	const struct sockaddr_storage	*ss;
17197c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
17207c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
17217c478bd9Sstevel@tonic-gate 	in6_addr_t addr;
17227c478bd9Sstevel@tonic-gate 	mblk_t *mp;
17237c478bd9Sstevel@tonic-gate 	sctp_saddr_ipif_t *sp;
17247c478bd9Sstevel@tonic-gate 	sctp_addip4_t *ad4;
17257c478bd9Sstevel@tonic-gate 	sctp_addip6_t *ad6;
17267c478bd9Sstevel@tonic-gate 	sctp_asconf_t asc[1];
17277c478bd9Sstevel@tonic-gate 	int error = 0;
17281d8c4025Svi 	uint_t	ifindex = 0;
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/* Does the peer understand ASCONF and Add-IP? */
17317c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_understands_asconf || !sctp->sctp_understands_addip) {
17327c478bd9Sstevel@tonic-gate 		return (EOPNOTSUPP);
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	/* Don't do anything if we are not connected */
17367c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state != SCTPS_ESTABLISHED)
17377c478bd9Sstevel@tonic-gate 		return (EINVAL);
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	ss = &prim->ssp_addr;
17407c478bd9Sstevel@tonic-gate 	sin = NULL;
17417c478bd9Sstevel@tonic-gate 	sin6 = NULL;
17427c478bd9Sstevel@tonic-gate 	if (ss->ss_family == AF_INET) {
17437c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)ss;
17447c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &addr);
17457c478bd9Sstevel@tonic-gate 	} else if (ss->ss_family == AF_INET6) {
17467c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)ss;
17477c478bd9Sstevel@tonic-gate 		addr = sin6->sin6_addr;
17481d8c4025Svi 		ifindex = sin6->sin6_scope_id;
17497c478bd9Sstevel@tonic-gate 	} else {
17507c478bd9Sstevel@tonic-gate 		return (EAFNOSUPPORT);
17517c478bd9Sstevel@tonic-gate 	}
17521d8c4025Svi 	sp = sctp_saddr_lookup(sctp, &addr, ifindex);
17537c478bd9Sstevel@tonic-gate 	if (sp == NULL)
17547c478bd9Sstevel@tonic-gate 		return (EADDRNOTAVAIL);
17557c478bd9Sstevel@tonic-gate 	sctp_asconf_init(asc);
17567c478bd9Sstevel@tonic-gate 	if (sin) {
17577c478bd9Sstevel@tonic-gate 		mp = allocb(sizeof (*ad4), BPRI_MED);
17587c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
17597c478bd9Sstevel@tonic-gate 			error = ENOMEM;
17607c478bd9Sstevel@tonic-gate 			goto fail;
17617c478bd9Sstevel@tonic-gate 		}
17627c478bd9Sstevel@tonic-gate 		mp->b_wptr += sizeof (*ad4);
17637c478bd9Sstevel@tonic-gate 		ad4 = (sctp_addip4_t *)mp->b_rptr;
17647c478bd9Sstevel@tonic-gate 		ad4->sad4_addip_ph.sph_type = htons(PARM_SET_PRIMARY);
17657c478bd9Sstevel@tonic-gate 		ad4->sad4_addip_ph.sph_len = htons(sizeof (sctp_parm_hdr_t) +
17667c478bd9Sstevel@tonic-gate 		    PARM_ADDR4_LEN + sizeof (ad4->asconf_req_cid));
17677c478bd9Sstevel@tonic-gate 		ad4->sad4_addr4_ph.sph_type = htons(PARM_ADDR4);
17687c478bd9Sstevel@tonic-gate 		ad4->sad4_addr4_ph.sph_len = htons(PARM_ADDR4_LEN);
17697c478bd9Sstevel@tonic-gate 		ad4->sad4_addr = sin->sin_addr.s_addr;
17707c478bd9Sstevel@tonic-gate 	} else {
17717c478bd9Sstevel@tonic-gate 		mp = allocb(sizeof (*ad6), BPRI_MED);
17727c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
17737c478bd9Sstevel@tonic-gate 			error = ENOMEM;
17747c478bd9Sstevel@tonic-gate 			goto fail;
17757c478bd9Sstevel@tonic-gate 		}
17767c478bd9Sstevel@tonic-gate 		mp->b_wptr += sizeof (*ad6);
17777c478bd9Sstevel@tonic-gate 		ad6 = (sctp_addip6_t *)mp->b_rptr;
17787c478bd9Sstevel@tonic-gate 		ad6->sad6_addip_ph.sph_type = htons(PARM_SET_PRIMARY);
17797c478bd9Sstevel@tonic-gate 		ad6->sad6_addip_ph.sph_len = htons(sizeof (sctp_parm_hdr_t) +
17807c478bd9Sstevel@tonic-gate 		    PARM_ADDR6_LEN + sizeof (ad6->asconf_req_cid));
17817c478bd9Sstevel@tonic-gate 		ad6->sad6_addr6_ph.sph_type = htons(PARM_ADDR6);
17827c478bd9Sstevel@tonic-gate 		ad6->sad6_addr6_ph.sph_len = htons(PARM_ADDR6_LEN);
17837c478bd9Sstevel@tonic-gate 		ad6->sad6_addr = sin6->sin6_addr;
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	error = sctp_asconf_add(asc, mp);
17877c478bd9Sstevel@tonic-gate 	if (error != 0) {
17887c478bd9Sstevel@tonic-gate 		goto fail;
17897c478bd9Sstevel@tonic-gate 	}
17907c478bd9Sstevel@tonic-gate 
17911d8c4025Svi 	error = sctp_asconf_send(sctp, asc, sctp->sctp_current, NULL);
17927c478bd9Sstevel@tonic-gate 	if (error == 0) {
17937c478bd9Sstevel@tonic-gate 		return (0);
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate fail:
17977c478bd9Sstevel@tonic-gate 	sctp_asconf_destroy(asc);
17987c478bd9Sstevel@tonic-gate 	return (error);
17997c478bd9Sstevel@tonic-gate }
1800