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: stable/9/lib/libc/net/name6.c 292827 2015-12-28 03:39:32Z ume $");
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 *
getipnodebyname(const char * name,int af,int flags,int * errp)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 *
getipnodebyaddr(const void * src,size_t len,int af,int * errp)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
freehostent(struct hostent * ptr)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 *
_hpcopy(struct hostent * hp,int * errp)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 *
_hpaddr(int af,const char * name,void * addr,int * errp)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 *
_hpmerge(struct hostent * hp1,struct hostent * hp2,int * errp)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 *
_hpmapv6(struct hostent * hp,int * errp)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 *
_hpsort(struct hostent * hp,res_state statp)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 *
_hpreorder(struct hostent * hp)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
get_addrselectpolicy(struct policyhead * head)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
free_addrselectpolicy(struct policyhead * head)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 *
match_addrselectpolicy(struct sockaddr * addr,struct policyhead * head)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 _map_v4v6_address(
803 (char *)&((struct sockaddr_in *)addr)->sin_addr,
804 (char *)&key.sin6_addr);
805 break;
806 default:
807 return(NULL);
808 }
809
810 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
811 pol = &ent->pc_policy;
812 matchlen = 0;
813
814 mp = (u_char *)&pol->addrmask.sin6_addr;
815 ep = mp + 16; /* XXX: scope field? */
816 k = (u_char *)&key.sin6_addr;
817 p = (u_char *)&pol->addr.sin6_addr;
818 for (; mp < ep && *mp; mp++, k++, p++) {
819 m = *mp;
820 if ((*k & m) != *p)
821 goto next; /* not match */
822 if (m == 0xff) /* short cut for a typical case */
823 matchlen += 8;
824 else {
825 while (m >= 0x80) {
826 matchlen++;
827 m <<= 1;
828 }
829 }
830 }
831
832 /* matched. check if this is better than the current best. */
833 if (matchlen > bestmatchlen) {
834 bestent = ent;
835 bestmatchlen = matchlen;
836 }
837
838 next:
839 continue;
840 }
841
842 return(bestent);
843 #else
844 return(NULL);
845 #endif
846
847 }
848
849 static void
set_source(struct hp_order * aio,struct policyhead * ph)850 set_source(struct hp_order *aio, struct policyhead *ph)
851 {
852 struct sockaddr_storage ss = aio->aio_un.aiou_ss;
853 socklen_t srclen;
854 int s;
855
856 /* set unspec ("no source is available"), just in case */
857 aio->aio_srcsa.sa_family = AF_UNSPEC;
858 aio->aio_srcscope = -1;
859
860 switch(ss.ss_family) {
861 case AF_INET:
862 ((struct sockaddr_in *)&ss)->sin_port = htons(1);
863 break;
864 #ifdef INET6
865 case AF_INET6:
866 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1);
867 break;
868 #endif
869 default: /* ignore unsupported AFs explicitly */
870 return;
871 }
872
873 /* open a socket to get the source address for the given dst */
874 if ((s = _socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
875 return; /* give up */
876 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0)
877 goto cleanup;
878 srclen = ss.ss_len;
879 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
880 aio->aio_srcsa.sa_family = AF_UNSPEC;
881 goto cleanup;
882 }
883 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
884 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
885 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss);
886 #ifdef INET6
887 if (ss.ss_family == AF_INET6) {
888 struct in6_ifreq ifr6;
889 u_int32_t flags6;
890
891 memset(&ifr6, 0, sizeof(ifr6));
892 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len);
893 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
894 flags6 = ifr6.ifr_ifru.ifru_flags6;
895 if ((flags6 & IN6_IFF_DEPRECATED))
896 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
897 }
898 }
899 #endif
900
901 cleanup:
902 _close(s);
903 return;
904 }
905
906 static int
matchlen(struct sockaddr * src,struct sockaddr * dst)907 matchlen(struct sockaddr *src, struct sockaddr *dst)
908 {
909 int match = 0;
910 u_char *s, *d;
911 u_char *lim, r;
912 int addrlen;
913
914 switch (src->sa_family) {
915 #ifdef INET6
916 case AF_INET6:
917 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
918 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
919 addrlen = sizeof(struct in6_addr);
920 lim = s + addrlen;
921 break;
922 #endif
923 case AF_INET:
924 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
925 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
926 addrlen = sizeof(struct in_addr);
927 lim = s + addrlen;
928 break;
929 default:
930 return(0);
931 }
932
933 while (s < lim)
934 if ((r = (*d++ ^ *s++)) != 0) {
935 while (r < addrlen * 8) {
936 match++;
937 r <<= 1;
938 }
939 break;
940 } else
941 match += 8;
942 return(match);
943 }
944
945 static int
comp_dst(const void * arg1,const void * arg2)946 comp_dst(const void *arg1, const void *arg2)
947 {
948 const struct hp_order *dst1 = arg1, *dst2 = arg2;
949
950 /*
951 * Rule 1: Avoid unusable destinations.
952 * XXX: we currently do not consider if an appropriate route exists.
953 */
954 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
955 dst2->aio_srcsa.sa_family == AF_UNSPEC) {
956 return(-1);
957 }
958 if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
959 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
960 return(1);
961 }
962
963 /* Rule 2: Prefer matching scope. */
964 if (dst1->aio_dstscope == dst1->aio_srcscope &&
965 dst2->aio_dstscope != dst2->aio_srcscope) {
966 return(-1);
967 }
968 if (dst1->aio_dstscope != dst1->aio_srcscope &&
969 dst2->aio_dstscope == dst2->aio_srcscope) {
970 return(1);
971 }
972
973 /* Rule 3: Avoid deprecated addresses. */
974 if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
975 dst2->aio_srcsa.sa_family != AF_UNSPEC) {
976 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
977 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
978 return(-1);
979 }
980 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
981 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
982 return(1);
983 }
984 }
985
986 /* Rule 4: Prefer home addresses. */
987 /* XXX: not implemented yet */
988
989 /* Rule 5: Prefer matching label. */
990 #ifdef INET6
991 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
992 dst1->aio_srcpolicy->pc_policy.label ==
993 dst1->aio_dstpolicy->pc_policy.label &&
994 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
995 dst2->aio_srcpolicy->pc_policy.label !=
996 dst2->aio_dstpolicy->pc_policy.label)) {
997 return(-1);
998 }
999 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
1000 dst2->aio_srcpolicy->pc_policy.label ==
1001 dst2->aio_dstpolicy->pc_policy.label &&
1002 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1003 dst1->aio_srcpolicy->pc_policy.label !=
1004 dst1->aio_dstpolicy->pc_policy.label)) {
1005 return(1);
1006 }
1007 #endif
1008
1009 /* Rule 6: Prefer higher precedence. */
1010 #ifdef INET6
1011 if (dst1->aio_dstpolicy &&
1012 (dst2->aio_dstpolicy == NULL ||
1013 dst1->aio_dstpolicy->pc_policy.preced >
1014 dst2->aio_dstpolicy->pc_policy.preced)) {
1015 return(-1);
1016 }
1017 if (dst2->aio_dstpolicy &&
1018 (dst1->aio_dstpolicy == NULL ||
1019 dst2->aio_dstpolicy->pc_policy.preced >
1020 dst1->aio_dstpolicy->pc_policy.preced)) {
1021 return(1);
1022 }
1023 #endif
1024
1025 /* Rule 7: Prefer native transport. */
1026 /* XXX: not implemented yet */
1027
1028 /* Rule 8: Prefer smaller scope. */
1029 if (dst1->aio_dstscope >= 0 &&
1030 dst1->aio_dstscope < dst2->aio_dstscope) {
1031 return(-1);
1032 }
1033 if (dst2->aio_dstscope >= 0 &&
1034 dst2->aio_dstscope < dst1->aio_dstscope) {
1035 return(1);
1036 }
1037
1038 /*
1039 * Rule 9: Use longest matching prefix.
1040 * We compare the match length in a same AF only.
1041 */
1042 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) {
1043 if (dst1->aio_matchlen > dst2->aio_matchlen) {
1044 return(-1);
1045 }
1046 if (dst1->aio_matchlen < dst2->aio_matchlen) {
1047 return(1);
1048 }
1049 }
1050
1051 /* Rule 10: Otherwise, leave the order unchanged. */
1052 return(-1);
1053 }
1054
1055 /*
1056 * Copy from scope.c.
1057 * XXX: we should standardize the functions and link them as standard
1058 * library.
1059 */
1060 static int
gai_addr2scopetype(struct sockaddr * sa)1061 gai_addr2scopetype(struct sockaddr *sa)
1062 {
1063 #ifdef INET6
1064 struct sockaddr_in6 *sa6;
1065 #endif
1066 struct sockaddr_in *sa4;
1067
1068 switch(sa->sa_family) {
1069 #ifdef INET6
1070 case AF_INET6:
1071 sa6 = (struct sockaddr_in6 *)sa;
1072 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1073 /* just use the scope field of the multicast address */
1074 return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1075 }
1076 /*
1077 * Unicast addresses: map scope type to corresponding scope
1078 * value defined for multcast addresses.
1079 * XXX: hardcoded scope type values are bad...
1080 */
1081 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1082 return(1); /* node local scope */
1083 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1084 return(2); /* link-local scope */
1085 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1086 return(5); /* site-local scope */
1087 return(14); /* global scope */
1088 break;
1089 #endif
1090 case AF_INET:
1091 /*
1092 * IPv4 pseudo scoping according to RFC 3484.
1093 */
1094 sa4 = (struct sockaddr_in *)sa;
1095 /* IPv4 autoconfiguration addresses have link-local scope. */
1096 if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1097 ((u_char *)&sa4->sin_addr)[1] == 254)
1098 return(2);
1099 /* Private addresses have site-local scope. */
1100 if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1101 (((u_char *)&sa4->sin_addr)[0] == 172 &&
1102 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1103 (((u_char *)&sa4->sin_addr)[0] == 192 &&
1104 ((u_char *)&sa4->sin_addr)[1] == 168))
1105 return(14); /* XXX: It should be 5 unless NAT */
1106 /* Loopback addresses have link-local scope. */
1107 if (((u_char *)&sa4->sin_addr)[0] == 127)
1108 return(2);
1109 return(14);
1110 break;
1111 default:
1112 errno = EAFNOSUPPORT; /* is this a good error? */
1113 return(-1);
1114 }
1115 }
1116 #endif
1117