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
5948f2876Sss150715 * Common Development and Distribution License (the "License").
6948f2876Sss150715 * 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*d62bc4baSyz147064 * Copyright 2008 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>
35948f2876Sss150715 #include <libdlpi.h>
367c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include <netdb.h>
397c478bd9Sstevel@tonic-gate #include <netinet/in.h>
407c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
417c478bd9Sstevel@tonic-gate
42948f2876Sss150715 #include "etheraddr.h"
43948f2876Sss150715
44*d62bc4baSyz147064 static boolean_t get_etheraddr(const char *linkname, void *arg);
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * get an individual arp entry
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate int
arp_get(uuid_node_t * node)507c478bd9Sstevel@tonic-gate arp_get(uuid_node_t *node)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate struct utsname name;
537c478bd9Sstevel@tonic-gate struct arpreq ar;
547c478bd9Sstevel@tonic-gate struct hostent *hp;
557c478bd9Sstevel@tonic-gate struct sockaddr_in *sin;
567c478bd9Sstevel@tonic-gate int s;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate if (uname(&name) == -1) {
597c478bd9Sstevel@tonic-gate return (-1);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate (void) memset(&ar, 0, sizeof (ar));
627c478bd9Sstevel@tonic-gate ar.arp_pa.sa_family = AF_INET;
637c478bd9Sstevel@tonic-gate /* LINTED pointer */
647c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&ar.arp_pa;
657c478bd9Sstevel@tonic-gate sin->sin_family = AF_INET;
667c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr = inet_addr(name.nodename);
677c478bd9Sstevel@tonic-gate if (sin->sin_addr.s_addr == (in_addr_t)-1) {
687c478bd9Sstevel@tonic-gate hp = gethostbyname(name.nodename);
697c478bd9Sstevel@tonic-gate if (hp == NULL) {
707c478bd9Sstevel@tonic-gate return (-1);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate (void) memcpy(&sin->sin_addr, hp->h_addr,
737c478bd9Sstevel@tonic-gate sizeof (sin->sin_addr));
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate s = socket(AF_INET, SOCK_DGRAM, 0);
767c478bd9Sstevel@tonic-gate if (s < 0) {
777c478bd9Sstevel@tonic-gate return (-1);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGARP, (caddr_t)&ar) < 0) {
807c478bd9Sstevel@tonic-gate (void) close(s);
817c478bd9Sstevel@tonic-gate return (-1);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate (void) close(s);
847c478bd9Sstevel@tonic-gate if (ar.arp_flags & ATF_COM) {
857c478bd9Sstevel@tonic-gate bcopy(&ar.arp_ha.sa_data, node, 6);
867c478bd9Sstevel@tonic-gate } else
877c478bd9Sstevel@tonic-gate return (-1);
887c478bd9Sstevel@tonic-gate return (0);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * Name: get_ethernet_address
937c478bd9Sstevel@tonic-gate *
947c478bd9Sstevel@tonic-gate * Description: Obtains the system ethernet address.
957c478bd9Sstevel@tonic-gate *
967c478bd9Sstevel@tonic-gate * Returns: 0 on success, non-zero otherwise. The system ethernet
977c478bd9Sstevel@tonic-gate * address is copied into the passed-in variable.
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate int
get_ethernet_address(uuid_node_t * node)1007c478bd9Sstevel@tonic-gate get_ethernet_address(uuid_node_t *node)
1017c478bd9Sstevel@tonic-gate {
102948f2876Sss150715 walker_arg_t state;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate if (arp_get(node) == 0)
1057c478bd9Sstevel@tonic-gate return (0);
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*
108948f2876Sss150715 * Try to get physical (ethernet) address from network interfaces.
1097c478bd9Sstevel@tonic-gate */
110948f2876Sss150715 state.wa_addrvalid = B_FALSE;
111*d62bc4baSyz147064 dlpi_walk(get_etheraddr, &state, 0);
112*d62bc4baSyz147064 if (state.wa_addrvalid)
113948f2876Sss150715 bcopy(state.wa_etheraddr, node, state.wa_etheraddrlen);
114948f2876Sss150715
115948f2876Sss150715 return (state.wa_addrvalid ? 0 : -1);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /*
119*d62bc4baSyz147064 * Get the physical address via DLPI and update the flag to true upon success.
1207c478bd9Sstevel@tonic-gate */
121*d62bc4baSyz147064 static boolean_t
get_etheraddr(const char * linkname,void * arg)122*d62bc4baSyz147064 get_etheraddr(const char *linkname, void *arg)
123948f2876Sss150715 {
124948f2876Sss150715 int retval;
125948f2876Sss150715 dlpi_handle_t dh;
126948f2876Sss150715 walker_arg_t *statep = arg;
1277c478bd9Sstevel@tonic-gate
128*d62bc4baSyz147064 if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
129*d62bc4baSyz147064 return (B_FALSE);
1307c478bd9Sstevel@tonic-gate
131948f2876Sss150715 statep->wa_etheraddrlen = DLPI_PHYSADDR_MAX;
132948f2876Sss150715 retval = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR,
133948f2876Sss150715 statep->wa_etheraddr, &(statep->wa_etheraddrlen));
134948f2876Sss150715
135948f2876Sss150715 dlpi_close(dh);
136*d62bc4baSyz147064
137*d62bc4baSyz147064 if (retval == DLPI_SUCCESS) {
138*d62bc4baSyz147064 statep->wa_addrvalid = B_TRUE;
139*d62bc4baSyz147064 return (B_TRUE);
140948f2876Sss150715 }
141*d62bc4baSyz147064 return (B_FALSE);
1427c478bd9Sstevel@tonic-gate }
143