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 $
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 strncpy(&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__) || defined(__DragonFly__)
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 * ifname - interface name from ifconfig structure
123 * eap - Ether address (output)
126 getether(char *ifname
, char *eap
)
130 struct ifreq ibuf
[16];
132 struct ifreq
*ifrp
, *ifend
;
134 /* Fetch the interface configuration */
135 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
137 report(LOG_ERR
, "getether: socket %s: %s", ifname
, get_errmsg());
140 ifc
.ifc_len
= sizeof(ibuf
);
141 ifc
.ifc_buf
= (caddr_t
) ibuf
;
142 if (ioctl(fd
, SIOCGIFCONF
, (char *) &ifc
) < 0 ||
143 ifc
.ifc_len
< sizeof(struct ifreq
)) {
144 report(LOG_ERR
, "getether: SIOCGIFCONF: %s", get_errmsg());
147 /* Search interface configuration list for link layer address. */
149 ifend
= (struct ifreq
*) ((char *) ibuf
+ ifc
.ifc_len
);
150 while (ifrp
< ifend
) {
151 /* Look for interface */
152 if (strcmp(ifname
, ifrp
->ifr_name
) == 0 &&
153 ifrp
->ifr_addr
.sa_family
== AF_LINK
&&
154 ((struct sockaddr_dl
*) &ifrp
->ifr_addr
)->sdl_type
== IFT_ETHER
) {
155 bcopy(LLADDR((struct sockaddr_dl
*) &ifrp
->ifr_addr
), eap
, EALEN
);
159 /* Bump interface config pointer */
160 n
= ifrp
->ifr_addr
.sa_len
+ sizeof(ifrp
->ifr_name
);
161 if (n
< sizeof(*ifrp
))
163 ifrp
= (struct ifreq
*) ((char *) ifrp
+ n
);
172 #endif /* __NetBSD__ */
177 * This is for "Streams TCP/IP" by Lachman Associates.
178 * They sure made this cumbersome! -gwr
181 #include <sys/sockio.h>
182 #include <sys/dlpi.h>
187 getether(ifname
, eap
)
188 char *ifname
; /* interface name from ifconfig structure */
189 char *eap
; /* Ether address (output) */
193 char tmpbuf
[sizeof(union DL_primitives
) + 16];
196 union DL_primitives
*dlp
;
198 int unit
= -1; /* which unit to attach */
200 snprintf(devname
, sizeof(devname
), "%s%s", _PATH_DEV
, ifname
);
201 fd
= open(devname
, 2);
203 /* Try without the trailing digit. */
204 char *p
= devname
+ 5;
211 fd
= open(devname
, 2);
213 report(LOG_ERR
, "getether: open %s: %s",
214 devname
, get_errmsg());
220 * If this is a "Style 2" DLPI, then we must "attach" first
221 * to tell the driver which unit (board, port) we want.
222 * For now, decide this based on the device name.
223 * (Should do "info_req" and check dl_provider_style ...)
226 memset(tmpbuf
, 0, sizeof(tmpbuf
));
227 dlp
= (union DL_primitives
*) tmpbuf
;
228 dlp
->dl_primitive
= DL_ATTACH_REQ
;
229 dlp
->attach_req
.dl_ppa
= unit
;
231 cbuf
.len
= DL_ATTACH_REQ_SIZE
;
232 if (putmsg(fd
, &cbuf
, NULL
, 0) < 0) {
233 report(LOG_ERR
, "getether: attach: putmsg: %s", get_errmsg());
238 cbuf
.maxlen
= sizeof(tmpbuf
);
240 if (getmsg(fd
, &cbuf
, NULL
, &flags
) < 0) {
241 report(LOG_ERR
, "getether: attach: getmsg: %s", get_errmsg());
245 * Check the type, etc.
247 if (dlp
->dl_primitive
== DL_ERROR_ACK
) {
248 report(LOG_ERR
, "getether: attach: dlpi_errno=%d, unix_errno=%d",
249 dlp
->error_ack
.dl_errno
,
250 dlp
->error_ack
.dl_unix_errno
);
253 if (dlp
->dl_primitive
!= DL_OK_ACK
) {
254 report(LOG_ERR
, "getether: attach: not OK or ERROR");
258 #endif /* DL_ATTACH_REQ */
261 * Get the Ethernet address the same way the ARP module
262 * does when it is pushed onto a new stream (bind).
263 * One should instead be able just do an dl_info_req
264 * but many drivers do not supply the hardware address
265 * in the response to dl_info_req (they MUST supply it
266 * for dl_bind_ack because the ARP module requires it).
268 memset(tmpbuf
, 0, sizeof(tmpbuf
));
269 dlp
= (union DL_primitives
*) tmpbuf
;
270 dlp
->dl_primitive
= DL_BIND_REQ
;
271 dlp
->bind_req
.dl_sap
= 0x8FF; /* XXX - Unused SAP */
273 cbuf
.len
= DL_BIND_REQ_SIZE
;
274 if (putmsg(fd
, &cbuf
, NULL
, 0) < 0) {
275 report(LOG_ERR
, "getether: bind: putmsg: %s", get_errmsg());
280 cbuf
.maxlen
= sizeof(tmpbuf
);
282 if (getmsg(fd
, &cbuf
, NULL
, &flags
) < 0) {
283 report(LOG_ERR
, "getether: bind: getmsg: %s", get_errmsg());
287 * Check the type, etc.
289 if (dlp
->dl_primitive
== DL_ERROR_ACK
) {
290 report(LOG_ERR
, "getether: bind: dlpi_errno=%d, unix_errno=%d",
291 dlp
->error_ack
.dl_errno
,
292 dlp
->error_ack
.dl_unix_errno
);
295 if (dlp
->dl_primitive
!= DL_BIND_ACK
) {
296 report(LOG_ERR
, "getether: bind: not OK or ERROR");
299 if (dlp
->bind_ack
.dl_addr_offset
== 0) {
300 report(LOG_ERR
, "getether: bind: ack has no address");
303 if (dlp
->bind_ack
.dl_addr_length
< EALEN
) {
304 report(LOG_ERR
, "getether: bind: ack address truncated");
308 * Copy the Ethernet address out of the message.
310 enaddr
= tmpbuf
+ dlp
->bind_ack
.dl_addr_offset
;
311 memcpy(eap
, enaddr
, EALEN
);
325 * This is really easy on Linux! This version (for linux)
326 * written by Nigel Metheringham <nigelm@ohm.york.ac.uk> and
327 * updated by Pauline Middelink <middelin@polyware.iaf.nl>
329 * The code is almost identical to the Ultrix code - however
330 * the names are different to confuse the innocent :-)
331 * Most of this code was stolen from the Ultrix bit above.
335 #include <sys/ioctl.h>
336 #include <net/if.h> /* struct ifreq */
337 #include <sys/socketio.h> /* Needed for IOCTL defs */
340 getether(ifname
, eap
)
347 memset(&phys
, 0, sizeof(phys
));
348 strcpy(phys
.ifr_name
, ifname
);
349 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
350 report(LOG_ERR
, "getether: socket(INET,DGRAM) failed");
353 if (ioctl(fd
, SIOCGIFHWADDR
, &phys
) < 0) {
354 report(LOG_ERR
, "getether: ioctl SIOCGIFHWADDR failed");
356 memcpy(eap
, &phys
.ifr_hwaddr
.sa_data
, EALEN
);
364 #endif /* __linux__ */
367 /* If we don't know how on this system, just return an error. */
370 getether(ifname
, eap
)
376 #endif /* !GETETHER */