sctp_asconf.c revision 169352
1163953Srrs/*-
2166675Srrs * Copyright (c) 2001-2007, 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 169352 2007-05-08 00:21:05Z 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
53163953Srrsstatic int
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;
73163953Srrs		return 0;
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;
87163953Srrs		return 0;
88163953Srrs	}
89163953Srrs#endif				/* INET6 */
90163953Srrs	else
91163953Srrs		return -1;
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) {
127163953Srrs#ifdef SCTP_DEBUG
128163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
129163953Srrs			printf("asconf_success_response: couldn't get mbuf!\n");
130163953Srrs		}
131163953Srrs#endif				/* SCTP_DEBUG */
132163953Srrs		return NULL;
133163953Srrs	}
134163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
135163953Srrs	aph->correlation_id = id;
136163953Srrs	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
137163953Srrs	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
138165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
139163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
140163953Srrs
141163953Srrs	return m_reply;
142163953Srrs}
143163953Srrs
144163953Srrsstatic struct mbuf *
145163953Srrssctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t * error_tlv,
146163953Srrs    uint16_t tlv_length)
147163953Srrs{
148163953Srrs	struct mbuf *m_reply = NULL;
149163953Srrs	struct sctp_asconf_paramhdr *aph;
150163953Srrs	struct sctp_error_cause *error;
151163953Srrs	uint8_t *tlv;
152163953Srrs
153163953Srrs	m_reply = sctp_get_mbuf_for_msg((sizeof(struct sctp_asconf_paramhdr) +
154163953Srrs	    tlv_length +
155163953Srrs	    sizeof(struct sctp_error_cause)),
156163953Srrs	    0, M_DONTWAIT, 1, MT_DATA);
157163953Srrs	if (m_reply == NULL) {
158163953Srrs#ifdef SCTP_DEBUG
159163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
160163953Srrs			printf("asconf_error_response: couldn't get mbuf!\n");
161163953Srrs		}
162163953Srrs#endif				/* SCTP_DEBUG */
163163953Srrs		return NULL;
164163953Srrs	}
165163953Srrs	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
166163953Srrs	error = (struct sctp_error_cause *)(aph + 1);
167163953Srrs
168163953Srrs	aph->correlation_id = id;
169163953Srrs	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
170163953Srrs	error->code = htons(cause);
171163953Srrs	error->length = tlv_length + sizeof(struct sctp_error_cause);
172163953Srrs	aph->ph.param_length = error->length +
173163953Srrs	    sizeof(struct sctp_asconf_paramhdr);
174163953Srrs
175163953Srrs	if (aph->ph.param_length > MLEN) {
176163953Srrs#ifdef SCTP_DEBUG
177163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
178163953Srrs			printf("asconf_error_response: tlv_length (%xh) too big\n",
179163953Srrs			    tlv_length);
180163953Srrs		}
181163953Srrs#endif				/* SCTP_DEBUG */
182163953Srrs		sctp_m_freem(m_reply);	/* discard */
183163953Srrs		return NULL;
184163953Srrs	}
185163953Srrs	if (error_tlv != NULL) {
186163953Srrs		tlv = (uint8_t *) (error + 1);
187163953Srrs		memcpy(tlv, error_tlv, tlv_length);
188163953Srrs	}
189165647Srrs	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
190163953Srrs	error->length = htons(error->length);
191163953Srrs	aph->ph.param_length = htons(aph->ph.param_length);
192163953Srrs
193163953Srrs	return m_reply;
194163953Srrs}
195163953Srrs
196163953Srrsstatic struct mbuf *
197163953Srrssctp_process_asconf_add_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
198163953Srrs    struct sctp_tcb *stcb, int response_required)
199163953Srrs{
200163953Srrs	struct mbuf *m_reply = NULL;
201163953Srrs	struct sockaddr_storage sa_source, sa_store;
202163953Srrs	struct sctp_ipv4addr_param *v4addr;
203163953Srrs	uint16_t param_type, param_length, aparam_length;
204163953Srrs	struct sockaddr *sa;
205163953Srrs	struct sockaddr_in *sin;
206163953Srrs	int zero_address = 0;
207163953Srrs
208163953Srrs#ifdef INET6
209163953Srrs	struct sockaddr_in6 *sin6;
210163953Srrs	struct sctp_ipv6addr_param *v6addr;
211163953Srrs
212163953Srrs#endif				/* INET6 */
213163953Srrs
214163953Srrs	aparam_length = ntohs(aph->ph.param_length);
215163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
216163953Srrs#ifdef INET6
217163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
218163953Srrs#endif				/* INET6 */
219163953Srrs	param_type = ntohs(v4addr->ph.param_type);
220163953Srrs	param_length = ntohs(v4addr->ph.param_length);
221163953Srrs
222163953Srrs	sa = (struct sockaddr *)&sa_store;
223163953Srrs	switch (param_type) {
224163953Srrs	case SCTP_IPV4_ADDRESS:
225163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
226163953Srrs			/* invalid param size */
227163953Srrs			return NULL;
228163953Srrs		}
229163953Srrs		sin = (struct sockaddr_in *)&sa_store;
230163953Srrs		bzero(sin, sizeof(*sin));
231163953Srrs		sin->sin_family = AF_INET;
232163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
233163953Srrs		sin->sin_port = stcb->rport;
234163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
235163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
236163953Srrs			zero_address = 1;
237163953Srrs#ifdef SCTP_DEBUG
238163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
239163953Srrs			printf("process_asconf_add_ip: adding ");
240163953Srrs			sctp_print_address(sa);
241163953Srrs		}
242163953Srrs#endif				/* SCTP_DEBUG */
243163953Srrs		break;
244163953Srrs	case SCTP_IPV6_ADDRESS:
245163953Srrs#ifdef INET6
246163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
247163953Srrs			/* invalid param size */
248163953Srrs			return NULL;
249163953Srrs		}
250163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
251163953Srrs		bzero(sin6, sizeof(*sin6));
252163953Srrs		sin6->sin6_family = AF_INET6;
253163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
254163953Srrs		sin6->sin6_port = stcb->rport;
255163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
256163953Srrs		    sizeof(struct in6_addr));
257163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
258163953Srrs			zero_address = 1;
259163953Srrs#ifdef SCTP_DEBUG
260163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
261163953Srrs			printf("process_asconf_add_ip: adding ");
262163953Srrs			sctp_print_address(sa);
263163953Srrs		}
264163953Srrs#endif				/* SCTP_DEBUG */
265163953Srrs#else
266163953Srrs		/* IPv6 not enabled! */
267163953Srrs		/* FIX ME: currently sends back an invalid param error */
268163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
269163953Srrs		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph, aparam_length);
270163953Srrs#ifdef SCTP_DEBUG
271163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
272163953Srrs			printf("process_asconf_add_ip: v6 disabled- skipping ");
273163953Srrs			sctp_print_address(sa);
274163953Srrs		}
275163953Srrs#endif				/* SCTP_DEBUG */
276163953Srrs		return m_reply;
277163953Srrs#endif				/* INET6 */
278163953Srrs		break;
279163953Srrs	default:
280163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
281163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
282163953Srrs		    aparam_length);
283163953Srrs		return m_reply;
284163953Srrs	}			/* end switch */
285163953Srrs
286163953Srrs	/* if 0.0.0.0/::0, add the source address instead */
287165647Srrs	if (zero_address && sctp_nat_friendly) {
288163953Srrs		sa = (struct sockaddr *)&sa_source;
289163953Srrs		sctp_asconf_get_source_ip(m, sa);
290163953Srrs#ifdef SCTP_DEBUG
291163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
292163953Srrs			printf("process_asconf_add_ip: using source addr ");
293163953Srrs			sctp_print_address(sa);
294163953Srrs		}
295163953Srrs#endif				/* SCTP_DEBUG */
296163953Srrs	}
297163953Srrs	/* add the address */
298165220Srrs	if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE,
299165220Srrs	    SCTP_ADDR_DYNAMIC_ADDED) != 0) {
300163953Srrs#ifdef SCTP_DEBUG
301163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
302163953Srrs			printf("process_asconf_add_ip: error adding address\n");
303163953Srrs		}
304163953Srrs#endif				/* SCTP_DEBUG */
305163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
306163953Srrs		    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
307163953Srrs		    aparam_length);
308163953Srrs	} else {
309163953Srrs		/* notify upper layer */
310163953Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa);
311163953Srrs		if (response_required) {
312163953Srrs			m_reply =
313163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
314163953Srrs		}
315165220Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_ASCONF + SCTP_LOC_1);
316163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, NULL);
317163953Srrs	}
318163953Srrs
319163953Srrs	return m_reply;
320163953Srrs}
321163953Srrs
322163953Srrsstatic int
323163953Srrssctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb,
324163953Srrs    struct sockaddr *src)
325163953Srrs{
326163953Srrs	struct sctp_nets *src_net, *net;
327163953Srrs
328163953Srrs	/* make sure the source address exists as a destination net */
329163953Srrs	src_net = sctp_findnet(stcb, src);
330163953Srrs	if (src_net == NULL) {
331163953Srrs		/* not found */
332163953Srrs		return -1;
333163953Srrs	}
334163953Srrs	/* delete all destination addresses except the source */
335163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
336163953Srrs		if (net != src_net) {
337163953Srrs			/* delete this address */
338163953Srrs			sctp_remove_net(stcb, net);
339163953Srrs#ifdef SCTP_DEBUG
340163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
341163953Srrs				printf("asconf_del_remote_addrs_except: deleting ");
342163953Srrs				sctp_print_address((struct sockaddr *)&net->ro._l_addr);
343163953Srrs			}
344163953Srrs#endif
345163953Srrs			/* notify upper layer */
346163953Srrs			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
347163953Srrs			    (struct sockaddr *)&net->ro._l_addr);
348163953Srrs		}
349163953Srrs	}
350163953Srrs	return 0;
351163953Srrs}
352163953Srrs
353163953Srrsstatic struct mbuf *
354163953Srrssctp_process_asconf_delete_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
355163953Srrs    struct sctp_tcb *stcb, int response_required)
356163953Srrs{
357163953Srrs	struct mbuf *m_reply = NULL;
358163953Srrs	struct sockaddr_storage sa_source, sa_store;
359163953Srrs	struct sctp_ipv4addr_param *v4addr;
360163953Srrs	uint16_t param_type, param_length, aparam_length;
361163953Srrs	struct sockaddr *sa;
362163953Srrs	struct sockaddr_in *sin;
363163953Srrs	int zero_address = 0;
364163953Srrs	int result;
365163953Srrs
366163953Srrs#ifdef INET6
367163953Srrs	struct sockaddr_in6 *sin6;
368163953Srrs	struct sctp_ipv6addr_param *v6addr;
369163953Srrs
370163953Srrs#endif				/* INET6 */
371163953Srrs
372163953Srrs	/* get the source IP address for src and 0.0.0.0/::0 delete checks */
373163953Srrs	sctp_asconf_get_source_ip(m, (struct sockaddr *)&sa_source);
374163953Srrs
375163953Srrs	aparam_length = ntohs(aph->ph.param_length);
376163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
377163953Srrs#ifdef INET6
378163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
379163953Srrs#endif				/* INET6 */
380163953Srrs	param_type = ntohs(v4addr->ph.param_type);
381163953Srrs	param_length = ntohs(v4addr->ph.param_length);
382163953Srrs
383163953Srrs	sa = (struct sockaddr *)&sa_store;
384163953Srrs	switch (param_type) {
385163953Srrs	case SCTP_IPV4_ADDRESS:
386163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
387163953Srrs			/* invalid param size */
388163953Srrs			return NULL;
389163953Srrs		}
390163953Srrs		sin = (struct sockaddr_in *)&sa_store;
391163953Srrs		bzero(sin, sizeof(*sin));
392163953Srrs		sin->sin_family = AF_INET;
393163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
394163953Srrs		sin->sin_port = stcb->rport;
395163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
396163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
397163953Srrs			zero_address = 1;
398163953Srrs#ifdef SCTP_DEBUG
399163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
400163953Srrs			printf("process_asconf_delete_ip: deleting ");
401163953Srrs			sctp_print_address(sa);
402163953Srrs		}
403163953Srrs#endif				/* SCTP_DEBUG */
404163953Srrs		break;
405163953Srrs	case SCTP_IPV6_ADDRESS:
406163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
407163953Srrs			/* invalid param size */
408163953Srrs			return NULL;
409163953Srrs		}
410163953Srrs#ifdef INET6
411163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
412163953Srrs		bzero(sin6, sizeof(*sin6));
413163953Srrs		sin6->sin6_family = AF_INET6;
414163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
415163953Srrs		sin6->sin6_port = stcb->rport;
416163953Srrs		memcpy(&sin6->sin6_addr, v6addr->addr,
417163953Srrs		    sizeof(struct in6_addr));
418163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
419163953Srrs			zero_address = 1;
420163953Srrs#ifdef SCTP_DEBUG
421163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
422163953Srrs			printf("process_asconf_delete_ip: deleting ");
423163953Srrs			sctp_print_address(sa);
424163953Srrs		}
425163953Srrs#endif				/* SCTP_DEBUG */
426163953Srrs#else
427163953Srrs		/* IPv6 not enabled!  No "action" needed; just ack it */
428163953Srrs#ifdef SCTP_DEBUG
429163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
430163953Srrs			printf("process_asconf_delete_ip: v6 disabled- ignoring: ");
431163953Srrs			sctp_print_address(sa);
432163953Srrs		}
433163953Srrs#endif				/* SCTP_DEBUG */
434163953Srrs		/* just respond with a "success" ASCONF-ACK */
435163953Srrs		return NULL;
436163953Srrs#endif				/* INET6 */
437163953Srrs		break;
438163953Srrs	default:
439163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
440163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
441163953Srrs		    aparam_length);
442163953Srrs		return m_reply;
443163953Srrs	}
444163953Srrs
445163953Srrs	/* make sure the source address is not being deleted */
446163953Srrs	if (sctp_cmpaddr(sa, (struct sockaddr *)&sa_source)) {
447163953Srrs		/* trying to delete the source address! */
448163953Srrs#ifdef SCTP_DEBUG
449163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
450163953Srrs			printf("process_asconf_delete_ip: tried to delete source addr\n");
451163953Srrs		}
452163953Srrs#endif				/* SCTP_DEBUG */
453163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
454163953Srrs		    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
455163953Srrs		    aparam_length);
456163953Srrs		return m_reply;
457163953Srrs	}
458163953Srrs	/* if deleting 0.0.0.0/::0, delete all addresses except src addr */
459165647Srrs	if (zero_address && sctp_nat_friendly) {
460163953Srrs		result = sctp_asconf_del_remote_addrs_except(stcb,
461163953Srrs		    (struct sockaddr *)&sa_source);
462163953Srrs
463163953Srrs		if (result) {
464163953Srrs			/* src address did not exist? */
465163953Srrs#ifdef SCTP_DEBUG
466163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
467163953Srrs				printf("process_asconf_delete_ip: src addr does not exist?\n");
468163953Srrs			}
469163953Srrs#endif				/* SCTP_DEBUG */
470163953Srrs			/* what error to reply with?? */
471163953Srrs			m_reply =
472163953Srrs			    sctp_asconf_error_response(aph->correlation_id,
473163953Srrs			    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
474163953Srrs			    aparam_length);
475163953Srrs		} else if (response_required) {
476163953Srrs			m_reply =
477163953Srrs			    sctp_asconf_success_response(aph->correlation_id);
478163953Srrs		}
479163953Srrs		return m_reply;
480163953Srrs	}
481163953Srrs	/* delete the address */
482163953Srrs	result = sctp_del_remote_addr(stcb, sa);
483163953Srrs	/*
484163953Srrs	 * note if result == -2, the address doesn't exist in the asoc but
485163953Srrs	 * since it's being deleted anyways, we just ack the delete -- but
486163953Srrs	 * this probably means something has already gone awry
487163953Srrs	 */
488163953Srrs	if (result == -1) {
489163953Srrs		/* only one address in the asoc */
490163953Srrs#ifdef SCTP_DEBUG
491163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
492163953Srrs			printf("process_asconf_delete_ip: tried to delete last IP addr!\n");
493163953Srrs		}
494163953Srrs#endif				/* SCTP_DEBUG */
495163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
496163953Srrs		    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
497163953Srrs		    aparam_length);
498163953Srrs	} else {
499163953Srrs		if (response_required) {
500163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
501163953Srrs		}
502163953Srrs		/* notify upper layer */
503163953Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa);
504163953Srrs	}
505163953Srrs	return m_reply;
506163953Srrs}
507163953Srrs
508163953Srrsstatic struct mbuf *
509163953Srrssctp_process_asconf_set_primary(struct mbuf *m,
510163953Srrs    struct sctp_asconf_paramhdr *aph, struct sctp_tcb *stcb,
511163953Srrs    int response_required)
512163953Srrs{
513163953Srrs	struct mbuf *m_reply = NULL;
514163953Srrs	struct sockaddr_storage sa_source, sa_store;
515163953Srrs	struct sctp_ipv4addr_param *v4addr;
516163953Srrs	uint16_t param_type, param_length, aparam_length;
517163953Srrs	struct sockaddr *sa;
518163953Srrs	struct sockaddr_in *sin;
519163953Srrs	int zero_address = 0;
520163953Srrs
521163953Srrs#ifdef INET6
522163953Srrs	struct sockaddr_in6 *sin6;
523163953Srrs	struct sctp_ipv6addr_param *v6addr;
524163953Srrs
525163953Srrs#endif				/* INET6 */
526163953Srrs
527163953Srrs	aparam_length = ntohs(aph->ph.param_length);
528163953Srrs	v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
529163953Srrs#ifdef INET6
530163953Srrs	v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
531163953Srrs#endif				/* INET6 */
532163953Srrs	param_type = ntohs(v4addr->ph.param_type);
533163953Srrs	param_length = ntohs(v4addr->ph.param_length);
534163953Srrs
535163953Srrs	sa = (struct sockaddr *)&sa_store;
536163953Srrs	switch (param_type) {
537163953Srrs	case SCTP_IPV4_ADDRESS:
538163953Srrs		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
539163953Srrs			/* invalid param size */
540163953Srrs			return NULL;
541163953Srrs		}
542163953Srrs		sin = (struct sockaddr_in *)&sa_store;
543163953Srrs		bzero(sin, sizeof(*sin));
544163953Srrs		sin->sin_family = AF_INET;
545163953Srrs		sin->sin_len = sizeof(struct sockaddr_in);
546163953Srrs		sin->sin_addr.s_addr = v4addr->addr;
547163953Srrs		if (sin->sin_addr.s_addr == INADDR_ANY)
548163953Srrs			zero_address = 1;
549163953Srrs#ifdef SCTP_DEBUG
550163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
551163953Srrs			printf("process_asconf_set_primary: ");
552163953Srrs			sctp_print_address(sa);
553163953Srrs		}
554163953Srrs#endif				/* SCTP_DEBUG */
555163953Srrs		break;
556163953Srrs	case SCTP_IPV6_ADDRESS:
557163953Srrs		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
558163953Srrs			/* invalid param size */
559163953Srrs			return NULL;
560163953Srrs		}
561163953Srrs#ifdef INET6
562163953Srrs		sin6 = (struct sockaddr_in6 *)&sa_store;
563163953Srrs		bzero(sin6, sizeof(*sin6));
564163953Srrs		sin6->sin6_family = AF_INET6;
565163953Srrs		sin6->sin6_len = sizeof(struct sockaddr_in6);
566163953Srrs		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
567163953Srrs		    sizeof(struct in6_addr));
568163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
569163953Srrs			zero_address = 1;
570163953Srrs#ifdef SCTP_DEBUG
571163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
572163953Srrs			printf("process_asconf_set_primary: ");
573163953Srrs			sctp_print_address(sa);
574163953Srrs		}
575163953Srrs#endif				/* SCTP_DEBUG */
576163953Srrs#else
577163953Srrs		/* IPv6 not enabled!  No "action" needed; just ack it */
578163953Srrs#ifdef SCTP_DEBUG
579163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
580163953Srrs			printf("process_asconf_set_primary: v6 disabled- ignoring: ");
581163953Srrs			sctp_print_address(sa);
582163953Srrs		}
583163953Srrs#endif				/* SCTP_DEBUG */
584163953Srrs		/* just respond with a "success" ASCONF-ACK */
585163953Srrs		return NULL;
586163953Srrs#endif				/* INET6 */
587163953Srrs		break;
588163953Srrs	default:
589163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
590163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
591163953Srrs		    aparam_length);
592163953Srrs		return m_reply;
593163953Srrs	}
594163953Srrs
595163953Srrs	/* if 0.0.0.0/::0, use the source address instead */
596165647Srrs	if (zero_address && sctp_nat_friendly) {
597163953Srrs		sa = (struct sockaddr *)&sa_source;
598163953Srrs		sctp_asconf_get_source_ip(m, sa);
599163953Srrs#ifdef SCTP_DEBUG
600163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
601163953Srrs			printf("process_asconf_set_primary: using source addr ");
602163953Srrs			sctp_print_address(sa);
603163953Srrs		}
604163953Srrs#endif				/* SCTP_DEBUG */
605163953Srrs	}
606163953Srrs	/* set the primary address */
607163953Srrs	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
608163953Srrs#ifdef SCTP_DEBUG
609163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
610163953Srrs			printf("process_asconf_set_primary: primary address set\n");
611163953Srrs		}
612163953Srrs#endif				/* SCTP_DEBUG */
613163953Srrs		/* notify upper layer */
614163953Srrs		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa);
615163953Srrs
616163953Srrs		if (response_required) {
617163953Srrs			m_reply = sctp_asconf_success_response(aph->correlation_id);
618163953Srrs		}
619163953Srrs	} else {
620163953Srrs		/* couldn't set the requested primary address! */
621163953Srrs#ifdef SCTP_DEBUG
622163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
623163953Srrs			printf("process_asconf_set_primary: set primary failed!\n");
624163953Srrs		}
625163953Srrs#endif				/* SCTP_DEBUG */
626163953Srrs		/* must have been an invalid address, so report */
627163953Srrs		m_reply = sctp_asconf_error_response(aph->correlation_id,
628163953Srrs		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
629163953Srrs		    aparam_length);
630163953Srrs	}
631163953Srrs
632163953Srrs	return m_reply;
633163953Srrs}
634163953Srrs
635163953Srrs/*
636163953Srrs * handles an ASCONF chunk.
637163953Srrs * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
638163953Srrs */
639163953Srrsvoid
640163953Srrssctp_handle_asconf(struct mbuf *m, unsigned int offset,
641163953Srrs    struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb)
642163953Srrs{
643163953Srrs	struct sctp_association *asoc;
644163953Srrs	uint32_t serial_num;
645163953Srrs	struct mbuf *m_ack, *m_result, *m_tail;
646163953Srrs	struct sctp_asconf_ack_chunk *ack_cp;
647163953Srrs	struct sctp_asconf_paramhdr *aph, *ack_aph;
648163953Srrs	struct sctp_ipv6addr_param *p_addr;
649163953Srrs	unsigned int asconf_limit;
650163953Srrs	int error = 0;		/* did an error occur? */
651163953Srrs
652163953Srrs	/* asconf param buffer */
653166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
654163953Srrs
655163953Srrs	/* verify minimum length */
656163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
657163953Srrs#ifdef SCTP_DEBUG
658163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
659163953Srrs			printf("handle_asconf: chunk too small = %xh\n",
660163953Srrs			    ntohs(cp->ch.chunk_length));
661163953Srrs		}
662163953Srrs#endif				/* SCTP_DEBUG */
663163953Srrs		return;
664163953Srrs	}
665163953Srrs	asoc = &stcb->asoc;
666163953Srrs	serial_num = ntohl(cp->serial_number);
667163953Srrs
668163953Srrs	if (serial_num == asoc->asconf_seq_in) {
669163953Srrs		/* got a duplicate ASCONF */
670163953Srrs#ifdef SCTP_DEBUG
671163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
672163953Srrs			printf("handle_asconf: got duplicate serial number = %xh\n",
673163953Srrs			    serial_num);
674163953Srrs		}
675163953Srrs#endif				/* SCTP_DEBUG */
676163953Srrs		/* resend last ASCONF-ACK... */
677163953Srrs		sctp_send_asconf_ack(stcb, 1);
678163953Srrs		return;
679163953Srrs	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
680163953Srrs#ifdef SCTP_DEBUG
681163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
682163953Srrs			printf("handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
683163953Srrs			    serial_num, asoc->asconf_seq_in + 1);
684163953Srrs		}
685163953Srrs#endif				/* SCTP_DEBUG */
686163953Srrs		return;
687163953Srrs	}
688163953Srrs	/* it's the expected "next" sequence number, so process it */
689163953Srrs	asoc->asconf_seq_in = serial_num;	/* update sequence */
690163953Srrs	/* get length of all the param's in the ASCONF */
691163953Srrs	asconf_limit = offset + ntohs(cp->ch.chunk_length);
692163953Srrs#ifdef SCTP_DEBUG
693163953Srrs	if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
694163953Srrs		printf("handle_asconf: asconf_limit=%u, sequence=%xh\n",
695163953Srrs		    asconf_limit, serial_num);
696163953Srrs	}
697163953Srrs#endif				/* SCTP_DEBUG */
698163953Srrs	if (asoc->last_asconf_ack_sent != NULL) {
699163953Srrs		/* free last ASCONF-ACK message sent */
700163953Srrs		sctp_m_freem(asoc->last_asconf_ack_sent);
701163953Srrs		asoc->last_asconf_ack_sent = NULL;
702163953Srrs	}
703165647Srrs	m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
704163953Srrs	    M_DONTWAIT, 1, MT_DATA);
705163953Srrs	if (m_ack == NULL) {
706163953Srrs#ifdef SCTP_DEBUG
707163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
708163953Srrs			printf("handle_asconf: couldn't get mbuf!\n");
709163953Srrs		}
710163953Srrs#endif				/* SCTP_DEBUG */
711163953Srrs		return;
712163953Srrs	}
713163953Srrs	m_tail = m_ack;		/* current reply chain's tail */
714163953Srrs
715163953Srrs	/* fill in ASCONF-ACK header */
716163953Srrs	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
717163953Srrs	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
718163953Srrs	ack_cp->ch.chunk_flags = 0;
719163953Srrs	ack_cp->serial_number = htonl(serial_num);
720163953Srrs	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
721165647Srrs	SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
722163953Srrs	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
723163953Srrs
724163953Srrs	/* skip the lookup address parameter */
725163953Srrs	offset += sizeof(struct sctp_asconf_chunk);
726163953Srrs	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *) & aparam_buf);
727163953Srrs	if (p_addr == NULL) {
728163953Srrs#ifdef SCTP_DEBUG
729163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
730163953Srrs			printf("handle_asconf: couldn't get lookup addr!\n");
731163953Srrs		}
732163953Srrs#endif				/* SCTP_DEBUG */
733163953Srrs
734163953Srrs		/* respond with a missing/invalid mandatory parameter error */
735163953Srrs		return;
736163953Srrs	}
737163953Srrs	/* param_length is already validated in process_control... */
738163953Srrs	offset += ntohs(p_addr->ph.param_length);	/* skip lookup addr */
739163953Srrs
740163953Srrs	/* get pointer to first asconf param in ASCONF-ACK */
741163953Srrs	ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, caddr_t)+sizeof(struct sctp_asconf_ack_chunk));
742163953Srrs	if (ack_aph == NULL) {
743163953Srrs#ifdef SCTP_DEBUG
744163953Srrs		printf("Gak in asconf2\n");
745163953Srrs#endif
746163953Srrs		return;
747163953Srrs	}
748163953Srrs	/* get pointer to first asconf param in ASCONF */
749163953Srrs	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *) & aparam_buf);
750163953Srrs	if (aph == NULL) {
751163953Srrs#ifdef SCTP_DEBUG
752163953Srrs		printf("Empty ASCONF received?\n");
753163953Srrs#endif
754163953Srrs		goto send_reply;
755163953Srrs	}
756163953Srrs	/* process through all parameters */
757163953Srrs	while (aph != NULL) {
758163953Srrs		unsigned int param_length, param_type;
759163953Srrs
760163953Srrs		param_type = ntohs(aph->ph.param_type);
761163953Srrs		param_length = ntohs(aph->ph.param_length);
762163953Srrs		if (offset + param_length > asconf_limit) {
763163953Srrs			/* parameter goes beyond end of chunk! */
764163953Srrs			sctp_m_freem(m_ack);
765163953Srrs			return;
766163953Srrs		}
767163953Srrs		m_result = NULL;
768163953Srrs
769163953Srrs		if (param_length > sizeof(aparam_buf)) {
770163953Srrs#ifdef SCTP_DEBUG
771163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
772163953Srrs				printf("handle_asconf: param length (%u) larger than buffer size!\n", param_length);
773163953Srrs			}
774163953Srrs#endif				/* SCTP_DEBUG */
775163953Srrs			sctp_m_freem(m_ack);
776163953Srrs			return;
777163953Srrs		}
778163953Srrs		if (param_length <= sizeof(struct sctp_paramhdr)) {
779163953Srrs#ifdef SCTP_DEBUG
780163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
781163953Srrs				printf("handle_asconf: param length (%u) too short\n", param_length);
782163953Srrs			}
783163953Srrs#endif				/* SCTP_DEBUG */
784163953Srrs			sctp_m_freem(m_ack);
785163953Srrs		}
786163953Srrs		/* get the entire parameter */
787163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
788163953Srrs		if (aph == NULL) {
789163953Srrs#ifdef SCTP_DEBUG
790163953Srrs			printf("Gag\n");
791163953Srrs#endif
792163953Srrs			sctp_m_freem(m_ack);
793163953Srrs			return;
794163953Srrs		}
795163953Srrs		switch (param_type) {
796163953Srrs		case SCTP_ADD_IP_ADDRESS:
797163953Srrs			asoc->peer_supports_asconf = 1;
798163953Srrs			m_result = sctp_process_asconf_add_ip(m, aph, stcb,
799163953Srrs			    error);
800163953Srrs			break;
801163953Srrs		case SCTP_DEL_IP_ADDRESS:
802163953Srrs			asoc->peer_supports_asconf = 1;
803163953Srrs			m_result = sctp_process_asconf_delete_ip(m, aph, stcb,
804163953Srrs			    error);
805163953Srrs			break;
806163953Srrs		case SCTP_ERROR_CAUSE_IND:
807163953Srrs			/* not valid in an ASCONF chunk */
808163953Srrs			break;
809163953Srrs		case SCTP_SET_PRIM_ADDR:
810163953Srrs			asoc->peer_supports_asconf = 1;
811163953Srrs			m_result = sctp_process_asconf_set_primary(m, aph,
812163953Srrs			    stcb, error);
813163953Srrs			break;
814163953Srrs		case SCTP_SUCCESS_REPORT:
815163953Srrs			/* not valid in an ASCONF chunk */
816163953Srrs			break;
817163953Srrs		case SCTP_ULP_ADAPTATION:
818163953Srrs			/* FIX */
819163953Srrs			break;
820163953Srrs		default:
821163953Srrs			if ((param_type & 0x8000) == 0) {
822163953Srrs				/* Been told to STOP at this param */
823163953Srrs				asconf_limit = offset;
824163953Srrs				/*
825163953Srrs				 * FIX FIX - We need to call
826163953Srrs				 * sctp_arethere_unrecognized_parameters()
827163953Srrs				 * to get a operr and send it for any
828163953Srrs				 * param's with the 0x4000 bit set OR do it
829163953Srrs				 * here ourselves... note we still must STOP
830163953Srrs				 * if the 0x8000 bit is clear.
831163953Srrs				 */
832163953Srrs			}
833163953Srrs			/* unknown/invalid param type */
834163953Srrs			break;
835163953Srrs		}		/* switch */
836163953Srrs
837163953Srrs		/* add any (error) result to the reply mbuf chain */
838163953Srrs		if (m_result != NULL) {
839165647Srrs			SCTP_BUF_NEXT(m_tail) = m_result;
840163953Srrs			m_tail = m_result;
841163953Srrs			/* update lengths, make sure it's aligned too */
842165647Srrs			SCTP_BUF_LEN(m_result) = SCTP_SIZE32(SCTP_BUF_LEN(m_result));
843165647Srrs			ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
844163953Srrs			/* set flag to force success reports */
845163953Srrs			error = 1;
846163953Srrs		}
847163953Srrs		offset += SCTP_SIZE32(param_length);
848163953Srrs		/* update remaining ASCONF message length to process */
849163953Srrs		if (offset >= asconf_limit) {
850163953Srrs			/* no more data in the mbuf chain */
851163953Srrs			break;
852163953Srrs		}
853163953Srrs		/* get pointer to next asconf param */
854163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
855163953Srrs		    sizeof(struct sctp_asconf_paramhdr),
856163953Srrs		    (uint8_t *) & aparam_buf);
857163953Srrs		if (aph == NULL) {
858163953Srrs			/* can't get an asconf paramhdr */
859163953Srrs#ifdef SCTP_DEBUG
860163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
861163953Srrs				printf("handle_asconf: can't get asconf param hdr!\n");
862163953Srrs			}
863163953Srrs#endif				/* SCTP_DEBUG */
864163953Srrs			/* FIX ME - add error here... */
865163953Srrs		}
866163953Srrs	}			/* while */
867163953Srrs
868163953Srrssend_reply:
869163953Srrs	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
870163953Srrs	/* save the ASCONF-ACK reply */
871163953Srrs	asoc->last_asconf_ack_sent = m_ack;
872163953Srrs
873163953Srrs	/* see if last_control_chunk_from is set properly (use IP src addr) */
874163953Srrs	if (stcb->asoc.last_control_chunk_from == NULL) {
875163953Srrs		/*
876163953Srrs		 * this could happen if the source address was just newly
877163953Srrs		 * added
878163953Srrs		 */
879163953Srrs		struct ip *iph;
880163953Srrs		struct sctphdr *sh;
881163953Srrs		struct sockaddr_storage from_store;
882163953Srrs		struct sockaddr *from = (struct sockaddr *)&from_store;
883163953Srrs
884163953Srrs#ifdef SCTP_DEBUG
885163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
886163953Srrs			printf("handle_asconf: looking up net for IP source address\n");
887163953Srrs#endif				/* SCTP_DEBUG */
888163953Srrs		/* pullup already done, IP options already stripped */
889163953Srrs		iph = mtod(m, struct ip *);
890163953Srrs		sh = (struct sctphdr *)((caddr_t)iph + sizeof(*iph));
891163953Srrs		if (iph->ip_v == IPVERSION) {
892163953Srrs			struct sockaddr_in *from4;
893163953Srrs
894163953Srrs			from4 = (struct sockaddr_in *)&from_store;
895163953Srrs			bzero(from4, sizeof(*from4));
896163953Srrs			from4->sin_family = AF_INET;
897163953Srrs			from4->sin_len = sizeof(struct sockaddr_in);
898163953Srrs			from4->sin_addr.s_addr = iph->ip_src.s_addr;
899163953Srrs			from4->sin_port = sh->src_port;
900163953Srrs		} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
901163953Srrs			struct ip6_hdr *ip6;
902163953Srrs			struct sockaddr_in6 *from6;
903163953Srrs
904163953Srrs			ip6 = mtod(m, struct ip6_hdr *);
905163953Srrs			from6 = (struct sockaddr_in6 *)&from_store;
906163953Srrs			bzero(from6, sizeof(*from6));
907163953Srrs			from6->sin6_family = AF_INET6;
908163953Srrs			from6->sin6_len = sizeof(struct sockaddr_in6);
909163953Srrs			from6->sin6_addr = ip6->ip6_src;
910163953Srrs			from6->sin6_port = sh->src_port;
911163953Srrs			/* Get the scopes in properly to the sin6 addr's */
912163953Srrs			/* we probably don't need these operations */
913163953Srrs			(void)sa6_recoverscope(from6);
914163953Srrs			sa6_embedscope(from6, ip6_use_defzone);
915163953Srrs		} else {
916163953Srrs			/* unknown address type */
917163953Srrs			from = NULL;
918163953Srrs		}
919163953Srrs		if (from != NULL) {
920163953Srrs#ifdef SCTP_DEBUG
921163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
922163953Srrs				printf("Looking for IP source: ");
923163953Srrs				sctp_print_address(from);
924163953Srrs			}
925163953Srrs#endif				/* SCTP_DEBUG */
926163953Srrs			/* look up the from address */
927163953Srrs			stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, from);
928163953Srrs#ifdef SCTP_DEBUG
929163953Srrs			if ((stcb->asoc.last_control_chunk_from == NULL) &&
930163953Srrs			    (sctp_debug_on & SCTP_DEBUG_ASCONF1))
931163953Srrs				printf("handle_asconf: IP source address not found?!\n");
932163953Srrs#endif				/* SCTP_DEBUG */
933163953Srrs		}
934163953Srrs	}
935163953Srrs	/* and send it (a new one) out... */
936163953Srrs	sctp_send_asconf_ack(stcb, 0);
937163953Srrs}
938163953Srrs
939163953Srrs/*
940163953Srrs * does the address match? returns 0 if not, 1 if so
941163953Srrs */
942163953Srrsstatic uint32_t
943163953Srrssctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
944163953Srrs{
945163953Srrs#ifdef INET6
946163953Srrs	if (sa->sa_family == AF_INET6) {
947163953Srrs		/* IPv6 sa address */
948163953Srrs		/* XXX scopeid */
949163953Srrs		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
950163953Srrs
951163953Srrs		if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
952163953Srrs		    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
953163953Srrs		    sizeof(struct in6_addr)) == 0)) {
954163953Srrs			return (1);
955163953Srrs		}
956163953Srrs	} else
957163953Srrs#endif				/* INET6 */
958163953Srrs	if (sa->sa_family == AF_INET) {
959163953Srrs		/* IPv4 sa address */
960163953Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
961163953Srrs
962163953Srrs		if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
963163953Srrs		    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
964163953Srrs		    sizeof(struct in_addr)) == 0)) {
965163953Srrs			return (1);
966163953Srrs		}
967163953Srrs	}
968163953Srrs	return (0);
969163953Srrs}
970163953Srrs
971163953Srrs/*
972163953Srrs * Cleanup for non-responded/OP ERR'd ASCONF
973163953Srrs */
974163953Srrsvoid
975163953Srrssctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
976163953Srrs{
977163953Srrs	/* mark peer as ASCONF incapable */
978163953Srrs	stcb->asoc.peer_supports_asconf = 0;
979163953Srrs	/*
980163953Srrs	 * clear out any existing asconfs going out
981163953Srrs	 */
982165220Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
983163953Srrs	stcb->asoc.asconf_seq_out++;
984163953Srrs	/* remove the old ASCONF on our outbound queue */
985163953Srrs	sctp_toss_old_asconf(stcb);
986163953Srrs}
987163953Srrs
988163953Srrs/*
989163953Srrs * process an ADD/DELETE IP ack from peer.
990167598Srrs * addr  corresponding sctp_ifa to the address being added/deleted.
991163953Srrs * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
992163953Srrs * flag: 1=success, 0=failure.
993163953Srrs */
994163953Srrsstatic void
995167598Srrssctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr,
996163953Srrs    uint16_t type, uint32_t flag)
997163953Srrs{
998163953Srrs	/*
999163953Srrs	 * do the necessary asoc list work- if we get a failure indication,
1000163953Srrs	 * leave the address on the "do not use" asoc list if we get a
1001163953Srrs	 * success indication, remove the address from the list
1002163953Srrs	 */
1003163953Srrs	/*
1004163953Srrs	 * Note: this will only occur for ADD_IP_ADDRESS, since
1005163953Srrs	 * DEL_IP_ADDRESS is never actually added to the list...
1006163953Srrs	 */
1007163953Srrs	if (flag) {
1008163953Srrs		/* success case, so remove from the list */
1009163953Srrs		sctp_del_local_addr_assoc(stcb, addr);
1010163953Srrs	}
1011163953Srrs	/* else, leave it on the list */
1012163953Srrs}
1013163953Srrs
1014163953Srrs/*
1015163953Srrs * add an asconf add/delete IP address parameter to the queue.
1016163953Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1017163953Srrs * returns 0 if completed, non-zero if not completed.
1018163953Srrs * NOTE: if adding, but delete already scheduled (and not yet sent out),
1019163953Srrs * simply remove from queue.  Same for deleting an address already scheduled
1020163953Srrs * for add.  If a duplicate operation is found, ignore the new one.
1021163953Srrs */
1022163953Srrsstatic uint32_t
1023167598Srrssctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa, uint16_t type)
1024163953Srrs{
1025163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1026163953Srrs	struct sockaddr *sa;
1027163953Srrs
1028163953Srrs	/* see if peer supports ASCONF */
1029163953Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
1030163953Srrs		return (-1);
1031163953Srrs	}
1032163953Srrs	/* make sure the request isn't already in the queue */
1033163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1034163953Srrs	    aa = aa_next) {
1035163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1036163953Srrs		/* address match? */
1037167598Srrs		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
1038163953Srrs			continue;
1039163953Srrs		/* is the request already in queue (sent or not) */
1040163953Srrs		if (aa->ap.aph.ph.param_type == type) {
1041163953Srrs			return (-1);
1042163953Srrs		}
1043163953Srrs		/* is the negative request already in queue, and not sent */
1044163953Srrs		if (aa->sent == 0 &&
1045163953Srrs		/* add requested, delete already queued */
1046163953Srrs		    ((type == SCTP_ADD_IP_ADDRESS &&
1047163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) ||
1048163953Srrs		/* delete requested, add already queued */
1049163953Srrs		    (type == SCTP_DEL_IP_ADDRESS &&
1050163953Srrs		    aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS))) {
1051163953Srrs			/* delete the existing entry in the queue */
1052163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1053163953Srrs			/* take the entry off the appropriate list */
1054163953Srrs			sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
1055163953Srrs			/* free the entry */
1056168299Srrs			sctp_free_ifa(aa->ifa);
1057163953Srrs			SCTP_FREE(aa);
1058163953Srrs			return (-1);
1059163953Srrs		}
1060163953Srrs	}			/* for each aa */
1061163953Srrs
1062163953Srrs	/* adding new request to the queue */
1063163953Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa), "AsconfAddr");
1064163953Srrs	if (aa == NULL) {
1065163953Srrs		/* didn't get memory */
1066163953Srrs#ifdef SCTP_DEBUG
1067163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1068163953Srrs			printf("asconf_queue_add: failed to get memory!\n");
1069163953Srrs		}
1070163953Srrs#endif				/* SCTP_DEBUG */
1071163953Srrs		return (-1);
1072163953Srrs	}
1073163953Srrs	/* fill in asconf address parameter fields */
1074163953Srrs	/* top level elements are "networked" during send */
1075163953Srrs	aa->ap.aph.ph.param_type = type;
1076163953Srrs	aa->ifa = ifa;
1077168299Srrs	atomic_add_int(&ifa->refcount, 1);
1078163953Srrs	/* correlation_id filled in during send routine later... */
1079167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1080163953Srrs		/* IPv6 address */
1081163953Srrs		struct sockaddr_in6 *sin6;
1082163953Srrs
1083167598Srrs		sin6 = (struct sockaddr_in6 *)&ifa->address.sa;
1084163953Srrs		sa = (struct sockaddr *)sin6;
1085163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1086163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1087163953Srrs		aa->ap.aph.ph.param_length =
1088163953Srrs		    sizeof(struct sctp_asconf_paramhdr) +
1089163953Srrs		    sizeof(struct sctp_ipv6addr_param);
1090163953Srrs		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1091163953Srrs		    sizeof(struct in6_addr));
1092167598Srrs	} else if (ifa->address.sa.sa_family == AF_INET) {
1093163953Srrs		/* IPv4 address */
1094167598Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)&ifa->address.sa;
1095163953Srrs
1096163953Srrs		sa = (struct sockaddr *)sin;
1097163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1098163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1099163953Srrs		aa->ap.aph.ph.param_length =
1100163953Srrs		    sizeof(struct sctp_asconf_paramhdr) +
1101163953Srrs		    sizeof(struct sctp_ipv4addr_param);
1102163953Srrs		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1103163953Srrs		    sizeof(struct in_addr));
1104163953Srrs	} else {
1105163953Srrs		/* invalid family! */
1106163953Srrs		return (-1);
1107163953Srrs	}
1108163953Srrs	aa->sent = 0;		/* clear sent flag */
1109163953Srrs
1110163953Srrs	/*
1111163953Srrs	 * if we are deleting an address it should go out last otherwise,
1112163953Srrs	 * add it to front of the pending queue
1113163953Srrs	 */
1114163953Srrs	if (type == SCTP_ADD_IP_ADDRESS) {
1115163953Srrs		/* add goes to the front of the queue */
1116163953Srrs		TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
1117163953Srrs#ifdef SCTP_DEBUG
1118163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
1119163953Srrs			printf("asconf_queue_add: appended asconf ADD_IP_ADDRESS: ");
1120163953Srrs			sctp_print_address(sa);
1121163953Srrs		}
1122163953Srrs#endif				/* SCTP_DEBUG */
1123163953Srrs	} else {
1124163953Srrs		/* delete and set primary goes to the back of the queue */
1125163953Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1126163953Srrs#ifdef SCTP_DEBUG
1127163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
1128163953Srrs			if (type == SCTP_DEL_IP_ADDRESS) {
1129163953Srrs				printf("asconf_queue_add: inserted asconf DEL_IP_ADDRESS: ");
1130163953Srrs				sctp_print_address(sa);
1131163953Srrs			} else {
1132163953Srrs				printf("asconf_queue_add: inserted asconf SET_PRIM_ADDR: ");
1133163953Srrs				sctp_print_address(sa);
1134163953Srrs			}
1135163953Srrs		}
1136163953Srrs#endif				/* SCTP_DEBUG */
1137163953Srrs	}
1138163953Srrs
1139163953Srrs	return (0);
1140163953Srrs}
1141163953Srrs
1142163953Srrs/*
1143163953Srrs * add an asconf add/delete IP address parameter to the queue by addr.
1144163953Srrs * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1145163953Srrs * returns 0 if completed, non-zero if not completed.
1146163953Srrs * NOTE: if adding, but delete already scheduled (and not yet sent out),
1147163953Srrs * simply remove from queue.  Same for deleting an address already scheduled
1148163953Srrs * for add.  If a duplicate operation is found, ignore the new one.
1149163953Srrs */
1150163953Srrsstatic uint32_t
1151163953Srrssctp_asconf_queue_add_sa(struct sctp_tcb *stcb, struct sockaddr *sa,
1152163953Srrs    uint16_t type)
1153163953Srrs{
1154168299Srrs	struct sctp_ifa *ifa;
1155163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1156167598Srrs	uint32_t vrf_id;
1157163953Srrs
1158163953Srrs	/* see if peer supports ASCONF */
1159163953Srrs	if (stcb->asoc.peer_supports_asconf == 0) {
1160163953Srrs		return (-1);
1161163953Srrs	}
1162163953Srrs	/* make sure the request isn't already in the queue */
1163163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1164163953Srrs	    aa = aa_next) {
1165163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1166163953Srrs		/* address match? */
1167163953Srrs		if (sctp_asconf_addr_match(aa, sa) == 0)
1168163953Srrs			continue;
1169163953Srrs		/* is the request already in queue (sent or not) */
1170163953Srrs		if (aa->ap.aph.ph.param_type == type) {
1171163953Srrs			return (-1);
1172163953Srrs		}
1173163953Srrs		/* is the negative request already in queue, and not sent */
1174163953Srrs		if (aa->sent == 1)
1175163953Srrs			continue;
1176163953Srrs		if (type == SCTP_ADD_IP_ADDRESS &&
1177163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1178163953Srrs			/* add requested, delete already queued */
1179163953Srrs
1180163953Srrs			/* delete the existing entry in the queue */
1181163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1182163953Srrs			/* free the entry */
1183168299Srrs			sctp_free_ifa(aa->ifa);
1184163953Srrs			SCTP_FREE(aa);
1185163953Srrs			return (-1);
1186163953Srrs		} else if (type == SCTP_DEL_IP_ADDRESS &&
1187163953Srrs		    aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1188163953Srrs			/* delete requested, add already queued */
1189163953Srrs
1190163953Srrs			/* delete the existing entry in the queue */
1191163953Srrs			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1192163953Srrs			/* take the entry off the appropriate list */
1193163953Srrs			sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
1194163953Srrs			/* free the entry */
1195168299Srrs			sctp_free_ifa(aa->ifa);
1196163953Srrs			SCTP_FREE(aa);
1197163953Srrs			return (-1);
1198163953Srrs		}
1199163953Srrs	}			/* for each aa */
1200168299Srrs	if (stcb) {
1201168299Srrs		vrf_id = stcb->asoc.vrf_id;
1202168299Srrs	} else {
1203168299Srrs		vrf_id = SCTP_DEFAULT_VRFID;
1204168299Srrs	}
1205163953Srrs
1206168299Srrs	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
1207168299Srrs	if (ifa == NULL) {
1208168299Srrs		/* Invalid address */
1209168299Srrs		return (-1);
1210168299Srrs	}
1211163953Srrs	/* adding new request to the queue */
1212163953Srrs	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa), "AsconfAddr");
1213163953Srrs	if (aa == NULL) {
1214163953Srrs		/* didn't get memory */
1215163953Srrs#ifdef SCTP_DEBUG
1216163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1217163953Srrs			printf("asconf_queue_add_sa: failed to get memory!\n");
1218163953Srrs		}
1219163953Srrs#endif				/* SCTP_DEBUG */
1220163953Srrs		return (-1);
1221163953Srrs	}
1222163953Srrs	/* fill in asconf address parameter fields */
1223163953Srrs	/* top level elements are "networked" during send */
1224163953Srrs	aa->ap.aph.ph.param_type = type;
1225168299Srrs	aa->ifa = ifa;
1226168299Srrs	atomic_add_int(&ifa->refcount, 1);
1227163953Srrs	/* correlation_id filled in during send routine later... */
1228163953Srrs	if (sa->sa_family == AF_INET6) {
1229163953Srrs		/* IPv6 address */
1230163953Srrs		struct sockaddr_in6 *sin6;
1231163953Srrs
1232163953Srrs		sin6 = (struct sockaddr_in6 *)sa;
1233163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1234163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1235163953Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1236163953Srrs		memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1237163953Srrs		    sizeof(struct in6_addr));
1238163953Srrs	} else if (sa->sa_family == AF_INET) {
1239163953Srrs		/* IPv4 address */
1240163953Srrs		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1241163953Srrs
1242163953Srrs		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1243163953Srrs		aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1244163953Srrs		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1245163953Srrs		memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1246163953Srrs		    sizeof(struct in_addr));
1247163953Srrs	} else {
1248163953Srrs		/* invalid family! */
1249164085Srrs		SCTP_FREE(aa);
1250163953Srrs		return (-1);
1251163953Srrs	}
1252163953Srrs	aa->sent = 0;		/* clear sent flag */
1253163953Srrs
1254163953Srrs	/*
1255163953Srrs	 * if we are deleting an address it should go out last otherwise,
1256163953Srrs	 * add it to front of the pending queue
1257163953Srrs	 */
1258163953Srrs	if (type == SCTP_ADD_IP_ADDRESS) {
1259163953Srrs		/* add goes to the front of the queue */
1260163953Srrs		TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
1261163953Srrs	} else {
1262163953Srrs		/* delete and set primary goes to the back of the queue */
1263163953Srrs		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1264163953Srrs	}
1265163953Srrs
1266163953Srrs	return (0);
1267163953Srrs}
1268163953Srrs
1269163953Srrs/*
1270163953Srrs * find a specific asconf param on our "sent" queue
1271163953Srrs */
1272163953Srrsstatic struct sctp_asconf_addr *
1273163953Srrssctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1274163953Srrs{
1275163953Srrs	struct sctp_asconf_addr *aa;
1276163953Srrs
1277163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1278163953Srrs		if (aa->ap.aph.correlation_id == correlation_id &&
1279163953Srrs		    aa->sent == 1) {
1280163953Srrs			/* found it */
1281163953Srrs			return (aa);
1282163953Srrs		}
1283163953Srrs	}
1284163953Srrs	/* didn't find it */
1285163953Srrs	return (NULL);
1286163953Srrs}
1287163953Srrs
1288163953Srrs/*
1289163953Srrs * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1290163953Srrs * notifications based on the error response
1291163953Srrs */
1292163953Srrsstatic void
1293163953Srrssctp_asconf_process_error(struct sctp_tcb *stcb,
1294163953Srrs    struct sctp_asconf_paramhdr *aph)
1295163953Srrs{
1296163953Srrs	struct sctp_error_cause *eh;
1297163953Srrs	struct sctp_paramhdr *ph;
1298163953Srrs	uint16_t param_type;
1299163953Srrs	uint16_t error_code;
1300163953Srrs
1301163953Srrs	eh = (struct sctp_error_cause *)(aph + 1);
1302163953Srrs	ph = (struct sctp_paramhdr *)(eh + 1);
1303163953Srrs	/* validate lengths */
1304163953Srrs	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1305163953Srrs	    htons(aph->ph.param_length)) {
1306163953Srrs		/* invalid error cause length */
1307163953Srrs#ifdef SCTP_DEBUG
1308163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1309163953Srrs			printf("asconf_process_error: cause element too long\n");
1310163953Srrs		}
1311163953Srrs#endif				/* SCTP_DEBUG */
1312163953Srrs		return;
1313163953Srrs	}
1314163953Srrs	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1315163953Srrs	    htons(eh->length)) {
1316163953Srrs		/* invalid included TLV length */
1317163953Srrs#ifdef SCTP_DEBUG
1318163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1319163953Srrs			printf("asconf_process_error: included TLV too long\n");
1320163953Srrs		}
1321163953Srrs#endif				/* SCTP_DEBUG */
1322163953Srrs		return;
1323163953Srrs	}
1324163953Srrs	/* which error code ? */
1325163953Srrs	error_code = ntohs(eh->code);
1326163953Srrs	param_type = ntohs(aph->ph.param_type);
1327163953Srrs	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1328163953Srrs	switch (error_code) {
1329163953Srrs	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1330163953Srrs		/* we allow ourselves to "try again" for this error */
1331163953Srrs		break;
1332163953Srrs	default:
1333163953Srrs		/* peer can't handle it... */
1334163953Srrs		switch (param_type) {
1335163953Srrs		case SCTP_ADD_IP_ADDRESS:
1336163953Srrs		case SCTP_DEL_IP_ADDRESS:
1337163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1338163953Srrs			break;
1339163953Srrs		case SCTP_SET_PRIM_ADDR:
1340163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1341163953Srrs			break;
1342163953Srrs		default:
1343163953Srrs			break;
1344163953Srrs		}
1345163953Srrs	}
1346163953Srrs}
1347163953Srrs
1348163953Srrs/*
1349163953Srrs * process an asconf queue param aparam: parameter to process, will be
1350163953Srrs * removed from the queue flag: 1=success, 0=failure
1351163953Srrs */
1352163953Srrsstatic void
1353163953Srrssctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1354163953Srrs    struct sctp_asconf_addr *aparam, uint32_t flag)
1355163953Srrs{
1356163953Srrs	uint16_t param_type;
1357163953Srrs
1358163953Srrs	/* process this param */
1359163953Srrs	param_type = aparam->ap.aph.ph.param_type;
1360163953Srrs	switch (param_type) {
1361163953Srrs	case SCTP_ADD_IP_ADDRESS:
1362163953Srrs#ifdef SCTP_DEBUG
1363163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1364163953Srrs			printf("process_param_ack: added IP address\n");
1365163953Srrs		}
1366163953Srrs#endif				/* SCTP_DEBUG */
1367163953Srrs		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
1368163953Srrs		break;
1369163953Srrs	case SCTP_DEL_IP_ADDRESS:
1370163953Srrs#ifdef SCTP_DEBUG
1371163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1372163953Srrs			printf("process_param_ack: deleted IP address\n");
1373163953Srrs		}
1374163953Srrs#endif				/* SCTP_DEBUG */
1375163953Srrs		/* nothing really to do... lists already updated */
1376163953Srrs		break;
1377163953Srrs	case SCTP_SET_PRIM_ADDR:
1378163953Srrs		/* nothing to do... peer may start using this addr */
1379163953Srrs		if (flag == 0)
1380163953Srrs			stcb->asoc.peer_supports_asconf = 0;
1381163953Srrs		break;
1382163953Srrs	default:
1383163953Srrs		/* should NEVER happen */
1384163953Srrs		break;
1385163953Srrs	}
1386163953Srrs
1387163953Srrs	/* remove the param and free it */
1388163953Srrs	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1389168299Srrs	sctp_free_ifa(aparam->ifa);
1390163953Srrs	SCTP_FREE(aparam);
1391163953Srrs}
1392163953Srrs
1393163953Srrs/*
1394163953Srrs * cleanup from a bad asconf ack parameter
1395163953Srrs */
1396163953Srrsstatic void
1397163953Srrssctp_asconf_ack_clear(struct sctp_tcb *stcb)
1398163953Srrs{
1399163953Srrs	/* assume peer doesn't really know how to do asconfs */
1400163953Srrs	stcb->asoc.peer_supports_asconf = 0;
1401163953Srrs	/* XXX we could free the pending queue here */
1402163953Srrs}
1403163953Srrs
1404163953Srrsvoid
1405163953Srrssctp_handle_asconf_ack(struct mbuf *m, int offset,
1406163953Srrs    struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1407163953Srrs    struct sctp_nets *net)
1408163953Srrs{
1409163953Srrs	struct sctp_association *asoc;
1410163953Srrs	uint32_t serial_num;
1411163953Srrs	uint16_t ack_length;
1412163953Srrs	struct sctp_asconf_paramhdr *aph;
1413163953Srrs	struct sctp_asconf_addr *aa, *aa_next;
1414163953Srrs	uint32_t last_error_id = 0;	/* last error correlation id */
1415163953Srrs	uint32_t id;
1416163953Srrs	struct sctp_asconf_addr *ap;
1417163953Srrs
1418163953Srrs	/* asconf param buffer */
1419166675Srrs	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1420163953Srrs
1421163953Srrs	/* verify minimum length */
1422163953Srrs	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1423163953Srrs#ifdef SCTP_DEBUG
1424163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1425163953Srrs			printf("handle_asconf_ack: chunk too small = %xh\n",
1426163953Srrs			    ntohs(cp->ch.chunk_length));
1427163953Srrs		}
1428163953Srrs#endif				/* SCTP_DEBUG */
1429163953Srrs		return;
1430163953Srrs	}
1431163953Srrs	asoc = &stcb->asoc;
1432163953Srrs	serial_num = ntohl(cp->serial_number);
1433163953Srrs
1434163953Srrs	/*
1435163953Srrs	 * NOTE: we may want to handle this differently- currently, we will
1436163953Srrs	 * abort when we get an ack for the expected serial number + 1 (eg.
1437163953Srrs	 * we didn't send it), process an ack normally if it is the expected
1438163953Srrs	 * serial number, and re-send the previous ack for *ALL* other
1439163953Srrs	 * serial numbers
1440163953Srrs	 */
1441163953Srrs
1442163953Srrs	/*
1443163953Srrs	 * if the serial number is the next expected, but I didn't send it,
1444163953Srrs	 * abort the asoc, since someone probably just hijacked us...
1445163953Srrs	 */
1446163953Srrs	if (serial_num == (asoc->asconf_seq_out + 1)) {
1447163953Srrs#ifdef SCTP_DEBUG
1448163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1449163953Srrs			printf("handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1450163953Srrs		}
1451163953Srrs#endif				/* SCTP_DEBUG */
1452163953Srrs		sctp_abort_an_association(stcb->sctp_ep, stcb,
1453163953Srrs		    SCTP_CAUSE_ILLEGAL_ASCONF_ACK, NULL);
1454163953Srrs		return;
1455163953Srrs	}
1456163953Srrs	if (serial_num != asoc->asconf_seq_out) {
1457163953Srrs		/* got a duplicate/unexpected ASCONF-ACK */
1458163953Srrs#ifdef SCTP_DEBUG
1459163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1460163953Srrs			printf("handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n", serial_num, asoc->asconf_seq_out);
1461163953Srrs		}
1462163953Srrs#endif				/* SCTP_DEBUG */
1463163953Srrs		return;
1464163953Srrs	}
1465163953Srrs	if (stcb->asoc.asconf_sent == 0) {
1466163953Srrs		/* got a unexpected ASCONF-ACK for serial not in flight */
1467163953Srrs#ifdef SCTP_DEBUG
1468163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1469163953Srrs			printf("handle_asconf_ack: got serial number = %xh but not in flight\n", serial_num);
1470163953Srrs		}
1471163953Srrs#endif				/* SCTP_DEBUG */
1472163953Srrs		/* nothing to do... duplicate ACK received */
1473163953Srrs		return;
1474163953Srrs	}
1475163953Srrs	/* stop our timer */
1476165220Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1477163953Srrs
1478163953Srrs	/* process the ASCONF-ACK contents */
1479163953Srrs	ack_length = ntohs(cp->ch.chunk_length) -
1480163953Srrs	    sizeof(struct sctp_asconf_ack_chunk);
1481163953Srrs	offset += sizeof(struct sctp_asconf_ack_chunk);
1482163953Srrs	/* process through all parameters */
1483163953Srrs	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1484163953Srrs		unsigned int param_length, param_type;
1485163953Srrs
1486163953Srrs		/* get pointer to next asconf parameter */
1487163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1488163953Srrs		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1489163953Srrs		if (aph == NULL) {
1490163953Srrs			/* can't get an asconf paramhdr */
1491163953Srrs			sctp_asconf_ack_clear(stcb);
1492163953Srrs			return;
1493163953Srrs		}
1494163953Srrs		param_type = ntohs(aph->ph.param_type);
1495163953Srrs		param_length = ntohs(aph->ph.param_length);
1496163953Srrs		if (param_length > ack_length) {
1497163953Srrs			sctp_asconf_ack_clear(stcb);
1498163953Srrs			return;
1499163953Srrs		}
1500163953Srrs		if (param_length < sizeof(struct sctp_paramhdr)) {
1501163953Srrs			sctp_asconf_ack_clear(stcb);
1502163953Srrs			return;
1503163953Srrs		}
1504163953Srrs		/* get the complete parameter... */
1505163953Srrs		if (param_length > sizeof(aparam_buf)) {
1506163953Srrs#ifdef SCTP_DEBUG
1507163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1508163953Srrs				printf("param length (%u) larger than buffer size!\n", param_length);
1509163953Srrs			}
1510163953Srrs#endif				/* SCTP_DEBUG */
1511163953Srrs			sctp_asconf_ack_clear(stcb);
1512163953Srrs			return;
1513163953Srrs		}
1514163953Srrs		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1515163953Srrs		if (aph == NULL) {
1516163953Srrs			sctp_asconf_ack_clear(stcb);
1517163953Srrs			return;
1518163953Srrs		}
1519163953Srrs		/* correlation_id is transparent to peer, no ntohl needed */
1520163953Srrs		id = aph->correlation_id;
1521163953Srrs
1522163953Srrs		switch (param_type) {
1523163953Srrs		case SCTP_ERROR_CAUSE_IND:
1524163953Srrs			last_error_id = id;
1525163953Srrs			/* find the corresponding asconf param in our queue */
1526163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1527163953Srrs			if (ap == NULL) {
1528163953Srrs				/* hmm... can't find this in our queue! */
1529163953Srrs				break;
1530163953Srrs			}
1531163953Srrs			/* process the parameter, failed flag */
1532163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 0);
1533163953Srrs			/* process the error response */
1534163953Srrs			sctp_asconf_process_error(stcb, aph);
1535163953Srrs			break;
1536163953Srrs		case SCTP_SUCCESS_REPORT:
1537163953Srrs			/* find the corresponding asconf param in our queue */
1538163953Srrs			ap = sctp_asconf_find_param(stcb, id);
1539163953Srrs			if (ap == NULL) {
1540163953Srrs				/* hmm... can't find this in our queue! */
1541163953Srrs				break;
1542163953Srrs			}
1543163953Srrs			/* process the parameter, success flag */
1544163953Srrs			sctp_asconf_process_param_ack(stcb, ap, 1);
1545163953Srrs			break;
1546163953Srrs		default:
1547163953Srrs			break;
1548163953Srrs		}		/* switch */
1549163953Srrs
1550163953Srrs		/* update remaining ASCONF-ACK message length to process */
1551163953Srrs		ack_length -= SCTP_SIZE32(param_length);
1552163953Srrs		if (ack_length <= 0) {
1553163953Srrs			/* no more data in the mbuf chain */
1554163953Srrs			break;
1555163953Srrs		}
1556163953Srrs		offset += SCTP_SIZE32(param_length);
1557163953Srrs	}			/* while */
1558163953Srrs
1559163953Srrs	/*
1560163953Srrs	 * if there are any "sent" params still on the queue, these are
1561163953Srrs	 * implicitly "success", or "failed" (if we got an error back) ...
1562163953Srrs	 * so process these appropriately
1563163953Srrs	 *
1564163953Srrs	 * we assume that the correlation_id's are monotonically increasing
1565163953Srrs	 * beginning from 1 and that we don't have *that* many outstanding
1566163953Srrs	 * at any given time
1567163953Srrs	 */
1568163953Srrs	if (last_error_id == 0)
1569163953Srrs		last_error_id--;/* set to "max" value */
1570163953Srrs	for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1571163953Srrs	    aa = aa_next) {
1572163953Srrs		aa_next = TAILQ_NEXT(aa, next);
1573163953Srrs		if (aa->sent == 1) {
1574163953Srrs			/*
1575163953Srrs			 * implicitly successful or failed if correlation_id
1576163953Srrs			 * < last_error_id, then success else, failure
1577163953Srrs			 */
1578163953Srrs			if (aa->ap.aph.correlation_id < last_error_id)
1579163953Srrs				sctp_asconf_process_param_ack(stcb, aa,
1580163953Srrs				    SCTP_SUCCESS_REPORT);
1581163953Srrs			else
1582163953Srrs				sctp_asconf_process_param_ack(stcb, aa,
1583163953Srrs				    SCTP_ERROR_CAUSE_IND);
1584163953Srrs		} else {
1585163953Srrs			/*
1586163953Srrs			 * since we always process in order (FIFO queue) if
1587163953Srrs			 * we reach one that hasn't been sent, the rest
1588163953Srrs			 * should not have been sent either. so, we're
1589163953Srrs			 * done...
1590163953Srrs			 */
1591163953Srrs			break;
1592163953Srrs		}
1593163953Srrs	}
1594163953Srrs
1595163953Srrs	/* update the next sequence number to use */
1596163953Srrs	asoc->asconf_seq_out++;
1597163953Srrs	/* remove the old ASCONF on our outbound queue */
1598163953Srrs	sctp_toss_old_asconf(stcb);
1599163953Srrs	/* clear the sent flag to allow new ASCONFs */
1600163953Srrs	asoc->asconf_sent = 0;
1601163953Srrs	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1602163953Srrs		/* we have more params, so restart our timer */
1603163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1604163953Srrs		    stcb, net);
1605163953Srrs	}
1606163953Srrs}
1607163953Srrs
1608163953Srrsstatic uint32_t
1609163953Srrssctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1610163953Srrs{
1611163953Srrs	struct sockaddr_in6 *sin6, *net6;
1612163953Srrs	struct sctp_nets *net;
1613163953Srrs
1614163953Srrs	if (sa->sa_family != AF_INET6) {
1615163953Srrs		/* wrong family */
1616163953Srrs		return (0);
1617163953Srrs	}
1618163953Srrs	sin6 = (struct sockaddr_in6 *)sa;
1619163953Srrs	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1620163953Srrs		/* not link local address */
1621163953Srrs		return (0);
1622163953Srrs	}
1623163953Srrs	/* hunt through our destination nets list for this scope_id */
1624163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1625163953Srrs		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1626163953Srrs		    AF_INET6)
1627163953Srrs			continue;
1628163953Srrs		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1629163953Srrs		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1630163953Srrs			continue;
1631163953Srrs		if (sctp_is_same_scope(sin6, net6)) {
1632163953Srrs			/* found one */
1633163953Srrs			return (1);
1634163953Srrs		}
1635163953Srrs	}
1636163953Srrs	/* didn't find one */
1637163953Srrs	return (0);
1638163953Srrs}
1639163953Srrs
1640163953Srrs/*
1641163953Srrs * address management functions
1642163953Srrs */
1643163953Srrsstatic void
1644163953Srrssctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1645167598Srrs    struct sctp_ifa *ifa, uint16_t type)
1646163953Srrs{
1647163953Srrs	int status;
1648163953Srrs
1649163953Srrs
1650163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
1651163953Srrs	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1652163953Srrs		/* subset bound, no ASCONF allowed case, so ignore */
1653163953Srrs		return;
1654163953Srrs	}
1655163953Srrs	/*
1656163953Srrs	 * note: we know this is not the subset bound, no ASCONF case eg.
1657163953Srrs	 * this is boundall or subset bound w/ASCONF allowed
1658163953Srrs	 */
1659163953Srrs
1660163953Srrs	/* first, make sure it's a good address family */
1661167598Srrs	if (ifa->address.sa.sa_family != AF_INET6 &&
1662167598Srrs	    ifa->address.sa.sa_family != AF_INET) {
1663163953Srrs		return;
1664163953Srrs	}
1665163953Srrs	/* make sure we're "allowed" to add this type of addr */
1666167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1667163953Srrs		/* invalid if we're not a v6 endpoint */
1668163953Srrs		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1669163953Srrs			return;
1670163953Srrs		/* is the v6 addr really valid ? */
1671167598Srrs		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1672163953Srrs			return;
1673163953Srrs		}
1674163953Srrs	}
1675163953Srrs	/* put this address on the "pending/do not use yet" list */
1676163953Srrs	/*
1677163953Srrs	 * Note: we do this primarily for the subset bind case We don't have
1678163953Srrs	 * scoping flags at the EP level, so we must add link local/site
1679163953Srrs	 * local addresses to the EP, then need to "negate" them here.
1680163953Srrs	 * Recall that this routine is only called for the subset bound
1681163953Srrs	 * w/ASCONF allowed case.
1682163953Srrs	 */
1683167598Srrs	sctp_add_local_addr_assoc(stcb, ifa, 1);
1684163953Srrs	/*
1685163953Srrs	 * check address scope if address is out of scope, don't queue
1686163953Srrs	 * anything... note: this would leave the address on both inp and
1687163953Srrs	 * asoc lists
1688163953Srrs	 */
1689167598Srrs	if (ifa->address.sa.sa_family == AF_INET6) {
1690163953Srrs		struct sockaddr_in6 *sin6;
1691163953Srrs
1692167598Srrs		sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1693163953Srrs		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1694163953Srrs			/* we skip unspecifed addresses */
1695163953Srrs			return;
1696163953Srrs		}
1697163953Srrs		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1698163953Srrs			if (stcb->asoc.local_scope == 0) {
1699163953Srrs				return;
1700163953Srrs			}
1701163953Srrs			/* is it the right link local scope? */
1702167598Srrs			if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1703163953Srrs				return;
1704163953Srrs			}
1705163953Srrs		}
1706163953Srrs		if (stcb->asoc.site_scope == 0 &&
1707163953Srrs		    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
1708163953Srrs			return;
1709163953Srrs		}
1710167598Srrs	} else if (ifa->address.sa.sa_family == AF_INET) {
1711163953Srrs		struct sockaddr_in *sin;
1712163953Srrs		struct in6pcb *inp6;
1713163953Srrs
1714163953Srrs		inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1715163953Srrs		/* invalid if we are a v6 only endpoint */
1716163953Srrs		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1717166023Srrs		    SCTP_IPV6_V6ONLY(inp6))
1718163953Srrs			return;
1719163953Srrs
1720167598Srrs		sin = (struct sockaddr_in *)&ifa->address.sa;
1721163953Srrs		if (sin->sin_addr.s_addr == 0) {
1722163953Srrs			/* we skip unspecifed addresses */
1723163953Srrs			return;
1724163953Srrs		}
1725163953Srrs		if (stcb->asoc.ipv4_local_scope == 0 &&
1726163953Srrs		    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1727163953Srrs			return;
1728163953Srrs		}
1729163953Srrs	} else {
1730163953Srrs		/* else, not AF_INET or AF_INET6, so skip */
1731163953Srrs		return;
1732163953Srrs	}
1733163953Srrs
1734163953Srrs	/* queue an asconf for this address add/delete */
1735163953Srrs	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1736163953Srrs		/* does the peer do asconf? */
1737163953Srrs		if (stcb->asoc.peer_supports_asconf) {
1738163953Srrs			/* queue an asconf for this addr */
1739163953Srrs			status = sctp_asconf_queue_add(stcb, ifa, type);
1740163953Srrs			/*
1741163953Srrs			 * if queued ok, and in correct state, set the
1742163953Srrs			 * ASCONF timer if in non-open state, we will set
1743163953Srrs			 * this timer when the state does go open and do all
1744163953Srrs			 * the asconf's
1745163953Srrs			 */
1746163953Srrs			if (status == 0 &&
1747163953Srrs			    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
1748163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
1749163953Srrs				    stcb, stcb->asoc.primary_destination);
1750163953Srrs			}
1751163953Srrs		}
1752163953Srrs	}
1753163953Srrs}
1754163953Srrs
1755167598Srrs
1756167598Srrsint
1757167598Srrssctp_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val)
1758163953Srrs{
1759167598Srrs	struct sctp_asconf_iterator *asc;
1760167598Srrs	struct sctp_ifa *ifa;
1761167598Srrs	struct sctp_laddr *l;
1762167598Srrs	int type;
1763167598Srrs	int cnt_invalid = 0;
1764163953Srrs
1765167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1766167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
1767167598Srrs		ifa = l->ifa;
1768167598Srrs		type = l->action;
1769167598Srrs		if (ifa->address.sa.sa_family == AF_INET6) {
1770167598Srrs			/* invalid if we're not a v6 endpoint */
1771167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1772167598Srrs				cnt_invalid++;
1773167598Srrs				if (asc->cnt == cnt_invalid)
1774167598Srrs					return (1);
1775167598Srrs				else
1776167598Srrs					continue;
1777167598Srrs			}
1778167598Srrs		} else if (ifa->address.sa.sa_family == AF_INET) {
1779167598Srrs			/* invalid if we are a v6 only endpoint */
1780167598Srrs			struct in6pcb *inp6;
1781163953Srrs
1782167598Srrs			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1783167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1784167598Srrs			    SCTP_IPV6_V6ONLY(inp6)) {
1785167598Srrs				cnt_invalid++;
1786167598Srrs				if (asc->cnt == cnt_invalid)
1787167598Srrs					return (1);
1788167598Srrs				else
1789167598Srrs					continue;
1790163953Srrs			}
1791163953Srrs		} else {
1792167598Srrs			/* invalid address family */
1793167598Srrs			cnt_invalid++;
1794167598Srrs			if (asc->cnt == cnt_invalid)
1795167598Srrs				return (1);
1796167598Srrs			else
1797167598Srrs				continue;
1798163953Srrs		}
1799163953Srrs	}
1800167598Srrs	return (0);
1801163953Srrs}
1802163953Srrs
1803167598Srrsint
1804167598Srrssctp_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val)
1805163953Srrs{
1806167598Srrs	struct sctp_ifa *ifa;
1807167598Srrs	struct sctp_asconf_iterator *asc;
1808167598Srrs	struct sctp_laddr *laddr, *nladdr, *l;
1809163953Srrs
1810167598Srrs	/* Only for specific case not bound all */
1811167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1812167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
1813167598Srrs		ifa = l->ifa;
1814167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
1815167598Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1816167598Srrs				if (laddr->ifa == ifa) {
1817167598Srrs					laddr->action = 0;
1818167598Srrs					break;
1819167598Srrs				}
1820167598Srrs			}
1821167598Srrs		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
1822167598Srrs			laddr = LIST_FIRST(&inp->sctp_addr_list);
1823167598Srrs			while (laddr) {
1824167598Srrs				nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
1825167598Srrs				/* remove only after all guys are done */
1826167598Srrs				if (laddr->ifa == ifa) {
1827167598Srrs					sctp_del_local_addr_ep(inp, ifa);
1828167598Srrs				}
1829167598Srrs				laddr = nladdr;
1830167598Srrs			}
1831167598Srrs		}
1832163953Srrs	}
1833167598Srrs	return (0);
1834163953Srrs}
1835163953Srrs
1836167598Srrsvoid
1837167598Srrssctp_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
1838167598Srrs    uint32_t val)
1839163953Srrs{
1840167598Srrs	struct sctp_asconf_iterator *asc;
1841167598Srrs	struct sctp_ifa *ifa;
1842167598Srrs	struct sctp_laddr *l;
1843167598Srrs	int cnt_invalid = 0;
1844167598Srrs	int type, status;
1845163953Srrs
1846167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1847167598Srrs	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
1848167598Srrs		ifa = l->ifa;
1849167598Srrs		type = l->action;
1850167598Srrs		/* Same checks again for assoc */
1851167598Srrs		if (ifa->address.sa.sa_family == AF_INET6) {
1852167598Srrs			/* invalid if we're not a v6 endpoint */
1853167598Srrs			struct sockaddr_in6 *sin6;
1854163953Srrs
1855167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1856167598Srrs				cnt_invalid++;
1857167598Srrs				if (asc->cnt == cnt_invalid)
1858167598Srrs					return;
1859167598Srrs				else
1860167598Srrs					continue;
1861167598Srrs			}
1862167598Srrs			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1863167598Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1864167598Srrs				/* we skip unspecifed addresses */
1865167598Srrs				continue;
1866167598Srrs			}
1867167598Srrs			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1868167598Srrs				if (stcb->asoc.local_scope == 0) {
1869167598Srrs					continue;
1870167598Srrs				}
1871167598Srrs				/* is it the right link local scope? */
1872167598Srrs				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1873167598Srrs					continue;
1874167598Srrs				}
1875167598Srrs			}
1876167598Srrs		} else if (ifa->address.sa.sa_family == AF_INET) {
1877167598Srrs			/* invalid if we are a v6 only endpoint */
1878167598Srrs			struct in6pcb *inp6;
1879167598Srrs			struct sockaddr_in *sin;
1880163953Srrs
1881167598Srrs			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1882167598Srrs			/* invalid if we are a v6 only endpoint */
1883167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1884167598Srrs			    SCTP_IPV6_V6ONLY(inp6))
1885167598Srrs				continue;
1886167598Srrs
1887167598Srrs			sin = (struct sockaddr_in *)&ifa->address.sa;
1888167598Srrs			if (sin->sin_addr.s_addr == 0) {
1889167598Srrs				/* we skip unspecifed addresses */
1890167598Srrs				continue;
1891167598Srrs			}
1892167598Srrs			if (stcb->asoc.ipv4_local_scope == 0 &&
1893167598Srrs			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1894167598Srrs				continue;;
1895167598Srrs			}
1896167598Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1897167598Srrs			    SCTP_IPV6_V6ONLY(inp6)) {
1898167598Srrs				cnt_invalid++;
1899167598Srrs				if (asc->cnt == cnt_invalid)
1900167598Srrs					return;
1901167598Srrs				else
1902167598Srrs					continue;
1903167598Srrs			}
1904163953Srrs		} else {
1905167598Srrs			/* invalid address family */
1906167598Srrs			cnt_invalid++;
1907167598Srrs			if (asc->cnt == cnt_invalid)
1908163953Srrs				return;
1909167598Srrs			else
1910167598Srrs				continue;
1911163953Srrs		}
1912163953Srrs
1913167598Srrs		/* put this address on the "pending/do not use yet" list */
1914167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
1915167598Srrs			sctp_add_local_addr_assoc(stcb, ifa, 1);
1916167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
1917163953Srrs			struct sctp_nets *net;
1918163953Srrs
1919163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1920169352Srrs				sctp_rtentry_t *rt;
1921163953Srrs
1922163953Srrs				/* delete this address if cached */
1923167598Srrs				if (net->ro._s_addr &&
1924167598Srrs				    (net->ro._s_addr->ifa == ifa)) {
1925167598Srrs					sctp_free_ifa(net->ro._s_addr);
1926167598Srrs					net->ro._s_addr = NULL;
1927167598Srrs					net->src_addr_selected = 0;
1928167598Srrs					rt = net->ro.ro_rt;
1929167598Srrs					if (rt) {
1930167598Srrs						RTFREE(rt);
1931167598Srrs						net->ro.ro_rt = NULL;
1932167598Srrs					}
1933167598Srrs					/*
1934167598Srrs					 * Now we deleted our src address,
1935167598Srrs					 * should we not also now reset the
1936167598Srrs					 * cwnd/rto to start as if its a new
1937167598Srrs					 * address?
1938167598Srrs					 */
1939167598Srrs					sctp_set_initial_cc_param(stcb, net);
1940167598Srrs					net->RTO = stcb->asoc.initial_rto;
1941167598Srrs
1942163953Srrs				}
1943167598Srrs			}
1944167598Srrs		} else if (type == SCTP_SET_PRIM_ADDR) {
1945167598Srrs			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1946167598Srrs				/*
1947167598Srrs				 * must validate the ifa in question is in
1948167598Srrs				 * the ep
1949167598Srrs				 */
1950167598Srrs				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
1951167598Srrs					continue;
1952163953Srrs				}
1953167598Srrs			} else {
1954167598Srrs				/* Need to check scopes for this guy */
1955167598Srrs				if (sctp_is_address_in_scope(ifa,
1956167598Srrs				    stcb->asoc.ipv4_addr_legal,
1957167598Srrs				    stcb->asoc.ipv6_addr_legal,
1958167598Srrs				    stcb->asoc.loopback_scope,
1959167598Srrs				    stcb->asoc.ipv4_local_scope,
1960167598Srrs				    stcb->asoc.local_scope,
1961167598Srrs				    stcb->asoc.site_scope, 0) == 0) {
1962167598Srrs					continue;
1963167598Srrs				}
1964163953Srrs			}
1965167598Srrs
1966163953Srrs		}
1967167598Srrs		/* queue an asconf for this address add/delete */
1968167598Srrs		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1969167598Srrs			/* does the peer do asconf? */
1970167598Srrs			if (stcb->asoc.peer_supports_asconf) {
1971167598Srrs				/* queue an asconf for this addr */
1972167598Srrs
1973167598Srrs				status = sctp_asconf_queue_add(stcb, ifa, type);
1974167598Srrs				/*
1975167598Srrs				 * if queued ok, and in correct state, set
1976167598Srrs				 * the ASCONF timer if in non-open state, we
1977167598Srrs				 * will set this timer when the state does
1978167598Srrs				 * go open and do all the asconf's
1979167598Srrs				 */
1980167598Srrs				if (status == 0 &&
1981167598Srrs				    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
1982167598Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
1983167598Srrs					    stcb, stcb->asoc.primary_destination);
1984167598Srrs				}
1985167598Srrs			}
1986167598Srrs		}
1987163953Srrs	}
1988163953Srrs}
1989163953Srrs
1990167598Srrsvoid
1991167598Srrssctp_iterator_end(void *ptr, uint32_t val)
1992167598Srrs{
1993167598Srrs	struct sctp_asconf_iterator *asc;
1994167598Srrs	struct sctp_ifa *ifa;
1995167598Srrs	struct sctp_laddr *l, *l_next;
1996167598Srrs
1997167598Srrs	asc = (struct sctp_asconf_iterator *)ptr;
1998167598Srrs	l = LIST_FIRST(&asc->list_of_work);
1999167598Srrs	while (l != NULL) {
2000167598Srrs		l_next = LIST_NEXT(l, sctp_nxt_addr);
2001167598Srrs		ifa = l->ifa;
2002167598Srrs		if (l->action == SCTP_ADD_IP_ADDRESS) {
2003167598Srrs			/* Clear the defer use flag */
2004167598Srrs			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
2005167598Srrs		}
2006167598Srrs		sctp_free_ifa(ifa);
2007167598Srrs		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, l);
2008167598Srrs		SCTP_DECR_LADDR_COUNT();
2009167598Srrs		l = l_next;
2010167598Srrs	}
2011167598Srrs	SCTP_FREE(asc);
2012167598Srrs}
2013167598Srrs
2014163953Srrs/*
2015163953Srrs * sa is the sockaddr to ask the peer to set primary to returns: 0 =
2016163953Srrs * completed, -1 = error
2017163953Srrs */
2018163953Srrsint32_t
2019163953Srrssctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2020163953Srrs{
2021163953Srrs	/* NOTE: we currently don't check the validity of the address! */
2022163953Srrs
2023163953Srrs	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2024163953Srrs	if (!sctp_asconf_queue_add_sa(stcb, sa, SCTP_SET_PRIM_ADDR)) {
2025163953Srrs		/* set primary queuing succeeded */
2026163953Srrs		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2027163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2028163953Srrs			    stcb->sctp_ep, stcb,
2029163953Srrs			    stcb->asoc.primary_destination);
2030163953Srrs		}
2031163953Srrs#ifdef SCTP_DEBUG
2032163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2033163953Srrs			printf("set_primary_ip_address_sa: queued on tcb=%p, ",
2034163953Srrs			    stcb);
2035163953Srrs			sctp_print_address(sa);
2036163953Srrs		}
2037163953Srrs#endif				/* SCTP_DEBUG */
2038163953Srrs	} else {
2039163953Srrs#ifdef SCTP_DEBUG
2040163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2041163953Srrs			printf("set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2042163953Srrs			    stcb);
2043163953Srrs			sctp_print_address(sa);
2044163953Srrs		}
2045163953Srrs#endif				/* SCTP_DEBUG */
2046163953Srrs		return (-1);
2047163953Srrs	}
2048163953Srrs	return (0);
2049163953Srrs}
2050163953Srrs
2051163953Srrsvoid
2052167598Srrssctp_set_primary_ip_address(struct sctp_ifa *ifa)
2053163953Srrs{
2054163953Srrs	struct sctp_inpcb *inp;
2055163953Srrs
2056163953Srrs	/* go through all our PCB's */
2057163953Srrs	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
2058163953Srrs		struct sctp_tcb *stcb;
2059163953Srrs
2060163953Srrs		/* process for all associations for this endpoint */
2061163953Srrs		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2062163953Srrs			/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2063163953Srrs			if (!sctp_asconf_queue_add(stcb, ifa,
2064163953Srrs			    SCTP_SET_PRIM_ADDR)) {
2065163953Srrs				/* set primary queuing succeeded */
2066163953Srrs				if (SCTP_GET_STATE(&stcb->asoc) ==
2067163953Srrs				    SCTP_STATE_OPEN) {
2068163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2069163953Srrs					    stcb->sctp_ep, stcb,
2070163953Srrs					    stcb->asoc.primary_destination);
2071163953Srrs				}
2072163953Srrs#ifdef SCTP_DEBUG
2073163953Srrs				if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2074163953Srrs					printf("set_primary_ip_address: queued on stcb=%p, ",
2075163953Srrs					    stcb);
2076167598Srrs					sctp_print_address(&ifa->address.sa);
2077163953Srrs				}
2078163953Srrs#endif				/* SCTP_DEBUG */
2079163953Srrs			}
2080163953Srrs		}		/* for each stcb */
2081163953Srrs	}			/* for each inp */
2082163953Srrs}
2083163953Srrs
2084163953Srrsstatic struct sockaddr *
2085163953Srrssctp_find_valid_localaddr(struct sctp_tcb *stcb)
2086163953Srrs{
2087167598Srrs	struct sctp_vrf *vrf = NULL;
2088167598Srrs	struct sctp_ifn *sctp_ifn;
2089167598Srrs	struct sctp_ifa *sctp_ifa;
2090163953Srrs
2091167598Srrs	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
2092167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2093167598Srrs		if (stcb->asoc.loopback_scope == 0 &&
2094167598Srrs		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2095163953Srrs			/* Skip if loopback_scope not set */
2096163953Srrs			continue;
2097163953Srrs		}
2098167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2099167598Srrs			if (sctp_ifa->address.sa.sa_family == AF_INET &&
2100163953Srrs			    stcb->asoc.ipv4_addr_legal) {
2101163953Srrs				struct sockaddr_in *sin;
2102163953Srrs
2103167598Srrs				sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
2104163953Srrs				if (sin->sin_addr.s_addr == 0) {
2105163953Srrs					/* skip unspecifed addresses */
2106163953Srrs					continue;
2107163953Srrs				}
2108163953Srrs				if (stcb->asoc.ipv4_local_scope == 0 &&
2109163953Srrs				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2110163953Srrs					continue;
2111163953Srrs
2112167598Srrs				if (sctp_is_addr_restricted(stcb, sctp_ifa))
2113163953Srrs					continue;
2114163953Srrs				/* found a valid local v4 address to use */
2115167598Srrs				return (&sctp_ifa->address.sa);
2116167598Srrs			} else if (sctp_ifa->address.sa.sa_family == AF_INET6 &&
2117163953Srrs			    stcb->asoc.ipv6_addr_legal) {
2118163953Srrs				struct sockaddr_in6 *sin6;
2119163953Srrs
2120167598Srrs				if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2121163953Srrs					continue;
2122167598Srrs				}
2123167598Srrs				sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
2124163953Srrs				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2125163953Srrs					/* we skip unspecifed addresses */
2126163953Srrs					continue;
2127163953Srrs				}
2128163953Srrs				if (stcb->asoc.local_scope == 0 &&
2129163953Srrs				    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2130163953Srrs					continue;
2131163953Srrs				if (stcb->asoc.site_scope == 0 &&
2132163953Srrs				    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2133163953Srrs					continue;
2134163953Srrs
2135163953Srrs				/* found a valid local v6 address to use */
2136167598Srrs				return (&sctp_ifa->address.sa);
2137163953Srrs			}
2138163953Srrs		}
2139163953Srrs	}
2140163953Srrs	/* no valid addresses found */
2141163953Srrs	return (NULL);
2142163953Srrs}
2143163953Srrs
2144163953Srrsstatic struct sockaddr *
2145163953Srrssctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2146163953Srrs{
2147163953Srrs	struct sctp_laddr *laddr;
2148163953Srrs
2149163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2150163953Srrs		if (laddr->ifa == NULL) {
2151163953Srrs			continue;
2152163953Srrs		}
2153167598Srrs		if (laddr->ifa == NULL) {
2154163953Srrs			continue;
2155163953Srrs		}
2156163953Srrs		/* is the address restricted ? */
2157167598Srrs		if (sctp_is_addr_restricted(stcb, laddr->ifa))
2158163953Srrs			continue;
2159163953Srrs
2160163953Srrs		/* found a valid local address to use */
2161167598Srrs		return (&laddr->ifa->address.sa);
2162163953Srrs	}
2163163953Srrs	/* no valid addresses found */
2164163953Srrs	return (NULL);
2165163953Srrs}
2166163953Srrs
2167163953Srrs/*
2168163953Srrs * builds an ASCONF chunk from queued ASCONF params returns NULL on error (no
2169163953Srrs * mbuf, no ASCONF params queued, etc)
2170163953Srrs */
2171163953Srrsstruct mbuf *
2172165647Srrssctp_compose_asconf(struct sctp_tcb *stcb, int *retlen)
2173163953Srrs{
2174163953Srrs	struct mbuf *m_asconf, *m_asconf_chk;
2175163953Srrs	struct sctp_asconf_addr *aa;
2176163953Srrs	struct sctp_asconf_chunk *acp;
2177163953Srrs	struct sctp_asconf_paramhdr *aph;
2178163953Srrs	struct sctp_asconf_addr_param *aap;
2179163953Srrs	uint32_t p_length;
2180163953Srrs	uint32_t correlation_id = 1;	/* 0 is reserved... */
2181163953Srrs	caddr_t ptr, lookup_ptr;
2182163953Srrs	uint8_t lookup_used = 0;
2183163953Srrs
2184163953Srrs	/* are there any asconf params to send? */
2185163953Srrs	if (TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
2186163953Srrs		return (NULL);
2187163953Srrs	}
2188163953Srrs	/*
2189163953Srrs	 * get a chunk header mbuf and a cluster for the asconf params since
2190163953Srrs	 * it's simpler to fill in the asconf chunk header lookup address on
2191163953Srrs	 * the fly
2192163953Srrs	 */
2193165647Srrs	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_DONTWAIT, 1, MT_DATA);
2194163953Srrs	if (m_asconf_chk == NULL) {
2195163953Srrs		/* no mbuf's */
2196163953Srrs#ifdef SCTP_DEBUG
2197163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
2198163953Srrs			printf("compose_asconf: couldn't get chunk mbuf!\n");
2199163953Srrs#endif				/* SCTP_DEBUG */
2200163953Srrs		return (NULL);
2201163953Srrs	}
2202165647Srrs	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
2203163953Srrs	if (m_asconf == NULL) {
2204163953Srrs		/* no mbuf's */
2205163953Srrs#ifdef SCTP_DEBUG
2206163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
2207163953Srrs			printf("compose_asconf: couldn't get mbuf!\n");
2208163953Srrs#endif				/* SCTP_DEBUG */
2209163953Srrs		sctp_m_freem(m_asconf_chk);
2210163953Srrs		return (NULL);
2211163953Srrs	}
2212165647Srrs	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2213165647Srrs	SCTP_BUF_LEN(m_asconf) = 0;
2214163953Srrs	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2215163953Srrs	bzero(acp, sizeof(struct sctp_asconf_chunk));
2216163953Srrs	/* save pointers to lookup address and asconf params */
2217163953Srrs	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2218163953Srrs	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2219163953Srrs
2220163953Srrs	/* fill in chunk header info */
2221163953Srrs	acp->ch.chunk_type = SCTP_ASCONF;
2222163953Srrs	acp->ch.chunk_flags = 0;
2223163953Srrs	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2224163953Srrs
2225163953Srrs	/* add parameters... up to smallest MTU allowed */
2226163953Srrs	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2227163953Srrs		/* get the parameter length */
2228163953Srrs		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2229163953Srrs		/* will it fit in current chunk? */
2230165647Srrs		if (SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu) {
2231163953Srrs			/* won't fit, so we're done with this chunk */
2232163953Srrs			break;
2233163953Srrs		}
2234163953Srrs		/* assign (and store) a correlation id */
2235163953Srrs		aa->ap.aph.correlation_id = correlation_id++;
2236163953Srrs
2237163953Srrs		/*
2238163953Srrs		 * fill in address if we're doing a delete this is a simple
2239163953Srrs		 * way for us to fill in the correlation address, which
2240163953Srrs		 * should only be used by the peer if we're deleting our
2241163953Srrs		 * source address and adding a new address (e.g. renumbering
2242163953Srrs		 * case)
2243163953Srrs		 */
2244163953Srrs		if (lookup_used == 0 &&
2245163953Srrs		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2246163953Srrs			struct sctp_ipv6addr_param *lookup;
2247163953Srrs			uint16_t p_size, addr_size;
2248163953Srrs
2249163953Srrs			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2250163953Srrs			lookup->ph.param_type =
2251163953Srrs			    htons(aa->ap.addrp.ph.param_type);
2252163953Srrs			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2253163953Srrs				/* copy IPv6 address */
2254163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2255163953Srrs				addr_size = sizeof(struct in6_addr);
2256163953Srrs			} else {
2257163953Srrs				/* copy IPv4 address */
2258163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2259163953Srrs				addr_size = sizeof(struct in_addr);
2260163953Srrs			}
2261163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2262163953Srrs			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2263165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2264163953Srrs			lookup_used = 1;
2265163953Srrs		}
2266163953Srrs		/* copy into current space */
2267163953Srrs		memcpy(ptr, &aa->ap, p_length);
2268163953Srrs
2269163953Srrs		/* network elements and update lengths */
2270163953Srrs		aph = (struct sctp_asconf_paramhdr *)ptr;
2271163953Srrs		aap = (struct sctp_asconf_addr_param *)ptr;
2272163953Srrs		/* correlation_id is transparent to peer, no htonl needed */
2273163953Srrs		aph->ph.param_type = htons(aph->ph.param_type);
2274163953Srrs		aph->ph.param_length = htons(aph->ph.param_length);
2275163953Srrs		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2276163953Srrs		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2277163953Srrs
2278165647Srrs		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2279163953Srrs		ptr += SCTP_SIZE32(p_length);
2280163953Srrs
2281163953Srrs		/*
2282163953Srrs		 * these params are removed off the pending list upon
2283163953Srrs		 * getting an ASCONF-ACK back from the peer, just set flag
2284163953Srrs		 */
2285163953Srrs		aa->sent = 1;
2286163953Srrs	}
2287163953Srrs	/* check to see if the lookup addr has been populated yet */
2288163953Srrs	if (lookup_used == 0) {
2289163953Srrs		/* NOTE: if the address param is optional, can skip this... */
2290163953Srrs		/* add any valid (existing) address... */
2291163953Srrs		struct sctp_ipv6addr_param *lookup;
2292163953Srrs		uint16_t p_size, addr_size;
2293163953Srrs		struct sockaddr *found_addr;
2294163953Srrs		caddr_t addr_ptr;
2295163953Srrs
2296163953Srrs		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2297163953Srrs			found_addr = sctp_find_valid_localaddr(stcb);
2298163953Srrs		else
2299163953Srrs			found_addr = sctp_find_valid_localaddr_ep(stcb);
2300163953Srrs
2301163953Srrs		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2302163953Srrs		if (found_addr != NULL) {
2303163953Srrs			if (found_addr->sa_family == AF_INET6) {
2304163953Srrs				/* copy IPv6 address */
2305163953Srrs				lookup->ph.param_type =
2306163953Srrs				    htons(SCTP_IPV6_ADDRESS);
2307163953Srrs				p_size = sizeof(struct sctp_ipv6addr_param);
2308163953Srrs				addr_size = sizeof(struct in6_addr);
2309163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2310163953Srrs				    found_addr)->sin6_addr;
2311163953Srrs			} else {
2312163953Srrs				/* copy IPv4 address */
2313163953Srrs				lookup->ph.param_type =
2314163953Srrs				    htons(SCTP_IPV4_ADDRESS);
2315163953Srrs				p_size = sizeof(struct sctp_ipv4addr_param);
2316163953Srrs				addr_size = sizeof(struct in_addr);
2317163953Srrs				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2318163953Srrs				    found_addr)->sin_addr;
2319163953Srrs			}
2320163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2321163953Srrs			memcpy(lookup->addr, addr_ptr, addr_size);
2322165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2323163953Srrs			lookup_used = 1;
2324163953Srrs		} else {
2325163953Srrs			/* uh oh... don't have any address?? */
2326163953Srrs#ifdef SCTP_DEBUG
2327163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
2328163953Srrs				printf("compose_asconf: no lookup addr!\n");
2329163953Srrs#endif				/* SCTP_DEBUG */
2330163953Srrs			/* for now, we send a IPv4 address of 0.0.0.0 */
2331163953Srrs			lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2332163953Srrs			lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2333163953Srrs			bzero(lookup->addr, sizeof(struct in_addr));
2334165647Srrs			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2335163953Srrs			lookup_used = 1;
2336163953Srrs		}
2337163953Srrs	}
2338163953Srrs	/* chain it all together */
2339165647Srrs	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
2340165647Srrs	*retlen = acp->ch.chunk_length = ntohs(SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf));
2341163953Srrs
2342163953Srrs	/* update "sent" flag */
2343163953Srrs	stcb->asoc.asconf_sent++;
2344163953Srrs
2345163953Srrs	return (m_asconf_chk);
2346163953Srrs}
2347163953Srrs
2348163953Srrs/*
2349163953Srrs * section to handle address changes before an association is up eg. changes
2350163953Srrs * during INIT/INIT-ACK/COOKIE-ECHO handshake
2351163953Srrs */
2352163953Srrs
2353163953Srrs/*
2354163953Srrs * processes the (local) addresses in the INIT-ACK chunk
2355163953Srrs */
2356163953Srrsstatic void
2357163953Srrssctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2358163953Srrs    unsigned int offset, unsigned int length)
2359163953Srrs{
2360163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2361163953Srrs	uint16_t plen, ptype;
2362167598Srrs	struct sctp_ifa *sctp_ifa;
2363163953Srrs	struct sctp_ipv6addr_param addr_store;
2364163953Srrs	struct sockaddr_in6 sin6;
2365163953Srrs	struct sockaddr_in sin;
2366163953Srrs	struct sockaddr *sa;
2367167598Srrs	uint32_t vrf_id;
2368163953Srrs
2369163953Srrs#ifdef SCTP_DEBUG
2370163953Srrs	if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2371163953Srrs		printf("processing init-ack addresses\n");
2372163953Srrs	}
2373163953Srrs#endif				/* SCTP_DEBUG */
2374163953Srrs
2375163953Srrs	/* convert to upper bound */
2376163953Srrs	length += offset;
2377163953Srrs
2378163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2379163953Srrs		return;
2380163953Srrs	}
2381163953Srrs	/* init the addresses */
2382163953Srrs	bzero(&sin6, sizeof(sin6));
2383163953Srrs	sin6.sin6_family = AF_INET6;
2384163953Srrs	sin6.sin6_len = sizeof(sin6);
2385163953Srrs	sin6.sin6_port = stcb->rport;
2386163953Srrs
2387163953Srrs	bzero(&sin, sizeof(sin));
2388163953Srrs	sin.sin_len = sizeof(sin);
2389163953Srrs	sin.sin_family = AF_INET;
2390163953Srrs	sin.sin_port = stcb->rport;
2391163953Srrs
2392163953Srrs	/* go through the addresses in the init-ack */
2393163953Srrs	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2394163953Srrs	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2395163953Srrs	while (ph != NULL) {
2396163953Srrs		ptype = ntohs(ph->param_type);
2397163953Srrs		plen = ntohs(ph->param_length);
2398163953Srrs		if (ptype == SCTP_IPV6_ADDRESS) {
2399163953Srrs			struct sctp_ipv6addr_param *a6p;
2400163953Srrs
2401163953Srrs			/* get the entire IPv6 address param */
2402163953Srrs			a6p = (struct sctp_ipv6addr_param *)
2403163953Srrs			    sctp_m_getptr(m, offset,
2404163953Srrs			    sizeof(struct sctp_ipv6addr_param),
2405163953Srrs			    (uint8_t *) & addr_store);
2406163953Srrs			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2407163953Srrs			    a6p == NULL) {
2408163953Srrs				return;
2409163953Srrs			}
2410163953Srrs			memcpy(&sin6.sin6_addr, a6p->addr,
2411163953Srrs			    sizeof(struct in6_addr));
2412163953Srrs			sa = (struct sockaddr *)&sin6;
2413163953Srrs		} else if (ptype == SCTP_IPV4_ADDRESS) {
2414163953Srrs			struct sctp_ipv4addr_param *a4p;
2415163953Srrs
2416163953Srrs			/* get the entire IPv4 address param */
2417167598Srrs			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
2418167598Srrs			    sizeof(struct sctp_ipv4addr_param),
2419167598Srrs			    (uint8_t *) & addr_store);
2420163953Srrs			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2421163953Srrs			    a4p == NULL) {
2422163953Srrs				return;
2423163953Srrs			}
2424163953Srrs			sin.sin_addr.s_addr = a4p->addr;
2425163953Srrs			sa = (struct sockaddr *)&sin;
2426163953Srrs		} else {
2427163953Srrs			goto next_addr;
2428163953Srrs		}
2429163953Srrs
2430163953Srrs		/* see if this address really (still) exists */
2431168299Srrs		if (stcb) {
2432168299Srrs			vrf_id = stcb->asoc.vrf_id;
2433168299Srrs		} else {
2434168299Srrs			vrf_id = SCTP_DEFAULT_VRFID;
2435168299Srrs		}
2436168299Srrs
2437167598Srrs		sctp_ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
2438167598Srrs		if (sctp_ifa == NULL) {
2439163953Srrs			/* address doesn't exist anymore */
2440163953Srrs			int status;
2441163953Srrs
2442163953Srrs			/* are ASCONFs allowed ? */
2443163953Srrs			if ((sctp_is_feature_on(stcb->sctp_ep,
2444163953Srrs			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2445163953Srrs			    stcb->asoc.peer_supports_asconf) {
2446163953Srrs				/* queue an ASCONF DEL_IP_ADDRESS */
2447163953Srrs				status = sctp_asconf_queue_add_sa(stcb, sa,
2448163953Srrs				    SCTP_DEL_IP_ADDRESS);
2449163953Srrs				/*
2450163953Srrs				 * if queued ok, and in correct state, set
2451163953Srrs				 * the ASCONF timer
2452163953Srrs				 */
2453163953Srrs				if (status == 0 &&
2454163953Srrs				    SCTP_GET_STATE(&stcb->asoc) ==
2455163953Srrs				    SCTP_STATE_OPEN) {
2456163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2457163953Srrs					    stcb->sctp_ep, stcb,
2458163953Srrs					    stcb->asoc.primary_destination);
2459163953Srrs				}
2460163953Srrs			}
2461163953Srrs		}
2462163953Srrsnext_addr:
2463163953Srrs		/*
2464163953Srrs		 * Sanity check:  Make sure the length isn't 0, otherwise
2465163953Srrs		 * we'll be stuck in this loop for a long time...
2466163953Srrs		 */
2467163953Srrs		if (SCTP_SIZE32(plen) == 0) {
2468163953Srrs#ifdef SCTP_DEBUG
2469163953Srrs			printf("process_initack_addrs: bad len (%d) type=%xh\n",
2470163953Srrs			    plen, ptype);
2471163953Srrs#endif
2472163953Srrs			return;
2473163953Srrs		}
2474163953Srrs		/* get next parameter */
2475163953Srrs		offset += SCTP_SIZE32(plen);
2476163953Srrs		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2477163953Srrs			return;
2478163953Srrs		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2479163953Srrs		    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2480163953Srrs	}			/* while */
2481163953Srrs}
2482163953Srrs
2483163953Srrs/* FIX ME: need to verify return result for v6 address type if v6 disabled */
2484163953Srrs/*
2485163953Srrs * checks to see if a specific address is in the initack address list returns
2486163953Srrs * 1 if found, 0 if not
2487163953Srrs */
2488163953Srrsstatic uint32_t
2489163953Srrssctp_addr_in_initack(struct sctp_tcb *stcb, struct mbuf *m, uint32_t offset,
2490163953Srrs    uint32_t length, struct sockaddr *sa)
2491163953Srrs{
2492163953Srrs	struct sctp_paramhdr tmp_param, *ph;
2493163953Srrs	uint16_t plen, ptype;
2494163953Srrs	struct sctp_ipv6addr_param addr_store;
2495163953Srrs	struct sockaddr_in *sin;
2496163953Srrs	struct sctp_ipv4addr_param *a4p;
2497163953Srrs
2498163953Srrs#ifdef INET6
2499167598Srrs	struct sockaddr_in6 *sin6;
2500163953Srrs	struct sctp_ipv6addr_param *a6p;
2501167598Srrs	struct sockaddr_in6 sin6_tmp;
2502163953Srrs
2503163953Srrs#endif				/* INET6 */
2504163953Srrs
2505163953Srrs	if (
2506163953Srrs#ifdef INET6
2507163953Srrs	    (sa->sa_family != AF_INET6) &&
2508163953Srrs#endif				/* INET6 */
2509163953Srrs	    (sa->sa_family != AF_INET))
2510163953Srrs		return (0);
2511163953Srrs
2512163953Srrs#ifdef SCTP_DEBUG
2513163953Srrs	if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2514163953Srrs		printf("find_initack_addr: starting search for ");
2515163953Srrs		sctp_print_address(sa);
2516163953Srrs	}
2517163953Srrs#endif				/* SCTP_DEBUG */
2518163953Srrs	/* convert to upper bound */
2519163953Srrs	length += offset;
2520163953Srrs
2521163953Srrs	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2522163953Srrs#ifdef SCTP_DEBUG
2523163953Srrs		if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2524163953Srrs			printf("find_initack_addr: invalid offset?\n");
2525163953Srrs		}
2526163953Srrs#endif				/* SCTP_DEBUG */
2527163953Srrs		return (0);
2528163953Srrs	}
2529163953Srrs	/* go through the addresses in the init-ack */
2530163953Srrs	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2531163953Srrs	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2532163953Srrs	while (ph != NULL) {
2533163953Srrs		ptype = ntohs(ph->param_type);
2534163953Srrs		plen = ntohs(ph->param_length);
2535163953Srrs#ifdef INET6
2536163953Srrs		if (ptype == SCTP_IPV6_ADDRESS && sa->sa_family == AF_INET6) {
2537163953Srrs			/* get the entire IPv6 address param */
2538163953Srrs			a6p = (struct sctp_ipv6addr_param *)
2539163953Srrs			    sctp_m_getptr(m, offset,
2540163953Srrs			    sizeof(struct sctp_ipv6addr_param),
2541163953Srrs			    (uint8_t *) & addr_store);
2542163953Srrs			if (plen != sizeof(struct sctp_ipv6addr_param) ||
2543163953Srrs			    ph == NULL) {
2544163953Srrs				return (0);
2545163953Srrs			}
2546163953Srrs			sin6 = (struct sockaddr_in6 *)sa;
2547163953Srrs			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2548163953Srrs				/* create a copy and clear scope */
2549163953Srrs				memcpy(&sin6_tmp, sin6,
2550163953Srrs				    sizeof(struct sockaddr_in6));
2551163953Srrs				sin6 = &sin6_tmp;
2552163953Srrs				in6_clearscope(&sin6->sin6_addr);
2553163953Srrs			}
2554163953Srrs			if (memcmp(&sin6->sin6_addr, a6p->addr,
2555163953Srrs			    sizeof(struct in6_addr)) == 0) {
2556163953Srrs				/* found it */
2557163953Srrs				return (1);
2558163953Srrs			}
2559163953Srrs		} else
2560163953Srrs#endif				/* INET6 */
2561163953Srrs
2562163953Srrs			if (ptype == SCTP_IPV4_ADDRESS &&
2563163953Srrs		    sa->sa_family == AF_INET) {
2564163953Srrs			/* get the entire IPv4 address param */
2565163953Srrs			a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m,
2566163953Srrs			    offset, sizeof(struct sctp_ipv4addr_param),
2567163953Srrs			    (uint8_t *) & addr_store);
2568163953Srrs			if (plen != sizeof(struct sctp_ipv4addr_param) ||
2569163953Srrs			    ph == NULL) {
2570163953Srrs				return (0);
2571163953Srrs			}
2572163953Srrs			sin = (struct sockaddr_in *)sa;
2573163953Srrs			if (sin->sin_addr.s_addr == a4p->addr) {
2574163953Srrs				/* found it */
2575163953Srrs				return (1);
2576163953Srrs			}
2577163953Srrs		}
2578163953Srrs		/* get next parameter */
2579163953Srrs		offset += SCTP_SIZE32(plen);
2580163953Srrs		if (offset + sizeof(struct sctp_paramhdr) > length)
2581163953Srrs			return (0);
2582163953Srrs		ph = (struct sctp_paramhdr *)
2583163953Srrs		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
2584163953Srrs		    (uint8_t *) & tmp_param);
2585163953Srrs	}			/* while */
2586163953Srrs	/* not found! */
2587163953Srrs	return (0);
2588163953Srrs}
2589163953Srrs
2590163953Srrs/*
2591163953Srrs * makes sure that the current endpoint local addr list is consistent with
2592163953Srrs * the new association (eg. subset bound, asconf allowed) adds addresses as
2593163953Srrs * necessary
2594163953Srrs */
2595163953Srrsstatic void
2596163953Srrssctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2597163953Srrs    int length, struct sockaddr *init_addr)
2598163953Srrs{
2599163953Srrs	struct sctp_laddr *laddr;
2600163953Srrs
2601163953Srrs	/* go through the endpoint list */
2602163953Srrs	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2603163953Srrs		/* be paranoid and validate the laddr */
2604163953Srrs		if (laddr->ifa == NULL) {
2605163953Srrs#ifdef SCTP_DEBUG
2606163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2607163953Srrs				printf("check_addr_list_ep: laddr->ifa is NULL");
2608163953Srrs			}
2609163953Srrs#endif				/* SCTP_DEBUG */
2610163953Srrs			continue;
2611163953Srrs		}
2612167598Srrs		if (laddr->ifa == NULL) {
2613163953Srrs#ifdef SCTP_DEBUG
2614163953Srrs			if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2615163953Srrs				printf("check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
2616163953Srrs			}
2617163953Srrs#endif				/* SCTP_DEBUG */
2618163953Srrs			continue;
2619163953Srrs		}
2620163953Srrs		/* do i have it implicitly? */
2621167598Srrs		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
2622163953Srrs			continue;
2623163953Srrs		}
2624163953Srrs		/* check to see if in the init-ack */
2625163953Srrs		if (!sctp_addr_in_initack(stcb, m, offset, length,
2626167598Srrs		    &laddr->ifa->address.sa)) {
2627163953Srrs			/* try to add it */
2628163953Srrs			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
2629163953Srrs			    SCTP_ADD_IP_ADDRESS);
2630163953Srrs		}
2631163953Srrs	}
2632163953Srrs}
2633163953Srrs
2634163953Srrs/*
2635163953Srrs * makes sure that the current kernel address list is consistent with the new
2636163953Srrs * association (with all addrs bound) adds addresses as necessary
2637163953Srrs */
2638163953Srrsstatic void
2639163953Srrssctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2640163953Srrs    int length, struct sockaddr *init_addr,
2641163953Srrs    uint16_t local_scope, uint16_t site_scope,
2642163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
2643163953Srrs{
2644167598Srrs	struct sctp_vrf *vrf = NULL;
2645167598Srrs	struct sctp_ifn *sctp_ifn;
2646167598Srrs	struct sctp_ifa *sctp_ifa;
2647167598Srrs	uint32_t vrf_id;
2648163953Srrs
2649168299Srrs	if (stcb) {
2650168299Srrs		vrf_id = stcb->asoc.vrf_id;
2651168299Srrs	} else {
2652168299Srrs		vrf_id = SCTP_DEFAULT_VRFID;
2653168299Srrs	}
2654167598Srrs	vrf = sctp_find_vrf(vrf_id);
2655167598Srrs	if (vrf == NULL) {
2656167598Srrs		return;
2657167598Srrs	}
2658163953Srrs	/* go through all our known interfaces */
2659167598Srrs	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2660167598Srrs		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2661163953Srrs			/* skip loopback interface */
2662163953Srrs			continue;
2663163953Srrs		}
2664163953Srrs		/* go through each interface address */
2665167598Srrs		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2666163953Srrs			/* do i have it implicitly? */
2667167598Srrs			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
2668163953Srrs				continue;
2669163953Srrs			}
2670163953Srrs			/* check to see if in the init-ack */
2671163953Srrs			if (!sctp_addr_in_initack(stcb, m, offset, length,
2672167598Srrs			    &sctp_ifa->address.sa)) {
2673163953Srrs				/* try to add it */
2674163953Srrs				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
2675167598Srrs				    sctp_ifa, SCTP_ADD_IP_ADDRESS);
2676163953Srrs			}
2677163953Srrs		}		/* end foreach ifa */
2678163953Srrs	}			/* end foreach ifn */
2679163953Srrs}
2680163953Srrs
2681163953Srrs/*
2682163953Srrs * validates an init-ack chunk (from a cookie-echo) with current addresses
2683163953Srrs * adds addresses from the init-ack into our local address list, if needed
2684163953Srrs * queues asconf adds/deletes addresses as needed and makes appropriate list
2685163953Srrs * changes for source address selection m, offset: points to the start of the
2686163953Srrs * address list in an init-ack chunk length: total length of the address
2687163953Srrs * params only init_addr: address where my INIT-ACK was sent from
2688163953Srrs */
2689163953Srrsvoid
2690163953Srrssctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2691163953Srrs    int length, struct sockaddr *init_addr,
2692163953Srrs    uint16_t local_scope, uint16_t site_scope,
2693163953Srrs    uint16_t ipv4_scope, uint16_t loopback_scope)
2694163953Srrs{
2695163953Srrs	/* process the local addresses in the initack */
2696163953Srrs	sctp_process_initack_addresses(stcb, m, offset, length);
2697163953Srrs
2698163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2699163953Srrs		/* bound all case */
2700163953Srrs		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
2701163953Srrs		    local_scope, site_scope, ipv4_scope, loopback_scope);
2702163953Srrs	} else {
2703163953Srrs		/* subset bound case */
2704163953Srrs		if (sctp_is_feature_on(stcb->sctp_ep,
2705163953Srrs		    SCTP_PCB_FLAGS_DO_ASCONF)) {
2706163953Srrs			/* asconf's allowed */
2707163953Srrs			sctp_check_address_list_ep(stcb, m, offset, length,
2708163953Srrs			    init_addr);
2709163953Srrs		}
2710163953Srrs		/* else, no asconfs allowed, so what we sent is what we get */
2711163953Srrs	}
2712163953Srrs}
2713163953Srrs
2714163953Srrs/*
2715163953Srrs * sctp_bindx() support
2716163953Srrs */
2717163953Srrsuint32_t
2718167598Srrssctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa, uint32_t type, uint32_t vrf_id)
2719163953Srrs{
2720167598Srrs	struct sctp_ifa *ifa;
2721163953Srrs
2722167598Srrs	if (sa->sa_len == 0) {
2723163953Srrs		return (EINVAL);
2724167598Srrs	}
2725167598Srrs	if (type == SCTP_ADD_IP_ADDRESS) {
2726167598Srrs		/* For an add the address MUST be on the system */
2727167598Srrs		ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
2728167598Srrs	} else if (type == SCTP_DEL_IP_ADDRESS) {
2729167598Srrs		/* For a delete we need to find it in the inp */
2730167598Srrs		ifa = sctp_find_ifa_in_ep(inp, sa, 0);
2731167598Srrs	} else {
2732167598Srrs		ifa = NULL;
2733167598Srrs	}
2734163953Srrs	if (ifa != NULL) {
2735167598Srrs		/* add this address */
2736167598Srrs		struct sctp_asconf_iterator *asc;
2737167598Srrs		struct sctp_laddr *wi;
2738163953Srrs
2739167598Srrs		SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
2740167598Srrs		    sizeof(struct sctp_asconf_iterator), "SCTP_ASCONF_ITERATOR");
2741167598Srrs		if (asc == NULL) {
2742167598Srrs			return (ENOMEM);
2743167598Srrs		}
2744167598Srrs		wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
2745167598Srrs		if (wi == NULL) {
2746167598Srrs			SCTP_FREE(asc);
2747167598Srrs			return (ENOMEM);
2748167598Srrs		}
2749167598Srrs		if (type == SCTP_ADD_IP_ADDRESS) {
2750167598Srrs			sctp_add_local_addr_ep(inp, ifa, type);
2751167598Srrs		} else if (type == SCTP_DEL_IP_ADDRESS) {
2752167598Srrs			struct sctp_laddr *laddr;
2753167598Srrs
2754167598Srrs			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2755167598Srrs				if (ifa == laddr->ifa) {
2756167598Srrs					/* Mark in the delete */
2757167598Srrs					laddr->action = type;
2758167598Srrs				}
2759163953Srrs			}
2760163953Srrs		}
2761167598Srrs		LIST_INIT(&asc->list_of_work);
2762167598Srrs		asc->cnt = 1;
2763167598Srrs		SCTP_INCR_LADDR_COUNT();
2764167598Srrs		wi->ifa = ifa;
2765167598Srrs		wi->action = type;
2766167598Srrs		atomic_add_int(&ifa->refcount, 1);
2767167598Srrs		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
2768167598Srrs		sctp_initiate_iterator(sctp_iterator_ep,
2769167598Srrs		    sctp_iterator_stcb,
2770167598Srrs		    sctp_iterator_ep_end,
2771167598Srrs		    SCTP_PCB_ANY_FLAGS,
2772167598Srrs		    SCTP_PCB_ANY_FEATURES, SCTP_ASOC_ANY_STATE, (void *)asc, 0,
2773167598Srrs		    sctp_iterator_end, inp, 0);
2774163953Srrs	} else {
2775163953Srrs		/* invalid address! */
2776163953Srrs		return (EADDRNOTAVAIL);
2777163953Srrs	}
2778163953Srrs	return (0);
2779163953Srrs}
2780