xref: /freebsd/sbin/ifconfig/af_nd6.c (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
1 /*
2  * Copyright (c) 2009 Hiroki Sato.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef lint
27 static const char rcsid[] =
28   "$FreeBSD$";
29 #endif /* not lint */
30 
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
35 #include <net/if.h>
36 #include <net/route.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <ifaddrs.h>
45 
46 #include <arpa/inet.h>
47 
48 #include <netinet/in.h>
49 #include <net/if_var.h>
50 #include <netinet/in_var.h>
51 #include <arpa/inet.h>
52 #include <netdb.h>
53 
54 #include <netinet6/nd6.h>
55 
56 #include "ifconfig.h"
57 
58 #define	MAX_SYSCTL_TRY	5
59 #define	ND6BITS	"\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \
60 		"\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \
61 		"\020DEFAULTIF"
62 
63 static int isnd6defif(int);
64 void setnd6flags(const char *, int, int, const struct afswtch *);
65 void setnd6defif(const char *, int, int, const struct afswtch *);
66 
67 void
68 setnd6flags(const char *dummyaddr __unused,
69 	int d, int s,
70 	const struct afswtch *afp)
71 {
72 	struct in6_ndireq nd;
73 	int error;
74 
75 	memset(&nd, 0, sizeof(nd));
76 	strncpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
77 	error = ioctl(s, SIOCGIFINFO_IN6, &nd);
78 	if (error) {
79 		warn("ioctl(SIOCGIFINFO_IN6)");
80 		return;
81 	}
82 	if (d < 0)
83 		nd.ndi.flags &= ~(-d);
84 	else
85 		nd.ndi.flags |= d;
86 	error = ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd);
87 	if (error)
88 		warn("ioctl(SIOCSIFINFO_IN6)");
89 }
90 
91 void
92 setnd6defif(const char *dummyaddr __unused,
93 	int d, int s,
94 	const struct afswtch *afp)
95 {
96 	struct in6_ndifreq ndifreq;
97 	int ifindex;
98 	int error;
99 
100 	memset(&ndifreq, 0, sizeof(ndifreq));
101 	strncpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
102 
103 	if (d < 0) {
104 		if (isnd6defif(s)) {
105 			/* ifindex = 0 means to remove default if */
106 			ifindex = 0;
107 		} else
108 			return;
109 	} else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) {
110 		warn("if_nametoindex(%s)", ndifreq.ifname);
111 		return;
112 	}
113 
114 	ndifreq.ifindex = ifindex;
115 	error = ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
116 	if (error)
117 		warn("ioctl(SIOCSDEFIFACE_IN6)");
118 }
119 
120 static int
121 isnd6defif(int s)
122 {
123 	struct in6_ndifreq ndifreq;
124 	unsigned int ifindex;
125 	int error;
126 
127 	memset(&ndifreq, 0, sizeof(ndifreq));
128 	strncpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
129 
130 	ifindex = if_nametoindex(ndifreq.ifname);
131 	error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq);
132 	if (error) {
133 		warn("ioctl(SIOCGDEFIFACE_IN6)");
134 		return (error);
135 	}
136 	return (ndifreq.ifindex == ifindex);
137 }
138 
139 static void
140 nd6_status(int s)
141 {
142 	struct in6_ndireq nd;
143 	struct rt_msghdr *rtm;
144 	size_t needed;
145 	char *buf, *next;
146 	int mib[6], ntry;
147 	int s6;
148 	int error;
149 	int isinet6, isdefif;
150 
151 	/* Check if the interface has at least one IPv6 address. */
152 	mib[0] = CTL_NET;
153 	mib[1] = PF_ROUTE;
154 	mib[2] = 0;
155 	mib[3] = AF_INET6;
156 	mib[4] = NET_RT_IFLIST;
157 	mib[5] = if_nametoindex(ifr.ifr_name);
158 
159 	/* Try to prevent a race between two sysctls. */
160 	ntry = 0;
161 	do {
162 		error = sysctl(mib, 6, NULL, &needed, NULL, 0);
163 		if (error) {
164 			warn("sysctl(NET_RT_IFLIST)/estimate");
165 			return;
166 		}
167 		buf = malloc(needed);
168 		if (buf == NULL) {
169 			warn("malloc for sysctl(NET_RT_IFLIST) failed");
170 			return;
171 		}
172 		if ((error = sysctl(mib, 6, buf, &needed, NULL, 0)) < 0) {
173 			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
174 				warn("sysctl(NET_RT_IFLIST)/get");
175 				free(buf);
176 				return;
177 			}
178 			free(buf);
179 			buf = NULL;
180 		}
181 	} while (buf == NULL);
182 
183 	isinet6 = 0;
184 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
185 		rtm = (struct rt_msghdr *)next;
186 
187 		if (rtm->rtm_version != RTM_VERSION)
188 			continue;
189 		if (rtm->rtm_type == RTM_NEWADDR) {
190 			isinet6 = 1;
191 			break;
192 		}
193 	}
194 	free(buf);
195 	if (!isinet6)
196 		return;
197 
198 	memset(&nd, 0, sizeof(nd));
199 	strncpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
200 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
201 		warn("socket(AF_INET6, SOCK_DGRAM)");
202 		return;
203 	}
204 	error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
205 	if (error) {
206 		warn("ioctl(SIOCGIFINFO_IN6)");
207 		close(s6);
208 		return;
209 	}
210 	isdefif = isnd6defif(s6);
211 	close(s6);
212 	if (nd.ndi.flags == 0 && !isdefif)
213 		return;
214 	printb("\tnd6 options",
215 	    (unsigned int)(nd.ndi.flags | (isdefif << 15)), ND6BITS);
216 	putchar('\n');
217 }
218 
219 static struct afswtch af_nd6 = {
220 	.af_name	= "nd6",
221 	.af_af		= AF_LOCAL,
222 	.af_other_status= nd6_status,
223 };
224 
225 static __constructor void
226 nd6_ctor(void)
227 {
228 	af_register(&af_nd6);
229 }
230