Home | History | Annotate | Line # | Download | only in bootptest
getether.c revision 1.1
      1  1.1  gwr /*
      2  1.1  gwr  * getether.c : get the ethernet address of an interface
      3  1.1  gwr  *
      4  1.1  gwr  * All of this code is quite system-specific.  As you may well
      5  1.1  gwr  * guess, it took a good bit of detective work to figure out!
      6  1.1  gwr  *
      7  1.1  gwr  * If you figure out how to do this on another system,
      8  1.1  gwr  * please let me know.  <gwr (at) mc.com>
      9  1.1  gwr  */
     10  1.1  gwr 
     11  1.1  gwr #include <sys/types.h>
     12  1.1  gwr #include <sys/socket.h>
     13  1.1  gwr 
     14  1.1  gwr #include <ctype.h>
     15  1.1  gwr #include <syslog.h>
     16  1.1  gwr 
     17  1.1  gwr #include "report.h"
     18  1.1  gwr #define EALEN 6
     19  1.1  gwr 
     20  1.1  gwr #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
     21  1.1  gwr /*
     22  1.1  gwr  * This is really easy on Ultrix!  Thanks to
     23  1.1  gwr  * Harald Lundberg <hl (at) tekla.fi> for this code.
     24  1.1  gwr  *
     25  1.1  gwr  * The code here is not specific to the Alpha, but that was the
     26  1.1  gwr  * only symbol we could find to identify DEC's version of OSF.
     27  1.1  gwr  * (Perhaps we should just define DEC in the Makefile... -gwr)
     28  1.1  gwr  */
     29  1.1  gwr 
     30  1.1  gwr #include <sys/ioctl.h>
     31  1.1  gwr #include <net/if.h>				/* struct ifdevea */
     32  1.1  gwr 
     33  1.1  gwr getether(ifname, eap)
     34  1.1  gwr 	char *ifname, *eap;
     35  1.1  gwr {
     36  1.1  gwr 	int rc = -1;
     37  1.1  gwr 	int fd;
     38  1.1  gwr 	struct ifdevea phys;
     39  1.1  gwr 	bzero(&phys, sizeof(phys));
     40  1.1  gwr 	strcpy(phys.ifr_name, ifname);
     41  1.1  gwr 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
     42  1.1  gwr 		report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
     43  1.1  gwr 		return -1;
     44  1.1  gwr 	}
     45  1.1  gwr 	if (ioctl(fd, SIOCRPHYSADDR, &phys) < 0) {
     46  1.1  gwr 		report(LOG_ERR, "getether: ioctl SIOCRPHYSADDR failed");
     47  1.1  gwr 	} else {
     48  1.1  gwr 		bcopy(&phys.current_pa[0], eap, EALEN);
     49  1.1  gwr 		rc = 0;
     50  1.1  gwr 	}
     51  1.1  gwr 	close(fd);
     52  1.1  gwr 	return rc;
     53  1.1  gwr }
     54  1.1  gwr 
     55  1.1  gwr #define	GETETHER
     56  1.1  gwr #endif /* ultrix|osf1 */
     57  1.1  gwr 
     58  1.1  gwr 
     60  1.1  gwr #ifdef	SUNOS
     61  1.1  gwr 
     62  1.1  gwr #include <sys/sockio.h>
     63  1.1  gwr #include <sys/time.h>			/* needed by net_if.h */
     64  1.1  gwr #include <net/nit_if.h>			/* for NIOCBIND */
     65  1.1  gwr #include <net/if.h>				/* for struct ifreq */
     66  1.1  gwr 
     67  1.1  gwr getether(ifname, eap)
     68  1.1  gwr 	char *ifname;				/* interface name from ifconfig structure */
     69  1.1  gwr 	char *eap;					/* Ether address (output) */
     70  1.1  gwr {
     71  1.1  gwr 	int rc = -1;
     72  1.1  gwr 
     73  1.1  gwr 	struct ifreq ifrnit;
     74  1.1  gwr 	int nit;
     75  1.1  gwr 
     76  1.1  gwr 	bzero((char *) &ifrnit, sizeof(ifrnit));
     77  1.1  gwr 	strncpy(&ifrnit.ifr_name[0], ifname, IFNAMSIZ);
     78  1.1  gwr 
     79  1.1  gwr 	nit = open("/dev/nit", 0);
     80  1.1  gwr 	if (nit < 0) {
     81  1.1  gwr 		report(LOG_ERR, "getether: open /dev/nit: %s",
     82  1.1  gwr 			   get_errmsg());
     83  1.1  gwr 		return rc;
     84  1.1  gwr 	}
     85  1.1  gwr 	do {
     86  1.1  gwr 		if (ioctl(nit, NIOCBIND, &ifrnit) < 0) {
     87  1.1  gwr 			report(LOG_ERR, "getether: NIOCBIND on nit");
     88  1.1  gwr 			break;
     89  1.1  gwr 		}
     90  1.1  gwr 		if (ioctl(nit, SIOCGIFADDR, &ifrnit) < 0) {
     91  1.1  gwr 			report(LOG_ERR, "getether: SIOCGIFADDR on nit");
     92  1.1  gwr 			break;
     93  1.1  gwr 		}
     94  1.1  gwr 		bcopy(&ifrnit.ifr_addr.sa_data[0], eap, EALEN);
     95  1.1  gwr 		rc = 0;
     96  1.1  gwr 	} while (0);
     97  1.1  gwr 	close(nit);
     98  1.1  gwr 	return rc;
     99  1.1  gwr }
    100  1.1  gwr 
    101  1.1  gwr #define	GETETHER
    102  1.1  gwr #endif /* SUNOS */
    103  1.1  gwr 
    104  1.1  gwr 
    106  1.1  gwr #if defined(__386BSD__) || defined(__NetBSD__)
    107  1.1  gwr /* Thanks to John Brezak <brezak (at) ch.hp.com> for this code. */
    108  1.1  gwr #include <sys/ioctl.h>
    109  1.1  gwr #include <net/if.h>
    110  1.1  gwr #include <net/if_dl.h>
    111  1.1  gwr #include <net/if_types.h>
    112  1.1  gwr 
    113  1.1  gwr getether(ifname, eap)
    114  1.1  gwr 	char *ifname;				/* interface name from ifconfig structure */
    115  1.1  gwr 	char *eap;					/* Ether address (output) */
    116  1.1  gwr {
    117  1.1  gwr 	int fd, rc = -1;
    118  1.1  gwr 	register int n;
    119  1.1  gwr 	struct ifreq ibuf[16], ifr;
    120  1.1  gwr 	struct ifconf ifc;
    121  1.1  gwr 	register struct ifreq *ifrp, *ifend;
    122  1.1  gwr 
    123  1.1  gwr 	/* Fetch the interface configuration */
    124  1.1  gwr 	fd = socket(AF_INET, SOCK_DGRAM, 0);
    125  1.1  gwr 	if (fd < 0) {
    126  1.1  gwr 		report(LOG_ERR, "getether: socket %s: %s", ifname, get_errmsg());
    127  1.1  gwr 		return (fd);
    128  1.1  gwr 	}
    129  1.1  gwr 	ifc.ifc_len = sizeof(ibuf);
    130  1.1  gwr 	ifc.ifc_buf = (caddr_t) ibuf;
    131  1.1  gwr 	if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0 ||
    132  1.1  gwr 		ifc.ifc_len < sizeof(struct ifreq)) {
    133  1.1  gwr 		report(LOG_ERR, "getether: SIOCGIFCONF: %s", get_errmsg);
    134  1.1  gwr 		goto out;
    135  1.1  gwr 	}
    136  1.1  gwr 	/* Search interface configuration list for link layer address. */
    137  1.1  gwr 	ifrp = ibuf;
    138  1.1  gwr 	ifend = (struct ifreq *) ((char *) ibuf + ifc.ifc_len);
    139  1.1  gwr 	while (ifrp < ifend) {
    140  1.1  gwr 		/* Look for interface */
    141  1.1  gwr 		if (strcmp(ifname, ifrp->ifr_name) == 0 &&
    142  1.1  gwr 			ifrp->ifr_addr.sa_family == AF_LINK &&
    143  1.1  gwr 		((struct sockaddr_dl *) &ifrp->ifr_addr)->sdl_type == IFT_ETHER) {
    144  1.1  gwr 			bcopy(LLADDR((struct sockaddr_dl *) &ifrp->ifr_addr), eap, EALEN);
    145  1.1  gwr 			rc = 0;
    146  1.1  gwr 			break;
    147  1.1  gwr 		}
    148  1.1  gwr 		/* Bump interface config pointer */
    149  1.1  gwr 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
    150  1.1  gwr 		if (n < sizeof(*ifrp))
    151  1.1  gwr 			n = sizeof(*ifrp);
    152  1.1  gwr 		ifrp = (struct ifreq *) ((char *) ifrp + n);
    153  1.1  gwr 	}
    154  1.1  gwr 
    155  1.1  gwr   out:
    156  1.1  gwr 	close(fd);
    157  1.1  gwr 	return (rc);
    158  1.1  gwr }
    159  1.1  gwr 
    160  1.1  gwr #define	GETETHER
    161  1.1  gwr #endif /* __NetBSD__ */
    162  1.1  gwr 
    163  1.1  gwr 
    165  1.1  gwr #ifdef	SVR4
    166  1.1  gwr /*
    167  1.1  gwr  * This is for "Streams TCP/IP" by Lachman Associates.
    168  1.1  gwr  * They sure made this cumbersome!  -gwr
    169  1.1  gwr  */
    170  1.1  gwr 
    171  1.1  gwr #include <sys/sockio.h>
    172  1.1  gwr #include <sys/dlpi.h>
    173  1.1  gwr #include <stropts.h>
    174  1.1  gwr #ifndef NULL
    175  1.1  gwr #define NULL 0
    176  1.1  gwr #endif
    177  1.1  gwr 
    178  1.1  gwr getether(ifname, eap)
    179  1.1  gwr 	char *ifname;				/* interface name from ifconfig structure */
    180  1.1  gwr 	char *eap;					/* Ether address (output) */
    181  1.1  gwr {
    182  1.1  gwr 	int rc = -1;
    183  1.1  gwr 	char devname[32];
    184  1.1  gwr 	char tmpbuf[sizeof(union DL_primitives) + 16];
    185  1.1  gwr 	struct strbuf cbuf;
    186  1.1  gwr 	int fd, flags;
    187  1.1  gwr 	union DL_primitives *dlp;
    188  1.1  gwr 	char *enaddr;
    189  1.1  gwr 	int unit = -1;				/* which unit to attach */
    190  1.1  gwr 
    191  1.1  gwr 	sprintf(devname, "/dev/%s", ifname);
    192  1.1  gwr 	fd = open(devname, 2);
    193  1.1  gwr 	if (fd < 0) {
    194  1.1  gwr 		/* Try without the trailing digit. */
    195  1.1  gwr 		char *p = devname + 5;
    196  1.1  gwr 		while (isalpha(*p))
    197  1.1  gwr 			p++;
    198  1.1  gwr 		if (isdigit(*p)) {
    199  1.1  gwr 			unit = *p - '0';
    200  1.1  gwr 			*p = '\0';
    201  1.1  gwr 		}
    202  1.1  gwr 		fd = open(devname, 2);
    203  1.1  gwr 		if (fd < 0) {
    204  1.1  gwr 			report(LOG_ERR, "getether: open %s: %s",
    205  1.1  gwr 				   devname, get_errmsg());
    206  1.1  gwr 			return rc;
    207  1.1  gwr 		}
    208  1.1  gwr 	}
    209  1.1  gwr #ifdef	DL_ATTACH_REQ
    210  1.1  gwr 	/*
    211  1.1  gwr 	 * If this is a "Style 2" DLPI, then we must "attach" first
    212  1.1  gwr 	 * to tell the driver which unit (board, port) we want.
    213  1.1  gwr 	 * For now, decide this based on the device name.
    214  1.1  gwr 	 * (Should do "info_req" and check dl_provider_style ...)
    215  1.1  gwr 	 */
    216  1.1  gwr 	if (unit >= 0) {
    217  1.1  gwr 		memset(tmpbuf, 0, sizeof(tmpbuf));
    218  1.1  gwr 		dlp = (union DL_primitives *) tmpbuf;
    219  1.1  gwr 		dlp->dl_primitive = DL_ATTACH_REQ;
    220  1.1  gwr 		dlp->attach_req.dl_ppa = unit;
    221  1.1  gwr 		cbuf.buf = tmpbuf;
    222  1.1  gwr 		cbuf.len = DL_ATTACH_REQ_SIZE;
    223  1.1  gwr 		if (putmsg(fd, &cbuf, NULL, 0) < 0) {
    224  1.1  gwr 			report(LOG_ERR, "getether: attach: putmsg: %s", get_errmsg());
    225  1.1  gwr 			goto out;
    226  1.1  gwr 		}
    227  1.1  gwr 		/* Recv the ack. */
    228  1.1  gwr 		cbuf.buf = tmpbuf;
    229  1.1  gwr 		cbuf.maxlen = sizeof(tmpbuf);
    230  1.1  gwr 		flags = 0;
    231  1.1  gwr 		if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
    232  1.1  gwr 			report(LOG_ERR, "getether: attach: getmsg: %s", get_errmsg());
    233  1.1  gwr 			goto out;
    234  1.1  gwr 		}
    235  1.1  gwr 		/*
    236  1.1  gwr 		 * Check the type, etc.
    237  1.1  gwr 		 */
    238  1.1  gwr 		if (dlp->dl_primitive == DL_ERROR_ACK) {
    239  1.1  gwr 			report(LOG_ERR, "getether: attach: dlpi_errno=%d, unix_errno=%d",
    240  1.1  gwr 				   dlp->error_ack.dl_errno,
    241  1.1  gwr 				   dlp->error_ack.dl_unix_errno);
    242  1.1  gwr 			goto out;
    243  1.1  gwr 		}
    244  1.1  gwr 		if (dlp->dl_primitive != DL_OK_ACK) {
    245  1.1  gwr 			report(LOG_ERR, "getether: attach: not OK or ERROR");
    246  1.1  gwr 			goto out;
    247  1.1  gwr 		}
    248  1.1  gwr 	} /* unit >= 0 */
    249  1.1  gwr #endif	/* DL_ATTACH_REQ */
    250  1.1  gwr 
    251  1.1  gwr 	/*
    252  1.1  gwr 	 * Get the Ethernet address the same way the ARP module
    253  1.1  gwr 	 * does when it is pushed onto a new stream (bind).
    254  1.1  gwr 	 * One should instead be able just do an dl_info_req
    255  1.1  gwr 	 * but many drivers do not supply the hardware address
    256  1.1  gwr 	 * in the response to dl_info_req (they MUST supply it
    257  1.1  gwr 	 * for dl_bind_ack because the ARP module requires it).
    258  1.1  gwr 	 */
    259  1.1  gwr 	memset(tmpbuf, 0, sizeof(tmpbuf));
    260  1.1  gwr 	dlp = (union DL_primitives *) tmpbuf;
    261  1.1  gwr 	dlp->dl_primitive = DL_BIND_REQ;
    262  1.1  gwr 	dlp->bind_req.dl_sap = 0x8FF;	/* XXX - Unused SAP */
    263  1.1  gwr 	cbuf.buf = tmpbuf;
    264  1.1  gwr 	cbuf.len = DL_BIND_REQ_SIZE;
    265  1.1  gwr 	if (putmsg(fd, &cbuf, NULL, 0) < 0) {
    266  1.1  gwr 		report(LOG_ERR, "getether: bind: putmsg: %s", get_errmsg());
    267  1.1  gwr 		goto out;
    268  1.1  gwr 	}
    269  1.1  gwr 	/* Recv the ack. */
    270  1.1  gwr 	cbuf.buf = tmpbuf;
    271  1.1  gwr 	cbuf.maxlen = sizeof(tmpbuf);
    272  1.1  gwr 	flags = 0;
    273  1.1  gwr 	if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
    274  1.1  gwr 		report(LOG_ERR, "getether: bind: getmsg: %s", get_errmsg());
    275  1.1  gwr 		goto out;
    276  1.1  gwr 	}
    277  1.1  gwr 	/*
    278  1.1  gwr 	 * Check the type, etc.
    279  1.1  gwr 	 */
    280  1.1  gwr 	if (dlp->dl_primitive == DL_ERROR_ACK) {
    281  1.1  gwr 		report(LOG_ERR, "getether: bind: dlpi_errno=%d, unix_errno=%d",
    282  1.1  gwr 			   dlp->error_ack.dl_errno,
    283  1.1  gwr 			   dlp->error_ack.dl_unix_errno);
    284  1.1  gwr 		goto out;
    285  1.1  gwr 	}
    286  1.1  gwr 	if (dlp->dl_primitive != DL_BIND_ACK) {
    287  1.1  gwr 		report(LOG_ERR, "getether: bind: not OK or ERROR");
    288  1.1  gwr 		goto out;
    289  1.1  gwr 	}
    290  1.1  gwr 	if (dlp->bind_ack.dl_addr_offset == 0) {
    291  1.1  gwr 		report(LOG_ERR, "getether: bind: ack has no address");
    292  1.1  gwr 		goto out;
    293  1.1  gwr 	}
    294  1.1  gwr 	if (dlp->bind_ack.dl_addr_length < EALEN) {
    295  1.1  gwr 		report(LOG_ERR, "getether: bind: ack address truncated");
    296  1.1  gwr 		goto out;
    297  1.1  gwr 	}
    298  1.1  gwr 	/*
    299  1.1  gwr 	 * Copy the Ethernet address out of the message.
    300  1.1  gwr 	 */
    301  1.1  gwr 	enaddr = tmpbuf + dlp->bind_ack.dl_addr_offset;
    302  1.1  gwr 	memcpy(eap, enaddr, EALEN);
    303  1.1  gwr 	rc = 0;
    304  1.1  gwr 
    305  1.1  gwr   out:
    306  1.1  gwr 	close(fd);
    307  1.1  gwr 	return rc;
    308  1.1  gwr }
    309  1.1  gwr 
    310  1.1  gwr #define	GETETHER
    311  1.1  gwr #endif /* SVR4 */
    312  1.1  gwr 
    313  1.1  gwr 
    315  1.1  gwr #ifdef	linux
    316  1.1  gwr /*
    317  1.1  gwr  * This is really easy on Linux!  This version (for linux)
    318  1.1  gwr  * written by Nigel Metheringham <nigelm (at) ohm.york.ac.uk>
    319  1.1  gwr  *
    320  1.1  gwr  * The code is almost identical to the Ultrix code - however
    321  1.1  gwr  * the names are different to confuse the innocent :-)
    322  1.1  gwr  * Most of this code was stolen from the Ultrix bit above.
    323  1.1  gwr  */
    324  1.1  gwr 
    325  1.1  gwr #include <sys/ioctl.h>
    326  1.1  gwr #include <net/if.h>	       	/* struct ifreq */
    327  1.1  gwr 
    328  1.1  gwr /* In a properly configured system this should be either sys/socketio.h
    329  1.1  gwr    or sys/sockios.h, but on my distribution these don't line up correctly */
    330  1.1  gwr #include <linux/sockios.h>	/* Needed for IOCTL defs */
    331  1.1  gwr 
    332  1.1  gwr getether(ifname, eap)
    333  1.1  gwr 	char *ifname, *eap;
    334  1.1  gwr {
    335  1.1  gwr 	int rc = -1;
    336  1.1  gwr 	int fd;
    337  1.1  gwr 	struct ifreq phys;
    338  1.1  gwr 	bzero(&phys, sizeof(phys));
    339  1.1  gwr 	strcpy(phys.ifr_name, ifname);
    340  1.1  gwr 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    341  1.1  gwr 		report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
    342  1.1  gwr 		return -1;
    343  1.1  gwr 	}
    344  1.1  gwr 	if (ioctl(fd, SIOCGIFHWADDR, &phys) < 0) {
    345  1.1  gwr 		report(LOG_ERR, "getether: ioctl SIOCGIFHWADDR failed");
    346  1.1  gwr 	} else {
    347  1.1  gwr 		bcopy(phys.ifr_hwaddr, eap, EALEN);
    348  1.1  gwr 		rc = 0;
    349  1.1  gwr 	}
    350  1.1  gwr 	close(fd);
    351  1.1  gwr 	return rc;
    352  1.1  gwr }
    353  1.1  gwr 
    354  1.1  gwr #define	GETETHER
    355  1.1  gwr #endif	/* linux */
    356  1.1  gwr 
    357  1.1  gwr 
    358  1.1  gwr /* If we don't know how on this system, just return an error. */
    359  1.1  gwr #ifndef	GETETHER
    360  1.1  gwr getether(ifname, eap)
    361  1.1  gwr 	char *ifname, *eap;
    362  1.1  gwr {
    363  1.1  gwr 	return -1;
    364  1.1  gwr }
    365  1.1  gwr 
    366  1.1  gwr #endif /* !GETETHER */
    367  1.1  gwr 
    368  1.1  gwr /*
    369  1.1  gwr  * Local Variables:
    370  1.1  gwr  * tab-width: 4
    371  1.1  gwr  * c-indent-level: 4
    372  1.1  gwr  * c-argdecl-indent: 4
    373  1.1  gwr  * c-continued-statement-offset: 4
    374  1.1  gwr  * c-continued-brace-offset: -4
    375            * c-label-offset: -4
    376            * c-brace-offset: 0
    377            * End:
    378            */
    379