From c70850f5fc6b760f7069625911674ea92c8b222b Mon Sep 17 00:00:00 2001 From: "Leigh B. Stoller" Date: Tue, 21 Aug 2001 22:50:14 +0000 Subject: [PATCH] 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. --- tmcd/GNUmakefile.in | 7 +- tmcd/findif.c | 201 ++++++++++++++++++++++++++++++++++++ tmcd/freebsd/GNUmakefile.in | 1 + tmcd/freebsd/setuplib.pm | 41 +++++++- tmcd/linux/GNUmakefile.in | 1 + tmcd/linux/setuplib.pm | 41 +++++++- tmcd/tmcd.c | 17 ++- 7 files changed, 297 insertions(+), 12 deletions(-) create mode 100644 tmcd/findif.c diff --git a/tmcd/GNUmakefile.in b/tmcd/GNUmakefile.in index 249b5325e..f74fd5d80 100644 --- a/tmcd/GNUmakefile.in +++ b/tmcd/GNUmakefile.in @@ -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 diff --git a/tmcd/findif.c b/tmcd/findif.c new file mode 100644 index 000000000..d4dae5da4 --- /dev/null +++ b/tmcd/findif.c @@ -0,0 +1,201 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#ifdef __FreeBSD__ +#include +#include +#include +#include +#endif +#ifdef linux +#include +#endif + +static int find_iface(char *mac); + +void +usage() +{ + fprintf(stderr, "usage: foo \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 diff --git a/tmcd/freebsd/GNUmakefile.in b/tmcd/freebsd/GNUmakefile.in index 1d14582f5..0918e4709 100644 --- a/tmcd/freebsd/GNUmakefile.in +++ b/tmcd/freebsd/GNUmakefile.in @@ -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 diff --git a/tmcd/freebsd/setuplib.pm b/tmcd/freebsd/setuplib.pm index 4ebb5aa53..df791ee57 100755 --- a/tmcd/freebsd/setuplib.pm +++ b/tmcd/freebsd/setuplib.pm @@ -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 () { - $_ =~ /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 = ; + + if (! close(FIF)) { + return 0; + } + + $iface =~ s/\n//g; + return $iface; +} + 1; diff --git a/tmcd/linux/GNUmakefile.in b/tmcd/linux/GNUmakefile.in index 1e0d03610..5d64cd772 100644 --- a/tmcd/linux/GNUmakefile.in +++ b/tmcd/linux/GNUmakefile.in @@ -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 diff --git a/tmcd/linux/setuplib.pm b/tmcd/linux/setuplib.pm index 68bbf57d6..d72d7fd24 100755 --- a/tmcd/linux/setuplib.pm +++ b/tmcd/linux/setuplib.pm @@ -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 () { - $_ =~ /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 = ; + + if (! close(FIF)) { + return 0; + } + + $iface =~ s/\n//g; + return $iface; +} + 1; diff --git a/tmcd/tmcd.c b/tmcd/tmcd.c index 3ae58f668..eac703577 100644 --- a/tmcd/tmcd.c +++ b/tmcd/tmcd.c @@ -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); -- GitLab