1994-09-30 05:45:07 +00:00
|
|
|
|
/*
|
|
|
|
|
* getether.c : get the ethernet address of an interface
|
|
|
|
|
*
|
|
|
|
|
* All of this code is quite system-specific. As you may well
|
|
|
|
|
* guess, it took a good bit of detective work to figure out!
|
|
|
|
|
*
|
|
|
|
|
* If you figure out how to do this on another system,
|
|
|
|
|
* please let me know. <gwr@mc.com>
|
1996-09-22 21:56:57 +00:00
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
|
* $FreeBSD$
|
1994-09-30 05:45:07 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
1996-01-23 01:35:04 +00:00
|
|
|
|
#ifndef NO_UNISTD
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
1994-09-30 05:45:07 +00:00
|
|
|
|
#include <ctype.h>
|
2000-12-09 09:35:55 +00:00
|
|
|
|
#include <paths.h>
|
2001-09-29 11:37:13 +00:00
|
|
|
|
#include <string.h>
|
1994-09-30 05:45:07 +00:00
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
1996-01-23 01:35:04 +00:00
|
|
|
|
#include "getether.h"
|
1994-09-30 05:45:07 +00:00
|
|
|
|
#include "report.h"
|
|
|
|
|
#define EALEN 6
|
|
|
|
|
|
|
|
|
|
#if defined(ultrix) || (defined(__osf__) && defined(__alpha))
|
|
|
|
|
/*
|
|
|
|
|
* This is really easy on Ultrix! Thanks to
|
|
|
|
|
* Harald Lundberg <hl@tekla.fi> for this code.
|
|
|
|
|
*
|
|
|
|
|
* The code here is not specific to the Alpha, but that was the
|
|
|
|
|
* only symbol we could find to identify DEC's version of OSF.
|
|
|
|
|
* (Perhaps we should just define DEC in the Makefile... -gwr)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <net/if.h> /* struct ifdevea */
|
|
|
|
|
|
|
|
|
|
getether(ifname, eap)
|
|
|
|
|
char *ifname, *eap;
|
|
|
|
|
{
|
|
|
|
|
int rc = -1;
|
|
|
|
|
int fd;
|
|
|
|
|
struct ifdevea phys;
|
|
|
|
|
bzero(&phys, sizeof(phys));
|
|
|
|
|
strcpy(phys.ifr_name, ifname);
|
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (ioctl(fd, SIOCRPHYSADDR, &phys) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: ioctl SIOCRPHYSADDR failed");
|
|
|
|
|
} else {
|
|
|
|
|
bcopy(&phys.current_pa[0], eap, EALEN);
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define GETETHER
|
|
|
|
|
#endif /* ultrix|osf1 */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SUNOS
|
|
|
|
|
|
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
|
#include <sys/time.h> /* needed by net_if.h */
|
|
|
|
|
#include <net/nit_if.h> /* for NIOCBIND */
|
|
|
|
|
#include <net/if.h> /* for struct ifreq */
|
|
|
|
|
|
|
|
|
|
getether(ifname, eap)
|
|
|
|
|
char *ifname; /* interface name from ifconfig structure */
|
|
|
|
|
char *eap; /* Ether address (output) */
|
|
|
|
|
{
|
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
|
|
struct ifreq ifrnit;
|
|
|
|
|
int nit;
|
|
|
|
|
|
|
|
|
|
bzero((char *) &ifrnit, sizeof(ifrnit));
|
2002-10-13 11:25:31 +00:00
|
|
|
|
strlcpy(&ifrnit.ifr_name[0], ifname, IFNAMSIZ);
|
1994-09-30 05:45:07 +00:00
|
|
|
|
|
|
|
|
|
nit = open("/dev/nit", 0);
|
|
|
|
|
if (nit < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: open /dev/nit: %s",
|
|
|
|
|
get_errmsg());
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
do {
|
|
|
|
|
if (ioctl(nit, NIOCBIND, &ifrnit) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: NIOCBIND on nit");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (ioctl(nit, SIOCGIFADDR, &ifrnit) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: SIOCGIFADDR on nit");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
bcopy(&ifrnit.ifr_addr.sa_data[0], eap, EALEN);
|
|
|
|
|
rc = 0;
|
|
|
|
|
} while (0);
|
|
|
|
|
close(nit);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define GETETHER
|
|
|
|
|
#endif /* SUNOS */
|
|
|
|
|
|
|
|
|
|
|
1996-01-23 01:58:00 +00:00
|
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__)
|
1994-09-30 05:45:07 +00:00
|
|
|
|
/* Thanks to John Brezak <brezak@ch.hp.com> for this code. */
|
|
|
|
|
#include <sys/ioctl.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
|
#include <sys/time.h>
|
1994-09-30 05:45:07 +00:00
|
|
|
|
#include <net/if.h>
|
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
|
#include <net/if_types.h>
|
|
|
|
|
|
2001-09-29 11:37:13 +00:00
|
|
|
|
int
|
1994-09-30 05:45:07 +00:00
|
|
|
|
getether(ifname, eap)
|
|
|
|
|
char *ifname; /* interface name from ifconfig structure */
|
|
|
|
|
char *eap; /* Ether address (output) */
|
|
|
|
|
{
|
|
|
|
|
int fd, rc = -1;
|
|
|
|
|
register int n;
|
2001-09-29 11:37:13 +00:00
|
|
|
|
struct ifreq ibuf[16];
|
1994-09-30 05:45:07 +00:00
|
|
|
|
struct ifconf ifc;
|
|
|
|
|
register struct ifreq *ifrp, *ifend;
|
|
|
|
|
|
|
|
|
|
/* Fetch the interface configuration */
|
|
|
|
|
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: socket %s: %s", ifname, get_errmsg());
|
|
|
|
|
return (fd);
|
|
|
|
|
}
|
|
|
|
|
ifc.ifc_len = sizeof(ibuf);
|
|
|
|
|
ifc.ifc_buf = (caddr_t) ibuf;
|
|
|
|
|
if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0 ||
|
|
|
|
|
ifc.ifc_len < sizeof(struct ifreq)) {
|
2002-02-04 01:28:45 +00:00
|
|
|
|
report(LOG_ERR, "getether: SIOCGIFCONF: %s", get_errmsg());
|
1994-09-30 05:45:07 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
/* Search interface configuration list for link layer address. */
|
|
|
|
|
ifrp = ibuf;
|
|
|
|
|
ifend = (struct ifreq *) ((char *) ibuf + ifc.ifc_len);
|
|
|
|
|
while (ifrp < ifend) {
|
|
|
|
|
/* Look for interface */
|
|
|
|
|
if (strcmp(ifname, ifrp->ifr_name) == 0 &&
|
|
|
|
|
ifrp->ifr_addr.sa_family == AF_LINK &&
|
|
|
|
|
((struct sockaddr_dl *) &ifrp->ifr_addr)->sdl_type == IFT_ETHER) {
|
|
|
|
|
bcopy(LLADDR((struct sockaddr_dl *) &ifrp->ifr_addr), eap, EALEN);
|
|
|
|
|
rc = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Bump interface config pointer */
|
|
|
|
|
n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
|
|
|
|
|
if (n < sizeof(*ifrp))
|
|
|
|
|
n = sizeof(*ifrp);
|
|
|
|
|
ifrp = (struct ifreq *) ((char *) ifrp + n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
close(fd);
|
|
|
|
|
return (rc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define GETETHER
|
|
|
|
|
#endif /* __NetBSD__ */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SVR4
|
|
|
|
|
/*
|
|
|
|
|
* This is for "Streams TCP/IP" by Lachman Associates.
|
|
|
|
|
* They sure made this cumbersome! -gwr
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
|
#include <sys/dlpi.h>
|
|
|
|
|
#include <stropts.h>
|
1996-01-23 01:35:04 +00:00
|
|
|
|
#include <string.h>
|
1994-09-30 05:45:07 +00:00
|
|
|
|
#ifndef NULL
|
|
|
|
|
#define NULL 0
|
|
|
|
|
#endif
|
|
|
|
|
|
1996-01-23 01:35:04 +00:00
|
|
|
|
int
|
1994-09-30 05:45:07 +00:00
|
|
|
|
getether(ifname, eap)
|
|
|
|
|
char *ifname; /* interface name from ifconfig structure */
|
|
|
|
|
char *eap; /* Ether address (output) */
|
|
|
|
|
{
|
|
|
|
|
int rc = -1;
|
|
|
|
|
char devname[32];
|
|
|
|
|
char tmpbuf[sizeof(union DL_primitives) + 16];
|
|
|
|
|
struct strbuf cbuf;
|
|
|
|
|
int fd, flags;
|
|
|
|
|
union DL_primitives *dlp;
|
|
|
|
|
char *enaddr;
|
|
|
|
|
int unit = -1; /* which unit to attach */
|
|
|
|
|
|
2000-12-09 09:35:55 +00:00
|
|
|
|
snprintf(devname, sizeof(devname), "%s%s", _PATH_DEV, ifname);
|
1994-09-30 05:45:07 +00:00
|
|
|
|
fd = open(devname, 2);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
/* Try without the trailing digit. */
|
|
|
|
|
char *p = devname + 5;
|
|
|
|
|
while (isalpha(*p))
|
|
|
|
|
p++;
|
|
|
|
|
if (isdigit(*p)) {
|
|
|
|
|
unit = *p - '0';
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
fd = open(devname, 2);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: open %s: %s",
|
|
|
|
|
devname, get_errmsg());
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#ifdef DL_ATTACH_REQ
|
|
|
|
|
/*
|
|
|
|
|
* If this is a "Style 2" DLPI, then we must "attach" first
|
|
|
|
|
* to tell the driver which unit (board, port) we want.
|
|
|
|
|
* For now, decide this based on the device name.
|
|
|
|
|
* (Should do "info_req" and check dl_provider_style ...)
|
|
|
|
|
*/
|
|
|
|
|
if (unit >= 0) {
|
|
|
|
|
memset(tmpbuf, 0, sizeof(tmpbuf));
|
|
|
|
|
dlp = (union DL_primitives *) tmpbuf;
|
|
|
|
|
dlp->dl_primitive = DL_ATTACH_REQ;
|
|
|
|
|
dlp->attach_req.dl_ppa = unit;
|
|
|
|
|
cbuf.buf = tmpbuf;
|
|
|
|
|
cbuf.len = DL_ATTACH_REQ_SIZE;
|
|
|
|
|
if (putmsg(fd, &cbuf, NULL, 0) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: attach: putmsg: %s", get_errmsg());
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
/* Recv the ack. */
|
|
|
|
|
cbuf.buf = tmpbuf;
|
|
|
|
|
cbuf.maxlen = sizeof(tmpbuf);
|
|
|
|
|
flags = 0;
|
|
|
|
|
if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: attach: getmsg: %s", get_errmsg());
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Check the type, etc.
|
|
|
|
|
*/
|
|
|
|
|
if (dlp->dl_primitive == DL_ERROR_ACK) {
|
|
|
|
|
report(LOG_ERR, "getether: attach: dlpi_errno=%d, unix_errno=%d",
|
|
|
|
|
dlp->error_ack.dl_errno,
|
|
|
|
|
dlp->error_ack.dl_unix_errno);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
if (dlp->dl_primitive != DL_OK_ACK) {
|
|
|
|
|
report(LOG_ERR, "getether: attach: not OK or ERROR");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
} /* unit >= 0 */
|
|
|
|
|
#endif /* DL_ATTACH_REQ */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get the Ethernet address the same way the ARP module
|
|
|
|
|
* does when it is pushed onto a new stream (bind).
|
|
|
|
|
* One should instead be able just do an dl_info_req
|
|
|
|
|
* but many drivers do not supply the hardware address
|
|
|
|
|
* in the response to dl_info_req (they MUST supply it
|
|
|
|
|
* for dl_bind_ack because the ARP module requires it).
|
|
|
|
|
*/
|
|
|
|
|
memset(tmpbuf, 0, sizeof(tmpbuf));
|
|
|
|
|
dlp = (union DL_primitives *) tmpbuf;
|
|
|
|
|
dlp->dl_primitive = DL_BIND_REQ;
|
|
|
|
|
dlp->bind_req.dl_sap = 0x8FF; /* XXX - Unused SAP */
|
|
|
|
|
cbuf.buf = tmpbuf;
|
|
|
|
|
cbuf.len = DL_BIND_REQ_SIZE;
|
|
|
|
|
if (putmsg(fd, &cbuf, NULL, 0) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: bind: putmsg: %s", get_errmsg());
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
/* Recv the ack. */
|
|
|
|
|
cbuf.buf = tmpbuf;
|
|
|
|
|
cbuf.maxlen = sizeof(tmpbuf);
|
|
|
|
|
flags = 0;
|
|
|
|
|
if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: bind: getmsg: %s", get_errmsg());
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Check the type, etc.
|
|
|
|
|
*/
|
|
|
|
|
if (dlp->dl_primitive == DL_ERROR_ACK) {
|
|
|
|
|
report(LOG_ERR, "getether: bind: dlpi_errno=%d, unix_errno=%d",
|
|
|
|
|
dlp->error_ack.dl_errno,
|
|
|
|
|
dlp->error_ack.dl_unix_errno);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
if (dlp->dl_primitive != DL_BIND_ACK) {
|
|
|
|
|
report(LOG_ERR, "getether: bind: not OK or ERROR");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
if (dlp->bind_ack.dl_addr_offset == 0) {
|
|
|
|
|
report(LOG_ERR, "getether: bind: ack has no address");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
if (dlp->bind_ack.dl_addr_length < EALEN) {
|
|
|
|
|
report(LOG_ERR, "getether: bind: ack address truncated");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Copy the Ethernet address out of the message.
|
|
|
|
|
*/
|
|
|
|
|
enaddr = tmpbuf + dlp->bind_ack.dl_addr_offset;
|
|
|
|
|
memcpy(eap, enaddr, EALEN);
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
close(fd);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define GETETHER
|
|
|
|
|
#endif /* SVR4 */
|
|
|
|
|
|
|
|
|
|
|
1996-01-23 01:35:04 +00:00
|
|
|
|
#ifdef __linux__
|
1994-09-30 05:45:07 +00:00
|
|
|
|
/*
|
|
|
|
|
* This is really easy on Linux! This version (for linux)
|
1996-01-23 01:35:04 +00:00
|
|
|
|
* written by Nigel Metheringham <nigelm@ohm.york.ac.uk> and
|
|
|
|
|
* updated by Pauline Middelink <middelin@polyware.iaf.nl>
|
1994-09-30 05:45:07 +00:00
|
|
|
|
*
|
|
|
|
|
* The code is almost identical to the Ultrix code - however
|
|
|
|
|
* the names are different to confuse the innocent :-)
|
|
|
|
|
* Most of this code was stolen from the Ultrix bit above.
|
|
|
|
|
*/
|
|
|
|
|
|
1996-01-23 01:35:04 +00:00
|
|
|
|
#include <memory.h>
|
1994-09-30 05:45:07 +00:00
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <net/if.h> /* struct ifreq */
|
1996-01-23 01:35:04 +00:00
|
|
|
|
#include <sys/socketio.h> /* Needed for IOCTL defs */
|
1994-09-30 05:45:07 +00:00
|
|
|
|
|
1996-01-23 01:35:04 +00:00
|
|
|
|
int
|
1994-09-30 05:45:07 +00:00
|
|
|
|
getether(ifname, eap)
|
|
|
|
|
char *ifname, *eap;
|
|
|
|
|
{
|
|
|
|
|
int rc = -1;
|
|
|
|
|
int fd;
|
|
|
|
|
struct ifreq phys;
|
1996-01-23 01:35:04 +00:00
|
|
|
|
|
|
|
|
|
memset(&phys, 0, sizeof(phys));
|
1994-09-30 05:45:07 +00:00
|
|
|
|
strcpy(phys.ifr_name, ifname);
|
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (ioctl(fd, SIOCGIFHWADDR, &phys) < 0) {
|
|
|
|
|
report(LOG_ERR, "getether: ioctl SIOCGIFHWADDR failed");
|
|
|
|
|
} else {
|
1996-01-23 01:35:04 +00:00
|
|
|
|
memcpy(eap, &phys.ifr_hwaddr.sa_data, EALEN);
|
1994-09-30 05:45:07 +00:00
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define GETETHER
|
1996-01-23 01:35:04 +00:00
|
|
|
|
#endif /* __linux__ */
|
1994-09-30 05:45:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* If we don't know how on this system, just return an error. */
|
|
|
|
|
#ifndef GETETHER
|
1996-01-23 01:35:04 +00:00
|
|
|
|
int
|
1994-09-30 05:45:07 +00:00
|
|
|
|
getether(ifname, eap)
|
|
|
|
|
char *ifname, *eap;
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* !GETETHER */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local Variables:
|
|
|
|
|
* tab-width: 4
|
|
|
|
|
* c-indent-level: 4
|
|
|
|
|
* c-argdecl-indent: 4
|
|
|
|
|
* c-continued-statement-offset: 4
|
|
|
|
|
* c-continued-brace-offset: -4
|
|
|
|
|
* c-label-offset: -4
|
|
|
|
|
* c-brace-offset: 0
|
|
|
|
|
* End:
|
|
|
|
|
*/
|