xref: /freebsd/usr.sbin/rtsold/if.c (revision a35f04fba2ebb8f86d4cbdc710c89a094572b08e)
1 /*	$KAME: if.c,v 1.27 2003/10/05 00:09:36 itojun Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
38 #include <sys/queue.h>
39 
40 #include <net/if.h>
41 #include <net/if_types.h>
42 #include <net/route.h>
43 #include <net/if_dl.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
46 #include <netinet/in.h>
47 #include <netinet/icmp6.h>
48 
49 #include <netinet6/in6_var.h>
50 #include <netinet6/nd6.h>
51 
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <syslog.h>
56 #include <string.h>
57 #include <fcntl.h>
58 #include <errno.h>
59 #include <limits.h>
60 #include <ifaddrs.h>
61 #include "rtsold.h"
62 
63 static int ifsock;
64 
65 static int get_llflag(const char *);
66 static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
67 
68 int
69 ifinit(void)
70 {
71 	ifsock = rssock;
72 
73 	return(0);
74 }
75 
76 int
77 interface_up(char *name)
78 {
79 	struct ifreq ifr;
80 	struct in6_ndireq nd;
81 	int llflag;
82 	int s;
83 
84 	memset(&ifr, 0, sizeof(ifr));
85 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
86 	memset(&nd, 0, sizeof(nd));
87 	strlcpy(nd.ifname, name, sizeof(nd.ifname));
88 
89 	if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
90 		warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
91 		    strerror(errno));
92 		return (-1);
93 	}
94 	if (!(ifr.ifr_flags & IFF_UP)) {
95 		ifr.ifr_flags |= IFF_UP;
96 		if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
97 			warnmsg(LOG_ERR, __func__,
98 			    "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
99 		return (-1);
100 	}
101 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
102 		warnmsg(LOG_WARNING, __func__, "socket(AF_INET6, SOCK_DGRAM): %s",
103 		    strerror(errno));
104 		return (-1);
105 	}
106 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
107 		warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFINFO_IN6): %s",
108 		    strerror(errno));
109 		close(s);
110 		return (-1);
111 	}
112 
113 	warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
114 
115 	if (nd.ndi.flags & ND6_IFF_IFDISABLED) {
116 		if (Fflag) {
117 			nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
118 			if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
119 				warnmsg(LOG_WARNING, __func__,
120 				    "ioctl(SIOCSIFINFO_IN6): %s",
121 		    		    strerror(errno));
122 				close(s);
123 				return (-1);
124 			}
125 		} else {
126 			warnmsg(LOG_WARNING, __func__,
127 			    "%s is disabled.", name);
128 			close(s);
129 			return (-1);
130 		}
131 	}
132 	if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) {
133 		if (Fflag) {
134 			nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
135 			if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
136 				warnmsg(LOG_WARNING, __func__,
137 				    "ioctl(SIOCSIFINFO_IN6): %s",
138 		    		    strerror(errno));
139 				close(s);
140 				return (-1);
141 			}
142 		} else {
143 			warnmsg(LOG_WARNING, __func__,
144 			    "%s does not accept Router Advertisement.", name);
145 			close(s);
146 			return (-1);
147 		}
148 	}
149 	close(s);
150 
151 	llflag = get_llflag(name);
152 	if (llflag < 0) {
153 		warnmsg(LOG_WARNING, __func__,
154 		    "get_llflag() failed, anyway I'll try");
155 		return (0);
156 	}
157 
158 	if (!(llflag & IN6_IFF_NOTREADY)) {
159 		warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
160 		return (0);
161 	} else {
162 		if (llflag & IN6_IFF_TENTATIVE) {
163 			warnmsg(LOG_DEBUG, __func__, "%s is tentative",
164 			    name);
165 			return (IFS_TENTATIVE);
166 		}
167 		if (llflag & IN6_IFF_DUPLICATED)
168 			warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
169 			    name);
170 		return (-1);
171 	}
172 }
173 
174 int
175 interface_status(struct ifinfo *ifinfo)
176 {
177 	char *ifname = ifinfo->ifname;
178 	struct ifreq ifr;
179 	struct ifmediareq ifmr;
180 
181 	/* get interface flags */
182 	memset(&ifr, 0, sizeof(ifr));
183 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
184 	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
185 		warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
186 		    ifname, strerror(errno));
187 		return (-1);
188 	}
189 	/*
190 	 * if one of UP and RUNNING flags is dropped,
191 	 * the interface is not active.
192 	 */
193 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
194 		goto inactive;
195 	/* Next, check carrier on the interface, if possible */
196 	if (!ifinfo->mediareqok)
197 		goto active;
198 	memset(&ifmr, 0, sizeof(ifmr));
199 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
200 
201 	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
202 		if (errno != EINVAL) {
203 			warnmsg(LOG_DEBUG, __func__,
204 			    "ioctl(SIOCGIFMEDIA) on %s: %s",
205 			    ifname, strerror(errno));
206 			return(-1);
207 		}
208 		/*
209 		 * EINVAL simply means that the interface does not support
210 		 * the SIOCGIFMEDIA ioctl. We regard it alive.
211 		 */
212 		ifinfo->mediareqok = 0;
213 		goto active;
214 	}
215 
216 	if (ifmr.ifm_status & IFM_AVALID) {
217 		switch (ifmr.ifm_active & IFM_NMASK) {
218 		case IFM_ETHER:
219 		case IFM_IEEE80211:
220 			if (ifmr.ifm_status & IFM_ACTIVE)
221 				goto active;
222 			else
223 				goto inactive;
224 			break;
225 		default:
226 			goto inactive;
227 		}
228 	}
229 
230   inactive:
231 	return (0);
232 
233   active:
234 	return (1);
235 }
236 
237 #define ROUNDUP(a, size) \
238 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
239 
240 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
241 	((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
242 	sizeof(u_long)) : sizeof(u_long)))
243 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
244 
245 int
246 lladdropt_length(struct sockaddr_dl *sdl)
247 {
248 	switch (sdl->sdl_type) {
249 	case IFT_ETHER:
250 		return (ROUNDUP8(ETHER_ADDR_LEN + 2));
251 	default:
252 		return (0);
253 	}
254 }
255 
256 void
257 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
258 {
259 	char *addr;
260 
261 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
262 
263 	switch (sdl->sdl_type) {
264 	case IFT_ETHER:
265 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
266 		addr = (char *)(ndopt + 1);
267 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
268 		break;
269 	default:
270 		warnmsg(LOG_ERR, __func__,
271 		    "unsupported link type(%d)", sdl->sdl_type);
272 		exit(1);
273 	}
274 
275 	return;
276 }
277 
278 struct sockaddr_dl *
279 if_nametosdl(char *name)
280 {
281 	int mib[] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
282 	char *buf, *next, *lim;
283 	size_t len;
284 	struct if_msghdr *ifm;
285 	struct sockaddr *sa, *rti_info[RTAX_MAX];
286 	struct sockaddr_dl *sdl = NULL, *ret_sdl;
287 
288 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
289 		return(NULL);
290 	if ((buf = malloc(len)) == NULL)
291 		return(NULL);
292 	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0) {
293 		free(buf);
294 		return (NULL);
295 	}
296 
297 	lim = buf + len;
298 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
299 		ifm = (struct if_msghdr *)(void *)next;
300 		if (ifm->ifm_type == RTM_IFINFO) {
301 			sa = (struct sockaddr *)(ifm + 1);
302 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
303 			if ((sa = rti_info[RTAX_IFP]) != NULL) {
304 				if (sa->sa_family == AF_LINK) {
305 					sdl = (struct sockaddr_dl *)(void *)sa;
306 					if (strlen(name) != sdl->sdl_nlen)
307 						continue; /* not same len */
308 					if (strncmp(&sdl->sdl_data[0],
309 						    name,
310 						    sdl->sdl_nlen) == 0) {
311 						break;
312 					}
313 				}
314 			}
315 		}
316 	}
317 	if (next == lim) {
318 		/* search failed */
319 		free(buf);
320 		return (NULL);
321 	}
322 
323 	if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) {
324 		free(buf);
325 		return (NULL);
326 	}
327 	memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
328 
329 	free(buf);
330 	return (ret_sdl);
331 }
332 
333 int
334 getinet6sysctl(int code)
335 {
336 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
337 	int value;
338 	size_t size;
339 
340 	mib[3] = code;
341 	size = sizeof(value);
342 	if (sysctl(mib, nitems(mib), &value, &size, NULL, 0) < 0)
343 		return (-1);
344 	else
345 		return (value);
346 }
347 
348 int
349 setinet6sysctl(int code, int newval)
350 {
351 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
352 	int value;
353 	size_t size;
354 
355 	mib[3] = code;
356 	size = sizeof(value);
357 	if (sysctl(mib, nitems(mib), &value, &size,
358 	    &newval, sizeof(newval)) < 0)
359 		return (-1);
360 	else
361 		return (value);
362 }
363 
364 /*------------------------------------------------------------*/
365 
366 /* get ia6_flags for link-local addr on if.  returns -1 on error. */
367 static int
368 get_llflag(const char *name)
369 {
370 	struct ifaddrs *ifap, *ifa;
371 	struct in6_ifreq ifr6;
372 	struct sockaddr_in6 *sin6;
373 	int s;
374 
375 	if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
376 		warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s",
377 		    strerror(errno));
378 		exit(1);
379 	}
380 	if (getifaddrs(&ifap) != 0) {
381 		warnmsg(LOG_ERR, __func__, "getifaddrs: %s",
382 		    strerror(errno));
383 		exit(1);
384 	}
385 
386 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
387 		if (strlen(ifa->ifa_name) != strlen(name) ||
388 		    strncmp(ifa->ifa_name, name, strlen(name)) != 0)
389 			continue;
390 		if (ifa->ifa_addr->sa_family != AF_INET6)
391 			continue;
392 		sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr;
393 		if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
394 			continue;
395 
396 		memset(&ifr6, 0, sizeof(ifr6));
397 		strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
398 		memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
399 		if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
400 			warnmsg(LOG_ERR, __func__,
401 			    "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
402 			exit(1);
403 		}
404 
405 		freeifaddrs(ifap);
406 		close(s);
407 		return (ifr6.ifr_ifru.ifru_flags6);
408 	}
409 
410 	freeifaddrs(ifap);
411 	close(s);
412 	return (-1);
413 }
414 
415 
416 static void
417 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
418 {
419 	int i;
420 
421 	for (i = 0; i < RTAX_MAX; i++) {
422 		if (addrs & (1 << i)) {
423 			rti_info[i] = sa;
424 			NEXT_SA(sa);
425 		} else
426 			rti_info[i] = NULL;
427 	}
428 }
429