xref: /freebsd/sbin/ifconfig/af_nd6.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
1 /*
2  * Copyright (c) 2009 Hiroki Sato.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef lint
27 static const char rcsid[] =
28   "$FreeBSD$";
29 #endif /* not lint */
30 
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
35 #include <net/if.h>
36 #include <net/route.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <ifaddrs.h>
45 
46 #include <arpa/inet.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <arpa/inet.h>
51 #include <netdb.h>
52 
53 #include <netinet6/nd6.h>
54 
55 #include "ifconfig.h"
56 
57 #define	MAX_SYSCTL_TRY	5
58 #define	ND6BITS	"\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \
59 		"\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \
60 		"\007NO_RADR\010NO_PREFER_IFACE\011IGNORELOOP\012NO_DAD" \
61 		"\020DEFAULTIF"
62 
63 static int isnd6defif(int);
64 void setnd6flags(const char *, int, int, const struct afswtch *);
65 void setnd6defif(const char *, int, int, const struct afswtch *);
66 void nd6_status(int);
67 
68 void
69 setnd6flags(const char *dummyaddr __unused,
70 	int d, int s,
71 	const struct afswtch *afp)
72 {
73 	struct in6_ndireq nd;
74 	int error;
75 
76 	memset(&nd, 0, sizeof(nd));
77 	strncpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
78 	error = ioctl(s, SIOCGIFINFO_IN6, &nd);
79 	if (error) {
80 		warn("ioctl(SIOCGIFINFO_IN6)");
81 		return;
82 	}
83 	if (d < 0)
84 		nd.ndi.flags &= ~(-d);
85 	else
86 		nd.ndi.flags |= d;
87 	error = ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd);
88 	if (error)
89 		warn("ioctl(SIOCSIFINFO_IN6)");
90 }
91 
92 void
93 setnd6defif(const char *dummyaddr __unused,
94 	int d, int s,
95 	const struct afswtch *afp)
96 {
97 	struct in6_ndifreq ndifreq;
98 	int ifindex;
99 	int error;
100 
101 	memset(&ndifreq, 0, sizeof(ndifreq));
102 	strncpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
103 
104 	if (d < 0) {
105 		if (isnd6defif(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(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
117 	if (error)
118 		warn("ioctl(SIOCSDEFIFACE_IN6)");
119 }
120 
121 static int
122 isnd6defif(int s)
123 {
124 	struct in6_ndifreq ndifreq;
125 	unsigned int ifindex;
126 	int error;
127 
128 	memset(&ndifreq, 0, sizeof(ndifreq));
129 	strncpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
130 
131 	ifindex = if_nametoindex(ndifreq.ifname);
132 	error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq);
133 	if (error) {
134 		warn("ioctl(SIOCGDEFIFACE_IN6)");
135 		return (error);
136 	}
137 	return (ndifreq.ifindex == ifindex);
138 }
139 
140 void
141 nd6_status(int s)
142 {
143 	struct in6_ndireq nd;
144 	int s6;
145 	int error;
146 	int isdefif;
147 
148 	memset(&nd, 0, sizeof(nd));
149 	strncpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
150 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
151 		if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
152 			warn("socket(AF_INET6, SOCK_DGRAM)");
153 		return;
154 	}
155 	error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
156 	if (error) {
157 		if (errno != EPFNOSUPPORT)
158 			warn("ioctl(SIOCGIFINFO_IN6)");
159 		close(s6);
160 		return;
161 	}
162 	isdefif = isnd6defif(s6);
163 	close(s6);
164 	if (nd.ndi.flags == 0 && !isdefif)
165 		return;
166 	printb("\tnd6 options",
167 	    (unsigned int)(nd.ndi.flags | (isdefif << 15)), ND6BITS);
168 	putchar('\n');
169 }
170