xref: /freebsd/lib/libc/net/linkaddr.c (revision efedac28c7ce4bef9d9b09b7901f1cb35babc2c4)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 
35 #include <net/if.h>
36 #include <net/if_dl.h>
37 
38 #include <assert.h>
39 #include <stdint.h>
40 #include <string.h>
41 
42 /* States*/
43 #define NAMING	0
44 #define GOTONE	1
45 #define GOTTWO	2
46 #define RESET	3
47 /* Inputs */
48 #define	DIGIT	(4*0)
49 #define	END	(4*1)
50 #define DELIM	(4*2)
51 #define LETTER	(4*3)
52 
53 void
54 link_addr(const char *addr, struct sockaddr_dl *sdl)
55 {
56 	char *cp = sdl->sdl_data;
57 	char *cplim = sdl->sdl_len + (char *)sdl;
58 	int byte = 0, state = NAMING, new;
59 
60 	bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1);
61 	sdl->sdl_family = AF_LINK;
62 	do {
63 		state &= ~LETTER;
64 		if ((*addr >= '0') && (*addr <= '9')) {
65 			new = *addr - '0';
66 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
67 			new = *addr - 'a' + 10;
68 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
69 			new = *addr - 'A' + 10;
70 		} else if (*addr == 0) {
71 			state |= END;
72 		} else if (state == NAMING &&
73 			   (((*addr >= 'A') && (*addr <= 'Z')) ||
74 			   ((*addr >= 'a') && (*addr <= 'z'))))
75 			state |= LETTER;
76 		else
77 			state |= DELIM;
78 		addr++;
79 		switch (state /* | INPUT */) {
80 		case NAMING | DIGIT:
81 		case NAMING | LETTER:
82 			*cp++ = addr[-1];
83 			continue;
84 		case NAMING | DELIM:
85 			state = RESET;
86 			sdl->sdl_nlen = cp - sdl->sdl_data;
87 			continue;
88 		case GOTTWO | DIGIT:
89 			*cp++ = byte;
90 			/* FALLTHROUGH */
91 		case RESET | DIGIT:
92 			state = GOTONE;
93 			byte = new;
94 			continue;
95 		case GOTONE | DIGIT:
96 			state = GOTTWO;
97 			byte = new + (byte << 4);
98 			continue;
99 		default: /* | DELIM */
100 			state = RESET;
101 			*cp++ = byte;
102 			byte = 0;
103 			continue;
104 		case GOTONE | END:
105 		case GOTTWO | END:
106 			*cp++ = byte;
107 			/* FALLTHROUGH */
108 		case RESET | END:
109 			break;
110 		}
111 		break;
112 	} while (cp < cplim);
113 	sdl->sdl_alen = cp - LLADDR(sdl);
114 	new = cp - (char *)sdl;
115 	if (new > sizeof(*sdl))
116 		sdl->sdl_len = new;
117 	return;
118 }
119 
120 char *
121 link_ntoa(const struct sockaddr_dl *sdl)
122 {
123 	static char obuf[64];
124 	size_t buflen;
125 	_Static_assert(sizeof(obuf) >= IFNAMSIZ + 20, "obuf is too small");
126 
127 	/*
128 	 * Ignoring the return value of link_ntoa_r() is safe here because it
129 	 * always writes the terminating NUL.  This preserves the traditional
130 	 * behaviour of link_ntoa().
131 	 */
132 	buflen = sizeof(obuf);
133 	(void)link_ntoa_r(sdl, obuf, &buflen);
134 	return obuf;
135 }
136 
137 int
138 link_ntoa_r(const struct sockaddr_dl *sdl, char *obuf, size_t *buflen)
139 {
140 	static const char hexlist[] = "0123456789abcdef";
141 	char *out;
142 	const u_char *in, *inlim;
143 	int namelen, i, rem;
144 	size_t needed;
145 
146 	assert(sdl);
147 	assert(buflen);
148 	/* obuf may be null */
149 
150 	needed = 1; /* 1 for the NUL */
151 	out = obuf;
152 	if (obuf)
153 		rem = *buflen;
154 	else
155 		rem = 0;
156 
157 /*
158  * Check if at least n bytes are available in the output buffer, plus 1 for the
159  * trailing NUL.  If not, set rem = 0 so we stop writing.
160  * Either way, increment needed by the amount we would have written.
161  */
162 #define CHECK(n) do {				\
163 		if ((SIZE_MAX - (n)) >= needed)	\
164 			needed += (n);		\
165 		if (rem >= ((n) + 1))		\
166 			rem -= (n);		\
167 		else				\
168 			rem = 0;		\
169 	} while (0)
170 
171 /*
172  * Write the char c to the output buffer, unless the buffer is full.
173  * Note that if obuf is NULL, rem is always zero.
174  */
175 #define OUT(c) do {			\
176 		if (rem > 0)		\
177 			*out++ = (c);	\
178 	} while (0)
179 
180 	namelen = (sdl->sdl_nlen <= IFNAMSIZ) ? sdl->sdl_nlen : IFNAMSIZ;
181 	if (namelen > 0) {
182 		CHECK(namelen);
183 		if (rem > 0) {
184 			bcopy(sdl->sdl_data, out, namelen);
185 			out += namelen;
186 		}
187 
188 		if (sdl->sdl_alen > 0) {
189 			CHECK(1);
190 			OUT(':');
191 		}
192 	}
193 
194 	in = (const u_char *)LLADDR(sdl);
195 	inlim = in + sdl->sdl_alen;
196 
197 	while (in < inlim) {
198 		if (in != (const u_char *)LLADDR(sdl)) {
199 			CHECK(1);
200 			OUT('.');
201 		}
202 		i = *in++;
203 		if (i > 0xf) {
204 			CHECK(2);
205 			OUT(hexlist[i >> 4]);
206 			OUT(hexlist[i & 0xf]);
207 		} else {
208 			CHECK(1);
209 			OUT(hexlist[i]);
210 		}
211 	}
212 
213 #undef CHECK
214 #undef OUT
215 
216 	/*
217 	 * We always leave enough room for the NUL if possible, but the user
218 	 * might have passed a NULL or zero-length buffer.
219 	 */
220 	if (out && *buflen)
221 		*out = '\0';
222 
223 	*buflen = needed;
224 	return ((rem > 0) ? 0 : -1);
225 }
226