o SIOCGIFCONF -> getifaddrs(3) conversion. As a side effect fix

bin/95041: subnet mask mismatch.

PR:		bin/95041
Obtained from:	NetBSD
Tested by:	Hans Lambermont
MFC after:	2 months
This commit is contained in:
Maxim Konovalov 2006-05-17 18:41:05 +00:00
parent af65c53afd
commit 27e42e6a7e
2 changed files with 43 additions and 96 deletions

View File

@ -15,6 +15,7 @@
#endif
#include "defs.h"
#include <netdb.h>
#include <ifaddrs.h>
/*
* Local function declarations
@ -25,7 +26,7 @@ static void yyerror __P((char *s));
static char * next_word __P((void));
static int yylex __P((void));
static u_int32 valid_if __P((char *s));
static struct ifreq * ifconfaddr __P((struct ifconf *ifcp, u_int32 a));
static const char * ifconfaddr(u_int32_t a);
int yyparse __P((void));
static FILE *f;
@ -128,15 +129,14 @@ stmt : error
}
ifmods
| TUNNEL interface addrname {
struct ifreq *ifr;
const char *ifname;
struct ifreq ffr;
vifi_t vifi;
order++;
ifr = ifconfaddr(&ifc, $2);
if (ifr == 0)
ifname = ifconfaddr($2);
if (ifname == 0)
fatal("Tunnel local address %s is not mine",
inet_fmt($2, s1));
@ -145,7 +145,7 @@ stmt : error
fatal("Tunnel local address %s is a loopback address",
inet_fmt($2, s1));
if (ifconfaddr(&ifc, $3) != 0)
if (ifconfaddr($3) != 0)
fatal("Tunnel remote address %s is one of mine",
inet_fmt($3, s1));
@ -165,7 +165,7 @@ stmt : error
if (numvifs == MAXVIFS)
fatal("too many vifs");
strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ);
strlcpy(ffr.ifr_name, ifname, sizeof(ffr.ifr_name));
if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0)
fatal("ioctl SIOCGIFFLAGS on %s", ffr.ifr_name);
@ -176,8 +176,7 @@ stmt : error
v->uv_lcl_addr = $2;
v->uv_rmt_addr = $3;
v->uv_dst_addr = $3;
strncpy(v->uv_name, ffr.ifr_name, IFNAMSIZ);
v->uv_name[IFNAMSIZ-1]='\0';
strlcpy(v->uv_name, ffr.ifr_name, sizeof(v->uv_name));
if (!(ffr.ifr_flags & IFF_UP)) {
v->uv_flags |= VIFF_DOWN;
@ -905,28 +904,24 @@ char *s;
return 0;
}
static struct ifreq *
ifconfaddr(ifcp, a)
struct ifconf *ifcp;
u_int32 a;
static const char *
ifconfaddr(u_int32_t a)
{
int n;
struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf;
struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len);
static char ifname[IFNAMSIZ];
struct ifaddrs *ifap, *ifa;
while (ifrp < ifend) {
if (ifrp->ifr_addr.sa_family == AF_INET &&
((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a)
return (ifrp);
#ifdef HAVE_SA_LEN
n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
if (n < sizeof(*ifrp))
++ifrp;
else
ifrp = (struct ifreq *)((char *)ifrp + n);
#else
++ifrp;
#endif
if (getifaddrs(&ifap) != 0)
return (NULL);
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family == AF_INET &&
((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == a) {
strlcpy(ifname, ifa->ifa_name, sizeof(ifname));
freeifaddrs(ifap);
return (ifname);
}
}
return (0);
freeifaddrs(ifap);
return (NULL);
}

View File

@ -16,9 +16,7 @@ static const char rcsid[] =
#endif /* not lint */
#include "defs.h"
struct ifconf ifc;
#include <ifaddrs.h>
/*
* Query the kernel to find network interfaces that are multicast-capable
@ -27,94 +25,47 @@ struct ifconf ifc;
void
config_vifs_from_kernel()
{
struct ifreq *ifrp, *ifend;
struct ifaddrs *ifa, *ifap;
register struct uvif *v;
register vifi_t vifi;
int n;
u_int32 addr, mask, subnet;
int flags;
int num_ifreq = 32;
ifc.ifc_len = num_ifreq * sizeof(struct ifreq);
ifc.ifc_buf = malloc(ifc.ifc_len);
while (ifc.ifc_buf) {
if (ioctl(udp_socket, SIOCGIFCONF, (char *)&ifc) < 0)
log(LOG_ERR, errno, "ioctl SIOCGIFCONF");
/*
* If the buffer was large enough to hold all the addresses
* then break out, otherwise increase the buffer size and
* try again.
*
* The only way to know that we definitely had enough space
* is to know that there was enough space for at least one
* more struct ifreq. ???
*/
if ((num_ifreq * sizeof(struct ifreq)) >=
ifc.ifc_len + sizeof(struct ifreq))
break;
num_ifreq *= 2;
ifc.ifc_len = num_ifreq * sizeof(struct ifreq);
ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
}
if (ifc.ifc_buf == NULL)
log(LOG_ERR, 0, "config_vifs_from_kernel: ran out of memory");
ifrp = (struct ifreq *)ifc.ifc_buf;
ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len);
if (getifaddrs(&ifap) < 0)
log(LOG_ERR, errno, "getifaddrs");
/*
* Loop through all of the interfaces.
*/
for (; ifrp < ifend; ifrp = (struct ifreq *)((char *)ifrp + n)) {
struct ifreq ifr;
#ifdef HAVE_SA_LEN
n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
if (n < sizeof(*ifrp))
n = sizeof(*ifrp);
#else
n = sizeof(*ifrp);
#endif
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
/*
* Ignore any interface for an address family other than IP.
*/
if (ifrp->ifr_addr.sa_family != AF_INET)
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
addr = ((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr;
/*
* Need a template to preserve address info that is
* used below to locate the next entry. (Otherwise,
* SIOCGIFFLAGS stomps over it because the requests
* are returned in a union.)
*/
bcopy(ifrp->ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
/*
* Ignore loopback interfaces and interfaces that do not support
* multicast.
*/
if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ifr) < 0)
log(LOG_ERR, errno, "ioctl SIOCGIFFLAGS for %s", ifr.ifr_name);
flags = ifr.ifr_flags;
if ((flags & (IFF_LOOPBACK|IFF_MULTICAST)) != IFF_MULTICAST) continue;
flags = ifa->ifa_flags;
if ((flags & (IFF_LOOPBACK|IFF_MULTICAST)) != IFF_MULTICAST)
continue;
/*
* Ignore any interface whose address and mask do not define a
* valid subnet number, or whose address is of the form {subnet,0}
* or {subnet,-1}.
*/
if (ioctl(udp_socket, SIOCGIFNETMASK, (char *)&ifr) < 0)
log(LOG_ERR, errno, "ioctl SIOCGIFNETMASK for %s", ifr.ifr_name);
mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
mask = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr;
subnet = addr & mask;
if (!inet_valid_subnet(subnet, mask) ||
addr == subnet ||
addr == (subnet | ~mask)) {
log(LOG_WARNING, 0,
"ignoring %s, has invalid address (%s) and/or mask (%s)",
ifr.ifr_name, inet_fmt(addr, s1), inet_fmt(mask, s2));
ifa->ifa_name, inet_fmt(addr, s1), inet_fmt(mask, s2));
continue;
}
@ -123,7 +74,7 @@ config_vifs_from_kernel()
* one already installed in the uvifs array.
*/
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (strcmp(v->uv_name, ifr.ifr_name) == 0) {
if (strcmp(v->uv_name, ifa->ifa_name) == 0) {
log(LOG_DEBUG, 0, "skipping %s (%s on subnet %s) (alias for vif#%u?)",
v->uv_name, inet_fmt(addr, s1),
inet_fmts(subnet, mask, s2), vifi);
@ -132,7 +83,7 @@ config_vifs_from_kernel()
if ((addr & v->uv_subnetmask) == v->uv_subnet ||
(v->uv_subnet & mask) == subnet) {
log(LOG_WARNING, 0, "ignoring %s, same subnet as %s",
ifr.ifr_name, v->uv_name);
ifa->ifa_name, v->uv_name);
break;
}
}
@ -142,7 +93,7 @@ config_vifs_from_kernel()
* If there is room in the uvifs array, install this interface.
*/
if (numvifs == MAXVIFS) {
log(LOG_WARNING, 0, "too many vifs, ignoring %s", ifr.ifr_name);
log(LOG_WARNING, 0, "too many vifs, ignoring %s", ifa->ifa_name);
continue;
}
v = &uvifs[numvifs];
@ -151,8 +102,7 @@ config_vifs_from_kernel()
v->uv_subnet = subnet;
v->uv_subnetmask = mask;
v->uv_subnetbcast = subnet | ~mask;
strncpy(v->uv_name, ifr.ifr_name, IFNAMSIZ);
v->uv_name[IFNAMSIZ-1] = '\0';
strlcpy(v->uv_name, ifa->ifa_name, sizeof(v->uv_name));
if (flags & IFF_POINTOPOINT)
v->uv_flags |= VIFF_REXMIT_PRUNES;
@ -172,4 +122,6 @@ config_vifs_from_kernel()
vifs_down = TRUE;
}
}
freeifaddrs(ifap);
}