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