xref: /titanic_41/usr/src/lib/libnsl/nss/getipnodeby.c (revision 2b637af7da499c08e98bb81902f8992f624e8d86)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * This file defines and implements the re-entrant getipnodebyname(),
27  * getipnodebyaddr(), and freehostent() routines for IPv6. These routines
28  * follow use the netdir_getbyYY() (see netdir_inet.c).
29  *
30  * lib/libnsl/nss/getipnodeby.c
31  */
32 
33 #include "mt.h"
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stropts.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <arpa/inet.h>
43 #include <nss_dbdefs.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <nss_netdir.h>
48 #include <net/if.h>
49 #include <netinet/in.h>
50 #include <netdir.h>
51 #include <thread.h>
52 #include <synch.h>
53 #include <fcntl.h>
54 #include <sys/time.h>
55 #include "nss.h"
56 
57 #define	IPV6_LITERAL_CHAR	':'
58 
59 /*
60  * The number of nanoseconds getipnodebyname() waits before getting
61  * fresh interface count information with SIOCGLIFNUM.  The default is
62  * five minutes.
63  */
64 #define	IFNUM_TIMEOUT	((hrtime_t)300 * NANOSEC)
65 
66 /*
67  * Bits in the bitfield returned by getipnodebyname_processflags().
68  *
69  * IPNODE_WANTIPV6	The user wants IPv6 addresses returned.
70  * IPNODE_WANTIPV4	The user wants IPv4 addresses returned.
71  * IPNODE_IPV4IFNOIPV6	The user only wants IPv4 addresses returned if no IPv6
72  *			addresses are returned.
73  * IPNODE_LOOKUPIPNODES	getipnodebyname() needs to lookup the name in ipnodes.
74  * IPNODE_LOOKUPHOSTS	getipnodebyname() needs to lookup the name in hosts.
75  * IPNODE_ISLITERAL	The name supplied is a literal address string.
76  */
77 #define	IPNODE_WANTIPV6		0x00000001u
78 #define	IPNODE_WANTIPV4		0x00000002u
79 #define	IPNODE_IPV4IFNOIPV6	0x00000004u
80 #define	IPNODE_LOOKUPIPNODES	0x00000008u
81 #define	IPNODE_LOOKUPHOSTS	0x00000010u
82 #define	IPNODE_LITERAL		0x00000020u
83 #define	IPNODE_IPV4		(IPNODE_WANTIPV4 | IPNODE_IPV4IFNOIPV6)
84 
85 /*
86  * The default set of bits corresponding to a getipnodebyname() flags
87  * argument of AI_DEFAULT.
88  */
89 #define	IPNODE_DEFAULT (IPNODE_WANTIPV6 | IPNODE_IPV4 | \
90 	IPNODE_LOOKUPIPNODES | IPNODE_LOOKUPHOSTS)
91 
92 extern struct netconfig *__rpc_getconfip(char *);
93 
94 static struct hostent *__mapv4tov6(struct hostent *, struct hostent *,
95     nss_XbyY_buf_t *, int);
96 struct hostent *__mappedtov4(struct hostent *, int *);
97 static struct hostent *__filter_addresses(int, struct hostent *);
98 static int __find_mapped(struct hostent *, int);
99 static nss_XbyY_buf_t *__IPv6_alloc(int);
100 static void __IPv6_cleanup(nss_XbyY_buf_t *);
101 static int __ai_addrconfig(int);
102 
103 
104 #ifdef PIC
105 struct hostent *
106 _uncached_getipnodebyname(const char *nam, struct hostent *result,
107 	char *buffer, int buflen, int af_family, int flags, int *h_errnop)
108 {
109 	return (_switch_getipnodebyname_r(nam, result, buffer, buflen,
110 	    af_family, flags, h_errnop));
111 }
112 
113 struct hostent *
114 _uncached_getipnodebyaddr(const char *addr, int length, int type,
115 	struct hostent *result, char *buffer, int buflen, int *h_errnop)
116 {
117 	if (type == AF_INET)
118 		return (_switch_gethostbyaddr_r(addr, length, type,
119 		    result, buffer, buflen, h_errnop));
120 	else if (type == AF_INET6)
121 		return (_switch_getipnodebyaddr_r(addr, length, type,
122 		    result, buffer, buflen, h_errnop));
123 	return (NULL);
124 }
125 #endif
126 
127 /*
128  * Given a name, an address family, and a set of flags, return a
129  * bitfield that getipnodebyname() will use.
130  */
131 static uint_t
132 getipnodebyname_processflags(const char *name, int af, int flags)
133 {
134 	uint_t		ipnode_bits = IPNODE_DEFAULT;
135 	boolean_t	ipv6configured = B_FALSE;
136 	boolean_t	ipv4configured = B_FALSE;
137 
138 	/*
139 	 * If AI_ADDRCONFIG is specified, we need to determine the number
140 	 * of addresses of each address family configured on the system as
141 	 * appropriate.
142 	 */
143 	if (flags & AI_ADDRCONFIG) {
144 		ipv6configured = (af == AF_INET6 &&
145 		    __ai_addrconfig(AF_INET6) > 0);
146 		ipv4configured = ((af == AF_INET || (flags & AI_V4MAPPED)) &&
147 		    __ai_addrconfig(AF_INET) > 0);
148 	}
149 
150 	/*
151 	 * Determine what kinds of addresses the user is interested
152 	 * in getting back.
153 	 */
154 	switch (af) {
155 	case AF_INET6:
156 		if ((flags & AI_ADDRCONFIG) && !ipv6configured)
157 			ipnode_bits &= ~IPNODE_WANTIPV6;
158 
159 		if (flags & AI_V4MAPPED) {
160 			if ((flags & AI_ADDRCONFIG) && !ipv4configured) {
161 				ipnode_bits &= ~IPNODE_IPV4;
162 			} else if (flags & AI_ALL) {
163 				ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
164 			}
165 		} else {
166 			ipnode_bits &= ~IPNODE_IPV4;
167 		}
168 		break;
169 	case AF_INET:
170 		if ((flags & AI_ADDRCONFIG) && !ipv4configured)
171 			ipnode_bits &= ~IPNODE_IPV4;
172 		ipnode_bits &= ~IPNODE_WANTIPV6;
173 		ipnode_bits &= ~IPNODE_IPV4IFNOIPV6;
174 		break;
175 	default:
176 		ipnode_bits = 0;
177 		break;
178 	}
179 
180 	/*
181 	 * If we're not looking for IPv4 addresses, don't bother looking
182 	 * in hosts.
183 	 */
184 	if (!(ipnode_bits & IPNODE_WANTIPV4))
185 		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
186 
187 	/*
188 	 * Determine if name is a literal IP address.  This will
189 	 * further narrow down what type of lookup we're going to do.
190 	 */
191 	if (strchr(name, IPV6_LITERAL_CHAR) != NULL) {
192 		/* Literal IPv6 address */
193 		ipnode_bits |= IPNODE_LITERAL;
194 		/*
195 		 * In s9 we accepted the literal without filtering independent
196 		 * of what family was passed in hints.  We continue to do
197 		 * this.
198 		 */
199 		ipnode_bits |= (IPNODE_WANTIPV6 | IPNODE_WANTIPV4);
200 		ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
201 	} else if (inet_addr(name) != 0xffffffffU) {
202 		/* Literal IPv4 address */
203 		ipnode_bits |= (IPNODE_LITERAL | IPNODE_WANTIPV4);
204 		ipnode_bits &= ~IPNODE_WANTIPV6;
205 		ipnode_bits &= ~IPNODE_LOOKUPIPNODES;
206 	}
207 	return (ipnode_bits);
208 }
209 
210 struct hostent *
211 getipnodebyname(const char *name, int af, int flags, int *error_num)
212 {
213 	struct hostent		*hp = NULL;
214 	nss_XbyY_buf_t		*buf4 = NULL;
215 	nss_XbyY_buf_t		*buf6 = NULL;
216 	struct netconfig	*nconf;
217 	struct nss_netdirbyname_in	nssin;
218 	union nss_netdirbyname_out	nssout;
219 	int			ret;
220 	uint_t			ipnode_bits;
221 
222 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
223 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
224 		*error_num = NO_RECOVERY;
225 		return (NULL);
226 	}
227 
228 	ipnode_bits = getipnodebyname_processflags(name, af, flags);
229 
230 	/* Make sure we have something to look up. */
231 	if (!(ipnode_bits & (IPNODE_WANTIPV6 | IPNODE_WANTIPV4))) {
232 		*error_num = HOST_NOT_FOUND;
233 		goto cleanup;
234 	}
235 
236 	/*
237 	 * Perform the requested lookups.  We always look through
238 	 * ipnodes first for both IPv4 and IPv6 addresses.  Depending
239 	 * on what was returned and what was needed, we either filter
240 	 * out the garbage, or ask for more using hosts.
241 	 */
242 	if (ipnode_bits & IPNODE_LOOKUPIPNODES) {
243 		if ((buf6 = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == NULL) {
244 			*error_num = NO_RECOVERY;
245 			goto cleanup;
246 		}
247 		nssin.op_t = NSS_HOST6;
248 		nssin.arg.nss.host6.name = name;
249 		nssin.arg.nss.host6.buf = buf6->buffer;
250 		nssin.arg.nss.host6.buflen = buf6->buflen;
251 		nssin.arg.nss.host6.af_family = af;
252 		nssin.arg.nss.host6.flags = flags;
253 		nssout.nss.host.hent = buf6->result;
254 		nssout.nss.host.herrno_p = error_num;
255 		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
256 		if (ret != ND_OK) {
257 			__IPv6_cleanup(buf6);
258 			buf6 = NULL;
259 		} else if (ipnode_bits & IPNODE_WANTIPV4) {
260 			/*
261 			 * buf6 may have all that we need if we either
262 			 * only wanted IPv4 addresses if there were no
263 			 * IPv6 addresses returned, or if there are
264 			 * IPv4-mapped addresses in buf6.  If either
265 			 * of these are true, then there's no need to
266 			 * look in hosts.
267 			 */
268 			if (ipnode_bits & IPNODE_IPV4IFNOIPV6 ||
269 			    __find_mapped(buf6->result, 0) != 0) {
270 				ipnode_bits &= ~IPNODE_LOOKUPHOSTS;
271 			} else if (!(ipnode_bits & IPNODE_WANTIPV6)) {
272 				/*
273 				 * If all we're looking for are IPv4
274 				 * addresses and there are none in
275 				 * buf6 then buf6 is now useless.
276 				 */
277 				__IPv6_cleanup(buf6);
278 				buf6 = NULL;
279 			}
280 		}
281 	}
282 	if (ipnode_bits & IPNODE_LOOKUPHOSTS) {
283 		if ((buf4 = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == NULL) {
284 			*error_num = NO_RECOVERY;
285 			goto cleanup;
286 		}
287 		nssin.op_t = NSS_HOST;
288 		nssin.arg.nss.host.name = name;
289 		nssin.arg.nss.host.buf = buf4->buffer;
290 		nssin.arg.nss.host.buflen = buf4->buflen;
291 		nssout.nss.host.hent = buf4->result;
292 		nssout.nss.host.herrno_p = error_num;
293 		ret = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
294 		if (ret != ND_OK) {
295 			__IPv6_cleanup(buf4);
296 			buf4 = NULL;
297 		}
298 	}
299 
300 	if (buf6 == NULL && buf4 == NULL) {
301 		*error_num = HOST_NOT_FOUND;
302 		goto cleanup;
303 	}
304 
305 	/* Extract the appropriate addresses from the returned buffer(s). */
306 	switch (af) {
307 	case AF_INET6: {
308 		if (buf4 != NULL) {
309 			nss_XbyY_buf_t *mergebuf;
310 
311 			/*
312 			 * The IPv4 results we have need to be
313 			 * converted to IPv4-mapped addresses,
314 			 * conditionally merged with the IPv6
315 			 * results, and the end result needs to be
316 			 * re-ordered.
317 			 */
318 			mergebuf = __IPv6_alloc(NSS_BUFLEN_IPNODES);
319 			if (mergebuf == NULL) {
320 				*error_num = NO_RECOVERY;
321 				goto cleanup;
322 			}
323 			hp = __mapv4tov6(buf4->result,
324 			    ((buf6 != NULL) ? buf6->result : NULL),
325 			    mergebuf, 1);
326 			if (hp != NULL)
327 				order_haddrlist_af(AF_INET6, hp->h_addr_list);
328 			else
329 				*error_num = NO_RECOVERY;
330 			free(mergebuf);
331 		}
332 
333 		if (buf4 == NULL && buf6 != NULL) {
334 			hp = buf6->result;
335 
336 			/*
337 			 * We have what we need in buf6, but we may need
338 			 * to filter out some addresses depending on what
339 			 * is being asked for.
340 			 */
341 			if (!(ipnode_bits & IPNODE_WANTIPV4))
342 				hp = __filter_addresses(AF_INET, buf6->result);
343 			else if (!(ipnode_bits & IPNODE_WANTIPV6))
344 				hp = __filter_addresses(AF_INET6, buf6->result);
345 
346 			if (hp == NULL)
347 				*error_num = NO_ADDRESS;
348 		}
349 
350 		break;
351 	}
352 
353 	case AF_INET:
354 		/* We could have results in buf6 or buf4, not both */
355 		if (buf6 != NULL) {
356 			/*
357 			 * Extract the IPv4-mapped addresses from buf6
358 			 * into hp.
359 			 */
360 			hp = __mappedtov4(buf6->result, error_num);
361 		} else {
362 			/* We have what we need in buf4. */
363 			hp = buf4->result;
364 			if (ipnode_bits & IPNODE_LITERAL) {
365 				/*
366 				 * There is a special case here for literal
367 				 * IPv4 address strings.  The hosts
368 				 * front-end sets h_aliases to a one
369 				 * element array containing a single NULL
370 				 * pointer (in ndaddr2hent()), while
371 				 * getipnodebyname() requires h_aliases to
372 				 * be a NULL pointer itself.  We're not
373 				 * going to change the front-end since it
374 				 * needs to remain backward compatible for
375 				 * gethostbyname() and friends.  Just set
376 				 * h_aliases to NULL here instead.
377 				 */
378 				hp->h_aliases = NULL;
379 			}
380 		}
381 
382 		break;
383 
384 	default:
385 		break;
386 	}
387 
388 cleanup:
389 	/*
390 	 * Free the memory we allocated, but make sure we don't free
391 	 * the memory we're returning to the caller.
392 	 */
393 	if (buf6 != NULL) {
394 		if (buf6->result == hp)
395 			buf6->result = NULL;
396 		__IPv6_cleanup(buf6);
397 	}
398 	if (buf4 != NULL) {
399 		if (buf4->result == hp)
400 			buf4->result = NULL;
401 		__IPv6_cleanup(buf4);
402 	}
403 	(void) freenetconfigent(nconf);
404 
405 	return (hp);
406 }
407 
408 /*
409  * This is the IPv6 interface for "gethostbyaddr".
410  */
411 struct hostent *
412 getipnodebyaddr(const void *src, size_t len, int type, int *error_num)
413 {
414 	struct in6_addr *addr6 = 0;
415 	struct in_addr *addr4 = 0;
416 	nss_XbyY_buf_t *buf = 0;
417 	nss_XbyY_buf_t *res = 0;
418 	struct netconfig *nconf;
419 	struct hostent *hp = 0;
420 	struct	nss_netdirbyaddr_in nssin;
421 	union	nss_netdirbyaddr_out nssout;
422 	int neterr;
423 	char tmpbuf[64];
424 
425 	if (type == AF_INET6) {
426 		if ((addr6 = (struct in6_addr *)src) == NULL) {
427 			*error_num = HOST_NOT_FOUND;
428 			return (NULL);
429 		}
430 	} else if (type == AF_INET) {
431 		if ((addr4 = (struct in_addr *)src) == NULL) {
432 			*error_num = HOST_NOT_FOUND;
433 			return (NULL);
434 		}
435 	} else {
436 		*error_num = HOST_NOT_FOUND;
437 		return (NULL);
438 	}
439 	/*
440 	 * Specific case: query for "::"
441 	 */
442 	if (type == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(addr6)) {
443 		*error_num = HOST_NOT_FOUND;
444 		return (NULL);
445 	}
446 	/*
447 	 * Step 1: IPv4-mapped address  or IPv4 Compat
448 	 */
449 	if ((type == AF_INET6 && len == 16) &&
450 	    ((IN6_IS_ADDR_V4MAPPED(addr6)) ||
451 	    (IN6_IS_ADDR_V4COMPAT(addr6)))) {
452 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
453 			*error_num = NO_RECOVERY;
454 			return (NULL);
455 		}
456 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
457 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
458 			*error_num = NO_RECOVERY;
459 			__IPv6_cleanup(buf);
460 			return (NULL);
461 		}
462 		nssin.op_t = NSS_HOST6;
463 		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
464 			(void) memcpy(tmpbuf, addr6, sizeof (*addr6));
465 			tmpbuf[10] = 0xffU;
466 			tmpbuf[11] = 0xffU;
467 			nssin.arg.nss.host.addr = (const char *)tmpbuf;
468 		} else {
469 			nssin.arg.nss.host.addr = (const char *)addr6;
470 		}
471 		nssin.arg.nss.host.len = sizeof (struct in6_addr);
472 		nssin.arg.nss.host.type = AF_INET6;
473 		nssin.arg.nss.host.buf = buf->buffer;
474 		nssin.arg.nss.host.buflen = buf->buflen;
475 
476 		nssout.nss.host.hent = buf->result;
477 		nssout.nss.host.herrno_p = error_num;
478 		/*
479 		 * We pass in nconf and let the implementation of the
480 		 * long-named func decide whether to use the switch based on
481 		 * nc_nlookups.
482 		 */
483 		neterr =
484 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
485 
486 		(void) freenetconfigent(nconf);
487 		if (neterr != ND_OK) {
488 			/* Failover case, try hosts db for v4 address */
489 			if (!gethostbyaddr_r(((char *)addr6) + 12,
490 			    sizeof (in_addr_t), AF_INET, buf->result,
491 			    buf->buffer, buf->buflen, error_num)) {
492 				__IPv6_cleanup(buf);
493 				return (NULL);
494 			}
495 			/* Found one, now format it into mapped/compat addr */
496 			if ((res = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
497 				__IPv6_cleanup(buf);
498 				*error_num = NO_RECOVERY;
499 				return (NULL);
500 			}
501 			/* Convert IPv4 to mapped/compat address w/name */
502 			hp = res->result;
503 			(void) __mapv4tov6(buf->result, 0, res,
504 			    IN6_IS_ADDR_V4MAPPED(addr6));
505 			__IPv6_cleanup(buf);
506 			free(res);
507 			return (hp);
508 		}
509 		/*
510 		 * At this point, we'll have a v4mapped hostent. If that's
511 		 * what was passed in, just return. If the request was a compat,
512 		 * twiggle the two bytes to make the mapped address a compat.
513 		 */
514 		hp = buf->result;
515 		if (IN6_IS_ADDR_V4COMPAT(addr6)) {
516 			/* LINTED pointer cast */
517 			addr6 = (struct in6_addr *)hp->h_addr_list[0];
518 			addr6->s6_addr[10] = 0;
519 			addr6->s6_addr[11] = 0;
520 		}
521 		free(buf);
522 		return (hp);
523 	}
524 	/*
525 	 * Step 2: AF_INET, v4 lookup. Since we're going to search the
526 	 * ipnodes (v6) path first, we need to treat this as a v4mapped
527 	 * address. nscd(1m) caches v4 from ipnodes as mapped v6's. The
528 	 * switch backend knows to lookup v4's (not v4mapped) from the
529 	 * name services.
530 	 */
531 	if (type == AF_INET) {
532 		struct in6_addr v4mapbuf;
533 		addr6 = &v4mapbuf;
534 
535 		IN6_INADDR_TO_V4MAPPED(addr4, addr6);
536 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
537 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
538 			*error_num = NO_RECOVERY;
539 			return (NULL);
540 		}
541 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
542 			*error_num = NO_RECOVERY;
543 			freenetconfigent(nconf);
544 			return (NULL);
545 		}
546 		nssin.op_t = NSS_HOST6;
547 		nssin.arg.nss.host.addr = (const char *)addr6;
548 		nssin.arg.nss.host.len = sizeof (struct in6_addr);
549 		nssin.arg.nss.host.type = AF_INET6;
550 		nssin.arg.nss.host.buf = buf->buffer;
551 		nssin.arg.nss.host.buflen = buf->buflen;
552 
553 		nssout.nss.host.hent = buf->result;
554 		nssout.nss.host.herrno_p = error_num;
555 		/*
556 		 * We pass in nconf and let the implementation of the
557 		 * long-named func decide whether to use the switch based on
558 		 * nc_nlookups.
559 		 */
560 		neterr =
561 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
562 
563 		(void) freenetconfigent(nconf);
564 		if (neterr != ND_OK) {
565 			/* Failover case, try hosts db for v4 address */
566 			hp = buf->result;
567 			if (!gethostbyaddr_r(src, len, type, buf->result,
568 			    buf->buffer, buf->buflen, error_num)) {
569 				__IPv6_cleanup(buf);
570 				return (NULL);
571 			}
572 			free(buf);
573 			return (hp);
574 		}
575 		if ((hp = __mappedtov4(buf->result, error_num)) == NULL) {
576 			__IPv6_cleanup(buf);
577 			return (NULL);
578 		}
579 		__IPv6_cleanup(buf);
580 		return (hp);
581 	}
582 	/*
583 	 * Step 3: AF_INET6, plain vanilla v6 getipnodebyaddr() call.
584 	 */
585 	if (type == AF_INET6) {
586 		if ((nconf = __rpc_getconfip("udp")) == NULL &&
587 		    (nconf = __rpc_getconfip("tcp")) == NULL) {
588 			*error_num = NO_RECOVERY;
589 			return (NULL);
590 		}
591 		if ((buf = __IPv6_alloc(NSS_BUFLEN_IPNODES)) == 0) {
592 			*error_num = NO_RECOVERY;
593 			freenetconfigent(nconf);
594 			return (NULL);
595 		}
596 		nssin.op_t = NSS_HOST6;
597 		nssin.arg.nss.host.addr = (const char *)addr6;
598 		nssin.arg.nss.host.len = len;
599 		nssin.arg.nss.host.type = type;
600 		nssin.arg.nss.host.buf = buf->buffer;
601 		nssin.arg.nss.host.buflen = buf->buflen;
602 
603 		nssout.nss.host.hent = buf->result;
604 		nssout.nss.host.herrno_p = error_num;
605 		/*
606 		 * We pass in nconf and let the implementation of the
607 		 * long-named func decide whether to use the switch based on
608 		 * nc_nlookups.
609 		 */
610 		neterr =
611 		    _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
612 
613 		(void) freenetconfigent(nconf);
614 		if (neterr != ND_OK) {
615 			__IPv6_cleanup(buf);
616 			return (NULL);
617 		}
618 		free(buf);
619 		return (nssout.nss.host.hent);
620 	}
621 	/*
622 	 * If we got here, unknown type.
623 	 */
624 	*error_num = HOST_NOT_FOUND;
625 	return (NULL);
626 }
627 
628 void
629 freehostent(struct hostent *hent)
630 {
631 	free(hent);
632 }
633 
634 static int
635 __ai_addrconfig(int af)
636 {
637 	struct lifnum	lifn;
638 	hrtime_t	now, *then;
639 	static hrtime_t	then4, then6; /* the last time we updated ifnum# */
640 	static int	ifnum4 = -1, ifnum6 = -1;
641 	int		*num;
642 
643 	switch (af) {
644 	case AF_INET:
645 		num = &ifnum4;
646 		then = &then4;
647 		break;
648 	case AF_INET6:
649 		num = &ifnum6;
650 		then = &then6;
651 		break;
652 	default:
653 		return (0);
654 	}
655 
656 	/*
657 	 * We don't need to check this every time someone does a name
658 	 * lookup.  Do it every IFNUM_TIMEOUT for each address family.
659 	 *
660 	 * There's no need to protect all of this with a lock.  The
661 	 * worst that can happen is that we update the interface count
662 	 * twice instead of once.  That's no big deal.
663 	 */
664 	now = gethrtime();
665 	if (*num == -1 || ((now - *then) >= IFNUM_TIMEOUT)) {
666 		lifn.lifn_family = af;
667 		/*
668 		 * We want to determine if this machine knows anything
669 		 * at all about the address family; the status of the
670 		 * interface is less important. Hence, set
671 		 * 'lifn_flags' to zero.
672 		 */
673 		lifn.lifn_flags = 0;
674 		if (nss_ioctl(af, SIOCGLIFNUM, &lifn) < 0)
675 			return (-1);
676 
677 		*num = lifn.lifn_count;
678 		*then = now;
679 	}
680 
681 	return (*num);
682 }
683 
684 /*
685  * This routine will either convert an IPv4 address to a mapped or compat
686  * IPv6 (if he6 == NULL) or merge IPv6 (he6) addresses with mapped
687  * v4 (he4) addresses. In either case, the results are returned in res.
688  * Caller must provide all buffers.
689  * Inputs:
690  * 		he4	pointer to IPv4 buffer
691  *		he6	pointer to IPv6 buffer (NULL if not merging v4/v6
692  *		res	pointer to results buffer
693  *		mapped	mapped == 1, map IPv4 : mapped == 0, compat IPv4
694  *			mapped flag is ignored if he6 != NULL
695  *
696  * The results are packed into the res->buffer as follows:
697  * <--------------- buffer + buflen -------------------------------------->
698  * |-----------------|-----------------|----------------|----------------|
699  * | pointers vector | pointers vector | aliases grow   | addresses grow |
700  * | for addresses   | for aliases     |                |                |
701  * | this way ->     | this way ->     | <- this way    |<- this way     |
702  * |-----------------|-----------------|----------------|----------------|
703  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
704  */
705 static struct hostent *
706 __mapv4tov6(struct hostent *he4, struct hostent *he6, nss_XbyY_buf_t *res,
707 		int mapped)
708 {
709 	char	*buffer, *limit;
710 	int	buflen = res->buflen;
711 	struct	in6_addr *addr6p;
712 	char	*buff_locp;
713 	struct	hostent *host;
714 	int	count = 0, len, i;
715 	char	*h_namep;
716 
717 	if (he4 == NULL || res == NULL) {
718 		return (NULL);
719 	}
720 	limit = res->buffer + buflen;
721 	host = (struct hostent *)res->result;
722 	buffer = res->buffer;
723 
724 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in6_addr));
725 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
726 	if ((char *)host->h_addr_list >= limit ||
727 	    buff_locp <= (char *)host->h_addr_list) {
728 		return (NULL);
729 	}
730 	if (he6 == NULL) {
731 		/*
732 		 * If he6==NULL, map the v4 address into the v6 address format.
733 		 * This is used for getipnodebyaddr() (single address, mapped or
734 		 * compatible) or for v4 mapped for getipnodebyname(), which
735 		 * could be multiple addresses. This could also be a literal
736 		 * address string, which is why there is a inet_addr() call.
737 		 */
738 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
739 			buff_locp -= sizeof (struct in6_addr);
740 			if (buff_locp <=
741 			    (char *)&(host->h_addr_list[count + 1])) {
742 			/*
743 			 * Has to be room for the pointer to the address we're
744 			 * about to add, as well as the final NULL ptr.
745 			 */
746 				return (NULL);
747 			}
748 			/* LINTED pointer cast */
749 			addr6p = (struct in6_addr *)buff_locp;
750 			host->h_addr_list[count] = (char *)addr6p;
751 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
752 			if (mapped) {
753 				addr6p->s6_addr[10] = 0xff;
754 				addr6p->s6_addr[11] = 0xff;
755 			}
756 			bcopy((char *)he4->h_addr_list[i],
757 			    &addr6p->s6_addr[12], sizeof (struct in_addr));
758 			++count;
759 		}
760 		/*
761 		 * Set last array element to NULL and add cname as first alias
762 		 */
763 		host->h_addr_list[count] = NULL;
764 		host->h_aliases = host->h_addr_list + count + 1;
765 		count = 0;
766 		if ((int)(inet_addr(he4->h_name)) != -1) {
767 		/*
768 		 * Literal address string, since we're mapping, we need the IPv6
769 		 * V4 mapped literal address string for h_name.
770 		 */
771 			char	tmpstr[128];
772 			(void) inet_ntop(AF_INET6, host->h_addr_list[0], tmpstr,
773 			    sizeof (tmpstr));
774 			buff_locp -= (len = strlen(tmpstr) + 1);
775 			h_namep = tmpstr;
776 			if (buff_locp <= (char *)(host->h_aliases))
777 				return (NULL);
778 			bcopy(h_namep, buff_locp, len);
779 			host->h_name = buff_locp;
780 			host->h_aliases = NULL; /* no aliases for literal */
781 			host->h_length = sizeof (struct in6_addr);
782 			host->h_addrtype = AF_INET6;
783 			return (host); 		/* we're done, return result */
784 		}
785 		/*
786 		 * Not a literal address string, so just copy h_name.
787 		 */
788 		buff_locp -= (len = strlen(he4->h_name) + 1);
789 		h_namep = he4->h_name;
790 		if (buff_locp <= (char *)(host->h_aliases))
791 			return (NULL);
792 		bcopy(h_namep, buff_locp, len);
793 		host->h_name = buff_locp;
794 		/*
795 		 * Pass 2 (IPv4 aliases):
796 		 */
797 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
798 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
799 			if (buff_locp <=
800 			    (char *)&(host->h_aliases[count + 1])) {
801 			/*
802 			 * Has to be room for the pointer to the address we're
803 			 * about to add, as well as the final NULL ptr.
804 			 */
805 				return (NULL);
806 			}
807 			host->h_aliases[count] = buff_locp;
808 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
809 			++count;
810 		}
811 		host->h_aliases[count] = NULL;
812 		host->h_length = sizeof (struct in6_addr);
813 		host->h_addrtype = AF_INET6;
814 		return (host);
815 	} else {
816 		/*
817 		 * Merge IPv4 mapped addresses with IPv6 addresses. The
818 		 * IPv6 address will go in first, followed by the v4 mapped.
819 		 *
820 		 * Pass 1 (IPv6 addresses):
821 		 */
822 		for (i = 0; he6->h_addr_list[i] != NULL; i++) {
823 			buff_locp -= sizeof (struct in6_addr);
824 			if (buff_locp <=
825 			    (char *)&(host->h_addr_list[count + 1])) {
826 			/*
827 			 * Has to be room for the pointer to the address we're
828 			 * about to add, as well as the final NULL ptr.
829 			 */
830 				return (NULL);
831 			}
832 			host->h_addr_list[count] = buff_locp;
833 			bcopy((char *)he6->h_addr_list[i], buff_locp,
834 			    sizeof (struct in6_addr));
835 			++count;
836 		}
837 		/*
838 		 * Pass 1 (IPv4 mapped addresses):
839 		 */
840 		for (i = 0; he4->h_addr_list[i] != NULL; i++) {
841 			buff_locp -= sizeof (struct in6_addr);
842 			if (buff_locp <=
843 			    (char *)&(host->h_addr_list[count + 1])) {
844 			/*
845 			 * Has to be room for the pointer to the address we're
846 			 * about to add, as well as the final NULL ptr.
847 			 */
848 				return (NULL);
849 			}
850 			/* LINTED pointer cast */
851 			addr6p = (struct in6_addr *)buff_locp;
852 			host->h_addr_list[count] = (char *)addr6p;
853 			bzero(addr6p->s6_addr, sizeof (struct in6_addr));
854 			addr6p->s6_addr[10] = 0xff;
855 			addr6p->s6_addr[11] = 0xff;
856 			bcopy(he4->h_addr_list[i], &addr6p->s6_addr[12],
857 			    sizeof (struct in_addr));
858 			++count;
859 		}
860 		/*
861 		 * Pass 2 (IPv6 aliases, host name first). We start h_aliases
862 		 * one after where h_addr_list array ended. This is where cname
863 		 * is put, followed by all aliases. Reset count to 0, for index
864 		 * in the h_aliases array.
865 		 */
866 		host->h_addr_list[count] = NULL;
867 		host->h_aliases = host->h_addr_list + count + 1;
868 		count = 0;
869 		buff_locp -= (len = strlen(he6->h_name) + 1);
870 		if (buff_locp <= (char *)(host->h_aliases))
871 			return (NULL);
872 		bcopy(he6->h_name, buff_locp, len);
873 		host->h_name = buff_locp;
874 		for (i = 0; he6->h_aliases[i] != NULL; i++) {
875 			buff_locp -= (len = strlen(he6->h_aliases[i]) + 1);
876 			if (buff_locp <=
877 			    (char *)&(host->h_aliases[count + 1])) {
878 			/*
879 			 * Has to be room for the pointer to the address we're
880 			 * about to add, as well as the final NULL ptr.
881 			 */
882 				return (NULL);
883 			}
884 			host->h_aliases[count] = buff_locp;
885 			bcopy((char *)he6->h_aliases[i], buff_locp, len);
886 			++count;
887 		}
888 		/*
889 		 * Pass 2 (IPv4 aliases):
890 		 */
891 		for (i = 0; he4->h_aliases[i] != NULL; i++) {
892 			buff_locp -= (len = strlen(he4->h_aliases[i]) + 1);
893 			if (buff_locp <=
894 			    (char *)&(host->h_aliases[count + 1])) {
895 			/*
896 			 * Has to be room for the pointer to the address we're
897 			 * about to add, as well as the final NULL ptr.
898 			 */
899 				return (NULL);
900 			}
901 			host->h_aliases[count] = buff_locp;
902 			bcopy((char *)he4->h_aliases[i], buff_locp, len);
903 			++count;
904 		}
905 		host->h_aliases[count] = NULL;
906 		host->h_length = sizeof (struct in6_addr);
907 		host->h_addrtype = AF_INET6;
908 		return (host);
909 	}
910 }
911 
912 /*
913  * This routine will convert a mapped v4 hostent (AF_INET6) to a
914  * AF_INET hostent. If no mapped addrs found, then a NULL is returned.
915  * If mapped addrs found, then a new buffer is alloc'd and all the v4 mapped
916  * addresses are extracted and copied to it. On sucess, a pointer to a new
917  * hostent is returned.
918  * There are two possible errors in which case a NULL is returned.
919  * One of two error codes are returned:
920  *
921  * NO_RECOVERY - a malloc failed or the like for which there's no recovery.
922  * NO_ADDRESS - after filtering all the v4, there was nothing left!
923  *
924  * Inputs:
925  *              he              pointer to hostent with mapped v4 addresses
926  *              filter_error    pointer to return error code
927  * Return:
928  *		pointer to a malloc'd hostent with v4 addresses.
929  *
930  * The results are packed into the res->buffer as follows:
931  * <--------------- buffer + buflen -------------------------------------->
932  * |-----------------|-----------------|----------------|----------------|
933  * | pointers vector | pointers vector | aliases grow   | addresses grow |
934  * | for addresses   | for aliases     |                |                |
935  * | this way ->     | this way ->     | <- this way    |<- this way     |
936  * |-----------------|-----------------|----------------|----------------|
937  * | grows in PASS 1 | grows in PASS2  | grows in PASS2 | grows in PASS 1|
938  */
939 struct hostent *
940 __mappedtov4(struct hostent *he, int *extract_error)
941 {
942 	char	*buffer, *limit;
943 	nss_XbyY_buf_t *res;
944 	int	buflen = NSS_BUFLEN_HOSTS;
945 	struct	in_addr *addr4p;
946 	char	*buff_locp;
947 	struct	hostent *host;
948 	int	count = 0, len, i;
949 	char	*h_namep;
950 
951 	if (he == NULL) {
952 		*extract_error = NO_ADDRESS;
953 		return (NULL);
954 	}
955 	if ((__find_mapped(he, 0)) == 0) {
956 		*extract_error = NO_ADDRESS;
957 		return (NULL);
958 	}
959 	if ((res = __IPv6_alloc(NSS_BUFLEN_HOSTS)) == 0) {
960 		*extract_error = NO_RECOVERY;
961 		return (NULL);
962 	}
963 	limit = res->buffer + buflen;
964 	host = (struct hostent *)res->result;
965 	buffer = res->buffer;
966 
967 	buff_locp = (char *)ROUND_DOWN(limit, sizeof (struct in_addr));
968 	host->h_addr_list = (char **)ROUND_UP(buffer, sizeof (char **));
969 	if ((char *)host->h_addr_list >= limit ||
970 	    buff_locp <= (char *)host->h_addr_list)
971 		goto cleanup;
972 	/*
973 	 * "Unmap" the v4 mapped address(es) into a v4 hostent format.
974 	 * This is used for getipnodebyaddr() (single address) or for
975 	 * v4 mapped for getipnodebyname(), which could be multiple
976 	 * addresses. This could also be a literal address string,
977 	 * which is why there is a inet_addr() call.
978 	 */
979 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
980 		/* LINTED pointer cast */
981 		if (!IN6_IS_ADDR_V4MAPPED((struct in6_addr *)
982 		    he->h_addr_list[i]))
983 			continue;
984 		buff_locp -= sizeof (struct in6_addr);
985 		/*
986 		 * Has to be room for the pointer to the address we're
987 		 * about to add, as well as the final NULL ptr.
988 		 */
989 		if (buff_locp <=
990 		    (char *)&(host->h_addr_list[count + 1]))
991 			goto cleanup;
992 		/* LINTED pointer cast */
993 		addr4p = (struct in_addr *)buff_locp;
994 		host->h_addr_list[count] = (char *)addr4p;
995 		bzero((char *)&addr4p->s_addr,
996 		    sizeof (struct in_addr));
997 		/* LINTED pointer cast */
998 		IN6_V4MAPPED_TO_INADDR(
999 		    (struct in6_addr *)he->h_addr_list[i], addr4p);
1000 		++count;
1001 	}
1002 	/*
1003 	 * Set last array element to NULL and add cname as first alias
1004 	 */
1005 	host->h_addr_list[count] = NULL;
1006 	host->h_aliases = host->h_addr_list + count + 1;
1007 	count = 0;
1008 	/* Copy official host name */
1009 	buff_locp -= (len = strlen(he->h_name) + 1);
1010 	h_namep = he->h_name;
1011 	if (buff_locp <= (char *)(host->h_aliases))
1012 		goto cleanup;
1013 	bcopy(h_namep, buff_locp, len);
1014 	host->h_name = buff_locp;
1015 	/*
1016 	 * Pass 2 (IPv4 aliases):
1017 	 */
1018 	if (he->h_aliases != NULL) {
1019 		for (i = 0; he->h_aliases[i] != NULL; i++) {
1020 			buff_locp -= (len = strlen(he->h_aliases[i]) + 1);
1021 			/*
1022 			 * Has to be room for the pointer to the address we're
1023 			 * about to add, as well as the final NULL ptr.
1024 			 */
1025 			if (buff_locp <=
1026 			    (char *)&(host->h_aliases[count + 1]))
1027 				goto cleanup;
1028 			host->h_aliases[count] = buff_locp;
1029 			bcopy((char *)he->h_aliases[i], buff_locp, len);
1030 			++count;
1031 		}
1032 	}
1033 	host->h_aliases[count] = NULL;
1034 	host->h_length = sizeof (struct in_addr);
1035 	host->h_addrtype = AF_INET;
1036 	free(res);
1037 	return (host);
1038 cleanup:
1039 	*extract_error = NO_RECOVERY;
1040 	(void) __IPv6_cleanup(res);
1041 	return (NULL);
1042 }
1043 
1044 /*
1045  * This routine takes as input a pointer to a hostent and filters out
1046  * the type of addresses specified by the af argument.  AF_INET
1047  * indicates that the caller wishes to filter out IPv4-mapped
1048  * addresses, and AF_INET6 indicates that the caller wishes to filter
1049  * out IPv6 addresses which aren't IPv4-mapped.  If filtering would
1050  * result in all addresses being filtered out, a NULL pointer is returned.
1051  * Otherwise, the he pointer passed in is returned, even if no addresses
1052  * were filtered out.
1053  */
1054 static struct hostent *
1055 __filter_addresses(int af, struct hostent *he)
1056 {
1057 	struct in6_addr	**in6addrlist, **in6addr;
1058 	boolean_t	isipv4mapped;
1059 	int		i = 0;
1060 
1061 	if (he == NULL)
1062 		return (NULL);
1063 
1064 	in6addrlist = (struct in6_addr **)he->h_addr_list;
1065 	for (in6addr = in6addrlist; *in6addr != NULL; in6addr++) {
1066 		isipv4mapped = IN6_IS_ADDR_V4MAPPED(*in6addr);
1067 
1068 		if ((af == AF_INET && !isipv4mapped) ||
1069 		    (af == AF_INET6 && isipv4mapped)) {
1070 			if (in6addrlist[i] != *in6addr)
1071 				in6addrlist[i] = *in6addr;
1072 			i++;
1073 		}
1074 	}
1075 
1076 	if (i == 0) {
1077 		/* We filtered everything out. */
1078 		return (NULL);
1079 	} else {
1080 		/* NULL terminate the list and return the hostent */
1081 		in6addrlist[i] = NULL;
1082 		return (he);
1083 	}
1084 }
1085 
1086 /*
1087  * This routine searches a hostent for v4 mapped IPv6 addresses.
1088  * he		hostent structure to seach
1089  * find_both	flag indicating if only want mapped or both map'd and v6
1090  * return values:
1091  * 			0 = No mapped addresses
1092  *			1 = Mapped v4 address found (returns on first one found)
1093  *			2 = Both v6 and v4 mapped are present
1094  *
1095  * If hostent passed in with no addresses, zero will be returned.
1096  */
1097 
1098 static int
1099 __find_mapped(struct hostent *he, int find_both)
1100 {
1101 	int i;
1102 	int mapd_found = 0;
1103 	int v6_found = 0;
1104 
1105 	for (i = 0; he->h_addr_list[i] != NULL; i++) {
1106 		/* LINTED pointer cast */
1107 		if (IN6_IS_ADDR_V4MAPPED(
1108 				(struct in6_addr *)he->h_addr_list[i])) {
1109 			if (find_both)
1110 				mapd_found = 1;
1111 			else
1112 				return (1);
1113 		} else {
1114 			v6_found = 1;
1115 		}
1116 		/* save some iterations once both found */
1117 		if (mapd_found && v6_found)
1118 			return (2);
1119 	}
1120 	return (mapd_found);
1121 }
1122 
1123 /*
1124  * This routine was added specifically for the IPv6 getipnodeby*() APIs. This
1125  * separates the result pointer (ptr to hostent+data buf) from the
1126  * nss_XbyY_buf_t ptr (required for nsswitch API). The returned hostent ptr
1127  * can be passed to freehostent() and freed independently.
1128  *
1129  *   bufp->result    bufp->buffer
1130  *		|		|
1131  *		V		V
1132  *		------------------------------------------------...--
1133  *		|struct hostent	|addresses		     aliases |
1134  *		------------------------------------------------...--
1135  *		|               |<--------bufp->buflen-------------->|
1136  */
1137 
1138 #define	ALIGN(x) ((((long)(x)) + sizeof (long) - 1) & ~(sizeof (long) - 1))
1139 
1140 static nss_XbyY_buf_t *
1141 __IPv6_alloc(int bufsz)
1142 {
1143 	nss_XbyY_buf_t *bufp;
1144 
1145 	if ((bufp = malloc(sizeof (nss_XbyY_buf_t))) == NULL)
1146 		return (NULL);
1147 
1148 	if ((bufp->result = malloc(ALIGN(sizeof (struct hostent)) + bufsz)) ==
1149 	    NULL) {
1150 		free(bufp);
1151 		return (NULL);
1152 	}
1153 	bufp->buffer = (char *)(bufp->result) + sizeof (struct hostent);
1154 	bufp->buflen = bufsz;
1155 	return (bufp);
1156 }
1157 
1158 /*
1159  * This routine is use only for error return cleanup. This will free the
1160  * hostent pointer, so don't use for successful returns.
1161  */
1162 static void
1163 __IPv6_cleanup(nss_XbyY_buf_t *bufp)
1164 {
1165 	if (bufp == NULL)
1166 		return;
1167 	if (bufp->result != NULL)
1168 		free(bufp->result);
1169 	free(bufp);
1170 }
1171