2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
35 #include <netinet/in.h>
42 #include "libifconfig.h"
43 #include "libifconfig_internal.h"
47 inet6_prefixlen(struct in6_addr *addr)
52 for (i = 0; i < 4; i++) {
53 int x = ffs(ntohl(addr->__u6_addr.__u6_addr32[i]));
54 prefixlen += x == 0 ? 0 : 33 - x;
63 ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
64 const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr)
66 struct sockaddr_in6 *netmask;
67 struct in6_ifreq ifr6;
69 bzero(addr, sizeof(*addr));
72 addr->sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
74 /* Set the destination address */
75 if (ifa->ifa_flags & IFF_POINTOPOINT) {
76 addr->dstin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
79 /* Set the prefixlen */
80 netmask = (struct sockaddr_in6 *)ifa->ifa_netmask;
81 addr->prefixlen = inet6_prefixlen(&netmask->sin6_addr);
84 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
85 ifr6.ifr_addr = *addr->sin6;
86 if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
89 addr->flags = ifr6.ifr_ifru.ifru_flags6;
91 /* Set the lifetimes */
92 memset(&addr->lifetime, 0, sizeof(addr->lifetime));
93 ifr6.ifr_addr = *addr->sin6;
94 if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
97 addr->lifetime = ifr6.ifr_ifru.ifru_lifetime; /* struct copy */
101 addr->vhid = ((struct if_data *)ifa->ifa_data)->ifi_vhid;