xref: /netbsd-src/usr.sbin/bootp/bootptest/getether.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: getether.c,v 1.4 2000/10/11 20:23:49 is Exp $	*/
2 
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: getether.c,v 1.4 2000/10/11 20:23:49 is 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 #ifdef	__STDC__
30 #define P(args) args
31 #else
32 #define P(args) ()
33 #endif
34 
35 extern int getether P((char *, char *));
36 
37 #undef P
38 
39 #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
40 /*
41  * This is really easy on Ultrix!  Thanks to
42  * Harald Lundberg <hl@tekla.fi> for this code.
43  *
44  * The code here is not specific to the Alpha, but that was the
45  * only symbol we could find to identify DEC's version of OSF.
46  * (Perhaps we should just define DEC in the Makefile... -gwr)
47  */
48 
49 #include <sys/ioctl.h>
50 #include <net/if.h>				/* struct ifdevea */
51 
52 int
53 getether(ifname, eap)
54 	char *ifname, *eap;
55 {
56 	int rc = -1;
57 	int fd;
58 	struct ifdevea phys;
59 	bzero(&phys, sizeof(phys));
60 	strcpy(phys.ifr_name, ifname);
61 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
62 		report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
63 		return -1;
64 	}
65 	if (ioctl(fd, SIOCRPHYSADDR, &phys) < 0) {
66 		report(LOG_ERR, "getether: ioctl SIOCRPHYSADDR failed");
67 	} else {
68 		bcopy(&phys.current_pa[0], eap, EALEN);
69 		rc = 0;
70 	}
71 	close(fd);
72 	return rc;
73 }
74 
75 #define	GETETHER
76 #endif /* ultrix|osf1 */
77 
78 
79 #ifdef	SUNOS
80 
81 #include <sys/sockio.h>
82 #include <sys/time.h>			/* needed by net_if.h */
83 #include <net/nit_if.h>			/* for NIOCBIND */
84 #include <net/if.h>				/* for struct ifreq */
85 
86 getether(ifname, eap)
87 	char *ifname;				/* interface name from ifconfig structure */
88 	char *eap;					/* Ether address (output) */
89 {
90 	int rc = -1;
91 
92 	struct ifreq ifrnit;
93 	int nit;
94 
95 	bzero((char *) &ifrnit, sizeof(ifrnit));
96 	strncpy(&ifrnit.ifr_name[0], ifname, IFNAMSIZ);
97 
98 	nit = open("/dev/nit", 0);
99 	if (nit < 0) {
100 		report(LOG_ERR, "getether: open /dev/nit: %s",
101 			   get_errmsg());
102 		return rc;
103 	}
104 	do {
105 		if (ioctl(nit, NIOCBIND, &ifrnit) < 0) {
106 			report(LOG_ERR, "getether: NIOCBIND on nit");
107 			break;
108 		}
109 		if (ioctl(nit, SIOCGIFADDR, &ifrnit) < 0) {
110 			report(LOG_ERR, "getether: SIOCGIFADDR on nit");
111 			break;
112 		}
113 		bcopy(&ifrnit.ifr_addr.sa_data[0], eap, EALEN);
114 		rc = 0;
115 	} while (0);
116 	close(nit);
117 	return rc;
118 }
119 
120 #define	GETETHER
121 #endif /* SUNOS */
122 
123 
124 #if defined(__386BSD__) || defined(__NetBSD__)
125 /* Thanks to John Brezak <brezak@ch.hp.com> for this code. */
126 #include <sys/ioctl.h>
127 #include <net/if.h>
128 #include <net/if_dl.h>
129 #include <net/if_types.h>
130 
131 int
132 getether(ifname, eap)
133 	char *ifname;				/* interface name from ifconfig structure */
134 	char *eap;					/* Ether address (output) */
135 {
136 	int fd, rc = -1;
137 	register int n;
138 	struct ifreq ibuf[16];
139 	struct ifconf ifc;
140 	register struct ifreq *ifrp, *ifend;
141 
142 	/* Fetch the interface configuration */
143 	fd = socket(AF_INET, SOCK_DGRAM, 0);
144 	if (fd < 0) {
145 		report(LOG_ERR, "getether: socket %s: %s", ifname, get_errmsg());
146 		return (fd);
147 	}
148 	ifc.ifc_len = sizeof(ibuf);
149 	ifc.ifc_buf = (caddr_t) ibuf;
150 	if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0 ||
151 		ifc.ifc_len < sizeof(struct ifreq)) {
152 		report(LOG_ERR, "getether: SIOCGIFCONF: %s", get_errmsg());
153 		goto out;
154 	}
155 	/* Search interface configuration list for link layer address. */
156 	ifrp = ibuf;
157 	ifend = (struct ifreq *) ((char *) ibuf + ifc.ifc_len);
158 	while (ifrp < ifend) {
159 		/* Look for interface */
160 		if (strcmp(ifname, ifrp->ifr_name) == 0 &&
161 			ifrp->ifr_addr.sa_family == AF_LINK &&
162 		((struct sockaddr_dl *) &ifrp->ifr_addr)->sdl_type == IFT_ETHER) {
163 			bcopy(LLADDR((struct sockaddr_dl *) &ifrp->ifr_addr), eap, EALEN);
164 			rc = 0;
165 			break;
166 		}
167 		/* Bump interface config pointer */
168 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
169 		if (n < sizeof(*ifrp))
170 			n = sizeof(*ifrp);
171 		ifrp = (struct ifreq *) ((char *) ifrp + n);
172 	}
173 
174   out:
175 	close(fd);
176 	return (rc);
177 }
178 
179 #define	GETETHER
180 #endif /* __NetBSD__ */
181 
182 
183 #ifdef	SVR4
184 /*
185  * This is for "Streams TCP/IP" by Lachman Associates.
186  * They sure made this cumbersome!  -gwr
187  */
188 
189 #include <sys/sockio.h>
190 #include <sys/dlpi.h>
191 #include <stropts.h>
192 #ifndef NULL
193 #define NULL 0
194 #endif
195 
196 getether(ifname, eap)
197 	char *ifname;				/* interface name from ifconfig structure */
198 	char *eap;					/* Ether address (output) */
199 {
200 	int rc = -1;
201 	char devname[32];
202 	char tmpbuf[sizeof(union DL_primitives) + 16];
203 	struct strbuf cbuf;
204 	int fd, flags;
205 	union DL_primitives *dlp;
206 	char *enaddr;
207 	int unit = -1;				/* which unit to attach */
208 
209 	sprintf(devname, "/dev/%s", ifname);
210 	fd = open(devname, 2);
211 	if (fd < 0) {
212 		/* Try without the trailing digit. */
213 		char *p = devname + 5;
214 		while (isalpha(*p))
215 			p++;
216 		if (isdigit(*p)) {
217 			unit = *p - '0';
218 			*p = '\0';
219 		}
220 		fd = open(devname, 2);
221 		if (fd < 0) {
222 			report(LOG_ERR, "getether: open %s: %s",
223 				   devname, get_errmsg());
224 			return rc;
225 		}
226 	}
227 #ifdef	DL_ATTACH_REQ
228 	/*
229 	 * If this is a "Style 2" DLPI, then we must "attach" first
230 	 * to tell the driver which unit (board, port) we want.
231 	 * For now, decide this based on the device name.
232 	 * (Should do "info_req" and check dl_provider_style ...)
233 	 */
234 	if (unit >= 0) {
235 		memset(tmpbuf, 0, sizeof(tmpbuf));
236 		dlp = (union DL_primitives *) tmpbuf;
237 		dlp->dl_primitive = DL_ATTACH_REQ;
238 		dlp->attach_req.dl_ppa = unit;
239 		cbuf.buf = tmpbuf;
240 		cbuf.len = DL_ATTACH_REQ_SIZE;
241 		if (putmsg(fd, &cbuf, NULL, 0) < 0) {
242 			report(LOG_ERR, "getether: attach: putmsg: %s", get_errmsg());
243 			goto out;
244 		}
245 		/* Recv the ack. */
246 		cbuf.buf = tmpbuf;
247 		cbuf.maxlen = sizeof(tmpbuf);
248 		flags = 0;
249 		if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
250 			report(LOG_ERR, "getether: attach: getmsg: %s", get_errmsg());
251 			goto out;
252 		}
253 		/*
254 		 * Check the type, etc.
255 		 */
256 		if (dlp->dl_primitive == DL_ERROR_ACK) {
257 			report(LOG_ERR, "getether: attach: dlpi_errno=%d, unix_errno=%d",
258 				   dlp->error_ack.dl_errno,
259 				   dlp->error_ack.dl_unix_errno);
260 			goto out;
261 		}
262 		if (dlp->dl_primitive != DL_OK_ACK) {
263 			report(LOG_ERR, "getether: attach: not OK or ERROR");
264 			goto out;
265 		}
266 	} /* unit >= 0 */
267 #endif	/* DL_ATTACH_REQ */
268 
269 	/*
270 	 * Get the Ethernet address the same way the ARP module
271 	 * does when it is pushed onto a new stream (bind).
272 	 * One should instead be able just do an dl_info_req
273 	 * but many drivers do not supply the hardware address
274 	 * in the response to dl_info_req (they MUST supply it
275 	 * for dl_bind_ack because the ARP module requires it).
276 	 */
277 	memset(tmpbuf, 0, sizeof(tmpbuf));
278 	dlp = (union DL_primitives *) tmpbuf;
279 	dlp->dl_primitive = DL_BIND_REQ;
280 	dlp->bind_req.dl_sap = 0x8FF;	/* XXX - Unused SAP */
281 	cbuf.buf = tmpbuf;
282 	cbuf.len = DL_BIND_REQ_SIZE;
283 	if (putmsg(fd, &cbuf, NULL, 0) < 0) {
284 		report(LOG_ERR, "getether: bind: putmsg: %s", get_errmsg());
285 		goto out;
286 	}
287 	/* Recv the ack. */
288 	cbuf.buf = tmpbuf;
289 	cbuf.maxlen = sizeof(tmpbuf);
290 	flags = 0;
291 	if (getmsg(fd, &cbuf, NULL, &flags) < 0) {
292 		report(LOG_ERR, "getether: bind: getmsg: %s", get_errmsg());
293 		goto out;
294 	}
295 	/*
296 	 * Check the type, etc.
297 	 */
298 	if (dlp->dl_primitive == DL_ERROR_ACK) {
299 		report(LOG_ERR, "getether: bind: dlpi_errno=%d, unix_errno=%d",
300 			   dlp->error_ack.dl_errno,
301 			   dlp->error_ack.dl_unix_errno);
302 		goto out;
303 	}
304 	if (dlp->dl_primitive != DL_BIND_ACK) {
305 		report(LOG_ERR, "getether: bind: not OK or ERROR");
306 		goto out;
307 	}
308 	if (dlp->bind_ack.dl_addr_offset == 0) {
309 		report(LOG_ERR, "getether: bind: ack has no address");
310 		goto out;
311 	}
312 	if (dlp->bind_ack.dl_addr_length < EALEN) {
313 		report(LOG_ERR, "getether: bind: ack address truncated");
314 		goto out;
315 	}
316 	/*
317 	 * Copy the Ethernet address out of the message.
318 	 */
319 	enaddr = tmpbuf + dlp->bind_ack.dl_addr_offset;
320 	memcpy(eap, enaddr, EALEN);
321 	rc = 0;
322 
323   out:
324 	close(fd);
325 	return rc;
326 }
327 
328 #define	GETETHER
329 #endif /* SVR4 */
330 
331 
332 #ifdef	linux
333 /*
334  * This is really easy on Linux!  This version (for linux)
335  * written by Nigel Metheringham <nigelm@ohm.york.ac.uk>
336  *
337  * The code is almost identical to the Ultrix code - however
338  * the names are different to confuse the innocent :-)
339  * Most of this code was stolen from the Ultrix bit above.
340  */
341 
342 #include <sys/ioctl.h>
343 #include <net/if.h>	       	/* struct ifreq */
344 
345 /* In a properly configured system this should be either sys/socketio.h
346    or sys/sockios.h, but on my distribution these don't line up correctly */
347 #include <linux/sockios.h>	/* Needed for IOCTL defs */
348 
349 getether(ifname, eap)
350 	char *ifname, *eap;
351 {
352 	int rc = -1;
353 	int fd;
354 	struct ifreq phys;
355 	bzero(&phys, sizeof(phys));
356 	strcpy(phys.ifr_name, ifname);
357 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
358 		report(LOG_ERR, "getether: socket(INET,DGRAM) failed");
359 		return -1;
360 	}
361 	if (ioctl(fd, SIOCGIFHWADDR, &phys) < 0) {
362 		report(LOG_ERR, "getether: ioctl SIOCGIFHWADDR failed");
363 	} else {
364 		bcopy(phys.ifr_hwaddr, eap, EALEN);
365 		rc = 0;
366 	}
367 	close(fd);
368 	return rc;
369 }
370 
371 #define	GETETHER
372 #endif	/* linux */
373 
374 
375 /* If we don't know how on this system, just return an error. */
376 #ifndef	GETETHER
377 getether(ifname, eap)
378 	char *ifname, *eap;
379 {
380 	return -1;
381 }
382 
383 #endif /* !GETETHER */
384 
385 /*
386  * Local Variables:
387  * tab-width: 4
388  * c-indent-level: 4
389  * c-argdecl-indent: 4
390  * c-continued-statement-offset: 4
391  * c-continued-brace-offset: -4
392  * c-label-offset: -4
393  * c-brace-offset: 0
394  * End:
395  */
396