2003-05-15 18:44:57 +04:00
|
|
|
/* $NetBSD: if.c,v 1.12 2003/05/15 14:44:57 itojun Exp $ */
|
2002-05-31 14:22:11 +04:00
|
|
|
/* $KAME: if.c,v 1.18 2002/05/31 10:10:03 itojun Exp $ */
|
1999-09-03 09:14:37 +04:00
|
|
|
|
1999-09-03 08:56:06 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/ioctl.h>
|
2000-08-13 10:20:02 +04:00
|
|
|
#include <sys/queue.h>
|
1999-09-03 08:56:06 +04:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_media.h>
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/icmp6.h>
|
|
|
|
|
|
|
|
#include <netinet6/in6_var.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2000-02-25 12:19:06 +03:00
|
|
|
#include <ifaddrs.h>
|
1999-09-03 08:56:06 +04:00
|
|
|
|
|
|
|
#include "rtsold.h"
|
|
|
|
|
2000-02-25 12:19:06 +03:00
|
|
|
extern int rssock;
|
1999-09-03 08:56:06 +04:00
|
|
|
static int ifsock;
|
|
|
|
|
2002-05-31 14:22:11 +04:00
|
|
|
static int get_llflag __P((const char *));
|
|
|
|
static void get_rtaddrs __P((int, struct sockaddr *, struct sockaddr **));
|
1999-09-03 08:56:06 +04:00
|
|
|
|
|
|
|
int
|
2002-07-14 04:37:46 +04:00
|
|
|
ifinit(void)
|
1999-09-03 08:56:06 +04:00
|
|
|
{
|
2000-02-25 12:19:06 +03:00
|
|
|
ifsock = rssock;
|
1999-09-03 08:56:06 +04:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
interface_up(char *name)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
1999-12-31 22:40:19 +03:00
|
|
|
int llflag;
|
1999-09-03 08:56:06 +04:00
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
|
|
|
|
|
|
|
if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
|
|
|
|
warnmsg(LOG_WARNING, __FUNCTION__, "ioctl(SIOCGIFFLAGS): %s",
|
2002-05-31 14:22:11 +04:00
|
|
|
strerror(errno));
|
1999-09-03 08:56:06 +04:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (!(ifr.ifr_flags & IFF_UP)) {
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
2002-05-31 14:22:11 +04:00
|
|
|
if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
|
1999-09-03 08:56:06 +04:00
|
|
|
warnmsg(LOG_ERR, __FUNCTION__,
|
2002-05-31 14:22:11 +04:00
|
|
|
"ioctl(SIOCSIFFLAGS): %s", strerror(errno));
|
1999-09-03 08:56:06 +04:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
warnmsg(LOG_DEBUG, __FUNCTION__, "checking if %s is ready...", name);
|
|
|
|
|
1999-12-31 22:40:19 +03:00
|
|
|
llflag = get_llflag(name);
|
|
|
|
if (llflag < 0) {
|
1999-09-03 08:56:06 +04:00
|
|
|
warnmsg(LOG_WARNING, __FUNCTION__,
|
2002-05-31 14:22:11 +04:00
|
|
|
"get_llflag() failed, anyway I'll try");
|
1999-09-03 08:56:06 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-12-31 22:40:19 +03:00
|
|
|
if (!(llflag & IN6_IFF_NOTREADY)) {
|
2002-05-31 14:22:11 +04:00
|
|
|
warnmsg(LOG_DEBUG, __FUNCTION__, "%s is ready", name);
|
1999-09-03 08:56:06 +04:00
|
|
|
return(0);
|
1999-12-31 22:40:19 +03:00
|
|
|
} else {
|
|
|
|
if (llflag & IN6_IFF_TENTATIVE) {
|
1999-09-03 08:56:06 +04:00
|
|
|
warnmsg(LOG_DEBUG, __FUNCTION__, "%s is tentative",
|
2002-05-31 14:22:11 +04:00
|
|
|
name);
|
1999-09-03 08:56:06 +04:00
|
|
|
return IFS_TENTATIVE;
|
|
|
|
}
|
1999-12-31 22:40:19 +03:00
|
|
|
if (llflag & IN6_IFF_DUPLICATED)
|
1999-09-03 08:56:06 +04:00
|
|
|
warnmsg(LOG_DEBUG, __FUNCTION__, "%s is duplicated",
|
2002-05-31 14:22:11 +04:00
|
|
|
name);
|
1999-09-03 08:56:06 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
interface_status(struct ifinfo *ifinfo)
|
|
|
|
{
|
|
|
|
char *ifname = ifinfo->ifname;
|
|
|
|
struct ifreq ifr;
|
|
|
|
struct ifmediareq ifmr;
|
2002-05-31 14:22:11 +04:00
|
|
|
|
1999-09-03 08:56:06 +04:00
|
|
|
/* get interface flags */
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
|
|
|
|
if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
|
|
|
|
warnmsg(LOG_ERR, __FUNCTION__, "ioctl(SIOCGIFFLAGS) on %s: %s",
|
2002-05-31 14:22:11 +04:00
|
|
|
ifname, strerror(errno));
|
1999-09-03 08:56:06 +04:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if one of UP and RUNNING flags is dropped,
|
|
|
|
* the interface is not active.
|
|
|
|
*/
|
|
|
|
if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
|
|
|
|
goto inactive;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, check carrier on the interface, if possible */
|
|
|
|
if (!ifinfo->mediareqok)
|
|
|
|
goto active;
|
|
|
|
memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
|
|
|
|
|
|
|
|
if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
|
|
|
|
if (errno != EINVAL) {
|
|
|
|
warnmsg(LOG_DEBUG, __FUNCTION__,
|
2002-05-31 14:22:11 +04:00
|
|
|
"ioctl(SIOCGIFMEDIA) on %s: %s",
|
|
|
|
ifname, strerror(errno));
|
1999-09-03 08:56:06 +04:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* EINVAL simply means that the interface does not support
|
|
|
|
* the SIOCGIFMEDIA ioctl. We regard it alive.
|
|
|
|
*/
|
|
|
|
ifinfo->mediareqok = 0;
|
|
|
|
goto active;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifmr.ifm_status & IFM_AVALID) {
|
2002-05-31 14:22:11 +04:00
|
|
|
switch (ifmr.ifm_active & IFM_NMASK) {
|
|
|
|
case IFM_ETHER:
|
|
|
|
if (ifmr.ifm_status & IFM_ACTIVE)
|
|
|
|
goto active;
|
|
|
|
else
|
|
|
|
goto inactive;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto inactive;
|
1999-09-03 08:56:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inactive:
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
active:
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ROUNDUP(a, size) \
|
|
|
|
(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
|
|
|
|
|
|
|
|
#define NEXT_SA(ap) (ap) = (struct sockaddr *) \
|
|
|
|
((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
|
2002-05-31 14:22:11 +04:00
|
|
|
sizeof(u_long)) : sizeof(u_long)))
|
1999-09-03 08:56:06 +04:00
|
|
|
#define ROUNDUP8(a) (1 + (((a) - 1) | 7))
|
|
|
|
|
|
|
|
int
|
|
|
|
lladdropt_length(struct sockaddr_dl *sdl)
|
|
|
|
{
|
2002-05-31 14:22:11 +04:00
|
|
|
switch (sdl->sdl_type) {
|
|
|
|
case IFT_ETHER:
|
2001-07-09 10:04:28 +04:00
|
|
|
#ifdef IFT_IEEE80211
|
|
|
|
case IFT_IEEE80211:
|
|
|
|
#endif
|
2002-05-31 14:22:11 +04:00
|
|
|
return(ROUNDUP8(ETHER_ADDR_LEN + 2));
|
|
|
|
default:
|
|
|
|
return(0);
|
1999-09-03 08:56:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
|
|
|
|
{
|
|
|
|
char *addr;
|
|
|
|
|
|
|
|
ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
|
|
|
|
|
2002-05-31 14:22:11 +04:00
|
|
|
switch (sdl->sdl_type) {
|
|
|
|
case IFT_ETHER:
|
2001-07-09 10:04:28 +04:00
|
|
|
#ifdef IFT_IEEE80211
|
|
|
|
case IFT_IEEE80211:
|
|
|
|
#endif
|
2002-05-31 14:22:11 +04:00
|
|
|
ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
|
|
|
|
addr = (char *)(ndopt + 1);
|
|
|
|
memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warnmsg(LOG_ERR, __FUNCTION__,
|
|
|
|
"unsupported link type(%d)", sdl->sdl_type);
|
|
|
|
exit(1);
|
1999-09-03 08:56:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_dl *
|
|
|
|
if_nametosdl(char *name)
|
|
|
|
{
|
|
|
|
int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
|
|
|
|
char *buf, *next, *lim;
|
|
|
|
size_t len;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct sockaddr *sa, *rti_info[RTAX_MAX];
|
|
|
|
struct sockaddr_dl *sdl = NULL, *ret_sdl;
|
|
|
|
|
|
|
|
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
|
|
|
|
return(NULL);
|
|
|
|
if ((buf = malloc(len)) == NULL)
|
|
|
|
return(NULL);
|
|
|
|
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
|
|
|
|
free(buf);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
lim = buf + len;
|
|
|
|
for (next = buf; next < lim; next += ifm->ifm_msglen) {
|
|
|
|
ifm = (struct if_msghdr *)next;
|
|
|
|
if (ifm->ifm_type == RTM_IFINFO) {
|
|
|
|
sa = (struct sockaddr *)(ifm + 1);
|
|
|
|
get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
|
|
|
|
if ((sa = rti_info[RTAX_IFP]) != NULL) {
|
|
|
|
if (sa->sa_family == AF_LINK) {
|
|
|
|
sdl = (struct sockaddr_dl *)sa;
|
1999-12-09 18:08:33 +03:00
|
|
|
if (strlen(name) != sdl->sdl_nlen)
|
|
|
|
continue; /* not same len */
|
1999-09-03 08:56:06 +04:00
|
|
|
if (strncmp(&sdl->sdl_data[0],
|
|
|
|
name,
|
|
|
|
sdl->sdl_nlen) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (next == lim) {
|
|
|
|
/* search failed */
|
|
|
|
free(buf);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
|
|
|
|
return(NULL);
|
|
|
|
memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
|
|
|
|
|
2001-07-09 10:04:28 +04:00
|
|
|
free(buf);
|
1999-09-03 08:56:06 +04:00
|
|
|
return(ret_sdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
getinet6sysctl(int code)
|
|
|
|
{
|
|
|
|
int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
|
|
|
|
int value;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
mib[3] = code;
|
|
|
|
size = sizeof(value);
|
|
|
|
if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0) < 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
|
|
|
1999-12-31 22:40:19 +03:00
|
|
|
/* get ia6_flags for link-local addr on if. returns -1 on error. */
|
1999-09-03 08:56:06 +04:00
|
|
|
static int
|
1999-12-31 22:40:19 +03:00
|
|
|
get_llflag(const char *name)
|
1999-09-03 08:56:06 +04:00
|
|
|
{
|
2000-02-25 12:19:06 +03:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct in6_ifreq ifr6;
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (getifaddrs(&ifap) != 0) {
|
2003-04-03 03:29:29 +04:00
|
|
|
warnmsg(LOG_ERR, __FUNCTION__, "getifaddrs: %s",
|
2000-02-25 12:19:06 +03:00
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
2002-05-31 14:22:11 +04:00
|
|
|
if (strlen(ifa->ifa_name) != strlen(name) ||
|
|
|
|
strncmp(ifa->ifa_name, name, strlen(name)) != 0)
|
2000-02-25 12:19:06 +03:00
|
|
|
continue;
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
|
|
|
|
if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memset(&ifr6, 0, sizeof(ifr6));
|
2001-11-14 04:55:38 +03:00
|
|
|
strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
|
2000-02-25 12:19:06 +03:00
|
|
|
memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
|
|
|
|
if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
|
|
|
|
warnmsg(LOG_ERR, __FUNCTION__,
|
|
|
|
"ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
close(s);
|
|
|
|
return ifr6.ifr_ifru.ifru_flags6;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
close(s);
|
|
|
|
return -1;
|
1999-09-03 08:56:06 +04:00
|
|
|
}
|
|
|
|
|
1999-12-31 22:40:19 +03:00
|
|
|
|
1999-09-03 08:56:06 +04:00
|
|
|
static void
|
|
|
|
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
|
|
|
|
{
|
|
|
|
int i;
|
2002-05-31 14:22:11 +04:00
|
|
|
|
1999-09-03 08:56:06 +04:00
|
|
|
for (i = 0; i < RTAX_MAX; i++) {
|
|
|
|
if (addrs & (1 << i)) {
|
|
|
|
rti_info[i] = sa;
|
|
|
|
NEXT_SA(sa);
|
2002-05-31 14:22:11 +04:00
|
|
|
} else
|
1999-09-03 08:56:06 +04:00
|
|
|
rti_info[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|