2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Hiroki Sato. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 static const char rcsid[] =
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
38 #include <net/route.h>
48 #include <arpa/inet.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <arpa/inet.h>
55 #include <netinet6/nd6.h>
59 #define MAX_SYSCTL_TRY 5
60 #define ND6BITS "\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \
61 "\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \
62 "\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD\020DEFAULTIF"
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 *);
70 setnd6flags(const char *dummyaddr __unused,
72 const struct afswtch *afp)
77 memset(&nd, 0, sizeof(nd));
78 strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
79 error = ioctl(s, SIOCGIFINFO_IN6, &nd);
81 warn("ioctl(SIOCGIFINFO_IN6)");
85 nd.ndi.flags &= ~(-d);
88 error = ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd);
90 warn("ioctl(SIOCSIFINFO_IN6)");
94 setnd6defif(const char *dummyaddr __unused,
96 const struct afswtch *afp)
98 struct in6_ndifreq ndifreq;
102 memset(&ndifreq, 0, sizeof(ndifreq));
103 strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
107 /* ifindex = 0 means to remove default if */
111 } else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) {
112 warn("if_nametoindex(%s)", ndifreq.ifname);
116 ndifreq.ifindex = ifindex;
117 error = ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
119 warn("ioctl(SIOCSDEFIFACE_IN6)");
125 struct in6_ndifreq ndifreq;
126 unsigned int ifindex;
129 memset(&ndifreq, 0, sizeof(ndifreq));
130 strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
132 ifindex = if_nametoindex(ndifreq.ifname);
133 error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq);
135 warn("ioctl(SIOCGDEFIFACE_IN6)");
138 return (ndifreq.ifindex == ifindex);
144 struct in6_ndireq nd;
149 memset(&nd, 0, sizeof(nd));
150 strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
151 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
152 if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
153 warn("socket(AF_INET6, SOCK_DGRAM)");
156 error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
158 if (errno != EPFNOSUPPORT)
159 warn("ioctl(SIOCGIFINFO_IN6)");
163 isdefif = isnd6defif(s6);
165 if (nd.ndi.flags == 0 && !isdefif)
167 printb("\tnd6 options",
168 (unsigned int)(nd.ndi.flags | (isdefif << 15)), ND6BITS);