]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/grabmyaddr.c
5abd87f2c54066758f4bc16a994f14b2793792a3
1 /* $KAME: grabmyaddr.c,v 1.35 2003/01/14 07:07:36 sakane Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
38 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
39 #include <net/if_var.h>
41 #include <net/route.h>
42 #include <netkey/key_var.h>
43 #include <netinet/in.h>
44 #include <netinet6/in6_var.h>
54 #ifdef HAVE_GETIFADDRS
65 #include "localconf.h"
66 #include "grabmyaddr.h"
68 #include "isakmp_var.h"
71 #ifndef HAVE_GETIFADDRS
72 static unsigned int if_maxindex
__P((void));
74 static struct myaddrs
*find_myaddr
__P((struct myaddrs
*, struct sockaddr
*));
75 static int suitable_ifaddr
__P((const char *, const struct sockaddr
*));
77 static int suitable_ifaddr6
__P((const char *, const struct sockaddr
*));
80 #ifndef HAVE_GETIFADDRS
84 struct if_nameindex
*p
, *p0
;
88 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
89 if (max
< p
->if_index
)
110 static struct myaddrs
*
111 find_myaddr(db
, addr
)
113 struct sockaddr
*addr
;
116 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
118 if (getnameinfo(addr
, addr
->sa_len
, h1
, sizeof(h1
), NULL
, 0,
119 NI_NUMERICHOST
| niflags
) != 0)
122 for (q
= db
; q
; q
= q
->next
) {
125 if (addr
->sa_len
!= q
->addr
->sa_len
)
127 if (getnameinfo(q
->addr
, q
->addr
->sa_len
, h2
, sizeof(h2
),
128 NULL
, 0, NI_NUMERICHOST
| niflags
) != 0)
130 if (strcmp(h1
, h2
) == 0)
137 // 1/19/04 - modified to avoid closing and opening sockets for
138 // all interfaces each time an interface change occurs.
139 // on return: addrcount = zero indicates address no longer used
140 // sock = -1 indicates a new address - no socket opened yet.
144 #ifdef HAVE_GETIFADDRS
146 struct ifaddrs
*ifa0
, *ifap
;
149 struct sockaddr_in6
*sin6
;
153 char addr1
[NI_MAXHOST
];
155 if (getifaddrs(&ifa0
)) {
156 plog(LLV_ERROR
, LOCATION
, NULL
,
157 "getifaddrs failed: %s\n", strerror(errno
));
162 // zero the count for each address in our list
163 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
)
166 for (ifap
= ifa0
; ifap
; ifap
= ifap
->ifa_next
) {
168 if (ifap
->ifa_addr
->sa_family
!= AF_INET
170 && ifap
->ifa_addr
->sa_family
!= AF_INET6
175 if (!suitable_ifaddr(ifap
->ifa_name
, ifap
->ifa_addr
)) {
176 plog(LLV_ERROR
, LOCATION
, NULL
,
177 "unsuitable address: %s %s\n",
179 saddrwop2str(ifap
->ifa_addr
));
185 if (ifap
->ifa_addr
->sa_family
== AF_INET6
) {
186 sin6
= (struct sockaddr_in6
*)ifap
->ifa_addr
;
187 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)
188 || IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
)) {
189 sin6
->sin6_scope_id
=
190 ntohs(*(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2]);
191 sin6
->sin6_addr
.s6_addr
[2] = 0;
192 sin6
->sin6_addr
.s6_addr
[3] = 0;
198 p
= find_myaddr(lcconf
->myaddrs
, ifap
->ifa_addr
);
207 p
->addr
= dupsaddr(ifap
->ifa_addr
);
208 if (p
->addr
== NULL
) {
218 if (getnameinfo(p
->addr
, p
->addr
->sa_len
,
219 addr1
, sizeof(addr1
),
221 NI_NUMERICHOST
| niflags
))
222 strlcpy(addr1
, "(invalid)", sizeof(addr1
));
223 plog(LLV_DEBUG
, LOCATION
, NULL
,
224 "my interface: %s (%s)\n",
225 addr1
, ifap
->ifa_name
);
227 p
->next
= lcconf
->myaddrs
;
235 #else /*!HAVE_GETIFADDRS*/
236 #error "NOT SUPPORTED"
237 #endif /*HAVE_GETIFADDRS*/
241 * check the interface is suitable or not
244 suitable_ifaddr(ifname
, ifaddr
)
246 const struct sockaddr
*ifaddr
;
248 switch(ifaddr
->sa_family
) {
253 return suitable_ifaddr6(ifname
, ifaddr
);
263 suitable_ifaddr6(ifname
, ifaddr
)
265 const struct sockaddr
*ifaddr
;
267 struct in6_ifreq ifr6
;
270 if (ifaddr
->sa_family
!= AF_INET6
)
273 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
275 plog(LLV_ERROR
, LOCATION
, NULL
,
276 "socket(SOCK_DGRAM) failed:%s\n", strerror(errno
));
280 memset(&ifr6
, 0, sizeof(ifr6
));
281 strncpy(ifr6
.ifr_name
, ifname
, strlen(ifname
));
283 ifr6
.ifr_addr
= *(const struct sockaddr_in6
*)ifaddr
;
285 if (ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) < 0) {
286 plog(LLV_ERROR
, LOCATION
, NULL
,
287 "ioctl(SIOCGIFAFLAG_IN6) failed:%s\n", strerror(errno
));
294 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DUPLICATED
295 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DETACHED
)
308 struct rt_msghdr
*rtm
;
310 len
= read(lcconf
->rtsock
, msg
, sizeof(msg
));
312 plog(LLV_ERROR
, LOCATION
, NULL
,
313 "read(PF_ROUTE) failed: %s\n",
317 rtm
= (struct rt_msghdr
*)msg
;
318 if (len
< rtm
->rtm_msglen
) {
319 plog(LLV_ERROR
, LOCATION
, NULL
,
320 "read(PF_ROUTE) short read\n");
323 if (rtm
->rtm_version
!= RTM_VERSION
) {
324 plog(LLV_ERROR
, LOCATION
, NULL
,
325 "routing socket version mismatch\n");
326 close(lcconf
->rtsock
);
330 switch (rtm
->rtm_type
) {
337 /* ignore this message silently */
340 plog(LLV_DEBUG
, LOCATION
, NULL
,
341 "msg %d not interesting\n", rtm
->rtm_type
);
344 /* XXX more filters here? */
346 plog(LLV_DEBUG
, LOCATION
, NULL
,
347 "caught rtm:%d, need update interface address list\n",
353 * initialize default port for ISAKMP to send, if no "listen"
354 * directive is specified in config file.
356 * DO NOT listen to wildcard addresses. if you receive packets to
357 * wildcard address, you'll be in trouble (DoS attack possible by
361 autoconf_myaddrsport()
364 struct sockaddr_in
*sin4
;
366 struct sockaddr_in6
*sin6
;
370 plog(LLV_DEBUG
, LOCATION
, NULL
,
371 "configuring default isakmp port.\n");
373 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
376 switch (p
->addr
->sa_family
) {
378 sin4
= (struct sockaddr_in
*)p
->addr
;
379 sin4
->sin_port
= htons(lcconf
->port_isakmp
);
383 sin6
= (struct sockaddr_in6
*)p
->addr
;
384 sin6
->sin6_port
= htons(lcconf
->port_isakmp
);
388 plog(LLV_ERROR
, LOCATION
, NULL
,
389 "unsupported AF %d\n", p
->addr
->sa_family
);
394 plog(LLV_DEBUG
, LOCATION
, NULL
,
395 "%d addrs are configured successfully\n", n
);
399 plog(LLV_ERROR
, LOCATION
, NULL
, "address autoconfiguration failed\n");
404 * get a port number to which racoon binded.
405 * NOTE: network byte order returned.
408 getmyaddrsport(local
)
409 struct sockaddr
*local
;
413 /* get a relative port */
414 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
417 if (!cmpsaddrwop(local
, p
->addr
)) {
418 switch (p
->addr
->sa_family
) {
420 return ((struct sockaddr_in
*)p
->addr
)->sin_port
;
423 return ((struct sockaddr_in6
*)p
->addr
)->sin6_port
;
426 plog(LLV_ERROR
, LOCATION
, NULL
,
427 "invalid family: %d\n",
435 return htons(PORT_ISAKMP
);
443 new = racoon_calloc(1, sizeof(*new));
445 plog(LLV_ERROR
, LOCATION
, NULL
,
446 "failed to allocate buffer for myaddrs.\n");
459 struct myaddrs
**head
;
467 struct myaddrs
*myaddr
;
470 racoon_free(myaddr
->addr
);
477 /* initialize routing socket */
478 lcconf
->rtsock
= socket(PF_ROUTE
, SOCK_RAW
, PF_UNSPEC
);
479 if (lcconf
->rtsock
< 0) {
480 plog(LLV_ERROR
, LOCATION
, NULL
,
481 "socket(PF_ROUTE) failed: %s",
486 if (lcconf
->myaddrs
== NULL
&& lcconf
->autograbaddr
== 1) {
489 if (autoconf_myaddrsport() < 0)
496 /* select the socket to be sent */
497 /* should implement other method. */
502 struct myaddrs
*p
, *lastresort
= NULL
;
504 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
507 if (my
->sa_family
== p
->addr
->sa_family
)
509 if (my
->sa_len
== p
->addr
->sa_len
510 && memcmp(my
, p
->addr
, my
->sa_len
) == 0) {
514 if (my
->sa_family
== p
->addr
->sa_family
&&
515 my
->sa_family
== AF_INET
&&
516 ((struct sockaddr_in
*)my
)->sin_addr
.s_addr
==
517 ((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
&&
518 ((struct sockaddr_in
*)my
)->sin_port
== htons(PORT_ISAKMP_NATT
))
520 plog(LLV_DEBUG
, LOCATION
, NULL
,
521 "picked natt socket (%d - %s) for sending\n",
522 p
->nattsock
, saddr2str(my
));
530 plog(LLV_ERROR
, LOCATION
, NULL
,
531 "no socket matches address family %d\n",