xref: /freebsd/sbin/ifconfig/af_nd6.c (revision 75e1fea68aaa613a20dfdcd0c59dd403aca02c49)
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 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
66 	[9]  = "IPV6_ONLY",
67 	[10] = "IPV6_ONLY_MANUAL",
68 #endif
69 	[15] = "DEFAULTIF",
70 };
71 
72 static int isnd6defif(if_ctx *ctx, int s);
73 void setnd6flags(if_ctx *, const char *, int);
74 void setnd6defif(if_ctx *,const char *, int);
75 void nd6_status(if_ctx *);
76 
77 void
78 setnd6flags(if_ctx *ctx, const char *dummyaddr __unused, int d)
79 {
80 	struct in6_ndireq nd = {};
81 	int error;
82 
83 	strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
84 	error = ioctl_ctx(ctx, SIOCGIFINFO_IN6, &nd);
85 	if (error) {
86 		warn("ioctl(SIOCGIFINFO_IN6)");
87 		return;
88 	}
89 	if (d < 0)
90 		nd.ndi.flags &= ~(-d);
91 	else
92 		nd.ndi.flags |= d;
93 	error = ioctl_ctx(ctx, SIOCSIFINFO_IN6, (caddr_t)&nd);
94 	if (error)
95 		warn("ioctl(SIOCSIFINFO_IN6)");
96 }
97 
98 void
99 setnd6defif(if_ctx *ctx, const char *dummyaddr __unused, int d)
100 {
101 	struct in6_ndifreq ndifreq = {};
102 	int ifindex;
103 	int error;
104 
105 	strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname));
106 
107 	if (d < 0) {
108 		if (isnd6defif(ctx, ctx->io_s)) {
109 			/* ifindex = 0 means to remove default if */
110 			ifindex = 0;
111 		} else
112 			return;
113 	} else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) {
114 		warn("if_nametoindex(%s)", ndifreq.ifname);
115 		return;
116 	}
117 
118 	ndifreq.ifindex = ifindex;
119 	error = ioctl_ctx(ctx, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
120 	if (error)
121 		warn("ioctl(SIOCSDEFIFACE_IN6)");
122 }
123 
124 static int
125 isnd6defif(if_ctx *ctx, int s)
126 {
127 	struct in6_ndifreq ndifreq = {};
128 	unsigned int ifindex;
129 	int error;
130 
131 	strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname));
132 
133 	ifindex = if_nametoindex(ndifreq.ifname);
134 	error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq);
135 	if (error) {
136 		warn("ioctl(SIOCGDEFIFACE_IN6)");
137 		return (error);
138 	}
139 	return (ndifreq.ifindex == ifindex);
140 }
141 
142 void
143 nd6_status(if_ctx *ctx)
144 {
145 	struct in6_ndireq nd = {};
146 	int s6;
147 	int error;
148 	int isdefif;
149 	uint32_t bits;
150 
151 	strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
152 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
153 		if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
154 			warn("socket(AF_INET6, SOCK_DGRAM)");
155 		return;
156 	}
157 	error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
158 	if (error) {
159 		if (errno != EPFNOSUPPORT)
160 			warn("ioctl(SIOCGIFINFO_IN6)");
161 		close(s6);
162 		return;
163 	}
164 	isdefif = isnd6defif(ctx, s6);
165 	close(s6);
166 	if (nd.ndi.flags == 0 && !isdefif)
167 		return;
168 	bits = (nd.ndi.flags | (isdefif << 15));
169 	printf("\tnd6 options=%x", bits);
170 	print_bits("options", &bits, 1, ND6BITS, nitems(ND6BITS));
171 	putchar('\n');
172 }
173