xref: /freebsd/lib/libifconfig/libifconfig_inet6.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 
32 #include <net/if.h>
33 #include <netinet/in.h>
34 
35 #include <errno.h>
36 #include <ifaddrs.h>
37 #include <string.h>
38 #include <strings.h>
39 
40 #include "libifconfig.h"
41 #include "libifconfig_internal.h"
42 
43 
44 static int
45 inet6_prefixlen(struct in6_addr *addr)
46 {
47 	int prefixlen = 0;
48 	int i;
49 
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;
53 		if (x != 1) {
54 			break;
55 		}
56 	}
57 	return (prefixlen);
58 }
59 
60 int
61 ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
62     const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr)
63 {
64 	struct sockaddr_in6 *netmask;
65 	struct in6_ifreq ifr6;
66 
67 	bzero(addr, sizeof(*addr));
68 
69 	/* Set the address */
70 	addr->sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
71 
72 	/* Set the destination address */
73 	if (ifa->ifa_flags & IFF_POINTOPOINT) {
74 		addr->dstin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
75 	}
76 
77 	/* Set the prefixlen */
78 	netmask = (struct sockaddr_in6 *)ifa->ifa_netmask;
79 	addr->prefixlen = inet6_prefixlen(&netmask->sin6_addr);
80 
81 	/* Set the flags */
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) {
85 		return (-1);
86 	}
87 	addr->flags = ifr6.ifr_ifru.ifru_flags6;
88 
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) {
93 		return (-1);
94 	}
95 	addr->lifetime = ifr6.ifr_ifru.ifru_lifetime; /* struct copy */
96 
97 	/* Set the vhid */
98 	if (ifa->ifa_data) {
99 		addr->vhid = ((struct if_data *)ifa->ifa_data)->ifi_vhid;
100 	}
101 
102 	return (0);
103 }
104