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>
10 * $FreeBSD: src/libexec/bootpd/getether.c,v 1.9.2.3 2003/02/15 05:36:01 kris Exp $
11 * $DragonFly: src/libexec/bootpd/getether.c,v 1.4 2008/06/05 18:01:49 swildner Exp $
14 #include <sys/types.h>
15 #include <sys/socket.h>
30 #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
32 * This is really easy on Ultrix! Thanks to
33 * Harald Lundberg <hl@tekla.fi> for this code.
35 * The code here is not specific to the Alpha, but that was the
36 * only symbol we could find to identify DEC's version of OSF.
37 * (Perhaps we should just define DEC in the Makefile... -gwr)
40 #include <sys/ioctl.h>
41 #include <net/if.h> /* struct ifdevea */
49 bzero(&phys
, sizeof(phys
));
50 strcpy(phys
.ifr_name
, ifname
);
51 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
52 report(LOG_ERR
, "getether: socket(INET,DGRAM) failed");
55 if (ioctl(fd
, SIOCRPHYSADDR
, &phys
) < 0) {
56 report(LOG_ERR
, "getether: ioctl SIOCRPHYSADDR failed");
58 bcopy(&phys
.current_pa
[0], eap
, EALEN
);
66 #endif /* ultrix|osf1 */
71 #include <sys/sockio.h>
72 #include <sys/time.h> /* needed by net_if.h */
73 #include <net/nit_if.h> /* for NIOCBIND */
74 #include <net/if.h> /* for struct ifreq */
77 char *ifname
; /* interface name from ifconfig structure */
78 char *eap
; /* Ether address (output) */
85 bzero((char *) &ifrnit
, sizeof(ifrnit
));
86 strncpy(&ifrnit
.ifr_name
[0], ifname
, IFNAMSIZ
);
88 nit
= open("/dev/nit", 0);
90 report(LOG_ERR
, "getether: open /dev/nit: %s",
95 if (ioctl(nit
, NIOCBIND
, &ifrnit
) < 0) {
96 report(LOG_ERR
, "getether: NIOCBIND on nit");
99 if (ioctl(nit
, SIOCGIFADDR
, &ifrnit
) < 0) {
100 report(LOG_ERR
, "getether: SIOCGIFADDR on nit");
103 bcopy(&ifrnit
.ifr_addr
.sa_data
[0], eap
, EALEN
);
114 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
115 /* Thanks to John Brezak <brezak@ch.hp.com> for this code. */
116 #include <sys/ioctl.h>
117 #include <sys/time.h>
119 #include <net/if_dl.h>
120 #include <net/if_types.h>
123 getether(ifname
, eap
)
124 char *ifname
; /* interface name from ifconfig structure */
125 char *eap
; /* Ether address (output) */
129 struct ifreq ibuf
[16];
131 register struct ifreq
*ifrp
, *ifend
;
133 /* Fetch the interface configuration */
134 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
136 report(LOG_ERR
, "getether: socket %s: %s", ifname
, get_errmsg());
139 ifc
.ifc_len
= sizeof(ibuf
);
140 ifc
.ifc_buf
= (caddr_t
) ibuf
;
141 if (ioctl(fd
, SIOCGIFCONF
, (char *) &ifc
) < 0 ||
142 ifc
.ifc_len
< sizeof(struct ifreq
)) {
143 report(LOG_ERR
, "getether: SIOCGIFCONF: %s", get_errmsg());
146 /* Search interface configuration list for link layer address. */
148 ifend
= (struct ifreq
*) ((char *) ibuf
+ ifc
.ifc_len
);
149 while (ifrp
< ifend
) {
150 /* Look for interface */
151 if (strcmp(ifname
, ifrp
->ifr_name
) == 0 &&
152 ifrp
->ifr_addr
.sa_family
== AF_LINK
&&
153 ((struct sockaddr_dl
*) &ifrp
->ifr_addr
)->sdl_type
== IFT_ETHER
) {
154 bcopy(LLADDR((struct sockaddr_dl
*) &ifrp
->ifr_addr
), eap
, EALEN
);
158 /* Bump interface config pointer */
159 n
= ifrp
->ifr_addr
.sa_len
+ sizeof(ifrp
->ifr_name
);
160 if (n
< sizeof(*ifrp
))
162 ifrp
= (struct ifreq
*) ((char *) ifrp
+ n
);
171 #endif /* __NetBSD__ */
176 * This is for "Streams TCP/IP" by Lachman Associates.
177 * They sure made this cumbersome! -gwr
180 #include <sys/sockio.h>
181 #include <sys/dlpi.h>
186 getether(ifname
, eap
)
187 char *ifname
; /* interface name from ifconfig structure */
188 char *eap
; /* Ether address (output) */
192 char tmpbuf
[sizeof(union DL_primitives
) + 16];
195 union DL_primitives
*dlp
;
197 int unit
= -1; /* which unit to attach */
199 snprintf(devname
, sizeof(devname
), "%s%s", _PATH_DEV
, ifname
);
200 fd
= open(devname
, 2);
202 /* Try without the trailing digit. */
203 char *p
= devname
+ 5;
210 fd
= open(devname
, 2);
212 report(LOG_ERR
, "getether: open %s: %s",
213 devname
, get_errmsg());
219 * If this is a "Style 2" DLPI, then we must "attach" first
220 * to tell the driver which unit (board, port) we want.
221 * For now, decide this based on the device name.
222 * (Should do "info_req" and check dl_provider_style ...)
225 memset(tmpbuf
, 0, sizeof(tmpbuf
));
226 dlp
= (union DL_primitives
*) tmpbuf
;
227 dlp
->dl_primitive
= DL_ATTACH_REQ
;
228 dlp
->attach_req
.dl_ppa
= unit
;
230 cbuf
.len
= DL_ATTACH_REQ_SIZE
;
231 if (putmsg(fd
, &cbuf
, NULL
, 0) < 0) {
232 report(LOG_ERR
, "getether: attach: putmsg: %s", get_errmsg());
237 cbuf
.maxlen
= sizeof(tmpbuf
);
239 if (getmsg(fd
, &cbuf
, NULL
, &flags
) < 0) {
240 report(LOG_ERR
, "getether: attach: getmsg: %s", get_errmsg());
244 * Check the type, etc.
246 if (dlp
->dl_primitive
== DL_ERROR_ACK
) {
247 report(LOG_ERR
, "getether: attach: dlpi_errno=%d, unix_errno=%d",
248 dlp
->error_ack
.dl_errno
,
249 dlp
->error_ack
.dl_unix_errno
);
252 if (dlp
->dl_primitive
!= DL_OK_ACK
) {
253 report(LOG_ERR
, "getether: attach: not OK or ERROR");
257 #endif /* DL_ATTACH_REQ */
260 * Get the Ethernet address the same way the ARP module
261 * does when it is pushed onto a new stream (bind).
262 * One should instead be able just do an dl_info_req
263 * but many drivers do not supply the hardware address
264 * in the response to dl_info_req (they MUST supply it
265 * for dl_bind_ack because the ARP module requires it).
267 memset(tmpbuf
, 0, sizeof(tmpbuf
));
268 dlp
= (union DL_primitives
*) tmpbuf
;
269 dlp
->dl_primitive
= DL_BIND_REQ
;
270 dlp
->bind_req
.dl_sap
= 0x8FF; /* XXX - Unused SAP */
272 cbuf
.len
= DL_BIND_REQ_SIZE
;
273 if (putmsg(fd
, &cbuf
, NULL
, 0) < 0) {
274 report(LOG_ERR
, "getether: bind: putmsg: %s", get_errmsg());
279 cbuf
.maxlen
= sizeof(tmpbuf
);
281 if (getmsg(fd
, &cbuf
, NULL
, &flags
) < 0) {
282 report(LOG_ERR
, "getether: bind: getmsg: %s", get_errmsg());
286 * Check the type, etc.
288 if (dlp
->dl_primitive
== DL_ERROR_ACK
) {
289 report(LOG_ERR
, "getether: bind: dlpi_errno=%d, unix_errno=%d",
290 dlp
->error_ack
.dl_errno
,
291 dlp
->error_ack
.dl_unix_errno
);
294 if (dlp
->dl_primitive
!= DL_BIND_ACK
) {
295 report(LOG_ERR
, "getether: bind: not OK or ERROR");
298 if (dlp
->bind_ack
.dl_addr_offset
== 0) {
299 report(LOG_ERR
, "getether: bind: ack has no address");
302 if (dlp
->bind_ack
.dl_addr_length
< EALEN
) {
303 report(LOG_ERR
, "getether: bind: ack address truncated");
307 * Copy the Ethernet address out of the message.
309 enaddr
= tmpbuf
+ dlp
->bind_ack
.dl_addr_offset
;
310 memcpy(eap
, enaddr
, EALEN
);
324 * This is really easy on Linux! This version (for linux)
325 * written by Nigel Metheringham <nigelm@ohm.york.ac.uk> and
326 * updated by Pauline Middelink <middelin@polyware.iaf.nl>
328 * The code is almost identical to the Ultrix code - however
329 * the names are different to confuse the innocent :-)
330 * Most of this code was stolen from the Ultrix bit above.
334 #include <sys/ioctl.h>
335 #include <net/if.h> /* struct ifreq */
336 #include <sys/socketio.h> /* Needed for IOCTL defs */
339 getether(ifname
, eap
)
346 memset(&phys
, 0, sizeof(phys
));
347 strcpy(phys
.ifr_name
, ifname
);
348 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
349 report(LOG_ERR
, "getether: socket(INET,DGRAM) failed");
352 if (ioctl(fd
, SIOCGIFHWADDR
, &phys
) < 0) {
353 report(LOG_ERR
, "getether: ioctl SIOCGIFHWADDR failed");
355 memcpy(eap
, &phys
.ifr_hwaddr
.sa_data
, EALEN
);
363 #endif /* __linux__ */
366 /* If we don't know how on this system, just return an error. */
369 getether(ifname
, eap
)
375 #endif /* !GETETHER */
381 * c-argdecl-indent: 4
382 * c-continued-statement-offset: 4
383 * c-continued-brace-offset: -4