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