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