1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
32 #include <net/if.h>
33 #include <net/route.h>
34
35 #include <err.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <ifaddrs.h>
42
43 #include <arpa/inet.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
49
50 #include <netinet6/nd6.h>
51
52 #include "ifconfig.h"
53
54 #define MAX_SYSCTL_TRY 5
55 static const char *ND6BITS[] = {
56 [0] = "PERFORMNUD",
57 [1] = "ACCEPT_RTADV",
58 [2] = "PREFER_SOURCE",
59 [3] = "IFDISABLED",
60 [4] = "DONT_SET_IFROUTE",
61 [5] = "AUTO_LINKLOCAL",
62 [6] = "NO_RADR",
63 [7] = "NO_PREFER_IFACE",
64 [8] = "NO_DAD",
65 [11] = "STABLEADDR",
66 [15] = "DEFAULTIF",
67 };
68
69 static int isnd6defif(if_ctx *ctx, int s);
70 void setnd6flags(if_ctx *, const char *, int);
71 void setnd6defif(if_ctx *,const char *, int);
72 void nd6_status(if_ctx *);
73
74 void
setnd6flags(if_ctx * ctx,const char * dummyaddr __unused,int d)75 setnd6flags(if_ctx *ctx, const char *dummyaddr __unused, int d)
76 {
77 struct in6_ndireq nd = {};
78 int error;
79
80 strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
81 error = ioctl_ctx(ctx, SIOCGIFINFO_IN6, &nd);
82 if (error) {
83 warn("ioctl(SIOCGIFINFO_IN6)");
84 return;
85 }
86 if (d < 0)
87 nd.ndi.flags &= ~(-d);
88 else
89 nd.ndi.flags |= d;
90 error = ioctl_ctx(ctx, SIOCSIFINFO_IN6, (caddr_t)&nd);
91 if (error)
92 warn("ioctl(SIOCSIFINFO_IN6)");
93 }
94
95 void
setnd6defif(if_ctx * ctx,const char * dummyaddr __unused,int d)96 setnd6defif(if_ctx *ctx, const char *dummyaddr __unused, int d)
97 {
98 struct in6_ndifreq ndifreq = {};
99 int ifindex;
100 int error;
101
102 strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname));
103
104 if (d < 0) {
105 if (isnd6defif(ctx, ctx->io_s)) {
106 /* ifindex = 0 means to remove default if */
107 ifindex = 0;
108 } else
109 return;
110 } else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) {
111 warn("if_nametoindex(%s)", ndifreq.ifname);
112 return;
113 }
114
115 ndifreq.ifindex = ifindex;
116 error = ioctl_ctx(ctx, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
117 if (error)
118 warn("ioctl(SIOCSDEFIFACE_IN6)");
119 }
120
121 static int
isnd6defif(if_ctx * ctx,int s)122 isnd6defif(if_ctx *ctx, int s)
123 {
124 struct in6_ndifreq ndifreq = {};
125 unsigned int ifindex;
126 int error;
127
128 strlcpy(ndifreq.ifname, ctx->ifname, 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 void
nd6_status(if_ctx * ctx)140 nd6_status(if_ctx *ctx)
141 {
142 struct in6_ndireq nd = {};
143 int s6;
144 int error;
145 int isdefif;
146 uint32_t bits;
147
148 strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
149 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
150 if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
151 warn("socket(AF_INET6, SOCK_DGRAM)");
152 return;
153 }
154 error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
155 if (error) {
156 if (errno != EPFNOSUPPORT)
157 warn("ioctl(SIOCGIFINFO_IN6)");
158 close(s6);
159 return;
160 }
161 isdefif = isnd6defif(ctx, s6);
162 close(s6);
163 if (nd.ndi.flags == 0 && !isdefif)
164 return;
165 bits = (nd.ndi.flags | (isdefif << 15));
166 printf("\tnd6 options=%x", bits);
167 print_bits("options", &bits, 1, ND6BITS, nitems(ND6BITS));
168 putchar('\n');
169 }
170