xref: /titanic_50/usr/src/lib/libsocket/inet/ether_addr.c (revision 97dfde0e576e5aaee47d98d1d8098fe4ad188e3b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 /*
32  * University Copyright- Copyright (c) 1982, 1986, 1988
33  * The Regents of the University of California
34  * All Rights Reserved
35  *
36  * University Acknowledgment- Portions of this document are derived from
37  * software developed by the University of California, Berkeley, and its
38  * contributors.
39  */
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 /*
44  * All routines necessary to deal the "ethers" database.  The sources
45  * contain mappings between 48 bit ethernet addresses and corresponding
46  * hosts names.  The addresses have an ascii representation of the form
47  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
48  * bytes are always in network order.
49  */
50 
51 #include "c_synonyms.h"
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include <sys/types.h>
57 #include <thread.h>
58 #include <sys/socket.h>
59 #include <net/if.h>
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
62 #include <nss_dbdefs.h>
63 
64 static int str2ether(const char *, int, void *, char *, int);
65 
66 static DEFINE_NSS_DB_ROOT(db_root);
67 
68 static void
69 _nss_initf_ethers(nss_db_params_t *p)
70 {
71 	p->name = NSS_DBNAM_ETHERS;
72 	p->default_config = NSS_DEFCONF_ETHERS;
73 }
74 
75 /*
76  * Given a host's name, this routine finds the corresponding 48 bit
77  * ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
78  * Returns zero if successful, non-zero otherwise.
79  */
80 int
81 ether_hostton(
82 	const char *host,		/* function input */
83 	struct ether_addr *e		/* function output */
84 )
85 {
86 	nss_XbyY_args_t arg;
87 	nss_status_t	res;
88 
89 	/*
90 	 * let the backend do the allocation to store stuff for parsing.
91 	 */
92 	NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether);
93 	arg.key.name = host;
94 	res = nss_search(&db_root, _nss_initf_ethers,
95 			NSS_DBOP_ETHERS_HOSTTON, &arg);
96 	(void) NSS_XbyY_FINI(&arg);
97 	return (arg.status = res);
98 }
99 
100 /*
101  * Given a 48 bit ethernet address, it finds the corresponding hostname
102  * ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
103  * Returns zero if successful, non-zero otherwise.
104  */
105 int
106 ether_ntohost(
107 	char *host,			/* function output */
108 	const struct ether_addr *e	/* function input */
109 )
110 {
111 	nss_XbyY_args_t arg;
112 	nss_status_t	res;
113 
114 	/*
115 	 * let the backend do the allocation to store stuff for parsing.
116 	 */
117 	NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether);
118 	arg.key.ether = (void *)e;
119 	res = nss_search(&db_root, _nss_initf_ethers,
120 			NSS_DBOP_ETHERS_NTOHOST, &arg);
121 	/* memcpy(host, ether_res.host, strlen(ether_res.host)); */
122 	(void) NSS_XbyY_FINI(&arg);
123 	return (arg.status = res);
124 }
125 
126 /*
127  * Parses a line from "ethers" database into its components.  The line has
128  * the form 8:0:20:1:17:c8	krypton
129  * where the first part is a 48 bit ethernet address and the second is
130  * the corresponding hosts name.
131  * Returns zero if successful, non-zero otherwise.
132  */
133 int
134 ether_line(
135 	const char *s,		/* the string to be parsed */
136 	struct ether_addr *e,	/* ethernet address struct to be filled in */
137 	char *hostname		/* hosts name to be set */
138 )
139 {
140 	int i;
141 	uint_t t[6];
142 
143 	i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
144 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
145 	if (i != 7) {
146 		return (7 - i);
147 	}
148 	for (i = 0; i < 6; i++)
149 		e->ether_addr_octet[i] = (uchar_t)t[i];
150 	return (0);
151 }
152 
153 /*
154  * Parses a line from "ethers" database into its components.
155  * Useful for the vile purposes of the backends that
156  * expect a str2ether() format.
157  *
158  * This function, after parsing the instr line, will
159  * place the resulting struct ether_addr in b->buf.result only if
160  * b->buf.result is initialized (not NULL). I.e. it always happens
161  * for "files" backend (that needs to parse input line and
162  * then do a match for the ether key) and happens for "nis"
163  * backend only if the call was ether_hostton.
164  *
165  * Also, it will place the resulting hostname into b->buf.buffer
166  * only if b->buf.buffer is initialized. I.e. it always happens
167  * for "files" backend (that needs to parse input line and
168  * then do a match for the host key) and happens for "nis"
169  * backend only if the call was ether_ntohost.
170  *
171  * Cannot use the sscanf() technique for parsing because instr
172  * is a read-only, not necessarily null-terminated, buffer.
173  *
174  * Return values: 0 = success, 1 = parse error, 2 = erange ...
175  * The structure pointer passed in is a structure in the caller's space
176  * wherein the field pointers would be set to areas in the buffer if
177  * need be. instring and buffer should be separate areas.
178  */
179 #define	DIGIT(x)	(isdigit(x) ? (x) - '0' : \
180 		islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
181 #define	lisalnum(x)	(isdigit(x) || \
182 		((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
183 /* ARGSUSED */
184 static int
185 str2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
186 {
187 	uchar_t	*ether =  (uchar_t *)ent;
188 	char	*host = buffer;
189 	const char	*p, *limit, *start;
190 	ptrdiff_t i;
191 
192 	p = instr;
193 	limit = p + lenstr;
194 
195 	/* skip beginning whitespace, if any */
196 	while (p < limit && isspace(*p))
197 		p++;
198 
199 	if (ether) {	/* parse ether */
200 		for (i = 0; i < 6; i++) {
201 			int	j = 0, n = 0;
202 
203 			start = p;
204 			while (p < limit && lisalnum(start[j])) {
205 				/* don't worry about overflow here */
206 				n = 16 * n + DIGIT(start[j]);
207 				j++;
208 				p++;
209 			}
210 			if (*p != ':' && i < 5) {
211 				return (NSS_STR_PARSE_PARSE);
212 			} else {
213 				p++;
214 				*(ether + i) = (uchar_t)n;
215 			}
216 		}
217 	} else {	/* skip ether */
218 		while (p < limit && !isspace(*p))
219 			p++;
220 	}
221 	if (host) {	/* parse host */
222 		while (p < limit && isspace(*p))	/* skip whitespace */
223 			p++;
224 		start = p;
225 		while (p < limit && !isspace(*p))	/* skip hostname */
226 			p++;
227 		if ((i = (p - start)) < MAXHOSTNAMELEN) {
228 			(void) memcpy(host, start, i);
229 			host[i] = '\0';
230 		} else
231 			return (NSS_STR_PARSE_ERANGE); /* failure */
232 	}
233 	return (NSS_STR_PARSE_SUCCESS);
234 }
235 
236 typedef struct {
237 	char			ea_string[18];
238 	struct ether_addr	ea_addr;
239 } eabuf_t;
240 
241 static eabuf_t *
242 ea_buf(void)
243 {
244 	static thread_key_t key;
245 	static int key_once = 0;
246 	static mutex_t tsd_lock = DEFAULTMUTEX;
247 	static eabuf_t ea_main;
248 	eabuf_t *eabuf = NULL;
249 
250 	if (thr_main())
251 		return (&ea_main);
252 
253 	if (key_once == 0) {
254 		(void) mutex_lock(&tsd_lock);
255 		if (key_once == 0) {
256 			if (thr_keycreate(&key, free) != 0) {
257 				(void) mutex_unlock(&tsd_lock);
258 				return (NULL);
259 			}
260 			key_once = 1;
261 		}
262 		(void) mutex_unlock(&tsd_lock);
263 	}
264 	(void) thr_getspecific(key, (void **)&eabuf);
265 	if (eabuf == NULL) {
266 		eabuf = malloc(sizeof (eabuf_t));
267 		(void) thr_setspecific(key, eabuf);
268 	}
269 	return (eabuf);
270 }
271 
272 /*
273  * Converts a 48 bit ethernet number to its string representation.
274  */
275 char *
276 ether_ntoa(const struct ether_addr *e)
277 {
278 	eabuf_t *eabuf;
279 	char *s;
280 
281 	if ((eabuf = ea_buf()) == NULL)
282 		return (NULL);
283 	s = eabuf->ea_string;
284 	(void) sprintf(s, "%x:%x:%x:%x:%x:%x",
285 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
286 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
287 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
288 	return (s);
289 }
290 
291 /*
292  * Converts an ethernet address representation back into its 48 bits.
293  */
294 struct ether_addr *
295 ether_aton(const char *s)
296 {
297 	eabuf_t *eabuf;
298 	struct ether_addr *e;
299 	int i;
300 	uint_t t[6];
301 
302 	if ((eabuf = ea_buf()) == NULL)
303 		return (NULL);
304 	e = &eabuf->ea_addr;
305 	i = sscanf(s, " %x:%x:%x:%x:%x:%x",
306 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
307 	if (i != 6)
308 	    return (NULL);
309 	for (i = 0; i < 6; i++)
310 		e->ether_addr_octet[i] = (uchar_t)t[i];
311 	return (e);
312 }
313