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