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