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