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