17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*948f2876Sss150715 * Common Development and Distribution License (the "License"). 6*948f2876Sss150715 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*948f2876Sss150715 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <stdlib.h> 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <strings.h> 317c478bd9Sstevel@tonic-gate #include <stropts.h> 327c478bd9Sstevel@tonic-gate #include <unistd.h> 337c478bd9Sstevel@tonic-gate #include <uuid/uuid.h> 347c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 35*948f2876Sss150715 #include <libdlpi.h> 36*948f2876Sss150715 #include <libdllink.h> 377c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #include <netdb.h> 407c478bd9Sstevel@tonic-gate #include <netinet/in.h> 417c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 427c478bd9Sstevel@tonic-gate 43*948f2876Sss150715 #include "etheraddr.h" 44*948f2876Sss150715 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * debugging flag 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate static int debug = 0; 497c478bd9Sstevel@tonic-gate 50*948f2876Sss150715 static void get_etheraddr(void *arg, const char *linkname); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * get an individual arp entry 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate int 567c478bd9Sstevel@tonic-gate arp_get(uuid_node_t *node) 577c478bd9Sstevel@tonic-gate { 587c478bd9Sstevel@tonic-gate struct utsname name; 597c478bd9Sstevel@tonic-gate struct arpreq ar; 607c478bd9Sstevel@tonic-gate struct hostent *hp; 617c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 627c478bd9Sstevel@tonic-gate int s; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate if (uname(&name) == -1) { 657c478bd9Sstevel@tonic-gate return (-1); 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate (void) memset(&ar, 0, sizeof (ar)); 687c478bd9Sstevel@tonic-gate ar.arp_pa.sa_family = AF_INET; 697c478bd9Sstevel@tonic-gate /* LINTED pointer */ 707c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&ar.arp_pa; 717c478bd9Sstevel@tonic-gate sin->sin_family = AF_INET; 727c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr = inet_addr(name.nodename); 737c478bd9Sstevel@tonic-gate if (sin->sin_addr.s_addr == (in_addr_t)-1) { 747c478bd9Sstevel@tonic-gate hp = gethostbyname(name.nodename); 757c478bd9Sstevel@tonic-gate if (hp == NULL) { 767c478bd9Sstevel@tonic-gate return (-1); 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate (void) memcpy(&sin->sin_addr, hp->h_addr, 797c478bd9Sstevel@tonic-gate sizeof (sin->sin_addr)); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate s = socket(AF_INET, SOCK_DGRAM, 0); 827c478bd9Sstevel@tonic-gate if (s < 0) { 837c478bd9Sstevel@tonic-gate return (-1); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGARP, (caddr_t)&ar) < 0) { 867c478bd9Sstevel@tonic-gate (void) close(s); 877c478bd9Sstevel@tonic-gate return (-1); 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate (void) close(s); 907c478bd9Sstevel@tonic-gate if (ar.arp_flags & ATF_COM) { 917c478bd9Sstevel@tonic-gate bcopy(&ar.arp_ha.sa_data, node, 6); 927c478bd9Sstevel@tonic-gate } else 937c478bd9Sstevel@tonic-gate return (-1); 947c478bd9Sstevel@tonic-gate return (0); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * Name: get_ethernet_address 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * Description: Obtains the system ethernet address. 1017c478bd9Sstevel@tonic-gate * 1027c478bd9Sstevel@tonic-gate * Returns: 0 on success, non-zero otherwise. The system ethernet 1037c478bd9Sstevel@tonic-gate * address is copied into the passed-in variable. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate int 1067c478bd9Sstevel@tonic-gate get_ethernet_address(uuid_node_t *node) 1077c478bd9Sstevel@tonic-gate { 108*948f2876Sss150715 walker_arg_t state; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if (arp_get(node) == 0) 1117c478bd9Sstevel@tonic-gate return (0); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* 114*948f2876Sss150715 * Try to get physical (ethernet) address from network interfaces. 1157c478bd9Sstevel@tonic-gate */ 116*948f2876Sss150715 state.wa_addrvalid = B_FALSE; 117*948f2876Sss150715 if (dladm_walk(get_etheraddr, &state) == 0 && state.wa_addrvalid) { 118*948f2876Sss150715 bcopy(state.wa_etheraddr, node, state.wa_etheraddrlen); 119*948f2876Sss150715 } 120*948f2876Sss150715 121*948f2876Sss150715 return (state.wa_addrvalid ? 0 : -1); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 125*948f2876Sss150715 * Get the physical address via dlpi and update the flag to true upon success. 1267c478bd9Sstevel@tonic-gate */ 127*948f2876Sss150715 static void 128*948f2876Sss150715 get_etheraddr(void *arg, const char *linkname) 129*948f2876Sss150715 { 130*948f2876Sss150715 int retval; 131*948f2876Sss150715 dlpi_handle_t dh; 132*948f2876Sss150715 walker_arg_t *statep = arg; 1337c478bd9Sstevel@tonic-gate 134*948f2876Sss150715 if (!(statep->wa_addrvalid)) { 135*948f2876Sss150715 if (debug) 136*948f2876Sss150715 (void) printf("get_etheraddr: opening %s\n", linkname); 137*948f2876Sss150715 if ((retval = dlpi_open(linkname, &dh, 0)) != DLPI_SUCCESS) { 138*948f2876Sss150715 if (debug) { 139*948f2876Sss150715 (void) fprintf(stderr, "get_etheraddr: " 140*948f2876Sss150715 "cannot open link: \"%s\" %s\n", 141*948f2876Sss150715 linkname, retval); 1427c478bd9Sstevel@tonic-gate } 143*948f2876Sss150715 return; 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 146*948f2876Sss150715 if (debug) { 147*948f2876Sss150715 (void) printf("get_etheraddr: getting ethernet address" 148*948f2876Sss150715 " from link: %s\n", linkname); 149*948f2876Sss150715 } 150*948f2876Sss150715 statep->wa_etheraddrlen = DLPI_PHYSADDR_MAX; 151*948f2876Sss150715 retval = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, 152*948f2876Sss150715 statep->wa_etheraddr, &(statep->wa_etheraddrlen)); 153*948f2876Sss150715 if (debug) { 154*948f2876Sss150715 (void) fprintf(stderr, "get_etheraddr: " 155*948f2876Sss150715 "dlpi_get_physaddr: \"%s\" %s\n", linkname, retval); 156*948f2876Sss150715 } 157*948f2876Sss150715 158*948f2876Sss150715 if (retval == DLPI_SUCCESS) 159*948f2876Sss150715 statep->wa_addrvalid = B_TRUE; 160*948f2876Sss150715 161*948f2876Sss150715 dlpi_close(dh); 162*948f2876Sss150715 } 1637c478bd9Sstevel@tonic-gate } 164