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