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