xref: /netbsd-src/usr.sbin/bootp/bootptest/getether.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: getether.c,v 1.7 2003/07/14 08:36:34 itojun Exp $	*/
2 
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: getether.c,v 1.7 2003/07/14 08:36:34 itojun Exp $");
6 #endif
7 
8 /*
9  * getether.c : get the ethernet address of an interface
10  *
11  * All of this code is quite system-specific.  As you may well
12  * guess, it took a good bit of detective work to figure out!
13  *
14  * If you figure out how to do this on another system,
15  * please let me know.  <gwr@mc.com>
16  */
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 
21 #include <ctype.h>
22 #include <string.h>
23 #include <syslog.h>
24 #include <unistd.h>
25 
26 #include "report.h"
27 #define EALEN 6
28 
29 extern int getether(char *, char *);
30 
31 #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
32 /*
33  * This is really easy on Ultrix!  Thanks to
34  * Harald Lundberg <hl@tekla.fi> for this code.
35  *
36  * The code here is not specific to the Alpha, but that was the
37  * only symbol we could find to identify DEC's version of OSF.
38  * (Perhaps we should just define DEC in the Makefile... -gwr)
39  */
40 
41 #include <sys/ioctl.h>
42 #include <net/if.h>				/* struct ifdevea */
43 
44 int
45 getether(char *ifname, char *eap)
46 {
47 	int rc = -1;
48 	int fd;
49 	struct ifdevea phys;
50 
51 	bzero(&phys, sizeof(phys));
52 	strncpy(phys.ifr_name, ifname, sizeof(phys.ifr_name));
53 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
54 		report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
55 		return -1;
56 	}
57 	if (ioctl(fd, SIOCRPHYSADDR, &phys) < 0) {
58 		report(LOG_ERR, "getether: ioctl SIOCRPHYSADDR failed");
59 	} else {
60 		bcopy(&phys.current_pa[0], eap, EALEN);
61 		rc = 0;
62 	}
63 	close(fd);
64 	return rc;
65 }
66 
67 #define	GETETHER
68 #endif /* ultrix|osf1 */
69 
70 
71 #ifdef	SUNOS
72 
73 #include <sys/sockio.h>
74 #include <sys/time.h>			/* needed by net_if.h */
75 #include <net/nit_if.h>			/* for NIOCBIND */
76 #include <net/if.h>				/* for struct ifreq */
77 
78 /* ifname: interface name from ifconfig structure */
79 /* eap: Ether address (output) */
80 getether(char *ifname, char *eap)
81 {
82 	int rc = -1;
83 
84 	struct ifreq ifrnit;
85 	int nit;
86 
87 	bzero((char *) &ifrnit, sizeof(ifrnit));
88 	strncpy(&ifrnit.ifr_name[0], ifname, IFNAMSIZ);
89 
90 	nit = open("/dev/nit", 0);
91 	if (nit < 0) {
92 		report(LOG_ERR, "getether: open /dev/nit: %s",
93 			   get_errmsg());
94 		return rc;
95 	}
96 	do {
97 		if (ioctl(nit, NIOCBIND, &ifrnit) < 0) {
98 			report(LOG_ERR, "getether: NIOCBIND on nit");
99 			break;
100 		}
101 		if (ioctl(nit, SIOCGIFADDR, &ifrnit) < 0) {
102 			report(LOG_ERR, "getether: SIOCGIFADDR on nit");
103 			break;
104 		}
105 		bcopy(&ifrnit.ifr_addr.sa_data[0], eap, EALEN);
106 		rc = 0;
107 	} while (0);
108 	close(nit);
109 	return rc;
110 }
111 
112 #define	GETETHER
113 #endif /* SUNOS */
114 
115 
116 #if defined(__386BSD__) || defined(__NetBSD__)
117 /* Thanks to John Brezak <brezak@ch.hp.com> for this code. */
118 #include <sys/ioctl.h>
119 #include <net/if.h>
120 #include <net/if_dl.h>
121 #include <net/if_types.h>
122 
123 /* ifname: interface name from ifconfig structure */
124 /* eap: Ether address (output) */
125 int
126 getether(char *ifname, char *eap)
127 {
128 	int fd, rc = -1;
129 	int n;
130 	struct ifreq ibuf[16];
131 	struct ifconf ifc;
132 	struct ifreq *ifrp, *ifend;
133 
134 	/* Fetch the interface configuration */
135 	fd = socket(AF_INET, SOCK_DGRAM, 0);
136 	if (fd < 0) {
137 		report(LOG_ERR, "getether: socket %s: %s", ifname, get_errmsg());
138 		return (fd);
139 	}
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());
145 		goto out;
146 	}
147 	/* Search interface configuration list for link layer address. */
148 	ifrp = ibuf;
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);
156 			rc = 0;
157 			break;
158 		}
159 		/* Bump interface config pointer */
160 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
161 		if (n < sizeof(*ifrp))
162 			n = sizeof(*ifrp);
163 		ifrp = (struct ifreq *) ((char *) ifrp + n);
164 	}
165 
166   out:
167 	close(fd);
168 	return (rc);
169 }
170 
171 #define	GETETHER
172 #endif /* __NetBSD__ */
173 
174 
175 #ifdef	SVR4
176 /*
177  * This is for "Streams TCP/IP" by Lachman Associates.
178  * They sure made this cumbersome!  -gwr
179  */
180 
181 #include <sys/sockio.h>
182 #include <sys/dlpi.h>
183 #include <stropts.h>
184 #ifndef NULL
185 #define NULL 0
186 #endif
187 
188 /* ifname: interface name from ifconfig structure */
189 /* eap: Ether address (output) */
190 getether(char *ifname, char *eap)
191 {
192 	int rc = -1;
193 	char devname[32];
194 	char tmpbuf[sizeof(union DL_primitives) + 16];
195 	struct strbuf cbuf;
196 	int fd, flags;
197 	union DL_primitives *dlp;
198 	char *enaddr;
199 	int unit = -1;				/* which unit to attach */
200 
201 	snprintf(devname, sizeof(devname), "/dev/%s", ifname);
202 	fd = open(devname, 2);
203 	if (fd < 0) {
204 		/* Try without the trailing digit. */
205 		char *p = devname + 5;
206 		while (isalpha(*p))
207 			p++;
208 		if (isdigit(*p)) {
209 			unit = *p - '0';
210 			*p = '\0';
211 		}
212 		fd = open(devname, 2);
213 		if (fd < 0) {
214 			report(LOG_ERR, "getether: open %s: %s",
215 				   devname, get_errmsg());
216 			return rc;
217 		}
218 	}
219 #ifdef	DL_ATTACH_REQ
220 	/*
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 ...)
225 	 */
226 	if (unit >= 0) {
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;
231 		cbuf.buf = tmpbuf;
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());
235 			goto out;
236 		}
237 		/* Recv the ack. */
238 		cbuf.buf = tmpbuf;
239 		cbuf.maxlen = sizeof(tmpbuf);
240 		flags = 0;
241 		if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
242 			report(LOG_ERR, "getether: attach: getmsg: %s", get_errmsg());
243 			goto out;
244 		}
245 		/*
246 		 * Check the type, etc.
247 		 */
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);
252 			goto out;
253 		}
254 		if (dlp->dl_primitive != DL_OK_ACK) {
255 			report(LOG_ERR, "getether: attach: not OK or ERROR");
256 			goto out;
257 		}
258 	} /* unit >= 0 */
259 #endif	/* DL_ATTACH_REQ */
260 
261 	/*
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 an 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).
268 	 */
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 */
273 	cbuf.buf = tmpbuf;
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());
277 		goto out;
278 	}
279 	/* Recv the ack. */
280 	cbuf.buf = tmpbuf;
281 	cbuf.maxlen = sizeof(tmpbuf);
282 	flags = 0;
283 	if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
284 		report(LOG_ERR, "getether: bind: getmsg: %s", get_errmsg());
285 		goto out;
286 	}
287 	/*
288 	 * Check the type, etc.
289 	 */
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);
294 		goto out;
295 	}
296 	if (dlp->dl_primitive != DL_BIND_ACK) {
297 		report(LOG_ERR, "getether: bind: not OK or ERROR");
298 		goto out;
299 	}
300 	if (dlp->bind_ack.dl_addr_offset == 0) {
301 		report(LOG_ERR, "getether: bind: ack has no address");
302 		goto out;
303 	}
304 	if (dlp->bind_ack.dl_addr_length < EALEN) {
305 		report(LOG_ERR, "getether: bind: ack address truncated");
306 		goto out;
307 	}
308 	/*
309 	 * Copy the Ethernet address out of the message.
310 	 */
311 	enaddr = tmpbuf + dlp->bind_ack.dl_addr_offset;
312 	memcpy(eap, enaddr, EALEN);
313 	rc = 0;
314 
315   out:
316 	close(fd);
317 	return rc;
318 }
319 
320 #define	GETETHER
321 #endif /* SVR4 */
322 
323 
324 #ifdef	linux
325 /*
326  * This is really easy on Linux!  This version (for linux)
327  * written by Nigel Metheringham <nigelm@ohm.york.ac.uk>
328  *
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.
332  */
333 
334 #include <sys/ioctl.h>
335 #include <net/if.h>	       	/* struct ifreq */
336 
337 /* In a properly configured system this should be either sys/socketio.h
338    or sys/sockios.h, but on my distribution these don't line up correctly */
339 #include <linux/sockios.h>	/* Needed for IOCTL defs */
340 
341 getether(char *ifname, char *eap)
342 {
343 	int rc = -1;
344 	int fd;
345 	struct ifreq phys;
346 
347 	bzero(&phys, sizeof(phys));
348 	strncpy(phys.ifr_name, ifname, sizeof(phys.ifr_name));
349 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
350 		report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
351 		return -1;
352 	}
353 	if (ioctl(fd, SIOCGIFHWADDR, &phys) < 0) {
354 		report(LOG_ERR, "getether: ioctl SIOCGIFHWADDR failed");
355 	} else {
356 		bcopy(phys.ifr_hwaddr, eap, EALEN);
357 		rc = 0;
358 	}
359 	close(fd);
360 	return rc;
361 }
362 
363 #define	GETETHER
364 #endif	/* linux */
365 
366 
367 /* If we don't know how on this system, just return an error. */
368 #ifndef	GETETHER
369 getether(char *ifname, char *eap)
370 {
371 	return -1;
372 }
373 
374 #endif /* !GETETHER */
375 
376 /*
377  * Local Variables:
378  * tab-width: 4
379  * c-indent-level: 4
380  * c-argdecl-indent: 4
381  * c-continued-statement-offset: 4
382  * c-continued-brace-offset: -4
383  * c-label-offset: -4
384  * c-brace-offset: 0
385  * End:
386  */
387