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