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