xref: /f-stack/tools/ifconfig/af_nd6.c (revision 9e60a85f)
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 <netinet/in_var.h>
50 #include <arpa/inet.h>
51 #include <netdb.h>
52 
53 #include <netinet6/in6_var.h>
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 		"\007NO_RADR\010NO_PREFER_IFACE\011IGNORELOOP\012NO_DAD" \
62 		"\020DEFAULTIF"
63 
64 static int isnd6defif(int);
65 void setnd6flags(const char *, int, int, const struct afswtch *);
66 void setnd6defif(const char *, int, int, const struct afswtch *);
67 void nd6_status(int);
68 
69 void
70 setnd6flags(const char *dummyaddr __unused,
71 	int d, int s,
72 	const struct afswtch *afp)
73 {
74 	struct in6_ndireq nd;
75 	int error;
76 
77 	memset(&nd, 0, sizeof(nd));
78 	strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
79 #ifndef FSTACK
80 	error = ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd);
81 #else
82 	error = ioctl_va(s, SIOCGIFINFO_IN6, (caddr_t)&nd, 1, AF_INET6);
83 #endif
84 	if (error) {
85 		warn("ioctl(SIOCGIFINFO_IN6)");
86 		return;
87 	}
88 	if (d < 0)
89 		nd.ndi.flags &= ~(-d);
90 	else
91 		nd.ndi.flags |= d;
92 
93 #ifndef FSTACK
94 	error = ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd);
95 #else
96 	error = ioctl_va(s, SIOCSIFINFO_IN6, (caddr_t)&nd, 1, AF_INET6);
97 #endif
98 	if (error)
99 		warn("ioctl(SIOCSIFINFO_IN6)");
100 }
101 
102 void
103 setnd6defif(const char *dummyaddr __unused,
104 	int d, int s,
105 	const struct afswtch *afp)
106 {
107 	struct in6_ndifreq ndifreq;
108 	int ifindex;
109 	int error;
110 
111 	memset(&ndifreq, 0, sizeof(ndifreq));
112 	strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
113 
114 	if (d < 0) {
115 		if (isnd6defif(s)) {
116 			/* ifindex = 0 means to remove default if */
117 			ifindex = 0;
118 		} else
119 			return;
120 	} else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) {
121 		warn("if_nametoindex(%s)", ndifreq.ifname);
122 		return;
123 	}
124 
125 	ndifreq.ifindex = ifindex;
126 #ifndef FSTACK
127 	error = ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
128 #else
129 	error = ioctl_va(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq, 1, AF_INET6);
130 #endif
131 	if (error)
132 		warn("ioctl(SIOCSDEFIFACE_IN6)");
133 }
134 
135 static int
136 isnd6defif(int s)
137 {
138 	struct in6_ndifreq ndifreq;
139 	unsigned int ifindex;
140 	int error;
141 
142 	memset(&ndifreq, 0, sizeof(ndifreq));
143 	strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
144 
145 	ifindex = if_nametoindex(ndifreq.ifname);
146 #ifndef FSTACK
147 	error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq);
148 #else
149 	error = ioctl_va(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq, 1, AF_INET6);
150 #endif
151 	if (error) {
152 		warn("ioctl(SIOCGDEFIFACE_IN6)");
153 		return (error);
154 	}
155 	return (ndifreq.ifindex == ifindex);
156 }
157 
158 void
159 nd6_status(int s)
160 {
161 	struct in6_ndireq nd;
162 	int s6;
163 	int error;
164 	int isdefif;
165 
166 	memset(&nd, 0, sizeof(nd));
167 	strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
168 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
169 		if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
170 			warn("socket(AF_INET6, SOCK_DGRAM)");
171 		return;
172 	}
173 
174 #ifndef FSTACK
175 	error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
176 #else
177 	error = ioctl_va(s, SIOCGIFINFO_IN6, (caddr_t)&nd, 1, AF_INET6);
178 #endif
179 	if (error) {
180 		if (errno != EPFNOSUPPORT)
181 			warn("ioctl(SIOCGIFINFO_IN6)");
182 		close(s6);
183 		return;
184 	}
185 	isdefif = isnd6defif(s6);
186 	close(s6);
187 	if (nd.ndi.flags == 0 && !isdefif)
188 		return;
189 	printb("\tnd6 options",
190 	    (unsigned int)(nd.ndi.flags | (isdefif << 15)), ND6BITS);
191 	putchar('\n');
192 }
193