xref: /freebsd/sbin/ifconfig/af_nd6.c (revision 401ab69cff8fa2320a9f8ea4baa114a6da6c952b)
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 #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 #include <netinet6/nd6.h>
56 
57 #include "ifconfig.h"
58 
59 #define	MAX_SYSCTL_TRY	5
60 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
61 #define	ND6BITS	"\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \
62 		"\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \
63 		"\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD" \
64 		"\012IPV6_ONLY\013IPV6_ONLY_MANUAL" \
65 		"\020DEFAULTIF"
66 #else
67 #define	ND6BITS	"\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \
68 		"\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \
69 		"\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD\020DEFAULTIF"
70 #endif
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 
150 	strlcpy(nd.ifname, ctx->ifname, 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)");
154 		return;
155 	}
156 	error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
157 	if (error) {
158 		if (errno != EPFNOSUPPORT)
159 			warn("ioctl(SIOCGIFINFO_IN6)");
160 		close(s6);
161 		return;
162 	}
163 	isdefif = isnd6defif(ctx, s6);
164 	close(s6);
165 	if (nd.ndi.flags == 0 && !isdefif)
166 		return;
167 	printb("\tnd6 options",
168 	    (unsigned int)(nd.ndi.flags | (isdefif << 15)), ND6BITS);
169 	putchar('\n');
170 }
171