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