xref: /freebsd/sbin/ifconfig/af_nd6.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
56 #define	ND6BITS	"\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \
57 		"\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \
58 		"\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD" \
59 		"\012IPV6_ONLY\013IPV6_ONLY_MANUAL" \
60 		"\020DEFAULTIF"
61 #else
62 #define	ND6BITS	"\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \
63 		"\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \
64 		"\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD\020DEFAULTIF"
65 #endif
66 
67 static int isnd6defif(if_ctx *ctx, int s);
68 void setnd6flags(if_ctx *, const char *, int);
69 void setnd6defif(if_ctx *,const char *, int);
70 void nd6_status(if_ctx *);
71 
72 void
73 setnd6flags(if_ctx *ctx, const char *dummyaddr __unused, int d)
74 {
75 	struct in6_ndireq nd = {};
76 	int error;
77 
78 	strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
79 	error = ioctl_ctx(ctx, SIOCGIFINFO_IN6, &nd);
80 	if (error) {
81 		warn("ioctl(SIOCGIFINFO_IN6)");
82 		return;
83 	}
84 	if (d < 0)
85 		nd.ndi.flags &= ~(-d);
86 	else
87 		nd.ndi.flags |= d;
88 	error = ioctl_ctx(ctx, SIOCSIFINFO_IN6, (caddr_t)&nd);
89 	if (error)
90 		warn("ioctl(SIOCSIFINFO_IN6)");
91 }
92 
93 void
94 setnd6defif(if_ctx *ctx, const char *dummyaddr __unused, int d)
95 {
96 	struct in6_ndifreq ndifreq = {};
97 	int ifindex;
98 	int error;
99 
100 	strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname));
101 
102 	if (d < 0) {
103 		if (isnd6defif(ctx, ctx->io_s)) {
104 			/* ifindex = 0 means to remove default if */
105 			ifindex = 0;
106 		} else
107 			return;
108 	} else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) {
109 		warn("if_nametoindex(%s)", ndifreq.ifname);
110 		return;
111 	}
112 
113 	ndifreq.ifindex = ifindex;
114 	error = ioctl_ctx(ctx, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
115 	if (error)
116 		warn("ioctl(SIOCSDEFIFACE_IN6)");
117 }
118 
119 static int
120 isnd6defif(if_ctx *ctx, int s)
121 {
122 	struct in6_ndifreq ndifreq = {};
123 	unsigned int ifindex;
124 	int error;
125 
126 	strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname));
127 
128 	ifindex = if_nametoindex(ndifreq.ifname);
129 	error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq);
130 	if (error) {
131 		warn("ioctl(SIOCGDEFIFACE_IN6)");
132 		return (error);
133 	}
134 	return (ndifreq.ifindex == ifindex);
135 }
136 
137 void
138 nd6_status(if_ctx *ctx)
139 {
140 	struct in6_ndireq nd = {};
141 	int s6;
142 	int error;
143 	int isdefif;
144 
145 	strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
146 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
147 		if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
148 			warn("socket(AF_INET6, SOCK_DGRAM)");
149 		return;
150 	}
151 	error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
152 	if (error) {
153 		if (errno != EPFNOSUPPORT)
154 			warn("ioctl(SIOCGIFINFO_IN6)");
155 		close(s6);
156 		return;
157 	}
158 	isdefif = isnd6defif(ctx, s6);
159 	close(s6);
160 	if (nd.ndi.flags == 0 && !isdefif)
161 		return;
162 	printb("\tnd6 options",
163 	    (unsigned int)(nd.ndi.flags | (isdefif << 15)), ND6BITS);
164 	putchar('\n');
165 }
166