Remove mdoc.local, we need to make this truly local.
[dragonfly.git] / sys / netinet / sctp_asconf.c
1 /*      $KAME: sctp_asconf.c,v 1.23 2004/08/17 06:28:01 t-momose Exp $  */
2 /*      $DragonFly: src/sys/netinet/sctp_asconf.c,v 1.3 2005/07/15 17:19:28 eirikn Exp $        */
3
4 /*
5  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 #if !(defined(__OpenBSD__) || defined(__APPLE__))
33 #include "opt_ipsec.h"
34 #endif
35 #if defined(__FreeBSD__)
36 #include "opt_compat.h"
37 #include "opt_inet6.h"
38 #include "opt_inet.h"
39 #endif
40 #if defined(__NetBSD__)
41 #include "opt_inet.h"
42 #endif
43
44 #ifdef __APPLE__
45 #include <sctp.h>
46 #elif !defined(__OpenBSD__)
47 #include "opt_sctp.h"
48 #endif
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/kernel.h>
57 #include <sys/sysctl.h>
58 #include <sys/thread2.h>
59
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70
71 #ifdef INET6
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #if defined(__FreeBSD__) || (__NetBSD__)
75 #include <netinet6/in6_pcb.h>
76 #elif defined(__OpenBSD__)
77 #include <netinet/in_pcb.h>
78 #endif
79 #include <netinet/icmp6.h>
80 #include <netinet6/nd6.h>
81 #endif /* INET6 */
82
83 #include <netinet/in_pcb.h>
84
85 #include <netinet/sctp_var.h>
86 #include <netinet/sctp_pcb.h>
87 #include <netinet/sctp_header.h>
88 #include <netinet/sctputil.h>
89 #include <netinet/sctp_output.h>
90 #include <netinet/sctp_asconf.h>
91
92 /*
93  * debug flags:
94  *   SCTP_DEBUG_ASCONF1: protocol info, general info and errors
95  *   SCTP_DEBUG_ASCONF2: detailed info
96  */
97 #ifdef SCTP_DEBUG
98 extern u_int32_t sctp_debug_on;
99
100 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
101 #define strlcpy strncpy
102 #endif
103 #endif /* SCTP_DEBUG */
104
105 /*
106  * draft-ietf-tsvwg-addip-sctp
107  *
108  * Address management only currently supported
109  * For the bound all case:
110  *      the asoc local addr list is always a "DO NOT USE" list
111  * For the subset bound case:
112  *      If ASCONFs are allowed:
113  *              the endpoint local addr list is the usable address list
114  *              the asoc local addr list is the "DO NOT USE" list
115  *      If ASCONFs are not allowed:
116  *              the endpoint local addr list is the default usable list
117  *              the asoc local addr list is the usable address list
118  *
119  * An ASCONF parameter queue exists per asoc which holds the pending
120  * address operations.  Lists are updated upon receipt of ASCONF-ACK.
121  *
122  * Deleted addresses are always immediately removed from the lists as
123  * they will (shortly) no longer exist in the kernel.  We send ASCONFs
124  * as a courtesy, only if allowed.
125  */
126
127 /*
128  * ASCONF parameter processing
129  * response_required: set if a reply is required (eg. SUCCESS_REPORT)
130  * returns a mbuf to an "error" response parameter or NULL/"success" if ok
131  * FIX: allocating this many mbufs on the fly is pretty inefficient...
132  */
133
134 static struct mbuf *
135 sctp_asconf_success_response(uint32_t id)
136 {
137         struct mbuf *m_reply = NULL;
138         struct sctp_asconf_paramhdr *aph;
139
140         MGET(m_reply, MB_DONTWAIT, MT_DATA);
141         if (m_reply == NULL) {
142 #ifdef SCTP_DEBUG
143                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
144                         printf("asconf_success_response: couldn't get mbuf!\n");
145                 }
146 #endif /* SCTP_DEBUG */
147                 return NULL;
148         }
149         aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
150         aph->correlation_id = id;
151         aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
152         aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
153         m_reply->m_len = aph->ph.param_length;
154         aph->ph.param_length = htons(aph->ph.param_length);
155
156         return m_reply;
157 }
158
159 static struct mbuf *
160 sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t *error_tlv,
161     uint16_t tlv_length)
162 {
163         struct mbuf *m_reply = NULL;
164         struct sctp_asconf_paramhdr *aph;
165         struct sctp_error_cause *error;
166         uint8_t *tlv;
167
168         MGET(m_reply, MB_DONTWAIT, MT_DATA);
169         if (m_reply == NULL) {
170 #ifdef SCTP_DEBUG
171                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
172                         printf("asconf_error_response: couldn't get mbuf!\n");
173                 }
174 #endif /* SCTP_DEBUG */
175                 return NULL;
176         }
177         aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
178         error = (struct sctp_error_cause *)(aph + 1);
179
180         aph->correlation_id = id;
181         aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
182         error->code = htons(cause);
183         error->length = tlv_length + sizeof(struct sctp_error_cause);
184         aph->ph.param_length = error->length +
185             sizeof(struct sctp_asconf_paramhdr);
186
187         if (aph->ph.param_length > MLEN) {
188 #ifdef SCTP_DEBUG
189                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
190                         printf("asconf_error_response: tlv_length (%xh) too big\n",
191                             tlv_length);
192                 }
193 #endif /* SCTP_DEBUG */
194                 sctp_m_freem(m_reply);  /* discard */
195                 return NULL;
196         }
197
198         if (error_tlv != NULL) {
199                 tlv = (uint8_t *)(error + 1);
200                 memcpy(tlv, error_tlv, tlv_length);
201         }
202
203         m_reply->m_len = aph->ph.param_length;
204         error->length = htons(error->length);
205         aph->ph.param_length = htons(aph->ph.param_length);
206
207         return m_reply;
208 }
209
210 static struct mbuf *
211 sctp_process_asconf_add_ip(struct sctp_asconf_paramhdr *aph,
212     struct sctp_tcb *stcb, int response_required)
213 {
214         struct mbuf *m_reply = NULL;
215         struct sockaddr_storage sa_store;
216         struct sctp_ipv4addr_param *v4addr;
217         uint16_t param_type, param_length, aparam_length;
218         struct sockaddr *sa;
219         struct sockaddr_in *sin;
220 #ifdef INET6
221         struct sockaddr_in6 *sin6;
222         struct sctp_ipv6addr_param *v6addr;
223 #endif /* INET6 */
224
225         aparam_length = ntohs(aph->ph.param_length);
226         v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
227 #ifdef INET6
228         v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
229 #endif /* INET6 */
230         param_type = ntohs(v4addr->ph.param_type);
231         param_length = ntohs(v4addr->ph.param_length);
232
233         sa = (struct sockaddr *)&sa_store;
234         switch (param_type) {
235         case SCTP_IPV4_ADDRESS:
236                 if (param_length != sizeof(struct sctp_ipv4addr_param)) {
237                         /* invalid param size */
238                         return NULL;
239                 }
240                 sin = (struct sockaddr_in *)&sa_store;
241                 bzero(sin, sizeof(*sin));
242                 sin->sin_family = AF_INET;
243                 sin->sin_len = sizeof(struct sockaddr_in);
244                 sin->sin_port = stcb->rport;
245                 sin->sin_addr.s_addr = v4addr->addr;
246 #ifdef SCTP_DEBUG
247                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
248                         printf("process_asconf_add_ip: adding ");
249                         sctp_print_address(sa);
250                 }
251 #endif /* SCTP_DEBUG */
252                 break;
253         case SCTP_IPV6_ADDRESS:
254 #ifdef INET6
255                 if (param_length != sizeof(struct sctp_ipv6addr_param)) {
256                         /* invalid param size */
257                         return NULL;
258                 }
259                 sin6 = (struct sockaddr_in6 *)&sa_store;
260                 bzero(sin6, sizeof(*sin6));
261                 sin6->sin6_family = AF_INET6;
262                 sin6->sin6_len = sizeof(struct sockaddr_in6);
263                 sin6->sin6_port = stcb->rport;
264                 memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
265                     sizeof(struct in6_addr));
266 #ifdef SCTP_DEBUG
267                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
268                         printf("process_asconf_add_ip: adding ");
269                         sctp_print_address(sa);
270                 }
271 #endif /* SCTP_DEBUG */
272 #else
273                 /* IPv6 not enabled! */
274                 /* FIX ME: currently sends back an invalid param error */
275                 m_reply = sctp_asconf_error_response(aph->correlation_id,
276                     SCTP_ERROR_INVALID_PARAM, (uint8_t *)aph, aparam_length);
277 #ifdef SCTP_DEBUG
278                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
279                         printf("process_asconf_add_ip: v6 disabled- skipping ");
280                         sctp_print_address(sa);
281                 }
282 #endif /* SCTP_DEBUG */
283                 return m_reply;
284 #endif /* INET6 */
285                 break;
286         default:
287                 m_reply = sctp_asconf_error_response(aph->correlation_id,
288                     SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
289                     aparam_length);
290                 return m_reply;
291         } /* end switch */
292
293         /* add the address */
294         if (sctp_add_remote_addr(stcb, sa, 0, 6) != 0) {
295 #ifdef SCTP_DEBUG
296                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
297                         printf("process_asconf_add_ip: error adding address\n");
298                 }
299 #endif /* SCTP_DEBUG */
300                 m_reply = sctp_asconf_error_response(aph->correlation_id,
301                     SCTP_ERROR_RESOURCE_SHORTAGE, (uint8_t *)aph,
302                     aparam_length);
303         } else {
304                 /* notify upper layer */
305                 sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa);
306                 if (response_required) {
307                         m_reply =
308                             sctp_asconf_success_response(aph->correlation_id);
309                 }
310         }
311
312         return m_reply;
313 }
314
315 static struct mbuf *
316 sctp_process_asconf_delete_ip(struct mbuf *m, struct sctp_asconf_paramhdr *aph,
317     struct sctp_tcb *stcb, int response_required)
318 {
319         struct mbuf *m_reply = NULL;
320         struct sockaddr_storage sa_store, sa_source;
321         struct sctp_ipv4addr_param *v4addr;
322         uint16_t param_type, param_length, aparam_length;
323         struct sockaddr *sa;
324         struct sockaddr_in *sin;
325         struct ip *iph;
326         int result;
327 #ifdef INET6
328         struct sockaddr_in6 *sin6;
329         struct sctp_ipv6addr_param *v6addr;
330 #endif /* INET6 */
331
332         aparam_length = ntohs(aph->ph.param_length);
333         v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
334 #ifdef INET6
335         v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
336 #endif /* INET6 */
337         param_type = ntohs(v4addr->ph.param_type);
338         param_length = ntohs(v4addr->ph.param_length);
339
340         /* get the source IP address for deletion check */
341         iph = mtod(m, struct ip *);
342         if (iph->ip_v == IPVERSION) {
343                 /* IPv4 source */
344                 sin = (struct sockaddr_in *)&sa_source;
345                 bzero(sin, sizeof(*sin));
346                 sin->sin_family = AF_INET;
347                 sin->sin_len = sizeof(struct sockaddr_in);
348                 sin->sin_port = stcb->rport;
349                 sin->sin_addr.s_addr = iph->ip_src.s_addr;
350         }
351 #ifdef INET6
352         else if (iph->ip_v == (IPV6_VERSION >> 4)) {
353                 /* IPv6 source */
354                 struct ip6_hdr *ip6;
355
356                 sin6 = (struct sockaddr_in6 *)&sa_source;
357                 bzero(sin6, sizeof(*sin6));
358                 sin6->sin6_family = AF_INET6;
359                 sin6->sin6_len = sizeof(struct sockaddr_in6);
360                 sin6->sin6_port = stcb->rport;
361                 ip6 = mtod(m, struct ip6_hdr *);
362                 sin6->sin6_addr = ip6->ip6_src;
363         }
364 #endif /* INET6 */
365         else
366                 return NULL;
367
368         sa = (struct sockaddr *)&sa_store;
369         switch (param_type) {
370         case SCTP_IPV4_ADDRESS:
371                 if (param_length != sizeof(struct sctp_ipv4addr_param)) {
372                         /* invalid param size */
373                         return NULL;
374                 }
375                 sin = (struct sockaddr_in *)&sa_store;
376                 bzero(sin, sizeof(*sin));
377                 sin->sin_family = AF_INET;
378                 sin->sin_len = sizeof(struct sockaddr_in);
379                 sin->sin_port = stcb->rport;
380                 sin->sin_addr.s_addr = v4addr->addr;
381 #ifdef SCTP_DEBUG
382                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
383                         printf("process_asconf_delete_ip: deleting ");
384                         sctp_print_address(sa);
385                 }
386 #endif /* SCTP_DEBUG */
387                 break;
388         case SCTP_IPV6_ADDRESS:
389                 if (param_length != sizeof(struct sctp_ipv6addr_param)) {
390                         /* invalid param size */
391                         return NULL;
392                 }
393 #ifdef INET6
394                 sin6 = (struct sockaddr_in6 *)&sa_store;
395                 bzero(sin6, sizeof(*sin6));
396                 sin6->sin6_family = AF_INET6;
397                 sin6->sin6_len = sizeof(struct sockaddr_in6);
398                 sin6->sin6_port = stcb->rport;
399                 memcpy(&sin6->sin6_addr, v6addr->addr,
400                     sizeof(struct in6_addr));
401 #ifdef SCTP_DEBUG
402                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
403                         printf("process_asconf_delete_ip: deleting ");
404                         sctp_print_address(sa);
405                 }
406 #endif /* SCTP_DEBUG */
407 #else
408                 /* IPv6 not enabled!  No "action" needed; just ack it */
409 #ifdef SCTP_DEBUG
410                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
411                         printf("process_asconf_delete_ip: v6 disabled- ignoring: ");
412                         sctp_print_address(sa);
413                 }
414 #endif /* SCTP_DEBUG */
415                 /* just respond with a "success" ASCONF-ACK */
416                 return NULL;
417 #endif /* INET6 */
418                 break;
419         default:
420                 m_reply = sctp_asconf_error_response(aph->correlation_id,
421                     SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
422                     aparam_length);
423                 return m_reply;
424         } /* end switch */
425
426         /* make sure the source address is not being deleted */
427         if ((memcmp(sa, &sa_source, sizeof(struct sockaddr_in)) == 0)
428 #ifdef INET6
429             || (memcmp(sa, &sa_source, sizeof(struct sockaddr_in6)) == 0)
430 #endif /* INET6 */
431                 ) {
432                 /* trying to delete the source address! */
433 #ifdef SCTP_DEBUG
434                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
435                         printf("process_asconf_delete_ip: tried to delete source addr\n");
436                 }
437 #endif /* SCTP_DEBUG */
438                 m_reply = sctp_asconf_error_response(aph->correlation_id,
439                     SCTP_ERROR_DELETE_SOURCE_ADDR, (uint8_t *)aph,
440                     aparam_length);
441                 return m_reply;
442         }
443
444         /* delete the address */
445         result = sctp_del_remote_addr(stcb, sa);
446         /*
447          * note if result == -2, the address doesn't exist in the asoc
448          * but since it's being deleted anyways, we just ack the delete
449          * -- but this probably means something has already gone awry
450          */
451         if (result == -1) {
452                 /* only one address in the asoc */
453 #ifdef SCTP_DEBUG
454                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
455                         printf("process_asconf_delete_ip: tried to delete last IP addr!\n");
456                 }
457 #endif /* SCTP_DEBUG */
458                 m_reply = sctp_asconf_error_response(aph->correlation_id,
459                     SCTP_ERROR_DELETE_LAST_ADDR, (uint8_t *)aph,
460                     aparam_length);
461         } else {
462                 /* notify upper layer */
463                 sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa);
464         }
465
466         if (response_required) {
467                 m_reply = sctp_asconf_success_response(aph->correlation_id);
468         }
469         return m_reply;
470 }
471
472 static struct mbuf *
473 sctp_process_asconf_set_primary(struct sctp_asconf_paramhdr *aph,
474     struct sctp_tcb *stcb, int response_required)
475 {
476         struct mbuf *m_reply = NULL;
477         struct sockaddr_storage sa_store;
478         struct sctp_ipv4addr_param *v4addr;
479         uint16_t param_type, param_length, aparam_length;
480         struct sockaddr *sa;
481         struct sockaddr_in *sin;
482 #ifdef INET6
483         struct sockaddr_in6 *sin6;
484         struct sctp_ipv6addr_param *v6addr;
485 #endif /* INET6 */
486
487         aparam_length = ntohs(aph->ph.param_length);
488         v4addr = (struct sctp_ipv4addr_param *)(aph + 1);
489 #ifdef INET6
490         v6addr = (struct sctp_ipv6addr_param *)(aph + 1);
491 #endif /* INET6 */
492         param_type = ntohs(v4addr->ph.param_type);
493         param_length = ntohs(v4addr->ph.param_length);
494
495         sa = (struct sockaddr *)&sa_store;
496         switch (param_type) {
497         case SCTP_IPV4_ADDRESS:
498                 if (param_length != sizeof(struct sctp_ipv4addr_param)) {
499                         /* invalid param size */
500                         return NULL;
501                 }
502                 sin = (struct sockaddr_in *)&sa_store;
503                 bzero(sin, sizeof(*sin));
504                 sin->sin_family = AF_INET;
505                 sin->sin_len = sizeof(struct sockaddr_in);
506                 sin->sin_addr.s_addr = v4addr->addr;
507 #ifdef SCTP_DEBUG
508                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
509                         printf("process_asconf_set_primary: ");
510                         sctp_print_address(sa);
511                 }
512 #endif /* SCTP_DEBUG */
513                 break;
514         case SCTP_IPV6_ADDRESS:
515                 if (param_length != sizeof(struct sctp_ipv6addr_param)) {
516                         /* invalid param size */
517                         return NULL;
518                 }
519 #ifdef INET6
520                 sin6 = (struct sockaddr_in6 *)&sa_store;
521                 bzero(sin6, sizeof(*sin6));
522                 sin6->sin6_family = AF_INET6;
523                 sin6->sin6_len = sizeof(struct sockaddr_in6);
524                 memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
525                     sizeof(struct in6_addr));
526 #ifdef SCTP_DEBUG
527                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
528                         printf("process_asconf_set_primary: ");
529                         sctp_print_address(sa);
530                 }
531 #endif /* SCTP_DEBUG */
532 #else
533                 /* IPv6 not enabled!  No "action" needed; just ack it */
534 #ifdef SCTP_DEBUG
535                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
536                         printf("process_asconf_set_primary: v6 disabled- ignoring: ");
537                         sctp_print_address(sa);
538                 }
539 #endif /* SCTP_DEBUG */
540                 /* just respond with a "success" ASCONF-ACK */
541                 return NULL;
542 #endif /* INET6 */
543                 break;
544         default:
545                 m_reply = sctp_asconf_error_response(aph->correlation_id,
546                     SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
547                     aparam_length);
548                 return m_reply;
549         } /* end switch */
550
551         /* set the primary address */
552         if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
553 #ifdef SCTP_DEBUG
554                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
555                         printf("process_asconf_set_primary: primary address set\n");
556                 }
557 #endif /* SCTP_DEBUG */
558                 /* notify upper layer */
559                 sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa);
560
561                 if (response_required) {
562                         m_reply = sctp_asconf_success_response(aph->correlation_id);
563                 }
564         } else {
565                 /* couldn't set the requested primary address! */
566 #ifdef SCTP_DEBUG
567                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
568                         printf("process_asconf_set_primary: set primary failed!\n");
569                 }
570 #endif /* SCTP_DEBUG */
571                 /* must have been an invalid address, so report */
572                 m_reply = sctp_asconf_error_response(aph->correlation_id,
573                     SCTP_ERROR_UNRESOLVABLE_ADDR, (uint8_t *)aph,
574                     aparam_length);
575         }
576
577         return m_reply;
578 }
579
580 /*
581  * handles an ASCONF chunk
582  * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
583  */
584 void
585 sctp_handle_asconf(struct mbuf *m, unsigned int offset, struct sctp_asconf_chunk *cp,
586     struct sctp_tcb *stcb, struct sctp_nets *net)
587 {
588         struct sctp_association *asoc;
589         uint32_t serial_num;
590         struct mbuf *m_ack, *m_result, *m_tail;
591         struct sctp_asconf_ack_chunk *ack_cp;
592         struct sctp_asconf_paramhdr *aph, *ack_aph;
593         struct sctp_ipv6addr_param *p_addr;
594         unsigned int asconf_limit;
595         int error = 0;          /* did an error occur? */
596         /* asconf param buffer */
597         static u_int8_t aparam_buf[DEFAULT_PARAM_BUFFER];
598
599         /* verify minimum length */
600         if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
601 #ifdef SCTP_DEBUG
602                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
603                         printf("handle_asconf: chunk too small = %xh\n",
604                             ntohs(cp->ch.chunk_length));
605                 }
606 #endif /* SCTP_DEBUG */
607                 return;
608         }
609
610         asoc = &stcb->asoc;
611         serial_num = ntohl(cp->serial_number);
612
613         if (serial_num == asoc->asconf_seq_in) {
614                 /* got a duplicate ASCONF */
615 #ifdef SCTP_DEBUG
616                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
617                         printf("handle_asconf: got duplicate serial number = %xh\n",
618                                serial_num);
619                 }
620 #endif /* SCTP_DEBUG */
621                 /* resend last ASCONF-ACK... */
622                 sctp_send_asconf_ack(stcb, 1);
623                 return;
624         } else if (serial_num != (asoc->asconf_seq_in + 1)) {
625 #ifdef SCTP_DEBUG
626                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
627                         printf("handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
628                             serial_num, asoc->asconf_seq_in+1);
629                 }
630 #endif /* SCTP_DEBUG */
631                 return;
632         }
633
634         /* it's the expected "next" sequence number, so process it */
635         asoc->asconf_seq_in = serial_num;       /* update sequence */
636         /* get length of all the param's in the ASCONF */
637         asconf_limit = offset + ntohs(cp->ch.chunk_length);
638 #ifdef SCTP_DEBUG
639         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
640                 printf("handle_asconf: asconf_limit=%u, sequence=%xh\n",
641                     asconf_limit, serial_num);
642         }
643 #endif /* SCTP_DEBUG */
644         if (asoc->last_asconf_ack_sent != NULL) {
645                 /* free last ASCONF-ACK message sent */
646                 sctp_m_freem(asoc->last_asconf_ack_sent);
647                 asoc->last_asconf_ack_sent = NULL;
648         }
649         MGETHDR(m_ack, MB_DONTWAIT, MT_DATA);
650         if (m_ack == NULL) {
651 #ifdef SCTP_DEBUG
652                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
653                         printf("handle_asconf: couldn't get mbuf!\n");
654                 }
655 #endif /* SCTP_DEBUG */
656                 return;
657         }
658         m_tail = m_ack;         /* current reply chain's tail */
659
660         /* fill in ASCONF-ACK header */
661         ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
662         ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
663         ack_cp->ch.chunk_flags = 0;
664         ack_cp->serial_number = htonl(serial_num);
665         /* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
666         m_ack->m_len = sizeof(struct sctp_asconf_ack_chunk);
667         ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
668         m_ack->m_pkthdr.len = sizeof(struct sctp_asconf_ack_chunk);
669
670         /* skip the lookup address parameter */
671         offset += sizeof(struct sctp_asconf_chunk);
672         p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *)&aparam_buf);
673         if (p_addr == NULL) {
674 #ifdef SCTP_DEBUG
675                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
676                         printf("handle_asconf: couldn't get lookup addr!\n");
677                 }
678 #endif /* SCTP_DEBUG */
679
680                 /* respond with a missing/invalid mandatory parameter error */
681                 return;
682         }
683         /* param_length is already validated in process_control... */
684         offset += ntohs(p_addr->ph.param_length);   /* skip lookup addr */
685
686         /* get pointer to first asconf param in ASCONF */
687         aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *)&aparam_buf);
688         /* get pointer to first asconf param in ASCONF-ACK */
689         if (aph == NULL) {
690                 printf("Gak in asconf\n");
691                 return;
692         }
693         ack_aph = (struct sctp_asconf_paramhdr *)(mtod(m_ack, caddr_t) + sizeof(struct sctp_asconf_ack_chunk));
694         if (ack_aph == NULL) {
695                 printf("Gak in asconf2\n");
696                 return;
697         }
698
699         /* process through all parameters */
700         while (aph != NULL) {
701                 unsigned int param_length, param_type;
702
703                 param_type = ntohs(aph->ph.param_type);
704                 param_length = ntohs(aph->ph.param_length);
705                 if (offset + param_length > asconf_limit) {
706                         /* parameter goes beyond end of chunk! */
707                         sctp_m_freem(m_ack);
708                         return;
709                 }
710                 m_result = NULL;
711
712                 if (param_length > sizeof(aparam_buf)) {
713 #ifdef SCTP_DEBUG
714                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
715                                 printf("handle_asconf: param length (%u) larger than buffer size!\n", param_length);
716                         }
717 #endif /* SCTP_DEBUG */
718                         sctp_m_freem(m_ack);
719                         return;
720                 }
721                 if (param_length <= sizeof(struct sctp_paramhdr)) {
722 #ifdef SCTP_DEBUG
723                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
724                                 printf("handle_asconf: param length (%u) too short\n", param_length);
725                         }
726 #endif /* SCTP_DEBUG */
727                         sctp_m_freem(m_ack);
728                 }
729
730                 /* get the entire parameter */
731                 aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
732                 if (aph == NULL) {
733                         printf("Gag\n");
734                         sctp_m_freem(m_ack);
735                         return;
736                 }
737                 switch (param_type) {
738                 case SCTP_ADD_IP_ADDRESS:
739                         asoc->peer_supports_asconf = 1;
740                         m_result = sctp_process_asconf_add_ip(aph, stcb, error);
741                         break;
742                 case SCTP_DEL_IP_ADDRESS:
743                         asoc->peer_supports_asconf = 1;
744                         m_result = sctp_process_asconf_delete_ip(m, aph, stcb,
745                             error);
746                         break;
747                 case SCTP_ERROR_CAUSE_IND:
748                         /* not valid in an ASCONF chunk */
749                         break;
750                 case SCTP_SET_PRIM_ADDR:
751                         asoc->peer_supports_asconf_setprim = 1;
752                         m_result = sctp_process_asconf_set_primary(aph, stcb,
753                             error);
754                         break;
755                 case SCTP_SUCCESS_REPORT:
756                         /* not valid in an ASCONF chunk */
757                         break;
758                 case SCTP_ULP_ADAPTION:
759                         /* FIX */
760                         break;
761                 default:
762                         if ((param_type & 0x8000) == 0) {
763                                 /* Been told to STOP at this param */
764                                 asconf_limit = offset;
765                                 /* FIX FIX - We need to call sctp_arethere_unrecognized_parameters()
766                                  * to get a operr and send it for any param's with the
767                                  * 0x4000 bit set OR do it here ourselves... note we still
768                                  * must STOP if the 0x8000 bit is clear.
769                                  */
770                         }
771                         /* unknown/invalid param type */
772                         break;
773                 } /* switch */
774
775                 /* add any (error) result to the reply mbuf chain */
776                 if (m_result != NULL) {
777 #ifdef SCTP_DEBUG
778                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
779                                 printf("handle_asconf: adding reply...\n");
780                         }
781 #endif /* SCTP_DEBUG */
782                         m_tail->m_next = m_result;
783                         m_tail = m_result;
784                         /* update lengths, make sure it's aligned too */
785                         m_result->m_len = SCTP_SIZE32(m_result->m_len);
786                         m_ack->m_pkthdr.len += m_result->m_len;
787                         ack_cp->ch.chunk_length += m_result->m_len;
788                         /* set flag to force success reports */
789                         error = 1;
790                 }
791
792                 offset += SCTP_SIZE32(param_length);
793                 /* update remaining ASCONF message length to process */
794                 if (offset >= asconf_limit) {
795                         /* no more data in the mbuf chain */
796                         break;
797                 }
798                 /* get pointer to next asconf param */
799                 aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
800                     sizeof(struct sctp_asconf_paramhdr),
801                     (uint8_t *)&aparam_buf);
802                 if (aph == NULL) {
803                         /* can't get an asconf paramhdr */
804 #ifdef SCTP_DEBUG
805                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
806                                 printf("handle_asconf: can't get asconf param hdr!\n");
807                         }
808 #endif /* SCTP_DEBUG */
809                         /* FIX ME - add error here... */
810                 }
811         } /* while */
812
813         ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
814         /* save the ASCONF-ACK reply */
815         asoc->last_asconf_ack_sent = m_ack;
816         /* and send (a new one) it out... */
817         sctp_send_asconf_ack(stcb, 0);
818 }
819
820 /*
821  * does the address match?
822  * returns 0 if not, 1 if so
823  */
824 static uint32_t
825 sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
826 {
827 #ifdef INET6
828         if (sa->sa_family == AF_INET6) {
829                 /* IPv6 sa address */
830                 /* XXX scopeid */
831                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
832                 if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
833                     (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
834                             sizeof(struct in6_addr)) == 0)) {
835                         return (1);
836                 }
837         } else
838 #endif /* INET6 */
839         if (sa->sa_family == AF_INET) {
840                 /* IPv4 sa address */
841                 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
842                 if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
843                     (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
844                             sizeof(struct in_addr)) == 0)) {
845                         return (1);
846                 }
847         }
848         return (0);
849 }
850
851 /*
852  * Cleanup for non-responded/OP ERR'd ASCONF
853  */
854 void
855 sctp_asconf_cleanup(struct sctp_tcb *stcb, struct sctp_nets *net)
856 {
857 #ifdef SCTP_DEBUG
858         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
859                 printf("asconf_cleanup: marking peer ASCONF incapable and cleaning up\n");
860         }
861 #endif /* SCTP_DEBUG */
862         /* mark peer as ASCONF incapable */
863         stcb->asoc.peer_supports_asconf = 0;
864         stcb->asoc.peer_supports_asconf_setprim = 0;
865         /*
866          * clear out any existing asconfs going out
867          */
868         sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net);
869         stcb->asoc.asconf_seq_out++;
870         /* remove the old ASCONF on our outbound queue */
871         sctp_toss_old_asconf(stcb);
872 }
873
874 /*
875  * process an ADD/DELETE IP ack from peer
876  * ifa:  corresponding ifaddr to the address being added/deleted
877  * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS
878  * flag: 1=success, 0=failure
879  */
880 static void
881 sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct ifaddr *addr,
882     uint16_t type, uint32_t flag)
883 {
884
885         /*
886          * do the necessary asoc list work-
887          * if we get a failure indication, leave the address on the
888          *   "do not use" asoc list
889          * if we get a success indication, remove the address from
890          *   the list
891          */
892         /*
893          * Note: this will only occur for ADD_IP_ADDRESS, since
894          * DEL_IP_ADDRESS is never actually added to the list...
895          */
896         if (flag) {
897                 /* success case, so remove from the list */
898                 sctp_del_local_addr_assoc(stcb, addr);
899         }
900         /* else, leave it on the list */
901 }
902
903 /*
904  * add an asconf add/delete IP address parameter to the queue
905  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR
906  * returns 0 if completed, non-zero if not completed
907  * NOTE: if adding, but delete already scheduled (and not yet
908  *      sent out), simply remove from queue.  Same for deleting
909  *      an address already scheduled for add.  If a duplicate
910  *      operation is found, ignore the new one.
911  */
912 static uint32_t
913 sctp_asconf_queue_add(struct sctp_tcb *stcb, struct ifaddr *ifa, uint16_t type)
914 {
915         struct sctp_asconf_addr *aa, *aa_next;
916 #ifdef SCTP_DEBUG
917         char buf[128];  /* for address in string format */
918 #endif /* SCTP_DEBUG */
919
920         /* see if peer supports ASCONF */
921         if (stcb->asoc.peer_supports_asconf == 0) {
922 #ifdef SCTP_DEBUG
923                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
924                         printf("asconf_queue_add: peer doesn't support ASCONF\n");
925                 }
926 #endif /* SCTP_DEBUG */
927                 return (-1);
928         }
929
930         /* make sure the request isn't already in the queue */
931         for (aa=TAILQ_FIRST(&stcb->asoc.asconf_queue); aa!=NULL; aa=aa_next) {
932                 aa_next = TAILQ_NEXT(aa, next);
933                 /* address match? */
934                 if (sctp_asconf_addr_match(aa, ifa->ifa_addr) == 0)
935                         continue;
936                 /* is the request already in queue (sent or not) */
937                 if (aa->ap.aph.ph.param_type == type) {
938 #ifdef SCTP_DEBUG
939                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
940                                 printf("asconf_queue_add: request already exists\n");
941                         }
942 #endif /* SCTP_DEBUG */
943                         return (-1);
944                 }
945                 /* is the negative request already in queue, and not sent */
946                 if (aa->sent == 0 &&
947                     /* add requested, delete already queued */
948                     ((type == SCTP_ADD_IP_ADDRESS &&
949                       aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) ||
950                      /* delete requested, add already queued */
951                      (type == SCTP_DEL_IP_ADDRESS &&
952                       aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS))) {
953                         /* delete the existing entry in the queue */
954                         TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
955                         /* take the entry off the appropriate list */
956                         sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
957                         /* free the entry */
958                         FREE(aa, M_PCB);
959
960 #ifdef SCTP_DEBUG
961                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
962                                 printf("asconf_queue_add: removing 'opposite' queued request\n");
963                         }
964 #endif /* SCTP_DEBUG */
965                         return (-1);
966                 }
967         } /* for each aa */
968
969         /* adding new request to the queue */
970         MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa), M_PCB, M_NOWAIT);
971         if (aa == NULL) {
972                 /* didn't get memory */
973 #ifdef SCTP_DEBUG
974                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
975                         printf("asconf_queue_add: failed to get memory!\n");
976                 }
977 #endif /* SCTP_DEBUG */
978                 return (-1);
979         }
980         /* fill in asconf address parameter fields */
981         /* top level elements are "networked" during send */
982         aa->ap.aph.ph.param_type = type;
983         aa->ifa = ifa;
984         /* correlation_id filled in during send routine later... */
985         if (ifa->ifa_addr->sa_family == AF_INET6) {
986                 /* IPv6 address */
987                 struct sockaddr_in6 *sin6;
988
989                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
990                 aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
991                 aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
992                 aa->ap.aph.ph.param_length =
993                     sizeof(struct sctp_asconf_paramhdr) +
994                     sizeof(struct sctp_ipv6addr_param);
995                 memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
996                     sizeof(struct in6_addr));
997 #ifdef SCTP_DEBUG
998                 strlcpy(buf, ip6_sprintf(&sin6->sin6_addr), sizeof(buf));
999 #endif /* SCTP_DEBUG */
1000
1001         } else if (ifa->ifa_addr->sa_family == AF_INET) {
1002                 /* IPv4 address */
1003                 struct sockaddr_in *sin = (struct sockaddr_in *)ifa->ifa_addr;
1004                 aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1005                 aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1006                 aa->ap.aph.ph.param_length =
1007                     sizeof(struct sctp_asconf_paramhdr) +
1008                     sizeof(struct sctp_ipv4addr_param);
1009                 memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1010                     sizeof(struct in_addr));
1011 #ifdef SCTP_DEBUG
1012                 strlcpy(buf, inet_ntoa(sin->sin_addr), sizeof(buf));
1013 #endif /* SCTP_DEBUG */
1014         } else {
1015                 /* invalid family! */
1016                 return (-1);
1017         }
1018         aa->sent = 0;                   /* clear sent flag */
1019
1020         /*
1021          * if we are deleting an address it should go out last
1022          * otherwise, add it to front of the pending queue
1023          */
1024         if (type == SCTP_ADD_IP_ADDRESS) {
1025                 /* add goes to the front of the queue */
1026                 TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
1027 #ifdef SCTP_DEBUG
1028                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1029                         printf("asconf_queue_add: appended asconf ADD_IP_ADDRESS: %s\n", buf);
1030                 }
1031 #endif /* SCTP_DEBUG */
1032         } else {
1033                 /* delete and set primary goes to the back of the queue */
1034                 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1035 #ifdef SCTP_DEBUG
1036                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1037                         if (type == SCTP_DEL_IP_ADDRESS) {
1038                                 printf("asconf_queue_add: inserted asconf DEL_IP_ADDRESS: %s\n", buf);
1039                         } else {
1040                                 printf("asconf_queue_add: inserted asconf SET_PRIM_ADDR: %s\n", buf);
1041                         }
1042                 }
1043 #endif /* SCTP_DEBUG */
1044         }
1045
1046         return (0);
1047 }
1048
1049 /*
1050  * add an asconf add/delete IP address parameter to the queue by addr
1051  * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR
1052  * returns 0 if completed, non-zero if not completed
1053  * NOTE: if adding, but delete already scheduled (and not yet
1054  *      sent out), simply remove from queue.  Same for deleting
1055  *      an address already scheduled for add.  If a duplicate
1056  *      operation is found, ignore the new one.
1057  */
1058 static uint32_t
1059 sctp_asconf_queue_add_sa(struct sctp_tcb *stcb, struct sockaddr *sa,
1060     uint16_t type)
1061 {
1062         struct sctp_asconf_addr *aa, *aa_next;
1063
1064         /* see if peer supports ASCONF */
1065         if (stcb->asoc.peer_supports_asconf == 0) {
1066 #ifdef SCTP_DEBUG
1067                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1068                         printf("asconf_queue_add_sa: peer doesn't support ASCONF\n");
1069                 }
1070 #endif /* SCTP_DEBUG */
1071                 return (-1);
1072         }
1073
1074         /* make sure the request isn't already in the queue */
1075         for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1076             aa = aa_next) {
1077                 aa_next = TAILQ_NEXT(aa, next);
1078                 /* address match? */
1079                 if (sctp_asconf_addr_match(aa, sa) == 0)
1080                         continue;
1081                 /* is the request already in queue (sent or not) */
1082                 if (aa->ap.aph.ph.param_type == type) {
1083 #ifdef SCTP_DEBUG
1084                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1085                                 printf("asconf_queue_add_sa: request already exists\n");
1086                         }
1087 #endif /* SCTP_DEBUG */
1088                         return (-1);
1089                 }
1090
1091                 /* is the negative request already in queue, and not sent */
1092                 if (aa->sent == 1)
1093                         continue;
1094                 if (type == SCTP_ADD_IP_ADDRESS &&
1095                     aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
1096                         /* add requested, delete already queued */
1097
1098                         /* delete the existing entry in the queue */
1099                         TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1100                         /* free the entry */
1101                         FREE(aa, M_PCB);
1102 #ifdef SCTP_DEBUG
1103                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1104                                 printf("asconf_queue_add_sa: removing queued delete request\n");
1105                         }
1106 #endif /* SCTP_DEBUG */
1107                         return (-1);
1108                 } else if (type == SCTP_DEL_IP_ADDRESS &&
1109                            aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
1110                         /* delete requested, add already queued */
1111
1112                         /* delete the existing entry in the queue */
1113                         TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
1114                         /* take the entry off the appropriate list */
1115                         sctp_asconf_addr_mgmt_ack(stcb, aa->ifa, type, 1);
1116                         /* free the entry */
1117                         FREE(aa, M_PCB);
1118 #ifdef SCTP_DEBUG
1119                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1120                                 printf("asconf_queue_add_sa: removing queued add request\n");
1121                         }
1122 #endif /* SCTP_DEBUG */
1123                         return (-1);
1124                 }
1125         } /* for each aa */
1126
1127         /* adding new request to the queue */
1128         MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa), M_PCB, M_NOWAIT);
1129         if (aa == NULL) {
1130                 /* didn't get memory */
1131 #ifdef SCTP_DEBUG
1132                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1133                         printf("asconf_queue_add_sa: failed to get memory!\n");
1134                 }
1135 #endif /* SCTP_DEBUG */
1136                 return (-1);
1137         }
1138         /* fill in asconf address parameter fields */
1139         /* top level elements are "networked" during send */
1140         aa->ap.aph.ph.param_type = type;
1141         aa->ifa = sctp_find_ifa_by_addr(sa);
1142         /* correlation_id filled in during send routine later... */
1143         if (sa->sa_family == AF_INET6) {
1144                 /* IPv6 address */
1145                 struct sockaddr_in6 *sin6;
1146
1147                 sin6 = (struct sockaddr_in6 *)sa;
1148                 aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
1149                 aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
1150                 aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
1151                 memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
1152                     sizeof(struct in6_addr));
1153         } else if (sa->sa_family == AF_INET) {
1154                 /* IPv4 address */
1155                 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
1156                 aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
1157                 aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
1158                 aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
1159                 memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
1160                     sizeof(struct in_addr));
1161         } else {
1162                 /* invalid family! */
1163                 return (-1);
1164         }
1165         aa->sent = 0;                   /* clear sent flag */
1166
1167         /*
1168          * if we are deleting an address it should go out last
1169          * otherwise, add it to front of the pending queue
1170          */
1171         if (type == SCTP_ADD_IP_ADDRESS) {
1172                 /* add goes to the front of the queue */
1173                 TAILQ_INSERT_HEAD(&stcb->asoc.asconf_queue, aa, next);
1174 #ifdef SCTP_DEBUG
1175                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1176                         printf("asconf_queue_add_sa: appended asconf ADD_IP_ADDRESS\n");
1177                 }
1178 #endif /* SCTP_DEBUG */
1179         } else {
1180                 /* delete and set primary goes to the back of the queue */
1181                 TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
1182 #ifdef SCTP_DEBUG
1183                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1184                         if (type == SCTP_DEL_IP_ADDRESS) {
1185                                 printf("asconf_queue_add_sa: inserted asconf DEL_IP_ADDRESS\n");
1186                         } else {
1187                                 printf("asconf_queue_add_sa: inserted asconf SET_PRIM_ADDR\n");
1188                         }
1189                 }
1190 #endif /* SCTP_DEBUG */
1191         }
1192
1193         return (0);
1194 }
1195
1196 /*
1197  * find a specific asconf param on our "sent" queue
1198  */
1199 static struct sctp_asconf_addr *
1200 sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
1201 {
1202         struct sctp_asconf_addr *aa;
1203
1204         TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
1205                 if (aa->ap.aph.correlation_id == correlation_id &&
1206                     aa->sent == 1) {
1207                         /* found it */
1208                         return (aa);
1209                 }
1210         }
1211         /* didn't find it */
1212         return (NULL);
1213 }
1214
1215 /*
1216  * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter
1217  * and do notifications based on the error response
1218  */
1219 static void
1220 sctp_asconf_process_error(struct sctp_tcb *stcb,
1221     struct sctp_asconf_paramhdr *aph)
1222 {
1223         struct sctp_error_cause *eh;
1224         struct sctp_paramhdr *ph;
1225         uint16_t param_type;
1226         uint16_t error_code;
1227
1228         eh = (struct sctp_error_cause *)(aph + 1);
1229         ph = (struct sctp_paramhdr *)(eh + 1);
1230         /* validate lengths */
1231         if (htons(eh->length) + sizeof(struct sctp_error_cause) >
1232             htons(aph->ph.param_length)) {
1233                 /* invalid error cause length */
1234 #ifdef SCTP_DEBUG
1235                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1236                         printf("asconf_process_error: cause element too long\n");
1237                 }
1238 #endif /* SCTP_DEBUG */
1239                 return;
1240         }
1241         if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
1242             htons(eh->length)) {
1243                 /* invalid included TLV length */
1244 #ifdef SCTP_DEBUG
1245                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1246                         printf("asconf_process_error: included TLV too long\n");
1247                 }
1248 #endif /* SCTP_DEBUG */
1249                 return;
1250         }
1251
1252         /* which error code ? */
1253         error_code = ntohs(eh->code);
1254         param_type = ntohs(aph->ph.param_type);
1255         /* FIX: this should go back up the REMOTE_ERROR ULP notify */
1256         switch (error_code) {
1257         case SCTP_ERROR_RESOURCE_SHORTAGE:
1258                 /* we allow ourselves to "try again" for this error */
1259                 break;
1260         default:
1261                 /* peer can't handle it... */
1262                 switch (param_type) {
1263                 case SCTP_ADD_IP_ADDRESS:
1264                 case SCTP_DEL_IP_ADDRESS:
1265                         stcb->asoc.peer_supports_asconf = 0;
1266                         break;
1267                 case SCTP_SET_PRIM_ADDR:
1268                         stcb->asoc.peer_supports_asconf_setprim = 0;
1269                         break;
1270                 default:
1271                         break;
1272                 }
1273         }
1274 }
1275
1276 /*
1277  * process an asconf queue param
1278  * aparam: parameter to process, will be removed from the queue
1279  * flag: 1=success, 0=failure
1280  */
1281 static void
1282 sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
1283     struct sctp_asconf_addr *aparam, uint32_t flag)
1284 {
1285         uint16_t param_type;
1286
1287         /* process this param */
1288         param_type = aparam->ap.aph.ph.param_type;
1289 #ifdef SCTP_DEBUG
1290         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1291                 printf("process_param_ack: handling asconf parameter type=%xh\n", param_type);
1292         }
1293 #endif /* SCTP_DEBUG */
1294         switch (param_type) {
1295         case SCTP_ADD_IP_ADDRESS:
1296 #ifdef SCTP_DEBUG
1297                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1298                         printf("process_param_ack: added IP address\n");
1299                 }
1300 #endif /* SCTP_DEBUG */
1301                 sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, param_type, flag);
1302                 break;
1303         case SCTP_DEL_IP_ADDRESS:
1304 #ifdef SCTP_DEBUG
1305                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1306                         printf("process_param_ack: deleted IP address\n");
1307                 }
1308 #endif /* SCTP_DEBUG */
1309                 /* nothing really to do... lists already updated */
1310                 break;
1311         case SCTP_SET_PRIM_ADDR:
1312                 /* nothing to do... peer may start using this addr */
1313                 if (flag == 0)
1314                         stcb->asoc.peer_supports_asconf_setprim = 0;
1315                 break;
1316         default:
1317                 /* should NEVER happen */
1318                 break;
1319         } /* switch */
1320
1321         /* remove the param and free it */
1322         TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
1323         FREE(aparam, M_PCB);
1324 }
1325
1326 /*
1327  * cleanup from a bad asconf ack parameter
1328  */
1329 static void
1330 sctp_asconf_ack_clear(struct sctp_tcb *stcb)
1331 {
1332         /* assume peer doesn't really know how to do asconfs */
1333         stcb->asoc.peer_supports_asconf = 0;
1334         stcb->asoc.peer_supports_asconf_setprim = 0;
1335         /* XXX we could free the pending queue here */
1336 }
1337
1338 void
1339 sctp_handle_asconf_ack(struct mbuf *m, int offset,
1340     struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
1341     struct sctp_nets *net)
1342 {
1343         struct sctp_association *asoc;
1344         uint32_t serial_num;
1345         uint16_t ack_length;
1346         struct sctp_asconf_paramhdr *aph;
1347         struct sctp_asconf_addr *aa, *aa_next;
1348         uint32_t last_error_id = 0;             /* last error correlation id */
1349         uint32_t id;
1350         struct sctp_asconf_addr *ap;
1351         /* asconf param buffer */
1352         static u_int8_t aparam_buf[DEFAULT_PARAM_BUFFER];
1353
1354         /* verify minimum length */
1355         if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
1356 #ifdef SCTP_DEBUG
1357                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1358                         printf("handle_asconf_ack: chunk too small = %xh\n",
1359                                ntohs(cp->ch.chunk_length));
1360                 }
1361 #endif /* SCTP_DEBUG */
1362                 return;
1363         }
1364
1365         asoc = &stcb->asoc;
1366         serial_num = ntohl(cp->serial_number);
1367
1368         /*
1369          * NOTE: we may want to handle this differently- currently, we
1370          * will abort when we get an ack for the expected serial number + 1
1371          * (eg. we didn't send it), process an ack normally if it is the
1372          * expected serial number, and re-send the previous ack for *ALL*
1373          * other serial numbers
1374          */
1375
1376         /*
1377          * if the serial number is the next expected, but I didn't send it,
1378          * abort the asoc, since someone probably just hijacked us...
1379          */
1380         if (serial_num == (asoc->asconf_seq_out + 1)) {
1381                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1382                     SCTP_ERROR_ILLEGAL_ASCONF_ACK, NULL);
1383 #ifdef SCTP_DEBUG
1384                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1385                         printf("handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
1386                 }
1387 #endif /* SCTP_DEBUG */
1388                 return;
1389         }
1390
1391         if (serial_num != asoc->asconf_seq_out) {
1392                 /* got a duplicate/unexpected ASCONF-ACK */
1393 #ifdef SCTP_DEBUG
1394                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1395                         printf("handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n", serial_num, asoc->asconf_seq_out);
1396                 }
1397 #endif /* SCTP_DEBUG */
1398                 return;
1399         }
1400         /* stop our timer */
1401         sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, net);
1402
1403         /* process the ASCONF-ACK contents */
1404         ack_length = ntohs(cp->ch.chunk_length) -
1405             sizeof(struct sctp_asconf_ack_chunk);
1406         offset += sizeof(struct sctp_asconf_ack_chunk);
1407         /* process through all parameters */
1408         while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
1409                 unsigned int param_length, param_type;
1410
1411                 /* get pointer to next asconf parameter */
1412                 aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
1413                     sizeof(struct sctp_asconf_paramhdr), aparam_buf);
1414                 if (aph == NULL) {
1415                         /* can't get an asconf paramhdr */
1416                         sctp_asconf_ack_clear(stcb);
1417                         return;
1418                 }
1419                 param_type = ntohs(aph->ph.param_type);
1420                 param_length = ntohs(aph->ph.param_length);
1421                 if (param_length > ack_length) {
1422                         sctp_asconf_ack_clear(stcb);
1423                         return;
1424                 }
1425                 if (param_length < sizeof(struct sctp_paramhdr)) {
1426                         sctp_asconf_ack_clear(stcb);
1427                         return;
1428                 }
1429
1430                 /* get the complete parameter... */
1431                 if (param_length > sizeof(aparam_buf)) {
1432 #ifdef SCTP_DEBUG
1433                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1434                                 printf("param length (%u) larger than buffer size!\n", param_length);
1435                         }
1436 #endif /* SCTP_DEBUG */
1437                         sctp_asconf_ack_clear(stcb);
1438                         return;
1439                 }
1440                 aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
1441                 if (aph == NULL) {
1442                         sctp_asconf_ack_clear(stcb);
1443                         return;
1444                 }
1445                 /* correlation_id is transparent to peer, no ntohl needed */
1446                 id = aph->correlation_id;
1447
1448                 switch (param_type) {
1449                 case SCTP_ERROR_CAUSE_IND:
1450                         last_error_id = id;
1451                         /* find the corresponding asconf param in our queue */
1452                         ap = sctp_asconf_find_param(stcb, id);
1453                         if (ap == NULL) {
1454                                 /* hmm... can't find this in our queue! */
1455                                 break;
1456                         }
1457                         /* process the parameter, failed flag */
1458                         sctp_asconf_process_param_ack(stcb, ap, 0);
1459                         /* process the error response */
1460                         sctp_asconf_process_error(stcb, aph);
1461                         break;
1462                 case SCTP_SUCCESS_REPORT:
1463                         /* find the corresponding asconf param in our queue */
1464                         ap = sctp_asconf_find_param(stcb, id);
1465                         if (ap == NULL) {
1466                                 /* hmm... can't find this in our queue! */
1467                                 break;
1468                         }
1469                         /* process the parameter, success flag */
1470                         sctp_asconf_process_param_ack(stcb, ap, 1);
1471                         break;
1472                 default:
1473                         break;
1474                 } /* switch */
1475
1476                 /* update remaining ASCONF-ACK message length to process */
1477                 ack_length -= SCTP_SIZE32(param_length);
1478                 if (ack_length <= 0) {
1479                         /* no more data in the mbuf chain */
1480                         break;
1481                 }
1482                 offset += SCTP_SIZE32(param_length);
1483         } /* while */
1484
1485         /*
1486          * if there are any "sent" params still on the queue, these are
1487          * implicitly "success", or "failed" (if we got an error back)
1488          * ... so process these appropriately
1489          *
1490          * we assume that the correlation_id's are monotonically increasing
1491          * beginning from 1 and that we don't have *that* many outstanding
1492          * at any given time
1493          */
1494         if (last_error_id == 0)
1495                 last_error_id--;        /* set to "max" value */
1496         for (aa = TAILQ_FIRST(&stcb->asoc.asconf_queue); aa != NULL;
1497             aa = aa_next) {
1498                 aa_next = TAILQ_NEXT(aa, next);
1499                 if (aa->sent == 1) {
1500                         /*
1501                          * implicitly successful or failed
1502                          * if correlation_id < last_error_id, then success
1503                          * else, failure
1504                          */
1505                         if (aa->ap.aph.correlation_id < last_error_id)
1506                                 sctp_asconf_process_param_ack(stcb, aa,
1507                                     SCTP_SUCCESS_REPORT);
1508                         else
1509                                 sctp_asconf_process_param_ack(stcb, aa,
1510                                     SCTP_ERROR_CAUSE_IND);
1511                 } else {
1512                         /*
1513                          * since we always process in order (FIFO queue)
1514                          * if we reach one that hasn't been sent, the
1515                          * rest should not have been sent either.
1516                          * so, we're done...
1517                          */
1518                         break;
1519                 }
1520         }
1521
1522         /* update the next sequence number to use */
1523         asoc->asconf_seq_out++;
1524         /* remove the old ASCONF on our outbound queue */
1525         sctp_toss_old_asconf(stcb);
1526         /* clear the sent flag to allow new ASCONFs */
1527         asoc->asconf_sent = 0;
1528         if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
1529                 /* we have more params, so restart our timer */
1530                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
1531                     stcb, net);
1532         }
1533 }
1534
1535 /* is this an interface that we care about at all? */
1536 static uint32_t
1537 sctp_is_desired_interface_type(struct ifaddr *ifa)
1538 {
1539         int result;
1540
1541         /* check the interface type to see if it's one we care about */
1542         switch (ifa->ifa_ifp->if_type) {
1543         case IFT_ETHER:
1544         case IFT_ISO88023:
1545         case IFT_ISO88025:
1546         case IFT_STARLAN:
1547         case IFT_P10:
1548         case IFT_P80:
1549         case IFT_HY:
1550         case IFT_FDDI:
1551         case IFT_PPP:
1552         case IFT_XETHER:
1553         case IFT_SLIP:
1554         case IFT_GIF:
1555                 result = 1;
1556                 break;
1557         default:
1558 #ifdef SCTP_DEBUG
1559                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1560                         printf("ignoring interface type = %u\n",
1561                                ifa->ifa_ifp->if_type);
1562                 }
1563 #endif /* SCTP_DEBUG */
1564                 result = 0;
1565         } /* end switch */
1566
1567         return (result);
1568 }
1569
1570 static uint32_t
1571 sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
1572 {
1573         struct sockaddr_in6 *sin6, *net6;
1574         struct sctp_nets *net;
1575
1576         if (sa->sa_family != AF_INET6) {
1577                 /* wrong family */
1578                 return (0);
1579         }
1580
1581         sin6 = (struct sockaddr_in6 *)sa;
1582         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
1583                 /* not link local address */
1584                 return (0);
1585         }
1586
1587         /* hunt through our destination nets list for this scope_id */
1588         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1589                 if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
1590                     AF_INET6)
1591                         continue;
1592                 net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1593                 if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
1594                         continue;
1595                 if (sctp_is_same_scope(sin6, net6)) {
1596                         /* found one */
1597                         return (1);
1598                 }
1599         }
1600         /* didn't find one */
1601         return (0);
1602 }
1603
1604 /*
1605  * address management functions
1606  */
1607 static void
1608 sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1609     struct ifaddr *ifa, uint16_t type)
1610 {
1611         int status;
1612 #ifdef SCTP_DEBUG
1613         char buf[128];  /* for address in string format */
1614 #endif /* SCTP_DEBUG */
1615
1616         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
1617             (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0) {
1618                 /* subset bound, no ASCONF allowed case, so ignore */
1619                 return;
1620         }
1621
1622         /*
1623          * note: we know this is not the subset bound, no ASCONF case
1624          *      eg. this is boundall or subset bound w/ASCONF allowed
1625          */
1626
1627         /* first, make sure it's a good address family */
1628         if (ifa->ifa_addr->sa_family != AF_INET6 &&
1629             ifa->ifa_addr->sa_family != AF_INET) {
1630                 return;
1631         }
1632
1633         /* make sure we're "allowed" to add this type of addr */
1634         if (ifa->ifa_addr->sa_family == AF_INET6) {
1635                 struct in6_ifaddr *ifa6;
1636
1637                 /* invalid if we're not a v6 endpoint */
1638                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
1639                         return;
1640                 /* is the v6 addr really valid ? */
1641                 ifa6 = (struct in6_ifaddr *)ifa;
1642                 if (IFA6_IS_DEPRECATED(ifa6) ||
1643                     (ifa6->ia6_flags &
1644                      (IN6_IFF_DETACHED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) {
1645                         /* can't use an invalid address */
1646                         return;
1647                 }
1648         }
1649
1650         /* put this address on the "pending/do not use yet" list */
1651         /*
1652          * Note: we do this primarily for the subset bind case
1653          * We don't have scoping flags at the EP level, so we must
1654          * add link local/site local addresses to the EP, then need
1655          * to "negate" them here.  Recall that this routine is only
1656          * called for the subset bound w/ASCONF allowed case.
1657          */
1658
1659         /*
1660          * do a scope_id check against any link local addresses
1661          * in the destination nets list to see if we should put
1662          * this local address on the pending list or not
1663          * eg. don't put on the list if we have a link local
1664          * destination with the same scope_id
1665          */
1666         if (type == SCTP_ADD_IP_ADDRESS) {
1667                 if (sctp_is_scopeid_in_nets(stcb, ifa->ifa_addr) == 0) {
1668                         sctp_add_local_addr_assoc(stcb, ifa);
1669 #ifdef SCTP_DEBUG
1670                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1671                                 printf("addr_mgmt_assoc: added to pending list ");
1672                                 sctp_print_address(ifa->ifa_addr);
1673                         }
1674 #endif /* SCTP_DEBUG */
1675                 }
1676         }
1677         /*
1678          * check address scope
1679          * if address is out of scope, don't queue anything...
1680          * note: this would leave the address on both inp and asoc lists
1681          */
1682         if (ifa->ifa_addr->sa_family == AF_INET6) {
1683                 struct sockaddr_in6 *sin6;
1684
1685                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1686 #ifdef SCTP_DEBUG
1687                 strlcpy(buf, ip6_sprintf(&sin6->sin6_addr), sizeof(buf));
1688 #endif /* SCTP_DEBUG */
1689                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1690                         /* we skip unspecifed addresses */
1691 #ifdef SCTP_DEBUG
1692                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1693                                 printf("addr_mgmt_assoc: unspecified IPv6 addr\n");
1694                         }
1695 #endif /* SCTP_DEBUG */
1696                         return;
1697                 }
1698                 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1699                         if (stcb->asoc.local_scope == 0) {
1700 #ifdef SCTP_DEBUG
1701                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1702                                         printf("addr_mgmt_assoc: skipping link local IPv6 addr: %s\n", buf);
1703                                 }
1704 #endif /* SCTP_DEBUG */
1705                                 return;
1706                         }
1707                         /* is it the right link local scope? */
1708                         if (sctp_is_scopeid_in_nets(stcb, ifa->ifa_addr) == 0) {
1709 #ifdef SCTP_DEBUG
1710                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1711                                         printf("addr_mgmt_assoc: skipping link local IPv6 addr: %s, wrong scope_id\n", buf);
1712                                 }
1713 #endif /* SCTP_DEBUG */
1714                                 return;
1715                         }
1716                 }
1717                 if (stcb->asoc.site_scope == 0 &&
1718                     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
1719 #ifdef SCTP_DEBUG
1720                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1721                                 printf("addr_mgmt_assoc: skipping site local IPv6 addr: %s\n", buf);
1722                         }
1723 #endif /* SCTP_DEBUG */
1724                         return;
1725                 }
1726         } else if (ifa->ifa_addr->sa_family == AF_INET) {
1727                 struct sockaddr_in *sin;
1728                 struct in6pcb *inp6;
1729
1730                 inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1731                 /* invalid if we are a v6 only endpoint */
1732                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1733 #if defined(__OpenBSD__)
1734                     (0) /* we always do dual bind */
1735 #elif defined (__NetBSD__)
1736                     (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1737 #else
1738                     (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1739 #endif
1740                         )
1741                         return;
1742
1743                 sin = (struct sockaddr_in *)ifa->ifa_addr;
1744 #ifdef SCTP_DEBUG
1745                 strlcpy(buf, inet_ntoa(sin->sin_addr), sizeof(buf));
1746 #endif /* SCTP_DEBUG */
1747                 if (sin->sin_addr.s_addr == 0) {
1748                         /* we skip unspecifed addresses */
1749 #ifdef SCTP_DEBUG
1750                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1751                                 printf("addr_mgmt_assoc: unspecified IPv4 addr\n");
1752                         }
1753 #endif /* SCTP_DEBUG */
1754                         return;
1755                 }
1756                 if (stcb->asoc.ipv4_local_scope == 0 &&
1757                     IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1758 #ifdef SCTP_DEBUG
1759                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1760                                 printf("addr_mgmt_assoc: skipping private IPv4 addr: %s\n", buf);
1761                         }
1762 #endif /* SCTP_DEBUG */
1763                         return;
1764                 }
1765         } else {
1766                 /* else, not AF_INET or AF_INET6, so skip */
1767 #ifdef SCTP_DEBUG
1768                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1769                         printf("addr_mgmt_assoc: not AF_INET or AF_INET6\n");
1770                 }
1771 #endif /* SCTP_DEBUG */
1772                 return;
1773         }
1774
1775         /* queue an asconf for this address add/delete */
1776         if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1777                 /* does the peer do asconf? */
1778                 if (stcb->asoc.peer_supports_asconf) {
1779                         /* queue an asconf for this addr */
1780                         status = sctp_asconf_queue_add(stcb, ifa, type);
1781                         /*
1782                          * if queued ok, and in correct state, set the
1783                          * ASCONF timer
1784                          * if in non-open state, we will set this timer
1785                          * when the state does go open and do all the
1786                          * asconf's
1787                          */
1788                         if (status == 0 &&
1789                             SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
1790                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
1791                                     stcb, stcb->asoc.primary_destination);
1792                         }
1793                 }
1794         } else {
1795                 /* this is the boundall, no ASCONF case */
1796 #if 0 /* assume kernel will delete this very shortly; add done above */
1797                 if (type == SCTP_DEL_IP_ADDRESS) {
1798                         /* if deleting, add this addr to the do not use list */
1799                         sctp_add_local_addr_assoc(stcb, ifa);
1800                 }
1801 #endif
1802         }
1803 }
1804
1805 static void
1806 sctp_addr_mgmt_ep(struct sctp_inpcb *inp, struct ifaddr *ifa, uint16_t type)
1807 {
1808         struct sctp_tcb *stcb;
1809
1810         SCTP_INP_WLOCK(inp);
1811         /* make sure we're "allowed" to add this type of addr */
1812         if (ifa->ifa_addr->sa_family == AF_INET6) {
1813                 struct in6_ifaddr *ifa6;
1814
1815                 /* invalid if we're not a v6 endpoint */
1816                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1817                         SCTP_INP_WUNLOCK(inp);
1818                         return;
1819                 }
1820                 /* is the v6 addr really valid ? */
1821                 ifa6 = (struct in6_ifaddr *)ifa;
1822                 if (IFA6_IS_DEPRECATED(ifa6) ||
1823                     (ifa6->ia6_flags &
1824                      (IN6_IFF_DETACHED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) {
1825                         /* can't use an invalid address */
1826                         SCTP_INP_WUNLOCK(inp);
1827                         return;
1828                 }
1829         } else if (ifa->ifa_addr->sa_family == AF_INET) {
1830                 /* invalid if we are a v6 only endpoint */
1831                 struct in6pcb *inp6;
1832                 inp6 = (struct in6pcb *)&inp->ip_inp.inp;
1833
1834                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1835 #if defined(__OpenBSD__)
1836                     (0) /* we always do dual bind */
1837 #elif defined (__NetBSD__)
1838                     (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1839 #else
1840                     (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1841 #endif
1842                         ) {
1843                         SCTP_INP_WUNLOCK(inp);
1844                         return;
1845                 }
1846         } else {
1847                 /* invalid address family */
1848                 SCTP_INP_WUNLOCK(inp);
1849                 return;
1850         }
1851         /* is this endpoint subset bound ? */
1852         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1853                 /* subset bound endpoint */
1854                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0) {
1855                         /*
1856                          * subset bound, but ASCONFs not allowed...
1857                          * if adding, nothing to do, since not allowed
1858                          * if deleting, remove address from endpoint
1859                          *      peer will have to "timeout" this addr
1860                          */
1861                         if (type == SCTP_DEL_IP_ADDRESS) {
1862                                 sctp_del_local_addr_ep(inp, ifa);
1863                         }
1864                         /* no asconfs to queue for this inp... */
1865                         SCTP_INP_WUNLOCK(inp);
1866                         return;
1867                 } else {
1868                         /*
1869                          * subset bound, ASCONFs allowed...
1870                          * if adding, add address to endpoint list
1871                          * if deleting, remove address from endpoint
1872                          */
1873                         if (type == SCTP_ADD_IP_ADDRESS) {
1874                                 sctp_add_local_addr_ep(inp, ifa);
1875                         } else {
1876                                 sctp_del_local_addr_ep(inp, ifa);
1877                         }
1878                         /* drop through and notify all asocs */
1879                 }
1880         }
1881
1882         crit_enter();
1883         /* process for all associations for this endpoint */
1884         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1885                 SCTP_TCB_LOCK(stcb);
1886                 sctp_addr_mgmt_assoc(inp, stcb, ifa, type);
1887                 SCTP_TCB_UNLOCK(stcb);
1888         } /* for each stcb */
1889         crit_exit();
1890         SCTP_INP_WUNLOCK(inp);
1891 }
1892
1893 /*
1894  * restrict the use of this address
1895  */
1896 static void
1897 sctp_addr_mgmt_restrict_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
1898 {
1899         struct sctp_tcb *stcb;
1900
1901         /* is this endpoint bound to all? */
1902         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1903                 /*
1904                  * Nothing to do for subset bound case.
1905                  * Allow sctp_bindx() to manage the address lists
1906                  */
1907                 return;
1908         }
1909
1910         crit_enter();
1911         SCTP_INP_RLOCK(inp);
1912         /* process for all associations for this endpoint */
1913         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1914                 /* put this address on the "pending/do not use yet" list */
1915                 SCTP_TCB_LOCK(stcb);
1916                 sctp_add_local_addr_assoc(stcb, ifa);
1917                 SCTP_TCB_UNLOCK(stcb);
1918 #ifdef SCTP_DEBUG
1919                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1920                         printf("restrict_ep: added addr to unusable list\n");
1921                 }
1922 #endif /* SCTP_DEBUG */
1923         } /* for each stcb */
1924         crit_exit();
1925         SCTP_INP_RUNLOCK(inp);
1926 }
1927
1928 /*
1929  * this is only called for kernel initiated address changes
1930  * eg. it will check the PCB_FLAGS_AUTO_ASCONF flag
1931  */
1932 static void
1933 sctp_addr_mgmt(struct ifaddr *ifa, uint16_t type) {
1934         struct sockaddr *sa;
1935         struct sctp_inpcb *inp;
1936
1937         /* make sure we care about this interface... */
1938         if (!sctp_is_desired_interface_type(ifa)) {
1939 #ifdef SCTP_DEBUG
1940                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1941                         printf("sctp_addr_mgmt: ignoring this interface\n");
1942                 }
1943 #endif /* SCTP_DEBUG */
1944                 return;
1945         }
1946
1947         sa = ifa->ifa_addr;
1948         if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
1949                 return;
1950
1951 #ifdef SCTP_DEBUG
1952         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
1953                 if (type == SCTP_ADD_IP_ADDRESS)
1954                         printf("sctp_addr_mgmt: kernel adds ");
1955                 else
1956                         printf("sctp_addr_mgmt: kernel deletes ");
1957                 sctp_print_address(sa);
1958         }
1959 #endif /* SCTP_DEBUG */
1960
1961         /* go through all our PCB's */
1962         LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
1963                 if (inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF) {
1964                         sctp_addr_mgmt_ep(inp, ifa, type);
1965                 } else {
1966                         /* this address is going away anyways... */
1967                         if (type == SCTP_DEL_IP_ADDRESS)
1968                                 return;
1969                         /* (temporarily) restrict this address */
1970                         sctp_addr_mgmt_restrict_ep(inp, ifa);
1971                 }
1972                 /* else, not allowing automatic asconf's, so ignore */
1973         } /* for each inp */
1974 }
1975
1976 /*
1977  * add/delete IP address requests from kernel (via routing change)
1978  * assumed that the address is non-broadcast, non-multicast
1979  * all addresses are passed from any type of interface-- need to filter
1980  * duplicate addresses may get requested
1981  */
1982
1983 void
1984 sctp_add_ip_address(struct ifaddr *ifa)
1985 {
1986         sctp_addr_mgmt(ifa, SCTP_ADD_IP_ADDRESS);
1987 }
1988
1989 void
1990 sctp_delete_ip_address(struct ifaddr *ifa)
1991 {
1992         struct sctp_inpcb *inp;
1993
1994         /* process the delete */
1995         sctp_addr_mgmt(ifa, SCTP_DEL_IP_ADDRESS);
1996
1997         /*
1998          * need to remove this ifaddr from any cached routes
1999          * and also any from any assoc "restricted/pending" lists
2000          */
2001         /* make sure we care about this interface... */
2002         if (!sctp_is_desired_interface_type(ifa)) {
2003 #ifdef SCTP_DEBUG
2004                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2005                         printf("sctp_delete_ip_address: ignoring this interface\n");
2006                 }
2007 #endif /* SCTP_DEBUG */
2008                 return;
2009         }
2010
2011         /* go through all our PCB's */
2012         SCTP_INP_INFO_RLOCK();
2013         LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
2014                 struct sctp_tcb *stcb;
2015                 struct sctp_laddr *laddr, *laddr_next;
2016
2017                 /* process for all associations for this endpoint */
2018                 SCTP_INP_RLOCK(inp);
2019                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2020                         struct sctp_nets *net;
2021
2022                         /* process through the nets list */
2023                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2024                                 struct rtentry *rt;
2025                                 /* delete this address if cached */
2026                                 rt = net->ro.ro_rt;
2027                                 if (rt != NULL && rt->rt_ifa == ifa) {
2028 /*                                      RTFREE(rt);*/
2029                                         net->ro.ro_rt = NULL;
2030                                 }
2031                         } /* for each net */
2032                         /* process through the asoc "pending" list */
2033                         laddr = LIST_FIRST(&stcb->asoc.sctp_local_addr_list);
2034                         while (laddr != NULL) {
2035                                 laddr_next = LIST_NEXT(laddr, sctp_nxt_addr);
2036                                 /* remove if in use */
2037                                 if (laddr->ifa == ifa) {
2038                                         sctp_remove_laddr(laddr);
2039                                 }
2040                                 laddr = laddr_next;
2041                         } /* while */
2042                 } /* for each stcb */
2043                 /* process through the inp bound addr list */
2044                 laddr = LIST_FIRST(&inp->sctp_addr_list);
2045                 while (laddr != NULL) {
2046                         laddr_next = LIST_NEXT(laddr, sctp_nxt_addr);
2047                         /* remove if in use */
2048                         if (laddr->ifa == ifa) {
2049                                 sctp_remove_laddr(laddr);
2050                         }
2051                         laddr = laddr_next;
2052                 } /* while */
2053                 SCTP_INP_RUNLOCK(inp);
2054         } /* for each inp */
2055         SCTP_INP_INFO_RUNLOCK();
2056 }
2057
2058 /*
2059  * sa is the sockaddr to ask the peer to set primary to
2060  * returns: 0 = completed, -1 = error
2061  */
2062 int32_t
2063 sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
2064 {
2065         /* NOTE: we currently don't check the validity of the address! */
2066
2067         /* queue an ASCONF:SET_PRIM_ADDR to be sent */
2068         if (!sctp_asconf_queue_add_sa(stcb, sa, SCTP_SET_PRIM_ADDR)) {
2069                 /* set primary queuing succeeded */
2070                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
2071                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2072                             stcb->sctp_ep, stcb,
2073                             stcb->asoc.primary_destination);
2074                 }
2075 #ifdef SCTP_DEBUG
2076                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2077                         printf("set_primary_ip_address_sa: queued on tcb=%p, ",
2078                             stcb);
2079                         sctp_print_address(sa);
2080                 }
2081 #endif /* SCTP_DEBUG */
2082         } else {
2083 #ifdef SCTP_DEBUG
2084                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2085                         printf("set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
2086                             stcb);
2087                         sctp_print_address(sa);
2088                 }
2089 #endif /* SCTP_DEBUG */
2090                 return (-1);
2091         }
2092         return (0);
2093 }
2094
2095 void
2096 sctp_set_primary_ip_address(struct ifaddr *ifa)
2097 {
2098         struct sctp_inpcb *inp;
2099
2100         /* make sure we care about this interface... */
2101         if (!sctp_is_desired_interface_type(ifa)) {
2102 #ifdef SCTP_DEBUG
2103                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2104                         printf("set_primary_ip_address: ignoring this interface\n");
2105                 }
2106 #endif /* SCTP_DEBUG */
2107                 return;
2108         }
2109
2110         /* go through all our PCB's */
2111         LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
2112                 struct sctp_tcb *stcb;
2113
2114                 /* process for all associations for this endpoint */
2115                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2116                         /* queue an ASCONF:SET_PRIM_ADDR to be sent */
2117                         if (!sctp_asconf_queue_add(stcb, ifa,
2118                             SCTP_SET_PRIM_ADDR)) {
2119                                 /* set primary queuing succeeded */
2120                                 if (SCTP_GET_STATE(&stcb->asoc) ==
2121                                     SCTP_STATE_OPEN) {
2122                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2123                                             stcb->sctp_ep, stcb,
2124                                             stcb->asoc.primary_destination);
2125                                 }
2126 #ifdef SCTP_DEBUG
2127                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2128                                         printf("set_primary_ip_address: queued on stcb=%p, ",
2129                                             stcb);
2130                                         sctp_print_address(ifa->ifa_addr);
2131                                 }
2132 #endif /* SCTP_DEBUG */
2133                         } else {
2134 #ifdef SCTP_DEBUG
2135                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2136                                         printf("set_primary_ip_address: failed to add to queue, ");
2137                                         sctp_print_address(ifa->ifa_addr);
2138                                 }
2139 #endif /* SCTP_DEBUG */
2140                         }
2141                 } /* for each stcb */
2142         } /* for each inp */
2143 }
2144
2145 static struct sockaddr *
2146 sctp_find_valid_localaddr(struct sctp_tcb *stcb)
2147 {
2148         struct ifnet *ifn;
2149         struct ifaddr *ifa;
2150
2151         TAILQ_FOREACH(ifn, &ifnet, if_list) {
2152                 if (stcb->asoc.loopback_scope == 0 && ifn->if_type == IFT_LOOP) {
2153                         /* Skip if loopback_scope not set */
2154                         continue;
2155                 }
2156                 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
2157                         if (ifa->ifa_addr->sa_family == AF_INET &&
2158                             stcb->asoc.ipv4_addr_legal) {
2159                                 struct sockaddr_in *sin;
2160
2161                                 sin = (struct sockaddr_in *)ifa->ifa_addr;
2162                                 if (sin->sin_addr.s_addr == 0) {
2163                                         /* skip unspecifed addresses */
2164                                         continue;
2165                                 }
2166                                 if (stcb->asoc.ipv4_local_scope == 0 &&
2167                                     IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
2168                                         continue;
2169
2170                                 if (sctp_is_addr_restricted(stcb,
2171                                     ifa->ifa_addr))
2172                                         continue;
2173                                 /* found a valid local v4 address to use */
2174                                 return (ifa->ifa_addr);
2175                         } else if (ifa->ifa_addr->sa_family == AF_INET6 &&
2176                             stcb->asoc.ipv6_addr_legal) {
2177                                 struct sockaddr_in6 *sin6;
2178                                 struct in6_ifaddr *ifa6;
2179
2180                                 ifa6 = (struct in6_ifaddr *)ifa;
2181                                 if (IFA6_IS_DEPRECATED(ifa6) ||
2182                                     (ifa6->ia6_flags & (IN6_IFF_DETACHED |
2183                                      IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)))
2184                                         continue;
2185
2186                                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2187                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2188                                         /* we skip unspecifed addresses */
2189                                         continue;
2190                                 }
2191                                 if (stcb->asoc.local_scope == 0 &&
2192                                     IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2193                                         continue;
2194                                 if (stcb->asoc.site_scope == 0 &&
2195                                     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
2196                                         continue;
2197
2198                                 /* found a valid local v6 address to use */
2199                                 return (ifa->ifa_addr);
2200                         }
2201                 }
2202         }
2203         /* no valid addresses found */
2204         return (NULL);
2205 }
2206
2207 static struct sockaddr *
2208 sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
2209 {
2210         struct sctp_laddr *laddr;
2211
2212         LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2213                 if (laddr->ifa == NULL) {
2214 #ifdef SCTP_DEBUG
2215                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2216                                 printf("find_valid_localaddr_ep: laddr error\n");
2217                         }
2218 #endif /* SCTP_DEBUG */
2219                         continue;
2220                 }
2221                 if (laddr->ifa->ifa_addr == NULL) {
2222 #ifdef SCTP_DEBUG
2223                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2224                                 printf("find_valid_localaddr_ep: laddr->ifa error\n");
2225                         }
2226 #endif /* SCTP_DEBUG */
2227                         continue;
2228                 }
2229                 /* is the address restricted ? */
2230                 if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr))
2231                         continue;
2232
2233                 /* found a valid local address to use */
2234                 return (laddr->ifa->ifa_addr);
2235         }
2236         /* no valid addresses found */
2237         return (NULL);
2238 }
2239
2240 /*
2241  * builds an ASCONF chunk from queued ASCONF params
2242  * returns NULL on error (no mbuf, no ASCONF params queued, etc)
2243  */
2244 struct mbuf *
2245 sctp_compose_asconf(struct sctp_tcb *stcb)
2246 {
2247         struct mbuf *m_asconf, *m_asconf_chk;
2248         struct sctp_asconf_addr *aa;
2249         struct sctp_asconf_chunk *acp;
2250         struct sctp_asconf_paramhdr *aph;
2251         struct sctp_asconf_addr_param *aap;
2252         uint32_t p_length;
2253         uint32_t correlation_id = 1;            /* 0 is reserved... */
2254         caddr_t ptr, lookup_ptr;
2255         uint8_t lookup_used = 0;
2256
2257         /* are there any asconf params to send? */
2258         if (TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
2259                 return (NULL);
2260         }
2261
2262         /*
2263          * get a chunk header mbuf and a cluster for the asconf params
2264          * since it's simpler to fill in the asconf chunk header lookup
2265          * address on the fly
2266          */
2267         m_asconf_chk = NULL;
2268         MGETHDR(m_asconf_chk, MB_DONTWAIT, MT_DATA);
2269         if (m_asconf_chk == NULL) {
2270                 /* no mbuf's */
2271 #ifdef SCTP_DEBUG
2272                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
2273                         printf("compose_asconf: couldn't get chunk mbuf!\n");
2274 #endif /* SCTP_DEBUG */
2275                 return (NULL);
2276         }
2277         m_asconf = NULL;
2278         MGETHDR(m_asconf, MB_DONTWAIT, MT_HEADER);
2279         if (m_asconf == NULL) {
2280                 /* no mbuf's */
2281 #ifdef SCTP_DEBUG
2282                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
2283                         printf("compose_asconf: couldn't get mbuf!\n");
2284 #endif /* SCTP_DEBUG */
2285                 sctp_m_freem(m_asconf_chk);
2286                 return (NULL);
2287         }
2288         MCLGET(m_asconf, MB_DONTWAIT);
2289         if ((m_asconf->m_flags & M_EXT) != M_EXT) {
2290                 /* failed to get cluster buffer */
2291 #ifdef SCTP_DEBUG
2292                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
2293                         printf("compose_asconf: couldn't get cluster!\n");
2294 #endif /* SCTP_DEBUG */
2295                 sctp_m_freem(m_asconf_chk);
2296                 sctp_m_freem(m_asconf);
2297                 return (NULL);
2298         }
2299
2300         m_asconf_chk->m_len = sizeof(struct sctp_asconf_chunk);
2301         m_asconf->m_len = 0;
2302         acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
2303         bzero(acp, sizeof(struct sctp_asconf_chunk));
2304         /* save pointers to lookup address and asconf params */
2305         lookup_ptr = (caddr_t)(acp + 1);        /* after the header */
2306         ptr = mtod(m_asconf, caddr_t);          /* beginning of cluster */
2307
2308         /* fill in chunk header info */
2309         acp->ch.chunk_type = SCTP_ASCONF;
2310         acp->ch.chunk_flags = 0;
2311         acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
2312
2313         /* add parameters... up to smallest MTU allowed */
2314         TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
2315                 /* get the parameter length */
2316                 p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
2317                 /* will it fit in current chunk? */
2318                 if (m_asconf->m_len + p_length > stcb->asoc.smallest_mtu) {
2319                         /* won't fit, so we're done with this chunk */
2320                         break;
2321                 }
2322                 /* assign (and store) a correlation id */
2323                 aa->ap.aph.correlation_id = correlation_id++;
2324
2325                 /*
2326                  * fill in address if we're doing a delete
2327                  * this is a simple way for us to fill in the correlation
2328                  * address, which should only be used by the peer if we're
2329                  * deleting our source address and adding a new address
2330                  * (e.g. renumbering case)
2331                  */
2332                 if (lookup_used == 0 &&
2333                     aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
2334                         struct sctp_ipv6addr_param *lookup;
2335                         uint16_t p_size, addr_size;
2336
2337                         lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2338                         lookup->ph.param_type =
2339                             htons(aa->ap.addrp.ph.param_type);
2340                         if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
2341                                 /* copy IPv6 address */
2342                                 p_size = sizeof(struct sctp_ipv6addr_param);
2343                                 addr_size = sizeof(struct in6_addr);
2344                         } else {
2345                                 /* copy IPv4 address */
2346                                 p_size = sizeof(struct sctp_ipv4addr_param);
2347                                 addr_size = sizeof(struct in_addr);
2348                         }
2349                         lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2350                         memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
2351                         m_asconf_chk->m_len += SCTP_SIZE32(p_size);
2352                         lookup_used = 1;
2353                 }
2354
2355                 /* copy into current space */
2356                 memcpy(ptr, &aa->ap, p_length);
2357
2358                 /* network elements and update lengths */
2359                 aph = (struct sctp_asconf_paramhdr *) ptr;
2360                 aap = (struct sctp_asconf_addr_param *) ptr;
2361                 /* correlation_id is transparent to peer, no htonl needed */
2362                 aph->ph.param_type = htons(aph->ph.param_type);
2363                 aph->ph.param_length = htons(aph->ph.param_length);
2364                 aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
2365                 aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
2366
2367                 m_asconf->m_len += SCTP_SIZE32(p_length);
2368                 ptr += SCTP_SIZE32(p_length);
2369
2370                 /*
2371                  * these params are removed off the pending list upon
2372                  * getting an ASCONF-ACK back from the peer, just set flag
2373                  */
2374                 aa->sent = 1;
2375         }
2376         /* check to see if the lookup addr has been populated yet */
2377         if (lookup_used == 0) {
2378                 /* NOTE: if the address param is optional, can skip this... */
2379                 /* add any valid (existing) address... */
2380                 struct sctp_ipv6addr_param *lookup;
2381                 uint16_t p_size, addr_size;
2382                 struct sockaddr *found_addr;
2383                 caddr_t addr_ptr;
2384
2385                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
2386                         found_addr = sctp_find_valid_localaddr(stcb);
2387                 else
2388                         found_addr = sctp_find_valid_localaddr_ep(stcb);
2389
2390                 lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
2391                 if (found_addr != NULL) {
2392                         if (found_addr->sa_family == AF_INET6) {
2393                                 /* copy IPv6 address */
2394                                 lookup->ph.param_type =
2395                                     htons(SCTP_IPV6_ADDRESS);
2396                                 p_size = sizeof(struct sctp_ipv6addr_param);
2397                                 addr_size = sizeof(struct in6_addr);
2398                                 addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
2399                                     found_addr)->sin6_addr;
2400                         } else {
2401                                 /* copy IPv4 address */
2402                                 lookup->ph.param_type =
2403                                     htons(SCTP_IPV4_ADDRESS);
2404                                 p_size = sizeof(struct sctp_ipv4addr_param);
2405                                 addr_size = sizeof(struct in_addr);
2406                                 addr_ptr = (caddr_t)&((struct sockaddr_in *)
2407                                     found_addr)->sin_addr;
2408                         }
2409                         lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
2410                         memcpy(lookup->addr, addr_ptr, addr_size);
2411                         m_asconf_chk->m_len += SCTP_SIZE32(p_size);
2412                         lookup_used = 1;
2413                 } else {
2414                         /* uh oh... don't have any address?? */
2415 #ifdef SCTP_DEBUG
2416                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1)
2417                                 printf("compose_asconf: no lookup addr!\n");
2418 #endif /* SCTP_DEBUG */
2419                         /* for now, we send a IPv4 address of 0.0.0.0 */
2420                         lookup->ph.param_type = htons(SCTP_IPV4_ADDRESS);
2421                         lookup->ph.param_length = htons(SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param)));
2422                         bzero(lookup->addr, sizeof(struct in_addr));
2423                         m_asconf_chk->m_len += SCTP_SIZE32(sizeof(struct sctp_ipv4addr_param));
2424                         lookup_used = 1;
2425                 }
2426         }
2427
2428         /* chain it all together */
2429         m_asconf_chk->m_next = m_asconf;
2430         m_asconf_chk->m_pkthdr.len = m_asconf_chk->m_len + m_asconf->m_len;
2431         acp->ch.chunk_length = ntohs(m_asconf_chk->m_pkthdr.len);
2432
2433         /* update "sent" flag */
2434         stcb->asoc.asconf_sent++;
2435
2436         return (m_asconf_chk);
2437 }
2438
2439 /*
2440  * section to handle address changes before an association is up
2441  * eg. changes during INIT/INIT-ACK/COOKIE-ECHO handshake
2442  */
2443
2444 /*
2445  * processes the (local) addresses in the INIT-ACK chunk
2446  */
2447 static void
2448 sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
2449     unsigned int offset, unsigned int length)
2450 {
2451         struct sctp_paramhdr tmp_param, *ph;
2452         uint16_t plen, ptype;
2453         struct sctp_ipv6addr_param addr_store;
2454         struct sockaddr_in6 sin6;
2455         struct sockaddr_in sin;
2456         struct sockaddr *sa;
2457         struct ifaddr *ifa;
2458
2459 #ifdef SCTP_DEBUG
2460         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2461                 printf("processing init-ack addresses\n");
2462         }
2463 #endif /* SCTP_DEBUG */
2464
2465         /* convert to upper bound */
2466         length += offset;
2467
2468         if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2469 #ifdef SCTP_DEBUG
2470                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2471                         printf("process_initack_addrs: invalid offset?\n");
2472                 }
2473 #endif /* SCTP_DEBUG */
2474                 return;
2475         }
2476
2477         /* init the addresses */
2478         bzero(&sin6, sizeof(sin6));
2479         sin6.sin6_family = AF_INET6;
2480         sin6.sin6_len = sizeof(sin6);
2481         sin6.sin6_port = stcb->rport;
2482
2483         bzero(&sin, sizeof(sin));
2484         sin.sin_len = sizeof(sin);
2485         sin.sin_family = AF_INET;
2486         sin.sin_port = stcb->rport;
2487
2488         /* go through the addresses in the init-ack */
2489         ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2490             sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
2491         while (ph != NULL) {
2492                 ptype = ntohs(ph->param_type);
2493                 plen = ntohs(ph->param_length);
2494                 if (ptype == SCTP_IPV6_ADDRESS) {
2495                         struct sctp_ipv6addr_param *a6p;
2496                         /* get the entire IPv6 address param */
2497 #ifdef SCTP_DEBUG
2498                         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2499                                 printf("process_initack_addrs: checking IPv6 param\n");
2500                         }
2501 #endif /* SCTP_DEBUG */
2502                         a6p = (struct sctp_ipv6addr_param *)
2503                                 sctp_m_getptr(m, offset,
2504                                     sizeof(struct sctp_ipv6addr_param),
2505                                     (uint8_t *)&addr_store);
2506                         if (plen != sizeof(struct sctp_ipv6addr_param) ||
2507                             a6p == NULL) {
2508 #ifdef SCTP_DEBUG
2509                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2510                                         printf("process_initack_addrs: invalid IPv6 param length\n");
2511                                 }
2512 #endif /* SCTP_DEBUG */
2513                                 return;
2514                         }
2515                         memcpy(&sin6.sin6_addr, a6p->addr,
2516                             sizeof(struct in6_addr));
2517                         sa = (struct sockaddr *)&sin6;
2518                 } else if (ptype == SCTP_IPV4_ADDRESS) {
2519                         struct sctp_ipv4addr_param *a4p;
2520                         /* get the entire IPv4 address param */
2521 #ifdef SCTP_DEBUG
2522                         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2523                                 printf("process_initack_addrs: checking IPv4 param\n");
2524                         }
2525 #endif /* SCTP_DEBUG */
2526                         a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_ipv4addr_param), (uint8_t *)&addr_store);
2527                         if (plen != sizeof(struct sctp_ipv4addr_param) ||
2528                             a4p == NULL) {
2529 #ifdef SCTP_DEBUG
2530                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2531                                         printf("process_initack_addrs: invalid IPv4 param length\n");
2532                                 }
2533 #endif /* SCTP_DEBUG */
2534                                 return;
2535                         }
2536                         sin.sin_addr.s_addr = a4p->addr;
2537                         sa = (struct sockaddr *)&sin;
2538                 } else {
2539 #ifdef SCTP_DEBUG
2540                         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2541                                 printf("process_initack_addrs: skipping param type=%xh\n", ptype);
2542                         }
2543 #endif /* SCTP_DEBUG */
2544                         goto next_addr;
2545                 }
2546
2547                 /* see if this address really (still) exists */
2548                 ifa = sctp_find_ifa_by_addr(sa);
2549                 if (ifa == NULL) {
2550                         /* address doesn't exist anymore */
2551                         int status;
2552                         /* are ASCONFs allowed ? */
2553                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
2554                             stcb->asoc.peer_supports_asconf) {
2555                                 /* queue an ASCONF DEL_IP_ADDRESS */
2556                                 status = sctp_asconf_queue_add_sa(stcb, sa,
2557                                     SCTP_DEL_IP_ADDRESS);
2558                                 /*
2559                                  * if queued ok, and in correct state,
2560                                  * set the ASCONF timer
2561                                  */
2562                                 if (status == 0 &&
2563                                     SCTP_GET_STATE(&stcb->asoc) ==
2564                                     SCTP_STATE_OPEN) {
2565                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2566                                             stcb->sctp_ep, stcb,
2567                                             stcb->asoc.primary_destination);
2568                                 }
2569                         }
2570                 } else {
2571                         /* address still exists */
2572                         /*
2573                          * if subset bound, ep addr's managed by default
2574                          * if not doing ASCONF, add the address to the assoc
2575                          */
2576                         if ((stcb->sctp_ep->sctp_flags &
2577                              SCTP_PCB_FLAGS_BOUNDALL) == 0 &&
2578                             (stcb->sctp_ep->sctp_flags &
2579                              SCTP_PCB_FLAGS_DO_ASCONF) == 0) {
2580 #ifdef SCTP_DEBUG
2581                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2582                                         printf("process_initack_addrs: adding local addr to asoc\n");
2583                                 }
2584 #endif /* SCTP_DEBUG */
2585                                 sctp_add_local_addr_assoc(stcb, ifa);
2586                         }
2587                 }
2588
2589         next_addr:
2590                 /* get next parameter */
2591                 offset += SCTP_SIZE32(plen);
2592                 if ((offset + sizeof(struct sctp_paramhdr)) > length)
2593                         return;
2594                 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2595                     sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
2596         } /* while */
2597 }
2598
2599 /* FIX ME: need to verify return result for v6 address type if v6 disabled */
2600 /*
2601  * checks to see if a specific address is in the initack address list
2602  * returns 1 if found, 0 if not
2603  */
2604 static uint32_t
2605 sctp_addr_in_initack(struct sctp_tcb *stcb, struct mbuf *m, unsigned int offset,
2606     unsigned int length, struct sockaddr *sa)
2607 {
2608         struct sctp_paramhdr tmp_param, *ph;
2609         uint16_t plen, ptype;
2610         struct sctp_ipv6addr_param addr_store;
2611         struct sockaddr_in *sin;
2612         struct sctp_ipv4addr_param *a4p;
2613 #ifdef INET6
2614         struct sockaddr_in6 *sin6, sin6_tmp;
2615         struct sctp_ipv6addr_param *a6p;
2616 #endif /* INET6 */
2617
2618         if (
2619 #ifdef INET6
2620                 (sa->sa_family != AF_INET6) &&
2621 #endif /* INET6 */
2622                 (sa->sa_family != AF_INET))
2623                 return (0);
2624
2625 #ifdef SCTP_DEBUG
2626         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2627                 printf("find_initack_addr: starting search for ");
2628                 sctp_print_address(sa);
2629         }
2630 #endif /* SCTP_DEBUG */
2631         /* convert to upper bound */
2632         length += offset;
2633
2634         if ((offset + sizeof(struct sctp_paramhdr)) > length) {
2635 #ifdef SCTP_DEBUG
2636                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2637                         printf("find_initack_addr: invalid offset?\n");
2638                 }
2639 #endif /* SCTP_DEBUG */
2640                 return (0);
2641         }
2642
2643         /* go through the addresses in the init-ack */
2644         ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
2645             sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
2646         while (ph != NULL) {
2647                 ptype = ntohs(ph->param_type);
2648                 plen = ntohs(ph->param_length);
2649 #ifdef INET6
2650                 if (ptype == SCTP_IPV6_ADDRESS && sa->sa_family == AF_INET6) {
2651                         /* get the entire IPv6 address param */
2652 #ifdef SCTP_DEBUG
2653                         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2654                                 printf("addr_in_initack: checking IPv6 param\n");
2655                         }
2656 #endif /* SCTP_DEBUG */
2657                         a6p = (struct sctp_ipv6addr_param *)
2658                                 sctp_m_getptr(m, offset,
2659                                     sizeof(struct sctp_ipv6addr_param),
2660                                     (uint8_t *)&addr_store);
2661                         if (plen != sizeof(struct sctp_ipv6addr_param) ||
2662                             ph == NULL) {
2663 #ifdef SCTP_DEBUG
2664                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2665                                         printf("addr_in_initack: invalid IPv6 param length\n");
2666                                 }
2667 #endif /* SCTP_DEBUG */
2668                                 return (0);
2669                         }
2670                         sin6 = (struct sockaddr_in6 *)sa;
2671                         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2672                                 /* create a copy and clear scope */
2673                                 memcpy(&sin6_tmp, sin6,
2674                                     sizeof(struct sockaddr_in6));
2675                                 sin6 = &sin6_tmp;
2676                                 in6_clearscope(&sin6->sin6_addr);
2677                         }
2678                         if (memcmp(&sin6->sin6_addr, a6p->addr,
2679                             sizeof(struct in6_addr)) == 0) {
2680                                 /* found it */
2681 #ifdef SCTP_DEBUG
2682                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2683                                         printf("addr_in_initack: found IPv6 addr\n");
2684                                 }
2685 #endif /* SCTP_DEBUG */
2686                                 return (1);
2687                         }
2688                 } else
2689 #endif /* INET6 */
2690
2691                 if (ptype == SCTP_IPV4_ADDRESS &&
2692                     sa->sa_family == AF_INET) {
2693                         /* get the entire IPv4 address param */
2694 #ifdef SCTP_DEBUG
2695                         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2696                                 printf("addr_in_initack: checking IPv4 param\n");
2697                         }
2698 #endif /* SCTP_DEBUG */
2699                         a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m,
2700                             offset, sizeof(struct sctp_ipv4addr_param),
2701                             (uint8_t *)&addr_store);
2702                         if (plen != sizeof(struct sctp_ipv4addr_param) ||
2703                             ph == NULL) {
2704 #ifdef SCTP_DEBUG
2705                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2706                                         printf("addr_in_initack: invalid IPv4 param length\n");
2707                                 }
2708 #endif /* SCTP_DEBUG */
2709                                 return (0);
2710                         }
2711                         sin = (struct sockaddr_in *)sa;
2712                         if (sin->sin_addr.s_addr == a4p->addr) {
2713                                 /* found it */
2714 #ifdef SCTP_DEBUG
2715                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2716                                         printf("addr_in_initack: found IPv4 addr\n");
2717                                 }
2718 #endif /* SCTP_DEBUG */
2719                                 return (1);
2720                         }
2721                 } else {
2722 #ifdef SCTP_DEBUG
2723                         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2724                                 printf("addr_in_initack: skipping param type=%xh\n", ptype);
2725                         }
2726 #endif /* SCTP_DEBUG */
2727                 }
2728                 /* get next parameter */
2729                 offset += SCTP_SIZE32(plen);
2730                 if (offset + sizeof(struct sctp_paramhdr) > length)
2731                         return (0);
2732                 ph = (struct sctp_paramhdr *)
2733                         sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
2734                             (uint8_t *)&tmp_param);
2735         } /* while */
2736         /* not found! */
2737 #ifdef SCTP_DEBUG
2738         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2739                 printf("addr_in_initack: not found!\n");
2740         }
2741 #endif /* SCTP_DEBUG */
2742         return (0);
2743 }
2744
2745 /*
2746  * makes sure that the current endpoint local addr list is consistent
2747  * with the new association (eg. subset bound, asconf allowed)
2748  * adds addresses as necessary
2749  */
2750 static void
2751 sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2752     int length, struct sockaddr *init_addr)
2753 {
2754         struct sctp_laddr *laddr;
2755
2756         /* go through the endpoint list */
2757         LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
2758                 /* be paranoid and validate the laddr */
2759                 if (laddr->ifa == NULL) {
2760 #ifdef SCTP_DEBUG
2761                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2762                                 printf("check_addr_list_ep: laddr->ifa is NULL");
2763                         }
2764 #endif
2765                         continue;
2766                 }
2767                 if (laddr->ifa->ifa_addr == NULL) {
2768 #ifdef SCTP_DEBUG
2769                         if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2770                                 printf("check_addr_list_ep: laddr->ifa->ifa_addr is NULL");
2771                         }
2772 #endif
2773                         continue;
2774                 }
2775                 /* do i have it implicitly? */
2776                 if (sctp_cmpaddr(laddr->ifa->ifa_addr, init_addr)) {
2777 #ifdef SCTP_DEBUG
2778                         if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2779                                 printf("check_address_list_all: skipping ");
2780                                 sctp_print_address(laddr->ifa->ifa_addr);
2781                         }
2782 #endif /* SCTP_DEBUG */
2783                         continue;
2784                 }
2785                 /* check to see if in the init-ack */
2786                 if (!sctp_addr_in_initack(stcb, m, offset, length,
2787                                           laddr->ifa->ifa_addr)) {
2788                         /* try to add it */
2789                         sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
2790                             SCTP_ADD_IP_ADDRESS);
2791                 }
2792         }
2793 }
2794
2795 /*
2796  * makes sure that the current kernel address list is consistent
2797  * with the new association (with all addrs bound)
2798  * adds addresses as necessary
2799  */
2800 static void
2801 sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2802     int length, struct sockaddr *init_addr, uint16_t local_scope,
2803     uint16_t site_scope, uint16_t ipv4_scope, uint16_t loopback_scope)
2804 {
2805         struct ifnet *ifn;
2806         struct ifaddr *ifa;
2807
2808         /* go through all our known interfaces */
2809         TAILQ_FOREACH(ifn, &ifnet, if_list) {
2810                 if (loopback_scope == 0 && ifn->if_type == IFT_LOOP) {
2811                         /* skip loopback interface */
2812                         continue;
2813                 }
2814
2815                 /* go through each interface address */
2816                 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
2817                         /* do i have it implicitly? */
2818                         if (sctp_cmpaddr(ifa->ifa_addr, init_addr)) {
2819 #ifdef SCTP_DEBUG
2820                                 if (sctp_debug_on & SCTP_DEBUG_ASCONF2) {
2821                                         printf("check_address_list_all: skipping ");
2822                                         sctp_print_address(ifa->ifa_addr);
2823                                 }
2824 #endif /* SCTP_DEBUG */
2825                                 continue;
2826                         }
2827                         /* check to see if in the init-ack */
2828                         if (!sctp_addr_in_initack(stcb, m, offset, length,
2829                             ifa->ifa_addr)) {
2830                                 /* try to add it */
2831                                 sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
2832                                     ifa, SCTP_ADD_IP_ADDRESS);
2833                         }
2834                 } /* end foreach ifa */
2835         } /* end foreach ifn */
2836 }
2837
2838 /*
2839  * validates an init-ack chunk (from a cookie-echo) with current addresses
2840  * adds addresses from the init-ack into our local address list, if needed
2841  * queues asconf adds/deletes addresses as needed and makes appropriate
2842  * list changes for source address selection
2843  * m, offset: points to the start of the address list in an init-ack chunk
2844  * length: total length of the address params only
2845  * init_addr: address where my INIT-ACK was sent from
2846  */
2847 void
2848 sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
2849     int length, struct sockaddr *init_addr, uint16_t local_scope,
2850     uint16_t site_scope, uint16_t ipv4_scope, uint16_t loopback_scope)
2851 {
2852
2853         /* process the local addresses in the initack */
2854         sctp_process_initack_addresses(stcb, m, offset, length);
2855
2856         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2857                 /* bound all case */
2858                 sctp_check_address_list_all(stcb, m, offset, length, init_addr,
2859                     local_scope, site_scope, ipv4_scope, loopback_scope);
2860         } else {
2861                 /* subset bound case */
2862                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
2863                         /* asconf's allowed */
2864                         sctp_check_address_list_ep(stcb, m, offset, length,
2865                             init_addr);
2866                 }
2867                 /* else, no asconfs allowed, so what we sent is what we get */
2868         }
2869 }
2870
2871 /*
2872  * sctp_bindx() support
2873  */
2874 uint32_t
2875 sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa, uint16_t type)
2876 {
2877         struct ifaddr *ifa;
2878
2879         if (sa->sa_len == 0)
2880                 return (EINVAL);
2881
2882         ifa = sctp_find_ifa_by_addr(sa);
2883         if (ifa != NULL) {
2884 #ifdef INET6
2885                 if (ifa->ifa_addr->sa_family == AF_INET6) {
2886                         struct in6_ifaddr *ifa6;
2887                         ifa6 = (struct in6_ifaddr *)ifa;
2888                         if (IFA6_IS_DEPRECATED(ifa6) ||
2889                             (ifa6->ia6_flags & (IN6_IFF_DETACHED |
2890                              IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) {
2891                                 /* Can't bind a non-existent addr. */
2892                                 return (EINVAL);
2893                         }
2894                 }
2895 #endif /* INET6 */
2896                 /* add this address */
2897                 sctp_addr_mgmt_ep(inp, ifa, type);
2898         } else {
2899                 /* invalid address! */
2900 #ifdef SCTP_DEBUG
2901                 if (sctp_debug_on & SCTP_DEBUG_ASCONF1) {
2902                         printf("addr_mgmt_ep_sa: got invalid address!\n");
2903                 }
2904 #endif /* SCTP_DEBUG */
2905                 return (EADDRNOTAVAIL);
2906         }
2907         return (0);
2908 }