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