xref: /onnv-gate/usr/src/uts/common/io/etheraddr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  * Copyright (c) 1982, 1986 Regents of the University of California.
11*0Sstevel@tonic-gate  * All rights reserved.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
14*0Sstevel@tonic-gate  * provided that this notice is preserved and that due credit is given
15*0Sstevel@tonic-gate  * to the University of California at Berkeley. The name of the University
16*0Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
17*0Sstevel@tonic-gate  * software without specific prior written permission.  This software
18*0Sstevel@tonic-gate  * is provided ``as is'' without express or implied warranty.
19*0Sstevel@tonic-gate  */
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #include <sys/types.h>
24*0Sstevel@tonic-gate #include <sys/sysmacros.h>
25*0Sstevel@tonic-gate #include <sys/ethernet.h>
26*0Sstevel@tonic-gate #include <sys/cmn_err.h>
27*0Sstevel@tonic-gate #include <sys/ksynch.h>
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Store and retrieve local individual ethernet address.
31*0Sstevel@tonic-gate  * This is typically initialized (called with 'hint' nonnull)
32*0Sstevel@tonic-gate  * by the boot code.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate static kmutex_t localetheraddr_lock;   /* Perimeter lock for localetheraddr */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate int
localetheraddr(struct ether_addr * hint,struct ether_addr * result)38*0Sstevel@tonic-gate localetheraddr(struct ether_addr *hint, struct ether_addr *result)
39*0Sstevel@tonic-gate {
40*0Sstevel@tonic-gate 	static int found = 0;
41*0Sstevel@tonic-gate 	static	struct	ether_addr	addr;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	mutex_enter(&localetheraddr_lock);
44*0Sstevel@tonic-gate 	if (!found) {
45*0Sstevel@tonic-gate 		if (hint == NULL) {
46*0Sstevel@tonic-gate 			mutex_exit(&localetheraddr_lock);
47*0Sstevel@tonic-gate 			return (0);
48*0Sstevel@tonic-gate 		}
49*0Sstevel@tonic-gate 		found = 1;
50*0Sstevel@tonic-gate 		addr = *hint;
51*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "?Ethernet address = %s\n",
52*0Sstevel@tonic-gate 		    ether_sprintf(&addr));
53*0Sstevel@tonic-gate 	}
54*0Sstevel@tonic-gate 	if (result != NULL)
55*0Sstevel@tonic-gate 		*result = addr;
56*0Sstevel@tonic-gate 	mutex_exit(&localetheraddr_lock);
57*0Sstevel@tonic-gate 	return (1);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Convert Ethernet address to printable (loggable) representation.
62*0Sstevel@tonic-gate  *
63*0Sstevel@tonic-gate  * XXX This is not MT-safe, but its only race is for the "etherbuf".
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate char *
ether_sprintf(struct ether_addr * addr)66*0Sstevel@tonic-gate ether_sprintf(struct ether_addr *addr)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate 	static char etherbuf[18];
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	(void) snprintf(etherbuf, sizeof (etherbuf), "%x:%x:%x:%x:%x:%x",
71*0Sstevel@tonic-gate 	    addr->ether_addr_octet[0], addr->ether_addr_octet[1],
72*0Sstevel@tonic-gate 	    addr->ether_addr_octet[2], addr->ether_addr_octet[3],
73*0Sstevel@tonic-gate 	    addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
74*0Sstevel@tonic-gate 	return (etherbuf);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static int
hexval(char dig)78*0Sstevel@tonic-gate hexval(char dig)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	if ('0' <= dig && dig <= '9') {
81*0Sstevel@tonic-gate 		return (dig - '0');
82*0Sstevel@tonic-gate 	} else if ('a' <= dig && dig <= 'f') {
83*0Sstevel@tonic-gate 		return (dig - 'a' + 10);
84*0Sstevel@tonic-gate 	} else if ('A' <= dig && dig <= 'F') {
85*0Sstevel@tonic-gate 		return (dig - 'A' + 10);
86*0Sstevel@tonic-gate 	} else {
87*0Sstevel@tonic-gate 		return (-1);
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * Convert Ethernet address from ascii to binary form.
93*0Sstevel@tonic-gate  * Return number of bytes written.
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate int
ether_aton(char * addr,uchar_t * macaddr)96*0Sstevel@tonic-gate ether_aton(char *addr, uchar_t *macaddr)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	int i = 0;
99*0Sstevel@tonic-gate 	uint_t val = 0;
100*0Sstevel@tonic-gate 	char *cp = addr;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	while (*cp != 0 && i < 6) {
103*0Sstevel@tonic-gate 		if (*cp == ':') {
104*0Sstevel@tonic-gate 			macaddr[i++] = val;
105*0Sstevel@tonic-gate 			val = 0;
106*0Sstevel@tonic-gate 			cp++;
107*0Sstevel@tonic-gate 			continue;
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 		val = (val << 4) | hexval(*cp++);
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 	macaddr[i] = val;
113*0Sstevel@tonic-gate 	return (i + 1);
114*0Sstevel@tonic-gate }
115