2 * getether.c : get the ethernet address of an interface
4 * All of this code is quite system-specific. As you may well
5 * guess, it took a good bit of detective work to figure out!
7 * If you figure out how to do this on another system,
8 * please let me know. <gwr@mc.com>
13 #include <sys/types.h>
14 #include <sys/socket.h>
29 #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
31 * This is really easy on Ultrix! Thanks to
32 * Harald Lundberg <hl@tekla.fi> for this code.
34 * The code here is not specific to the Alpha, but that was the
35 * only symbol we could find to identify DEC's version of OSF.
36 * (Perhaps we should just define DEC in the Makefile... -gwr)
39 #include <sys/ioctl.h>
40 #include <net/if.h> /* struct ifdevea */
48 bzero(&phys, sizeof(phys));
49 strcpy(phys.ifr_name, ifname);
50 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
51 report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
54 if (ioctl(fd, SIOCRPHYSADDR, &phys) < 0) {
55 report(LOG_ERR, "getether: ioctl SIOCRPHYSADDR failed");
57 bcopy(&phys.current_pa[0], eap, EALEN);
65 #endif /* ultrix|osf1 */
70 #include <sys/sockio.h>
71 #include <sys/time.h> /* needed by net_if.h */
72 #include <net/nit_if.h> /* for NIOCBIND */
73 #include <net/if.h> /* for struct ifreq */
76 char *ifname; /* interface name from ifconfig structure */
77 char *eap; /* Ether address (output) */
84 bzero((char *) &ifrnit, sizeof(ifrnit));
85 strlcpy(&ifrnit.ifr_name[0], ifname, IFNAMSIZ);
87 nit = open("/dev/nit", 0);
89 report(LOG_ERR, "getether: open /dev/nit: %s",
94 if (ioctl(nit, NIOCBIND, &ifrnit) < 0) {
95 report(LOG_ERR, "getether: NIOCBIND on nit");
98 if (ioctl(nit, SIOCGIFADDR, &ifrnit) < 0) {
99 report(LOG_ERR, "getether: SIOCGIFADDR on nit");
102 bcopy(&ifrnit.ifr_addr.sa_data[0], eap, EALEN);
113 #if defined(__FreeBSD__) || defined(__NetBSD__)
114 /* Thanks to John Brezak <brezak@ch.hp.com> for this code. */
115 #include <sys/ioctl.h>
116 #include <sys/time.h>
118 #include <net/if_dl.h>
119 #include <net/if_types.h>
122 getether(ifname, eap)
123 char *ifname; /* interface name from ifconfig structure */
124 char *eap; /* Ether address (output) */
128 struct ifreq ibuf[16];
130 struct ifreq *ifrp, *ifend;
132 /* Fetch the interface configuration */
133 fd = socket(AF_INET, SOCK_DGRAM, 0);
135 report(LOG_ERR, "getether: socket %s: %s", ifname, get_errmsg());
138 ifc.ifc_len = sizeof(ibuf);
139 ifc.ifc_buf = (caddr_t) ibuf;
140 if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0 ||
141 ifc.ifc_len < sizeof(struct ifreq)) {
142 report(LOG_ERR, "getether: SIOCGIFCONF: %s", get_errmsg());
145 /* Search interface configuration list for link layer address. */
147 ifend = (struct ifreq *) ((char *) ibuf + ifc.ifc_len);
148 while (ifrp < ifend) {
149 /* Look for interface */
150 if (strcmp(ifname, ifrp->ifr_name) == 0 &&
151 ifrp->ifr_addr.sa_family == AF_LINK &&
152 ((struct sockaddr_dl *) &ifrp->ifr_addr)->sdl_type == IFT_ETHER) {
153 bcopy(LLADDR((struct sockaddr_dl *) &ifrp->ifr_addr), eap, EALEN);
157 /* Bump interface config pointer */
158 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
159 if (n < sizeof(*ifrp))
161 ifrp = (struct ifreq *) ((char *) ifrp + n);
170 #endif /* __NetBSD__ */
175 * This is for "Streams TCP/IP" by Lachman Associates.
176 * They sure made this cumbersome! -gwr
179 #include <sys/sockio.h>
180 #include <sys/dlpi.h>
188 getether(ifname, eap)
189 char *ifname; /* interface name from ifconfig structure */
190 char *eap; /* Ether address (output) */
194 char tmpbuf[sizeof(union DL_primitives) + 16];
197 union DL_primitives *dlp;
199 int unit = -1; /* which unit to attach */
201 snprintf(devname, sizeof(devname), "%s%s", _PATH_DEV, ifname);
202 fd = open(devname, 2);
204 /* Try without the trailing digit. */
205 char *p = devname + 5;
212 fd = open(devname, 2);
214 report(LOG_ERR, "getether: open %s: %s",
215 devname, get_errmsg());
221 * If this is a "Style 2" DLPI, then we must "attach" first
222 * to tell the driver which unit (board, port) we want.
223 * For now, decide this based on the device name.
224 * (Should do "info_req" and check dl_provider_style ...)
227 memset(tmpbuf, 0, sizeof(tmpbuf));
228 dlp = (union DL_primitives *) tmpbuf;
229 dlp->dl_primitive = DL_ATTACH_REQ;
230 dlp->attach_req.dl_ppa = unit;
232 cbuf.len = DL_ATTACH_REQ_SIZE;
233 if (putmsg(fd, &cbuf, NULL, 0) < 0) {
234 report(LOG_ERR, "getether: attach: putmsg: %s", get_errmsg());
239 cbuf.maxlen = sizeof(tmpbuf);
241 if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
242 report(LOG_ERR, "getether: attach: getmsg: %s", get_errmsg());
246 * Check the type, etc.
248 if (dlp->dl_primitive == DL_ERROR_ACK) {
249 report(LOG_ERR, "getether: attach: dlpi_errno=%d, unix_errno=%d",
250 dlp->error_ack.dl_errno,
251 dlp->error_ack.dl_unix_errno);
254 if (dlp->dl_primitive != DL_OK_ACK) {
255 report(LOG_ERR, "getether: attach: not OK or ERROR");
259 #endif /* DL_ATTACH_REQ */
262 * Get the Ethernet address the same way the ARP module
263 * does when it is pushed onto a new stream (bind).
264 * One should instead be able just do a dl_info_req
265 * but many drivers do not supply the hardware address
266 * in the response to dl_info_req (they MUST supply it
267 * for dl_bind_ack because the ARP module requires it).
269 memset(tmpbuf, 0, sizeof(tmpbuf));
270 dlp = (union DL_primitives *) tmpbuf;
271 dlp->dl_primitive = DL_BIND_REQ;
272 dlp->bind_req.dl_sap = 0x8FF; /* XXX - Unused SAP */
274 cbuf.len = DL_BIND_REQ_SIZE;
275 if (putmsg(fd, &cbuf, NULL, 0) < 0) {
276 report(LOG_ERR, "getether: bind: putmsg: %s", get_errmsg());
281 cbuf.maxlen = sizeof(tmpbuf);
283 if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
284 report(LOG_ERR, "getether: bind: getmsg: %s", get_errmsg());
288 * Check the type, etc.
290 if (dlp->dl_primitive == DL_ERROR_ACK) {
291 report(LOG_ERR, "getether: bind: dlpi_errno=%d, unix_errno=%d",
292 dlp->error_ack.dl_errno,
293 dlp->error_ack.dl_unix_errno);
296 if (dlp->dl_primitive != DL_BIND_ACK) {
297 report(LOG_ERR, "getether: bind: not OK or ERROR");
300 if (dlp->bind_ack.dl_addr_offset == 0) {
301 report(LOG_ERR, "getether: bind: ack has no address");
304 if (dlp->bind_ack.dl_addr_length < EALEN) {
305 report(LOG_ERR, "getether: bind: ack address truncated");
309 * Copy the Ethernet address out of the message.
311 enaddr = tmpbuf + dlp->bind_ack.dl_addr_offset;
312 memcpy(eap, enaddr, EALEN);
326 * This is really easy on Linux! This version (for linux)
327 * written by Nigel Metheringham <nigelm@ohm.york.ac.uk> and
328 * updated by Pauline Middelink <middelin@polyware.iaf.nl>
330 * The code is almost identical to the Ultrix code - however
331 * the names are different to confuse the innocent :-)
332 * Most of this code was stolen from the Ultrix bit above.
336 #include <sys/ioctl.h>
337 #include <net/if.h> /* struct ifreq */
338 #include <sys/socketio.h> /* Needed for IOCTL defs */
341 getether(ifname, eap)
348 memset(&phys, 0, sizeof(phys));
349 strcpy(phys.ifr_name, ifname);
350 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
351 report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
354 if (ioctl(fd, SIOCGIFHWADDR, &phys) < 0) {
355 report(LOG_ERR, "getether: ioctl SIOCGIFHWADDR failed");
357 memcpy(eap, &phys.ifr_hwaddr.sa_data, EALEN);
365 #endif /* __linux__ */
368 /* If we don't know how on this system, just return an error. */
371 getether(ifname, eap)
377 #endif /* !GETETHER */
383 * c-argdecl-indent: 4
384 * c-continued-statement-offset: 4
385 * c-continued-brace-offset: -4