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