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