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
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
33 #include <netinet/in.h>
40 #include "libifconfig.h"
41 #include "libifconfig_internal.h"
45 inet6_prefixlen(struct in6_addr *addr)
50 for (i = 0; i < 4; i++) {
51 int x = ffs(ntohl(addr->__u6_addr.__u6_addr32[i]));
52 prefixlen += x == 0 ? 0 : 33 - x;
61 ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
62 const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr)
64 struct sockaddr_in6 *netmask;
65 struct in6_ifreq ifr6;
67 bzero(addr, sizeof(*addr));
70 addr->sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
72 /* Set the destination address */
73 if (ifa->ifa_flags & IFF_POINTOPOINT) {
74 addr->dstin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
77 /* Set the prefixlen */
78 netmask = (struct sockaddr_in6 *)ifa->ifa_netmask;
79 addr->prefixlen = inet6_prefixlen(&netmask->sin6_addr);
82 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
83 ifr6.ifr_addr = *addr->sin6;
84 if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
87 addr->flags = ifr6.ifr_ifru.ifru_flags6;
89 /* Set the lifetimes */
90 memset(&addr->lifetime, 0, sizeof(addr->lifetime));
91 ifr6.ifr_addr = *addr->sin6;
92 if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
95 addr->lifetime = ifr6.ifr_ifru.ifru_lifetime; /* struct copy */
99 addr->vhid = ((struct if_data *)ifa->ifa_data)->ifi_vhid;