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