Commit c70850f5 authored by Leigh B. Stoller's avatar Leigh B. Stoller

Add findif program to map from a MAC address to its interface name.

This program is now invoked from the setup script to determine which
interface needs to be configured. Changed tmcd to return the MAC
address from the DB in the ifconfig response. All this because the
ordering of the interfaces is no longer consistent between linux and
freebsd.
parent b5af39ab
......@@ -8,7 +8,7 @@ SUBDIR = tmcd
include $(OBJDIR)/Makeconf
all: tmcd tmcc tmcd.restart
all: tmcd tmcc tmcd.restart findif
include $(TESTBED_SRCDIR)/GNUmakerules
......@@ -19,7 +19,10 @@ tmcd: tmcd.o
$(LFLAGS) -L/usr/local/lib/mysql -lmysqlclient
tmcc: tmcc.o
$(CC) $(CFLAGS) -g -o tmcc tmcc.o $(LFLAGS)
$(CC) $(CFLAGS) -g -o tmcc tmcc.o $(LFLAGS) -static
findif: findif.o
$(CC) $(CFLAGS) -g -o findif findif.o $(LFLAGS) -static
tmcc.o: tmcc.c decls.h
tmcd.o: tmcd.c decls.h
......
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
*/
/*
* A hugely silly program to map a MAC to the eth/fxp/whatever device.
* Complicated by that fact that no OS agrees on how this info should
* be presented.
*/
#include <stdio.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#ifdef __FreeBSD__
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#endif
#ifdef linux
#include <net/if.h>
#endif
static int find_iface(char *mac);
void
usage()
{
fprintf(stderr, "usage: foo <macaddr>\n");
exit(1);
}
main(int argc, char **argv)
{
unsigned char ether_addr[ETHER_ADDR_LEN];
if (argc != 2)
usage();
exit(find_iface(argv[1]));
}
#ifdef __FreeBSD__
static int
find_iface(char *macaddr)
{
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
char *buf, *lim, *next, *cp;
size_t needed;
int n, mib[6];
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = 0; /* address family */
mib[4] = NET_RT_IFLIST;
mib[5] = 0;
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
errx(1, "iflist-sysctl-estimate");
if ((buf = (char *) malloc(needed)) == NULL)
errx(1, "malloc");
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
errx(1, "actual retrieval of interface table");
lim = buf + needed;
next = buf;
while (next < lim) {
ifm = (struct if_msghdr *) next;
if (ifm->ifm_type == RTM_IFINFO) {
sdl = (struct sockaddr_dl *)(ifm + 1);
}
else {
fprintf(stderr, "error parsing IFLIST\n");
exit(1);
}
next += ifm->ifm_msglen;
while (next < lim) {
struct if_msghdr *nextifm = (struct if_msghdr *)next;
if (nextifm->ifm_type != RTM_NEWADDR)
break;
next += nextifm->ifm_msglen;
}
cp = (char *)LLADDR(sdl);
if ((n = sdl->sdl_alen) > 0 &&
sdl->sdl_type == IFT_ETHER) {
char enet[BUFSIZ], *bp = enet;
*bp = 0;
while (--n >= 0) {
sprintf(bp, "%02x", *cp++ & 0xff);
bp += 2;
}
*bp = 0;
if (strcasecmp(enet, macaddr) == 0) {
printf("%s\n", sdl->sdl_data);
return 0;
}
}
}
return 1;
}
#endif
#ifdef linux
static int
find_iface(char *macaddr)
{
int sock, i;
struct ifreq ifrbuf, *ifr = &ifrbuf;
FILE *fp;
char buf[BUFSIZ], enet[BUFSIZ];
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket()");
return -1;
}
/*
* Get a list of all the interfaces.
*
* SIOCGIFCONF appears to return a list of just the configured
* interfaces, but we need all of them.
*/
if ((fp = fopen("/proc/net/dev", "r")) == NULL) {
fprintf(stderr, "Could not open /proc/net/dev\n");
return -1;
}
/* Eat a couple of lines */
fgets(buf, sizeof(buf), fp);
fgets(buf, sizeof(buf), fp);
while (fgets(buf, sizeof(buf), fp)) {
sscanf(buf, "%s:", ifr->ifr_name);
if (ifr->ifr_name[strlen(ifr->ifr_name) - 1] == ':')
ifr->ifr_name[strlen(ifr->ifr_name) - 1] = 0;
ifr->ifr_addr.sa_family = AF_INET;
if (ioctl(sock, SIOCGIFHWADDR, ifr) < 0)
continue;
sprintf(enet, "%02x%02x%02x%02x%02x%02x",
(unsigned char) ifr->ifr_addr.sa_data[0],
(unsigned char) ifr->ifr_addr.sa_data[1],
(unsigned char) ifr->ifr_addr.sa_data[2],
(unsigned char) ifr->ifr_addr.sa_data[3],
(unsigned char) ifr->ifr_addr.sa_data[4],
(unsigned char) ifr->ifr_addr.sa_data[5]);
/* printf("%s %s\n", ifr->ifr_name, enet); */
if (strcasecmp(enet, macaddr) == 0) {
printf("%s\n", ifr->ifr_name);
fclose(fp);
return 0;
}
}
fclose(fp);
return 1;
}
#endif
......@@ -36,6 +36,7 @@ install:
$(INSTALL) -m 755 $(SRCDIR)/sethostname $(INSTALL_DIR)/sethostname
$(INSTALL) -m 644 $(SRCDIR)/hosts $(INSTALL_DIR)/hosts
$(INSTALL) -m 755 ../tmcc $(INSTALL_DIR)/tmcc
$(INSTALL) -m 755 ../findif $(INSTALL_DIR)/findif
$(INSTALL) -m 755 $(SRCDIR)/supfile $(INSTALL_DIR)/supfile
-mkdir -p /root/.cvsup
$(INSTALL) -m 600 $(SRCDIR)/cvsup.auth /root/.cvsup/auth
......
......@@ -15,6 +15,7 @@ my $TMPASSWD = "/etc/testbed/master.passwd";
my $TMHOSTS = "/etc/testbed/hosts";
my $HOSTSFILE = "/etc/hosts";
my $TMNICKNAME = "/etc/testbed/nickname";
my $FINDIF = "/etc/testbed/findif";
my @CONFIGS = ($TMIFC, $TMDELAY, $TMRPM, $TMSTARTUPCMD, $TMNICKNAME,
$TMTARBALLS);
my $REBOOTCMD = "reboot";
......@@ -29,7 +30,7 @@ my $STARTUPCMD = "startupcmd";
my $DELTACMD = "deltas";
my $STARTSTATCMD= "startstatus";
my $READYCMD = "ready";
my $IFCONFIG = "/sbin/ifconfig fxp%d inet %s netmask %s ".
my $IFCONFIG = "/sbin/ifconfig %s inet %s netmask %s ".
"media 100baseTX mediaopt full-duplex\n";
my $RPMINSTALL = "/usr/local/bin/rpm -i %s\n";
my $CP = "/bin/cp -f";
......@@ -160,9 +161,20 @@ sub doifconfig ()
print IFC "#!/bin/sh\n";
while (<TM>) {
$_ =~ /INTERFACE=(\d*) INET=([0-9.]*) MASK=([0-9.]*)/;
printf STDOUT " $IFCONFIG", $1, $2, $3;
printf IFC $IFCONFIG, $1, $2, $3;
if ($_ =~ /INTERFACE=(\d*) INET=([0-9.]*) MASK=([0-9.]*) MAC=(\w*)/) {
my $iface;
if ($iface = findiface($4)) {
printf STDOUT " $IFCONFIG", $iface, $2, $3;
printf IFC $IFCONFIG, $iface, $2, $3;
}
else {
print STDERR "Bad MAC: $4\n";
}
}
else {
print STDERR "Bad ifconfig line: $_\n";
}
}
close(TM);
close(IFC);
......@@ -600,4 +612,25 @@ sub cleanup_node () {
}
}
#
# Convert from MAC to iface name (eth0/fxp0/etc) using little helper program.
#
sub findiface($)
{
my($mac) = @_;
my($iface);
open(FIF, "$FINDIF $mac |")
or die "Cannot start $FINDIF: $!";
$iface = <FIF>;
if (! close(FIF)) {
return 0;
}
$iface =~ s/\n//g;
return $iface;
}
1;
......@@ -38,6 +38,7 @@ install:
$(INSTALL) -m 755 $(SRCDIR)/sethostname $(INSTALL_DIR)/sethostname
$(INSTALL) -m 644 $(SRCDIR)/hosts $(INSTALL_DIR)/hosts
$(INSTALL) -m 755 ../tmcc $(INSTALL_DIR)/tmcc
$(INSTALL) -m 755 ../findif $(INSTALL_DIR)/findif
$(INSTALL) -m 755 $(SRCDIR)/supfile $(INSTALL_DIR)/supfile
-mkdir -p /root/.cvsup
$(INSTALL) -m 600 $(SRCDIR)/cvsup.auth /root/.cvsup/auth
......
......@@ -15,6 +15,7 @@ my $TMSHADOW = "/etc/rc.d/testbed/shadow";
my $TMGSHADOW = "/etc/rc.d/testbed/gshadow";
my $TMHOSTS = "/etc/rc.d/testbed/hosts";
my $TMNICKNAME = "/etc/rc.d/testbed/nickname";
my $FINDIF = "/etc/rc.d/testbed/findif";
my $HOSTSFILE = "/etc/hosts";
my @CONFIGS = ($TMIFC, $TMRPM, $TMSTARTUPCMD, $TMNICKNAME, $TMTARBALLS);
my @LOCKFILES = ("/etc/group.lock", "/etc/gshadow.lock");
......@@ -28,7 +29,7 @@ my $RPMCMD = "rpms";
my $TARBALLCMD = "tarballs";
my $STARTUPCMD = "startupcmd";
my $DELTACMD = "deltas";
my $IFCONFIG = "/sbin/ifconfig eth%d inet %s netmask %s\n";
my $IFCONFIG = "/sbin/ifconfig %s inet %s netmask %s\n";
my $RPMINSTALL = "/bin/rpm -i %s\n";
my $CP = "/bin/cp -f";
my $USERADD = "/usr/sbin/useradd";
......@@ -153,9 +154,20 @@ sub doifconfig ()
print IFC "#!/bin/sh\n";
while (<TM>) {
$_ =~ /INTERFACE=(\d*) INET=([0-9.]*) MASK=([0-9.]*)/;
printf STDOUT " $IFCONFIG", $1, $2, $3;
printf IFC $IFCONFIG, $1, $2, $3;
if ($_ =~ /INTERFACE=(\d*) INET=([0-9.]*) MASK=([0-9.]*) MAC=(\w*)/) {
my $iface;
if ($iface = findiface($4)) {
printf STDOUT " $IFCONFIG", $iface, $2, $3;
printf IFC $IFCONFIG, $iface, $2, $3;
}
else {
print STDERR "Bad MAC: $4\n";
}
}
else {
print STDERR "Bad ifconfig line: $_\n";
}
}
close(TM);
close(IFC);
......@@ -441,4 +453,25 @@ sub cleanup_node () {
}
}
#
# Convert from MAC to iface name (eth0/fxp0/etc) using little helper program.
#
sub findiface($)
{
my($mac) = @_;
my($iface);
open(FIF, "$FINDIF $mac |")
or die "Cannot start $FINDIF: $!";
$iface = <FIF>;
if (! close(FIF)) {
return 0;
}
$iface =~ s/\n//g;
return $iface;
}
1;
......@@ -523,9 +523,9 @@ doifconfig(int sock, struct in_addr ipaddr, char *rdata, int tcp)
/*
* Find all the interfaces.
*/
res = mydb_query("select card,IP,IPalias from interfaces "
res = mydb_query("select card,IP,IPalias,MAC from interfaces "
"where node_id='%s'",
3, nodeid);
4, nodeid);
if (!res) {
syslog(LOG_ERR, "IFCONFIG: %s: DB Error getting interfaces!",
nodeid);
......@@ -542,6 +542,12 @@ doifconfig(int sock, struct in_addr ipaddr, char *rdata, int tcp)
if (row[1] && row[1][0]) {
int card = atoi(row[0]);
/*
* INTERFACE can go away when all machines running
* updated (MAC based) setup. Right now MAC is at
* the end (see below) cause of the sharks, but they
* should be dead soon too.
*/
sprintf(buf, "INTERFACE=%d INET=%s MASK=%s",
card, row[1], NETMASK);
......@@ -562,6 +568,13 @@ doifconfig(int sock, struct in_addr ipaddr, char *rdata, int tcp)
else if (card == control_net)
goto skipit;
/*
* Tack on MAC, which should go up above after
* Sharks are sunk.
*/
strcat(buf, " MAC=");
strcat(buf, row[3]);
strcat(buf, "\n");
client_writeback(sock, buf, strlen(buf), tcp);
syslog(LOG_INFO, "IFCONFIG: %s", buf);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment