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