sctp_asconf.c revision 277033
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * a) Redistributions of source code must retain the above copyright notice,
10 *    this list of conditions and the following disclaimer.
11 *
12 * b) Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in
14 *    the documentation and/or other materials provided with the distribution.
15 *
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 *    contributors may be used to endorse or promote products derived
18 *    from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/netinet/sctp_asconf.c 277033 2015-01-11 22:23:39Z tuexen $");
35
36#include <netinet/sctp_os.h>
37#include <netinet/sctp_var.h>
38#include <netinet/sctp_sysctl.h>
39#include <netinet/sctp_pcb.h>
40#include <netinet/sctp_header.h>
41#include <netinet/sctputil.h>
42#include <netinet/sctp_output.h>
43#include <netinet/sctp_asconf.h>
44#include <netinet/sctp_timer.h>
45
46/*
47 * debug flags:
48 * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
49 * SCTP_DEBUG_ASCONF2: detailed info
50 */
51
52
53/*
54 * RFC 5061
55 *
56 * An ASCONF parameter queue exists per asoc which holds the pending address
57 * operations.  Lists are updated upon receipt of ASCONF-ACK.
58 *
59 * A restricted_addrs list exists per assoc to hold local addresses that are
60 * not (yet) usable by the assoc as a source address.  These addresses are
61 * either pending an ASCONF operation (and exist on the ASCONF parameter
62 * queue), or they are permanently restricted (the peer has returned an
63 * ERROR indication to an ASCONF(ADD), or the peer does not support ASCONF).
64 *
65 * Deleted addresses are always immediately removed from the lists as they will
66 * (shortly) no longer exist in the kernel.  We send ASCONFs as a courtesy,
67 * only if allowed.
68 */
69
70/*
71 * ASCONF parameter processing.
72 * response_required: set if a reply is required (eg. SUCCESS_REPORT).
73 * returns a mbuf to an "error" response parameter or NULL/"success" if ok.
74 * FIX: allocating this many mbufs on the fly is pretty inefficient...
75 */
76static struct mbuf *
77sctp_asconf_success_response(uint32_t id)
78{
79	struct mbuf *m_reply = NULL;
80	struct sctp_asconf_paramhdr *aph;
81
82	m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
83	    0, M_NOWAIT, 1, MT_DATA);
84	if (m_reply == NULL) {
85		SCTPDBG(SCTP_DEBUG_ASCONF1,
86		    "asconf_success_response: couldn't get mbuf!\n");
87		return (NULL);
88	}
89	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
90	aph->correlation_id = id;
91	aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
92	aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
93	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
94	aph->ph.param_length = htons(aph->ph.param_length);
95
96	return (m_reply);
97}
98
99static struct mbuf *
100sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t * error_tlv,
101    uint16_t tlv_length)
102{
103	struct mbuf *m_reply = NULL;
104	struct sctp_asconf_paramhdr *aph;
105	struct sctp_error_cause *error;
106	uint8_t *tlv;
107
108	m_reply = sctp_get_mbuf_for_msg((sizeof(struct sctp_asconf_paramhdr) +
109	    tlv_length +
110	    sizeof(struct sctp_error_cause)),
111	    0, M_NOWAIT, 1, MT_DATA);
112	if (m_reply == NULL) {
113		SCTPDBG(SCTP_DEBUG_ASCONF1,
114		    "asconf_error_response: couldn't get mbuf!\n");
115		return (NULL);
116	}
117	aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
118	error = (struct sctp_error_cause *)(aph + 1);
119
120	aph->correlation_id = id;
121	aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
122	error->code = htons(cause);
123	error->length = tlv_length + sizeof(struct sctp_error_cause);
124	aph->ph.param_length = error->length +
125	    sizeof(struct sctp_asconf_paramhdr);
126
127	if (aph->ph.param_length > MLEN) {
128		SCTPDBG(SCTP_DEBUG_ASCONF1,
129		    "asconf_error_response: tlv_length (%xh) too big\n",
130		    tlv_length);
131		sctp_m_freem(m_reply);	/* discard */
132		return (NULL);
133	}
134	if (error_tlv != NULL) {
135		tlv = (uint8_t *) (error + 1);
136		memcpy(tlv, error_tlv, tlv_length);
137	}
138	SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
139	error->length = htons(error->length);
140	aph->ph.param_length = htons(aph->ph.param_length);
141
142	return (m_reply);
143}
144
145static struct mbuf *
146sctp_process_asconf_add_ip(struct sockaddr *src, struct sctp_asconf_paramhdr *aph,
147    struct sctp_tcb *stcb, int send_hb, int response_required)
148{
149	struct sctp_nets *net;
150	struct mbuf *m_reply = NULL;
151	union sctp_sockstore store;
152	struct sctp_paramhdr *ph;
153	uint16_t param_type, aparam_length;
154
155#if defined(INET) || defined(INET6)
156	uint16_t param_length;
157
158#endif
159	struct sockaddr *sa;
160	int zero_address = 0;
161	int bad_address = 0;
162
163#ifdef INET
164	struct sockaddr_in *sin;
165	struct sctp_ipv4addr_param *v4addr;
166
167#endif
168#ifdef INET6
169	struct sockaddr_in6 *sin6;
170	struct sctp_ipv6addr_param *v6addr;
171
172#endif
173
174	aparam_length = ntohs(aph->ph.param_length);
175	ph = (struct sctp_paramhdr *)(aph + 1);
176	param_type = ntohs(ph->param_type);
177#if defined(INET) || defined(INET6)
178	param_length = ntohs(ph->param_length);
179#endif
180	sa = &store.sa;
181	switch (param_type) {
182#ifdef INET
183	case SCTP_IPV4_ADDRESS:
184		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
185			/* invalid param size */
186			return (NULL);
187		}
188		v4addr = (struct sctp_ipv4addr_param *)ph;
189		sin = &store.sin;
190		bzero(sin, sizeof(*sin));
191		sin->sin_family = AF_INET;
192		sin->sin_len = sizeof(struct sockaddr_in);
193		sin->sin_port = stcb->rport;
194		sin->sin_addr.s_addr = v4addr->addr;
195		if ((sin->sin_addr.s_addr == INADDR_BROADCAST) ||
196		    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
197			bad_address = 1;
198		}
199		if (sin->sin_addr.s_addr == INADDR_ANY)
200			zero_address = 1;
201		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
202		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
203		break;
204#endif
205#ifdef INET6
206	case SCTP_IPV6_ADDRESS:
207		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
208			/* invalid param size */
209			return (NULL);
210		}
211		v6addr = (struct sctp_ipv6addr_param *)ph;
212		sin6 = &store.sin6;
213		bzero(sin6, sizeof(*sin6));
214		sin6->sin6_family = AF_INET6;
215		sin6->sin6_len = sizeof(struct sockaddr_in6);
216		sin6->sin6_port = stcb->rport;
217		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
218		    sizeof(struct in6_addr));
219		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
220			bad_address = 1;
221		}
222		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
223			zero_address = 1;
224		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
225		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
226		break;
227#endif
228	default:
229		m_reply = sctp_asconf_error_response(aph->correlation_id,
230		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
231		    aparam_length);
232		return (m_reply);
233	}			/* end switch */
234
235	/* if 0.0.0.0/::0, add the source address instead */
236	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
237		sa = src;
238		SCTPDBG(SCTP_DEBUG_ASCONF1,
239		    "process_asconf_add_ip: using source addr ");
240		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
241	}
242	/* add the address */
243	if (bad_address) {
244		m_reply = sctp_asconf_error_response(aph->correlation_id,
245		    SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
246		    aparam_length);
247	} else if (sctp_add_remote_addr(stcb, sa, &net, SCTP_DONOT_SETSCOPE,
248	    SCTP_ADDR_DYNAMIC_ADDED) != 0) {
249		SCTPDBG(SCTP_DEBUG_ASCONF1,
250		    "process_asconf_add_ip: error adding address\n");
251		m_reply = sctp_asconf_error_response(aph->correlation_id,
252		    SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
253		    aparam_length);
254	} else {
255		/* notify upper layer */
256		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
257		if (response_required) {
258			m_reply =
259			    sctp_asconf_success_response(aph->correlation_id);
260		}
261		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
262		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
263		    stcb, net);
264		if (send_hb) {
265			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
266		}
267	}
268	return (m_reply);
269}
270
271static int
272sctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb, struct sockaddr *src)
273{
274	struct sctp_nets *src_net, *net;
275
276	/* make sure the source address exists as a destination net */
277	src_net = sctp_findnet(stcb, src);
278	if (src_net == NULL) {
279		/* not found */
280		return (-1);
281	}
282	/* delete all destination addresses except the source */
283	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
284		if (net != src_net) {
285			/* delete this address */
286			sctp_remove_net(stcb, net);
287			SCTPDBG(SCTP_DEBUG_ASCONF1,
288			    "asconf_del_remote_addrs_except: deleting ");
289			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
290			    (struct sockaddr *)&net->ro._l_addr);
291			/* notify upper layer */
292			sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
293			    (struct sockaddr *)&net->ro._l_addr, SCTP_SO_NOT_LOCKED);
294		}
295	}
296	return (0);
297}
298
299static struct mbuf *
300sctp_process_asconf_delete_ip(struct sockaddr *src,
301    struct sctp_asconf_paramhdr *aph,
302    struct sctp_tcb *stcb, int response_required)
303{
304	struct mbuf *m_reply = NULL;
305	union sctp_sockstore store;
306	struct sctp_paramhdr *ph;
307	uint16_t param_type, aparam_length;
308
309#if defined(INET) || defined(INET6)
310	uint16_t param_length;
311
312#endif
313	struct sockaddr *sa;
314	int zero_address = 0;
315	int result;
316
317#ifdef INET
318	struct sockaddr_in *sin;
319	struct sctp_ipv4addr_param *v4addr;
320
321#endif
322#ifdef INET6
323	struct sockaddr_in6 *sin6;
324	struct sctp_ipv6addr_param *v6addr;
325
326#endif
327
328	aparam_length = ntohs(aph->ph.param_length);
329	ph = (struct sctp_paramhdr *)(aph + 1);
330	param_type = ntohs(ph->param_type);
331#if defined(INET) || defined(INET6)
332	param_length = ntohs(ph->param_length);
333#endif
334	sa = &store.sa;
335	switch (param_type) {
336#ifdef INET
337	case SCTP_IPV4_ADDRESS:
338		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
339			/* invalid param size */
340			return (NULL);
341		}
342		v4addr = (struct sctp_ipv4addr_param *)ph;
343		sin = &store.sin;
344		bzero(sin, sizeof(*sin));
345		sin->sin_family = AF_INET;
346		sin->sin_len = sizeof(struct sockaddr_in);
347		sin->sin_port = stcb->rport;
348		sin->sin_addr.s_addr = v4addr->addr;
349		if (sin->sin_addr.s_addr == INADDR_ANY)
350			zero_address = 1;
351		SCTPDBG(SCTP_DEBUG_ASCONF1,
352		    "process_asconf_delete_ip: deleting ");
353		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
354		break;
355#endif
356#ifdef INET6
357	case SCTP_IPV6_ADDRESS:
358		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
359			/* invalid param size */
360			return (NULL);
361		}
362		v6addr = (struct sctp_ipv6addr_param *)ph;
363		sin6 = &store.sin6;
364		bzero(sin6, sizeof(*sin6));
365		sin6->sin6_family = AF_INET6;
366		sin6->sin6_len = sizeof(struct sockaddr_in6);
367		sin6->sin6_port = stcb->rport;
368		memcpy(&sin6->sin6_addr, v6addr->addr,
369		    sizeof(struct in6_addr));
370		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
371			zero_address = 1;
372		SCTPDBG(SCTP_DEBUG_ASCONF1,
373		    "process_asconf_delete_ip: deleting ");
374		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
375		break;
376#endif
377	default:
378		m_reply = sctp_asconf_error_response(aph->correlation_id,
379		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
380		    aparam_length);
381		return (m_reply);
382	}
383
384	/* make sure the source address is not being deleted */
385	if (sctp_cmpaddr(sa, src)) {
386		/* trying to delete the source address! */
387		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
388		m_reply = sctp_asconf_error_response(aph->correlation_id,
389		    SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
390		    aparam_length);
391		return (m_reply);
392	}
393	/* if deleting 0.0.0.0/::0, delete all addresses except src addr */
394	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
395		result = sctp_asconf_del_remote_addrs_except(stcb, src);
396
397		if (result) {
398			/* src address did not exist? */
399			SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
400			/* what error to reply with?? */
401			m_reply =
402			    sctp_asconf_error_response(aph->correlation_id,
403			    SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
404			    aparam_length);
405		} else if (response_required) {
406			m_reply =
407			    sctp_asconf_success_response(aph->correlation_id);
408		}
409		return (m_reply);
410	}
411	/* delete the address */
412	result = sctp_del_remote_addr(stcb, sa);
413	/*
414	 * note if result == -2, the address doesn't exist in the asoc but
415	 * since it's being deleted anyways, we just ack the delete -- but
416	 * this probably means something has already gone awry
417	 */
418	if (result == -1) {
419		/* only one address in the asoc */
420		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
421		m_reply = sctp_asconf_error_response(aph->correlation_id,
422		    SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
423		    aparam_length);
424	} else {
425		if (response_required) {
426			m_reply = sctp_asconf_success_response(aph->correlation_id);
427		}
428		/* notify upper layer */
429		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
430	}
431	return (m_reply);
432}
433
434static struct mbuf *
435sctp_process_asconf_set_primary(struct sockaddr *src,
436    struct sctp_asconf_paramhdr *aph,
437    struct sctp_tcb *stcb, int response_required)
438{
439	struct mbuf *m_reply = NULL;
440	union sctp_sockstore store;
441	struct sctp_paramhdr *ph;
442	uint16_t param_type, aparam_length;
443
444#if defined(INET) || defined(INET6)
445	uint16_t param_length;
446
447#endif
448	struct sockaddr *sa;
449	int zero_address = 0;
450
451#ifdef INET
452	struct sockaddr_in *sin;
453	struct sctp_ipv4addr_param *v4addr;
454
455#endif
456#ifdef INET6
457	struct sockaddr_in6 *sin6;
458	struct sctp_ipv6addr_param *v6addr;
459
460#endif
461
462	aparam_length = ntohs(aph->ph.param_length);
463	ph = (struct sctp_paramhdr *)(aph + 1);
464	param_type = ntohs(ph->param_type);
465#if defined(INET) || defined(INET6)
466	param_length = ntohs(ph->param_length);
467#endif
468	sa = &store.sa;
469	switch (param_type) {
470#ifdef INET
471	case SCTP_IPV4_ADDRESS:
472		if (param_length != sizeof(struct sctp_ipv4addr_param)) {
473			/* invalid param size */
474			return (NULL);
475		}
476		v4addr = (struct sctp_ipv4addr_param *)ph;
477		sin = &store.sin;
478		bzero(sin, sizeof(*sin));
479		sin->sin_family = AF_INET;
480		sin->sin_len = sizeof(struct sockaddr_in);
481		sin->sin_addr.s_addr = v4addr->addr;
482		if (sin->sin_addr.s_addr == INADDR_ANY)
483			zero_address = 1;
484		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
485		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
486		break;
487#endif
488#ifdef INET6
489	case SCTP_IPV6_ADDRESS:
490		if (param_length != sizeof(struct sctp_ipv6addr_param)) {
491			/* invalid param size */
492			return (NULL);
493		}
494		v6addr = (struct sctp_ipv6addr_param *)ph;
495		sin6 = &store.sin6;
496		bzero(sin6, sizeof(*sin6));
497		sin6->sin6_family = AF_INET6;
498		sin6->sin6_len = sizeof(struct sockaddr_in6);
499		memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
500		    sizeof(struct in6_addr));
501		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
502			zero_address = 1;
503		SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
504		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
505		break;
506#endif
507	default:
508		m_reply = sctp_asconf_error_response(aph->correlation_id,
509		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
510		    aparam_length);
511		return (m_reply);
512	}
513
514	/* if 0.0.0.0/::0, use the source address instead */
515	if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
516		sa = src;
517		SCTPDBG(SCTP_DEBUG_ASCONF1,
518		    "process_asconf_set_primary: using source addr ");
519		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
520	}
521	/* set the primary address */
522	if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
523		SCTPDBG(SCTP_DEBUG_ASCONF1,
524		    "process_asconf_set_primary: primary address set\n");
525		/* notify upper layer */
526		sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
527		if ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_REACHABLE) &&
528		    (!(stcb->asoc.primary_destination->dest_state & SCTP_ADDR_PF)) &&
529		    (stcb->asoc.alternate)) {
530			sctp_free_remote_addr(stcb->asoc.alternate);
531			stcb->asoc.alternate = NULL;
532		}
533		if (response_required) {
534			m_reply = sctp_asconf_success_response(aph->correlation_id);
535		}
536		/*
537		 * Mobility adaptation. Ideally, when the reception of SET
538		 * PRIMARY with DELETE IP ADDRESS of the previous primary
539		 * destination, unacknowledged DATA are retransmitted
540		 * immediately to the new primary destination for seamless
541		 * handover. If the destination is UNCONFIRMED and marked to
542		 * REQ_PRIM, The retransmission occur when reception of the
543		 * HEARTBEAT-ACK.  (See sctp_handle_heartbeat_ack in
544		 * sctp_input.c) Also, when change of the primary
545		 * destination, it is better that all subsequent new DATA
546		 * containing already queued DATA are transmitted to the new
547		 * primary destination. (by micchie)
548		 */
549		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
550		    SCTP_MOBILITY_BASE) ||
551		    sctp_is_mobility_feature_on(stcb->sctp_ep,
552		    SCTP_MOBILITY_FASTHANDOFF)) &&
553		    sctp_is_mobility_feature_on(stcb->sctp_ep,
554		    SCTP_MOBILITY_PRIM_DELETED) &&
555		    (stcb->asoc.primary_destination->dest_state &
556		    SCTP_ADDR_UNCONFIRMED) == 0) {
557
558			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_TIMER + SCTP_LOC_7);
559			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
560			    SCTP_MOBILITY_FASTHANDOFF)) {
561				sctp_assoc_immediate_retrans(stcb,
562				    stcb->asoc.primary_destination);
563			}
564			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
565			    SCTP_MOBILITY_BASE)) {
566				sctp_move_chunks_from_net(stcb,
567				    stcb->asoc.deleted_primary);
568			}
569			sctp_delete_prim_timer(stcb->sctp_ep, stcb,
570			    stcb->asoc.deleted_primary);
571		}
572	} else {
573		/* couldn't set the requested primary address! */
574		SCTPDBG(SCTP_DEBUG_ASCONF1,
575		    "process_asconf_set_primary: set primary failed!\n");
576		/* must have been an invalid address, so report */
577		m_reply = sctp_asconf_error_response(aph->correlation_id,
578		    SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
579		    aparam_length);
580	}
581
582	return (m_reply);
583}
584
585/*
586 * handles an ASCONF chunk.
587 * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
588 */
589void
590sctp_handle_asconf(struct mbuf *m, unsigned int offset,
591    struct sockaddr *src,
592    struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb,
593    int first)
594{
595	struct sctp_association *asoc;
596	uint32_t serial_num;
597	struct mbuf *n, *m_ack, *m_result, *m_tail;
598	struct sctp_asconf_ack_chunk *ack_cp;
599	struct sctp_asconf_paramhdr *aph, *ack_aph;
600	struct sctp_ipv6addr_param *p_addr;
601	unsigned int asconf_limit, cnt;
602	int error = 0;		/* did an error occur? */
603
604	/* asconf param buffer */
605	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
606	struct sctp_asconf_ack *ack, *ack_next;
607
608	/* verify minimum length */
609	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
610		SCTPDBG(SCTP_DEBUG_ASCONF1,
611		    "handle_asconf: chunk too small = %xh\n",
612		    ntohs(cp->ch.chunk_length));
613		return;
614	}
615	asoc = &stcb->asoc;
616	serial_num = ntohl(cp->serial_number);
617
618	if (SCTP_TSN_GE(asoc->asconf_seq_in, serial_num)) {
619		/* got a duplicate ASCONF */
620		SCTPDBG(SCTP_DEBUG_ASCONF1,
621		    "handle_asconf: got duplicate serial number = %xh\n",
622		    serial_num);
623		return;
624	} else if (serial_num != (asoc->asconf_seq_in + 1)) {
625		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
626		    serial_num, asoc->asconf_seq_in + 1);
627		return;
628	}
629	/* it's the expected "next" sequence number, so process it */
630	asoc->asconf_seq_in = serial_num;	/* update sequence */
631	/* get length of all the param's in the ASCONF */
632	asconf_limit = offset + ntohs(cp->ch.chunk_length);
633	SCTPDBG(SCTP_DEBUG_ASCONF1,
634	    "handle_asconf: asconf_limit=%u, sequence=%xh\n",
635	    asconf_limit, serial_num);
636
637	if (first) {
638		/* delete old cache */
639		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: Now processing first ASCONF. Try to delete old cache\n");
640
641		TAILQ_FOREACH_SAFE(ack, &asoc->asconf_ack_sent, next, ack_next) {
642			if (ack->serial_number == serial_num)
643				break;
644			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: delete old(%u) < first(%u)\n",
645			    ack->serial_number, serial_num);
646			TAILQ_REMOVE(&asoc->asconf_ack_sent, ack, next);
647			if (ack->data != NULL) {
648				sctp_m_freem(ack->data);
649			}
650			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), ack);
651		}
652	}
653	m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
654	    M_NOWAIT, 1, MT_DATA);
655	if (m_ack == NULL) {
656		SCTPDBG(SCTP_DEBUG_ASCONF1,
657		    "handle_asconf: couldn't get mbuf!\n");
658		return;
659	}
660	m_tail = m_ack;		/* current reply chain's tail */
661
662	/* fill in ASCONF-ACK header */
663	ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
664	ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
665	ack_cp->ch.chunk_flags = 0;
666	ack_cp->serial_number = htonl(serial_num);
667	/* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
668	SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
669	ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
670
671	/* skip the lookup address parameter */
672	offset += sizeof(struct sctp_asconf_chunk);
673	p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *) & aparam_buf);
674	if (p_addr == NULL) {
675		SCTPDBG(SCTP_DEBUG_ASCONF1,
676		    "handle_asconf: couldn't get lookup addr!\n");
677		/* respond with a missing/invalid mandatory parameter error */
678		return;
679	}
680	/* param_length is already validated in process_control... */
681	offset += ntohs(p_addr->ph.param_length);	/* skip lookup addr */
682
683	/* get pointer to first asconf param in ASCONF-ACK */
684	ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, caddr_t)+sizeof(struct sctp_asconf_ack_chunk));
685	if (ack_aph == NULL) {
686		SCTPDBG(SCTP_DEBUG_ASCONF1, "Gak in asconf2\n");
687		return;
688	}
689	/* get pointer to first asconf param in ASCONF */
690	aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *) & aparam_buf);
691	if (aph == NULL) {
692		SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
693		goto send_reply;
694	}
695	/* process through all parameters */
696	cnt = 0;
697	while (aph != NULL) {
698		unsigned int param_length, param_type;
699
700		param_type = ntohs(aph->ph.param_type);
701		param_length = ntohs(aph->ph.param_length);
702		if (offset + param_length > asconf_limit) {
703			/* parameter goes beyond end of chunk! */
704			sctp_m_freem(m_ack);
705			return;
706		}
707		m_result = NULL;
708
709		if (param_length > sizeof(aparam_buf)) {
710			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
711			sctp_m_freem(m_ack);
712			return;
713		}
714		if (param_length <= sizeof(struct sctp_paramhdr)) {
715			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
716			sctp_m_freem(m_ack);
717		}
718		/* get the entire parameter */
719		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
720		if (aph == NULL) {
721			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
722			sctp_m_freem(m_ack);
723			return;
724		}
725		switch (param_type) {
726		case SCTP_ADD_IP_ADDRESS:
727			m_result = sctp_process_asconf_add_ip(src, aph, stcb,
728			    (cnt < SCTP_BASE_SYSCTL(sctp_hb_maxburst)), error);
729			cnt++;
730			break;
731		case SCTP_DEL_IP_ADDRESS:
732			m_result = sctp_process_asconf_delete_ip(src, aph, stcb,
733			    error);
734			break;
735		case SCTP_ERROR_CAUSE_IND:
736			/* not valid in an ASCONF chunk */
737			break;
738		case SCTP_SET_PRIM_ADDR:
739			m_result = sctp_process_asconf_set_primary(src, aph,
740			    stcb, error);
741			break;
742		case SCTP_NAT_VTAGS:
743			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: sees a NAT VTAG state parameter\n");
744			break;
745		case SCTP_SUCCESS_REPORT:
746			/* not valid in an ASCONF chunk */
747			break;
748		case SCTP_ULP_ADAPTATION:
749			/* FIX */
750			break;
751		default:
752			if ((param_type & 0x8000) == 0) {
753				/* Been told to STOP at this param */
754				asconf_limit = offset;
755				/*
756				 * FIX FIX - We need to call
757				 * sctp_arethere_unrecognized_parameters()
758				 * to get a operr and send it for any
759				 * param's with the 0x4000 bit set OR do it
760				 * here ourselves... note we still must STOP
761				 * if the 0x8000 bit is clear.
762				 */
763			}
764			/* unknown/invalid param type */
765			break;
766		}		/* switch */
767
768		/* add any (error) result to the reply mbuf chain */
769		if (m_result != NULL) {
770			SCTP_BUF_NEXT(m_tail) = m_result;
771			m_tail = m_result;
772			/* update lengths, make sure it's aligned too */
773			SCTP_BUF_LEN(m_result) = SCTP_SIZE32(SCTP_BUF_LEN(m_result));
774			ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
775			/* set flag to force success reports */
776			error = 1;
777		}
778		offset += SCTP_SIZE32(param_length);
779		/* update remaining ASCONF message length to process */
780		if (offset >= asconf_limit) {
781			/* no more data in the mbuf chain */
782			break;
783		}
784		/* get pointer to next asconf param */
785		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
786		    sizeof(struct sctp_asconf_paramhdr),
787		    (uint8_t *) & aparam_buf);
788		if (aph == NULL) {
789			/* can't get an asconf paramhdr */
790			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
791			/* FIX ME - add error here... */
792		}
793	}
794
795send_reply:
796	ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
797	/* save the ASCONF-ACK reply */
798	ack = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asconf_ack),
799	    struct sctp_asconf_ack);
800	if (ack == NULL) {
801		sctp_m_freem(m_ack);
802		return;
803	}
804	ack->serial_number = serial_num;
805	ack->last_sent_to = NULL;
806	ack->data = m_ack;
807	ack->len = 0;
808	for (n = m_ack; n != NULL; n = SCTP_BUF_NEXT(n)) {
809		ack->len += SCTP_BUF_LEN(n);
810	}
811	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_ack_sent, ack, next);
812
813	/* see if last_control_chunk_from is set properly (use IP src addr) */
814	if (stcb->asoc.last_control_chunk_from == NULL) {
815		/*
816		 * this could happen if the source address was just newly
817		 * added
818		 */
819		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
820		SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
821		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
822		/* look up the from address */
823		stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, src);
824#ifdef SCTP_DEBUG
825		if (stcb->asoc.last_control_chunk_from == NULL) {
826			SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
827		}
828#endif
829	}
830}
831
832/*
833 * does the address match? returns 0 if not, 1 if so
834 */
835static uint32_t
836sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
837{
838	switch (sa->sa_family) {
839#ifdef INET6
840	case AF_INET6:
841		{
842			/* XXX scopeid */
843			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
844
845			if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
846			    (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
847			    sizeof(struct in6_addr)) == 0)) {
848				return (1);
849			}
850			break;
851		}
852#endif
853#ifdef INET
854	case AF_INET:
855		{
856			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
857
858			if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
859			    (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
860			    sizeof(struct in_addr)) == 0)) {
861				return (1);
862			}
863			break;
864		}
865#endif
866	default:
867		break;
868	}
869	return (0);
870}
871
872/*
873 * does the address match? returns 0 if not, 1 if so
874 */
875static uint32_t
876sctp_addr_match(struct sctp_paramhdr *ph, struct sockaddr *sa)
877{
878#if defined(INET) || defined(INET6)
879	uint16_t param_type, param_length;
880
881	param_type = ntohs(ph->param_type);
882	param_length = ntohs(ph->param_length);
883#endif
884	switch (sa->sa_family) {
885#ifdef INET6
886	case AF_INET6:
887		{
888			/* XXX scopeid */
889			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
890			struct sctp_ipv6addr_param *v6addr;
891
892			v6addr = (struct sctp_ipv6addr_param *)ph;
893			if ((param_type == SCTP_IPV6_ADDRESS) &&
894			    (param_length == sizeof(struct sctp_ipv6addr_param)) &&
895			    (memcmp(&v6addr->addr, &sin6->sin6_addr,
896			    sizeof(struct in6_addr)) == 0)) {
897				return (1);
898			}
899			break;
900		}
901#endif
902#ifdef INET
903	case AF_INET:
904		{
905			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
906			struct sctp_ipv4addr_param *v4addr;
907
908			v4addr = (struct sctp_ipv4addr_param *)ph;
909			if ((param_type == SCTP_IPV4_ADDRESS) &&
910			    (param_length == sizeof(struct sctp_ipv4addr_param)) &&
911			    (memcmp(&v4addr->addr, &sin->sin_addr,
912			    sizeof(struct in_addr)) == 0)) {
913				return (1);
914			}
915			break;
916		}
917#endif
918	default:
919		break;
920	}
921	return (0);
922}
923
924/*
925 * Cleanup for non-responded/OP ERR'd ASCONF
926 */
927void
928sctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
929{
930	/*
931	 * clear out any existing asconfs going out
932	 */
933	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
934	    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
935	stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
936	/* remove the old ASCONF on our outbound queue */
937	sctp_toss_old_asconf(stcb);
938}
939
940/*
941 * cleanup any cached source addresses that may be topologically
942 * incorrect after a new address has been added to this interface.
943 */
944static void
945sctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
946{
947	struct sctp_nets *net;
948
949	/*
950	 * Ideally, we want to only clear cached routes and source addresses
951	 * that are topologically incorrect.  But since there is no easy way
952	 * to know whether the newly added address on the ifn would cause a
953	 * routing change (i.e. a new egress interface would be chosen)
954	 * without doing a new routing lookup and source address selection,
955	 * we will (for now) just flush any cached route using a different
956	 * ifn (and cached source addrs) and let output re-choose them
957	 * during the next send on that net.
958	 */
959	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
960		/*
961		 * clear any cached route (and cached source address) if the
962		 * route's interface is NOT the same as the address change.
963		 * If it's the same interface, just clear the cached source
964		 * address.
965		 */
966		if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
967		    ((ifn == NULL) ||
968		    (SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index))) {
969			/* clear any cached route */
970			RTFREE(net->ro.ro_rt);
971			net->ro.ro_rt = NULL;
972		}
973		/* clear any cached source address */
974		if (net->src_addr_selected) {
975			sctp_free_ifa(net->ro._s_addr);
976			net->ro._s_addr = NULL;
977			net->src_addr_selected = 0;
978		}
979	}
980}
981
982
983void
984sctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
985{
986	int error;
987
988	if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
989		return;
990	}
991	if (stcb->asoc.deleted_primary == NULL) {
992		return;
993	}
994	if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
995		SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
996		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
997		SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
998		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
999		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
1000		    stcb->asoc.deleted_primary,
1001		    SCTP_FROM_SCTP_TIMER + SCTP_LOC_8);
1002		stcb->asoc.num_send_timers_up--;
1003		if (stcb->asoc.num_send_timers_up < 0) {
1004			stcb->asoc.num_send_timers_up = 0;
1005		}
1006		SCTP_TCB_LOCK_ASSERT(stcb);
1007		error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
1008		    stcb->asoc.deleted_primary);
1009		if (error) {
1010			SCTP_INP_DECR_REF(stcb->sctp_ep);
1011			return;
1012		}
1013		SCTP_TCB_LOCK_ASSERT(stcb);
1014#ifdef SCTP_AUDITING_ENABLED
1015		sctp_auditing(4, stcb->sctp_ep, stcb, stcb->asoc.deleted_primary);
1016#endif
1017		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1018		if ((stcb->asoc.num_send_timers_up == 0) &&
1019		    (stcb->asoc.sent_queue_cnt > 0)) {
1020			struct sctp_tmit_chunk *chk;
1021
1022			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1023			sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
1024			    stcb, chk->whoTo);
1025		}
1026	}
1027	return;
1028}
1029
1030static int
1031    sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
1032
1033void
1034sctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
1035{
1036	struct sctp_tmit_chunk *chk;
1037
1038	SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
1039	sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
1040	    SCTP_FROM_SCTP_TIMER + SCTP_LOC_5);
1041	stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
1042	net->error_count = 0;
1043	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1044		if (chk->whoTo == net) {
1045			if (chk->sent < SCTP_DATAGRAM_RESEND) {
1046				chk->sent = SCTP_DATAGRAM_RESEND;
1047				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1048				sctp_flight_size_decrease(chk);
1049				sctp_total_flight_decrease(stcb, chk);
1050				net->marked_retrans++;
1051				stcb->asoc.marked_retrans++;
1052			}
1053		}
1054	}
1055	if (net->marked_retrans) {
1056		sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1057	}
1058}
1059
1060static void
1061sctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
1062{
1063	struct sctp_nets *net;
1064	int addrnum, changed;
1065
1066	/*
1067	 * If number of local valid addresses is 1, the valid address is
1068	 * probably newly added address. Several valid addresses in this
1069	 * association.  A source address may not be changed.  Additionally,
1070	 * they can be configured on a same interface as "alias" addresses.
1071	 * (by micchie)
1072	 */
1073	addrnum = sctp_local_addr_count(stcb);
1074	SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
1075	    addrnum);
1076	if (addrnum == 1) {
1077		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1078			/* clear any cached route and source address */
1079			if (net->ro.ro_rt) {
1080				RTFREE(net->ro.ro_rt);
1081				net->ro.ro_rt = NULL;
1082			}
1083			if (net->src_addr_selected) {
1084				sctp_free_ifa(net->ro._s_addr);
1085				net->ro._s_addr = NULL;
1086				net->src_addr_selected = 0;
1087			}
1088			/* Retransmit unacknowledged DATA chunks immediately */
1089			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1090			    SCTP_MOBILITY_FASTHANDOFF)) {
1091				sctp_net_immediate_retrans(stcb, net);
1092			}
1093			/* also, SET PRIMARY is maybe already sent */
1094		}
1095		return;
1096	}
1097	/* Multiple local addresses exsist in the association.  */
1098	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1099		/* clear any cached route and source address */
1100		if (net->ro.ro_rt) {
1101			RTFREE(net->ro.ro_rt);
1102			net->ro.ro_rt = NULL;
1103		}
1104		if (net->src_addr_selected) {
1105			sctp_free_ifa(net->ro._s_addr);
1106			net->ro._s_addr = NULL;
1107			net->src_addr_selected = 0;
1108		}
1109		/*
1110		 * Check if the nexthop is corresponding to the new address.
1111		 * If the new address is corresponding to the current
1112		 * nexthop, the path will be changed. If the new address is
1113		 * NOT corresponding to the current nexthop, the path will
1114		 * not be changed.
1115		 */
1116		SCTP_RTALLOC((sctp_route_t *) & net->ro,
1117		    stcb->sctp_ep->def_vrf_id);
1118		if (net->ro.ro_rt == NULL)
1119			continue;
1120
1121		changed = 0;
1122		switch (net->ro._l_addr.sa.sa_family) {
1123#ifdef INET
1124		case AF_INET:
1125			if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *) & net->ro)) {
1126				changed = 1;
1127			}
1128			break;
1129#endif
1130#ifdef INET6
1131		case AF_INET6:
1132			if (sctp_v6src_match_nexthop(
1133			    &newifa->address.sin6, (sctp_route_t *) & net->ro)) {
1134				changed = 1;
1135			}
1136			break;
1137#endif
1138		default:
1139			break;
1140		}
1141		/*
1142		 * if the newly added address does not relate routing
1143		 * information, we skip.
1144		 */
1145		if (changed == 0)
1146			continue;
1147		/* Retransmit unacknowledged DATA chunks immediately */
1148		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1149		    SCTP_MOBILITY_FASTHANDOFF)) {
1150			sctp_net_immediate_retrans(stcb, net);
1151		}
1152		/* Send SET PRIMARY for this new address */
1153		if (net == stcb->asoc.primary_destination) {
1154			(void)sctp_asconf_queue_mgmt(stcb, newifa,
1155			    SCTP_SET_PRIM_ADDR);
1156		}
1157	}
1158}
1159
1160/*
1161 * process an ADD/DELETE IP ack from peer.
1162 * addr: corresponding sctp_ifa to the address being added/deleted.
1163 * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
1164 * flag: 1=success, 0=failure.
1165 */
1166static void
1167sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr, uint32_t flag)
1168{
1169	/*
1170	 * do the necessary asoc list work- if we get a failure indication,
1171	 * leave the address on the assoc's restricted list.  If we get a
1172	 * success indication, remove the address from the restricted list.
1173	 */
1174	/*
1175	 * Note: this will only occur for ADD_IP_ADDRESS, since
1176	 * DEL_IP_ADDRESS is never actually added to the list...
1177	 */
1178	if (flag) {
1179		/* success case, so remove from the restricted list */
1180		sctp_del_local_addr_restricted(stcb, addr);
1181
1182		if (sctp_is_mobility_feature_on(stcb->sctp_ep,
1183		    SCTP_MOBILITY_BASE) ||
1184		    sctp_is_mobility_feature_on(stcb->sctp_ep,
1185		    SCTP_MOBILITY_FASTHANDOFF)) {
1186			sctp_path_check_and_react(stcb, addr);
1187			return;
1188		}
1189		/* clear any cached/topologically incorrect source addresses */
1190		sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
1191	}
1192	/* else, leave it on the list */
1193}
1194
1195/*
1196 * add an asconf add/delete/set primary IP address parameter to the queue.
1197 * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1198 * returns 0 if queued, -1 if not queued/removed.
1199 * NOTE: if adding, but a delete for the same address is already scheduled
1200 * (and not yet sent out), simply remove it from queue.  Same for deleting
1201 * an address already scheduled for add.  If a duplicate operation is found,
1202 * ignore the new one.
1203 */
1204static int
1205sctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1206    uint16_t type)
1207{
1208	struct sctp_asconf_addr *aa, *aa_next;
1209
1210	/* make sure the request isn't already in the queue */
1211	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1212		/* address match? */
1213		if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
1214			continue;
1215		/*
1216		 * is the request already in queue but not sent? pass the
1217		 * request already sent in order to resolve the following
1218		 * case: 1. arrival of ADD, then sent 2. arrival of DEL. we
1219		 * can't remove the ADD request already sent 3. arrival of
1220		 * ADD
1221		 */
1222		if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
1223			return (-1);
1224		}
1225		/* is the negative request already in queue, and not sent */
1226		if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
1227		    (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
1228			/* add requested, delete already queued */
1229			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1230			/* remove the ifa from the restricted list */
1231			sctp_del_local_addr_restricted(stcb, ifa);
1232			/* free the asconf param */
1233			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1234			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
1235			return (-1);
1236		}
1237		if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
1238		    (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
1239			/* delete requested, add already queued */
1240			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1241			/* remove the aa->ifa from the restricted list */
1242			sctp_del_local_addr_restricted(stcb, aa->ifa);
1243			/* free the asconf param */
1244			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1245			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
1246			return (-1);
1247		}
1248	}			/* for each aa */
1249
1250	/* adding new request to the queue */
1251	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1252	    SCTP_M_ASC_ADDR);
1253	if (aa == NULL) {
1254		/* didn't get memory */
1255		SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
1256		return (-1);
1257	}
1258	aa->special_del = 0;
1259	/* fill in asconf address parameter fields */
1260	/* top level elements are "networked" during send */
1261	aa->ap.aph.ph.param_type = type;
1262	aa->ifa = ifa;
1263	atomic_add_int(&ifa->refcount, 1);
1264	/* correlation_id filled in during send routine later... */
1265	switch (ifa->address.sa.sa_family) {
1266#ifdef INET6
1267	case AF_INET6:
1268		{
1269			struct sockaddr_in6 *sin6;
1270
1271			sin6 = &ifa->address.sin6;
1272			aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1273			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1274			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1275			    sizeof(struct sctp_ipv6addr_param);
1276			memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1277			    sizeof(struct in6_addr));
1278			break;
1279		}
1280#endif
1281#ifdef INET
1282	case AF_INET:
1283		{
1284			struct sockaddr_in *sin;
1285
1286			sin = &ifa->address.sin;
1287			aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1288			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1289			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
1290			    sizeof(struct sctp_ipv4addr_param);
1291			memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1292			    sizeof(struct in_addr));
1293			break;
1294		}
1295#endif
1296	default:
1297		/* invalid family! */
1298		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1299		sctp_free_ifa(ifa);
1300		return (-1);
1301	}
1302	aa->sent = 0;		/* clear sent flag */
1303
1304	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1305#ifdef SCTP_DEBUG
1306	if (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_ASCONF2) {
1307		if (type == SCTP_ADD_IP_ADDRESS) {
1308			SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
1309			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1310		} else if (type == SCTP_DEL_IP_ADDRESS) {
1311			SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
1312			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1313		} else {
1314			SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
1315			SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
1316		}
1317	}
1318#endif
1319
1320	return (0);
1321}
1322
1323
1324/*
1325 * add an asconf operation for the given ifa and type.
1326 * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
1327 * returns 0 if completed, -1 if not completed, 1 if immediate send is
1328 * advisable.
1329 */
1330static int
1331sctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
1332    uint16_t type)
1333{
1334	uint32_t status;
1335	int pending_delete_queued = 0;
1336
1337	/* see if peer supports ASCONF */
1338	if (stcb->asoc.asconf_supported == 0) {
1339		return (-1);
1340	}
1341	/*
1342	 * if this is deleting the last address from the assoc, mark it as
1343	 * pending.
1344	 */
1345	if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending &&
1346	    (sctp_local_addr_count(stcb) < 2)) {
1347		/* set the pending delete info only */
1348		stcb->asoc.asconf_del_pending = 1;
1349		stcb->asoc.asconf_addr_del_pending = ifa;
1350		atomic_add_int(&ifa->refcount, 1);
1351		SCTPDBG(SCTP_DEBUG_ASCONF2,
1352		    "asconf_queue_add: mark delete last address pending\n");
1353		return (-1);
1354	}
1355	/* queue an asconf parameter */
1356	status = sctp_asconf_queue_mgmt(stcb, ifa, type);
1357
1358	/*
1359	 * if this is an add, and there is a delete also pending (i.e. the
1360	 * last local address is being changed), queue the pending delete
1361	 * too.
1362	 */
1363	if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
1364		/* queue in the pending delete */
1365		if (sctp_asconf_queue_mgmt(stcb,
1366		    stcb->asoc.asconf_addr_del_pending,
1367		    SCTP_DEL_IP_ADDRESS) == 0) {
1368			SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queing pending delete\n");
1369			pending_delete_queued = 1;
1370			/* clear out the pending delete info */
1371			stcb->asoc.asconf_del_pending = 0;
1372			sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
1373			stcb->asoc.asconf_addr_del_pending = NULL;
1374		}
1375	}
1376	if (pending_delete_queued) {
1377		struct sctp_nets *net;
1378
1379		/*
1380		 * since we know that the only/last address is now being
1381		 * changed in this case, reset the cwnd/rto on all nets to
1382		 * start as a new address and path.  Also clear the error
1383		 * counts to give the assoc the best chance to complete the
1384		 * address change.
1385		 */
1386		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1387			stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
1388			    net);
1389			net->RTO = 0;
1390			net->error_count = 0;
1391		}
1392		stcb->asoc.overall_error_count = 0;
1393		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1394			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1395			    stcb->asoc.overall_error_count,
1396			    0,
1397			    SCTP_FROM_SCTP_ASCONF,
1398			    __LINE__);
1399		}
1400		/* queue in an advisory set primary too */
1401		(void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
1402		/* let caller know we should send this out immediately */
1403		status = 1;
1404	}
1405	return (status);
1406}
1407
1408/*-
1409 * add an asconf delete IP address parameter to the queue by sockaddr and
1410 * possibly with no sctp_ifa available.  This is only called by the routine
1411 * that checks the addresses in an INIT-ACK against the current address list.
1412 * returns 0 if completed, non-zero if not completed.
1413 * NOTE: if an add is already scheduled (and not yet sent out), simply
1414 * remove it from queue.  If a duplicate operation is found, ignore the
1415 * new one.
1416 */
1417static int
1418sctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
1419{
1420	struct sctp_ifa *ifa;
1421	struct sctp_asconf_addr *aa, *aa_next;
1422
1423	if (stcb == NULL) {
1424		return (-1);
1425	}
1426	/* see if peer supports ASCONF */
1427	if (stcb->asoc.asconf_supported == 0) {
1428		return (-1);
1429	}
1430	/* make sure the request isn't already in the queue */
1431	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1432		/* address match? */
1433		if (sctp_asconf_addr_match(aa, sa) == 0)
1434			continue;
1435		/* is the request already in queue (sent or not) */
1436		if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1437			return (-1);
1438		}
1439		/* is the negative request already in queue, and not sent */
1440		if (aa->sent == 1)
1441			continue;
1442		if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1443			/* add already queued, so remove existing entry */
1444			TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1445			sctp_del_local_addr_restricted(stcb, aa->ifa);
1446			/* free the entry */
1447			SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1448			return (-1);
1449		}
1450	}			/* for each aa */
1451
1452	/* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
1453	ifa = sctp_find_ifa_by_addr(sa, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
1454
1455	/* adding new request to the queue */
1456	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
1457	    SCTP_M_ASC_ADDR);
1458	if (aa == NULL) {
1459		/* didn't get memory */
1460		SCTPDBG(SCTP_DEBUG_ASCONF1,
1461		    "sctp_asconf_queue_sa_delete: failed to get memory!\n");
1462		return (-1);
1463	}
1464	aa->special_del = 0;
1465	/* fill in asconf address parameter fields */
1466	/* top level elements are "networked" during send */
1467	aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
1468	aa->ifa = ifa;
1469	if (ifa)
1470		atomic_add_int(&ifa->refcount, 1);
1471	/* correlation_id filled in during send routine later... */
1472	switch (sa->sa_family) {
1473#ifdef INET6
1474	case AF_INET6:
1475		{
1476			/* IPv6 address */
1477			struct sockaddr_in6 *sin6;
1478
1479			sin6 = (struct sockaddr_in6 *)sa;
1480			aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1481			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1482			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1483			memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1484			    sizeof(struct in6_addr));
1485			break;
1486		}
1487#endif
1488#ifdef INET
1489	case AF_INET:
1490		{
1491			/* IPv4 address */
1492			struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1493
1494			aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1495			aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1496			aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1497			memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1498			    sizeof(struct in_addr));
1499			break;
1500		}
1501#endif
1502	default:
1503		/* invalid family! */
1504		SCTP_FREE(aa, SCTP_M_ASC_ADDR);
1505		if (ifa)
1506			sctp_free_ifa(ifa);
1507		return (-1);
1508	}
1509	aa->sent = 0;		/* clear sent flag */
1510
1511	/* delete goes to the back of the queue */
1512	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1513
1514	/* sa_ignore MEMLEAK {memory is put on the tailq} */
1515	return (0);
1516}
1517
1518/*
1519 * find a specific asconf param on our "sent" queue
1520 */
1521static struct sctp_asconf_addr *
1522sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1523{
1524	struct sctp_asconf_addr *aa;
1525
1526	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1527		if (aa->ap.aph.correlation_id == correlation_id &&
1528		    aa->sent == 1) {
1529			/* found it */
1530			return (aa);
1531		}
1532	}
1533	/* didn't find it */
1534	return (NULL);
1535}
1536
1537/*
1538 * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
1539 * notifications based on the error response
1540 */
1541static void
1542sctp_asconf_process_error(struct sctp_tcb *stcb SCTP_UNUSED,
1543    struct sctp_asconf_paramhdr *aph)
1544{
1545	struct sctp_error_cause *eh;
1546	struct sctp_paramhdr *ph;
1547	uint16_t param_type;
1548	uint16_t error_code;
1549
1550	eh = (struct sctp_error_cause *)(aph + 1);
1551	ph = (struct sctp_paramhdr *)(eh + 1);
1552	/* validate lengths */
1553	if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1554	    htons(aph->ph.param_length)) {
1555		/* invalid error cause length */
1556		SCTPDBG(SCTP_DEBUG_ASCONF1,
1557		    "asconf_process_error: cause element too long\n");
1558		return;
1559	}
1560	if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1561	    htons(eh->length)) {
1562		/* invalid included TLV length */
1563		SCTPDBG(SCTP_DEBUG_ASCONF1,
1564		    "asconf_process_error: included TLV too long\n");
1565		return;
1566	}
1567	/* which error code ? */
1568	error_code = ntohs(eh->code);
1569	param_type = ntohs(aph->ph.param_type);
1570	/* FIX: this should go back up the REMOTE_ERROR ULP notify */
1571	switch (error_code) {
1572	case SCTP_CAUSE_RESOURCE_SHORTAGE:
1573		/* we allow ourselves to "try again" for this error */
1574		break;
1575	default:
1576		/* peer can't handle it... */
1577		switch (param_type) {
1578		case SCTP_ADD_IP_ADDRESS:
1579		case SCTP_DEL_IP_ADDRESS:
1580		case SCTP_SET_PRIM_ADDR:
1581			break;
1582		default:
1583			break;
1584		}
1585	}
1586}
1587
1588/*
1589 * process an asconf queue param.
1590 * aparam: parameter to process, will be removed from the queue.
1591 * flag: 1=success case, 0=failure case
1592 */
1593static void
1594sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1595    struct sctp_asconf_addr *aparam, uint32_t flag)
1596{
1597	uint16_t param_type;
1598
1599	/* process this param */
1600	param_type = aparam->ap.aph.ph.param_type;
1601	switch (param_type) {
1602	case SCTP_ADD_IP_ADDRESS:
1603		SCTPDBG(SCTP_DEBUG_ASCONF1,
1604		    "process_param_ack: added IP address\n");
1605		sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, flag);
1606		break;
1607	case SCTP_DEL_IP_ADDRESS:
1608		SCTPDBG(SCTP_DEBUG_ASCONF1,
1609		    "process_param_ack: deleted IP address\n");
1610		/* nothing really to do... lists already updated */
1611		break;
1612	case SCTP_SET_PRIM_ADDR:
1613		SCTPDBG(SCTP_DEBUG_ASCONF1,
1614		    "process_param_ack: set primary IP address\n");
1615		/* nothing to do... peer may start using this addr */
1616		break;
1617	default:
1618		/* should NEVER happen */
1619		break;
1620	}
1621
1622	/* remove the param and free it */
1623	TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1624	if (aparam->ifa)
1625		sctp_free_ifa(aparam->ifa);
1626	SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1627}
1628
1629/*
1630 * cleanup from a bad asconf ack parameter
1631 */
1632static void
1633sctp_asconf_ack_clear(struct sctp_tcb *stcb SCTP_UNUSED)
1634{
1635	/* assume peer doesn't really know how to do asconfs */
1636	/* XXX we could free the pending queue here */
1637
1638}
1639
1640void
1641sctp_handle_asconf_ack(struct mbuf *m, int offset,
1642    struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1643    struct sctp_nets *net, int *abort_no_unlock)
1644{
1645	struct sctp_association *asoc;
1646	uint32_t serial_num;
1647	uint16_t ack_length;
1648	struct sctp_asconf_paramhdr *aph;
1649	struct sctp_asconf_addr *aa, *aa_next;
1650	uint32_t last_error_id = 0;	/* last error correlation id */
1651	uint32_t id;
1652	struct sctp_asconf_addr *ap;
1653
1654	/* asconf param buffer */
1655	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
1656
1657	/* verify minimum length */
1658	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1659		SCTPDBG(SCTP_DEBUG_ASCONF1,
1660		    "handle_asconf_ack: chunk too small = %xh\n",
1661		    ntohs(cp->ch.chunk_length));
1662		return;
1663	}
1664	asoc = &stcb->asoc;
1665	serial_num = ntohl(cp->serial_number);
1666
1667	/*
1668	 * NOTE: we may want to handle this differently- currently, we will
1669	 * abort when we get an ack for the expected serial number + 1 (eg.
1670	 * we didn't send it), process an ack normally if it is the expected
1671	 * serial number, and re-send the previous ack for *ALL* other
1672	 * serial numbers
1673	 */
1674
1675	/*
1676	 * if the serial number is the next expected, but I didn't send it,
1677	 * abort the asoc, since someone probably just hijacked us...
1678	 */
1679	if (serial_num == (asoc->asconf_seq_out + 1)) {
1680		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1681		sctp_abort_an_association(stcb->sctp_ep, stcb, NULL, SCTP_SO_NOT_LOCKED);
1682		*abort_no_unlock = 1;
1683		return;
1684	}
1685	if (serial_num != asoc->asconf_seq_out_acked + 1) {
1686		/* got a duplicate/unexpected ASCONF-ACK */
1687		SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
1688		    serial_num, asoc->asconf_seq_out_acked + 1);
1689		return;
1690	}
1691	if (serial_num == asoc->asconf_seq_out - 1) {
1692		/* stop our timer */
1693		sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net,
1694		    SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
1695	}
1696	/* process the ASCONF-ACK contents */
1697	ack_length = ntohs(cp->ch.chunk_length) -
1698	    sizeof(struct sctp_asconf_ack_chunk);
1699	offset += sizeof(struct sctp_asconf_ack_chunk);
1700	/* process through all parameters */
1701	while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1702		unsigned int param_length, param_type;
1703
1704		/* get pointer to next asconf parameter */
1705		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1706		    sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1707		if (aph == NULL) {
1708			/* can't get an asconf paramhdr */
1709			sctp_asconf_ack_clear(stcb);
1710			return;
1711		}
1712		param_type = ntohs(aph->ph.param_type);
1713		param_length = ntohs(aph->ph.param_length);
1714		if (param_length > ack_length) {
1715			sctp_asconf_ack_clear(stcb);
1716			return;
1717		}
1718		if (param_length < sizeof(struct sctp_paramhdr)) {
1719			sctp_asconf_ack_clear(stcb);
1720			return;
1721		}
1722		/* get the complete parameter... */
1723		if (param_length > sizeof(aparam_buf)) {
1724			SCTPDBG(SCTP_DEBUG_ASCONF1,
1725			    "param length (%u) larger than buffer size!\n", param_length);
1726			sctp_asconf_ack_clear(stcb);
1727			return;
1728		}
1729		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1730		if (aph == NULL) {
1731			sctp_asconf_ack_clear(stcb);
1732			return;
1733		}
1734		/* correlation_id is transparent to peer, no ntohl needed */
1735		id = aph->correlation_id;
1736
1737		switch (param_type) {
1738		case SCTP_ERROR_CAUSE_IND:
1739			last_error_id = id;
1740			/* find the corresponding asconf param in our queue */
1741			ap = sctp_asconf_find_param(stcb, id);
1742			if (ap == NULL) {
1743				/* hmm... can't find this in our queue! */
1744				break;
1745			}
1746			/* process the parameter, failed flag */
1747			sctp_asconf_process_param_ack(stcb, ap, 0);
1748			/* process the error response */
1749			sctp_asconf_process_error(stcb, aph);
1750			break;
1751		case SCTP_SUCCESS_REPORT:
1752			/* find the corresponding asconf param in our queue */
1753			ap = sctp_asconf_find_param(stcb, id);
1754			if (ap == NULL) {
1755				/* hmm... can't find this in our queue! */
1756				break;
1757			}
1758			/* process the parameter, success flag */
1759			sctp_asconf_process_param_ack(stcb, ap, 1);
1760			break;
1761		default:
1762			break;
1763		}		/* switch */
1764
1765		/* update remaining ASCONF-ACK message length to process */
1766		ack_length -= SCTP_SIZE32(param_length);
1767		if (ack_length <= 0) {
1768			/* no more data in the mbuf chain */
1769			break;
1770		}
1771		offset += SCTP_SIZE32(param_length);
1772	}			/* while */
1773
1774	/*
1775	 * if there are any "sent" params still on the queue, these are
1776	 * implicitly "success", or "failed" (if we got an error back) ...
1777	 * so process these appropriately
1778	 *
1779	 * we assume that the correlation_id's are monotonically increasing
1780	 * beginning from 1 and that we don't have *that* many outstanding
1781	 * at any given time
1782	 */
1783	if (last_error_id == 0)
1784		last_error_id--;/* set to "max" value */
1785	TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
1786		if (aa->sent == 1) {
1787			/*
1788			 * implicitly successful or failed if correlation_id
1789			 * < last_error_id, then success else, failure
1790			 */
1791			if (aa->ap.aph.correlation_id < last_error_id)
1792				sctp_asconf_process_param_ack(stcb, aa, 1);
1793			else
1794				sctp_asconf_process_param_ack(stcb, aa, 0);
1795		} else {
1796			/*
1797			 * since we always process in order (FIFO queue) if
1798			 * we reach one that hasn't been sent, the rest
1799			 * should not have been sent either. so, we're
1800			 * done...
1801			 */
1802			break;
1803		}
1804	}
1805
1806	/* update the next sequence number to use */
1807	asoc->asconf_seq_out_acked++;
1808	/* remove the old ASCONF on our outbound queue */
1809	sctp_toss_old_asconf(stcb);
1810	if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1811#ifdef SCTP_TIMER_BASED_ASCONF
1812		/* we have more params, so restart our timer */
1813		sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1814		    stcb, net);
1815#else
1816		/* we have more params, so send out more */
1817		sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
1818#endif
1819	}
1820}
1821
1822#ifdef INET6
1823static uint32_t
1824sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1825{
1826	struct sockaddr_in6 *sin6, *net6;
1827	struct sctp_nets *net;
1828
1829	if (sa->sa_family != AF_INET6) {
1830		/* wrong family */
1831		return (0);
1832	}
1833	sin6 = (struct sockaddr_in6 *)sa;
1834	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1835		/* not link local address */
1836		return (0);
1837	}
1838	/* hunt through our destination nets list for this scope_id */
1839	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1840		if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1841		    AF_INET6)
1842			continue;
1843		net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1844		if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1845			continue;
1846		if (sctp_is_same_scope(sin6, net6)) {
1847			/* found one */
1848			return (1);
1849		}
1850	}
1851	/* didn't find one */
1852	return (0);
1853}
1854
1855#endif
1856
1857/*
1858 * address management functions
1859 */
1860static void
1861sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1862    struct sctp_ifa *ifa, uint16_t type, int addr_locked)
1863{
1864	int status;
1865
1866	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 ||
1867	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1868		/* subset bound, no ASCONF allowed case, so ignore */
1869		return;
1870	}
1871	/*
1872	 * note: we know this is not the subset bound, no ASCONF case eg.
1873	 * this is boundall or subset bound w/ASCONF allowed
1874	 */
1875
1876	/* first, make sure that the address is IPv4 or IPv6 and not jailed */
1877	switch (ifa->address.sa.sa_family) {
1878#ifdef INET6
1879	case AF_INET6:
1880		if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1881		    &ifa->address.sin6.sin6_addr) != 0) {
1882			return;
1883		}
1884		break;
1885#endif
1886#ifdef INET
1887	case AF_INET:
1888		if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1889		    &ifa->address.sin.sin_addr) != 0) {
1890			return;
1891		}
1892		break;
1893#endif
1894	default:
1895		return;
1896	}
1897#ifdef INET6
1898	/* make sure we're "allowed" to add this type of addr */
1899	if (ifa->address.sa.sa_family == AF_INET6) {
1900		/* invalid if we're not a v6 endpoint */
1901		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1902			return;
1903		/* is the v6 addr really valid ? */
1904		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1905			return;
1906		}
1907	}
1908#endif
1909	/* put this address on the "pending/do not use yet" list */
1910	sctp_add_local_addr_restricted(stcb, ifa);
1911	/*
1912	 * check address scope if address is out of scope, don't queue
1913	 * anything... note: this would leave the address on both inp and
1914	 * asoc lists
1915	 */
1916	switch (ifa->address.sa.sa_family) {
1917#ifdef INET6
1918	case AF_INET6:
1919		{
1920			struct sockaddr_in6 *sin6;
1921
1922			sin6 = &ifa->address.sin6;
1923			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1924				/* we skip unspecifed addresses */
1925				return;
1926			}
1927			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1928				if (stcb->asoc.scope.local_scope == 0) {
1929					return;
1930				}
1931				/* is it the right link local scope? */
1932				if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
1933					return;
1934				}
1935			}
1936			if (stcb->asoc.scope.site_scope == 0 &&
1937			    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
1938				return;
1939			}
1940			break;
1941		}
1942#endif
1943#ifdef INET
1944	case AF_INET:
1945		{
1946			struct sockaddr_in *sin;
1947			struct in6pcb *inp6;
1948
1949			inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1950			/* invalid if we are a v6 only endpoint */
1951			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1952			    SCTP_IPV6_V6ONLY(inp6))
1953				return;
1954
1955			sin = &ifa->address.sin;
1956			if (sin->sin_addr.s_addr == 0) {
1957				/* we skip unspecifed addresses */
1958				return;
1959			}
1960			if (stcb->asoc.scope.ipv4_local_scope == 0 &&
1961			    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1962				return;
1963			}
1964			break;
1965		}
1966#endif
1967	default:
1968		/* else, not AF_INET or AF_INET6, so skip */
1969		return;
1970	}
1971
1972	/* queue an asconf for this address add/delete */
1973	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
1974		/* does the peer do asconf? */
1975		if (stcb->asoc.asconf_supported) {
1976			/* queue an asconf for this addr */
1977			status = sctp_asconf_queue_add(stcb, ifa, type);
1978
1979			/*
1980			 * if queued ok, and in the open state, send out the
1981			 * ASCONF.  If in the non-open state, these will be
1982			 * sent when the state goes open.
1983			 */
1984			if (status == 0 &&
1985			    SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
1986#ifdef SCTP_TIMER_BASED_ASCONF
1987				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
1988				    stcb, stcb->asoc.primary_destination);
1989#else
1990				sctp_send_asconf(stcb, NULL, addr_locked);
1991#endif
1992			}
1993		}
1994	}
1995}
1996
1997
1998int
1999sctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
2000{
2001	struct sctp_asconf_iterator *asc;
2002	struct sctp_ifa *ifa;
2003	struct sctp_laddr *l;
2004	int cnt_invalid = 0;
2005
2006	asc = (struct sctp_asconf_iterator *)ptr;
2007	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2008		ifa = l->ifa;
2009		switch (ifa->address.sa.sa_family) {
2010#ifdef INET6
2011		case AF_INET6:
2012			/* invalid if we're not a v6 endpoint */
2013			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2014				cnt_invalid++;
2015				if (asc->cnt == cnt_invalid)
2016					return (1);
2017			}
2018			break;
2019#endif
2020#ifdef INET
2021		case AF_INET:
2022			{
2023				/* invalid if we are a v6 only endpoint */
2024				struct in6pcb *inp6;
2025
2026				inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2027				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2028				    SCTP_IPV6_V6ONLY(inp6)) {
2029					cnt_invalid++;
2030					if (asc->cnt == cnt_invalid)
2031						return (1);
2032				}
2033				break;
2034			}
2035#endif
2036		default:
2037			/* invalid address family */
2038			cnt_invalid++;
2039			if (asc->cnt == cnt_invalid)
2040				return (1);
2041		}
2042	}
2043	return (0);
2044}
2045
2046static int
2047sctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
2048{
2049	struct sctp_ifa *ifa;
2050	struct sctp_asconf_iterator *asc;
2051	struct sctp_laddr *laddr, *nladdr, *l;
2052
2053	/* Only for specific case not bound all */
2054	asc = (struct sctp_asconf_iterator *)ptr;
2055	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2056		ifa = l->ifa;
2057		if (l->action == SCTP_ADD_IP_ADDRESS) {
2058			LIST_FOREACH(laddr, &inp->sctp_addr_list,
2059			    sctp_nxt_addr) {
2060				if (laddr->ifa == ifa) {
2061					laddr->action = 0;
2062					break;
2063				}
2064			}
2065		} else if (l->action == SCTP_DEL_IP_ADDRESS) {
2066			LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
2067				/* remove only after all guys are done */
2068				if (laddr->ifa == ifa) {
2069					sctp_del_local_addr_ep(inp, ifa);
2070				}
2071			}
2072		}
2073	}
2074	return (0);
2075}
2076
2077void
2078sctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2079    void *ptr, uint32_t val SCTP_UNUSED)
2080{
2081	struct sctp_asconf_iterator *asc;
2082	struct sctp_ifa *ifa;
2083	struct sctp_laddr *l;
2084	int cnt_invalid = 0;
2085	int type, status;
2086	int num_queued = 0;
2087
2088	asc = (struct sctp_asconf_iterator *)ptr;
2089	LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
2090		ifa = l->ifa;
2091		type = l->action;
2092
2093		/* address's vrf_id must be the vrf_id of the assoc */
2094		if (ifa->vrf_id != stcb->asoc.vrf_id) {
2095			continue;
2096		}
2097		/* Same checks again for assoc */
2098		switch (ifa->address.sa.sa_family) {
2099#ifdef INET6
2100		case AF_INET6:
2101			{
2102				/* invalid if we're not a v6 endpoint */
2103				struct sockaddr_in6 *sin6;
2104
2105				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
2106					cnt_invalid++;
2107					if (asc->cnt == cnt_invalid)
2108						return;
2109					else
2110						continue;
2111				}
2112				sin6 = &ifa->address.sin6;
2113				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2114					/* we skip unspecifed addresses */
2115					continue;
2116				}
2117				if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2118				    &sin6->sin6_addr) != 0) {
2119					continue;
2120				}
2121				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2122					if (stcb->asoc.scope.local_scope == 0) {
2123						continue;
2124					}
2125					/* is it the right link local scope? */
2126					if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
2127						continue;
2128					}
2129				}
2130				break;
2131			}
2132#endif
2133#ifdef INET
2134		case AF_INET:
2135			{
2136				/* invalid if we are a v6 only endpoint */
2137				struct in6pcb *inp6;
2138				struct sockaddr_in *sin;
2139
2140				inp6 = (struct in6pcb *)&inp->ip_inp.inp;
2141				/* invalid if we are a v6 only endpoint */
2142				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2143				    SCTP_IPV6_V6ONLY(inp6))
2144					continue;
2145
2146				sin = &ifa->address.sin;
2147				if (sin->sin_addr.s_addr == 0) {
2148					/* we skip unspecifed addresses */
2149					continue;
2150				}
2151				if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2152				    &sin->sin_addr) != 0) {
2153					continue;
2154				}
2155				if (stcb->asoc.scope.ipv4_local_scope == 0 &&
2156				    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2157					continue;
2158				}
2159				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2160				    SCTP_IPV6_V6ONLY(inp6)) {
2161					cnt_invalid++;
2162					if (asc->cnt == cnt_invalid)
2163						return;
2164					else
2165						continue;
2166				}
2167				break;
2168			}
2169#endif
2170		default:
2171			/* invalid address family */
2172			cnt_invalid++;
2173			if (asc->cnt == cnt_invalid)
2174				return;
2175			else
2176				continue;
2177			break;
2178		}
2179
2180		if (type == SCTP_ADD_IP_ADDRESS) {
2181			/* prevent this address from being used as a source */
2182			sctp_add_local_addr_restricted(stcb, ifa);
2183		} else if (type == SCTP_DEL_IP_ADDRESS) {
2184			struct sctp_nets *net;
2185
2186			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2187				sctp_rtentry_t *rt;
2188
2189				/* delete this address if cached */
2190				if (net->ro._s_addr == ifa) {
2191					sctp_free_ifa(net->ro._s_addr);
2192					net->ro._s_addr = NULL;
2193					net->src_addr_selected = 0;
2194					rt = net->ro.ro_rt;
2195					if (rt) {
2196						RTFREE(rt);
2197						net->ro.ro_rt = NULL;
2198					}
2199					/*
2200					 * Now we deleted our src address,
2201					 * should we not also now reset the
2202					 * cwnd/rto to start as if its a new
2203					 * address?
2204					 */
2205					stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
2206					net->RTO = 0;
2207
2208				}
2209			}
2210		} else if (type == SCTP_SET_PRIM_ADDR) {
2211			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2212				/* must validate the ifa is in the ep */
2213				if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
2214					continue;
2215				}
2216			} else {
2217				/* Need to check scopes for this guy */
2218				if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2219					continue;
2220				}
2221			}
2222		}
2223		/* queue an asconf for this address add/delete */
2224		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
2225		    stcb->asoc.asconf_supported == 1) {
2226			/* queue an asconf for this addr */
2227			status = sctp_asconf_queue_add(stcb, ifa, type);
2228			/*
2229			 * if queued ok, and in the open state, update the
2230			 * count of queued params.  If in the non-open
2231			 * state, these get sent when the assoc goes open.
2232			 */
2233			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2234				if (status >= 0) {
2235					num_queued++;
2236				}
2237			}
2238		}
2239	}
2240	/*
2241	 * If we have queued params in the open state, send out an ASCONF.
2242	 */
2243	if (num_queued > 0) {
2244		sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2245	}
2246}
2247
2248void
2249sctp_asconf_iterator_end(void *ptr, uint32_t val SCTP_UNUSED)
2250{
2251	struct sctp_asconf_iterator *asc;
2252	struct sctp_ifa *ifa;
2253	struct sctp_laddr *l, *nl;
2254
2255	asc = (struct sctp_asconf_iterator *)ptr;
2256	LIST_FOREACH_SAFE(l, &asc->list_of_work, sctp_nxt_addr, nl) {
2257		ifa = l->ifa;
2258		if (l->action == SCTP_ADD_IP_ADDRESS) {
2259			/* Clear the defer use flag */
2260			ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
2261		}
2262		sctp_free_ifa(ifa);
2263		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
2264		SCTP_DECR_LADDR_COUNT();
2265	}
2266	SCTP_FREE(asc, SCTP_M_ASC_IT);
2267}
2268
2269/*
2270 * sa is the sockaddr to ask the peer to set primary to.
2271 * returns: 0 = completed, -1 = error
2272 */
2273int32_t
2274sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2275{
2276	uint32_t vrf_id;
2277	struct sctp_ifa *ifa;
2278
2279	/* find the ifa for the desired set primary */
2280	vrf_id = stcb->asoc.vrf_id;
2281	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
2282	if (ifa == NULL) {
2283		/* Invalid address */
2284		return (-1);
2285	}
2286	/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2287	if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
2288		/* set primary queuing succeeded */
2289		SCTPDBG(SCTP_DEBUG_ASCONF1,
2290		    "set_primary_ip_address_sa: queued on tcb=%p, ",
2291		    (void *)stcb);
2292		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2293		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2294#ifdef SCTP_TIMER_BASED_ASCONF
2295			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2296			    stcb->sctp_ep, stcb,
2297			    stcb->asoc.primary_destination);
2298#else
2299			sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2300#endif
2301		}
2302	} else {
2303		SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2304		    (void *)stcb);
2305		SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
2306		return (-1);
2307	}
2308	return (0);
2309}
2310
2311void
2312sctp_set_primary_ip_address(struct sctp_ifa *ifa)
2313{
2314	struct sctp_inpcb *inp;
2315
2316	/* go through all our PCB's */
2317	LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
2318		struct sctp_tcb *stcb;
2319
2320		/* process for all associations for this endpoint */
2321		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2322			/* queue an ASCONF:SET_PRIM_ADDR to be sent */
2323			if (!sctp_asconf_queue_add(stcb, ifa,
2324			    SCTP_SET_PRIM_ADDR)) {
2325				/* set primary queuing succeeded */
2326				SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address: queued on stcb=%p, ",
2327				    (void *)stcb);
2328				SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &ifa->address.sa);
2329				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2330#ifdef SCTP_TIMER_BASED_ASCONF
2331					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2332					    stcb->sctp_ep, stcb,
2333					    stcb->asoc.primary_destination);
2334#else
2335					sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2336#endif
2337				}
2338			}
2339		}		/* for each stcb */
2340	}			/* for each inp */
2341}
2342
2343int
2344sctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
2345{
2346	struct sctp_tmit_chunk *chk, *nchk;
2347	unsigned int offset, asconf_limit;
2348	struct sctp_asconf_chunk *acp;
2349	struct sctp_asconf_paramhdr *aph;
2350	uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
2351	struct sctp_paramhdr *ph;
2352	int add_cnt, del_cnt;
2353	uint16_t last_param_type;
2354
2355	add_cnt = del_cnt = 0;
2356	last_param_type = 0;
2357	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.asconf_send_queue, sctp_next, nchk) {
2358		if (chk->data == NULL) {
2359			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
2360			continue;
2361		}
2362		offset = 0;
2363		acp = mtod(chk->data, struct sctp_asconf_chunk *);
2364		offset += sizeof(struct sctp_asconf_chunk);
2365		asconf_limit = ntohs(acp->ch.chunk_length);
2366		ph = (struct sctp_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
2367		if (ph == NULL) {
2368			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
2369			continue;
2370		}
2371		offset += ntohs(ph->param_length);
2372
2373		aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2374		if (aph == NULL) {
2375			SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
2376			continue;
2377		}
2378		while (aph != NULL) {
2379			unsigned int param_length, param_type;
2380
2381			param_type = ntohs(aph->ph.param_type);
2382			param_length = ntohs(aph->ph.param_length);
2383			if (offset + param_length > asconf_limit) {
2384				/* parameter goes beyond end of chunk! */
2385				break;
2386			}
2387			if (param_length > sizeof(aparam_buf)) {
2388				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
2389				break;
2390			}
2391			if (param_length <= sizeof(struct sctp_paramhdr)) {
2392				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
2393				break;
2394			}
2395			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
2396			if (aph == NULL) {
2397				SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
2398				break;
2399			}
2400			ph = (struct sctp_paramhdr *)(aph + 1);
2401			if (sctp_addr_match(ph, &sctp_ifa->address.sa) != 0) {
2402				switch (param_type) {
2403				case SCTP_ADD_IP_ADDRESS:
2404					add_cnt++;
2405					break;
2406				case SCTP_DEL_IP_ADDRESS:
2407					del_cnt++;
2408					break;
2409				default:
2410					break;
2411				}
2412				last_param_type = param_type;
2413			}
2414			offset += SCTP_SIZE32(param_length);
2415			if (offset >= asconf_limit) {
2416				/* no more data in the mbuf chain */
2417				break;
2418			}
2419			/* get pointer to next asconf param */
2420			aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
2421		}
2422	}
2423
2424	/*
2425	 * we want to find the sequences which consist of ADD -> DEL -> ADD
2426	 * or DEL -> ADD
2427	 */
2428	if (add_cnt > del_cnt ||
2429	    (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
2430		return (1);
2431	}
2432	return (0);
2433}
2434
2435static struct sockaddr *
2436sctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
2437{
2438	struct sctp_vrf *vrf = NULL;
2439	struct sctp_ifn *sctp_ifn;
2440	struct sctp_ifa *sctp_ifa;
2441
2442	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2443		SCTP_IPI_ADDR_RLOCK();
2444	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
2445	if (vrf == NULL) {
2446		if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2447			SCTP_IPI_ADDR_RUNLOCK();
2448		return (NULL);
2449	}
2450	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2451		if (stcb->asoc.scope.loopback_scope == 0 &&
2452		    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2453			/* Skip if loopback_scope not set */
2454			continue;
2455		}
2456		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2457			switch (sctp_ifa->address.sa.sa_family) {
2458#ifdef INET
2459			case AF_INET:
2460				if (stcb->asoc.scope.ipv4_addr_legal) {
2461					struct sockaddr_in *sin;
2462
2463					sin = &sctp_ifa->address.sin;
2464					if (sin->sin_addr.s_addr == 0) {
2465						/* skip unspecifed addresses */
2466						continue;
2467					}
2468					if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
2469					    &sin->sin_addr) != 0) {
2470						continue;
2471					}
2472					if (stcb->asoc.scope.ipv4_local_scope == 0 &&
2473					    IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2474						continue;
2475
2476					if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2477					    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2478						continue;
2479					/*
2480					 * found a valid local v4 address to
2481					 * use
2482					 */
2483					if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2484						SCTP_IPI_ADDR_RUNLOCK();
2485					return (&sctp_ifa->address.sa);
2486				}
2487				break;
2488#endif
2489#ifdef INET6
2490			case AF_INET6:
2491				if (stcb->asoc.scope.ipv6_addr_legal) {
2492					struct sockaddr_in6 *sin6;
2493
2494					if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2495						continue;
2496					}
2497					sin6 = &sctp_ifa->address.sin6;
2498					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2499						/*
2500						 * we skip unspecifed
2501						 * addresses
2502						 */
2503						continue;
2504					}
2505					if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
2506					    &sin6->sin6_addr) != 0) {
2507						continue;
2508					}
2509					if (stcb->asoc.scope.local_scope == 0 &&
2510					    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2511						continue;
2512					if (stcb->asoc.scope.site_scope == 0 &&
2513					    IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2514						continue;
2515
2516					if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
2517					    (!sctp_is_addr_pending(stcb, sctp_ifa)))
2518						continue;
2519					/*
2520					 * found a valid local v6 address to
2521					 * use
2522					 */
2523					if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2524						SCTP_IPI_ADDR_RUNLOCK();
2525					return (&sctp_ifa->address.sa);
2526				}
2527				break;
2528#endif
2529			default:
2530				break;
2531			}
2532		}
2533	}
2534	/* no valid addresses found */
2535	if (addr_locked == SCTP_ADDR_NOT_LOCKED)
2536		SCTP_IPI_ADDR_RUNLOCK();
2537	return (NULL);
2538}
2539
2540static struct sockaddr *
2541sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2542{
2543	struct sctp_laddr *laddr;
2544
2545	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2546		if (laddr->ifa == NULL) {
2547			continue;
2548		}
2549		/* is the address restricted ? */
2550		if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
2551		    (!sctp_is_addr_pending(stcb, laddr->ifa)))
2552			continue;
2553
2554		/* found a valid local address to use */
2555		return (&laddr->ifa->address.sa);
2556	}
2557	/* no valid addresses found */
2558	return (NULL);
2559}
2560
2561/*
2562 * builds an ASCONF chunk from queued ASCONF params.
2563 * returns NULL on error (no mbuf, no ASCONF params queued, etc).
2564 */
2565struct mbuf *
2566sctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
2567{
2568	struct mbuf *m_asconf, *m_asconf_chk;
2569	struct sctp_asconf_addr *aa;
2570	struct sctp_asconf_chunk *acp;
2571	struct sctp_asconf_paramhdr *aph;
2572	struct sctp_asconf_addr_param *aap;
2573	uint32_t p_length;
2574	uint32_t correlation_id = 1;	/* 0 is reserved... */
2575	caddr_t ptr, lookup_ptr;
2576	uint8_t lookup_used = 0;
2577
2578	/* are there any asconf params to send? */
2579	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2580		if (aa->sent == 0)
2581			break;
2582	}
2583	if (aa == NULL)
2584		return (NULL);
2585
2586	/*
2587	 * get a chunk header mbuf and a cluster for the asconf params since
2588	 * it's simpler to fill in the asconf chunk header lookup address on
2589	 * the fly
2590	 */
2591	m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_NOWAIT, 1, MT_DATA);
2592	if (m_asconf_chk == NULL) {
2593		/* no mbuf's */
2594		SCTPDBG(SCTP_DEBUG_ASCONF1,
2595		    "compose_asconf: couldn't get chunk mbuf!\n");
2596		return (NULL);
2597	}
2598	m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
2599	if (m_asconf == NULL) {
2600		/* no mbuf's */
2601		SCTPDBG(SCTP_DEBUG_ASCONF1,
2602		    "compose_asconf: couldn't get mbuf!\n");
2603		sctp_m_freem(m_asconf_chk);
2604		return (NULL);
2605	}
2606	SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
2607	SCTP_BUF_LEN(m_asconf) = 0;
2608	acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2609	bzero(acp, sizeof(struct sctp_asconf_chunk));
2610	/* save pointers to lookup address and asconf params */
2611	lookup_ptr = (caddr_t)(acp + 1);	/* after the header */
2612	ptr = mtod(m_asconf, caddr_t);	/* beginning of cluster */
2613
2614	/* fill in chunk header info */
2615	acp->ch.chunk_type = SCTP_ASCONF;
2616	acp->ch.chunk_flags = 0;
2617	acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2618	stcb->asoc.asconf_seq_out++;
2619
2620	/* add parameters... up to smallest MTU allowed */
2621	TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2622		if (aa->sent)
2623			continue;
2624		/* get the parameter length */
2625		p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2626		/* will it fit in current chunk? */
2627		if ((SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu) ||
2628		    (SCTP_BUF_LEN(m_asconf) + p_length > MCLBYTES)) {
2629			/* won't fit, so we're done with this chunk */
2630			break;
2631		}
2632		/* assign (and store) a correlation id */
2633		aa->ap.aph.correlation_id = correlation_id++;
2634
2635		/*
2636		 * fill in address if we're doing a delete this is a simple
2637		 * way for us to fill in the correlation address, which
2638		 * should only be used by the peer if we're deleting our
2639		 * source address and adding a new address (e.g. renumbering
2640		 * case)
2641		 */
2642		if (lookup_used == 0 &&
2643		    (aa->special_del == 0) &&
2644		    aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2645			struct sctp_ipv6addr_param *lookup;
2646			uint16_t p_size, addr_size;
2647
2648			lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2649			lookup->ph.param_type =
2650			    htons(aa->ap.addrp.ph.param_type);
2651			if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2652				/* copy IPv6 address */
2653				p_size = sizeof(struct sctp_ipv6addr_param);
2654				addr_size = sizeof(struct in6_addr);
2655			} else {
2656				/* copy IPv4 address */
2657				p_size = sizeof(struct sctp_ipv4addr_param);
2658				addr_size = sizeof(struct in_addr);
2659			}
2660			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2661			memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2662			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2663			lookup_used = 1;
2664		}
2665		/* copy into current space */
2666		memcpy(ptr, &aa->ap, p_length);
2667
2668		/* network elements and update lengths */
2669		aph = (struct sctp_asconf_paramhdr *)ptr;
2670		aap = (struct sctp_asconf_addr_param *)ptr;
2671		/* correlation_id is transparent to peer, no htonl needed */
2672		aph->ph.param_type = htons(aph->ph.param_type);
2673		aph->ph.param_length = htons(aph->ph.param_length);
2674		aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2675		aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2676
2677		SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
2678		ptr += SCTP_SIZE32(p_length);
2679
2680		/*
2681		 * these params are removed off the pending list upon
2682		 * getting an ASCONF-ACK back from the peer, just set flag
2683		 */
2684		aa->sent = 1;
2685	}
2686	/* check to see if the lookup addr has been populated yet */
2687	if (lookup_used == 0) {
2688		/* NOTE: if the address param is optional, can skip this... */
2689		/* add any valid (existing) address... */
2690		struct sctp_ipv6addr_param *lookup;
2691		uint16_t p_size, addr_size;
2692		struct sockaddr *found_addr;
2693		caddr_t addr_ptr;
2694
2695		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2696			found_addr = sctp_find_valid_localaddr(stcb,
2697			    addr_locked);
2698		else
2699			found_addr = sctp_find_valid_localaddr_ep(stcb);
2700
2701		lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2702		if (found_addr != NULL) {
2703			switch (found_addr->sa_family) {
2704#ifdef INET6
2705			case AF_INET6:
2706				/* copy IPv6 address */
2707				lookup->ph.param_type =
2708				    htons(SCTP_IPV6_ADDRESS);
2709				p_size = sizeof(struct sctp_ipv6addr_param);
2710				addr_size = sizeof(struct in6_addr);
2711				addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2712				    found_addr)->sin6_addr;
2713				break;
2714#endif
2715#ifdef INET
2716			case AF_INET:
2717				/* copy IPv4 address */
2718				lookup->ph.param_type =
2719				    htons(SCTP_IPV4_ADDRESS);
2720				p_size = sizeof(struct sctp_ipv4addr_param);
2721				addr_size = sizeof(struct in_addr);
2722				addr_ptr = (caddr_t)&((struct sockaddr_in *)
2723				    found_addr)->sin_addr;
2724				break;
2725#endif
2726			default:
2727				p_size = 0;
2728				addr_size = 0;
2729				addr_ptr = NULL;
2730				break;
2731			}
2732			lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2733			memcpy(lookup->addr, addr_ptr, addr_size);
2734			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
2735		} else {
2736			/* uh oh... don't have any address?? */
2737			SCTPDBG(SCTP_DEBUG_ASCONF1,
2738			    "compose_asconf: no lookup addr!\n");
2739			/* XXX for now, we send a IPv4 address of 0.0.0.0 */
2740			lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2741			lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2742			bzero(lookup->addr, sizeof(struct in_addr));
2743			SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2744		}
2745	}
2746	/* chain it all together */
2747	SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
2748	*retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
2749	acp->ch.chunk_length = htons(*retlen);
2750
2751	return (m_asconf_chk);
2752}
2753
2754/*
2755 * section to handle address changes before an association is up eg. changes
2756 * during INIT/INIT-ACK/COOKIE-ECHO handshake
2757 */
2758
2759/*
2760 * processes the (local) addresses in the INIT-ACK chunk
2761 */
2762static void
2763sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2764    unsigned int offset, unsigned int length)
2765{
2766	struct sctp_paramhdr tmp_param, *ph;
2767	uint16_t plen, ptype;
2768	struct sctp_ifa *sctp_ifa;
2769	union sctp_sockstore store;
2770
2771#ifdef INET6
2772	struct sctp_ipv6addr_param addr6_store;
2773
2774#endif
2775#ifdef INET
2776	struct sctp_ipv4addr_param addr4_store;
2777
2778#endif
2779
2780	SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
2781	if (stcb == NULL)	/* Un-needed check for SA */
2782		return;
2783
2784	/* convert to upper bound */
2785	length += offset;
2786
2787	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2788		return;
2789	}
2790	/* go through the addresses in the init-ack */
2791	ph = (struct sctp_paramhdr *)
2792	    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
2793	    (uint8_t *) & tmp_param);
2794	while (ph != NULL) {
2795		ptype = ntohs(ph->param_type);
2796		plen = ntohs(ph->param_length);
2797		switch (ptype) {
2798#ifdef INET6
2799		case SCTP_IPV6_ADDRESS:
2800			{
2801				struct sctp_ipv6addr_param *a6p;
2802
2803				/* get the entire IPv6 address param */
2804				a6p = (struct sctp_ipv6addr_param *)
2805				    sctp_m_getptr(m, offset,
2806				    sizeof(struct sctp_ipv6addr_param),
2807				    (uint8_t *) & addr6_store);
2808				if (plen != sizeof(struct sctp_ipv6addr_param) ||
2809				    a6p == NULL) {
2810					return;
2811				}
2812				memset(&store, 0, sizeof(union sctp_sockstore));
2813				store.sin6.sin6_family = AF_INET6;
2814				store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2815				store.sin6.sin6_port = stcb->rport;
2816				memcpy(&store.sin6.sin6_addr, a6p->addr, sizeof(struct in6_addr));
2817				break;
2818			}
2819#endif
2820#ifdef INET
2821		case SCTP_IPV4_ADDRESS:
2822			{
2823				struct sctp_ipv4addr_param *a4p;
2824
2825				/* get the entire IPv4 address param */
2826				a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
2827				    sizeof(struct sctp_ipv4addr_param),
2828				    (uint8_t *) & addr4_store);
2829				if (plen != sizeof(struct sctp_ipv4addr_param) ||
2830				    a4p == NULL) {
2831					return;
2832				}
2833				memset(&store, 0, sizeof(union sctp_sockstore));
2834				store.sin.sin_family = AF_INET;
2835				store.sin.sin_len = sizeof(struct sockaddr_in);
2836				store.sin.sin_port = stcb->rport;
2837				store.sin.sin_addr.s_addr = a4p->addr;
2838				break;
2839			}
2840#endif
2841		default:
2842			goto next_addr;
2843		}
2844
2845		/* see if this address really (still) exists */
2846		sctp_ifa = sctp_find_ifa_by_addr(&store.sa, stcb->asoc.vrf_id,
2847		    SCTP_ADDR_NOT_LOCKED);
2848		if (sctp_ifa == NULL) {
2849			/* address doesn't exist anymore */
2850			int status;
2851
2852			/* are ASCONFs allowed ? */
2853			if ((sctp_is_feature_on(stcb->sctp_ep,
2854			    SCTP_PCB_FLAGS_DO_ASCONF)) &&
2855			    stcb->asoc.asconf_supported) {
2856				/* queue an ASCONF DEL_IP_ADDRESS */
2857				status = sctp_asconf_queue_sa_delete(stcb, &store.sa);
2858				/*
2859				 * if queued ok, and in correct state, send
2860				 * out the ASCONF.
2861				 */
2862				if (status == 0 &&
2863				    SCTP_GET_STATE(&stcb->asoc) ==
2864				    SCTP_STATE_OPEN) {
2865#ifdef SCTP_TIMER_BASED_ASCONF
2866					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2867					    stcb->sctp_ep, stcb,
2868					    stcb->asoc.primary_destination);
2869#else
2870					sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
2871#endif
2872				}
2873			}
2874		}
2875next_addr:
2876		/*
2877		 * Sanity check:  Make sure the length isn't 0, otherwise
2878		 * we'll be stuck in this loop for a long time...
2879		 */
2880		if (SCTP_SIZE32(plen) == 0) {
2881			SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
2882			    plen, ptype);
2883			return;
2884		}
2885		/* get next parameter */
2886		offset += SCTP_SIZE32(plen);
2887		if ((offset + sizeof(struct sctp_paramhdr)) > length)
2888			return;
2889		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2890		    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2891	}			/* while */
2892}
2893
2894/* FIX ME: need to verify return result for v6 address type if v6 disabled */
2895/*
2896 * checks to see if a specific address is in the initack address list returns
2897 * 1 if found, 0 if not
2898 */
2899static uint32_t
2900sctp_addr_in_initack(struct mbuf *m, uint32_t offset, uint32_t length, struct sockaddr *sa)
2901{
2902	struct sctp_paramhdr tmp_param, *ph;
2903	uint16_t plen, ptype;
2904
2905#ifdef INET
2906	struct sockaddr_in *sin;
2907	struct sctp_ipv4addr_param *a4p;
2908	struct sctp_ipv6addr_param addr4_store;
2909
2910#endif
2911#ifdef INET6
2912	struct sockaddr_in6 *sin6;
2913	struct sctp_ipv6addr_param *a6p;
2914	struct sctp_ipv6addr_param addr6_store;
2915	struct sockaddr_in6 sin6_tmp;
2916
2917#endif
2918
2919	switch (sa->sa_family) {
2920#ifdef INET
2921	case AF_INET:
2922		break;
2923#endif
2924#ifdef INET6
2925	case AF_INET6:
2926		break;
2927#endif
2928	default:
2929		return (0);
2930	}
2931
2932	SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
2933	SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
2934	/* convert to upper bound */
2935	length += offset;
2936
2937	if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2938		SCTPDBG(SCTP_DEBUG_ASCONF1,
2939		    "find_initack_addr: invalid offset?\n");
2940		return (0);
2941	}
2942	/* go through the addresses in the init-ack */
2943	ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2944	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
2945	while (ph != NULL) {
2946		ptype = ntohs(ph->param_type);
2947		plen = ntohs(ph->param_length);
2948		switch (ptype) {
2949#ifdef INET6
2950		case SCTP_IPV6_ADDRESS:
2951			if (sa->sa_family == AF_INET6) {
2952				/* get the entire IPv6 address param */
2953				if (plen != sizeof(struct sctp_ipv6addr_param)) {
2954					break;
2955				}
2956				/* get the entire IPv6 address param */
2957				a6p = (struct sctp_ipv6addr_param *)
2958				    sctp_m_getptr(m, offset,
2959				    sizeof(struct sctp_ipv6addr_param),
2960				    (uint8_t *) & addr6_store);
2961				if (a6p == NULL) {
2962					return (0);
2963				}
2964				sin6 = (struct sockaddr_in6 *)sa;
2965				if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2966					/* create a copy and clear scope */
2967					memcpy(&sin6_tmp, sin6,
2968					    sizeof(struct sockaddr_in6));
2969					sin6 = &sin6_tmp;
2970					in6_clearscope(&sin6->sin6_addr);
2971				}
2972				if (memcmp(&sin6->sin6_addr, a6p->addr,
2973				    sizeof(struct in6_addr)) == 0) {
2974					/* found it */
2975					return (1);
2976				}
2977			}
2978			break;
2979#endif				/* INET6 */
2980#ifdef INET
2981		case SCTP_IPV4_ADDRESS:
2982			if (sa->sa_family == AF_INET) {
2983				if (plen != sizeof(struct sctp_ipv4addr_param)) {
2984					break;
2985				}
2986				/* get the entire IPv4 address param */
2987				a4p = (struct sctp_ipv4addr_param *)
2988				    sctp_m_getptr(m, offset,
2989				    sizeof(struct sctp_ipv4addr_param),
2990				    (uint8_t *) & addr4_store);
2991				if (a4p == NULL) {
2992					return (0);
2993				}
2994				sin = (struct sockaddr_in *)sa;
2995				if (sin->sin_addr.s_addr == a4p->addr) {
2996					/* found it */
2997					return (1);
2998				}
2999			}
3000			break;
3001#endif
3002		default:
3003			break;
3004		}
3005		/* get next parameter */
3006		offset += SCTP_SIZE32(plen);
3007		if (offset + sizeof(struct sctp_paramhdr) > length) {
3008			return (0);
3009		}
3010		ph = (struct sctp_paramhdr *)
3011		    sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
3012		    (uint8_t *) & tmp_param);
3013	}			/* while */
3014	/* not found! */
3015	return (0);
3016}
3017
3018/*
3019 * makes sure that the current endpoint local addr list is consistent with
3020 * the new association (eg. subset bound, asconf allowed) adds addresses as
3021 * necessary
3022 */
3023static void
3024sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3025    int length, struct sockaddr *init_addr)
3026{
3027	struct sctp_laddr *laddr;
3028
3029	/* go through the endpoint list */
3030	LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3031		/* be paranoid and validate the laddr */
3032		if (laddr->ifa == NULL) {
3033			SCTPDBG(SCTP_DEBUG_ASCONF1,
3034			    "check_addr_list_ep: laddr->ifa is NULL");
3035			continue;
3036		}
3037		if (laddr->ifa == NULL) {
3038			SCTPDBG(SCTP_DEBUG_ASCONF1, "check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
3039			continue;
3040		}
3041		/* do i have it implicitly? */
3042		if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
3043			continue;
3044		}
3045		/* check to see if in the init-ack */
3046		if (!sctp_addr_in_initack(m, offset, length, &laddr->ifa->address.sa)) {
3047			/* try to add it */
3048			sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
3049			    SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
3050		}
3051	}
3052}
3053
3054/*
3055 * makes sure that the current kernel address list is consistent with the new
3056 * association (with all addrs bound) adds addresses as necessary
3057 */
3058static void
3059sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3060    int length, struct sockaddr *init_addr,
3061    uint16_t local_scope, uint16_t site_scope,
3062    uint16_t ipv4_scope, uint16_t loopback_scope)
3063{
3064	struct sctp_vrf *vrf = NULL;
3065	struct sctp_ifn *sctp_ifn;
3066	struct sctp_ifa *sctp_ifa;
3067	uint32_t vrf_id;
3068
3069#ifdef INET
3070	struct sockaddr_in *sin;
3071
3072#endif
3073#ifdef INET6
3074	struct sockaddr_in6 *sin6;
3075
3076#endif
3077
3078	if (stcb) {
3079		vrf_id = stcb->asoc.vrf_id;
3080	} else {
3081		return;
3082	}
3083	SCTP_IPI_ADDR_RLOCK();
3084	vrf = sctp_find_vrf(vrf_id);
3085	if (vrf == NULL) {
3086		SCTP_IPI_ADDR_RUNLOCK();
3087		return;
3088	}
3089	/* go through all our known interfaces */
3090	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3091		if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3092			/* skip loopback interface */
3093			continue;
3094		}
3095		/* go through each interface address */
3096		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3097			/* do i have it implicitly? */
3098			if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
3099				continue;
3100			}
3101			switch (sctp_ifa->address.sa.sa_family) {
3102#ifdef INET
3103			case AF_INET:
3104				sin = &sctp_ifa->address.sin;
3105				if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
3106				    &sin->sin_addr) != 0) {
3107					continue;
3108				}
3109				if ((ipv4_scope == 0) &&
3110				    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
3111					/* private address not in scope */
3112					continue;
3113				}
3114				break;
3115#endif
3116#ifdef INET6
3117			case AF_INET6:
3118				sin6 = &sctp_ifa->address.sin6;
3119				if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
3120				    &sin6->sin6_addr) != 0) {
3121					continue;
3122				}
3123				if ((local_scope == 0) &&
3124				    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
3125					continue;
3126				}
3127				if ((site_scope == 0) &&
3128				    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
3129					continue;
3130				}
3131				break;
3132#endif
3133			default:
3134				break;
3135			}
3136			/* check to see if in the init-ack */
3137			if (!sctp_addr_in_initack(m, offset, length, &sctp_ifa->address.sa)) {
3138				/* try to add it */
3139				sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
3140				    sctp_ifa, SCTP_ADD_IP_ADDRESS,
3141				    SCTP_ADDR_LOCKED);
3142			}
3143		}		/* end foreach ifa */
3144	}			/* end foreach ifn */
3145	SCTP_IPI_ADDR_RUNLOCK();
3146}
3147
3148/*
3149 * validates an init-ack chunk (from a cookie-echo) with current addresses
3150 * adds addresses from the init-ack into our local address list, if needed
3151 * queues asconf adds/deletes addresses as needed and makes appropriate list
3152 * changes for source address selection m, offset: points to the start of the
3153 * address list in an init-ack chunk length: total length of the address
3154 * params only init_addr: address where my INIT-ACK was sent from
3155 */
3156void
3157sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3158    int length, struct sockaddr *init_addr,
3159    uint16_t local_scope, uint16_t site_scope,
3160    uint16_t ipv4_scope, uint16_t loopback_scope)
3161{
3162	/* process the local addresses in the initack */
3163	sctp_process_initack_addresses(stcb, m, offset, length);
3164
3165	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3166		/* bound all case */
3167		sctp_check_address_list_all(stcb, m, offset, length, init_addr,
3168		    local_scope, site_scope, ipv4_scope, loopback_scope);
3169	} else {
3170		/* subset bound case */
3171		if (sctp_is_feature_on(stcb->sctp_ep,
3172		    SCTP_PCB_FLAGS_DO_ASCONF)) {
3173			/* asconf's allowed */
3174			sctp_check_address_list_ep(stcb, m, offset, length,
3175			    init_addr);
3176		}
3177		/* else, no asconfs allowed, so what we sent is what we get */
3178	}
3179}
3180
3181/*
3182 * sctp_bindx() support
3183 */
3184uint32_t
3185sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
3186    uint32_t type, uint32_t vrf_id, struct sctp_ifa *sctp_ifap)
3187{
3188	struct sctp_ifa *ifa;
3189	struct sctp_laddr *laddr, *nladdr;
3190
3191	if (sa->sa_len == 0) {
3192		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3193		return (EINVAL);
3194	}
3195	if (sctp_ifap) {
3196		ifa = sctp_ifap;
3197	} else if (type == SCTP_ADD_IP_ADDRESS) {
3198		/* For an add the address MUST be on the system */
3199		ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
3200	} else if (type == SCTP_DEL_IP_ADDRESS) {
3201		/* For a delete we need to find it in the inp */
3202		ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
3203	} else {
3204		ifa = NULL;
3205	}
3206	if (ifa != NULL) {
3207		if (type == SCTP_ADD_IP_ADDRESS) {
3208			sctp_add_local_addr_ep(inp, ifa, type);
3209		} else if (type == SCTP_DEL_IP_ADDRESS) {
3210			if (inp->laddr_count < 2) {
3211				/* can't delete the last local address */
3212				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
3213				return (EINVAL);
3214			}
3215			LIST_FOREACH(laddr, &inp->sctp_addr_list,
3216			    sctp_nxt_addr) {
3217				if (ifa == laddr->ifa) {
3218					/* Mark in the delete */
3219					laddr->action = type;
3220				}
3221			}
3222		}
3223		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
3224			/*
3225			 * There is no need to start the iterator if the inp
3226			 * has no associations.
3227			 */
3228			if (type == SCTP_DEL_IP_ADDRESS) {
3229				LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3230					if (laddr->ifa == ifa) {
3231						sctp_del_local_addr_ep(inp, ifa);
3232					}
3233				}
3234			}
3235		} else {
3236			struct sctp_asconf_iterator *asc;
3237			struct sctp_laddr *wi;
3238
3239			SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
3240			    sizeof(struct sctp_asconf_iterator),
3241			    SCTP_M_ASC_IT);
3242			if (asc == NULL) {
3243				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3244				return (ENOMEM);
3245			}
3246			wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3247			if (wi == NULL) {
3248				SCTP_FREE(asc, SCTP_M_ASC_IT);
3249				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
3250				return (ENOMEM);
3251			}
3252			LIST_INIT(&asc->list_of_work);
3253			asc->cnt = 1;
3254			SCTP_INCR_LADDR_COUNT();
3255			wi->ifa = ifa;
3256			wi->action = type;
3257			atomic_add_int(&ifa->refcount, 1);
3258			LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
3259			(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
3260			    sctp_asconf_iterator_stcb,
3261			    sctp_asconf_iterator_ep_end,
3262			    SCTP_PCB_ANY_FLAGS,
3263			    SCTP_PCB_ANY_FEATURES,
3264			    SCTP_ASOC_ANY_STATE,
3265			    (void *)asc, 0,
3266			    sctp_asconf_iterator_end, inp, 0);
3267		}
3268		return (0);
3269	} else {
3270		/* invalid address! */
3271		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
3272		return (EADDRNOTAVAIL);
3273	}
3274}
3275
3276void
3277sctp_asconf_send_nat_state_update(struct sctp_tcb *stcb,
3278    struct sctp_nets *net)
3279{
3280	struct sctp_asconf_addr *aa;
3281	struct sctp_ifa *sctp_ifap;
3282	struct sctp_asconf_tag_param *vtag;
3283
3284#ifdef INET
3285	struct sockaddr_in *to;
3286
3287#endif
3288#ifdef INET6
3289	struct sockaddr_in6 *to6;
3290
3291#endif
3292	if (net == NULL) {
3293		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing net\n");
3294		return;
3295	}
3296	if (stcb == NULL) {
3297		SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing stcb\n");
3298		return;
3299	}
3300	/*
3301	 * Need to have in the asconf: - vtagparam(my_vtag/peer_vtag) -
3302	 * add(0.0.0.0) - del(0.0.0.0) - Any global addresses add(addr)
3303	 */
3304	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3305	    SCTP_M_ASC_ADDR);
3306	if (aa == NULL) {
3307		/* didn't get memory */
3308		SCTPDBG(SCTP_DEBUG_ASCONF1,
3309		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3310		return;
3311	}
3312	aa->special_del = 0;
3313	/* fill in asconf address parameter fields */
3314	/* top level elements are "networked" during send */
3315	aa->ifa = NULL;
3316	aa->sent = 0;		/* clear sent flag */
3317	vtag = (struct sctp_asconf_tag_param *)&aa->ap.aph;
3318	vtag->aph.ph.param_type = SCTP_NAT_VTAGS;
3319	vtag->aph.ph.param_length = sizeof(struct sctp_asconf_tag_param);
3320	vtag->local_vtag = htonl(stcb->asoc.my_vtag);
3321	vtag->remote_vtag = htonl(stcb->asoc.peer_vtag);
3322	TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3323
3324	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3325	    SCTP_M_ASC_ADDR);
3326	if (aa == NULL) {
3327		/* didn't get memory */
3328		SCTPDBG(SCTP_DEBUG_ASCONF1,
3329		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3330		return;
3331	}
3332	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3333	/* fill in asconf address parameter fields */
3334	/* ADD(0.0.0.0) */
3335	switch (net->ro._l_addr.sa.sa_family) {
3336#ifdef INET
3337	case AF_INET:
3338		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3339		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3340		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3341		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3342		/* No need to add an address, we are using 0.0.0.0 */
3343		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3344		break;
3345#endif
3346#ifdef INET6
3347	case AF_INET6:
3348		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3349		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3350		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3351		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3352		/* No need to add an address, we are using 0.0.0.0 */
3353		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3354		break;
3355#endif
3356	}
3357	SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
3358	    SCTP_M_ASC_ADDR);
3359	if (aa == NULL) {
3360		/* didn't get memory */
3361		SCTPDBG(SCTP_DEBUG_ASCONF1,
3362		    "sctp_asconf_send_nat_state_update: failed to get memory!\n");
3363		return;
3364	}
3365	memset(aa, 0, sizeof(struct sctp_asconf_addr));
3366	/* fill in asconf address parameter fields */
3367	/* ADD(0.0.0.0) */
3368	switch (net->ro._l_addr.sa.sa_family) {
3369#ifdef INET
3370	case AF_INET:
3371		aa->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
3372		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
3373		aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
3374		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv4addr_param);
3375		/* No need to add an address, we are using 0.0.0.0 */
3376		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3377		break;
3378#endif
3379#ifdef INET6
3380	case AF_INET6:
3381		aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
3382		aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
3383		aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
3384		aa->ap.addrp.ph.param_length = sizeof(struct sctp_ipv6addr_param);
3385		/* No need to add an address, we are using 0.0.0.0 */
3386		TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
3387		break;
3388#endif
3389	}
3390	/* Now we must hunt the addresses and add all global addresses */
3391	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3392		struct sctp_vrf *vrf = NULL;
3393		struct sctp_ifn *sctp_ifnp;
3394		uint32_t vrf_id;
3395
3396		vrf_id = stcb->sctp_ep->def_vrf_id;
3397		vrf = sctp_find_vrf(vrf_id);
3398		if (vrf == NULL) {
3399			goto skip_rest;
3400		}
3401		SCTP_IPI_ADDR_RLOCK();
3402		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3403			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3404				switch (sctp_ifap->address.sa.sa_family) {
3405#ifdef INET
3406				case AF_INET:
3407					to = &sctp_ifap->address.sin;
3408					if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
3409					    &to->sin_addr) != 0) {
3410						continue;
3411					}
3412					if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3413						continue;
3414					}
3415					if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3416						continue;
3417					}
3418					break;
3419#endif
3420#ifdef INET6
3421				case AF_INET6:
3422					to6 = &sctp_ifap->address.sin6;
3423					if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
3424					    &to6->sin6_addr) != 0) {
3425						continue;
3426					}
3427					if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3428						continue;
3429					}
3430					if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3431						continue;
3432					}
3433					break;
3434#endif
3435				default:
3436					continue;
3437				}
3438				sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3439			}
3440		}
3441		SCTP_IPI_ADDR_RUNLOCK();
3442	} else {
3443		struct sctp_laddr *laddr;
3444
3445		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
3446			if (laddr->ifa == NULL) {
3447				continue;
3448			}
3449			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3450				/*
3451				 * Address being deleted by the system, dont
3452				 * list.
3453				 */
3454				continue;
3455			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3456				/*
3457				 * Address being deleted on this ep don't
3458				 * list.
3459				 */
3460				continue;
3461			}
3462			sctp_ifap = laddr->ifa;
3463			switch (sctp_ifap->address.sa.sa_family) {
3464#ifdef INET
3465			case AF_INET:
3466				to = &sctp_ifap->address.sin;
3467				if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
3468					continue;
3469				}
3470				if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3471					continue;
3472				}
3473				break;
3474#endif
3475#ifdef INET6
3476			case AF_INET6:
3477				to6 = &sctp_ifap->address.sin6;
3478				if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3479					continue;
3480				}
3481				if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3482					continue;
3483				}
3484				break;
3485#endif
3486			default:
3487				continue;
3488			}
3489			sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
3490		}
3491	}
3492skip_rest:
3493	/* Now we must send the asconf into the queue */
3494	sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
3495}
3496