1 /* $KAME: if.c,v 1.27 2003/10/05 00:09:36 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 #include <sys/ioctl.h>
40 #include <sys/queue.h>
43 #include <net/if_types.h>
44 #include <net/route.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/ethernet.h>
48 #include <netinet/in.h>
49 #include <netinet/icmp6.h>
51 #include <netinet6/in6_var.h>
52 #include <netinet6/nd6.h>
67 static int get_llflag(const char *);
68 static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
79 interface_up(char *name)
86 memset(&ifr, 0, sizeof(ifr));
87 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
88 memset(&nd, 0, sizeof(nd));
89 strlcpy(nd.ifname, name, sizeof(nd.ifname));
91 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
92 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
96 if (!(ifr.ifr_flags & IFF_UP)) {
97 ifr.ifr_flags |= IFF_UP;
98 if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
99 warnmsg(LOG_ERR, __func__,
100 "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
103 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
104 warnmsg(LOG_WARNING, __func__, "socket(AF_INET6, SOCK_DGRAM): %s",
108 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
109 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFINFO_IN6): %s",
115 warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
117 if (nd.ndi.flags & ND6_IFF_IFDISABLED) {
119 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
120 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
121 warnmsg(LOG_WARNING, __func__,
122 "ioctl(SIOCSIFINFO_IN6): %s",
128 warnmsg(LOG_WARNING, __func__,
129 "%s is disabled.", name);
134 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) {
136 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
137 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
138 warnmsg(LOG_WARNING, __func__,
139 "ioctl(SIOCSIFINFO_IN6): %s",
145 warnmsg(LOG_WARNING, __func__,
146 "%s does not accept Router Advertisement.", name);
153 llflag = get_llflag(name);
155 warnmsg(LOG_WARNING, __func__,
156 "get_llflag() failed, anyway I'll try");
160 if (!(llflag & IN6_IFF_NOTREADY)) {
161 warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
164 if (llflag & IN6_IFF_TENTATIVE) {
165 warnmsg(LOG_DEBUG, __func__, "%s is tentative",
167 return (IFS_TENTATIVE);
169 if (llflag & IN6_IFF_DUPLICATED)
170 warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
177 interface_status(struct ifinfo *ifinfo)
179 char *ifname = ifinfo->ifname;
181 struct ifmediareq ifmr;
183 /* get interface flags */
184 memset(&ifr, 0, sizeof(ifr));
185 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
186 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
187 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
188 ifname, strerror(errno));
192 * if one of UP and RUNNING flags is dropped,
193 * the interface is not active.
195 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
197 /* Next, check carrier on the interface, if possible */
198 if (!ifinfo->mediareqok)
200 memset(&ifmr, 0, sizeof(ifmr));
201 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
203 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
204 if (errno != EINVAL) {
205 warnmsg(LOG_DEBUG, __func__,
206 "ioctl(SIOCGIFMEDIA) on %s: %s",
207 ifname, strerror(errno));
211 * EINVAL simply means that the interface does not support
212 * the SIOCGIFMEDIA ioctl. We regard it alive.
214 ifinfo->mediareqok = 0;
218 if (ifmr.ifm_status & IFM_AVALID) {
219 switch (ifmr.ifm_active & IFM_NMASK) {
222 if (ifmr.ifm_status & IFM_ACTIVE)
239 #define ROUNDUP(a, size) \
240 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
242 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
243 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
244 sizeof(u_long)) : sizeof(u_long)))
245 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
248 lladdropt_length(struct sockaddr_dl *sdl)
250 switch (sdl->sdl_type) {
252 return (ROUNDUP8(ETHER_ADDR_LEN + 2));
259 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
263 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
265 switch (sdl->sdl_type) {
267 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
268 addr = (char *)(ndopt + 1);
269 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
272 warnmsg(LOG_ERR, __func__,
273 "unsupported link type(%d)", sdl->sdl_type);
281 if_nametosdl(char *name)
283 int mib[] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
284 char *buf, *next, *lim;
286 struct if_msghdr *ifm;
287 struct sockaddr *sa, *rti_info[RTAX_MAX];
288 struct sockaddr_dl *sdl = NULL, *ret_sdl;
290 if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
292 if ((buf = malloc(len)) == NULL)
294 if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0) {
300 for (next = buf; next < lim; next += ifm->ifm_msglen) {
301 ifm = (struct if_msghdr *)(void *)next;
302 if (ifm->ifm_type == RTM_IFINFO) {
303 sa = (struct sockaddr *)(ifm + 1);
304 get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
305 if ((sa = rti_info[RTAX_IFP]) != NULL) {
306 if (sa->sa_family == AF_LINK) {
307 sdl = (struct sockaddr_dl *)(void *)sa;
308 if (strlen(name) != sdl->sdl_nlen)
309 continue; /* not same len */
310 if (strncmp(&sdl->sdl_data[0],
312 sdl->sdl_nlen) == 0) {
325 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) {
329 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
336 getinet6sysctl(int code)
338 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
343 size = sizeof(value);
344 if (sysctl(mib, nitems(mib), &value, &size, NULL, 0) < 0)
351 setinet6sysctl(int code, int newval)
353 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
358 size = sizeof(value);
359 if (sysctl(mib, nitems(mib), &value, &size,
360 &newval, sizeof(newval)) < 0)
366 /*------------------------------------------------------------*/
368 /* get ia6_flags for link-local addr on if. returns -1 on error. */
370 get_llflag(const char *name)
372 struct ifaddrs *ifap, *ifa;
373 struct in6_ifreq ifr6;
374 struct sockaddr_in6 *sin6;
377 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
378 warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s",
382 if (getifaddrs(&ifap) != 0) {
383 warnmsg(LOG_ERR, __func__, "getifaddrs: %s",
388 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
389 if (strlen(ifa->ifa_name) != strlen(name) ||
390 strncmp(ifa->ifa_name, name, strlen(name)) != 0)
392 if (ifa->ifa_addr->sa_family != AF_INET6)
394 sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr;
395 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
398 memset(&ifr6, 0, sizeof(ifr6));
399 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
400 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
401 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
402 warnmsg(LOG_ERR, __func__,
403 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
409 return (ifr6.ifr_ifru.ifru_flags6);
419 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
423 for (i = 0; i < RTAX_MAX; i++) {
424 if (addrs & (1 << i)) {