No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / rtsold / if.c
blob39e414f2913bfc7fbefda9ef258437f0fc9c1ba6
1 /* $NetBSD: if.c,v 1.14 2006/03/18 21:35:35 dan Exp $ */
2 /* $KAME: if.c,v 1.18 2002/05/31 10:10:03 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 #include <sys/ioctl.h>
37 #include <sys/queue.h>
39 #include <net/if.h>
40 #include <net/if_types.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_ether.h>
45 #include <netinet/in.h>
46 #include <netinet/icmp6.h>
48 #include <netinet6/in6_var.h>
50 #include <stdio.h>
51 #include <unistd.h>
52 #include <stdlib.h>
53 #include <syslog.h>
54 #include <string.h>
55 #include <fcntl.h>
56 #include <errno.h>
57 #include <limits.h>
58 #include <ifaddrs.h>
60 #include "rtsold.h"
62 extern int rssock;
63 static int ifsock;
65 static int get_llflag __P((const char *));
66 static void get_rtaddrs __P((int, struct sockaddr *, struct sockaddr **));
68 int
69 ifinit(void)
71 ifsock = rssock;
73 return(0);
76 int
77 interface_up(char *name)
79 struct ifreq ifr;
80 int llflag;
82 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
84 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
85 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
86 strerror(errno));
87 return(-1);
89 if (!(ifr.ifr_flags & IFF_UP)) {
90 ifr.ifr_flags |= IFF_UP;
91 if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
92 warnmsg(LOG_ERR, __func__,
93 "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
94 return(-1);
97 warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
99 llflag = get_llflag(name);
100 if (llflag < 0) {
101 warnmsg(LOG_WARNING, __func__,
102 "get_llflag() failed, anyway I'll try");
103 return 0;
106 if (!(llflag & IN6_IFF_NOTREADY)) {
107 warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
108 return(0);
109 } else {
110 if (llflag & IN6_IFF_TENTATIVE) {
111 warnmsg(LOG_DEBUG, __func__, "%s is tentative",
112 name);
113 return IFS_TENTATIVE;
115 if (llflag & IN6_IFF_DUPLICATED)
116 warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
117 name);
118 return -1;
123 interface_status(struct ifinfo *ifinfo)
125 char *ifname = ifinfo->ifname;
126 struct ifreq ifr;
127 struct ifmediareq ifmr;
129 /* get interface flags */
130 memset(&ifr, 0, sizeof(ifr));
131 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
132 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
133 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
134 ifname, strerror(errno));
135 return(-1);
138 * if one of UP and RUNNING flags is dropped,
139 * the interface is not active.
141 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
142 goto inactive;
145 /* Next, check carrier on the interface, if possible */
146 if (!ifinfo->mediareqok)
147 goto active;
148 memset(&ifmr, 0, sizeof(ifmr));
149 strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
151 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
152 if (errno != EINVAL) {
153 warnmsg(LOG_DEBUG, __func__,
154 "ioctl(SIOCGIFMEDIA) on %s: %s",
155 ifname, strerror(errno));
156 return(-1);
159 * EINVAL simply means that the interface does not support
160 * the SIOCGIFMEDIA ioctl. We regard it alive.
162 ifinfo->mediareqok = 0;
163 goto active;
166 if (ifmr.ifm_status & IFM_AVALID) {
167 switch (ifmr.ifm_active & IFM_NMASK) {
168 case IFM_ETHER:
169 if (ifmr.ifm_status & IFM_ACTIVE)
170 goto active;
171 else
172 goto inactive;
173 break;
174 default:
175 goto inactive;
179 inactive:
180 return(0);
182 active:
183 return(1);
186 #define ROUNDUP(a, size) \
187 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
189 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
190 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
191 sizeof(u_long)) : sizeof(u_long)))
192 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
195 lladdropt_length(struct sockaddr_dl *sdl)
197 switch (sdl->sdl_type) {
198 case IFT_ETHER:
199 #ifdef IFT_IEEE80211
200 case IFT_IEEE80211:
201 #endif
202 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
203 default:
204 return(0);
208 void
209 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
211 char *addr;
213 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
215 switch (sdl->sdl_type) {
216 case IFT_ETHER:
217 #ifdef IFT_IEEE80211
218 case IFT_IEEE80211:
219 #endif
220 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
221 addr = (char *)(ndopt + 1);
222 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
223 break;
224 default:
225 warnmsg(LOG_ERR, __func__,
226 "unsupported link type(%d)", sdl->sdl_type);
227 exit(1);
230 return;
233 struct sockaddr_dl *
234 if_nametosdl(char *name)
236 int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
237 char *buf, *next, *lim;
238 size_t len;
239 struct if_msghdr *ifm;
240 struct sockaddr *sa, *rti_info[RTAX_MAX];
241 struct sockaddr_dl *sdl = NULL, *ret_sdl;
243 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
244 return(NULL);
245 if ((buf = malloc(len)) == NULL)
246 return(NULL);
247 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
248 free(buf);
249 return(NULL);
252 lim = buf + len;
253 for (next = buf; next < lim; next += ifm->ifm_msglen) {
254 ifm = (struct if_msghdr *)next;
255 if (ifm->ifm_type == RTM_IFINFO) {
256 sa = (struct sockaddr *)(ifm + 1);
257 get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
258 if ((sa = rti_info[RTAX_IFP]) != NULL) {
259 if (sa->sa_family == AF_LINK) {
260 sdl = (struct sockaddr_dl *)sa;
261 if (strlen(name) != sdl->sdl_nlen)
262 continue; /* not same len */
263 if (strncmp(&sdl->sdl_data[0],
264 name,
265 sdl->sdl_nlen) == 0) {
266 break;
272 if (next == lim || sdl == NULL) {
273 /* search failed */
274 free(buf);
275 return(NULL);
278 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) {
279 free(buf);
280 return(NULL);
282 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
284 free(buf);
285 return(ret_sdl);
289 getinet6sysctl(int code)
291 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
292 int value;
293 size_t size;
295 mib[3] = code;
296 size = sizeof(value);
297 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0) < 0)
298 return -1;
299 else
300 return value;
303 /*------------------------------------------------------------*/
305 /* get ia6_flags for link-local addr on if. returns -1 on error. */
306 static int
307 get_llflag(const char *name)
309 struct ifaddrs *ifap, *ifa;
310 struct in6_ifreq ifr6;
311 struct sockaddr_in6 *sin6;
312 int s;
314 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
315 warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s",
316 strerror(errno));
317 exit(1);
319 if (getifaddrs(&ifap) != 0) {
320 warnmsg(LOG_ERR, __func__, "getifaddrs: %s",
321 strerror(errno));
322 exit(1);
325 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
326 if (strlen(ifa->ifa_name) != strlen(name) ||
327 strncmp(ifa->ifa_name, name, strlen(name)) != 0)
328 continue;
329 if (ifa->ifa_addr->sa_family != AF_INET6)
330 continue;
331 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
332 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
333 continue;
335 memset(&ifr6, 0, sizeof(ifr6));
336 strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
337 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
338 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
339 warnmsg(LOG_ERR, __func__,
340 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
341 exit(1);
344 freeifaddrs(ifap);
345 close(s);
346 return ifr6.ifr_ifru.ifru_flags6;
349 freeifaddrs(ifap);
350 close(s);
351 return -1;
355 static void
356 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
358 int i;
360 for (i = 0; i < RTAX_MAX; i++) {
361 if (addrs & (1 << i)) {
362 rti_info[i] = sa;
363 NEXT_SA(sa);
364 } else
365 rti_info[i] = NULL;