1 /*
2  * Copyright (c) 1983, 1993
3  *  The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 
34 #include <net/if.h>
35 #include <netinet/in.h>
36 
37 #include <errno.h>
38 #include <ifaddrs.h>
39 #include <string.h>
40 #include <strings.h>
41 
42 #include "libifconfig.h"
43 #include "libifconfig_internal.h"
44 
45 
46 static int
47 inet6_prefixlen(struct in6_addr *addr)
48 {
49 	int prefixlen = 0;
50 	int i;
51 
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;
55 		if (x != 1) {
56 			break;
57 		}
58 	}
59 	return (prefixlen);
60 }
61 
62 int
63 ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
64     const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr)
65 {
66 	struct sockaddr_in6 *netmask;
67 	struct in6_ifreq ifr6;
68 
69 	bzero(addr, sizeof(*addr));
70 
71 	/* Set the address */
72 	addr->sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
73 
74 	/* Set the destination address */
75 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
76 		addr->dstin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
77 	}
78 
79 	/* Set the prefixlen */
80 	netmask = (struct sockaddr_in6 *)ifa->ifa_netmask;
81 	addr->prefixlen = inet6_prefixlen(&netmask->sin6_addr);
82 
83 	/* Set the flags */
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) {
87 		return (-1);
88 	}
89 	addr->flags = ifr6.ifr_ifru.ifru_flags6;
90 
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) {
95 		return (-1);
96 	}
97 	addr->lifetime = ifr6.ifr_ifru.ifru_lifetime; /* struct copy */
98 
99 	/* Set the vhid */
100 	if (ifa->ifa_data) {
101 		addr->vhid = ((struct if_data *)ifa->ifa_data)->ifi_vhid;
102 	}
103 
104 	return (0);
105 }
106