2001-01-21 15:25:46 +00:00
|
|
|
/* $FreeBSD$ */
|
2001-06-11 12:39:29 +00:00
|
|
|
/* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */
|
2000-07-05 22:09:50 +00:00
|
|
|
|
2000-01-06 12:40:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-07-05 22:09:50 +00:00
|
|
|
*
|
2000-01-06 12:40:54 +00:00
|
|
|
* 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.
|
2000-07-05 22:09:50 +00:00
|
|
|
*
|
2000-01-06 12:40:54 +00:00
|
|
|
* 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>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_types.h>
|
2000-07-05 22:09:50 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
# include <net/ethernet.h>
|
|
|
|
#endif
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#endif
|
2000-01-06 12:40:54 +00:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/icmp6.h>
|
2000-07-05 22:09:50 +00:00
|
|
|
#ifdef __bsdi__
|
|
|
|
# include <netinet/if_ether.h>
|
|
|
|
#endif
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#endif
|
2000-01-06 12:40:54 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include "rtadvd.h"
|
|
|
|
#include "if.h"
|
|
|
|
|
2000-07-05 22:09:50 +00:00
|
|
|
#define ROUNDUP(a, size) \
|
2000-01-06 12:40:54 +00:00
|
|
|
(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
|
|
|
|
|
2000-07-05 22:09:50 +00:00
|
|
|
#define NEXT_SA(ap) (ap) = (struct sockaddr *) \
|
2000-01-06 12:40:54 +00:00
|
|
|
((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
|
|
|
|
sizeof(u_long)) :\
|
|
|
|
sizeof(u_long)))
|
|
|
|
|
2000-07-05 22:09:50 +00:00
|
|
|
struct if_msghdr **iflist;
|
|
|
|
int iflist_init_ok;
|
|
|
|
size_t ifblock_size;
|
|
|
|
char *ifblock;
|
2000-01-06 12:40:54 +00:00
|
|
|
|
2000-07-05 22:09:50 +00:00
|
|
|
static void get_iflist __P((char **buf, size_t *size));
|
|
|
|
static void parse_iflist __P((struct if_msghdr ***ifmlist_p, char *buf,
|
2000-01-06 12:40:54 +00:00
|
|
|
size_t bufsize));
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
|
|
|
|
{
|
|
|
|
int i;
|
2000-07-05 22:09:50 +00:00
|
|
|
|
2000-01-06 12:40:54 +00:00
|
|
|
for (i = 0; i < RTAX_MAX; i++) {
|
|
|
|
if (addrs & (1 << i)) {
|
|
|
|
rti_info[i] = sa;
|
|
|
|
NEXT_SA(sa);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rti_info[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2000-07-05 22:09:50 +00:00
|
|
|
if (strlen(name) != sdl->sdl_nlen)
|
|
|
|
continue; /* not same len */
|
2000-01-06 12:40:54 +00: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);
|
|
|
|
return(ret_sdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
if_getmtu(char *name)
|
|
|
|
{
|
2000-07-05 22:09:50 +00:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct if_data *ifd;
|
2000-10-28 17:57:38 +00:00
|
|
|
u_long mtu = 0;
|
2000-07-05 22:09:50 +00:00
|
|
|
|
|
|
|
if (getifaddrs(&ifap) < 0)
|
|
|
|
return(0);
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if (strcmp(ifa->ifa_name, name) == 0) {
|
|
|
|
ifd = ifa->ifa_data;
|
|
|
|
if (ifd)
|
2000-10-28 17:57:38 +00:00
|
|
|
mtu = ifd->ifi_mtu;
|
|
|
|
break;
|
2000-07-05 22:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
freeifaddrs(ifap);
|
2000-10-28 17:57:38 +00:00
|
|
|
|
|
|
|
#ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
|
|
|
|
if (mtu == 0) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
ifr.ifr_addr.sa_family = AF_INET6;
|
|
|
|
strncpy(ifr.ifr_name, name,
|
|
|
|
sizeof(ifr.ifr_name));
|
|
|
|
if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
|
|
|
|
close(s);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
|
|
|
|
mtu = ifr.ifr_mtu;
|
|
|
|
}
|
2000-07-05 22:09:50 +00:00
|
|
|
#endif
|
2000-10-28 17:57:38 +00:00
|
|
|
|
|
|
|
return(mtu);
|
2000-01-06 12:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* give interface index and its old flags, then new flags returned */
|
|
|
|
int
|
|
|
|
if_getflags(int ifindex, int oifflags)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
|
|
|
|
strerror(errno));
|
|
|
|
return (oifflags & ~IFF_UP);
|
|
|
|
}
|
|
|
|
|
|
|
|
if_indextoname(ifindex, ifr.ifr_name);
|
|
|
|
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
|
|
|
|
syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
|
|
|
|
__FUNCTION__, ifr.ifr_name);
|
|
|
|
close(s);
|
|
|
|
return (oifflags & ~IFF_UP);
|
|
|
|
}
|
2000-10-28 17:57:38 +00:00
|
|
|
close(s);
|
2000-01-06 12:40:54 +00:00
|
|
|
return (ifr.ifr_flags);
|
|
|
|
}
|
|
|
|
|
2000-07-05 22:09:50 +00:00
|
|
|
#define ROUNDUP8(a) (1 + (((a) - 1) | 7))
|
2000-01-06 12:40:54 +00:00
|
|
|
int
|
|
|
|
lladdropt_length(struct sockaddr_dl *sdl)
|
|
|
|
{
|
|
|
|
switch(sdl->sdl_type) {
|
|
|
|
case IFT_ETHER:
|
|
|
|
return(ROUNDUP8(ETHER_ADDR_LEN + 2));
|
|
|
|
default:
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
|
|
|
|
{
|
|
|
|
char *addr;
|
|
|
|
|
|
|
|
ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
|
|
|
|
|
|
|
|
switch(sdl->sdl_type) {
|
|
|
|
case IFT_ETHER:
|
|
|
|
ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
|
|
|
|
addr = (char *)(ndopt + 1);
|
|
|
|
memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"<%s> unsupported link type(%d)",
|
|
|
|
__FUNCTION__, sdl->sdl_type);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rtbuf_len()
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
|
|
|
|
|
|
|
|
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
return(len);
|
|
|
|
}
|
|
|
|
|
2000-07-05 22:09:50 +00:00
|
|
|
#define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
|
|
|
|
#define SIN6(s) ((struct sockaddr_in6 *)(s))
|
|
|
|
#define SDL(s) ((struct sockaddr_dl *)(s))
|
2000-01-06 12:40:54 +00:00
|
|
|
char *
|
|
|
|
get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm;
|
|
|
|
struct ifa_msghdr *ifam;
|
|
|
|
struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
|
|
|
|
|
|
|
|
*lenp = 0;
|
|
|
|
for (rtm = (struct rt_msghdr *)buf;
|
|
|
|
rtm < (struct rt_msghdr *)lim;
|
|
|
|
rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
|
|
|
|
/* just for safety */
|
|
|
|
if (!rtm->rtm_msglen) {
|
|
|
|
syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
|
|
|
|
"(buf=%p lim=%p rtm=%p)", __FUNCTION__,
|
|
|
|
buf, lim, rtm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rtm->rtm_type) {
|
|
|
|
case RTM_GET:
|
|
|
|
case RTM_ADD:
|
|
|
|
case RTM_DELETE:
|
|
|
|
/* address related checks */
|
|
|
|
sa = (struct sockaddr *)(rtm + 1);
|
|
|
|
get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
|
|
|
|
if ((dst = rti_info[RTAX_DST]) == NULL ||
|
|
|
|
dst->sa_family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
|
|
|
|
IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
|
|
|
|
gw->sa_family != AF_LINK)
|
|
|
|
continue;
|
|
|
|
if (ifindex && SDL(gw)->sdl_index != ifindex)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rti_info[RTAX_NETMASK] == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* found */
|
|
|
|
*lenp = rtm->rtm_msglen;
|
|
|
|
return (char *)rtm;
|
|
|
|
/* NOTREACHED */
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_DELADDR:
|
|
|
|
ifam = (struct ifa_msghdr *)rtm;
|
|
|
|
|
|
|
|
/* address related checks */
|
|
|
|
sa = (struct sockaddr *)(ifam + 1);
|
|
|
|
get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
|
|
|
|
if ((ifa = rti_info[RTAX_IFA]) == NULL ||
|
|
|
|
(ifa->sa_family != AF_INET &&
|
|
|
|
ifa->sa_family != AF_INET6))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ifa->sa_family == AF_INET6 &&
|
|
|
|
(IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
|
|
|
|
IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ifindex && ifam->ifam_index != ifindex)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* found */
|
|
|
|
*lenp = ifam->ifam_msglen;
|
|
|
|
return (char *)rtm;
|
|
|
|
/* NOTREACHED */
|
|
|
|
case RTM_IFINFO:
|
|
|
|
/* found */
|
|
|
|
*lenp = rtm->rtm_msglen;
|
|
|
|
return (char *)rtm;
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char *)rtm;
|
|
|
|
}
|
2001-06-11 12:39:29 +00:00
|
|
|
#undef FILTER_MATCH
|
2000-01-06 12:40:54 +00:00
|
|
|
|
|
|
|
struct in6_addr *
|
|
|
|
get_addr(char *buf)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
|
|
|
|
struct sockaddr *sa, *rti_info[RTAX_MAX];
|
|
|
|
|
|
|
|
sa = (struct sockaddr *)(rtm + 1);
|
|
|
|
get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
|
|
|
|
|
|
|
|
return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_rtm_ifindex(char *buf)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
|
|
|
|
struct sockaddr *sa, *rti_info[RTAX_MAX];
|
|
|
|
|
|
|
|
sa = (struct sockaddr *)(rtm + 1);
|
|
|
|
get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
|
|
|
|
|
|
|
|
return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_ifm_ifindex(char *buf)
|
|
|
|
{
|
|
|
|
struct if_msghdr *ifm = (struct if_msghdr *)buf;
|
|
|
|
|
|
|
|
return ((int)ifm->ifm_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_ifam_ifindex(char *buf)
|
|
|
|
{
|
|
|
|
struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
|
|
|
|
|
|
|
|
return ((int)ifam->ifam_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_ifm_flags(char *buf)
|
|
|
|
{
|
|
|
|
struct if_msghdr *ifm = (struct if_msghdr *)buf;
|
|
|
|
|
|
|
|
return (ifm->ifm_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_prefixlen(char *buf)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
|
|
|
|
struct sockaddr *sa, *rti_info[RTAX_MAX];
|
|
|
|
u_char *p, *lim;
|
2000-07-05 22:09:50 +00:00
|
|
|
|
2000-01-06 12:40:54 +00:00
|
|
|
sa = (struct sockaddr *)(rtm + 1);
|
|
|
|
get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
|
|
|
|
sa = rti_info[RTAX_NETMASK];
|
|
|
|
|
|
|
|
p = (u_char *)(&SIN6(sa)->sin6_addr);
|
|
|
|
lim = (u_char *)sa + sa->sa_len;
|
2000-10-28 17:57:38 +00:00
|
|
|
return prefixlen(p, lim);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
prefixlen(u_char *p, u_char *lim)
|
|
|
|
{
|
|
|
|
int masklen;
|
|
|
|
|
2000-01-06 12:40:54 +00:00
|
|
|
for (masklen = 0; p < lim; p++) {
|
|
|
|
switch (*p) {
|
|
|
|
case 0xff:
|
|
|
|
masklen += 8;
|
|
|
|
break;
|
|
|
|
case 0xfe:
|
|
|
|
masklen += 7;
|
|
|
|
break;
|
|
|
|
case 0xfc:
|
|
|
|
masklen += 6;
|
|
|
|
break;
|
|
|
|
case 0xf8:
|
|
|
|
masklen += 5;
|
|
|
|
break;
|
|
|
|
case 0xf0:
|
|
|
|
masklen += 4;
|
|
|
|
break;
|
|
|
|
case 0xe0:
|
|
|
|
masklen += 3;
|
|
|
|
break;
|
|
|
|
case 0xc0:
|
|
|
|
masklen += 2;
|
|
|
|
break;
|
|
|
|
case 0x80:
|
|
|
|
masklen += 1;
|
|
|
|
break;
|
|
|
|
case 0x00:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(masklen);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rtmsg_type(char *buf)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
|
|
|
|
|
|
|
|
return(rtm->rtm_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rtmsg_len(char *buf)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
|
|
|
|
|
|
|
|
return(rtm->rtm_msglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ifmsg_len(char *buf)
|
|
|
|
{
|
|
|
|
struct if_msghdr *ifm = (struct if_msghdr *)buf;
|
|
|
|
|
|
|
|
return(ifm->ifm_msglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* alloc buffer and get if_msghdrs block from kernel,
|
|
|
|
* and put them into the buffer
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
get_iflist(char **buf, size_t *size)
|
|
|
|
{
|
|
|
|
int mib[6];
|
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = AF_INET6;
|
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
mib[5] = 0;
|
|
|
|
|
|
|
|
if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
|
|
|
|
__FUNCTION__);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ((*buf = malloc(*size)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
|
|
|
|
__FUNCTION__);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* alloc buffer and parse if_msghdrs block passed as arg,
|
|
|
|
* and init the buffer as list of pointers ot each of the if_msghdr.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
int iflentry_size, malloc_size;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct ifa_msghdr *ifam;
|
|
|
|
char *lim;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Estimate least size of an iflist entry, to be obtained from kernel.
|
|
|
|
* Should add sizeof(sockaddr) ??
|
|
|
|
*/
|
|
|
|
iflentry_size = sizeof(struct if_msghdr);
|
|
|
|
/* roughly estimate max list size of pointers to each if_msghdr */
|
|
|
|
malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
|
|
|
|
if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lim = buf + bufsize;
|
|
|
|
for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
|
|
|
|
if (ifm->ifm_msglen == 0) {
|
|
|
|
syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
|
|
|
|
"(buf=%p lim=%p ifm=%p)", __FUNCTION__,
|
|
|
|
buf, lim, ifm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifm->ifm_type == RTM_IFINFO) {
|
|
|
|
(*ifmlist_p)[ifm->ifm_index] = ifm;
|
|
|
|
} else {
|
|
|
|
syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
|
|
|
|
"expected %d, got %d\n msglen = %d\n"
|
|
|
|
"buf:%p, ifm:%p, lim:%p\n",
|
|
|
|
RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
|
|
|
|
buf, ifm, lim);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
for (ifam = (struct ifa_msghdr *)
|
|
|
|
((char *)ifm + ifm->ifm_msglen);
|
|
|
|
ifam < (struct ifa_msghdr *)lim;
|
|
|
|
ifam = (struct ifa_msghdr *)
|
|
|
|
((char *)ifam + ifam->ifam_msglen)) {
|
|
|
|
/* just for safety */
|
|
|
|
if (!ifam->ifam_msglen) {
|
|
|
|
syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
|
|
|
|
"(buf=%p lim=%p ifam=%p)", __FUNCTION__,
|
|
|
|
buf, lim, ifam);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ifam->ifam_type != RTM_NEWADDR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ifm = (struct if_msghdr *)ifam;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_iflist()
|
|
|
|
{
|
|
|
|
if (ifblock) {
|
|
|
|
free(ifblock);
|
|
|
|
ifblock_size = 0;
|
|
|
|
}
|
|
|
|
if (iflist)
|
|
|
|
free(iflist);
|
|
|
|
/* get iflist block from kernel */
|
|
|
|
get_iflist(&ifblock, &ifblock_size);
|
|
|
|
|
|
|
|
/* make list of pointers to each if_msghdr */
|
|
|
|
parse_iflist(&iflist, ifblock, ifblock_size);
|
|
|
|
}
|