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