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
66 #include "localconf.h"
68 #include "grabmyaddr.h"
70 #include "isakmp_var.h"
72 #include "nattraversal.h"
74 #ifndef HAVE_GETIFADDRS
75 static unsigned int if_maxindex (void);
78 static int suitable_ifaddr (const char *, const struct sockaddr
*);
80 static int suitable_ifaddr6 (const char *, const struct sockaddr
*);
83 #ifndef HAVE_GETIFADDRS
87 struct if_nameindex
*p
, *p0
;
91 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
92 if (max
< p
->if_index
)
104 struct myaddrs
*p
, *next
;
106 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
112 lcconf
->myaddrs
= NULL
;
118 find_myaddr(addr
, udp_encap
)
119 struct sockaddr
*addr
;
123 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
125 if (getnameinfo(addr
, sysdep_sa_len(addr
), h1
, sizeof(h1
), NULL
, 0,
126 NI_NUMERICHOST
| niflags
) != 0)
129 for (q
= lcconf
->myaddrs
; q
; q
= q
->next
) {
132 if ((q
->udp_encap
&& !udp_encap
)
133 || (!q
->udp_encap
&& udp_encap
))
135 if (addr
->sa_family
!= q
->addr
->ss_family
)
137 if (getnameinfo((struct sockaddr
*)q
->addr
, sysdep_sa_len((struct sockaddr
*)q
->addr
), h2
, sizeof(h2
),
138 NULL
, 0, NI_NUMERICHOST
| niflags
) != 0)
140 if (strcmp(h1
, h2
) == 0)
148 // modified to avoid closing and opening sockets for
149 // all interfaces each time an interface change occurs.
150 // on return: addrcount = zero indicates address no longer used
151 // sock = -1 indicates a new address - no socket opened yet.
155 struct myaddrs
*p
, *q
;
156 struct ifaddrs
*ifa0
, *ifap
;
158 char addr1
[NI_MAXHOST
];
160 if (getifaddrs(&ifa0
)) {
162 "getifaddrs failed: %s\n", strerror(errno
));
167 // clear the in_use flag for each address in the list
168 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
)
171 for (ifap
= ifa0
; ifap
; ifap
= ifap
->ifa_next
) {
173 if (ifap
->ifa_addr
->sa_family
!= AF_INET
175 && ifap
->ifa_addr
->sa_family
!= AF_INET6
180 if (!suitable_ifaddr(ifap
->ifa_name
, ifap
->ifa_addr
)) {
181 plog(ASL_LEVEL_DEBUG
,
182 "unsuitable address: %s %s\n",
184 saddrwop2str(ifap
->ifa_addr
));
188 p
= find_myaddr(ifap
->ifa_addr
, 0);
192 q
= find_myaddr(ifap
->ifa_addr
, 1);
195 else if (natt_enabled_in_rmconf ()) {
199 "unable to allocate space for natt addr.\n");
209 "unable to allocate space for addr.\n");
213 p
->addr
= dupsaddr(ALIGNED_CAST(struct sockaddr_storage
*)ifap
->ifa_addr
);
214 if (p
->addr
== NULL
) {
216 "unable to duplicate addr.\n");
220 p
->ifname
= racoon_strdup(ifap
->ifa_name
);
221 if (p
->ifname
== NULL
) {
223 "unable to duplicate ifname.\n");
229 if (getnameinfo((struct sockaddr
*)p
->addr
, p
->addr
->ss_len
,
230 addr1
, sizeof(addr1
),
232 NI_NUMERICHOST
| niflags
))
233 strlcpy(addr1
, "(invalid)", sizeof(addr1
));
234 plog(ASL_LEVEL_DEBUG
,
235 "my interface: %s (%s)\n",
236 addr1
, ifap
->ifa_name
);
238 p
->next
= lcconf
->myaddrs
;
242 if (natt_enabled_in_rmconf ()) {
246 "unable to allocate space for natt addr.\n");
261 * check the interface is suitable or not
264 suitable_ifaddr(ifname
, ifaddr
)
266 const struct sockaddr
*ifaddr
;
268 #if 0 //we need to be able to do nested ipsec for BTMM... stub out ifdef ENABLE_HYBRID
269 /* Exclude any address we got through ISAKMP mode config */
270 if (exclude_cfg_addr(ifaddr
) == 0)
273 switch(ifaddr
->sa_family
) {
278 return suitable_ifaddr6(ifname
, ifaddr
);
288 suitable_ifaddr6(ifname
, ifaddr
)
290 const struct sockaddr
*ifaddr
;
292 struct in6_ifreq ifr6
;
295 if (ifaddr
->sa_family
!= AF_INET6
)
298 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
301 "socket(SOCK_DGRAM) failed:%s\n", strerror(errno
));
305 if (fcntl(s
, F_SETFL
, O_NONBLOCK
) == -1) {
306 plog(ASL_LEVEL_ERR
, "failed to put IPv6 socket in non-blocking mode\n");
309 memset(&ifr6
, 0, sizeof(ifr6
));
310 strlcpy(ifr6
.ifr_name
, ifname
, sizeof(ifr6
.ifr_name
));
312 memcpy(&ifr6
.ifr_addr
, ifaddr
, sizeof(struct sockaddr_in6
)); // Wcast-align fix - copy instread of assign with cast
314 if (ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) < 0) {
316 "ioctl(SIOCGIFAFLAG_IN6) failed:%s\n", strerror(errno
));
323 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DUPLICATED
324 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DETACHED
325 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_ANYCAST
326 /* Deprecated addresses will now be dropped by isakmp_close_unused */
327 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DEPRECATED
)
337 * initialize default port for ISAKMP to send, if no "listen"
338 * directive is specified in config file.
340 * DO NOT listen to wildcard addresses. if you receive packets to
341 * wildcard address, you'll be in trouble (DoS attack possible by
345 autoconf_myaddrsport()
350 plog(ASL_LEVEL_DEBUG
,
351 "configuring default isakmp port.\n");
353 for (p
= lcconf
->myaddrs
, n
= 0; p
; p
= p
->next
, n
++) {
354 set_port (p
->addr
, p
->udp_encap
? lcconf
->port_isakmp_natt
: lcconf
->port_isakmp
);
356 plog(ASL_LEVEL_DEBUG
,
357 "%d addrs are configured successfully\n", n
);
363 * get a port number to which racoon binded.
364 * NOTE: network byte order returned.
367 getmyaddrsport(local
)
368 struct sockaddr_storage
*local
;
370 struct myaddrs
*p
, *bestmatch
= NULL
;
371 u_short bestmatch_port
= PORT_ISAKMP
;
373 /* get a relative port */
374 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
377 if (!cmpsaddrwop(local
, p
->addr
)) {
383 switch (p
->addr
->ss_family
) {
385 if (((struct sockaddr_in
*)p
->addr
)->sin_port
== PORT_ISAKMP
) {
387 bestmatch_port
= ((struct sockaddr_in
*)p
->addr
)->sin_port
;
393 if (((struct sockaddr_in6
*)p
->addr
)->sin6_port
== PORT_ISAKMP
) {
395 bestmatch_port
= ((struct sockaddr_in6
*)p
->addr
)->sin6_port
;
402 "unsupported AF %d\n", p
->addr
->ss_family
);
408 return htons(bestmatch_port
);
416 new = racoon_calloc(1, sizeof(*new));
419 "failed to allocate buffer for myaddrs.\n");
435 dupmyaddr(struct myaddrs
*old
)
439 new = racoon_calloc(1, sizeof(*new));
442 "failed to allocate buffer for myaddrs.\n");
446 /* Copy the whole structure and set the differences. */
447 memcpy (new, old
, sizeof (*new));
448 new->addr
= dupsaddr (old
->addr
);
449 if (new->addr
== NULL
) {
451 "failed to allocate buffer for duplicate addr.\n");
456 new->ifname
= racoon_strdup(old
->ifname
);
457 if (new->ifname
== NULL
) {
459 "failed to allocate buffer for duplicate ifname.\n");
460 racoon_free(new->addr
);
468 new->next
= old
->next
;
477 struct myaddrs
**head
;
485 struct myaddrs
*myaddr
;
488 racoon_free(myaddr
->addr
);
490 racoon_free(myaddr
->ifname
);
495 update_myaddrs(void *unused
)
498 isakmp_close_unused();
499 autoconf_myaddrsport();
508 if (lcconf
->myaddrs
== NULL
&& lcconf
->autograbaddr
== 1) {
511 if (autoconf_myaddrsport() < 0)
519 /* select the socket to be sent */
520 /* should implement other method. */
522 getsockmyaddr(struct sockaddr
*my
)
524 struct myaddrs
*p
, *lastresort
= NULL
;
526 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
529 if (my
->sa_family
== p
->addr
->ss_family
) {
532 if (sysdep_sa_len(my
) == sysdep_sa_len((struct sockaddr
*)p
->addr
)
533 && memcmp(my
, p
->addr
, sysdep_sa_len(my
)) == 0) {
541 "no socket matches address family %d\n",
550 pfroute_handler(void *unused
)
553 struct rtmessage
{ // Wcast-align fix - force alignment
554 struct rt_msghdr rtm
;
555 char discard
[BUFSIZ
];
560 while((len
= read(lcconf
->rtsock
, &msg
, sizeof(msg
))) < 0) {
563 plog(ASL_LEVEL_DEBUG
,
564 "read(PF_ROUTE) failed: %s\n",
568 if (len
< msg
.rtm
.rtm_msglen
) {
569 plog(ASL_LEVEL_DEBUG
,
570 "read(PF_ROUTE) short read\n");
573 switch (msg
.rtm
.rtm_type
) {
580 /* ignore this message silently */
583 //plog(ASL_LEVEL_DEBUG,
584 // "msg %d not interesting\n", msg.rtm.rtm_type);
587 /* XXX more filters here? */
589 plog(ASL_LEVEL_DEBUG
,
590 "caught rtm:%d, need update interface address list\n",
593 // Interface changes occurred - update addrs
594 update_myaddrs(NULL
);
601 dispatch_source_cancel(lcconf
->rt_source
);
602 lcconf
->rt_source
= NULL
;
610 /* initialize routing socket */
611 lcconf
->rtsock
= socket(PF_ROUTE
, SOCK_RAW
, PF_UNSPEC
);
612 if (lcconf
->rtsock
< 0) {
613 plog(ASL_LEVEL_DEBUG
,
614 "socket(PF_ROUTE) failed: %s",
618 if (fcntl(lcconf
->rtsock
, F_SETFL
, O_NONBLOCK
) == -1) {
619 plog(ASL_LEVEL_DEBUG
, "failed to put PF_ROUTE socket in non-blocking mode\n");
622 lcconf
->rt_source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, lcconf
->rtsock
, 0, dispatch_get_main_queue());
623 if (lcconf
->rt_source
== NULL
) {
624 plog(ASL_LEVEL_DEBUG
, "could not create pfroute socket source.");
627 dispatch_source_set_event_handler_f(lcconf
->rt_source
, pfroute_handler
);
628 sock
= lcconf
->rtsock
;
629 dispatch_source_set_cancel_handler(lcconf
->rt_source
,
633 dispatch_resume(lcconf
->rt_source
);