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