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