]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/grabmyaddr.c
1 /* $Id: grabmyaddr.c,v 1.23.4.2 2005/07/16 04:41:01 monas 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
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
40 #include <net/if_var.h>
41 #include <netinet/in.h>
42 #include <netinet6/in6_var.h>
43 #include <net/route.h>
53 #ifdef HAVE_GETIFADDRS
65 #include "localconf.h"
67 #include "grabmyaddr.h"
69 #include "isakmp_var.h"
71 #include "nattraversal.h"
73 #ifndef HAVE_GETIFADDRS
74 static unsigned int if_maxindex
__P((void));
77 static int suitable_ifaddr
__P((const char *, const struct sockaddr
*));
79 static int suitable_ifaddr6
__P((const char *, const struct sockaddr
*));
82 #ifndef HAVE_GETIFADDRS
86 struct if_nameindex
*p
, *p0
;
90 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
91 if (max
< p
->if_index
)
103 struct myaddrs
*p
, *next
;
105 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
111 lcconf
->myaddrs
= NULL
;
117 find_myaddr(addr
, udp_encap
)
118 struct sockaddr
*addr
;
122 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
124 if (getnameinfo(addr
, sysdep_sa_len(addr
), h1
, sizeof(h1
), NULL
, 0,
125 NI_NUMERICHOST
| niflags
) != 0)
128 for (q
= lcconf
->myaddrs
; q
; q
= q
->next
) {
131 if (q
->udp_encap
&& !udp_encap
132 || !q
->udp_encap
&& udp_encap
)
134 if (addr
->sa_family
!= q
->addr
->sa_family
)
136 if (getnameinfo(q
->addr
, sysdep_sa_len(q
->addr
), h2
, sizeof(h2
),
137 NULL
, 0, NI_NUMERICHOST
| niflags
) != 0)
139 if (strcmp(h1
, h2
) == 0)
147 // modified to avoid closing and opening sockets for
148 // all interfaces each time an interface change occurs.
149 // on return: addrcount = zero indicates address no longer used
150 // sock = -1 indicates a new address - no socket opened yet.
154 #ifdef HAVE_GETIFADDRS
155 struct myaddrs
*p
, *q
;
156 struct ifaddrs
*ifa0
, *ifap
;
158 struct sockaddr_in6
*sin6
;
161 char addr1
[NI_MAXHOST
];
163 if (getifaddrs(&ifa0
)) {
164 plog(LLV_ERROR2
, LOCATION
, NULL
,
165 "getifaddrs failed: %s\n", strerror(errno
));
170 // clear the in_use flag for each address in the list
171 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
)
174 for (ifap
= ifa0
; ifap
; ifap
= ifap
->ifa_next
) {
176 if (ifap
->ifa_addr
->sa_family
!= AF_INET
178 && ifap
->ifa_addr
->sa_family
!= AF_INET6
183 if (!suitable_ifaddr(ifap
->ifa_name
, ifap
->ifa_addr
)) {
184 plog(LLV_DEBUG2
, LOCATION
, NULL
,
185 "unsuitable address: %s %s\n",
187 saddrwop2str(ifap
->ifa_addr
));
191 p
= find_myaddr(ifap
->ifa_addr
, 0);
195 q
= find_myaddr(ifap
->ifa_addr
, 1);
202 plog(LLV_ERROR2
, LOCATION
, NULL
,
203 "unable to allocate space for addr.\n");
207 p
->addr
= dupsaddr(ifap
->ifa_addr
);
208 if (p
->addr
== NULL
) {
209 plog(LLV_ERROR2
, LOCATION
, NULL
,
210 "unable to duplicate addr.\n");
214 p
->ifname
= racoon_strdup(ifap
->ifa_name
);
215 if (p
->ifname
== NULL
) {
216 plog(LLV_ERROR2
, LOCATION
, NULL
,
217 "unable to duplicate ifname.\n");
225 if (getnameinfo(p
->addr
, p
->addr
->sa_len
,
226 addr1
, sizeof(addr1
),
228 NI_NUMERICHOST
| niflags
))
229 strlcpy(addr1
, "(invalid)", sizeof(addr1
));
230 plog(LLV_DEBUG
, LOCATION
, NULL
,
231 "my interface: %s (%s)\n",
232 addr1
, ifap
->ifa_name
);
234 p
->next
= lcconf
->myaddrs
;
238 if (natt_enabled_in_rmconf ()) {
241 plog(LLV_ERROR2
, LOCATION
, NULL
,
242 "unable to allocate space for natt addr.\n");
255 #else /*!HAVE_GETIFADDRS*/
256 #error "NOT SUPPORTED"
257 #endif /*HAVE_GETIFADDRS*/
262 * check the interface is suitable or not
265 suitable_ifaddr(ifname
, ifaddr
)
267 const struct sockaddr
*ifaddr
;
269 #if 0 //we need to be able to do nested ipsec for BTMM... stub out ifdef ENABLE_HYBRID
270 /* Exclude any address we got through ISAKMP mode config */
271 if (exclude_cfg_addr(ifaddr
) == 0)
274 switch(ifaddr
->sa_family
) {
279 return suitable_ifaddr6(ifname
, ifaddr
);
289 suitable_ifaddr6(ifname
, ifaddr
)
291 const struct sockaddr
*ifaddr
;
293 struct in6_ifreq ifr6
;
296 if (ifaddr
->sa_family
!= AF_INET6
)
299 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
301 plog(LLV_ERROR
, LOCATION
, NULL
,
302 "socket(SOCK_DGRAM) failed:%s\n", strerror(errno
));
306 memset(&ifr6
, 0, sizeof(ifr6
));
307 strlcpy(ifr6
.ifr_name
, ifname
, sizeof(ifr6
.ifr_name
));
309 ifr6
.ifr_addr
= *(const struct sockaddr_in6
*)ifaddr
;
311 if (ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) < 0) {
312 plog(LLV_ERROR
, LOCATION
, NULL
,
313 "ioctl(SIOCGIFAFLAG_IN6) failed:%s\n", strerror(errno
));
320 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DUPLICATED
321 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DETACHED
322 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_ANYCAST
)
335 struct rt_msghdr
*rtm
;
337 len
= read(lcconf
->rtsock
, msg
, sizeof(msg
));
339 plog(LLV_ERROR
, LOCATION
, NULL
,
340 "read(PF_ROUTE) failed: %s\n",
344 rtm
= (struct rt_msghdr
*)msg
;
345 if (len
< rtm
->rtm_msglen
) {
346 plog(LLV_ERROR
, LOCATION
, NULL
,
347 "read(PF_ROUTE) short read\n");
350 if (rtm
->rtm_version
!= RTM_VERSION
) {
351 plog(LLV_ERROR
, LOCATION
, NULL
,
352 "routing socket version mismatch\n");
353 close(lcconf
->rtsock
);
357 switch (rtm
->rtm_type
) {
364 /* ignore this message silently */
367 plog(LLV_DEBUG
, LOCATION
, NULL
,
368 "msg %d not interesting\n", rtm
->rtm_type
);
371 /* XXX more filters here? */
373 plog(LLV_DEBUG
, LOCATION
, NULL
,
374 "caught rtm:%d, need update interface address list\n",
381 * initialize default port for ISAKMP to send, if no "listen"
382 * directive is specified in config file.
384 * DO NOT listen to wildcard addresses. if you receive packets to
385 * wildcard address, you'll be in trouble (DoS attack possible by
389 autoconf_myaddrsport()
394 plog(LLV_DEBUG
, LOCATION
, NULL
,
395 "configuring default isakmp port.\n");
397 for (p
= lcconf
->myaddrs
, n
= 0; p
; p
= p
->next
, n
++) {
398 set_port (p
->addr
, p
->udp_encap
? lcconf
->port_isakmp_natt
: lcconf
->port_isakmp
);
400 plog(LLV_DEBUG
, LOCATION
, NULL
,
401 "%d addrs are configured successfully\n", n
);
407 * get a port number to which racoon binded.
408 * NOTE: network byte order returned.
411 getmyaddrsport(local
)
412 struct sockaddr
*local
;
414 struct myaddrs
*p
, *bestmatch
= NULL
;
415 u_short bestmatch_port
= PORT_ISAKMP
;
417 /* get a relative port */
418 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
421 if (!cmpsaddrwop(local
, p
->addr
)) {
427 switch (p
->addr
->sa_family
) {
429 if (((struct sockaddr_in
*)p
->addr
)->sin_port
== PORT_ISAKMP
) {
431 bestmatch_port
= ((struct sockaddr_in
*)p
->addr
)->sin_port
;
437 if (((struct sockaddr_in6
*)p
->addr
)->sin6_port
== PORT_ISAKMP
) {
439 bestmatch_port
= ((struct sockaddr_in6
*)p
->addr
)->sin6_port
;
445 plog(LLV_ERROR
, LOCATION
, NULL
,
446 "unsupported AF %d\n", p
->addr
->sa_family
);
452 return htons(bestmatch_port
);
460 new = racoon_calloc(1, sizeof(*new));
462 plog(LLV_ERROR
, LOCATION
, NULL
,
463 "failed to allocate buffer for myaddrs.\n");
477 dupmyaddr(struct myaddrs
*old
)
481 new = racoon_calloc(1, sizeof(*new));
483 plog(LLV_ERROR
, LOCATION
, NULL
,
484 "failed to allocate buffer for myaddrs.\n");
488 /* Copy the whole structure and set the differences. */
489 memcpy (new, old
, sizeof (*new));
490 new->addr
= dupsaddr (old
->addr
);
491 if (new->addr
== NULL
) {
492 plog(LLV_ERROR
, LOCATION
, NULL
,
493 "failed to allocate buffer for duplicate addr.\n");
498 new->ifname
= racoon_strdup(old
->ifname
);
499 if (new->ifname
== NULL
) {
500 plog(LLV_ERROR
, LOCATION
, NULL
,
501 "failed to allocate buffer for duplicate ifname.\n");
502 racoon_free(new->addr
);
508 new->next
= old
->next
;
517 struct myaddrs
**head
;
525 struct myaddrs
*myaddr
;
528 racoon_free(myaddr
->addr
);
530 racoon_free(myaddr
->ifname
);
537 /* initialize routing socket */
538 lcconf
->rtsock
= socket(PF_ROUTE
, SOCK_RAW
, PF_UNSPEC
);
539 if (lcconf
->rtsock
< 0) {
540 plog(LLV_ERROR
, LOCATION
, NULL
,
541 "socket(PF_ROUTE) failed: %s",
546 if (lcconf
->myaddrs
== NULL
&& lcconf
->autograbaddr
== 1) {
549 if (autoconf_myaddrsport() < 0)
556 /* select the socket to be sent */
557 /* should implement other method. */
562 struct myaddrs
*p
, *lastresort
= NULL
;
564 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
567 if (my
->sa_family
== p
->addr
->sa_family
) {
570 if (sysdep_sa_len(my
) == sysdep_sa_len(p
->addr
)
571 && memcmp(my
, p
->addr
, sysdep_sa_len(my
)) == 0) {
578 plog(LLV_ERROR
, LOCATION
, NULL
,
579 "no socket matches address family %d\n",