1 /*	$NetBSD: getnameinfo.c,v 1.59 2015/09/22 16:15:08 christos Exp $	*/
2 /*	$KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $	*/
3 
4 /*
5  * Copyright (c) 2000 Ben Harris.
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Issues to be discussed:
36  * - Thread safe-ness must be checked
37  * - RFC2553 says that we should raise error on short buffer.  X/Open says
38  *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
39  *   modified).  ipngwg rough consensus seems to follow RFC2553.
40  * - What is "local" in NI_FQDN?
41  * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
42  * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
43  *   sin6_scope_id is filled - standardization status?
44  *   XXX breaks backward compat for code that expects no scopeid.
45  *   beware on merge.
46  */
47 
48 #include <sys/cdefs.h>
49 #if defined(LIBC_SCCS) && !defined(lint)
50 __RCSID("$NetBSD: getnameinfo.c,v 1.59 2015/09/22 16:15:08 christos Exp $");
51 #endif /* LIBC_SCCS and not lint */
52 
53 #ifndef RUMP_ACTION
54 #include "namespace.h"
55 #endif
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/un.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_ieee1394.h>
62 #include <net/if_types.h>
63 #include <netatalk/at.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
67 #include <assert.h>
68 #include <limits.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <stddef.h>
72 #include <string.h>
73 
74 #include "servent.h"
75 #include "hostent.h"
76 
77 #ifndef RUMP_ACTION
78 #ifdef __weak_alias
79 __weak_alias(getnameinfo,_getnameinfo)
80 #endif
81 #endif
82 
83 static const struct afd {
84 	int		a_af;
85 	socklen_t	a_addrlen;
86 	socklen_t	a_socklen;
87 	int		a_off;
88 } afdl [] = {
89 #ifdef INET6
90 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
91 		offsetof(struct sockaddr_in6, sin6_addr)},
92 #endif
93 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
94 		offsetof(struct sockaddr_in, sin_addr)},
95 	{0, 0, 0, 0},
96 };
97 
98 struct sockinet {
99 	u_char	si_len;
100 	u_char	si_family;
101 	u_short	si_port;
102 };
103 
104 static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
105     socklen_t, char *, socklen_t, int);
106 #ifdef INET6
107 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
108 				 socklen_t, int);
109 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
110 #endif
111 static int getnameinfo_atalk(const struct sockaddr *, socklen_t, char *,
112     socklen_t, char *, socklen_t, int);
113 static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
114     socklen_t, char *, socklen_t, int);
115 
116 static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
117     socklen_t, char *, socklen_t, int);
118 static int hexname(const uint8_t *, size_t, char *, socklen_t);
119 
120 /*
121  * Top-level getnameinfo() code.  Look at the address family, and pick an
122  * appropriate function to call.
123  */
124 int
getnameinfo(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)125 getnameinfo(const struct sockaddr *sa, socklen_t salen,
126 	char *host, socklen_t hostlen,
127 	char *serv, socklen_t servlen,
128 	int flags)
129 {
130 
131 	switch (sa->sa_family) {
132 	case AF_APPLETALK:
133 		return getnameinfo_atalk(sa, salen, host, hostlen,
134 		    serv, servlen, flags);
135 	case AF_INET:
136 	case AF_INET6:
137 		return getnameinfo_inet(sa, salen, host, hostlen,
138 		    serv, servlen, flags);
139 	case AF_LINK:
140 		return getnameinfo_link(sa, salen, host, hostlen,
141 		    serv, servlen, flags);
142 	case AF_LOCAL:
143 		return getnameinfo_local(sa, salen, host, hostlen,
144 		    serv, servlen, flags);
145 	default:
146 		return EAI_FAMILY;
147 	}
148 }
149 
150 /*
151  * getnameinfo_atalk():
152  * Format an AppleTalk address into a printable format.
153  */
154 /* ARGSUSED */
155 static int
getnameinfo_atalk(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)156 getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
157     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
158     int flags)
159 {
160 	char numserv[8];
161 	int n, m=0;
162 
163 	const struct sockaddr_at *sat =
164 	    (const struct sockaddr_at *)(const void *)sa;
165 
166 	if (serv != NULL && servlen > 0) {
167 		snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
168 		if (strlen(numserv) + 1 > servlen)
169 			return EAI_MEMORY;
170 		strlcpy(serv, numserv, servlen);
171 	}
172 
173         n = snprintf(host, hostlen, "%u.%u",
174 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
175 
176 	if (n < 0 || (socklen_t)(m+n) >= hostlen)
177 		goto errout;
178 
179 	m += n;
180 
181 	if (sat->sat_range.r_netrange.nr_phase) {
182         	n = snprintf(host+m, hostlen-m, " phase %u",
183 			sat->sat_range.r_netrange.nr_phase);
184 
185 		if (n < 0 || (socklen_t)(m+n) >= hostlen)
186 			goto errout;
187 
188 		m += n;
189 	}
190 	if (sat->sat_range.r_netrange.nr_firstnet) {
191         	n = snprintf(host+m, hostlen-m, " range %u - %u",
192 			ntohs(sat->sat_range.r_netrange.nr_firstnet),
193 			ntohs(sat->sat_range.r_netrange.nr_lastnet ));
194 
195 		if (n < 0 || (socklen_t)(m+n) >= hostlen)
196 			goto errout;
197 
198 		m += n;
199 	}
200 
201 	return 0;
202 
203 errout:
204 	if (host && hostlen>0)
205 		host[m] = '\0';	/* XXX ??? */
206 
207 	return EAI_MEMORY;
208 }
209 
210 /*
211  * getnameinfo_local():
212  * Format an local address into a printable format.
213  */
214 /* ARGSUSED */
215 static int
getnameinfo_local(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)216 getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
217     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
218     int flags)
219 {
220 	const struct sockaddr_un *sun =
221 	    (const struct sockaddr_un *)(const void *)sa;
222 
223 	if (serv != NULL && servlen > 0)
224 		serv[0] = '\0';
225 
226 	if (host && hostlen > 0)
227 		strlcpy(host, sun->sun_path,
228 		    MIN(sizeof(sun->sun_path) + 1, hostlen));
229 
230 	return 0;
231 }
232 
233 /*
234  * getnameinfo_inet():
235  * Format an IPv4 or IPv6 sockaddr into a printable string.
236  */
237 static int
getnameinfo_inet(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)238 getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
239 	char *host, socklen_t hostlen,
240 	char *serv, socklen_t servlen,
241 	int flags)
242 {
243 	const struct afd *afd;
244 	struct servent *sp;
245 	struct hostent *hp;
246 	u_short port;
247 	int family, i;
248 	const char *addr;
249 	uint32_t v4a;
250 	char numserv[512];
251 	char numaddr[512];
252 
253 	/* sa is checked below */
254 	/* host may be NULL */
255 	/* serv may be NULL */
256 
257 	if (sa == NULL)
258 		return EAI_FAIL;
259 
260 	family = sa->sa_family;
261 	for (i = 0; afdl[i].a_af; i++)
262 		if (afdl[i].a_af == family) {
263 			afd = &afdl[i];
264 			goto found;
265 		}
266 	return EAI_FAMILY;
267 
268  found:
269 	if (salen != afd->a_socklen)
270 		return EAI_FAIL;
271 
272 	/* network byte order */
273 	port = ((const struct sockinet *)(const void *)sa)->si_port;
274 	addr = (const char *)(const void *)sa + afd->a_off;
275 
276 	if (serv == NULL || servlen == 0) {
277 		/*
278 		 * do nothing in this case.
279 		 * in case you are wondering if "&&" is more correct than
280 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
281 		 * servlen == 0 means that the caller does not want the result.
282 		 */
283 	} else {
284 		struct servent_data svd;
285 		struct servent sv;
286 
287 		if (flags & NI_NUMERICSERV)
288 			sp = NULL;
289 		else {
290 			(void)memset(&svd, 0, sizeof(svd));
291 			sp = getservbyport_r(port,
292 				(flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
293 		}
294 		if (sp) {
295 			if (strlen(sp->s_name) + 1 > servlen) {
296 				endservent_r(&svd);
297 				return EAI_MEMORY;
298 			}
299 			strlcpy(serv, sp->s_name, servlen);
300 			endservent_r(&svd);
301 		} else {
302 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
303 			if (strlen(numserv) + 1 > servlen)
304 				return EAI_MEMORY;
305 			strlcpy(serv, numserv, servlen);
306 		}
307 	}
308 
309 	switch (sa->sa_family) {
310 	case AF_INET:
311 		v4a = (uint32_t)
312 		    ntohl(((const struct sockaddr_in *)
313 		    (const void *)sa)->sin_addr.s_addr);
314 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
315 			flags |= NI_NUMERICHOST;
316 		v4a >>= IN_CLASSA_NSHIFT;
317 		if (v4a == 0)
318 			flags |= NI_NUMERICHOST;
319 		break;
320 #ifdef INET6
321 	case AF_INET6:
322 	    {
323 		const struct sockaddr_in6 *sin6;
324 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
325 		switch (sin6->sin6_addr.s6_addr[0]) {
326 		case 0x00:
327 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
328 				;
329 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
330 				;
331 			else
332 				flags |= NI_NUMERICHOST;
333 			break;
334 		default:
335 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
336 				flags |= NI_NUMERICHOST;
337 			}
338 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
339 				flags |= NI_NUMERICHOST;
340 			break;
341 		}
342 	    }
343 		break;
344 #endif
345 	}
346 	if (host == NULL || hostlen == 0) {
347 		/*
348 		 * do nothing in this case.
349 		 * in case you are wondering if "&&" is more correct than
350 		 * "||" here: rfc2553bis-03 says that host == NULL or
351 		 * hostlen == 0 means that the caller does not want the result.
352 		 */
353 	} else if (flags & NI_NUMERICHOST) {
354 		size_t numaddrlen;
355 
356 		/* NUMERICHOST and NAMEREQD conflicts with each other */
357 		if (flags & NI_NAMEREQD)
358 			return EAI_NONAME;
359 
360 		switch(afd->a_af) {
361 #ifdef INET6
362 		case AF_INET6:
363 		{
364 			int error;
365 
366 			if ((error = ip6_parsenumeric(sa, addr, host,
367 						      hostlen, flags)) != 0)
368 				return(error);
369 			break;
370 		}
371 #endif
372 		default:
373 			if (inet_ntop(afd->a_af, addr, numaddr,
374 			    (socklen_t)sizeof(numaddr)) == NULL)
375 				return EAI_SYSTEM;
376 			numaddrlen = strlen(numaddr);
377 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
378 				return EAI_MEMORY;
379 			strlcpy(host, numaddr, hostlen);
380 			break;
381 		}
382 	} else {
383 		struct hostent hent;
384 		char hbuf[4096];
385 		int he;
386 		hp = gethostbyaddr_r(addr, afd->a_addrlen, afd->a_af, &hent,
387 		    hbuf, sizeof(hbuf), &he);
388 
389 		if (hp) {
390 #if 0
391 			/*
392 			 * commented out, since "for local host" is not
393 			 * implemented here - see RFC2553 p30
394 			 */
395 			if (flags & NI_NOFQDN) {
396 				char *p;
397 				p = strchr(hp->h_name, '.');
398 				if (p)
399 					*p = '\0';
400 			}
401 #endif
402 			if (strlen(hp->h_name) + 1 > hostlen) {
403 				return EAI_MEMORY;
404 			}
405 			strlcpy(host, hp->h_name, hostlen);
406 		} else {
407 			switch (he) {
408 			case NO_DATA:
409 			case HOST_NOT_FOUND:
410 				if (flags & NI_NAMEREQD)
411 					return EAI_NONAME;
412 				break;
413 			case TRY_AGAIN:
414 				return EAI_AGAIN;
415 			case NETDB_SUCCESS:
416 			case NETDB_INTERNAL:
417 			case NO_RECOVERY:
418 				/*FALLTHROUGH*/
419 			default:
420 				return EAI_SYSTEM;
421 			}
422 			switch(afd->a_af) {
423 #ifdef INET6
424 			case AF_INET6:
425 			{
426 				int error;
427 
428 				if ((error = ip6_parsenumeric(sa, addr, host,
429 							      hostlen,
430 							      flags)) != 0)
431 					return(error);
432 				break;
433 			}
434 #endif
435 			default:
436 				if (inet_ntop(afd->a_af, addr, host,
437 				    hostlen) == NULL)
438 					return EAI_SYSTEM;
439 				break;
440 			}
441 		}
442 	}
443 	return(0);
444 }
445 
446 #ifdef INET6
447 static int
ip6_parsenumeric(const struct sockaddr * sa,const char * addr,char * host,socklen_t hostlen,int flags)448 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
449 	socklen_t hostlen, int flags)
450 {
451 	size_t numaddrlen;
452 	char numaddr[512];
453 
454 	_DIAGASSERT(sa != NULL);
455 	_DIAGASSERT(addr != NULL);
456 	_DIAGASSERT(host != NULL);
457 
458 	if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr))
459 	    == NULL)
460 		return EAI_SYSTEM;
461 
462 	numaddrlen = strlen(numaddr);
463 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
464 		return EAI_OVERFLOW;
465 	strlcpy(host, numaddr, hostlen);
466 
467 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
468 		char zonebuf[MAXHOSTNAMELEN];
469 		int zonelen;
470 
471 		zonelen = ip6_sa2str(
472 		    (const struct sockaddr_in6 *)(const void *)sa,
473 		    zonebuf, sizeof(zonebuf), flags);
474 		if (zonelen < 0)
475 			return EAI_OVERFLOW;
476 		if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
477 			return EAI_OVERFLOW;
478 		/* construct <numeric-addr><delim><zoneid> */
479 		memcpy(host + numaddrlen + 1, zonebuf,
480 		    (size_t)zonelen);
481 		host[numaddrlen] = SCOPE_DELIMITER;
482 		host[numaddrlen + 1 + zonelen] = '\0';
483 	}
484 
485 	return 0;
486 }
487 
488 /* ARGSUSED */
489 static int
ip6_sa2str(const struct sockaddr_in6 * sa6,char * buf,size_t bufsiz,int flags)490 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
491 {
492 	unsigned int ifindex;
493 	const struct in6_addr *a6;
494 	int n;
495 
496 	_DIAGASSERT(sa6 != NULL);
497 	_DIAGASSERT(buf != NULL);
498 
499 	ifindex = (unsigned int)sa6->sin6_scope_id;
500 	a6 = &sa6->sin6_addr;
501 
502 #ifdef NI_NUMERICSCOPE
503 	if ((flags & NI_NUMERICSCOPE) != 0) {
504 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
505 		if (n < 0 || (size_t)n >= bufsiz)
506 			return -1;
507 		else
508 			return n;
509 	}
510 #endif
511 
512 	/* if_indextoname() does not take buffer size.  not a good api... */
513 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
514 	    bufsiz >= IF_NAMESIZE) {
515 		char *p = if_indextoname(ifindex, buf);
516 		if (p) {
517 			return (int)strlen(p);
518 		}
519 	}
520 
521 	/* last resort */
522 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
523 	if (n < 0 || (size_t) n >= bufsiz)
524 		return -1;
525 	else
526 		return n;
527 }
528 #endif /* INET6 */
529 
530 
531 /*
532  * getnameinfo_link():
533  * Format a link-layer address into a printable format, paying attention to
534  * the interface type.
535  */
536 /* ARGSUSED */
537 static int
getnameinfo_link(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)538 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
539     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
540     int flags)
541 {
542 	const struct sockaddr_dl *sdl =
543 	    (const struct sockaddr_dl *)(const void *)sa;
544 	const struct ieee1394_hwaddr *iha;
545 	int n;
546 
547 	if (serv != NULL && servlen > 0)
548 		*serv = '\0';
549 
550 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
551 		n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
552 		goto out;
553 	}
554 
555 	switch (sdl->sdl_type) {
556 #ifdef IFT_ECONET
557 	case IFT_ECONET:
558 		if (sdl->sdl_alen < 2)
559 			return EAI_FAMILY;
560 		if (CLLADDR(sdl)[1] == 0)
561 			n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
562 		else
563 			n = snprintf(host, hostlen, "%u.%u",
564 			    CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
565 		goto out;
566 #endif
567 	case IFT_IEEE1394:
568 		if (sdl->sdl_alen < sizeof(iha->iha_uid))
569 			return EAI_FAMILY;
570 		iha =
571 		    (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
572 		return hexname(iha->iha_uid, sizeof(iha->iha_uid),
573 		    host, hostlen);
574 	/*
575 	 * The following have zero-length addresses.
576 	 * IFT_ATM	(net/if_atmsubr.c)
577 	 * IFT_FAITH	(net/if_faith.c)
578 	 * IFT_GIF	(net/if_gif.c)
579 	 * IFT_LOOP	(net/if_loop.c)
580 	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
581 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
582 	 * IFT_STF	(net/if_stf.c)
583 	 * IFT_L2VLAN	(net/if_vlan.c)
584 	 * IFT_PROPVIRTUAL (net/if_bridge.h>
585 	 */
586 	/*
587 	 * The following use IPv4 addresses as link-layer addresses:
588 	 * IFT_OTHER	(net/if_gre.c)
589 	 */
590 	case IFT_ARCNET: /* default below is believed correct for all these. */
591 	case IFT_ETHER:
592 	case IFT_FDDI:
593 	case IFT_HIPPI:
594 	case IFT_ISO88025:
595 	default:
596 		return hexname((const uint8_t *)CLLADDR(sdl),
597 		    (size_t)sdl->sdl_alen, host, hostlen);
598 	}
599 out:
600 	if (n < 0 || (socklen_t) n >= hostlen) {
601 		*host = '\0';
602 		return EAI_MEMORY;
603 	}
604 	return 0;
605 }
606 
607 static int
hexname(const uint8_t * cp,size_t len,char * host,socklen_t hostlen)608 hexname(const uint8_t *cp, size_t len, char *host, socklen_t hostlen)
609 {
610 	int n;
611 	size_t i;
612 	char *outp = host;
613 
614 	*outp = '\0';
615 	for (i = 0; i < len; i++) {
616 		n = snprintf(outp, hostlen, "%s%02x",
617 		    i ? ":" : "", cp[i]);
618 		if (n < 0 || (socklen_t) n >= hostlen) {
619 			*host = '\0';
620 			return EAI_MEMORY;
621 		}
622 		outp += n;
623 		hostlen -= n;
624 	}
625 	return 0;
626 }
627