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