xref: /freebsd/lib/libc/net/name6.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*	$KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 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  * ++Copyright++ 1985, 1988, 1993
35  * -
36  * Copyright (c) 1985, 1988, 1993
37  *    The Regents of the University of California.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the University nor the names of its contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  * -
63  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
64  *
65  * Permission to use, copy, modify, and distribute this software for any
66  * purpose with or without fee is hereby granted, provided that the above
67  * copyright notice and this permission notice appear in all copies, and that
68  * the name of Digital Equipment Corporation not be used in advertising or
69  * publicity pertaining to distribution of the document or software without
70  * specific, written prior permission.
71  *
72  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
73  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
74  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
75  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
76  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
77  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
78  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
79  * SOFTWARE.
80  * -
81  * --Copyright--
82  */
83 
84 /*
85  *	Atsushi Onoe <onoe@sm.sony.co.jp>
86  */
87 
88 #include "namespace.h"
89 #include <sys/param.h>
90 #include <sys/socket.h>
91 #include <sys/time.h>
92 #include <sys/queue.h>
93 #include <netinet/in.h>
94 #ifdef INET6
95 #include <net/if.h>
96 #include <sys/sysctl.h>
97 #include <sys/ioctl.h>
98 #include <netinet6/in6_var.h>	/* XXX */
99 #endif
100 
101 #include <arpa/inet.h>
102 #include <arpa/nameser.h>
103 
104 #include <errno.h>
105 #include <netdb.h>
106 #include <resolv.h>
107 #include <stdio.h>
108 #include <stdlib.h>
109 #include <string.h>
110 #include <stdarg.h>
111 #include <nsswitch.h>
112 #include <unistd.h>
113 #include "un-namespace.h"
114 #include "netdb_private.h"
115 #include "res_private.h"
116 
117 #ifndef MAXALIASES
118 #define	MAXALIASES	10
119 #endif
120 #ifndef	MAXADDRS
121 #define	MAXADDRS	20
122 #endif
123 #ifndef MAXDNAME
124 #define	MAXDNAME	1025
125 #endif
126 
127 #ifdef INET6
128 #define	ADDRLEN(af)	((af) == AF_INET6 ? sizeof(struct in6_addr) : \
129 					    sizeof(struct in_addr))
130 #else
131 #define	ADDRLEN(af)	sizeof(struct in_addr)
132 #endif
133 
134 #define	MAPADDR(ab, ina) \
135 do {									\
136 	memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr));		\
137 	memset((ab)->map_zero, 0, sizeof((ab)->map_zero));		\
138 	memset((ab)->map_one, 0xff, sizeof((ab)->map_one));		\
139 } while (0)
140 #define	MAPADDRENABLED(flags) \
141 	(((flags) & AI_V4MAPPED) || \
142 	 (((flags) & AI_V4MAPPED_CFG)))
143 
144 union inx_addr {
145 	struct in_addr	in_addr;
146 #ifdef INET6
147 	struct in6_addr	in6_addr;
148 #endif
149 	struct {
150 		u_char	mau_zero[10];
151 		u_char	mau_one[2];
152 		struct in_addr mau_inaddr;
153 	}		map_addr_un;
154 #define	map_zero	map_addr_un.mau_zero
155 #define	map_one		map_addr_un.mau_one
156 #define	map_inaddr	map_addr_un.mau_inaddr
157 };
158 
159 struct policyqueue {
160 	TAILQ_ENTRY(policyqueue) pc_entry;
161 #ifdef INET6
162 	struct in6_addrpolicy pc_policy;
163 #endif
164 };
165 TAILQ_HEAD(policyhead, policyqueue);
166 
167 #define AIO_SRCFLAG_DEPRECATED	0x1
168 
169 struct hp_order {
170 	union {
171 		struct sockaddr_storage aiou_ss;
172 		struct sockaddr aiou_sa;
173 	} aio_src_un;
174 #define aio_srcsa aio_src_un.aiou_sa
175 	u_int32_t aio_srcflag;
176 	int aio_srcscope;
177 	int aio_dstscope;
178 	struct policyqueue *aio_srcpolicy;
179 	struct policyqueue *aio_dstpolicy;
180 	union {
181 		struct sockaddr_storage aiou_ss;
182 		struct sockaddr aiou_sa;
183 	} aio_un;
184 #define aio_sa aio_un.aiou_sa
185 	int aio_matchlen;
186 	char *aio_h_addr;
187 	int aio_initial_sequence;
188 };
189 
190 static struct	 hostent *_hpcopy(struct hostent *, int *);
191 static struct	 hostent *_hpaddr(int, const char *, void *, int *);
192 #ifdef INET6
193 static struct	 hostent *_hpmerge(struct hostent *, struct hostent *, int *);
194 static struct	 hostent *_hpmapv6(struct hostent *, int *);
195 #endif
196 static struct	 hostent *_hpsort(struct hostent *, res_state);
197 
198 #ifdef INET6
199 static struct	 hostent *_hpreorder(struct hostent *);
200 static int	 get_addrselectpolicy(struct policyhead *);
201 static void	 free_addrselectpolicy(struct policyhead *);
202 static struct	 policyqueue *match_addrselectpolicy(struct sockaddr *,
203 	struct policyhead *);
204 static void	 set_source(struct hp_order *, struct policyhead *);
205 static int	 matchlen(struct sockaddr *, struct sockaddr *);
206 static int	 comp_dst(const void *, const void *);
207 static int	 gai_addr2scopetype(struct sockaddr *);
208 #endif
209 
210 /*
211  * Functions defined in RFC2553
212  *	getipnodebyname, getipnodebyaddr, freehostent
213  */
214 
215 struct hostent *
216 getipnodebyname(const char *name, int af, int flags, int *errp)
217 {
218 	struct hostent *hp;
219 	union inx_addr addrbuf;
220 	res_state statp;
221 	u_long options;
222 
223 	switch (af) {
224 	case AF_INET:
225 #ifdef INET6
226 	case AF_INET6:
227 #endif
228 		break;
229 	default:
230 		*errp = NO_RECOVERY;
231 		return NULL;
232 	}
233 
234 	if (flags & AI_ADDRCONFIG) {
235 		int s;
236 
237 		if ((s = _socket(af, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
238 			return NULL;
239 		/*
240 		 * TODO:
241 		 * Note that implementation dependent test for address
242 		 * configuration should be done every time called
243 		 * (or appropriate interval),
244 		 * because addresses will be dynamically assigned or deleted.
245 		 */
246 		_close(s);
247 	}
248 
249 #ifdef INET6
250 	/* special case for literal address */
251 	if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
252 		if (af != AF_INET6) {
253 			*errp = HOST_NOT_FOUND;
254 			return NULL;
255 		}
256 		return _hpaddr(af, name, &addrbuf, errp);
257 	}
258 #endif
259 	if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
260 		if (af != AF_INET) {
261 			if (MAPADDRENABLED(flags)) {
262 				MAPADDR(&addrbuf, &addrbuf.in_addr);
263 			} else {
264 				*errp = HOST_NOT_FOUND;
265 				return NULL;
266 			}
267 		}
268 		return _hpaddr(af, name, &addrbuf, errp);
269 	}
270 
271 
272 	statp = __res_state();
273 	if ((statp->options & RES_INIT) == 0) {
274 		if (res_ninit(statp) < 0) {
275 			*errp = NETDB_INTERNAL;
276 			return NULL;
277 		}
278 	}
279 
280 	options = statp->options;
281 	statp->options &= ~RES_USE_INET6;
282 
283 	hp = gethostbyname2(name, af);
284 	hp = _hpcopy(hp, errp);
285 #ifdef INET6
286 	if (af == AF_INET6)
287 		hp = _hpreorder(hp);
288 
289 	if (af == AF_INET6 && ((flags & AI_ALL) || hp == NULL) &&
290 	    MAPADDRENABLED(flags)) {
291 		struct hostent *hp2 = gethostbyname2(name, AF_INET);
292 		if (hp == NULL)
293 			if (hp2 == NULL)
294 				*errp = statp->res_h_errno;
295 			else
296 				hp = _hpmapv6(hp2, errp);
297 		else {
298 			if (hp2 && strcmp(hp->h_name, hp2->h_name) == 0) {
299 				struct hostent *hpb = hp;
300 				hp = _hpmerge(hpb, hp2, errp);
301 				freehostent(hpb);
302 			}
303 		}
304 	}
305 #endif
306 
307 	if (hp == NULL)
308 		*errp = statp->res_h_errno;
309 
310 	statp->options = options;
311 	return _hpsort(hp, statp);
312 }
313 
314 struct hostent *
315 getipnodebyaddr(const void *src, size_t len, int af, int *errp)
316 {
317 	struct hostent *hp;
318 	res_state statp;
319 	u_long options;
320 
321 #ifdef INET6
322 	struct in6_addr addrbuf;
323 #else
324 	struct in_addr addrbuf;
325 #endif
326 
327 	switch (af) {
328 	case AF_INET:
329 		if (len != sizeof(struct in_addr)) {
330 			*errp = NO_RECOVERY;
331 			return NULL;
332 		}
333 		if (rounddown2((long)src, sizeof(struct in_addr))) {
334 			memcpy(&addrbuf, src, len);
335 			src = &addrbuf;
336 		}
337 		if (((struct in_addr *)src)->s_addr == 0)
338 			return NULL;
339 		break;
340 #ifdef INET6
341 	case AF_INET6:
342 		if (len != sizeof(struct in6_addr)) {
343 			*errp = NO_RECOVERY;
344 			return NULL;
345 		}
346 		if (rounddown2((long)src, sizeof(struct in6_addr) / 2)) {
347 			/* XXX */
348 			memcpy(&addrbuf, src, len);
349 			src = &addrbuf;
350 		}
351 		if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
352 			return NULL;
353 		if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
354 		||  IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
355 			src = (char *)src +
356 			    (sizeof(struct in6_addr) - sizeof(struct in_addr));
357 			af = AF_INET;
358 			len = sizeof(struct in_addr);
359 		}
360 		break;
361 #endif
362 	default:
363 		*errp = NO_RECOVERY;
364 		return NULL;
365 	}
366 
367 	statp = __res_state();
368 	if ((statp->options & RES_INIT) == 0) {
369 		if (res_ninit(statp) < 0) {
370 			RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
371 			return NULL;
372 		}
373 	}
374 
375 	options = statp->options;
376 	statp->options &= ~RES_USE_INET6;
377 
378 	hp = gethostbyaddr(src, len, af);
379 	if (hp == NULL)
380 		*errp = statp->res_h_errno;
381 
382 	statp->options = options;
383 	return (_hpcopy(hp, errp));
384 }
385 
386 void
387 freehostent(struct hostent *ptr)
388 {
389 	free(ptr);
390 }
391 
392 /*
393  * Private utility functions
394  */
395 
396 /*
397  * _hpcopy: allocate and copy hostent structure
398  */
399 static struct hostent *
400 _hpcopy(struct hostent *hp, int *errp)
401 {
402 	struct hostent *nhp;
403 	char *cp, **pp;
404 	int size, addrsize;
405 	int nalias = 0, naddr = 0;
406 	int al_off;
407 	int i;
408 
409 	if (hp == NULL)
410 		return hp;
411 
412 	/* count size to be allocated */
413 	size = sizeof(struct hostent);
414 	if (hp->h_name != NULL)
415 		size += strlen(hp->h_name) + 1;
416 	if ((pp = hp->h_aliases) != NULL) {
417 		for (i = 0; *pp != NULL; i++, pp++) {
418 			if (**pp != '\0') {
419 				size += strlen(*pp) + 1;
420 				nalias++;
421 			}
422 		}
423 	}
424 	/* adjust alignment */
425 	size = ALIGN(size);
426 	al_off = size;
427 	size += sizeof(char *) * (nalias + 1);
428 	addrsize = ALIGN(hp->h_length);
429 	if ((pp = hp->h_addr_list) != NULL) {
430 		while (*pp++ != NULL)
431 			naddr++;
432 	}
433 	size += addrsize * naddr;
434 	size += sizeof(char *) * (naddr + 1);
435 
436 	/* copy */
437 	if ((nhp = (struct hostent *)malloc(size)) == NULL) {
438 		*errp = TRY_AGAIN;
439 		return NULL;
440 	}
441 	cp = (char *)&nhp[1];
442 	if (hp->h_name != NULL) {
443 		nhp->h_name = cp;
444 		strcpy(cp, hp->h_name);
445 		cp += strlen(cp) + 1;
446 	} else
447 		nhp->h_name = NULL;
448 	nhp->h_aliases = (char **)((char *)nhp + al_off);
449 	if ((pp = hp->h_aliases) != NULL) {
450 		for (i = 0; *pp != NULL; pp++) {
451 			if (**pp != '\0') {
452 				nhp->h_aliases[i++] = cp;
453 				strcpy(cp, *pp);
454 				cp += strlen(cp) + 1;
455 			}
456 		}
457 	}
458 	nhp->h_aliases[nalias] = NULL;
459 	cp = (char *)&nhp->h_aliases[nalias + 1];
460 	nhp->h_addrtype = hp->h_addrtype;
461 	nhp->h_length = hp->h_length;
462 	nhp->h_addr_list = (char **)cp;
463 	if ((pp = hp->h_addr_list) != NULL) {
464 		cp = (char *)&nhp->h_addr_list[naddr + 1];
465 		for (i = 0; *pp != NULL; pp++) {
466 			nhp->h_addr_list[i++] = cp;
467 			memcpy(cp, *pp, hp->h_length);
468 			cp += addrsize;
469 		}
470 	}
471 	nhp->h_addr_list[naddr] = NULL;
472 	return nhp;
473 }
474 
475 /*
476  * _hpaddr: construct hostent structure with one address
477  */
478 static struct hostent *
479 _hpaddr(int af, const char *name, void *addr, int *errp)
480 {
481 	struct hostent *hp, hpbuf;
482 	char *addrs[2];
483 
484 	hp = &hpbuf;
485 	hp->h_name = (char *)name;
486 	hp->h_aliases = NULL;
487 	hp->h_addrtype = af;
488 	hp->h_length = ADDRLEN(af);
489 	hp->h_addr_list = addrs;
490 	addrs[0] = (char *)addr;
491 	addrs[1] = NULL;
492 	return (_hpcopy(hp, errp));
493 }
494 
495 #ifdef INET6
496 /*
497  * _hpmerge: merge 2 hostent structure, arguments will be freed
498  */
499 static struct hostent *
500 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
501 {
502 	int i, j;
503 	int naddr, nalias;
504 	char **pp;
505 	struct hostent *hp, hpbuf;
506 	char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
507 	union inx_addr addrbuf[MAXADDRS];
508 
509 	if (hp1 == NULL)
510 		return _hpcopy(hp2, errp);
511 	if (hp2 == NULL)
512 		return _hpcopy(hp1, errp);
513 
514 #define	HP(i)	(i == 1 ? hp1 : hp2)
515 	hp = &hpbuf;
516 	hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
517 	hp->h_aliases = aliases;
518 	nalias = 0;
519 	for (i = 1; i <= 2; i++) {
520 		if ((pp = HP(i)->h_aliases) == NULL)
521 			continue;
522 		for (; nalias < MAXALIASES && *pp != NULL; pp++) {
523 			/* check duplicates */
524 			for (j = 0; j < nalias; j++)
525 				if (strcasecmp(*pp, aliases[j]) == 0)
526 					break;
527 			if (j == nalias)
528 				aliases[nalias++] = *pp;
529 		}
530 	}
531 	aliases[nalias] = NULL;
532 	if (hp1->h_length != hp2->h_length) {
533 		hp->h_addrtype = AF_INET6;
534 		hp->h_length = sizeof(struct in6_addr);
535 	} else {
536 		hp->h_addrtype = hp1->h_addrtype;
537 		hp->h_length = hp1->h_length;
538 	}
539 
540 	hp->h_addr_list = addrs;
541 	naddr = 0;
542 	for (i = 1; i <= 2; i++) {
543 		if ((pp = HP(i)->h_addr_list) == NULL)
544 			continue;
545 		if (HP(i)->h_length == hp->h_length) {
546 			while (naddr < MAXADDRS && *pp != NULL)
547 				addrs[naddr++] = *pp++;
548 		} else {
549 			/* copy IPv4 addr as mapped IPv6 addr */
550 			while (naddr < MAXADDRS && *pp != NULL) {
551 				MAPADDR(&addrbuf[naddr], *pp++);
552 				addrs[naddr] = (char *)&addrbuf[naddr];
553 				naddr++;
554 			}
555 		}
556 	}
557 	addrs[naddr] = NULL;
558 	return (_hpcopy(hp, errp));
559 }
560 #endif
561 
562 /*
563  * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
564  */
565 #ifdef INET6
566 static struct hostent *
567 _hpmapv6(struct hostent *hp, int *errp)
568 {
569 	struct hostent hp6;
570 
571 	if (hp == NULL)
572 		return NULL;
573 	if (hp->h_addrtype == AF_INET6)
574 		return _hpcopy(hp, errp);
575 
576 	memset(&hp6, 0, sizeof(struct hostent));
577 	hp6.h_addrtype = AF_INET6;
578 	hp6.h_length = sizeof(struct in6_addr);
579 	return _hpmerge(&hp6, hp, errp);
580 }
581 #endif
582 
583 /*
584  * _hpsort: sort address by sortlist
585  */
586 static struct hostent *
587 _hpsort(struct hostent *hp, res_state statp)
588 {
589 	int i, j, n;
590 	u_char *ap, *sp, *mp, **pp;
591 	char t;
592 	char order[MAXADDRS];
593 	int nsort = statp->nsort;
594 
595 	if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
596 		return hp;
597 	for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
598 		for (j = 0; j < nsort; j++) {
599 #ifdef INET6
600 			if (statp->_u._ext.ext->sort_list[j].af !=
601 			    hp->h_addrtype)
602 				continue;
603 			sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
604 			mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
605 #else
606 			sp = (u_char *)&statp->sort_list[j].addr;
607 			mp = (u_char *)&statp->sort_list[j].mask;
608 #endif
609 			for (n = 0; n < hp->h_length; n++) {
610 				if ((ap[n] & mp[n]) != sp[n])
611 					break;
612 			}
613 			if (n == hp->h_length)
614 				break;
615 		}
616 		order[i] = j;
617 	}
618 	n = i;
619 	pp = (u_char **)hp->h_addr_list;
620 	for (i = 0; i < n - 1; i++) {
621 		for (j = i + 1; j < n; j++) {
622 			if (order[i] > order[j]) {
623 				ap = pp[i];
624 				pp[i] = pp[j];
625 				pp[j] = ap;
626 				t = order[i];
627 				order[i] = order[j];
628 				order[j] = t;
629 			}
630 		}
631 	}
632 	return hp;
633 }
634 
635 #ifdef INET6
636 /*
637  * _hpreorder: sort address by default address selection
638  */
639 static struct hostent *
640 _hpreorder(struct hostent *hp)
641 {
642 	struct hp_order *aio;
643 	int i, n;
644 	char *ap;
645 	struct sockaddr *sa;
646 	struct policyhead policyhead;
647 
648 	if (hp == NULL)
649 		return hp;
650 
651 	switch (hp->h_addrtype) {
652 	case AF_INET:
653 #ifdef INET6
654 	case AF_INET6:
655 #endif
656 		break;
657 	default:
658 		return hp;
659 	}
660 
661 	/* count the number of addrinfo elements for sorting. */
662 	for (n = 0; hp->h_addr_list[n] != NULL; n++)
663 		;
664 
665 	/*
666 	 * If the number is small enough, we can skip the reordering process.
667 	 */
668 	if (n <= 1)
669 		return hp;
670 
671 	/* allocate a temporary array for sort and initialization of it. */
672 	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
673 		return hp;	/* give up reordering */
674 	memset(aio, 0, sizeof(*aio) * n);
675 
676 	/* retrieve address selection policy from the kernel */
677 	TAILQ_INIT(&policyhead);
678 	if (!get_addrselectpolicy(&policyhead)) {
679 		/* no policy is installed into kernel, we don't sort. */
680 		free(aio);
681 		return hp;
682 	}
683 
684 	for (i = 0; i < n; i++) {
685 		ap = hp->h_addr_list[i];
686 		aio[i].aio_h_addr = ap;
687 		sa = &aio[i].aio_sa;
688 		switch (hp->h_addrtype) {
689 		case AF_INET:
690 			sa->sa_family = AF_INET;
691 			sa->sa_len = sizeof(struct sockaddr_in);
692 			memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
693 			    sizeof(struct in_addr));
694 			break;
695 #ifdef INET6
696 		case AF_INET6:
697 			if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
698 				sa->sa_family = AF_INET;
699 				sa->sa_len = sizeof(struct sockaddr_in);
700 				memcpy(&((struct sockaddr_in *)sa)->sin_addr,
701 				    &ap[12], sizeof(struct in_addr));
702 			} else {
703 				sa->sa_family = AF_INET6;
704 				sa->sa_len = sizeof(struct sockaddr_in6);
705 				memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
706 				    ap, sizeof(struct in6_addr));
707 			}
708 			break;
709 #endif
710 		}
711 		aio[i].aio_dstscope = gai_addr2scopetype(sa);
712 		aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
713 		set_source(&aio[i], &policyhead);
714 		aio[i].aio_initial_sequence = i;
715 	}
716 
717 	/* perform sorting. */
718 	qsort(aio, n, sizeof(*aio), comp_dst);
719 
720 	/* reorder the h_addr_list. */
721 	for (i = 0; i < n; i++)
722 		hp->h_addr_list[i] = aio[i].aio_h_addr;
723 
724 	/* cleanup and return */
725 	free(aio);
726 	free_addrselectpolicy(&policyhead);
727 	return hp;
728 }
729 
730 static int
731 get_addrselectpolicy(struct policyhead *head)
732 {
733 #ifdef INET6
734 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
735 	size_t l;
736 	char *buf;
737 	struct in6_addrpolicy *pol, *ep;
738 
739 	if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0)
740 		return (0);
741 	if ((buf = malloc(l)) == NULL)
742 		return (0);
743 	if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
744 		free(buf);
745 		return (0);
746 	}
747 
748 	ep = (struct in6_addrpolicy *)(buf + l);
749 	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
750 		struct policyqueue *new;
751 
752 		if ((new = malloc(sizeof(*new))) == NULL) {
753 			free_addrselectpolicy(head); /* make the list empty */
754 			break;
755 		}
756 		new->pc_policy = *pol;
757 		TAILQ_INSERT_TAIL(head, new, pc_entry);
758 	}
759 
760 	free(buf);
761 	return (1);
762 #else
763 	return (0);
764 #endif
765 }
766 
767 static void
768 free_addrselectpolicy(struct policyhead *head)
769 {
770 	struct policyqueue *ent, *nent;
771 
772 	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
773 		nent = TAILQ_NEXT(ent, pc_entry);
774 		TAILQ_REMOVE(head, ent, pc_entry);
775 		free(ent);
776 	}
777 }
778 
779 static struct policyqueue *
780 match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
781 {
782 #ifdef INET6
783 	struct policyqueue *ent, *bestent = NULL;
784 	struct in6_addrpolicy *pol;
785 	int matchlen, bestmatchlen = -1;
786 	u_char *mp, *ep, *k, *p, m;
787 	struct sockaddr_in6 key;
788 
789 	switch(addr->sa_family) {
790 	case AF_INET6:
791 		key = *(struct sockaddr_in6 *)addr;
792 		break;
793 	case AF_INET:
794 		/* convert the address into IPv4-mapped IPv6 address. */
795 		memset(&key, 0, sizeof(key));
796 		key.sin6_family = AF_INET6;
797 		key.sin6_len = sizeof(key);
798 		_map_v4v6_address(
799 		    (char *)&((struct sockaddr_in *)addr)->sin_addr,
800 		    (char *)&key.sin6_addr);
801 		break;
802 	default:
803 		return(NULL);
804 	}
805 
806 	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
807 		pol = &ent->pc_policy;
808 		matchlen = 0;
809 
810 		mp = (u_char *)&pol->addrmask.sin6_addr;
811 		ep = mp + 16;	/* XXX: scope field? */
812 		k = (u_char *)&key.sin6_addr;
813 		p = (u_char *)&pol->addr.sin6_addr;
814 		for (; mp < ep && *mp; mp++, k++, p++) {
815 			m = *mp;
816 			if ((*k & m) != *p)
817 				goto next; /* not match */
818 			if (m == 0xff) /* short cut for a typical case */
819 				matchlen += 8;
820 			else {
821 				while (m >= 0x80) {
822 					matchlen++;
823 					m <<= 1;
824 				}
825 			}
826 		}
827 
828 		/* matched.  check if this is better than the current best. */
829 		if (matchlen > bestmatchlen) {
830 			bestent = ent;
831 			bestmatchlen = matchlen;
832 		}
833 
834 	  next:
835 		continue;
836 	}
837 
838 	return(bestent);
839 #else
840 	return(NULL);
841 #endif
842 
843 }
844 
845 static void
846 set_source(struct hp_order *aio, struct policyhead *ph)
847 {
848 	struct sockaddr_storage ss = aio->aio_un.aiou_ss;
849 	socklen_t srclen;
850 	int s;
851 
852 	/* set unspec ("no source is available"), just in case */
853 	aio->aio_srcsa.sa_family = AF_UNSPEC;
854 	aio->aio_srcscope = -1;
855 
856 	switch(ss.ss_family) {
857 	case AF_INET:
858 		((struct sockaddr_in *)&ss)->sin_port = htons(1);
859 		break;
860 #ifdef INET6
861 	case AF_INET6:
862 		((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
863 		break;
864 #endif
865 	default:		/* ignore unsupported AFs explicitly */
866 		return;
867 	}
868 
869 	/* open a socket to get the source address for the given dst */
870 	if ((s = _socket(ss.ss_family, SOCK_DGRAM | SOCK_CLOEXEC,
871 	    IPPROTO_UDP)) < 0)
872 		return;		/* give up */
873 	if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
874 		goto cleanup;
875 	srclen = ss.ss_len;
876 	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
877 		aio->aio_srcsa.sa_family = AF_UNSPEC;
878 		goto cleanup;
879 	}
880 	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
881 	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
882 	aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
883 #ifdef INET6
884 	if (ss.ss_family == AF_INET6) {
885 		struct in6_ifreq ifr6;
886 		u_int32_t flags6;
887 
888 		memset(&ifr6, 0, sizeof(ifr6));
889 		memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
890 		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
891 			flags6 = ifr6.ifr_ifru.ifru_flags6;
892 			if ((flags6 & IN6_IFF_DEPRECATED))
893 				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
894 		}
895 	}
896 #endif
897 
898   cleanup:
899 	_close(s);
900 	return;
901 }
902 
903 static int
904 matchlen(struct sockaddr *src, struct sockaddr *dst)
905 {
906 	int match = 0;
907 	u_char *s, *d;
908 	u_char *lim, r;
909 	int addrlen;
910 
911 	switch (src->sa_family) {
912 #ifdef INET6
913 	case AF_INET6:
914 		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
915 		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
916 		addrlen = sizeof(struct in6_addr);
917 		lim = s + addrlen;
918 		break;
919 #endif
920 	case AF_INET:
921 		s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
922 		d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
923 		addrlen = sizeof(struct in_addr);
924 		lim = s + addrlen;
925 		break;
926 	default:
927 		return(0);
928 	}
929 
930 	while (s < lim)
931 		if ((r = (*d++ ^ *s++)) != 0) {
932 			while ((r & 0x80) == 0) {
933 				match++;
934 				r <<= 1;
935 			}
936 			break;
937 		} else
938 			match += 8;
939 	return(match);
940 }
941 
942 static int
943 comp_dst(const void *arg1, const void *arg2)
944 {
945 	const struct hp_order *dst1 = arg1, *dst2 = arg2;
946 
947 	/*
948 	 * Rule 1: Avoid unusable destinations.
949 	 * XXX: we currently do not consider if an appropriate route exists.
950 	 */
951 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
952 	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
953 		return(-1);
954 	}
955 	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
956 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
957 		return(1);
958 	}
959 
960 	/* Rule 2: Prefer matching scope. */
961 	if (dst1->aio_dstscope == dst1->aio_srcscope &&
962 	    dst2->aio_dstscope != dst2->aio_srcscope) {
963 		return(-1);
964 	}
965 	if (dst1->aio_dstscope != dst1->aio_srcscope &&
966 	    dst2->aio_dstscope == dst2->aio_srcscope) {
967 		return(1);
968 	}
969 
970 	/* Rule 3: Avoid deprecated addresses. */
971 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
972 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
973 		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
974 		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
975 			return(-1);
976 		}
977 		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
978 		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
979 			return(1);
980 		}
981 	}
982 
983 	/* Rule 4: Prefer home addresses. */
984 	/* XXX: not implemented yet */
985 
986 	/* Rule 5: Prefer matching label. */
987 #ifdef INET6
988 	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
989 	    dst1->aio_srcpolicy->pc_policy.label ==
990 	    dst1->aio_dstpolicy->pc_policy.label &&
991 	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
992 	     dst2->aio_srcpolicy->pc_policy.label !=
993 	     dst2->aio_dstpolicy->pc_policy.label)) {
994 		return(-1);
995 	}
996 	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
997 	    dst2->aio_srcpolicy->pc_policy.label ==
998 	    dst2->aio_dstpolicy->pc_policy.label &&
999 	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1000 	     dst1->aio_srcpolicy->pc_policy.label !=
1001 	     dst1->aio_dstpolicy->pc_policy.label)) {
1002 		return(1);
1003 	}
1004 #endif
1005 
1006 	/* Rule 6: Prefer higher precedence. */
1007 #ifdef INET6
1008 	if (dst1->aio_dstpolicy &&
1009 	    (dst2->aio_dstpolicy == NULL ||
1010 	     dst1->aio_dstpolicy->pc_policy.preced >
1011 	     dst2->aio_dstpolicy->pc_policy.preced)) {
1012 		return(-1);
1013 	}
1014 	if (dst2->aio_dstpolicy &&
1015 	    (dst1->aio_dstpolicy == NULL ||
1016 	     dst2->aio_dstpolicy->pc_policy.preced >
1017 	     dst1->aio_dstpolicy->pc_policy.preced)) {
1018 		return(1);
1019 	}
1020 #endif
1021 
1022 	/* Rule 7: Prefer native transport. */
1023 	/* XXX: not implemented yet */
1024 
1025 	/* Rule 8: Prefer smaller scope. */
1026 	if (dst1->aio_dstscope >= 0 &&
1027 	    dst1->aio_dstscope < dst2->aio_dstscope) {
1028 		return(-1);
1029 	}
1030 	if (dst2->aio_dstscope >= 0 &&
1031 	    dst2->aio_dstscope < dst1->aio_dstscope) {
1032 		return(1);
1033 	}
1034 
1035 	/*
1036 	 * Rule 9: Use longest matching prefix.
1037 	 * We compare the match length in a same AF only.
1038 	 */
1039 	if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1040 		if (dst1->aio_matchlen > dst2->aio_matchlen) {
1041 			return(-1);
1042 		}
1043 		if (dst1->aio_matchlen < dst2->aio_matchlen) {
1044 			return(1);
1045 		}
1046 	}
1047 
1048 	/* Rule 10: Otherwise, leave the order unchanged. */
1049 
1050 	/*
1051 	 * Note that qsort is unstable; so, we can't return zero and
1052 	 * expect the order to be unchanged.
1053 	 * That also means we can't depend on the current position of
1054 	 * dst2 being after dst1.  We must enforce the initial order
1055 	 * with an explicit compare on the original position.
1056 	 * The qsort specification requires that "When the same objects
1057 	 * (consisting of width bytes, irrespective of their current
1058 	 * positions in the array) are passed more than once to the
1059 	 * comparison function, the results shall be consistent with one
1060 	 * another."
1061 	 * In other words, If A < B, then we must also return B > A.
1062 	 */
1063 	if (dst2->aio_initial_sequence < dst1->aio_initial_sequence)
1064 		return(1);
1065 
1066 	return(-1);
1067 }
1068 
1069 /*
1070  * Copy from scope.c.
1071  * XXX: we should standardize the functions and link them as standard
1072  * library.
1073  */
1074 static int
1075 gai_addr2scopetype(struct sockaddr *sa)
1076 {
1077 #ifdef INET6
1078 	struct sockaddr_in6 *sa6;
1079 #endif
1080 	struct sockaddr_in *sa4;
1081 
1082 	switch(sa->sa_family) {
1083 #ifdef INET6
1084 	case AF_INET6:
1085 		sa6 = (struct sockaddr_in6 *)sa;
1086 		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1087 			/* just use the scope field of the multicast address */
1088 			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1089 		}
1090 		/*
1091 		 * Unicast addresses: map scope type to corresponding scope
1092 		 * value defined for multcast addresses.
1093 		 * XXX: hardcoded scope type values are bad...
1094 		 */
1095 		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1096 			return(1); /* node local scope */
1097 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1098 			return(2); /* link-local scope */
1099 		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1100 			return(5); /* site-local scope */
1101 		return(14);	/* global scope */
1102 		break;
1103 #endif
1104 	case AF_INET:
1105 		/*
1106 		 * IPv4 pseudo scoping according to RFC 3484.
1107 		 */
1108 		sa4 = (struct sockaddr_in *)sa;
1109 		/* IPv4 autoconfiguration addresses have link-local scope. */
1110 		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1111 		    ((u_char *)&sa4->sin_addr)[1] == 254)
1112 			return(2);
1113 		/* Private addresses have site-local scope. */
1114 		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1115 		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
1116 		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1117 		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
1118 		     ((u_char *)&sa4->sin_addr)[1] == 168))
1119 			return(14);	/* XXX: It should be 5 unless NAT */
1120 		/* Loopback addresses have link-local scope. */
1121 		if (((u_char *)&sa4->sin_addr)[0] == 127)
1122 			return(2);
1123 		return(14);
1124 		break;
1125 	default:
1126 		errno = EAFNOSUPPORT; /* is this a good error? */
1127 		return(-1);
1128 	}
1129 }
1130 #endif
1131