1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 
13 /*! \file */
14 
15 #include <config.h>
16 
17 #include <stdbool.h>
18 #include <stdio.h>
19 #include <inttypes.h>
20 
21 #include <isc/buffer.h>
22 #include <isc/msgs.h>
23 #include <isc/net.h>
24 #include <isc/netaddr.h>
25 #include <isc/print.h>
26 #include <isc/sockaddr.h>
27 #include <isc/string.h>
28 #include <isc/util.h>
29 
30 bool
isc_netaddr_equal(const isc_netaddr_t * a,const isc_netaddr_t * b)31 isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b) {
32 	REQUIRE(a != NULL && b != NULL);
33 
34 	if (a->family != b->family)
35 		return (false);
36 
37 	if (a->zone != b->zone)
38 		return (false);
39 
40 	switch (a->family) {
41 	case AF_INET:
42 		if (a->type.in.s_addr != b->type.in.s_addr)
43 			return (false);
44 		break;
45 	case AF_INET6:
46 		if (memcmp(&a->type.in6, &b->type.in6,
47 			   sizeof(a->type.in6)) != 0 ||
48 		    a->zone != b->zone)
49 			return (false);
50 		break;
51 #ifdef ISC_PLATFORM_HAVESYSUNH
52 	case AF_UNIX:
53 		if (strcmp(a->type.un, b->type.un) != 0)
54 			return (false);
55 		break;
56 #endif
57 	default:
58 		return (false);
59 	}
60 	return (true);
61 }
62 
63 bool
isc_netaddr_eqprefix(const isc_netaddr_t * a,const isc_netaddr_t * b,unsigned int prefixlen)64 isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
65 		     unsigned int prefixlen)
66 {
67 	const unsigned char *pa = NULL, *pb = NULL;
68 	unsigned int ipabytes = 0; /* Length of whole IP address in bytes */
69 	unsigned int nbytes;       /* Number of significant whole bytes */
70 	unsigned int nbits;        /* Number of significant leftover bits */
71 
72 	REQUIRE(a != NULL && b != NULL);
73 
74 	if (a->family != b->family)
75 		return (false);
76 
77 	if (a->zone != b->zone && b->zone != 0)
78 		return (false);
79 
80 	switch (a->family) {
81 	case AF_INET:
82 		pa = (const unsigned char *) &a->type.in;
83 		pb = (const unsigned char *) &b->type.in;
84 		ipabytes = 4;
85 		break;
86 	case AF_INET6:
87 		pa = (const unsigned char *) &a->type.in6;
88 		pb = (const unsigned char *) &b->type.in6;
89 		ipabytes = 16;
90 		break;
91 	default:
92 		return (false);
93 	}
94 
95 	/*
96 	 * Don't crash if we get a pattern like 10.0.0.1/9999999.
97 	 */
98 	if (prefixlen > ipabytes * 8)
99 		prefixlen = ipabytes * 8;
100 
101 	nbytes = prefixlen / 8;
102 	nbits = prefixlen % 8;
103 
104 	if (nbytes > 0) {
105 		if (memcmp(pa, pb, nbytes) != 0)
106 			return (false);
107 	}
108 	if (nbits > 0) {
109 		unsigned int bytea, byteb, mask;
110 		INSIST(nbytes < ipabytes);
111 		INSIST(nbits < 8);
112 		bytea = pa[nbytes];
113 		byteb = pb[nbytes];
114 		mask = (0xFF << (8-nbits)) & 0xFF;
115 		if ((bytea & mask) != (byteb & mask))
116 			return (false);
117 	}
118 	return (true);
119 }
120 
121 isc_result_t
isc_netaddr_totext(const isc_netaddr_t * netaddr,isc_buffer_t * target)122 isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
123 	char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
124 	char zbuf[sizeof("%4294967295")];
125 	unsigned int alen;
126 	int zlen;
127 	const char *r;
128 	const void *type;
129 
130 	REQUIRE(netaddr != NULL);
131 
132 	switch (netaddr->family) {
133 	case AF_INET:
134 		type = &netaddr->type.in;
135 		break;
136 	case AF_INET6:
137 		type = &netaddr->type.in6;
138 		break;
139 #ifdef ISC_PLATFORM_HAVESYSUNH
140 	case AF_UNIX:
141 		alen = strlen(netaddr->type.un);
142 		if (alen > isc_buffer_availablelength(target))
143 			return (ISC_R_NOSPACE);
144 		isc_buffer_putmem(target,
145 				  (const unsigned char *)(netaddr->type.un),
146 				  alen);
147 		return (ISC_R_SUCCESS);
148 #endif
149 	default:
150 		return (ISC_R_FAILURE);
151 	}
152 	r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
153 	if (r == NULL)
154 		return (ISC_R_FAILURE);
155 
156 	alen = strlen(abuf);
157 	INSIST(alen < sizeof(abuf));
158 
159 	zlen = 0;
160 	if (netaddr->family == AF_INET6 && netaddr->zone != 0) {
161 		zlen = snprintf(zbuf, sizeof(zbuf), "%%%u", netaddr->zone);
162 		if (zlen < 0)
163 			return (ISC_R_FAILURE);
164 		INSIST((unsigned int)zlen < sizeof(zbuf));
165 	}
166 
167 	if (alen + zlen > isc_buffer_availablelength(target))
168 		return (ISC_R_NOSPACE);
169 
170 	isc_buffer_putmem(target, (unsigned char *)abuf, alen);
171 	isc_buffer_putmem(target, (unsigned char *)zbuf, (unsigned int)zlen);
172 
173 	return (ISC_R_SUCCESS);
174 }
175 
176 void
isc_netaddr_format(const isc_netaddr_t * na,char * array,unsigned int size)177 isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
178 	isc_result_t result;
179 	isc_buffer_t buf;
180 
181 	isc_buffer_init(&buf, array, size);
182 	result = isc_netaddr_totext(na, &buf);
183 
184 	if (size == 0)
185 		return;
186 
187 	/*
188 	 * Null terminate.
189 	 */
190 	if (result == ISC_R_SUCCESS) {
191 		if (isc_buffer_availablelength(&buf) >= 1)
192 			isc_buffer_putuint8(&buf, 0);
193 		else
194 			result = ISC_R_NOSPACE;
195 	}
196 
197 	if (result != ISC_R_SUCCESS) {
198 		snprintf(array, size,
199 			 isc_msgcat_get(isc_msgcat, ISC_MSGSET_NETADDR,
200 					ISC_MSG_UNKNOWNADDR,
201 					"<unknown address, family %u>"),
202 			 na->family);
203 		array[size - 1] = '\0';
204 	}
205 }
206 
207 
208 isc_result_t
isc_netaddr_prefixok(const isc_netaddr_t * na,unsigned int prefixlen)209 isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen) {
210 	static const unsigned char zeros[16];
211 	unsigned int nbits, nbytes, ipbytes = 0;
212 	const unsigned char *p;
213 
214 	switch (na->family) {
215 	case AF_INET:
216 		p = (const unsigned char *) &na->type.in;
217 		ipbytes = 4;
218 		if (prefixlen > 32)
219 			return (ISC_R_RANGE);
220 		break;
221 	case AF_INET6:
222 		p = (const unsigned char *) &na->type.in6;
223 		ipbytes = 16;
224 		if (prefixlen > 128)
225 			return (ISC_R_RANGE);
226 		break;
227 	default:
228 		return (ISC_R_NOTIMPLEMENTED);
229 	}
230 	nbytes = prefixlen / 8;
231 	nbits = prefixlen % 8;
232 	if (nbits != 0) {
233 		INSIST(nbytes < ipbytes);
234 		if ((p[nbytes] & (0xff>>nbits)) != 0U)
235 			return (ISC_R_FAILURE);
236 		nbytes++;
237 	}
238 	if (nbytes < ipbytes && memcmp(p + nbytes, zeros, ipbytes - nbytes) != 0)
239 		return (ISC_R_FAILURE);
240 	return (ISC_R_SUCCESS);
241 }
242 
243 isc_result_t
isc_netaddr_masktoprefixlen(const isc_netaddr_t * s,unsigned int * lenp)244 isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
245 	unsigned int nbits = 0, nbytes = 0, ipbytes = 0, i;
246 	const unsigned char *p;
247 
248 	switch (s->family) {
249 	case AF_INET:
250 		p = (const unsigned char *) &s->type.in;
251 		ipbytes = 4;
252 		break;
253 	case AF_INET6:
254 		p = (const unsigned char *) &s->type.in6;
255 		ipbytes = 16;
256 		break;
257 	default:
258 		return (ISC_R_NOTIMPLEMENTED);
259 	}
260 	for (i = 0; i < ipbytes; i++) {
261 		if (p[i] != 0xFF)
262 			break;
263 	}
264 	nbytes = i;
265 	if (i < ipbytes) {
266 		unsigned int c = p[nbytes];
267 		while ((c & 0x80) != 0 && nbits < 8) {
268 			c <<= 1; nbits++;
269 		}
270 		if ((c & 0xFF) != 0)
271 			return (ISC_R_MASKNONCONTIG);
272 		i++;
273 	}
274 	for (; i < ipbytes; i++) {
275 		if (p[i] != 0)
276 			return (ISC_R_MASKNONCONTIG);
277 	}
278 	*lenp = nbytes * 8 + nbits;
279 	return (ISC_R_SUCCESS);
280 }
281 
282 void
isc_netaddr_fromin(isc_netaddr_t * netaddr,const struct in_addr * ina)283 isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina) {
284 	memset(netaddr, 0, sizeof(*netaddr));
285 	netaddr->family = AF_INET;
286 	netaddr->type.in = *ina;
287 }
288 
289 void
isc_netaddr_fromin6(isc_netaddr_t * netaddr,const struct in6_addr * ina6)290 isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6) {
291 	memset(netaddr, 0, sizeof(*netaddr));
292 	netaddr->family = AF_INET6;
293 	netaddr->type.in6 = *ina6;
294 }
295 
296 isc_result_t
isc_netaddr_frompath(isc_netaddr_t * netaddr,const char * path)297 isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path) {
298 #ifdef ISC_PLATFORM_HAVESYSUNH
299 	if (strlen(path) > sizeof(netaddr->type.un) - 1)
300 		return (ISC_R_NOSPACE);
301 
302 	memset(netaddr, 0, sizeof(*netaddr));
303 	netaddr->family = AF_UNIX;
304 	strlcpy(netaddr->type.un, path, sizeof(netaddr->type.un));
305 	netaddr->zone = 0;
306 	return (ISC_R_SUCCESS);
307 #else
308 	UNUSED(netaddr);
309 	UNUSED(path);
310 	return (ISC_R_NOTIMPLEMENTED);
311 #endif
312 }
313 
314 
315 void
isc_netaddr_setzone(isc_netaddr_t * netaddr,uint32_t zone)316 isc_netaddr_setzone(isc_netaddr_t *netaddr, uint32_t zone) {
317 	/* we currently only support AF_INET6. */
318 	REQUIRE(netaddr->family == AF_INET6);
319 
320 	netaddr->zone = zone;
321 }
322 
323 uint32_t
isc_netaddr_getzone(const isc_netaddr_t * netaddr)324 isc_netaddr_getzone(const isc_netaddr_t *netaddr) {
325 	return (netaddr->zone);
326 }
327 
328 void
isc_netaddr_fromsockaddr(isc_netaddr_t * t,const isc_sockaddr_t * s)329 isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
330 	int family = s->type.sa.sa_family;
331 	t->family = family;
332 	switch (family) {
333 	case AF_INET:
334 		t->type.in = s->type.sin.sin_addr;
335 		t->zone = 0;
336 		break;
337 	case AF_INET6:
338 		memmove(&t->type.in6, &s->type.sin6.sin6_addr, 16);
339 #ifdef ISC_PLATFORM_HAVESCOPEID
340 		t->zone = s->type.sin6.sin6_scope_id;
341 #else
342 		t->zone = 0;
343 #endif
344 		break;
345 #ifdef ISC_PLATFORM_HAVESYSUNH
346 	case AF_UNIX:
347 		memmove(t->type.un, s->type.sunix.sun_path, sizeof(t->type.un));
348 		t->zone = 0;
349 		break;
350 #endif
351 	default:
352 		INSIST(0);
353 		ISC_UNREACHABLE();
354 	}
355 }
356 
357 void
isc_netaddr_any(isc_netaddr_t * netaddr)358 isc_netaddr_any(isc_netaddr_t *netaddr) {
359 	memset(netaddr, 0, sizeof(*netaddr));
360 	netaddr->family = AF_INET;
361 	netaddr->type.in.s_addr = INADDR_ANY;
362 }
363 
364 void
isc_netaddr_any6(isc_netaddr_t * netaddr)365 isc_netaddr_any6(isc_netaddr_t *netaddr) {
366 	memset(netaddr, 0, sizeof(*netaddr));
367 	netaddr->family = AF_INET6;
368 	netaddr->type.in6 = in6addr_any;
369 }
370 
371 bool
isc_netaddr_ismulticast(isc_netaddr_t * na)372 isc_netaddr_ismulticast(isc_netaddr_t *na) {
373 	switch (na->family) {
374 	case AF_INET:
375 		return (ISC_IPADDR_ISMULTICAST(na->type.in.s_addr));
376 	case AF_INET6:
377 		return (IN6_IS_ADDR_MULTICAST(&na->type.in6));
378 	default:
379 		return (false);  /* XXXMLG ? */
380 	}
381 }
382 
383 bool
isc_netaddr_isexperimental(isc_netaddr_t * na)384 isc_netaddr_isexperimental(isc_netaddr_t *na) {
385 	switch (na->family) {
386 	case AF_INET:
387 		return (ISC_IPADDR_ISEXPERIMENTAL(na->type.in.s_addr));
388 	default:
389 		return (false);  /* XXXMLG ? */
390 	}
391 }
392 
393 bool
isc_netaddr_islinklocal(isc_netaddr_t * na)394 isc_netaddr_islinklocal(isc_netaddr_t *na) {
395 	switch (na->family) {
396 	case AF_INET:
397 		return (false);
398 	case AF_INET6:
399 		return (IN6_IS_ADDR_LINKLOCAL(&na->type.in6));
400 	default:
401 		return (false);
402 	}
403 }
404 
405 bool
isc_netaddr_issitelocal(isc_netaddr_t * na)406 isc_netaddr_issitelocal(isc_netaddr_t *na) {
407 	switch (na->family) {
408 	case AF_INET:
409 		return (false);
410 	case AF_INET6:
411 		return (IN6_IS_ADDR_SITELOCAL(&na->type.in6));
412 	default:
413 		return (false);
414 	}
415 }
416 
417 #define ISC_IPADDR_ISNETZERO(i) \
418 	       (((uint32_t)(i) & ISC__IPADDR(0xff000000)) \
419 		== ISC__IPADDR(0x00000000))
420 
421 bool
isc_netaddr_isnetzero(isc_netaddr_t * na)422 isc_netaddr_isnetzero(isc_netaddr_t *na) {
423 	switch (na->family) {
424 	case AF_INET:
425 		return (ISC_IPADDR_ISNETZERO(na->type.in.s_addr));
426 	case AF_INET6:
427 		return (false);
428 	default:
429 		return (false);
430 	}
431 }
432 
433 void
isc_netaddr_fromv4mapped(isc_netaddr_t * t,const isc_netaddr_t * s)434 isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
435 	isc_netaddr_t *src;
436 
437 	DE_CONST(s, src);	/* Must come before IN6_IS_ADDR_V4MAPPED. */
438 
439 	REQUIRE(s->family == AF_INET6);
440 	REQUIRE(IN6_IS_ADDR_V4MAPPED(&src->type.in6));
441 
442 	memset(t, 0, sizeof(*t));
443 	t->family = AF_INET;
444 	memmove(&t->type.in, (char *)&src->type.in6 + 12, 4);
445 	return;
446 }
447 
448 bool
isc_netaddr_isloopback(const isc_netaddr_t * na)449 isc_netaddr_isloopback(const isc_netaddr_t *na) {
450 	switch (na->family) {
451 	case AF_INET:
452 		return ((ntohl(na->type.in.s_addr) & 0xff000000U) ==
453 			0x7f000000U);
454 	case AF_INET6:
455 		return (IN6_IS_ADDR_LOOPBACK(&na->type.in6));
456 	default:
457 		return (false);
458 	}
459 }
460