sctp_asconf.c revision 170091
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs
31163953Srrs/* $KAME: sctp_asconf.c,v 1.24 2005/03/06 16:04:16 itojun Exp $	 */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163957Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_asconf.c 170091 2007-05-29 09:29:03Z rrs $");
35163953Srrs#include <netinet/sctp_os.h>
36163953Srrs#include <netinet/sctp_var.h>
37167598Srrs#include <netinet/sctp_sysctl.h>
38163953Srrs#include <netinet/sctp_pcb.h>
39163953Srrs#include <netinet/sctp_header.h>
40163953Srrs#include <netinet/sctputil.h>
41163953Srrs#include <netinet/sctp_output.h>
42163953Srrs#include <netinet/sctp_asconf.h>
43163953Srrs
44163953Srrs/*
45163953Srrs * debug flags:
46163953Srrs * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
47163953Srrs * SCTP_DEBUG_ASCONF2: detailed info
48163953Srrs */
49163953Srrs#ifdef SCTP_DEBUG
50163953Srrs#endif				/* SCTP_DEBUG */
51163953Srrs
52163953Srrs
53169420Srrsstatic void
54163953Srrssctp_asconf_get_source_ip(struct mbuf *m, struct sockaddr *sa)
55163953Srrs{
56163953Srrs	struct ip *iph;
57163953Srrs	struct sockaddr_in *sin;
58163953Srrs
59163953Srrs#ifdef INET6
60163953Srrs	struct sockaddr_in6 *sin6;
61163953Srrs
62163953Srrs#endif
63163953Srrs
64163953Srrs	iph = mtod(m, struct ip *);
65163953Srrs	if (iph->ip_v == IPVERSION) {
66163953Srrs		/* IPv4 source */
67163953Srrs		sin = (struct sockaddr_in *)sa;
68163953Srrs		bzero(sin, sizeof(*sin));
69163953Srrs		sin->sin_family = AF_INET;
70163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
71163953Srrs		sin->sin_port = 0;
72163953Srrs		sin->sin_addr.s_addr = iph->ip_src.s_addr;
73169420Srrs		return;
74163953Srrs	}
75163953Srrs#ifdef INET6
76163953Srrs	else if (iph->ip_v == (IPV6_VERSION >> 4)) {
77163953Srrs		/* IPv6 source */
78163953Srrs		struct ip6_hdr *ip6;
79163953Srrs
80163953Srrs		sin6 = (struct sockaddr_in6 *)sa;
81163953Srrs		bzero(sin6, sizeof(*sin6));
82163953Srrs		sin6->sin6_family = AF_INET6;
83163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
84163953Srrs		sin6->sin6_port = 0;
85163953Srrs		ip6 = mtod(m, struct ip6_hdr *);
86163953Srrs		sin6->sin6_addr = ip6->ip6_src;
87169420Srrs		return;
88163953Srrs	}
89163953Srrs#endif				/* INET6 */
90163953Srrs	else
91169420Srrs		return;
92163953Srrs}
93163953Srrs
94163953Srrs/*
95163953Srrs * draft-ietf-tsvwg-addip-sctp
96163953Srrs *
97163953Srrs * Address management only currently supported For the bound all case: the asoc
98163953Srrs * local addr list is always a "DO NOT USE" list For the subset bound case:
99163953Srrs * If ASCONFs are allowed: the endpoint local addr list is the usable address
100163953Srrs * list the asoc local addr list is the "DO NOT USE" list If ASCONFs are not
101163953Srrs * allowed: the endpoint local addr list is the default usable list the asoc
102163953Srrs * local addr list is the usable address list
103163953Srrs *
104163953Srrs * An ASCONF parameter queue exists per asoc which holds the pending address
105163953Srrs * operations.  Lists are updated upon receipt of ASCONF-ACK.
106163953Srrs *
107163953Srrs * Deleted addresses are always immediately removed from the lists as they will
108163953Srrs * (shortly) no longer exist in the kernel.  We send ASCONFs as a courtesy,
109163953Srrs * only if allowed.
110163953Srrs */
111163953Srrs
112163953Srrs/*
113163953Srrs * ASCONF parameter processing response_required: set if a reply is required
114163953Srrs * (eg. SUCCESS_REPORT) returns a mbuf to an "error" response parameter or
115163953Srrs * NULL/"success" if ok FIX: allocating this many mbufs on the fly is pretty
116163953Srrs * inefficient...
117163953Srrs */
118163953Srrsstatic struct mbuf *
119163953Srrssctp_asconf_success_response(uint32_t id)
120163953Srrs{
121163953Srrs	struct mbuf *m_reply = NULL;
122163953Srrs	struct sctp_asconf_paramhdr *aph;
123163953Srrs
124163953Srrs	m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
125163953Srrs	    0, M_DONTWAIT, 1, MT_DATA);
126163953Srrs	if (m_reply == NULL) {
127169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
128169420Srrs		    "asconf_success_response: couldn't get mbuf!\n");
129163953Srrs		return NULL;
130163953Srrs	}
131163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
132163953Srrs	aph->correlation_id = id;
133163953Srrs	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
134163953Srrs	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
135165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
136163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
137163953Srrs
138163953Srrs	return m_reply;
139163953Srrs}
140163953Srrs
141163953Srrsstatic struct mbuf *
142163953Srrssctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t * error_tlv,
143163953Srrs    uint16_t tlv_length)
144163953Srrs{
145163953Srrs	struct mbuf *m_reply = NULL;
146163953Srrs	struct sctp_asconf_paramhdr *aph;
147163953Srrs	struct sctp_error_cause *error;
148163953Srrs	uint8_t *tlv;
149163953Srrs
150163953Srrs	m_reply = sctp_get_mbuf_for_msg((sizeof(struct sctp_asconf_paramhdr) +
151163953Srrs	    tlv_length +
152163953Srrs	    sizeof(struct sctp_error_cause)),
153163953Srrs	    0, M_DONTWAIT, 1, MT_DATA);
154163953Srrs	if (m_reply == NULL) {
155169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
156169420Srrs		    "asconf_error_response: couldn't get mbuf!\n");
157163953Srrs		return NULL;
158163953Srrs	}
159163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
160163953Srrs	error = (struct sctp_error_cause *)(aph + 1);
161163953Srrs
162163953Srrs	aph->correlation_id = id;
163163953Srrs	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
164163953Srrs	error->code = htons(cause);
165163953Srrs	error->length = tlv_length + sizeof(struct sctp_error_cause);
166163953Srrs	aph->ph.param_length = error->length +
167163953Srrs	    sizeof(struct sctp_asconf_paramhdr);
168163953Srrs
169163953Srrs	if (aph->ph.param_length > MLEN) {
170169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
171169420Srrs		    "asconf_error_response: tlv_length (%xh) too big\n",
172169420Srrs		    tlv_length);
173163953Srrs		sctp_m_freem(m_reply);	/* discard */
174163953Srrs		return NULL;
175163953Srrs	}
176163953Srrs	if (error_tlv != NULL) {
177163953Srrs		tlv = (uint8_t *) (error + 1);
178163953Srrs		memcpy(tlv, error_tlv, tlv_length);
179163953Srrs	}
180165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
181163953Srrs	error->length = htons(error->length);
182163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
183163953Srrs
184163953Srrs	return m_reply;
185163953Srrs}
186163953Srrs
187163953Srrsstatic struct mbuf *
188163953Srrssctp_process_asconf_add_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
189163953Srrs    struct sctp_tcb *stcb, int response_required)
190163953Srrs{
191163953Srrs	struct mbuf *m_reply = NULL;
192163953Srrs	struct sockaddr_storage sa_source, sa_store;
193163953Srrs	struct sctp_ipv4addr_param *v4addr;
194163953Srrs	uint16_t param_type, param_length, aparam_length;
195163953Srrs	struct sockaddr *sa;
196163953Srrs	struct sockaddr_in *sin;
197163953Srrs	int zero_address = 0;
198163953Srrs
199163953Srrs#ifdef INET6
200163953Srrs	struct sockaddr_in6 *sin6;
201163953Srrs	struct sctp_ipv6addr_param *v6addr;
202163953Srrs
203163953Srrs#endif				/* INET6 */
204163953Srrs
205163953Srrs	aparam_length = ntohs(aph->ph.param_length);
206163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
207163953Srrs#ifdef INET6
208163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
209163953Srrs#endif				/* INET6 */
210163953Srrs	param_type = ntohs(v4addr->ph.param_type);
211163953Srrs	param_length = ntohs(v4addr->ph.param_length);
212163953Srrs
213163953Srrs	sa = (struct sockaddr *)&sa_store;
214163953Srrs	switch (param_type) {
215163953Srrs	case SCTP_IPV4_ADDRESS:
216163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
217163953Srrs			/* invalid param size */
218163953Srrs			return NULL;
219163953Srrs		}
220163953Srrs		sin = (struct sockaddr_in *)&sa_store;
221163953Srrs		bzero(sin, sizeof(*sin));
222163953Srrs		sin->sin_family = AF_INET;
223163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
224163953Srrs		sin->sin_port = stcb->rport;
225163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
226163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
227163953Srrs			zero_address = 1;
228169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
229169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
230163953Srrs		break;
231163953Srrs	case SCTP_IPV6_ADDRESS:
232163953Srrs#ifdef INET6
233163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
234163953Srrs			/* invalid param size */
235163953Srrs			return NULL;
236163953Srrs		}
237163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
238163953Srrs		bzero(sin6, sizeof(*sin6));
239163953Srrs		sin6->sin6_family = AF_INET6;
240163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
241163953Srrs		sin6->sin6_port = stcb->rport;
242163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
243163953Srrs		    sizeof(struct in6_addr));
244163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
245163953Srrs			zero_address = 1;
246169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
247169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
248163953Srrs#else
249163953Srrs		/* IPv6 not enabled! */
250163953Srrs		/* FIX ME: currently sends back an invalid param error */
251163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
252163953Srrs		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph, aparam_length);
253169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
254169420Srrs		    "process_asconf_add_ip: v6 disabled- skipping ");
255169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
256163953Srrs		return m_reply;
257169420Srrs#endif
258163953Srrs		break;
259163953Srrs	default:
260163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
261163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
262163953Srrs		    aparam_length);
263163953Srrs		return m_reply;
264163953Srrs	}			/* end switch */
265163953Srrs
266163953Srrs	/* if 0.0.0.0/::0, add the source address instead */
267165647Srrs	if (zero_address && sctp_nat_friendly) {
268163953Srrs		sa = (struct sockaddr *)&sa_source;
269163953Srrs		sctp_asconf_get_source_ip(m, sa);
270169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
271169420Srrs		    "process_asconf_add_ip: using source addr ");
272169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
273163953Srrs	}
274163953Srrs	/* add the address */
275165220Srrs	if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE,
276165220Srrs	    SCTP_ADDR_DYNAMIC_ADDED) != 0) {
277169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
278169420Srrs		    "process_asconf_add_ip: error adding address\n");
279163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
280163953Srrs		    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
281163953Srrs		    aparam_length);
282163953Srrs	} else {
283163953Srrs		/* notify upper layer */
284163953Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa);
285163953Srrs		if (response_required) {
286163953Srrs			m_reply =
287163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
288163953Srrs		}
289169655Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
290169655Srrs		    NULL, SCTP_FROM_SCTP_ASCONF + SCTP_LOC_1);
291169655Srrs		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
292169655Srrs		    stcb, NULL);
293163953Srrs	}
294163953Srrs
295163953Srrs	return m_reply;
296163953Srrs}
297163953Srrs
298163953Srrsstatic int
299163953Srrssctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb,
300163953Srrs    struct sockaddr *src)
301163953Srrs{
302163953Srrs	struct sctp_nets *src_net, *net;
303163953Srrs
304163953Srrs	/* make sure the source address exists as a destination net */
305163953Srrs	src_net = sctp_findnet(stcb, src);
306163953Srrs	if (src_net == NULL) {
307163953Srrs		/* not found */
308163953Srrs		return -1;
309163953Srrs	}
310163953Srrs	/* delete all destination addresses except the source */
311163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
312163953Srrs		if (net != src_net) {
313163953Srrs			/* delete this address */
314163953Srrs			sctp_remove_net(stcb, net);
315169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
316169420Srrs			    "asconf_del_remote_addrs_except: deleting ");
317169420Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
318169420Srrs			    (struct sockaddr *)&net->ro._l_addr);
319163953Srrs			/* notify upper layer */
320163953Srrs			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
321163953Srrs			    (struct sockaddr *)&net->ro._l_addr);
322163953Srrs		}
323163953Srrs	}
324163953Srrs	return 0;
325163953Srrs}
326163953Srrs
327163953Srrsstatic struct mbuf *
328163953Srrssctp_process_asconf_delete_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
329163953Srrs    struct sctp_tcb *stcb, int response_required)
330163953Srrs{
331163953Srrs	struct mbuf *m_reply = NULL;
332163953Srrs	struct sockaddr_storage sa_source, sa_store;
333163953Srrs	struct sctp_ipv4addr_param *v4addr;
334163953Srrs	uint16_t param_type, param_length, aparam_length;
335163953Srrs	struct sockaddr *sa;
336163953Srrs	struct sockaddr_in *sin;
337163953Srrs	int zero_address = 0;
338163953Srrs	int result;
339163953Srrs
340163953Srrs#ifdef INET6
341163953Srrs	struct sockaddr_in6 *sin6;
342163953Srrs	struct sctp_ipv6addr_param *v6addr;
343163953Srrs
344163953Srrs#endif				/* INET6 */
345163953Srrs
346163953Srrs	/* get the source IP address for src and 0.0.0.0/::0 delete checks */
347163953Srrs	sctp_asconf_get_source_ip(m, (struct sockaddr *)&sa_source);
348163953Srrs
349163953Srrs	aparam_length = ntohs(aph->ph.param_length);
350163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
351163953Srrs#ifdef INET6
352163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
353163953Srrs#endif				/* INET6 */
354163953Srrs	param_type = ntohs(v4addr->ph.param_type);
355163953Srrs	param_length = ntohs(v4addr->ph.param_length);
356163953Srrs
357163953Srrs	sa = (struct sockaddr *)&sa_store;
358163953Srrs	switch (param_type) {
359163953Srrs	case SCTP_IPV4_ADDRESS:
360163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
361163953Srrs			/* invalid param size */
362163953Srrs			return NULL;
363163953Srrs		}
364163953Srrs		sin = (struct sockaddr_in *)&sa_store;
365163953Srrs		bzero(sin, sizeof(*sin));
366163953Srrs		sin->sin_family = AF_INET;
367163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
368163953Srrs		sin->sin_port = stcb->rport;
369163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
370163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
371163953Srrs			zero_address = 1;
372169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
373169420Srrs		    "process_asconf_delete_ip: deleting ");
374169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
375163953Srrs		break;
376163953Srrs	case SCTP_IPV6_ADDRESS:
377163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
378163953Srrs			/* invalid param size */
379163953Srrs			return NULL;
380163953Srrs		}
381163953Srrs#ifdef INET6
382163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
383163953Srrs		bzero(sin6, sizeof(*sin6));
384163953Srrs		sin6->sin6_family = AF_INET6;
385163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
386163953Srrs		sin6->sin6_port = stcb->rport;
387163953Srrs		memcpy(&sin6->sin6_addr, v6addr->addr,
388163953Srrs		    sizeof(struct in6_addr));
389163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
390163953Srrs			zero_address = 1;
391169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
392169420Srrs		    "process_asconf_delete_ip: deleting ");
393169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
394163953Srrs#else
395163953Srrs		/* IPv6 not enabled!  No "action" needed; just ack it */
396169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
397169420Srrs		    "process_asconf_delete_ip: v6 disabled- ignoring: ");
398169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
399163953Srrs		/* just respond with a "success" ASCONF-ACK */
400163953Srrs		return NULL;
401169420Srrs#endif
402163953Srrs		break;
403163953Srrs	default:
404163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
405163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
406163953Srrs		    aparam_length);
407163953Srrs		return m_reply;
408163953Srrs	}
409163953Srrs
410163953Srrs	/* make sure the source address is not being deleted */
411163953Srrs	if (sctp_cmpaddr(sa, (struct sockaddr *)&sa_source)) {
412163953Srrs		/* trying to delete the source address! */
413169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
414163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
415163953Srrs		    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
416163953Srrs		    aparam_length);
417163953Srrs		return m_reply;
418163953Srrs	}
419163953Srrs	/* if deleting 0.0.0.0/::0, delete all addresses except src addr */
420165647Srrs	if (zero_address && sctp_nat_friendly) {
421163953Srrs		result = sctp_asconf_del_remote_addrs_except(stcb,
422163953Srrs		    (struct sockaddr *)&sa_source);
423163953Srrs
424163953Srrs		if (result) {
425163953Srrs			/* src address did not exist? */
426169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
427163953Srrs			/* what error to reply with?? */
428163953Srrs			m_reply =
429163953Srrs			    sctp_asconf_error_response(aph->correlation_id,
430163953Srrs			    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
431163953Srrs			    aparam_length);
432163953Srrs		} else if (response_required) {
433163953Srrs			m_reply =
434163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
435163953Srrs		}
436163953Srrs		return m_reply;
437163953Srrs	}
438163953Srrs	/* delete the address */
439163953Srrs	result = sctp_del_remote_addr(stcb, sa);
440163953Srrs	/*
441163953Srrs	 * note if result == -2, the address doesn't exist in the asoc but
442163953Srrs	 * since it's being deleted anyways, we just ack the delete -- but
443163953Srrs	 * this probably means something has already gone awry
444163953Srrs	 */
445163953Srrs	if (result == -1) {
446163953Srrs		/* only one address in the asoc */
447169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
448163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
449163953Srrs		    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
450163953Srrs		    aparam_length);
451163953Srrs	} else {
452163953Srrs		if (response_required) {
453163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
454163953Srrs		}
455163953Srrs		/* notify upper layer */
456163953Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa);
457163953Srrs	}
458163953Srrs	return m_reply;
459163953Srrs}
460163953Srrs
461163953Srrsstatic struct mbuf *
462163953Srrssctp_process_asconf_set_primary(struct mbuf *m,
463163953Srrs    struct sctp_asconf_paramhdr *aph, struct sctp_tcb *stcb,
464163953Srrs    int response_required)
465163953Srrs{
466163953Srrs	struct mbuf *m_reply = NULL;
467163953Srrs	struct sockaddr_storage sa_source, sa_store;
468163953Srrs	struct sctp_ipv4addr_param *v4addr;
469163953Srrs	uint16_t param_type, param_length, aparam_length;
470163953Srrs	struct sockaddr *sa;
471163953Srrs	struct sockaddr_in *sin;
472163953Srrs	int zero_address = 0;
473163953Srrs
474163953Srrs#ifdef INET6
475163953Srrs	struct sockaddr_in6 *sin6;
476163953Srrs	struct sctp_ipv6addr_param *v6addr;
477163953Srrs
478163953Srrs#endif				/* INET6 */
479163953Srrs
480163953Srrs	aparam_length = ntohs(aph->ph.param_length);
481163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
482163953Srrs#ifdef INET6
483163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
484163953Srrs#endif				/* INET6 */
485163953Srrs	param_type = ntohs(v4addr->ph.param_type);
486163953Srrs	param_length = ntohs(v4addr->ph.param_length);
487163953Srrs
488163953Srrs	sa = (struct sockaddr *)&sa_store;
489163953Srrs	switch (param_type) {
490163953Srrs	case SCTP_IPV4_ADDRESS:
491163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
492163953Srrs			/* invalid param size */
493163953Srrs			return NULL;
494163953Srrs		}
495163953Srrs		sin = (struct sockaddr_in *)&sa_store;
496163953Srrs		bzero(sin, sizeof(*sin));
497163953Srrs		sin->sin_family = AF_INET;
498163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
499163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
500163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
501163953Srrs			zero_address = 1;
502169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
503169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
504163953Srrs		break;
505163953Srrs	case SCTP_IPV6_ADDRESS:
506163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
507163953Srrs			/* invalid param size */
508163953Srrs			return NULL;
509163953Srrs		}
510163953Srrs#ifdef INET6
511163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
512163953Srrs		bzero(sin6, sizeof(*sin6));
513163953Srrs		sin6->sin6_family = AF_INET6;
514163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
515163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
516163953Srrs		    sizeof(struct in6_addr));
517163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
518163953Srrs			zero_address = 1;
519169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
520169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
521163953Srrs#else
522163953Srrs		/* IPv6 not enabled!  No "action" needed; just ack it */
523169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
524169420Srrs		    "process_asconf_set_primary: v6 disabled- ignoring: ");
525169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
526163953Srrs		/* just respond with a "success" ASCONF-ACK */
527163953Srrs		return NULL;
528169420Srrs#endif
529163953Srrs		break;
530163953Srrs	default:
531163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
532163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
533163953Srrs		    aparam_length);
534163953Srrs		return m_reply;
535163953Srrs	}
536163953Srrs
537163953Srrs	/* if 0.0.0.0/::0, use the source address instead */
538165647Srrs	if (zero_address && sctp_nat_friendly) {
539163953Srrs		sa = (struct sockaddr *)&sa_source;
540163953Srrs		sctp_asconf_get_source_ip(m, sa);
541169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
542169420Srrs		    "process_asconf_set_primary: using source addr ");
543169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
544163953Srrs	}
545163953Srrs	/* set the primary address */
546163953Srrs	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
547169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
548169420Srrs		    "process_asconf_set_primary: primary address set\n");
549163953Srrs		/* notify upper layer */
550163953Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa);
551163953Srrs
552163953Srrs		if (response_required) {
553163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
554163953Srrs		}
555163953Srrs	} else {
556163953Srrs		/* couldn't set the requested primary address! */
557169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
558169420Srrs		    "process_asconf_set_primary: set primary failed!\n");
559163953Srrs		/* must have been an invalid address, so report */
560163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
561163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
562163953Srrs		    aparam_length);
563163953Srrs	}
564163953Srrs
565163953Srrs	return m_reply;
566163953Srrs}
567163953Srrs
568163953Srrs/*
569163953Srrs * handles an ASCONF chunk.
570163953Srrs * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
571163953Srrs */
572163953Srrsvoid
573163953Srrssctp_handle_asconf(struct mbuf *m, unsigned int offset,
574163953Srrs    struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb)
575163953Srrs{
576163953Srrs	struct sctp_association *asoc;
577163953Srrs	uint32_t serial_num;
578163953Srrs	struct mbuf *m_ack, *m_result, *m_tail;
579163953Srrs	struct sctp_asconf_ack_chunk *ack_cp;
580163953Srrs	struct sctp_asconf_paramhdr *aph, *ack_aph;
581163953Srrs	struct sctp_ipv6addr_param *p_addr;
582163953Srrs	unsigned int asconf_limit;
583163953Srrs	int error = 0;		/* did an error occur? */
584163953Srrs
585163953Srrs	/* asconf param buffer */
586166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
587163953Srrs
588163953Srrs	/* verify minimum length */
589163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
590169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
591169420Srrs		    "handle_asconf: chunk too small = %xh\n",
592169420Srrs		    ntohs(cp->ch.chunk_length));
593163953Srrs		return;
594163953Srrs	}
595163953Srrs	asoc = &stcb->asoc;
596163953Srrs	serial_num = ntohl(cp->serial_number);
597163953Srrs
598163953Srrs	if (serial_num == asoc->asconf_seq_in) {
599163953Srrs		/* got a duplicate ASCONF */
600169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
601169420Srrs		    "handle_asconf: got duplicate serial number = %xh\n",
602169420Srrs		    serial_num);
603163953Srrs		/* resend last ASCONF-ACK... */
604163953Srrs		sctp_send_asconf_ack(stcb, 1);
605163953Srrs		return;
606163953Srrs	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
607169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
608169420Srrs		    serial_num, asoc->asconf_seq_in + 1);
609163953Srrs		return;
610163953Srrs	}
611163953Srrs	/* it's the expected "next" sequence number, so process it */
612163953Srrs	asoc->asconf_seq_in = serial_num;	/* update sequence */
613163953Srrs	/* get length of all the param's in the ASCONF */
614163953Srrs	asconf_limit = offset + ntohs(cp->ch.chunk_length);
615169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF1,
616169420Srrs	    "handle_asconf: asconf_limit=%u, sequence=%xh\n",
617169420Srrs	    asconf_limit, serial_num);
618163953Srrs	if (asoc->last_asconf_ack_sent != NULL) {
619163953Srrs		/* free last ASCONF-ACK message sent */
620163953Srrs		sctp_m_freem(asoc->last_asconf_ack_sent);
621163953Srrs		asoc->last_asconf_ack_sent = NULL;
622163953Srrs	}
623165647Srrs	m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
624163953Srrs	    M_DONTWAIT, 1, MT_DATA);
625163953Srrs	if (m_ack == NULL) {
626169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
627169420Srrs		    "handle_asconf: couldn't get mbuf!\n");
628163953Srrs		return;
629163953Srrs	}
630163953Srrs	m_tail = m_ack;		/* current reply chain's tail */
631163953Srrs
632163953Srrs	/* fill in ASCONF-ACK header */
633163953Srrs	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
634163953Srrs	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
635163953Srrs	ack_cp->ch.chunk_flags = 0;
636163953Srrs	ack_cp->serial_number = htonl(serial_num);
637163953Srrs	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
638165647Srrs	SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
639163953Srrs	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
640163953Srrs
641163953Srrs	/* skip the lookup address parameter */
642163953Srrs	offset += sizeof(struct sctp_asconf_chunk);
643163953Srrs	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *) & aparam_buf);
644163953Srrs	if (p_addr == NULL) {
645169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
646169420Srrs		    "handle_asconf: couldn't get lookup addr!\n");
647163953Srrs		/* respond with a missing/invalid mandatory parameter error */
648163953Srrs		return;
649163953Srrs	}
650163953Srrs	/* param_length is already validated in process_control... */
651163953Srrs	offset += ntohs(p_addr->ph.param_length);	/* skip lookup addr */
652163953Srrs
653163953Srrs	/* get pointer to first asconf param in ASCONF-ACK */
654163953Srrs	ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, caddr_t)+sizeof(struct sctp_asconf_ack_chunk));
655163953Srrs	if (ack_aph == NULL) {
656169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Gak in asconf2\n");
657163953Srrs		return;
658163953Srrs	}
659163953Srrs	/* get pointer to first asconf param in ASCONF */
660163953Srrs	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *) & aparam_buf);
661163953Srrs	if (aph == NULL) {
662169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
663163953Srrs		goto send_reply;
664163953Srrs	}
665163953Srrs	/* process through all parameters */
666163953Srrs	while (aph != NULL) {
667163953Srrs		unsigned int param_length, param_type;
668163953Srrs
669163953Srrs		param_type = ntohs(aph->ph.param_type);
670163953Srrs		param_length = ntohs(aph->ph.param_length);
671163953Srrs		if (offset + param_length > asconf_limit) {
672163953Srrs			/* parameter goes beyond end of chunk! */
673163953Srrs			sctp_m_freem(m_ack);
674163953Srrs			return;
675163953Srrs		}
676163953Srrs		m_result = NULL;
677163953Srrs
678163953Srrs		if (param_length > sizeof(aparam_buf)) {
679169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
680163953Srrs			sctp_m_freem(m_ack);
681163953Srrs			return;
682163953Srrs		}
683163953Srrs		if (param_length <= sizeof(struct sctp_paramhdr)) {
684169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
685163953Srrs			sctp_m_freem(m_ack);
686163953Srrs		}
687163953Srrs		/* get the entire parameter */
688163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
689163953Srrs		if (aph == NULL) {
690169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
691163953Srrs			sctp_m_freem(m_ack);
692163953Srrs			return;
693163953Srrs		}
694163953Srrs		switch (param_type) {
695163953Srrs		case SCTP_ADD_IP_ADDRESS:
696163953Srrs			asoc->peer_supports_asconf = 1;
697163953Srrs			m_result = sctp_process_asconf_add_ip(m, aph, stcb,
698163953Srrs			    error);
699163953Srrs			break;
700163953Srrs		case SCTP_DEL_IP_ADDRESS:
701163953Srrs			asoc->peer_supports_asconf = 1;
702163953Srrs			m_result = sctp_process_asconf_delete_ip(m, aph, stcb,
703163953Srrs			    error);
704163953Srrs			break;
705163953Srrs		case SCTP_ERROR_CAUSE_IND:
706163953Srrs			/* not valid in an ASCONF chunk */
707163953Srrs			break;
708163953Srrs		case SCTP_SET_PRIM_ADDR:
709163953Srrs			asoc->peer_supports_asconf = 1;
710163953Srrs			m_result = sctp_process_asconf_set_primary(m, aph,
711163953Srrs			    stcb, error);
712163953Srrs			break;
713163953Srrs		case SCTP_SUCCESS_REPORT:
714163953Srrs			/* not valid in an ASCONF chunk */
715163953Srrs			break;
716163953Srrs		case SCTP_ULP_ADAPTATION:
717163953Srrs			/* FIX */
718163953Srrs			break;
719163953Srrs		default:
720163953Srrs			if ((param_type & 0x8000) == 0) {
721163953Srrs				/* Been told to STOP at this param */
722163953Srrs				asconf_limit = offset;
723163953Srrs				/*
724163953Srrs				 * FIX FIX - We need to call
725163953Srrs				 * sctp_arethere_unrecognized_parameters()
726163953Srrs				 * to get a operr and send it for any
727163953Srrs				 * param's with the 0x4000 bit set OR do it
728163953Srrs				 * here ourselves... note we still must STOP
729163953Srrs				 * if the 0x8000 bit is clear.
730163953Srrs				 */
731163953Srrs			}
732163953Srrs			/* unknown/invalid param type */
733163953Srrs			break;
734163953Srrs		}		/* switch */
735163953Srrs
736163953Srrs		/* add any (error) result to the reply mbuf chain */
737163953Srrs		if (m_result != NULL) {
738165647Srrs			SCTP_BUF_NEXT(m_tail) = m_result;
739163953Srrs			m_tail = m_result;
740163953Srrs			/* update lengths, make sure it's aligned too */
741165647Srrs			SCTP_BUF_LEN(m_result) = SCTP_SIZE32(SCTP_BUF_LEN(m_result));
742165647Srrs			ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
743163953Srrs			/* set flag to force success reports */
744163953Srrs			error = 1;
745163953Srrs		}
746163953Srrs		offset += SCTP_SIZE32(param_length);
747163953Srrs		/* update remaining ASCONF message length to process */
748163953Srrs		if (offset >= asconf_limit) {
749163953Srrs			/* no more data in the mbuf chain */
750163953Srrs			break;
751163953Srrs		}
752163953Srrs		/* get pointer to next asconf param */
753163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
754163953Srrs		    sizeof(struct sctp_asconf_paramhdr),
755163953Srrs		    (uint8_t *) & aparam_buf);
756163953Srrs		if (aph == NULL) {
757163953Srrs			/* can't get an asconf paramhdr */
758169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
759163953Srrs			/* FIX ME - add error here... */
760163953Srrs		}
761169420Srrs	}
762163953Srrs
763163953Srrssend_reply:
764163953Srrs	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
765163953Srrs	/* save the ASCONF-ACK reply */
766163953Srrs	asoc->last_asconf_ack_sent = m_ack;
767163953Srrs
768163953Srrs	/* see if last_control_chunk_from is set properly (use IP src addr) */
769163953Srrs	if (stcb->asoc.last_control_chunk_from == NULL) {
770163953Srrs		/*
771163953Srrs		 * this could happen if the source address was just newly
772163953Srrs		 * added
773163953Srrs		 */
774163953Srrs		struct ip *iph;
775163953Srrs		struct sctphdr *sh;
776163953Srrs		struct sockaddr_storage from_store;
777163953Srrs		struct sockaddr *from = (struct sockaddr *)&from_store;
778163953Srrs
779169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
780163953Srrs		/* pullup already done, IP options already stripped */
781163953Srrs		iph = mtod(m, struct ip *);
782163953Srrs		sh = (struct sctphdr *)((caddr_t)iph + sizeof(*iph));
783163953Srrs		if (iph->ip_v == IPVERSION) {
784163953Srrs			struct sockaddr_in *from4;
785163953Srrs
786163953Srrs			from4 = (struct sockaddr_in *)&from_store;
787163953Srrs			bzero(from4, sizeof(*from4));
788163953Srrs			from4->sin_family = AF_INET;
789163953Srrs			from4->sin_len = sizeof(struct sockaddr_in);
790163953Srrs			from4->sin_addr.s_addr = iph->ip_src.s_addr;
791163953Srrs			from4->sin_port = sh->src_port;
792163953Srrs		} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
793163953Srrs			struct ip6_hdr *ip6;
794163953Srrs			struct sockaddr_in6 *from6;
795163953Srrs
796163953Srrs			ip6 = mtod(m, struct ip6_hdr *);
797163953Srrs			from6 = (struct sockaddr_in6 *)&from_store;
798163953Srrs			bzero(from6, sizeof(*from6));
799163953Srrs			from6->sin6_family = AF_INET6;
800163953Srrs			from6->sin6_len = sizeof(struct sockaddr_in6);
801163953Srrs			from6->sin6_addr = ip6->ip6_src;
802163953Srrs			from6->sin6_port = sh->src_port;
803163953Srrs			/* Get the scopes in properly to the sin6 addr's */
804163953Srrs			/* we probably don't need these operations */
805163953Srrs			(void)sa6_recoverscope(from6);
806163953Srrs			sa6_embedscope(from6, ip6_use_defzone);
807163953Srrs		} else {
808163953Srrs			/* unknown address type */
809163953Srrs			from = NULL;
810163953Srrs		}
811163953Srrs		if (from != NULL) {
812169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
813169420Srrs			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, from);
814163953Srrs			/* look up the from address */
815163953Srrs			stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, from);
816163953Srrs#ifdef SCTP_DEBUG
817169420Srrs			if (stcb->asoc.last_control_chunk_from == NULL)
818169420Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
819169420Srrs#endif
820163953Srrs		}
821163953Srrs	}
822163953Srrs	/* and send it (a new one) out... */
823163953Srrs	sctp_send_asconf_ack(stcb, 0);
824163953Srrs}
825163953Srrs
826163953Srrs/*
827163953Srrs * does the address match? returns 0 if not, 1 if so
828163953Srrs */
829163953Srrsstatic uint32_t
830163953Srrssctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
831163953Srrs{
832163953Srrs#ifdef INET6
833163953Srrs	if (sa->sa_family == AF_INET6) {
834163953Srrs		/* IPv6 sa address */
835163953Srrs		/* XXX scopeid */
836163953Srrs		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
837163953Srrs
838163953Srrs		if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
839163953Srrs		    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
840163953Srrs		    sizeof(struct in6_addr)) == 0)) {
841163953Srrs			return (1);
842163953Srrs		}
843163953Srrs	} else
844163953Srrs#endif				/* INET6 */
845163953Srrs	if (sa->sa_family == AF_INET) {
846163953Srrs		/* IPv4 sa address */
847163953Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
848163953Srrs
849163953Srrs		if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
850163953Srrs		    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
851163953Srrs		    sizeof(struct in_addr)) == 0)) {
852163953Srrs			return (1);
853163953Srrs		}
854163953Srrs	}
855163953Srrs	return (0);
856163953Srrs}
857163953Srrs
858163953Srrs/*
859163953Srrs * Cleanup for non-responded/OP ERR'd ASCONF
860163953Srrs */
861163953Srrsvoid
862163953Srrssctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
863163953Srrs{
864163953Srrs	/* mark peer as ASCONF incapable */
865163953Srrs	stcb->asoc.peer_supports_asconf = 0;
866163953Srrs	/*
867163953Srrs	 * clear out any existing asconfs going out
868163953Srrs	 */
869169655Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
870169655Srrs	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
871163953Srrs	stcb->asoc.asconf_seq_out++;
872163953Srrs	/* remove the old ASCONF on our outbound queue */
873163953Srrs	sctp_toss_old_asconf(stcb);
874163953Srrs}
875163953Srrs
876163953Srrs/*
877163953Srrs * process an ADD/DELETE IP ack from peer.
878167598Srrs * addr  corresponding sctp_ifa to the address being added/deleted.
879163953Srrs * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
880163953Srrs * flag: 1=success, 0=failure.
881163953Srrs */
882163953Srrsstatic void
883167598Srrssctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr,
884163953Srrs    uint16_t type, uint32_t flag)
885163953Srrs{
886163953Srrs	/*
887163953Srrs	 * do the necessary asoc list work- if we get a failure indication,
888163953Srrs	 * leave the address on the "do not use" asoc list if we get a
889163953Srrs	 * success indication, remove the address from the list
890163953Srrs	 */
891163953Srrs	/*
892163953Srrs	 * Note: this will only occur for ADD_IP_ADDRESS, since
893163953Srrs	 * DEL_IP_ADDRESS is never actually added to the list...
894163953Srrs	 */
895163953Srrs	if (flag) {
896163953Srrs		/* success case, so remove from the list */
897163953Srrs		sctp_del_local_addr_assoc(stcb, addr);
898163953Srrs	}
899163953Srrs	/* else, leave it on the list */
900163953Srrs}
901163953Srrs
902163953Srrs/*
903163953Srrs * add an asconf add/delete IP address parameter to the queue.
904163953Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
905163953Srrs * returns 0 if completed, non-zero if not completed.
906163953Srrs * NOTE: if adding, but delete already scheduled (and not yet sent out),
907163953Srrs * simply remove from queue.  Same for deleting an address already scheduled
908163953Srrs * for add.  If a duplicate operation is found, ignore the new one.
909163953Srrs */
910163953Srrsstatic uint32_t
911167598Srrssctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa, uint16_t type)
912163953Srrs{
913163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
914163953Srrs	struct sockaddr *sa;
915163953Srrs
916163953Srrs	/* see if peer supports ASCONF */
917163953Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
918163953Srrs		return (-1);
919163953Srrs	}
920163953Srrs	/* make sure the request isn't already in the queue */
921163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
922163953Srrs	    aa = aa_next) {
923163953Srrs		aa_next = TAILQ_NEXT(aa, next);
924163953Srrs		/* address match? */
925167598Srrs		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
926163953Srrs			continue;
927163953Srrs		/* is the request already in queue (sent or not) */
928163953Srrs		if (aa->ap.aph.ph.param_type == type) {
929163953Srrs			return (-1);
930163953Srrs		}
931163953Srrs		/* is the negative request already in queue, and not sent */
932163953Srrs		if (aa->sent == 0 &&
933163953Srrs		/* add requested, delete already queued */
934163953Srrs		    ((type == SCTP_ADD_IP_ADDRESS &&
935163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) ||
936163953Srrs		/* delete requested, add already queued */
937163953Srrs		    (type == SCTP_DEL_IP_ADDRESS &&
938163953Srrs		    aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS))) {
939163953Srrs			/* delete the existing entry in the queue */
940163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
941163953Srrs			/* take the entry off the appropriate list */
942163953Srrs			sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
943163953Srrs			/* free the entry */
944168299Srrs			sctp_free_ifa(aa->ifa);
945170091Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
946163953Srrs			return (-1);
947163953Srrs		}
948163953Srrs	}			/* for each aa */
949163953Srrs
950163953Srrs	/* adding new request to the queue */
951170091Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa), SCTP_M_ASC_ADDR);
952163953Srrs	if (aa == NULL) {
953163953Srrs		/* didn't get memory */
954169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
955169420Srrs		    "asconf_queue_add: failed to get memory!\n");
956163953Srrs		return (-1);
957163953Srrs	}
958163953Srrs	/* fill in asconf address parameter fields */
959163953Srrs	/* top level elements are "networked" during send */
960163953Srrs	aa->ap.aph.ph.param_type = type;
961163953Srrs	aa->ifa = ifa;
962168299Srrs	atomic_add_int(&ifa->refcount, 1);
963163953Srrs	/* correlation_id filled in during send routine later... */
964167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
965163953Srrs		/* IPv6 address */
966163953Srrs		struct sockaddr_in6 *sin6;
967163953Srrs
968167598Srrs		sin6 = (struct sockaddr_in6 *)&ifa->address.sa;
969163953Srrs		sa = (struct sockaddr *)sin6;
970163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
971163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
972163953Srrs		aa->ap.aph.ph.param_length =
973163953Srrs		    sizeof(struct sctp_asconf_paramhdr) +
974163953Srrs		    sizeof(struct sctp_ipv6addr_param);
975163953Srrs		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
976163953Srrs		    sizeof(struct in6_addr));
977167598Srrs	} else if (ifa->address.sa.sa_family == AF_INET) {
978163953Srrs		/* IPv4 address */
979167598Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)&ifa->address.sa;
980163953Srrs
981163953Srrs		sa = (struct sockaddr *)sin;
982163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
983163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
984163953Srrs		aa->ap.aph.ph.param_length =
985163953Srrs		    sizeof(struct sctp_asconf_paramhdr) +
986163953Srrs		    sizeof(struct sctp_ipv4addr_param);
987163953Srrs		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
988163953Srrs		    sizeof(struct in_addr));
989163953Srrs	} else {
990163953Srrs		/* invalid family! */
991170091Srrs		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
992163953Srrs		return (-1);
993163953Srrs	}
994163953Srrs	aa->sent = 0;		/* clear sent flag */
995163953Srrs
996163953Srrs	/*
997163953Srrs	 * if we are deleting an address it should go out last otherwise,
998163953Srrs	 * add it to front of the pending queue
999163953Srrs	 */
1000163953Srrs	if (type == SCTP_ADD_IP_ADDRESS) {
1001163953Srrs		/* add goes to the front of the queue */
1002163953Srrs		TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
1003169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF2,
1004169420Srrs		    "asconf_queue_add: appended asconf ADD_IP_ADDRESS: ");
1005169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1006163953Srrs	} else {
1007163953Srrs		/* delete and set primary goes to the back of the queue */
1008163953Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1009163953Srrs#ifdef SCTP_DEBUG
1010169420Srrs		if (sctp_debug_on && SCTP_DEBUG_ASCONF2) {
1011163953Srrs			if (type == SCTP_DEL_IP_ADDRESS) {
1012169420Srrs				SCTP_PRINTF("asconf_queue_add: inserted asconf DEL_IP_ADDRESS: ");
1013169420Srrs				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1014163953Srrs			} else {
1015169420Srrs				SCTP_PRINTF("asconf_queue_add: inserted asconf SET_PRIM_ADDR: ");
1016169420Srrs				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
1017163953Srrs			}
1018163953Srrs		}
1019169420Srrs#endif
1020163953Srrs	}
1021163953Srrs
1022163953Srrs	return (0);
1023163953Srrs}
1024163953Srrs
1025163953Srrs/*
1026163953Srrs * add an asconf add/delete IP address parameter to the queue by addr.
1027163953Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1028163953Srrs * returns 0 if completed, non-zero if not completed.
1029163953Srrs * NOTE: if adding, but delete already scheduled (and not yet sent out),
1030163953Srrs * simply remove from queue.  Same for deleting an address already scheduled
1031163953Srrs * for add.  If a duplicate operation is found, ignore the new one.
1032163953Srrs */
1033163953Srrsstatic uint32_t
1034163953Srrssctp_asconf_queue_add_sa(struct sctp_tcb *stcb, struct sockaddr *sa,
1035163953Srrs    uint16_t type)
1036163953Srrs{
1037168299Srrs	struct sctp_ifa *ifa;
1038163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1039167598Srrs	uint32_t vrf_id;
1040163953Srrs
1041169420Srrs	if (stcb == NULL) {
1042169420Srrs		return (-1);
1043169420Srrs	}
1044163953Srrs	/* see if peer supports ASCONF */
1045163953Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
1046163953Srrs		return (-1);
1047163953Srrs	}
1048163953Srrs	/* make sure the request isn't already in the queue */
1049163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1050163953Srrs	    aa = aa_next) {
1051163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1052163953Srrs		/* address match? */
1053163953Srrs		if (sctp_asconf_addr_match(aa, sa) == 0)
1054163953Srrs			continue;
1055163953Srrs		/* is the request already in queue (sent or not) */
1056163953Srrs		if (aa->ap.aph.ph.param_type == type) {
1057163953Srrs			return (-1);
1058163953Srrs		}
1059163953Srrs		/* is the negative request already in queue, and not sent */
1060163953Srrs		if (aa->sent == 1)
1061163953Srrs			continue;
1062163953Srrs		if (type == SCTP_ADD_IP_ADDRESS &&
1063163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1064163953Srrs			/* add requested, delete already queued */
1065163953Srrs
1066163953Srrs			/* delete the existing entry in the queue */
1067163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1068163953Srrs			/* free the entry */
1069168299Srrs			sctp_free_ifa(aa->ifa);
1070170091Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1071163953Srrs			return (-1);
1072163953Srrs		} else if (type == SCTP_DEL_IP_ADDRESS &&
1073163953Srrs		    aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1074163953Srrs			/* delete requested, add already queued */
1075163953Srrs
1076163953Srrs			/* delete the existing entry in the queue */
1077163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1078163953Srrs			/* take the entry off the appropriate list */
1079163953Srrs			sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
1080163953Srrs			/* free the entry */
1081168299Srrs			sctp_free_ifa(aa->ifa);
1082170091Srrs			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1083163953Srrs			return (-1);
1084163953Srrs		}
1085163953Srrs	}			/* for each aa */
1086168299Srrs	if (stcb) {
1087168299Srrs		vrf_id = stcb->asoc.vrf_id;
1088168299Srrs	} else {
1089168299Srrs		vrf_id = SCTP_DEFAULT_VRFID;
1090168299Srrs	}
1091163953Srrs
1092168299Srrs	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
1093168299Srrs	if (ifa == NULL) {
1094168299Srrs		/* Invalid address */
1095168299Srrs		return (-1);
1096168299Srrs	}
1097163953Srrs	/* adding new request to the queue */
1098170091Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa), SCTP_M_ASC_ADDR);
1099163953Srrs	if (aa == NULL) {
1100163953Srrs		/* didn't get memory */
1101169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1102169420Srrs		    "asconf_queue_add_sa: failed to get memory!\n");
1103163953Srrs		return (-1);
1104163953Srrs	}
1105163953Srrs	/* fill in asconf address parameter fields */
1106163953Srrs	/* top level elements are "networked" during send */
1107163953Srrs	aa->ap.aph.ph.param_type = type;
1108168299Srrs	aa->ifa = ifa;
1109168299Srrs	atomic_add_int(&ifa->refcount, 1);
1110163953Srrs	/* correlation_id filled in during send routine later... */
1111163953Srrs	if (sa->sa_family == AF_INET6) {
1112163953Srrs		/* IPv6 address */
1113163953Srrs		struct sockaddr_in6 *sin6;
1114163953Srrs
1115163953Srrs		sin6 = (struct sockaddr_in6 *)sa;
1116163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1117163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1118163953Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1119163953Srrs		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1120163953Srrs		    sizeof(struct in6_addr));
1121163953Srrs	} else if (sa->sa_family == AF_INET) {
1122163953Srrs		/* IPv4 address */
1123163953Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1124163953Srrs
1125163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1126163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1127163953Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1128163953Srrs		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1129163953Srrs		    sizeof(struct in_addr));
1130163953Srrs	} else {
1131163953Srrs		/* invalid family! */
1132170091Srrs		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1133163953Srrs		return (-1);
1134163953Srrs	}
1135163953Srrs	aa->sent = 0;		/* clear sent flag */
1136163953Srrs
1137163953Srrs	/*
1138163953Srrs	 * if we are deleting an address it should go out last otherwise,
1139163953Srrs	 * add it to front of the pending queue
1140163953Srrs	 */
1141163953Srrs	if (type == SCTP_ADD_IP_ADDRESS) {
1142163953Srrs		/* add goes to the front of the queue */
1143163953Srrs		TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
1144163953Srrs	} else {
1145163953Srrs		/* delete and set primary goes to the back of the queue */
1146163953Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1147163953Srrs	}
1148163953Srrs
1149163953Srrs	return (0);
1150163953Srrs}
1151163953Srrs
1152163953Srrs/*
1153163953Srrs * find a specific asconf param on our "sent" queue
1154163953Srrs */
1155163953Srrsstatic struct sctp_asconf_addr *
1156163953Srrssctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1157163953Srrs{
1158163953Srrs	struct sctp_asconf_addr *aa;
1159163953Srrs
1160163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1161163953Srrs		if (aa->ap.aph.correlation_id == correlation_id &&
1162163953Srrs		    aa->sent == 1) {
1163163953Srrs			/* found it */
1164163953Srrs			return (aa);
1165163953Srrs		}
1166163953Srrs	}
1167163953Srrs	/* didn't find it */
1168163953Srrs	return (NULL);
1169163953Srrs}
1170163953Srrs
1171163953Srrs/*
1172163953Srrs * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1173163953Srrs * notifications based on the error response
1174163953Srrs */
1175163953Srrsstatic void
1176163953Srrssctp_asconf_process_error(struct sctp_tcb *stcb,
1177163953Srrs    struct sctp_asconf_paramhdr *aph)
1178163953Srrs{
1179163953Srrs	struct sctp_error_cause *eh;
1180163953Srrs	struct sctp_paramhdr *ph;
1181163953Srrs	uint16_t param_type;
1182163953Srrs	uint16_t error_code;
1183163953Srrs
1184163953Srrs	eh = (struct sctp_error_cause *)(aph + 1);
1185163953Srrs	ph = (struct sctp_paramhdr *)(eh + 1);
1186163953Srrs	/* validate lengths */
1187163953Srrs	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1188163953Srrs	    htons(aph->ph.param_length)) {
1189163953Srrs		/* invalid error cause length */
1190169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1191169420Srrs		    "asconf_process_error: cause element too long\n");
1192163953Srrs		return;
1193163953Srrs	}
1194163953Srrs	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1195163953Srrs	    htons(eh->length)) {
1196163953Srrs		/* invalid included TLV length */
1197169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1198169420Srrs		    "asconf_process_error: included TLV too long\n");
1199163953Srrs		return;
1200163953Srrs	}
1201163953Srrs	/* which error code ? */
1202163953Srrs	error_code = ntohs(eh->code);
1203163953Srrs	param_type = ntohs(aph->ph.param_type);
1204163953Srrs	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1205163953Srrs	switch (error_code) {
1206163953Srrs	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1207163953Srrs		/* we allow ourselves to "try again" for this error */
1208163953Srrs		break;
1209163953Srrs	default:
1210163953Srrs		/* peer can't handle it... */
1211163953Srrs		switch (param_type) {
1212163953Srrs		case SCTP_ADD_IP_ADDRESS:
1213163953Srrs		case SCTP_DEL_IP_ADDRESS:
1214163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1215163953Srrs			break;
1216163953Srrs		case SCTP_SET_PRIM_ADDR:
1217163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1218163953Srrs			break;
1219163953Srrs		default:
1220163953Srrs			break;
1221163953Srrs		}
1222163953Srrs	}
1223163953Srrs}
1224163953Srrs
1225163953Srrs/*
1226163953Srrs * process an asconf queue param aparam: parameter to process, will be
1227163953Srrs * removed from the queue flag: 1=success, 0=failure
1228163953Srrs */
1229163953Srrsstatic void
1230163953Srrssctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1231163953Srrs    struct sctp_asconf_addr *aparam, uint32_t flag)
1232163953Srrs{
1233163953Srrs	uint16_t param_type;
1234163953Srrs
1235163953Srrs	/* process this param */
1236163953Srrs	param_type = aparam->ap.aph.ph.param_type;
1237163953Srrs	switch (param_type) {
1238163953Srrs	case SCTP_ADD_IP_ADDRESS:
1239169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1240169420Srrs		    "process_param_ack: added IP address\n");
1241163953Srrs		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
1242163953Srrs		break;
1243163953Srrs	case SCTP_DEL_IP_ADDRESS:
1244169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1245169420Srrs		    "process_param_ack: deleted IP address\n");
1246163953Srrs		/* nothing really to do... lists already updated */
1247163953Srrs		break;
1248163953Srrs	case SCTP_SET_PRIM_ADDR:
1249163953Srrs		/* nothing to do... peer may start using this addr */
1250163953Srrs		if (flag == 0)
1251163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1252163953Srrs		break;
1253163953Srrs	default:
1254163953Srrs		/* should NEVER happen */
1255163953Srrs		break;
1256163953Srrs	}
1257163953Srrs
1258163953Srrs	/* remove the param and free it */
1259163953Srrs	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1260168299Srrs	sctp_free_ifa(aparam->ifa);
1261170091Srrs	SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1262163953Srrs}
1263163953Srrs
1264163953Srrs/*
1265163953Srrs * cleanup from a bad asconf ack parameter
1266163953Srrs */
1267163953Srrsstatic void
1268163953Srrssctp_asconf_ack_clear(struct sctp_tcb *stcb)
1269163953Srrs{
1270163953Srrs	/* assume peer doesn't really know how to do asconfs */
1271163953Srrs	stcb->asoc.peer_supports_asconf = 0;
1272163953Srrs	/* XXX we could free the pending queue here */
1273163953Srrs}
1274163953Srrs
1275163953Srrsvoid
1276163953Srrssctp_handle_asconf_ack(struct mbuf *m, int offset,
1277163953Srrs    struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1278163953Srrs    struct sctp_nets *net)
1279163953Srrs{
1280163953Srrs	struct sctp_association *asoc;
1281163953Srrs	uint32_t serial_num;
1282163953Srrs	uint16_t ack_length;
1283163953Srrs	struct sctp_asconf_paramhdr *aph;
1284163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1285163953Srrs	uint32_t last_error_id = 0;	/* last error correlation id */
1286163953Srrs	uint32_t id;
1287163953Srrs	struct sctp_asconf_addr *ap;
1288163953Srrs
1289163953Srrs	/* asconf param buffer */
1290166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1291163953Srrs
1292163953Srrs	/* verify minimum length */
1293163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1294169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1295169420Srrs		    "handle_asconf_ack: chunk too small = %xh\n",
1296169420Srrs		    ntohs(cp->ch.chunk_length));
1297163953Srrs		return;
1298163953Srrs	}
1299163953Srrs	asoc = &stcb->asoc;
1300163953Srrs	serial_num = ntohl(cp->serial_number);
1301163953Srrs
1302163953Srrs	/*
1303163953Srrs	 * NOTE: we may want to handle this differently- currently, we will
1304163953Srrs	 * abort when we get an ack for the expected serial number + 1 (eg.
1305163953Srrs	 * we didn't send it), process an ack normally if it is the expected
1306163953Srrs	 * serial number, and re-send the previous ack for *ALL* other
1307163953Srrs	 * serial numbers
1308163953Srrs	 */
1309163953Srrs
1310163953Srrs	/*
1311163953Srrs	 * if the serial number is the next expected, but I didn't send it,
1312163953Srrs	 * abort the asoc, since someone probably just hijacked us...
1313163953Srrs	 */
1314163953Srrs	if (serial_num == (asoc->asconf_seq_out + 1)) {
1315169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1316163953Srrs		sctp_abort_an_association(stcb->sctp_ep, stcb,
1317163953Srrs		    SCTP_CAUSE_ILLEGAL_ASCONF_ACK, NULL);
1318163953Srrs		return;
1319163953Srrs	}
1320163953Srrs	if (serial_num != asoc->asconf_seq_out) {
1321163953Srrs		/* got a duplicate/unexpected ASCONF-ACK */
1322169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
1323169420Srrs		    serial_num, asoc->asconf_seq_out);
1324163953Srrs		return;
1325163953Srrs	}
1326163953Srrs	if (stcb->asoc.asconf_sent == 0) {
1327163953Srrs		/* got a unexpected ASCONF-ACK for serial not in flight */
1328169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got serial number = %xh but not in flight\n",
1329169420Srrs		    serial_num);
1330163953Srrs		/* nothing to do... duplicate ACK received */
1331163953Srrs		return;
1332163953Srrs	}
1333163953Srrs	/* stop our timer */
1334169655Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
1335169655Srrs	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1336163953Srrs
1337163953Srrs	/* process the ASCONF-ACK contents */
1338163953Srrs	ack_length = ntohs(cp->ch.chunk_length) -
1339163953Srrs	    sizeof(struct sctp_asconf_ack_chunk);
1340163953Srrs	offset += sizeof(struct sctp_asconf_ack_chunk);
1341163953Srrs	/* process through all parameters */
1342163953Srrs	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1343163953Srrs		unsigned int param_length, param_type;
1344163953Srrs
1345163953Srrs		/* get pointer to next asconf parameter */
1346163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1347163953Srrs		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1348163953Srrs		if (aph == NULL) {
1349163953Srrs			/* can't get an asconf paramhdr */
1350163953Srrs			sctp_asconf_ack_clear(stcb);
1351163953Srrs			return;
1352163953Srrs		}
1353163953Srrs		param_type = ntohs(aph->ph.param_type);
1354163953Srrs		param_length = ntohs(aph->ph.param_length);
1355163953Srrs		if (param_length > ack_length) {
1356163953Srrs			sctp_asconf_ack_clear(stcb);
1357163953Srrs			return;
1358163953Srrs		}
1359163953Srrs		if (param_length < sizeof(struct sctp_paramhdr)) {
1360163953Srrs			sctp_asconf_ack_clear(stcb);
1361163953Srrs			return;
1362163953Srrs		}
1363163953Srrs		/* get the complete parameter... */
1364163953Srrs		if (param_length > sizeof(aparam_buf)) {
1365169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
1366169420Srrs			    "param length (%u) larger than buffer size!\n", param_length);
1367163953Srrs			sctp_asconf_ack_clear(stcb);
1368163953Srrs			return;
1369163953Srrs		}
1370163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1371163953Srrs		if (aph == NULL) {
1372163953Srrs			sctp_asconf_ack_clear(stcb);
1373163953Srrs			return;
1374163953Srrs		}
1375163953Srrs		/* correlation_id is transparent to peer, no ntohl needed */
1376163953Srrs		id = aph->correlation_id;
1377163953Srrs
1378163953Srrs		switch (param_type) {
1379163953Srrs		case SCTP_ERROR_CAUSE_IND:
1380163953Srrs			last_error_id = id;
1381163953Srrs			/* find the corresponding asconf param in our queue */
1382163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1383163953Srrs			if (ap == NULL) {
1384163953Srrs				/* hmm... can't find this in our queue! */
1385163953Srrs				break;
1386163953Srrs			}
1387163953Srrs			/* process the parameter, failed flag */
1388163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 0);
1389163953Srrs			/* process the error response */
1390163953Srrs			sctp_asconf_process_error(stcb, aph);
1391163953Srrs			break;
1392163953Srrs		case SCTP_SUCCESS_REPORT:
1393163953Srrs			/* find the corresponding asconf param in our queue */
1394163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1395163953Srrs			if (ap == NULL) {
1396163953Srrs				/* hmm... can't find this in our queue! */
1397163953Srrs				break;
1398163953Srrs			}
1399163953Srrs			/* process the parameter, success flag */
1400163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 1);
1401163953Srrs			break;
1402163953Srrs		default:
1403163953Srrs			break;
1404163953Srrs		}		/* switch */
1405163953Srrs
1406163953Srrs		/* update remaining ASCONF-ACK message length to process */
1407163953Srrs		ack_length -= SCTP_SIZE32(param_length);
1408163953Srrs		if (ack_length <= 0) {
1409163953Srrs			/* no more data in the mbuf chain */
1410163953Srrs			break;
1411163953Srrs		}
1412163953Srrs		offset += SCTP_SIZE32(param_length);
1413163953Srrs	}			/* while */
1414163953Srrs
1415163953Srrs	/*
1416163953Srrs	 * if there are any "sent" params still on the queue, these are
1417163953Srrs	 * implicitly "success", or "failed" (if we got an error back) ...
1418163953Srrs	 * so process these appropriately
1419163953Srrs	 *
1420163953Srrs	 * we assume that the correlation_id's are monotonically increasing
1421163953Srrs	 * beginning from 1 and that we don't have *that* many outstanding
1422163953Srrs	 * at any given time
1423163953Srrs	 */
1424163953Srrs	if (last_error_id == 0)
1425163953Srrs		last_error_id--;/* set to "max" value */
1426163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1427163953Srrs	    aa = aa_next) {
1428163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1429163953Srrs		if (aa->sent == 1) {
1430163953Srrs			/*
1431163953Srrs			 * implicitly successful or failed if correlation_id
1432163953Srrs			 * < last_error_id, then success else, failure
1433163953Srrs			 */
1434163953Srrs			if (aa->ap.aph.correlation_id < last_error_id)
1435163953Srrs				sctp_asconf_process_param_ack(stcb, aa,
1436163953Srrs				    SCTP_SUCCESS_REPORT);
1437163953Srrs			else
1438163953Srrs				sctp_asconf_process_param_ack(stcb, aa,
1439163953Srrs				    SCTP_ERROR_CAUSE_IND);
1440163953Srrs		} else {
1441163953Srrs			/*
1442163953Srrs			 * since we always process in order (FIFO queue) if
1443163953Srrs			 * we reach one that hasn't been sent, the rest
1444163953Srrs			 * should not have been sent either. so, we're
1445163953Srrs			 * done...
1446163953Srrs			 */
1447163953Srrs			break;
1448163953Srrs		}
1449163953Srrs	}
1450163953Srrs
1451163953Srrs	/* update the next sequence number to use */
1452163953Srrs	asoc->asconf_seq_out++;
1453163953Srrs	/* remove the old ASCONF on our outbound queue */
1454163953Srrs	sctp_toss_old_asconf(stcb);
1455163953Srrs	/* clear the sent flag to allow new ASCONFs */
1456163953Srrs	asoc->asconf_sent = 0;
1457163953Srrs	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1458163953Srrs		/* we have more params, so restart our timer */
1459163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1460163953Srrs		    stcb, net);
1461163953Srrs	}
1462163953Srrs}
1463163953Srrs
1464163953Srrsstatic uint32_t
1465163953Srrssctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1466163953Srrs{
1467163953Srrs	struct sockaddr_in6 *sin6, *net6;
1468163953Srrs	struct sctp_nets *net;
1469163953Srrs
1470163953Srrs	if (sa->sa_family != AF_INET6) {
1471163953Srrs		/* wrong family */
1472163953Srrs		return (0);
1473163953Srrs	}
1474163953Srrs	sin6 = (struct sockaddr_in6 *)sa;
1475163953Srrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1476163953Srrs		/* not link local address */
1477163953Srrs		return (0);
1478163953Srrs	}
1479163953Srrs	/* hunt through our destination nets list for this scope_id */
1480163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1481163953Srrs		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1482163953Srrs		    AF_INET6)
1483163953Srrs			continue;
1484163953Srrs		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1485163953Srrs		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1486163953Srrs			continue;
1487163953Srrs		if (sctp_is_same_scope(sin6, net6)) {
1488163953Srrs			/* found one */
1489163953Srrs			return (1);
1490163953Srrs		}
1491163953Srrs	}
1492163953Srrs	/* didn't find one */
1493163953Srrs	return (0);
1494163953Srrs}
1495163953Srrs
1496163953Srrs/*
1497163953Srrs * address management functions
1498163953Srrs */
1499163953Srrsstatic void
1500163953Srrssctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1501167598Srrs    struct sctp_ifa *ifa, uint16_t type)
1502163953Srrs{
1503163953Srrs	int status;
1504163953Srrs
1505163953Srrs
1506163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
1507163953Srrs	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1508163953Srrs		/* subset bound, no ASCONF allowed case, so ignore */
1509163953Srrs		return;
1510163953Srrs	}
1511163953Srrs	/*
1512163953Srrs	 * note: we know this is not the subset bound, no ASCONF case eg.
1513163953Srrs	 * this is boundall or subset bound w/ASCONF allowed
1514163953Srrs	 */
1515163953Srrs
1516163953Srrs	/* first, make sure it's a good address family */
1517167598Srrs	if (ifa->address.sa.sa_family != AF_INET6 &&
1518167598Srrs	    ifa->address.sa.sa_family != AF_INET) {
1519163953Srrs		return;
1520163953Srrs	}
1521163953Srrs	/* make sure we're "allowed" to add this type of addr */
1522167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1523163953Srrs		/* invalid if we're not a v6 endpoint */
1524163953Srrs		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1525163953Srrs			return;
1526163953Srrs		/* is the v6 addr really valid ? */
1527167598Srrs		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1528163953Srrs			return;
1529163953Srrs		}
1530163953Srrs	}
1531163953Srrs	/* put this address on the "pending/do not use yet" list */
1532163953Srrs	/*
1533163953Srrs	 * Note: we do this primarily for the subset bind case We don't have
1534163953Srrs	 * scoping flags at the EP level, so we must add link local/site
1535163953Srrs	 * local addresses to the EP, then need to "negate" them here.
1536163953Srrs	 * Recall that this routine is only called for the subset bound
1537163953Srrs	 * w/ASCONF allowed case.
1538163953Srrs	 */
1539167598Srrs	sctp_add_local_addr_assoc(stcb, ifa, 1);
1540163953Srrs	/*
1541163953Srrs	 * check address scope if address is out of scope, don't queue
1542163953Srrs	 * anything... note: this would leave the address on both inp and
1543163953Srrs	 * asoc lists
1544163953Srrs	 */
1545167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1546163953Srrs		struct sockaddr_in6 *sin6;
1547163953Srrs
1548167598Srrs		sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1549163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1550163953Srrs			/* we skip unspecifed addresses */
1551163953Srrs			return;
1552163953Srrs		}
1553163953Srrs		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1554163953Srrs			if (stcb->asoc.local_scope == 0) {
1555163953Srrs				return;
1556163953Srrs			}
1557163953Srrs			/* is it the right link local scope? */
1558167598Srrs			if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1559163953Srrs				return;
1560163953Srrs			}
1561163953Srrs		}
1562163953Srrs		if (stcb->asoc.site_scope == 0 &&
1563163953Srrs		    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
1564163953Srrs			return;
1565163953Srrs		}
1566167598Srrs	} else if (ifa->address.sa.sa_family == AF_INET) {
1567163953Srrs		struct sockaddr_in *sin;
1568163953Srrs		struct in6pcb *inp6;
1569163953Srrs
1570163953Srrs		inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1571163953Srrs		/* invalid if we are a v6 only endpoint */
1572163953Srrs		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1573166023Srrs		    SCTP_IPV6_V6ONLY(inp6))
1574163953Srrs			return;
1575163953Srrs
1576167598Srrs		sin = (struct sockaddr_in *)&ifa->address.sa;
1577163953Srrs		if (sin->sin_addr.s_addr == 0) {
1578163953Srrs			/* we skip unspecifed addresses */
1579163953Srrs			return;
1580163953Srrs		}
1581163953Srrs		if (stcb->asoc.ipv4_local_scope == 0 &&
1582163953Srrs		    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1583163953Srrs			return;
1584163953Srrs		}
1585163953Srrs	} else {
1586163953Srrs		/* else, not AF_INET or AF_INET6, so skip */
1587163953Srrs		return;
1588163953Srrs	}
1589163953Srrs
1590163953Srrs	/* queue an asconf for this address add/delete */
1591163953Srrs	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1592163953Srrs		/* does the peer do asconf? */
1593163953Srrs		if (stcb->asoc.peer_supports_asconf) {
1594163953Srrs			/* queue an asconf for this addr */
1595163953Srrs			status = sctp_asconf_queue_add(stcb, ifa, type);
1596163953Srrs			/*
1597163953Srrs			 * if queued ok, and in correct state, set the
1598163953Srrs			 * ASCONF timer if in non-open state, we will set
1599163953Srrs			 * this timer when the state does go open and do all
1600163953Srrs			 * the asconf's
1601163953Srrs			 */
1602163953Srrs			if (status == 0 &&
1603163953Srrs			    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
1604163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
1605163953Srrs				    stcb, stcb->asoc.primary_destination);
1606163953Srrs			}
1607163953Srrs		}
1608163953Srrs	}
1609163953Srrs}
1610163953Srrs
1611167598Srrs
1612167598Srrsint
1613167598Srrssctp_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val)
1614163953Srrs{
1615167598Srrs	struct sctp_asconf_iterator *asc;
1616167598Srrs	struct sctp_ifa *ifa;
1617167598Srrs	struct sctp_laddr *l;
1618167598Srrs	int type;
1619167598Srrs	int cnt_invalid = 0;
1620163953Srrs
1621167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1622167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
1623167598Srrs		ifa = l->ifa;
1624167598Srrs		type = l->action;
1625167598Srrs		if (ifa->address.sa.sa_family == AF_INET6) {
1626167598Srrs			/* invalid if we're not a v6 endpoint */
1627167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1628167598Srrs				cnt_invalid++;
1629167598Srrs				if (asc->cnt == cnt_invalid)
1630167598Srrs					return (1);
1631167598Srrs				else
1632167598Srrs					continue;
1633167598Srrs			}
1634167598Srrs		} else if (ifa->address.sa.sa_family == AF_INET) {
1635167598Srrs			/* invalid if we are a v6 only endpoint */
1636167598Srrs			struct in6pcb *inp6;
1637163953Srrs
1638167598Srrs			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1639167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1640167598Srrs			    SCTP_IPV6_V6ONLY(inp6)) {
1641167598Srrs				cnt_invalid++;
1642167598Srrs				if (asc->cnt == cnt_invalid)
1643167598Srrs					return (1);
1644167598Srrs				else
1645167598Srrs					continue;
1646163953Srrs			}
1647163953Srrs		} else {
1648167598Srrs			/* invalid address family */
1649167598Srrs			cnt_invalid++;
1650167598Srrs			if (asc->cnt == cnt_invalid)
1651167598Srrs				return (1);
1652167598Srrs			else
1653167598Srrs				continue;
1654163953Srrs		}
1655163953Srrs	}
1656167598Srrs	return (0);
1657163953Srrs}
1658163953Srrs
1659167598Srrsint
1660167598Srrssctp_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val)
1661163953Srrs{
1662167598Srrs	struct sctp_ifa *ifa;
1663167598Srrs	struct sctp_asconf_iterator *asc;
1664167598Srrs	struct sctp_laddr *laddr, *nladdr, *l;
1665163953Srrs
1666167598Srrs	/* Only for specific case not bound all */
1667167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1668167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
1669167598Srrs		ifa = l->ifa;
1670167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
1671169655Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list,
1672169655Srrs			    sctp_nxt_addr) {
1673167598Srrs				if (laddr->ifa == ifa) {
1674167598Srrs					laddr->action = 0;
1675167598Srrs					break;
1676167598Srrs				}
1677167598Srrs			}
1678167598Srrs		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
1679167598Srrs			laddr = LIST_FIRST(&inp->sctp_addr_list);
1680167598Srrs			while (laddr) {
1681167598Srrs				nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
1682167598Srrs				/* remove only after all guys are done */
1683167598Srrs				if (laddr->ifa == ifa) {
1684167598Srrs					sctp_del_local_addr_ep(inp, ifa);
1685167598Srrs				}
1686167598Srrs				laddr = nladdr;
1687167598Srrs			}
1688167598Srrs		}
1689163953Srrs	}
1690167598Srrs	return (0);
1691163953Srrs}
1692163953Srrs
1693167598Srrsvoid
1694167598Srrssctp_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
1695167598Srrs    uint32_t val)
1696163953Srrs{
1697167598Srrs	struct sctp_asconf_iterator *asc;
1698167598Srrs	struct sctp_ifa *ifa;
1699167598Srrs	struct sctp_laddr *l;
1700167598Srrs	int cnt_invalid = 0;
1701167598Srrs	int type, status;
1702163953Srrs
1703167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1704167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
1705167598Srrs		ifa = l->ifa;
1706167598Srrs		type = l->action;
1707167598Srrs		/* Same checks again for assoc */
1708167598Srrs		if (ifa->address.sa.sa_family == AF_INET6) {
1709167598Srrs			/* invalid if we're not a v6 endpoint */
1710167598Srrs			struct sockaddr_in6 *sin6;
1711163953Srrs
1712167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1713167598Srrs				cnt_invalid++;
1714167598Srrs				if (asc->cnt == cnt_invalid)
1715167598Srrs					return;
1716167598Srrs				else
1717167598Srrs					continue;
1718167598Srrs			}
1719167598Srrs			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1720167598Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1721167598Srrs				/* we skip unspecifed addresses */
1722167598Srrs				continue;
1723167598Srrs			}
1724167598Srrs			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1725167598Srrs				if (stcb->asoc.local_scope == 0) {
1726167598Srrs					continue;
1727167598Srrs				}
1728167598Srrs				/* is it the right link local scope? */
1729167598Srrs				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1730167598Srrs					continue;
1731167598Srrs				}
1732167598Srrs			}
1733167598Srrs		} else if (ifa->address.sa.sa_family == AF_INET) {
1734167598Srrs			/* invalid if we are a v6 only endpoint */
1735167598Srrs			struct in6pcb *inp6;
1736167598Srrs			struct sockaddr_in *sin;
1737163953Srrs
1738167598Srrs			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1739167598Srrs			/* invalid if we are a v6 only endpoint */
1740167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1741167598Srrs			    SCTP_IPV6_V6ONLY(inp6))
1742167598Srrs				continue;
1743167598Srrs
1744167598Srrs			sin = (struct sockaddr_in *)&ifa->address.sa;
1745167598Srrs			if (sin->sin_addr.s_addr == 0) {
1746167598Srrs				/* we skip unspecifed addresses */
1747167598Srrs				continue;
1748167598Srrs			}
1749167598Srrs			if (stcb->asoc.ipv4_local_scope == 0 &&
1750167598Srrs			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1751167598Srrs				continue;;
1752167598Srrs			}
1753167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1754167598Srrs			    SCTP_IPV6_V6ONLY(inp6)) {
1755167598Srrs				cnt_invalid++;
1756167598Srrs				if (asc->cnt == cnt_invalid)
1757167598Srrs					return;
1758167598Srrs				else
1759167598Srrs					continue;
1760167598Srrs			}
1761163953Srrs		} else {
1762167598Srrs			/* invalid address family */
1763167598Srrs			cnt_invalid++;
1764167598Srrs			if (asc->cnt == cnt_invalid)
1765163953Srrs				return;
1766167598Srrs			else
1767167598Srrs				continue;
1768163953Srrs		}
1769163953Srrs
1770167598Srrs		/* put this address on the "pending/do not use yet" list */
1771167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
1772167598Srrs			sctp_add_local_addr_assoc(stcb, ifa, 1);
1773167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
1774163953Srrs			struct sctp_nets *net;
1775163953Srrs
1776163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1777169352Srrs				sctp_rtentry_t *rt;
1778163953Srrs
1779163953Srrs				/* delete this address if cached */
1780167598Srrs				if (net->ro._s_addr &&
1781167598Srrs				    (net->ro._s_addr->ifa == ifa)) {
1782167598Srrs					sctp_free_ifa(net->ro._s_addr);
1783167598Srrs					net->ro._s_addr = NULL;
1784167598Srrs					net->src_addr_selected = 0;
1785167598Srrs					rt = net->ro.ro_rt;
1786167598Srrs					if (rt) {
1787167598Srrs						RTFREE(rt);
1788167598Srrs						net->ro.ro_rt = NULL;
1789167598Srrs					}
1790167598Srrs					/*
1791167598Srrs					 * Now we deleted our src address,
1792167598Srrs					 * should we not also now reset the
1793167598Srrs					 * cwnd/rto to start as if its a new
1794167598Srrs					 * address?
1795167598Srrs					 */
1796167598Srrs					sctp_set_initial_cc_param(stcb, net);
1797167598Srrs					net->RTO = stcb->asoc.initial_rto;
1798167598Srrs
1799163953Srrs				}
1800167598Srrs			}
1801167598Srrs		} else if (type == SCTP_SET_PRIM_ADDR) {
1802167598Srrs			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1803167598Srrs				/*
1804167598Srrs				 * must validate the ifa in question is in
1805167598Srrs				 * the ep
1806167598Srrs				 */
1807167598Srrs				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
1808167598Srrs					continue;
1809163953Srrs				}
1810167598Srrs			} else {
1811167598Srrs				/* Need to check scopes for this guy */
1812167598Srrs				if (sctp_is_address_in_scope(ifa,
1813167598Srrs				    stcb->asoc.ipv4_addr_legal,
1814167598Srrs				    stcb->asoc.ipv6_addr_legal,
1815167598Srrs				    stcb->asoc.loopback_scope,
1816167598Srrs				    stcb->asoc.ipv4_local_scope,
1817167598Srrs				    stcb->asoc.local_scope,
1818167598Srrs				    stcb->asoc.site_scope, 0) == 0) {
1819167598Srrs					continue;
1820167598Srrs				}
1821163953Srrs			}
1822167598Srrs
1823163953Srrs		}
1824167598Srrs		/* queue an asconf for this address add/delete */
1825167598Srrs		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1826167598Srrs			/* does the peer do asconf? */
1827167598Srrs			if (stcb->asoc.peer_supports_asconf) {
1828167598Srrs				/* queue an asconf for this addr */
1829167598Srrs				status = sctp_asconf_queue_add(stcb, ifa, type);
1830167598Srrs				/*
1831167598Srrs				 * if queued ok, and in correct state, set
1832167598Srrs				 * the ASCONF timer if in non-open state, we
1833167598Srrs				 * will set this timer when the state does
1834167598Srrs				 * go open and do all the asconf's
1835167598Srrs				 */
1836167598Srrs				if (status == 0 &&
1837167598Srrs				    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
1838167598Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
1839167598Srrs					    stcb, stcb->asoc.primary_destination);
1840167598Srrs				}
1841167598Srrs			}
1842167598Srrs		}
1843163953Srrs	}
1844163953Srrs}
1845163953Srrs
1846167598Srrsvoid
1847167598Srrssctp_iterator_end(void *ptr, uint32_t val)
1848167598Srrs{
1849167598Srrs	struct sctp_asconf_iterator *asc;
1850167598Srrs	struct sctp_ifa *ifa;
1851167598Srrs	struct sctp_laddr *l, *l_next;
1852167598Srrs
1853167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1854167598Srrs	l = LIST_FIRST(&asc->list_of_work);
1855167598Srrs	while (l != NULL) {
1856167598Srrs		l_next = LIST_NEXT(l, sctp_nxt_addr);
1857167598Srrs		ifa = l->ifa;
1858167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
1859167598Srrs			/* Clear the defer use flag */
1860167598Srrs			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
1861167598Srrs		}
1862167598Srrs		sctp_free_ifa(ifa);
1863167598Srrs		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, l);
1864167598Srrs		SCTP_DECR_LADDR_COUNT();
1865167598Srrs		l = l_next;
1866167598Srrs	}
1867170091Srrs	SCTP_FREE(asc, SCTP_M_ASC_IT);
1868167598Srrs}
1869167598Srrs
1870163953Srrs/*
1871163953Srrs * sa is the sockaddr to ask the peer to set primary to returns: 0 =
1872163953Srrs * completed, -1 = error
1873163953Srrs */
1874163953Srrsint32_t
1875163953Srrssctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
1876163953Srrs{
1877163953Srrs	/* NOTE: we currently don't check the validity of the address! */
1878163953Srrs
1879163953Srrs	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
1880163953Srrs	if (!sctp_asconf_queue_add_sa(stcb, sa, SCTP_SET_PRIM_ADDR)) {
1881163953Srrs		/* set primary queuing succeeded */
1882163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
1883163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
1884163953Srrs			    stcb->sctp_ep, stcb,
1885163953Srrs			    stcb->asoc.primary_destination);
1886163953Srrs		}
1887169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
1888169420Srrs		    "set_primary_ip_address_sa: queued on tcb=%p, ",
1889169420Srrs		    stcb);
1890169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
1891163953Srrs	} else {
1892169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
1893169420Srrs		    stcb);
1894169420Srrs		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
1895163953Srrs		return (-1);
1896163953Srrs	}
1897163953Srrs	return (0);
1898163953Srrs}
1899163953Srrs
1900163953Srrsvoid
1901167598Srrssctp_set_primary_ip_address(struct sctp_ifa *ifa)
1902163953Srrs{
1903163953Srrs	struct sctp_inpcb *inp;
1904163953Srrs
1905163953Srrs	/* go through all our PCB's */
1906163953Srrs	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
1907163953Srrs		struct sctp_tcb *stcb;
1908163953Srrs
1909163953Srrs		/* process for all associations for this endpoint */
1910163953Srrs		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1911163953Srrs			/* queue an ASCONF:SET_PRIM_ADDR to be sent */
1912163953Srrs			if (!sctp_asconf_queue_add(stcb, ifa,
1913163953Srrs			    SCTP_SET_PRIM_ADDR)) {
1914163953Srrs				/* set primary queuing succeeded */
1915163953Srrs				if (SCTP_GET_STATE(&stcb->asoc) ==
1916163953Srrs				    SCTP_STATE_OPEN) {
1917163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
1918163953Srrs					    stcb->sctp_ep, stcb,
1919163953Srrs					    stcb->asoc.primary_destination);
1920163953Srrs				}
1921169420Srrs				SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address: queued on stcb=%p, ",
1922169420Srrs				    stcb);
1923169420Srrs				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &ifa->address.sa);
1924163953Srrs			}
1925163953Srrs		}		/* for each stcb */
1926163953Srrs	}			/* for each inp */
1927163953Srrs}
1928163953Srrs
1929163953Srrsstatic struct sockaddr *
1930163953Srrssctp_find_valid_localaddr(struct sctp_tcb *stcb)
1931163953Srrs{
1932167598Srrs	struct sctp_vrf *vrf = NULL;
1933167598Srrs	struct sctp_ifn *sctp_ifn;
1934167598Srrs	struct sctp_ifa *sctp_ifa;
1935163953Srrs
1936167598Srrs	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
1937169420Srrs	if (vrf == NULL) {
1938169420Srrs		return (NULL);
1939169420Srrs	}
1940167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1941167598Srrs		if (stcb->asoc.loopback_scope == 0 &&
1942167598Srrs		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1943163953Srrs			/* Skip if loopback_scope not set */
1944163953Srrs			continue;
1945163953Srrs		}
1946167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1947167598Srrs			if (sctp_ifa->address.sa.sa_family == AF_INET &&
1948163953Srrs			    stcb->asoc.ipv4_addr_legal) {
1949163953Srrs				struct sockaddr_in *sin;
1950163953Srrs
1951167598Srrs				sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1952163953Srrs				if (sin->sin_addr.s_addr == 0) {
1953163953Srrs					/* skip unspecifed addresses */
1954163953Srrs					continue;
1955163953Srrs				}
1956163953Srrs				if (stcb->asoc.ipv4_local_scope == 0 &&
1957163953Srrs				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
1958163953Srrs					continue;
1959163953Srrs
1960167598Srrs				if (sctp_is_addr_restricted(stcb, sctp_ifa))
1961163953Srrs					continue;
1962163953Srrs				/* found a valid local v4 address to use */
1963167598Srrs				return (&sctp_ifa->address.sa);
1964167598Srrs			} else if (sctp_ifa->address.sa.sa_family == AF_INET6 &&
1965163953Srrs			    stcb->asoc.ipv6_addr_legal) {
1966163953Srrs				struct sockaddr_in6 *sin6;
1967163953Srrs
1968167598Srrs				if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1969163953Srrs					continue;
1970167598Srrs				}
1971167598Srrs				sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1972163953Srrs				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1973163953Srrs					/* we skip unspecifed addresses */
1974163953Srrs					continue;
1975163953Srrs				}
1976163953Srrs				if (stcb->asoc.local_scope == 0 &&
1977163953Srrs				    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
1978163953Srrs					continue;
1979163953Srrs				if (stcb->asoc.site_scope == 0 &&
1980163953Srrs				    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
1981163953Srrs					continue;
1982163953Srrs
1983163953Srrs				/* found a valid local v6 address to use */
1984167598Srrs				return (&sctp_ifa->address.sa);
1985163953Srrs			}
1986163953Srrs		}
1987163953Srrs	}
1988163953Srrs	/* no valid addresses found */
1989163953Srrs	return (NULL);
1990163953Srrs}
1991163953Srrs
1992163953Srrsstatic struct sockaddr *
1993163953Srrssctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
1994163953Srrs{
1995163953Srrs	struct sctp_laddr *laddr;
1996163953Srrs
1997163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1998163953Srrs		if (laddr->ifa == NULL) {
1999163953Srrs			continue;
2000163953Srrs		}
2001167598Srrs		if (laddr->ifa == NULL) {
2002163953Srrs			continue;
2003163953Srrs		}
2004163953Srrs		/* is the address restricted ? */
2005167598Srrs		if (sctp_is_addr_restricted(stcb, laddr->ifa))
2006163953Srrs			continue;
2007163953Srrs
2008163953Srrs		/* found a valid local address to use */
2009167598Srrs		return (&laddr->ifa->address.sa);
2010163953Srrs	}
2011163953Srrs	/* no valid addresses found */
2012163953Srrs	return (NULL);
2013163953Srrs}
2014163953Srrs
2015163953Srrs/*
2016163953Srrs * builds an ASCONF chunk from queued ASCONF params returns NULL on error (no
2017163953Srrs * mbuf, no ASCONF params queued, etc)
2018163953Srrs */
2019163953Srrsstruct mbuf *
2020165647Srrssctp_compose_asconf(struct sctp_tcb *stcb, int *retlen)
2021163953Srrs{
2022163953Srrs	struct mbuf *m_asconf, *m_asconf_chk;
2023163953Srrs	struct sctp_asconf_addr *aa;
2024163953Srrs	struct sctp_asconf_chunk *acp;
2025163953Srrs	struct sctp_asconf_paramhdr *aph;
2026163953Srrs	struct sctp_asconf_addr_param *aap;
2027163953Srrs	uint32_t p_length;
2028163953Srrs	uint32_t correlation_id = 1;	/* 0 is reserved... */
2029163953Srrs	caddr_t ptr, lookup_ptr;
2030163953Srrs	uint8_t lookup_used = 0;
2031163953Srrs
2032163953Srrs	/* are there any asconf params to send? */
2033163953Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
2034163953Srrs		return (NULL);
2035163953Srrs	}
2036163953Srrs	/*
2037163953Srrs	 * get a chunk header mbuf and a cluster for the asconf params since
2038163953Srrs	 * it's simpler to fill in the asconf chunk header lookup address on
2039163953Srrs	 * the fly
2040163953Srrs	 */
2041165647Srrs	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_DONTWAIT, 1, MT_DATA);
2042163953Srrs	if (m_asconf_chk == NULL) {
2043163953Srrs		/* no mbuf's */
2044169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2045169420Srrs		    "compose_asconf: couldn't get chunk mbuf!\n");
2046163953Srrs		return (NULL);
2047163953Srrs	}
2048165647Srrs	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
2049163953Srrs	if (m_asconf == NULL) {
2050163953Srrs		/* no mbuf's */
2051169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2052169420Srrs		    "compose_asconf: couldn't get mbuf!\n");
2053163953Srrs		sctp_m_freem(m_asconf_chk);
2054163953Srrs		return (NULL);
2055163953Srrs	}
2056165647Srrs	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2057165647Srrs	SCTP_BUF_LEN(m_asconf) = 0;
2058163953Srrs	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2059163953Srrs	bzero(acp, sizeof(struct sctp_asconf_chunk));
2060163953Srrs	/* save pointers to lookup address and asconf params */
2061163953Srrs	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2062163953Srrs	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2063163953Srrs
2064163953Srrs	/* fill in chunk header info */
2065163953Srrs	acp->ch.chunk_type = SCTP_ASCONF;
2066163953Srrs	acp->ch.chunk_flags = 0;
2067163953Srrs	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2068163953Srrs
2069163953Srrs	/* add parameters... up to smallest MTU allowed */
2070163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2071163953Srrs		/* get the parameter length */
2072163953Srrs		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2073163953Srrs		/* will it fit in current chunk? */
2074165647Srrs		if (SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu) {
2075163953Srrs			/* won't fit, so we're done with this chunk */
2076163953Srrs			break;
2077163953Srrs		}
2078163953Srrs		/* assign (and store) a correlation id */
2079163953Srrs		aa->ap.aph.correlation_id = correlation_id++;
2080163953Srrs
2081163953Srrs		/*
2082163953Srrs		 * fill in address if we're doing a delete this is a simple
2083163953Srrs		 * way for us to fill in the correlation address, which
2084163953Srrs		 * should only be used by the peer if we're deleting our
2085163953Srrs		 * source address and adding a new address (e.g. renumbering
2086163953Srrs		 * case)
2087163953Srrs		 */
2088163953Srrs		if (lookup_used == 0 &&
2089163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2090163953Srrs			struct sctp_ipv6addr_param *lookup;
2091163953Srrs			uint16_t p_size, addr_size;
2092163953Srrs
2093163953Srrs			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2094163953Srrs			lookup->ph.param_type =
2095163953Srrs			    htons(aa->ap.addrp.ph.param_type);
2096163953Srrs			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2097163953Srrs				/* copy IPv6 address */
2098163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2099163953Srrs				addr_size = sizeof(struct in6_addr);
2100163953Srrs			} else {
2101163953Srrs				/* copy IPv4 address */
2102163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2103163953Srrs				addr_size = sizeof(struct in_addr);
2104163953Srrs			}
2105163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2106163953Srrs			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2107165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2108163953Srrs			lookup_used = 1;
2109163953Srrs		}
2110163953Srrs		/* copy into current space */
2111163953Srrs		memcpy(ptr, &aa->ap, p_length);
2112163953Srrs
2113163953Srrs		/* network elements and update lengths */
2114163953Srrs		aph = (struct sctp_asconf_paramhdr *)ptr;
2115163953Srrs		aap = (struct sctp_asconf_addr_param *)ptr;
2116163953Srrs		/* correlation_id is transparent to peer, no htonl needed */
2117163953Srrs		aph->ph.param_type = htons(aph->ph.param_type);
2118163953Srrs		aph->ph.param_length = htons(aph->ph.param_length);
2119163953Srrs		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2120163953Srrs		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2121163953Srrs
2122165647Srrs		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2123163953Srrs		ptr += SCTP_SIZE32(p_length);
2124163953Srrs
2125163953Srrs		/*
2126163953Srrs		 * these params are removed off the pending list upon
2127163953Srrs		 * getting an ASCONF-ACK back from the peer, just set flag
2128163953Srrs		 */
2129163953Srrs		aa->sent = 1;
2130163953Srrs	}
2131163953Srrs	/* check to see if the lookup addr has been populated yet */
2132163953Srrs	if (lookup_used == 0) {
2133163953Srrs		/* NOTE: if the address param is optional, can skip this... */
2134163953Srrs		/* add any valid (existing) address... */
2135163953Srrs		struct sctp_ipv6addr_param *lookup;
2136163953Srrs		uint16_t p_size, addr_size;
2137163953Srrs		struct sockaddr *found_addr;
2138163953Srrs		caddr_t addr_ptr;
2139163953Srrs
2140163953Srrs		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2141163953Srrs			found_addr = sctp_find_valid_localaddr(stcb);
2142163953Srrs		else
2143163953Srrs			found_addr = sctp_find_valid_localaddr_ep(stcb);
2144163953Srrs
2145163953Srrs		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2146163953Srrs		if (found_addr != NULL) {
2147163953Srrs			if (found_addr->sa_family == AF_INET6) {
2148163953Srrs				/* copy IPv6 address */
2149163953Srrs				lookup->ph.param_type =
2150163953Srrs				    htons(SCTP_IPV6_ADDRESS);
2151163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2152163953Srrs				addr_size = sizeof(struct in6_addr);
2153163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2154163953Srrs				    found_addr)->sin6_addr;
2155163953Srrs			} else {
2156163953Srrs				/* copy IPv4 address */
2157163953Srrs				lookup->ph.param_type =
2158163953Srrs				    htons(SCTP_IPV4_ADDRESS);
2159163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2160163953Srrs				addr_size = sizeof(struct in_addr);
2161163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2162163953Srrs				    found_addr)->sin_addr;
2163163953Srrs			}
2164163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2165163953Srrs			memcpy(lookup->addr, addr_ptr, addr_size);
2166165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2167163953Srrs			lookup_used = 1;
2168163953Srrs		} else {
2169163953Srrs			/* uh oh... don't have any address?? */
2170169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
2171169420Srrs			    "compose_asconf: no lookup addr!\n");
2172163953Srrs			/* for now, we send a IPv4 address of 0.0.0.0 */
2173163953Srrs			lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2174163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2175163953Srrs			bzero(lookup->addr, sizeof(struct in_addr));
2176165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2177163953Srrs			lookup_used = 1;
2178163953Srrs		}
2179163953Srrs	}
2180163953Srrs	/* chain it all together */
2181165647Srrs	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
2182169655Srrs	*retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
2183169655Srrs	acp->ch.chunk_length = ntohs(*retlen);
2184163953Srrs
2185163953Srrs	/* update "sent" flag */
2186163953Srrs	stcb->asoc.asconf_sent++;
2187163953Srrs
2188163953Srrs	return (m_asconf_chk);
2189163953Srrs}
2190163953Srrs
2191163953Srrs/*
2192163953Srrs * section to handle address changes before an association is up eg. changes
2193163953Srrs * during INIT/INIT-ACK/COOKIE-ECHO handshake
2194163953Srrs */
2195163953Srrs
2196163953Srrs/*
2197163953Srrs * processes the (local) addresses in the INIT-ACK chunk
2198163953Srrs */
2199163953Srrsstatic void
2200163953Srrssctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2201163953Srrs    unsigned int offset, unsigned int length)
2202163953Srrs{
2203163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2204163953Srrs	uint16_t plen, ptype;
2205167598Srrs	struct sctp_ifa *sctp_ifa;
2206163953Srrs	struct sctp_ipv6addr_param addr_store;
2207163953Srrs	struct sockaddr_in6 sin6;
2208163953Srrs	struct sockaddr_in sin;
2209163953Srrs	struct sockaddr *sa;
2210167598Srrs	uint32_t vrf_id;
2211163953Srrs
2212169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
2213169420Srrs	if (stcb == NULL)	/* Un-needed check for SA */
2214169420Srrs		return;
2215163953Srrs
2216163953Srrs	/* convert to upper bound */
2217163953Srrs	length += offset;
2218163953Srrs
2219163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2220163953Srrs		return;
2221163953Srrs	}
2222163953Srrs	/* init the addresses */
2223163953Srrs	bzero(&sin6, sizeof(sin6));
2224163953Srrs	sin6.sin6_family = AF_INET6;
2225163953Srrs	sin6.sin6_len = sizeof(sin6);
2226163953Srrs	sin6.sin6_port = stcb->rport;
2227163953Srrs
2228163953Srrs	bzero(&sin, sizeof(sin));
2229163953Srrs	sin.sin_len = sizeof(sin);
2230163953Srrs	sin.sin_family = AF_INET;
2231163953Srrs	sin.sin_port = stcb->rport;
2232163953Srrs
2233163953Srrs	/* go through the addresses in the init-ack */
2234163953Srrs	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2235163953Srrs	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2236163953Srrs	while (ph != NULL) {
2237163953Srrs		ptype = ntohs(ph->param_type);
2238163953Srrs		plen = ntohs(ph->param_length);
2239163953Srrs		if (ptype == SCTP_IPV6_ADDRESS) {
2240163953Srrs			struct sctp_ipv6addr_param *a6p;
2241163953Srrs
2242163953Srrs			/* get the entire IPv6 address param */
2243163953Srrs			a6p = (struct sctp_ipv6addr_param *)
2244163953Srrs			    sctp_m_getptr(m, offset,
2245163953Srrs			    sizeof(struct sctp_ipv6addr_param),
2246163953Srrs			    (uint8_t *) & addr_store);
2247163953Srrs			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2248163953Srrs			    a6p == NULL) {
2249163953Srrs				return;
2250163953Srrs			}
2251163953Srrs			memcpy(&sin6.sin6_addr, a6p->addr,
2252163953Srrs			    sizeof(struct in6_addr));
2253163953Srrs			sa = (struct sockaddr *)&sin6;
2254163953Srrs		} else if (ptype == SCTP_IPV4_ADDRESS) {
2255163953Srrs			struct sctp_ipv4addr_param *a4p;
2256163953Srrs
2257163953Srrs			/* get the entire IPv4 address param */
2258167598Srrs			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
2259167598Srrs			    sizeof(struct sctp_ipv4addr_param),
2260167598Srrs			    (uint8_t *) & addr_store);
2261163953Srrs			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2262163953Srrs			    a4p == NULL) {
2263163953Srrs				return;
2264163953Srrs			}
2265163953Srrs			sin.sin_addr.s_addr = a4p->addr;
2266163953Srrs			sa = (struct sockaddr *)&sin;
2267163953Srrs		} else {
2268163953Srrs			goto next_addr;
2269163953Srrs		}
2270163953Srrs
2271163953Srrs		/* see if this address really (still) exists */
2272168299Srrs		if (stcb) {
2273168299Srrs			vrf_id = stcb->asoc.vrf_id;
2274168299Srrs		} else {
2275168299Srrs			vrf_id = SCTP_DEFAULT_VRFID;
2276168299Srrs		}
2277168299Srrs
2278167598Srrs		sctp_ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
2279167598Srrs		if (sctp_ifa == NULL) {
2280163953Srrs			/* address doesn't exist anymore */
2281163953Srrs			int status;
2282163953Srrs
2283163953Srrs			/* are ASCONFs allowed ? */
2284163953Srrs			if ((sctp_is_feature_on(stcb->sctp_ep,
2285163953Srrs			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2286163953Srrs			    stcb->asoc.peer_supports_asconf) {
2287163953Srrs				/* queue an ASCONF DEL_IP_ADDRESS */
2288163953Srrs				status = sctp_asconf_queue_add_sa(stcb, sa,
2289163953Srrs				    SCTP_DEL_IP_ADDRESS);
2290163953Srrs				/*
2291163953Srrs				 * if queued ok, and in correct state, set
2292163953Srrs				 * the ASCONF timer
2293163953Srrs				 */
2294163953Srrs				if (status == 0 &&
2295163953Srrs				    SCTP_GET_STATE(&stcb->asoc) ==
2296163953Srrs				    SCTP_STATE_OPEN) {
2297163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2298163953Srrs					    stcb->sctp_ep, stcb,
2299163953Srrs					    stcb->asoc.primary_destination);
2300163953Srrs				}
2301163953Srrs			}
2302163953Srrs		}
2303163953Srrsnext_addr:
2304163953Srrs		/*
2305163953Srrs		 * Sanity check:  Make sure the length isn't 0, otherwise
2306163953Srrs		 * we'll be stuck in this loop for a long time...
2307163953Srrs		 */
2308163953Srrs		if (SCTP_SIZE32(plen) == 0) {
2309169420Srrs			SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
2310163953Srrs			    plen, ptype);
2311163953Srrs			return;
2312163953Srrs		}
2313163953Srrs		/* get next parameter */
2314163953Srrs		offset += SCTP_SIZE32(plen);
2315163953Srrs		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2316163953Srrs			return;
2317163953Srrs		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2318163953Srrs		    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2319163953Srrs	}			/* while */
2320163953Srrs}
2321163953Srrs
2322163953Srrs/* FIX ME: need to verify return result for v6 address type if v6 disabled */
2323163953Srrs/*
2324163953Srrs * checks to see if a specific address is in the initack address list returns
2325163953Srrs * 1 if found, 0 if not
2326163953Srrs */
2327163953Srrsstatic uint32_t
2328163953Srrssctp_addr_in_initack(struct sctp_tcb *stcb, struct mbuf *m, uint32_t offset,
2329163953Srrs    uint32_t length, struct sockaddr *sa)
2330163953Srrs{
2331163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2332163953Srrs	uint16_t plen, ptype;
2333163953Srrs	struct sctp_ipv6addr_param addr_store;
2334163953Srrs	struct sockaddr_in *sin;
2335163953Srrs	struct sctp_ipv4addr_param *a4p;
2336163953Srrs
2337163953Srrs#ifdef INET6
2338167598Srrs	struct sockaddr_in6 *sin6;
2339163953Srrs	struct sctp_ipv6addr_param *a6p;
2340167598Srrs	struct sockaddr_in6 sin6_tmp;
2341163953Srrs
2342163953Srrs#endif				/* INET6 */
2343163953Srrs
2344163953Srrs	if (
2345163953Srrs#ifdef INET6
2346163953Srrs	    (sa->sa_family != AF_INET6) &&
2347163953Srrs#endif				/* INET6 */
2348163953Srrs	    (sa->sa_family != AF_INET))
2349163953Srrs		return (0);
2350163953Srrs
2351169420Srrs	SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
2352169420Srrs	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
2353163953Srrs	/* convert to upper bound */
2354163953Srrs	length += offset;
2355163953Srrs
2356163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2357169420Srrs		SCTPDBG(SCTP_DEBUG_ASCONF1,
2358169420Srrs		    "find_initack_addr: invalid offset?\n");
2359163953Srrs		return (0);
2360163953Srrs	}
2361163953Srrs	/* go through the addresses in the init-ack */
2362163953Srrs	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2363163953Srrs	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2364163953Srrs	while (ph != NULL) {
2365163953Srrs		ptype = ntohs(ph->param_type);
2366163953Srrs		plen = ntohs(ph->param_length);
2367163953Srrs#ifdef INET6
2368163953Srrs		if (ptype == SCTP_IPV6_ADDRESS && sa->sa_family == AF_INET6) {
2369163953Srrs			/* get the entire IPv6 address param */
2370163953Srrs			a6p = (struct sctp_ipv6addr_param *)
2371163953Srrs			    sctp_m_getptr(m, offset,
2372163953Srrs			    sizeof(struct sctp_ipv6addr_param),
2373163953Srrs			    (uint8_t *) & addr_store);
2374163953Srrs			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2375169420Srrs			    (ph == NULL) ||
2376169420Srrs			    (a6p == NULL)) {
2377163953Srrs				return (0);
2378163953Srrs			}
2379163953Srrs			sin6 = (struct sockaddr_in6 *)sa;
2380163953Srrs			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2381163953Srrs				/* create a copy and clear scope */
2382163953Srrs				memcpy(&sin6_tmp, sin6,
2383163953Srrs				    sizeof(struct sockaddr_in6));
2384163953Srrs				sin6 = &sin6_tmp;
2385163953Srrs				in6_clearscope(&sin6->sin6_addr);
2386163953Srrs			}
2387163953Srrs			if (memcmp(&sin6->sin6_addr, a6p->addr,
2388163953Srrs			    sizeof(struct in6_addr)) == 0) {
2389163953Srrs				/* found it */
2390163953Srrs				return (1);
2391163953Srrs			}
2392163953Srrs		} else
2393163953Srrs#endif				/* INET6 */
2394163953Srrs
2395163953Srrs			if (ptype == SCTP_IPV4_ADDRESS &&
2396163953Srrs		    sa->sa_family == AF_INET) {
2397163953Srrs			/* get the entire IPv4 address param */
2398163953Srrs			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m,
2399163953Srrs			    offset, sizeof(struct sctp_ipv4addr_param),
2400163953Srrs			    (uint8_t *) & addr_store);
2401163953Srrs			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2402169420Srrs			    (ph == NULL) ||
2403169420Srrs			    (a4p == NULL)) {
2404163953Srrs				return (0);
2405163953Srrs			}
2406163953Srrs			sin = (struct sockaddr_in *)sa;
2407163953Srrs			if (sin->sin_addr.s_addr == a4p->addr) {
2408163953Srrs				/* found it */
2409163953Srrs				return (1);
2410163953Srrs			}
2411163953Srrs		}
2412163953Srrs		/* get next parameter */
2413163953Srrs		offset += SCTP_SIZE32(plen);
2414163953Srrs		if (offset + sizeof(struct sctp_paramhdr) > length)
2415163953Srrs			return (0);
2416163953Srrs		ph = (struct sctp_paramhdr *)
2417163953Srrs		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
2418163953Srrs		    (uint8_t *) & tmp_param);
2419163953Srrs	}			/* while */
2420163953Srrs	/* not found! */
2421163953Srrs	return (0);
2422163953Srrs}
2423163953Srrs
2424163953Srrs/*
2425163953Srrs * makes sure that the current endpoint local addr list is consistent with
2426163953Srrs * the new association (eg. subset bound, asconf allowed) adds addresses as
2427163953Srrs * necessary
2428163953Srrs */
2429163953Srrsstatic void
2430163953Srrssctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2431163953Srrs    int length, struct sockaddr *init_addr)
2432163953Srrs{
2433163953Srrs	struct sctp_laddr *laddr;
2434163953Srrs
2435163953Srrs	/* go through the endpoint list */
2436163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2437163953Srrs		/* be paranoid and validate the laddr */
2438163953Srrs		if (laddr->ifa == NULL) {
2439169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1,
2440169420Srrs			    "check_addr_list_ep: laddr->ifa is NULL");
2441163953Srrs			continue;
2442163953Srrs		}
2443167598Srrs		if (laddr->ifa == NULL) {
2444169420Srrs			SCTPDBG(SCTP_DEBUG_ASCONF1, "check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
2445163953Srrs			continue;
2446163953Srrs		}
2447163953Srrs		/* do i have it implicitly? */
2448167598Srrs		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
2449163953Srrs			continue;
2450163953Srrs		}
2451163953Srrs		/* check to see if in the init-ack */
2452163953Srrs		if (!sctp_addr_in_initack(stcb, m, offset, length,
2453167598Srrs		    &laddr->ifa->address.sa)) {
2454163953Srrs			/* try to add it */
2455163953Srrs			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
2456163953Srrs			    SCTP_ADD_IP_ADDRESS);
2457163953Srrs		}
2458163953Srrs	}
2459163953Srrs}
2460163953Srrs
2461163953Srrs/*
2462163953Srrs * makes sure that the current kernel address list is consistent with the new
2463163953Srrs * association (with all addrs bound) adds addresses as necessary
2464163953Srrs */
2465163953Srrsstatic void
2466163953Srrssctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2467163953Srrs    int length, struct sockaddr *init_addr,
2468163953Srrs    uint16_t local_scope, uint16_t site_scope,
2469163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
2470163953Srrs{
2471167598Srrs	struct sctp_vrf *vrf = NULL;
2472167598Srrs	struct sctp_ifn *sctp_ifn;
2473167598Srrs	struct sctp_ifa *sctp_ifa;
2474167598Srrs	uint32_t vrf_id;
2475163953Srrs
2476168299Srrs	if (stcb) {
2477168299Srrs		vrf_id = stcb->asoc.vrf_id;
2478168299Srrs	} else {
2479169420Srrs		return;
2480168299Srrs	}
2481167598Srrs	vrf = sctp_find_vrf(vrf_id);
2482167598Srrs	if (vrf == NULL) {
2483167598Srrs		return;
2484167598Srrs	}
2485163953Srrs	/* go through all our known interfaces */
2486167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2487167598Srrs		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2488163953Srrs			/* skip loopback interface */
2489163953Srrs			continue;
2490163953Srrs		}
2491163953Srrs		/* go through each interface address */
2492167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2493163953Srrs			/* do i have it implicitly? */
2494167598Srrs			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
2495163953Srrs				continue;
2496163953Srrs			}
2497163953Srrs			/* check to see if in the init-ack */
2498163953Srrs			if (!sctp_addr_in_initack(stcb, m, offset, length,
2499167598Srrs			    &sctp_ifa->address.sa)) {
2500163953Srrs				/* try to add it */
2501163953Srrs				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
2502167598Srrs				    sctp_ifa, SCTP_ADD_IP_ADDRESS);
2503163953Srrs			}
2504163953Srrs		}		/* end foreach ifa */
2505163953Srrs	}			/* end foreach ifn */
2506163953Srrs}
2507163953Srrs
2508163953Srrs/*
2509163953Srrs * validates an init-ack chunk (from a cookie-echo) with current addresses
2510163953Srrs * adds addresses from the init-ack into our local address list, if needed
2511163953Srrs * queues asconf adds/deletes addresses as needed and makes appropriate list
2512163953Srrs * changes for source address selection m, offset: points to the start of the
2513163953Srrs * address list in an init-ack chunk length: total length of the address
2514163953Srrs * params only init_addr: address where my INIT-ACK was sent from
2515163953Srrs */
2516163953Srrsvoid
2517163953Srrssctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2518163953Srrs    int length, struct sockaddr *init_addr,
2519163953Srrs    uint16_t local_scope, uint16_t site_scope,
2520163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
2521163953Srrs{
2522163953Srrs	/* process the local addresses in the initack */
2523163953Srrs	sctp_process_initack_addresses(stcb, m, offset, length);
2524163953Srrs
2525163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2526163953Srrs		/* bound all case */
2527163953Srrs		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
2528163953Srrs		    local_scope, site_scope, ipv4_scope, loopback_scope);
2529163953Srrs	} else {
2530163953Srrs		/* subset bound case */
2531163953Srrs		if (sctp_is_feature_on(stcb->sctp_ep,
2532163953Srrs		    SCTP_PCB_FLAGS_DO_ASCONF)) {
2533163953Srrs			/* asconf's allowed */
2534163953Srrs			sctp_check_address_list_ep(stcb, m, offset, length,
2535163953Srrs			    init_addr);
2536163953Srrs		}
2537163953Srrs		/* else, no asconfs allowed, so what we sent is what we get */
2538163953Srrs	}
2539163953Srrs}
2540163953Srrs
2541163953Srrs/*
2542163953Srrs * sctp_bindx() support
2543163953Srrs */
2544163953Srrsuint32_t
2545169655Srrssctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
2546169655Srrs    uint32_t type, uint32_t vrf_id)
2547163953Srrs{
2548167598Srrs	struct sctp_ifa *ifa;
2549163953Srrs
2550167598Srrs	if (sa->sa_len == 0) {
2551163953Srrs		return (EINVAL);
2552167598Srrs	}
2553167598Srrs	if (type == SCTP_ADD_IP_ADDRESS) {
2554167598Srrs		/* For an add the address MUST be on the system */
2555167598Srrs		ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
2556167598Srrs	} else if (type == SCTP_DEL_IP_ADDRESS) {
2557167598Srrs		/* For a delete we need to find it in the inp */
2558167598Srrs		ifa = sctp_find_ifa_in_ep(inp, sa, 0);
2559167598Srrs	} else {
2560167598Srrs		ifa = NULL;
2561167598Srrs	}
2562163953Srrs	if (ifa != NULL) {
2563167598Srrs		/* add this address */
2564167598Srrs		struct sctp_asconf_iterator *asc;
2565167598Srrs		struct sctp_laddr *wi;
2566163953Srrs
2567167598Srrs		SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
2568169655Srrs		    sizeof(struct sctp_asconf_iterator),
2569170091Srrs		    SCTP_M_ASC_IT);
2570167598Srrs		if (asc == NULL) {
2571167598Srrs			return (ENOMEM);
2572167598Srrs		}
2573169655Srrs		wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr,
2574169655Srrs		    struct sctp_laddr);
2575167598Srrs		if (wi == NULL) {
2576170091Srrs			SCTP_FREE(asc, SCTP_M_ASC_IT);
2577167598Srrs			return (ENOMEM);
2578167598Srrs		}
2579167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
2580167598Srrs			sctp_add_local_addr_ep(inp, ifa, type);
2581167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
2582167598Srrs			struct sctp_laddr *laddr;
2583167598Srrs
2584169655Srrs			if (inp->laddr_count < 2) {
2585169655Srrs				/* can't delete the last local address */
2586169655Srrs				return (EINVAL);
2587169655Srrs			}
2588169655Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list,
2589169655Srrs			    sctp_nxt_addr) {
2590167598Srrs				if (ifa == laddr->ifa) {
2591167598Srrs					/* Mark in the delete */
2592167598Srrs					laddr->action = type;
2593167598Srrs				}
2594163953Srrs			}
2595163953Srrs		}
2596167598Srrs		LIST_INIT(&asc->list_of_work);
2597167598Srrs		asc->cnt = 1;
2598167598Srrs		SCTP_INCR_LADDR_COUNT();
2599167598Srrs		wi->ifa = ifa;
2600167598Srrs		wi->action = type;
2601167598Srrs		atomic_add_int(&ifa->refcount, 1);
2602167598Srrs		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
2603169420Srrs		(void)sctp_initiate_iterator(sctp_iterator_ep,
2604167598Srrs		    sctp_iterator_stcb,
2605167598Srrs		    sctp_iterator_ep_end,
2606167598Srrs		    SCTP_PCB_ANY_FLAGS,
2607169655Srrs		    SCTP_PCB_ANY_FEATURES,
2608169655Srrs		    SCTP_ASOC_ANY_STATE, (void *)asc, 0,
2609167598Srrs		    sctp_iterator_end, inp, 0);
2610163953Srrs	} else {
2611163953Srrs		/* invalid address! */
2612163953Srrs		return (EADDRNOTAVAIL);
2613163953Srrs	}
2614163953Srrs	return (0);
2615163953Srrs}
2616