b41ca4c5911ddb9d0a97bd6f4f175878c8699198
[dragonfly.git] / lib / libc / net / name6.c
1 /*      $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $  */
2
3 /*
4  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*
32  * ++Copyright++ 1985, 1988, 1993
33  * -
34  * Copyright (c) 1985, 1988, 1993
35  *    The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *      This product includes software developed by the University of
48  *      California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  * -
65  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
66  *
67  * Permission to use, copy, modify, and distribute this software for any
68  * purpose with or without fee is hereby granted, provided that the above
69  * copyright notice and this permission notice appear in all copies, and that
70  * the name of Digital Equipment Corporation not be used in advertising or
71  * publicity pertaining to distribution of the document or software without
72  * specific, written prior permission.
73  *
74  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
77  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81  * SOFTWARE.
82  * -
83  * --Copyright--
84  *
85  * $FreeBSD: src/lib/libc/net/name6.c,v 1.62 2007/07/31 16:09:41 bushman Exp $
86  */
87
88 /*
89  *      Atsushi Onoe <onoe@sm.sony.co.jp>
90  */
91
92 #include "namespace.h"
93 #include <sys/param.h>
94 #include <sys/socket.h>
95 #include <sys/time.h>
96 #include <sys/queue.h>
97 #include <netinet/in.h>
98 #ifdef INET6
99 #include <net/if.h>
100 #include <net/if_var.h>
101 #include <sys/sysctl.h>
102 #include <sys/ioctl.h>
103 #include <netinet6/in6_var.h>   /* XXX */
104 #endif
105
106 #include <arpa/inet.h>
107 #include <arpa/nameser.h>
108
109 #include <errno.h>
110 #include <netdb.h>
111 #include <resolv.h>
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115 #include <stdarg.h>
116 #include <nsswitch.h>
117 #include <unistd.h>
118 #include "un-namespace.h"
119 #include "netdb_private.h"
120 #include "res_private.h"
121
122 #ifndef MAXALIASES
123 #define MAXALIASES      10
124 #endif
125 #ifndef MAXADDRS
126 #define MAXADDRS        20
127 #endif
128 #ifndef MAXDNAME
129 #define MAXDNAME        1025
130 #endif
131
132 #ifdef INET6
133 #define ADDRLEN(af)     ((af) == AF_INET6 ? sizeof(struct in6_addr) : \
134                                             sizeof(struct in_addr))
135 #else
136 #define ADDRLEN(af)     sizeof(struct in_addr)
137 #endif
138
139 #define MAPADDR(ab, ina) \
140 do {                                                                    \
141         memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr));         \
142         memset((ab)->map_zero, 0, sizeof((ab)->map_zero));              \
143         memset((ab)->map_one, 0xff, sizeof((ab)->map_one));             \
144 } while (0)
145 #define MAPADDRENABLED(flags) \
146         (((flags) & AI_V4MAPPED) || \
147          (((flags) & AI_V4MAPPED_CFG)))
148
149 union inx_addr {
150         struct in_addr  in_addr;
151 #ifdef INET6
152         struct in6_addr in6_addr;
153 #endif
154         struct {
155                 u_char  mau_zero[10];
156                 u_char  mau_one[2];
157                 struct in_addr mau_inaddr;
158         }               map_addr_un;
159 #define map_zero        map_addr_un.mau_zero
160 #define map_one         map_addr_un.mau_one
161 #define map_inaddr      map_addr_un.mau_inaddr
162 };
163
164 struct policyqueue {
165         TAILQ_ENTRY(policyqueue) pc_entry;
166 #ifdef INET6
167         struct in6_addrpolicy pc_policy;
168 #endif
169 };
170 TAILQ_HEAD(policyhead, policyqueue);
171
172 #define AIO_SRCFLAG_DEPRECATED  0x1
173
174 struct hp_order {
175         union {
176                 struct sockaddr_storage aiou_ss;
177                 struct sockaddr aiou_sa;
178         } aio_src_un;
179 #define aio_srcsa aio_src_un.aiou_sa
180         u_int32_t aio_srcflag;
181         int aio_srcscope;
182         int aio_dstscope;
183         struct policyqueue *aio_srcpolicy;
184         struct policyqueue *aio_dstpolicy;
185         union {
186                 struct sockaddr_storage aiou_ss;
187                 struct sockaddr aiou_sa;
188         } aio_un;
189 #define aio_sa aio_un.aiou_sa
190         int aio_matchlen;
191         char *aio_h_addr;
192 };
193
194 static struct hostent   *_hpcopy(struct hostent *, int *);
195 static struct hostent   *_hpaddr(int, const char *, void *, int *);
196 #ifdef INET6
197 static struct hostent   *_hpmerge(struct hostent *, struct hostent *, int *);
198 static struct hostent   *_hpmapv6(struct hostent *, int *);
199 #endif
200 static struct hostent   *_hpsort(struct hostent *, res_state);
201
202 static struct hostent   *_hpreorder(struct hostent *);
203 static int               get_addrselectpolicy(struct policyhead *);
204 static void              free_addrselectpolicy(struct policyhead *);
205 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
206                                                   struct policyhead *);
207 static void              set_source(struct hp_order *, struct policyhead *);
208 static int               matchlen(struct sockaddr *, struct sockaddr *);
209 static int               comp_dst(const void *, const void *);
210 static int               gai_addr2scopetype(struct sockaddr *);
211
212 /*
213  * Functions defined in RFC2553
214  *      getipnodebyname, getipnodebyaddr, freehostent
215  */
216
217 struct hostent *
218 getipnodebyname(const char *name, int af, int flags, int *errp)
219 {
220         struct hostent *hp;
221         union inx_addr addrbuf;
222         res_state statp;
223         u_long options;
224
225         switch (af) {
226         case AF_INET:
227 #ifdef INET6
228         case AF_INET6:
229 #endif
230                 break;
231         default:
232                 *errp = NO_RECOVERY;
233                 return NULL;
234         }
235
236         if (flags & AI_ADDRCONFIG) {
237                 int s;
238
239                 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
240                         return NULL;
241                 /*
242                  * TODO:
243                  * Note that implementation dependent test for address
244                  * configuration should be done everytime called
245                  * (or apropriate interval),
246                  * because addresses will be dynamically assigned or deleted.
247                  */
248                 _close(s);
249         }
250
251 #ifdef INET6
252         /* special case for literal address */
253         if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
254                 if (af != AF_INET6) {
255                         *errp = HOST_NOT_FOUND;
256                         return NULL;
257                 }
258                 return _hpaddr(af, name, &addrbuf, errp);
259         }
260 #endif
261         if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
262                 if (af != AF_INET) {
263                         if (MAPADDRENABLED(flags)) {
264                                 MAPADDR(&addrbuf, &addrbuf.in_addr);
265                         } else {
266                                 *errp = HOST_NOT_FOUND;
267                                 return NULL;
268                         }
269                 }
270                 return _hpaddr(af, name, &addrbuf, errp);
271         }
272
273
274         statp = __res_state();
275         if ((statp->options & RES_INIT) == 0) {
276                 if (res_ninit(statp) < 0) {
277                         *errp = NETDB_INTERNAL;
278                         return NULL;
279                 }
280         }
281
282         options = statp->options;
283         statp->options &= ~RES_USE_INET6;
284
285         hp = gethostbyname2(name, af);
286         hp = _hpcopy(hp, errp);
287
288 #ifdef INET6
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 _hpreorder(_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 ((long)src & ~(sizeof(struct in_addr) - 1)) {
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 ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) {   /*XXX*/
347                         memcpy(&addrbuf, src, len);
348                         src = &addrbuf;
349                 }
350                 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
351                         return NULL;
352                 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
353                 ||  IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
354                         src = (char *)src +
355                             (sizeof(struct in6_addr) - sizeof(struct in_addr));
356                         af = AF_INET;
357                         len = sizeof(struct in_addr);
358                 }
359                 break;
360 #endif
361         default:
362                 *errp = NO_RECOVERY;
363                 return NULL;
364         }
365
366         statp = __res_state();
367         if ((statp->options & RES_INIT) == 0) {
368                 if (res_ninit(statp) < 0) {
369                         RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
370                         return NULL;
371                 }
372         }
373
374         options = statp->options;
375         statp->options &= ~RES_USE_INET6;
376
377         hp = gethostbyaddr(src, len, af);
378         if (hp == NULL)
379                 *errp = statp->res_h_errno;
380
381         statp->options = options;
382         return (_hpcopy(hp, errp));
383 }
384
385 void
386 freehostent(struct hostent *ptr)
387 {
388         free(ptr);
389 }
390
391 /*
392  * Private utility functions
393  */
394
395 /*
396  * _hpcopy: allocate and copy hostent structure
397  */
398 static struct hostent *
399 _hpcopy(struct hostent *hp, int *errp)
400 {
401         struct hostent *nhp;
402         char *cp, **pp;
403         int size, addrsize;
404         int nalias = 0, naddr = 0;
405         int al_off;
406         int i;
407
408         if (hp == NULL)
409                 return hp;
410
411         /* count size to be allocated */
412         size = sizeof(struct hostent);
413         if (hp->h_name != NULL)
414                 size += strlen(hp->h_name) + 1;
415         if ((pp = hp->h_aliases) != NULL) {
416                 for (i = 0; *pp != NULL; i++, pp++) {
417                         if (**pp != '\0') {
418                                 size += strlen(*pp) + 1;
419                                 nalias++;
420                         }
421                 }
422         }
423         /* adjust alignment */
424         size = ALIGN(size);
425         al_off = size;
426         size += sizeof(char *) * (nalias + 1);
427         addrsize = ALIGN(hp->h_length);
428         if ((pp = hp->h_addr_list) != NULL) {
429                 while (*pp++ != NULL)
430                         naddr++;
431         }
432         size += addrsize * naddr;
433         size += sizeof(char *) * (naddr + 1);
434
435         /* copy */
436         if ((nhp = (struct hostent *)malloc(size)) == NULL) {
437                 *errp = TRY_AGAIN;
438                 return NULL;
439         }
440         cp = (char *)&nhp[1];
441         if (hp->h_name != NULL) {
442                 nhp->h_name = cp;
443                 strcpy(cp, hp->h_name);
444                 cp += strlen(cp) + 1;
445         } else
446                 nhp->h_name = NULL;
447         nhp->h_aliases = (char **)((char *)nhp + al_off);
448         if ((pp = hp->h_aliases) != NULL) {
449                 for (i = 0; *pp != NULL; pp++) {
450                         if (**pp != '\0') {
451                                 nhp->h_aliases[i++] = cp;
452                                 strcpy(cp, *pp);
453                                 cp += strlen(cp) + 1;
454                         }
455                 }
456         }
457         nhp->h_aliases[nalias] = NULL;
458         cp = (char *)&nhp->h_aliases[nalias + 1];
459         nhp->h_addrtype = hp->h_addrtype;
460         nhp->h_length = hp->h_length;
461         nhp->h_addr_list = (char **)cp;
462         if ((pp = hp->h_addr_list) != NULL) {
463                 cp = (char *)&nhp->h_addr_list[naddr + 1];
464                 for (i = 0; *pp != NULL; pp++) {
465                         nhp->h_addr_list[i++] = cp;
466                         memcpy(cp, *pp, hp->h_length);
467                         cp += addrsize;
468                 }
469         }
470         nhp->h_addr_list[naddr] = NULL;
471         return nhp;
472 }
473
474 /*
475  * _hpaddr: construct hostent structure with one address
476  */
477 static struct hostent *
478 _hpaddr(int af, const char *name, void *addr, int *errp)
479 {
480         struct hostent *hp, hpbuf;
481         char *addrs[2];
482
483         hp = &hpbuf;
484         hp->h_name = (char *)name;
485         hp->h_aliases = NULL;
486         hp->h_addrtype = af;
487         hp->h_length = ADDRLEN(af);
488         hp->h_addr_list = addrs;
489         addrs[0] = (char *)addr;
490         addrs[1] = NULL;
491         return (_hpcopy(hp, errp));
492 }
493
494 #ifdef INET6
495 /*
496  * _hpmerge: merge 2 hostent structure, arguments will be freed
497  */
498 static struct hostent *
499 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
500 {
501         int i, j;
502         int naddr, nalias;
503         char **pp;
504         struct hostent *hp, hpbuf;
505         char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
506         union inx_addr addrbuf[MAXADDRS];
507
508         if (hp1 == NULL)
509                 return _hpcopy(hp2, errp);
510         if (hp2 == NULL)
511                 return _hpcopy(hp1, errp);
512
513 #define HP(i)   (i == 1 ? hp1 : hp2)
514         hp = &hpbuf;
515         hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
516         hp->h_aliases = aliases;
517         nalias = 0;
518         for (i = 1; i <= 2; i++) {
519                 if ((pp = HP(i)->h_aliases) == NULL)
520                         continue;
521                 for (; nalias < MAXALIASES && *pp != NULL; pp++) {
522                         /* check duplicates */
523                         for (j = 0; j < nalias; j++)
524                                 if (strcasecmp(*pp, aliases[j]) == 0)
525                                         break;
526                         if (j == nalias)
527                                 aliases[nalias++] = *pp;
528                 }
529         }
530         aliases[nalias] = NULL;
531         if (hp1->h_length != hp2->h_length) {
532                 hp->h_addrtype = AF_INET6;
533                 hp->h_length = sizeof(struct in6_addr);
534         } else {
535                 hp->h_addrtype = hp1->h_addrtype;
536                 hp->h_length = hp1->h_length;
537         }
538
539         hp->h_addr_list = addrs;
540         naddr = 0;
541         for (i = 1; i <= 2; i++) {
542                 if ((pp = HP(i)->h_addr_list) == NULL)
543                         continue;
544                 if (HP(i)->h_length == hp->h_length) {
545                         while (naddr < MAXADDRS && *pp != NULL)
546                                 addrs[naddr++] = *pp++;
547                 } else {
548                         /* copy IPv4 addr as mapped IPv6 addr */
549                         while (naddr < MAXADDRS && *pp != NULL) {
550                                 MAPADDR(&addrbuf[naddr], *pp++);
551                                 addrs[naddr] = (char *)&addrbuf[naddr];
552                                 naddr++;
553                         }
554                 }
555         }
556         addrs[naddr] = NULL;
557         return (_hpcopy(hp, errp));
558 }
559 #endif
560
561 /*
562  * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
563  */
564 #ifdef INET6
565 static struct hostent *
566 _hpmapv6(struct hostent *hp, int *errp)
567 {
568         struct hostent hp6;
569
570         if (hp == NULL)
571                 return NULL;
572         if (hp->h_addrtype == AF_INET6)
573                 return _hpcopy(hp, errp);
574
575         memset(&hp6, 0, sizeof(struct hostent));
576         hp6.h_addrtype = AF_INET6;
577         hp6.h_length = sizeof(struct in6_addr);
578         return _hpmerge(&hp6, hp, errp);
579 }
580 #endif
581
582 /*
583  * _hpsort: sort address by sortlist
584  */
585 static struct hostent *
586 _hpsort(struct hostent *hp, res_state statp)
587 {
588         int i, j, n;
589         u_char *ap, *sp, *mp, **pp;
590         char t;
591         char order[MAXADDRS];
592         int nsort = statp->nsort;
593
594         if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
595                 return hp;
596         for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
597                 for (j = 0; j < nsort; j++) {
598 #ifdef INET6
599                         if (statp->_u._ext.ext->sort_list[j].af !=
600                             hp->h_addrtype)
601                                 continue;
602                         sp = (u_char *)&statp->_u._ext.ext->sort_list[j].addr;
603                         mp = (u_char *)&statp->_u._ext.ext->sort_list[j].mask;
604 #else
605                         sp = (u_char *)&statp->sort_list[j].addr;
606                         mp = (u_char *)&statp->sort_list[j].mask;
607 #endif
608                         for (n = 0; n < hp->h_length; n++) {
609                                 if ((ap[n] & mp[n]) != sp[n])
610                                         break;
611                         }
612                         if (n == hp->h_length)
613                                 break;
614                 }
615                 order[i] = j;
616         }
617         n = i;
618         pp = (u_char **)hp->h_addr_list;
619         for (i = 0; i < n - 1; i++) {
620                 for (j = i + 1; j < n; j++) {
621                         if (order[i] > order[j]) {
622                                 ap = pp[i];
623                                 pp[i] = pp[j];
624                                 pp[j] = ap;
625                                 t = order[i];
626                                 order[i] = order[j];
627                                 order[j] = t;
628                         }
629                 }
630         }
631         return hp;
632 }
633
634 /*
635  * _hpreorder: sort address by default address selection
636  */
637 static struct hostent *
638 _hpreorder(struct hostent *hp)
639 {
640         struct hp_order *aio;
641         int i, n;
642         char *ap;
643         struct sockaddr *sa;
644         struct policyhead policyhead;
645
646         if (hp == NULL)
647                 return hp;
648
649         switch (hp->h_addrtype) {
650         case AF_INET:
651 #ifdef INET6
652         case AF_INET6:
653 #endif
654                 break;
655         default:
656                 free_addrselectpolicy(&policyhead);
657                 return hp;
658         }
659
660         /* count the number of addrinfo elements for sorting. */
661         for (n = 0; hp->h_addr_list[n] != NULL; n++)
662                 ;
663
664         /*
665          * If the number is small enough, we can skip the reordering process.
666          */
667         if (n <= 1)
668                 return hp;
669
670         /* allocate a temporary array for sort and initialization of it. */
671         if ((aio = malloc(sizeof(*aio) * n)) == NULL)
672                 return hp;      /* give up reordering */
673         memset(aio, 0, sizeof(*aio) * n);
674
675         /* retrieve address selection policy from the kernel */
676         TAILQ_INIT(&policyhead);
677         if (!get_addrselectpolicy(&policyhead)) {
678                 /* no policy is installed into kernel, we don't sort. */
679                 free(aio);
680                 return hp;
681         }
682
683         for (i = 0; i < n; i++) {
684                 ap = hp->h_addr_list[i];
685                 aio[i].aio_h_addr = ap;
686                 sa = &aio[i].aio_sa;
687                 switch (hp->h_addrtype) {
688                 case AF_INET:
689                         sa->sa_family = AF_INET;
690                         sa->sa_len = sizeof(struct sockaddr_in);
691                         memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap,
692                             sizeof(struct in_addr));
693                         break;
694 #ifdef INET6
695                 case AF_INET6:
696                         if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
697                                 sa->sa_family = AF_INET;
698                                 sa->sa_len = sizeof(struct sockaddr_in);
699                                 memcpy(&((struct sockaddr_in *)sa)->sin_addr,
700                                     &ap[12], sizeof(struct in_addr));
701                         } else {
702                                 sa->sa_family = AF_INET6;
703                                 sa->sa_len = sizeof(struct sockaddr_in6);
704                                 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr,
705                                     ap, sizeof(struct in6_addr));
706                         }
707                         break;
708 #endif
709                 }
710                 aio[i].aio_dstscope = gai_addr2scopetype(sa);
711                 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
712                 set_source(&aio[i], &policyhead);
713         }
714
715         /* perform sorting. */
716         qsort(aio, n, sizeof(*aio), comp_dst);
717
718         /* reorder the h_addr_list. */
719         for (i = 0; i < n; i++)
720                 hp->h_addr_list[i] = aio[i].aio_h_addr;
721
722         /* cleanup and return */
723         free(aio);
724         free_addrselectpolicy(&policyhead);
725         return hp;
726 }
727
728 static int
729 get_addrselectpolicy(struct policyhead *head)
730 {
731 #ifdef INET6
732         int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
733         size_t l;
734         char *buf;
735         struct in6_addrpolicy *pol, *ep;
736
737         if (sysctl(mib, NELEM(mib), NULL, &l, NULL, 0) < 0)
738                 return (0);
739         if ((buf = malloc(l)) == NULL)
740                 return (0);
741         if (sysctl(mib, NELEM(mib), buf, &l, NULL, 0) < 0) {
742                 free(buf);
743                 return (0);
744         }
745
746         ep = (struct in6_addrpolicy *)(buf + l);
747         for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
748                 struct policyqueue *new;
749
750                 if ((new = malloc(sizeof(*new))) == NULL) {
751                         free_addrselectpolicy(head); /* make the list empty */
752                         break;
753                 }
754                 new->pc_policy = *pol;
755                 TAILQ_INSERT_TAIL(head, new, pc_entry);
756         }
757
758         free(buf);
759         return (1);
760 #else
761         return (0);
762 #endif
763 }
764
765 static void
766 free_addrselectpolicy(struct policyhead *head)
767 {
768         struct policyqueue *ent, *nent;
769
770         for (ent = TAILQ_FIRST(head); ent; ent = nent) {
771                 nent = TAILQ_NEXT(ent, pc_entry);
772                 TAILQ_REMOVE(head, ent, pc_entry);
773                 free(ent);
774         }
775 }
776
777 static struct policyqueue *
778 match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
779 {
780 #ifdef INET6
781         struct policyqueue *ent, *bestent = NULL;
782         struct in6_addrpolicy *pol;
783         int matchlen, bestmatchlen = -1;
784         u_char *mp, *ep, *k, *p, m;
785         struct sockaddr_in6 key;
786
787         switch(addr->sa_family) {
788         case AF_INET6:
789                 key = *(struct sockaddr_in6 *)addr;
790                 break;
791         case AF_INET:
792                 /* convert the address into IPv4-mapped IPv6 address. */
793                 memset(&key, 0, sizeof(key));
794                 key.sin6_family = AF_INET6;
795                 key.sin6_len = sizeof(key);
796                 key.sin6_addr.s6_addr[10] = 0xff;
797                 key.sin6_addr.s6_addr[11] = 0xff;
798                 memcpy(&key.sin6_addr.s6_addr[12],
799                        &((struct sockaddr_in *)addr)->sin_addr, 4);
800                 break;
801         default:
802                 return(NULL);
803         }
804
805         for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
806                 pol = &ent->pc_policy;
807                 matchlen = 0;
808
809                 mp = (u_char *)&pol->addrmask.sin6_addr;
810                 ep = mp + 16;   /* XXX: scope field? */
811                 k = (u_char *)&key.sin6_addr;
812                 p = (u_char *)&pol->addr.sin6_addr;
813                 for (; mp < ep && *mp; mp++, k++, p++) {
814                         m = *mp;
815                         if ((*k & m) != *p)
816                                 goto next; /* not match */
817                         if (m == 0xff) /* short cut for a typical case */
818                                 matchlen += 8;
819                         else {
820                                 while (m >= 0x80) {
821                                         matchlen++;
822                                         m <<= 1;
823                                 }
824                         }
825                 }
826
827                 /* matched.  check if this is better than the current best. */
828                 if (matchlen > bestmatchlen) {
829                         bestent = ent;
830                         bestmatchlen = matchlen;
831                 }
832
833           next:
834                 continue;
835         }
836
837         return(bestent);
838 #else
839         return(NULL);
840 #endif
841
842 }
843
844 static void
845 set_source(struct hp_order *aio, struct policyhead *ph)
846 {
847         struct sockaddr_storage ss = aio->aio_un.aiou_ss;
848         socklen_t srclen;
849         int s;
850
851         /* set unspec ("no source is available"), just in case */
852         aio->aio_srcsa.sa_family = AF_UNSPEC;
853         aio->aio_srcscope = -1;
854
855         switch(ss.ss_family) {
856         case AF_INET:
857                 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
858                 break;
859 #ifdef INET6
860         case AF_INET6:
861                 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
862                 break;
863 #endif
864         default:                /* ignore unsupported AFs explicitly */
865                 return;
866         }
867
868         /* open a socket to get the source address for the given dst */
869         if ((s = _socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
870                 return;         /* give up */
871         if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
872                 goto cleanup;
873         srclen = ss.ss_len;
874         if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
875                 aio->aio_srcsa.sa_family = AF_UNSPEC;
876                 goto cleanup;
877         }
878         aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
879         aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
880         aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
881 #ifdef INET6
882         if (ss.ss_family == AF_INET6) {
883                 struct in6_ifreq ifr6;
884                 u_int32_t flags6;
885
886                 /* XXX: interface name should not be hardcoded */
887                 strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
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 < addrlen * 8) {
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         return(-1);
1050 }
1051
1052 /*
1053  * Copy from scope.c.
1054  * XXX: we should standardize the functions and link them as standard
1055  * library.
1056  */
1057 static int
1058 gai_addr2scopetype(struct sockaddr *sa)
1059 {
1060 #ifdef INET6
1061         struct sockaddr_in6 *sa6;
1062 #endif
1063         struct sockaddr_in *sa4;
1064
1065         switch(sa->sa_family) {
1066 #ifdef INET6
1067         case AF_INET6:
1068                 sa6 = (struct sockaddr_in6 *)sa;
1069                 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1070                         /* just use the scope field of the multicast address */
1071                         return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1072                 }
1073                 /*
1074                  * Unicast addresses: map scope type to corresponding scope
1075                  * value defined for multcast addresses.
1076                  * XXX: hardcoded scope type values are bad...
1077                  */
1078                 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1079                         return(1); /* node local scope */
1080                 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1081                         return(2); /* link-local scope */
1082                 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1083                         return(5); /* site-local scope */
1084                 return(14);     /* global scope */
1085                 break;
1086 #endif
1087         case AF_INET:
1088                 /*
1089                  * IPv4 pseudo scoping according to RFC 3484.
1090                  */
1091                 sa4 = (struct sockaddr_in *)sa;
1092                 /* IPv4 autoconfiguration addresses have link-local scope. */
1093                 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1094                     ((u_char *)&sa4->sin_addr)[1] == 254)
1095                         return(2);
1096                 /* Private addresses have site-local scope. */
1097                 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1098                     (((u_char *)&sa4->sin_addr)[0] == 172 &&
1099                      (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1100                     (((u_char *)&sa4->sin_addr)[0] == 192 &&
1101                      ((u_char *)&sa4->sin_addr)[1] == 168))
1102                         return(14);     /* XXX: It should be 5 unless NAT */
1103                 /* Loopback addresses have link-local scope. */
1104                 if (((u_char *)&sa4->sin_addr)[0] == 127)
1105                         return(2);
1106                 return(14);
1107                 break;
1108         default:
1109                 errno = EAFNOSUPPORT; /* is this a good error? */
1110                 return(-1);
1111         }
1112 }