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 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
41 #include <net/if_var.h>
43 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)
44 #include <netinet/in.h>
45 #include <netinet6/in6_var.h>
47 #include <net/route.h>
57 #ifdef HAVE_GETIFADDRS
69 #include "localconf.h"
71 #include "grabmyaddr.h"
73 #include "isakmp_var.h"
75 #include "nattraversal.h"
78 #include <linux/types.h>
79 #include <linux/rtnetlink.h>
80 #ifndef HAVE_GETIFADDRS
81 #define HAVE_GETIFADDRS
82 #define NEED_LINUX_GETIFADDRS
86 #ifndef HAVE_GETIFADDRS
87 static unsigned int if_maxindex
__P((void));
90 static struct myaddrs
*find_myaddr
__P((struct myaddrs
*, struct sockaddr
*, int));
92 static struct myaddrs
*find_myaddr
__P((struct myaddrs
*, struct myaddrs
*));
94 static int suitable_ifaddr
__P((const char *, const struct sockaddr
*));
96 static int suitable_ifaddr6
__P((const char *, const struct sockaddr
*));
99 #ifdef NEED_LINUX_GETIFADDRS
101 /* We could do this _much_ better. kame racoon in its current form
102 * will esentially die at frequent changes of address configuration.
107 struct ifaddrs
*ifa_next
;
110 struct sockaddr
*ifa_addr
;
111 struct sockaddr_storage ifa_addrbuf
;
114 static int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
116 while (RTA_OK(rta
, len
)) {
117 if (rta
->rta_type
<= max
)
118 tb
[rta
->rta_type
] = rta
;
119 rta
= RTA_NEXT(rta
,len
);
124 static void recvaddrs(int fd
, struct ifaddrs
**ifa
, __u32 seq
)
127 struct sockaddr_nl nladdr
;
128 struct iovec iov
= { buf
, sizeof(buf
) };
130 struct rtattr
* rta_tb
[IFA_MAX
+1];
137 struct msghdr msg
= {
138 (void*)&nladdr
, sizeof(nladdr
),
144 status
= recvmsg(fd
, &msg
, 0);
152 if (nladdr
.nl_pid
) /* Message not from kernel */
155 h
= (struct nlmsghdr
*)buf
;
156 while (NLMSG_OK(h
, status
)) {
157 if (h
->nlmsg_seq
!= seq
)
160 if (h
->nlmsg_type
== NLMSG_DONE
)
163 if (h
->nlmsg_type
== NLMSG_ERROR
)
166 if (h
->nlmsg_type
!= RTM_NEWADDR
)
171 if (m
->ifa_family
!= AF_INET
&&
172 m
->ifa_family
!= AF_INET6
)
175 if (m
->ifa_flags
&IFA_F_TENTATIVE
)
178 memset(rta_tb
, 0, sizeof(rta_tb
));
179 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(m
), h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*m
)));
181 if (rta_tb
[IFA_LOCAL
] == NULL
)
182 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
183 if (rta_tb
[IFA_LOCAL
] == NULL
)
186 I
= malloc(sizeof(struct ifaddrs
));
189 memset(I
, 0, sizeof(*I
));
191 I
->ifa_ifindex
= m
->ifa_index
;
192 I
->ifa_addr
= (struct sockaddr
*)&I
->ifa_addrbuf
;
193 I
->ifa_addr
->sa_family
= m
->ifa_family
;
194 if (m
->ifa_family
== AF_INET
) {
195 struct sockaddr_in
*sin
= (void*)I
->ifa_addr
;
196 memcpy(&sin
->sin_addr
, RTA_DATA(rta_tb
[IFA_LOCAL
]), 4);
198 struct sockaddr_in6
*sin
= (void*)I
->ifa_addr
;
199 memcpy(&sin
->sin6_addr
, RTA_DATA(rta_tb
[IFA_LOCAL
]), 16);
200 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
))
201 sin
->sin6_scope_id
= I
->ifa_ifindex
;
207 h
= NLMSG_NEXT(h
, status
);
209 if (msg
.msg_flags
& MSG_TRUNC
)
215 static int getifaddrs(struct ifaddrs
**ifa0
)
221 struct sockaddr_nl nladdr
;
226 fd
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
230 memset(&nladdr
, 0, sizeof(nladdr
));
231 nladdr
.nl_family
= AF_NETLINK
;
233 req
.nlh
.nlmsg_len
= sizeof(req
);
234 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
235 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
;
236 req
.nlh
.nlmsg_pid
= 0;
237 req
.nlh
.nlmsg_seq
= ++seq
;
238 req
.g
.rtgen_family
= AF_UNSPEC
;
240 if (sendto(fd
, (void*)&req
, sizeof(req
), 0, (struct sockaddr
*)&nladdr
, sizeof(nladdr
)) < 0) {
247 recvaddrs(fd
, ifa0
, seq
);
251 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
253 for (i
=*ifa0
; i
; i
= i
->ifa_next
) {
255 ifr
.ifr_ifindex
= i
->ifa_ifindex
;
256 ioctl(fd
, SIOCGIFNAME
, (void*)&ifr
);
257 memcpy(i
->ifa_name
, ifr
.ifr_name
, 16);
264 static void freeifaddrs(struct ifaddrs
*ifa0
)
277 #ifndef HAVE_GETIFADDRS
281 struct if_nameindex
*p
, *p0
;
282 unsigned int max
= 0;
285 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
286 if (max
< p
->if_index
)
289 if_freenameindex(p0
);
298 struct myaddrs
*p
, *next
;
300 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
304 racoon_free(p
->addr
);
308 lcconf
->myaddrs
= NULL
;
313 static struct myaddrs
*
314 find_myaddr(db
, addr
, udp_encap
)
316 struct sockaddr
*addr
;
320 char h1
[NI_MAXHOST
], h2
[NI_MAXHOST
];
322 if (getnameinfo(addr
, sysdep_sa_len(addr
), h1
, sizeof(h1
), NULL
, 0,
323 NI_NUMERICHOST
| niflags
) != 0)
326 for (q
= db
; q
; q
= q
->next
) {
329 if (q
->udp_encap
&& !udp_encap
330 || !q
->udp_encap
&& udp_encap
)
332 if (addr
->sa_family
!= q
->addr
->sa_family
)
334 if (getnameinfo(q
->addr
, sysdep_sa_len(q
->addr
), h2
, sizeof(h2
),
335 NULL
, 0, NI_NUMERICHOST
| niflags
) != 0)
337 if (strcmp(h1
, h2
) == 0)
345 // modified to avoid closing and opening sockets for
346 // all interfaces each time an interface change occurs.
347 // on return: addrcount = zero indicates address no longer used
348 // sock = -1 indicates a new address - no socket opened yet.
352 #ifdef HAVE_GETIFADDRS
353 struct myaddrs
*p
, *q
;
354 struct ifaddrs
*ifa0
, *ifap
;
356 struct sockaddr_in6
*sin6
;
359 char addr1
[NI_MAXHOST
];
361 if (getifaddrs(&ifa0
)) {
362 plog(LLV_ERROR2
, LOCATION
, NULL
,
363 "getifaddrs failed: %s\n", strerror(errno
));
368 // clear the in_use flag for each address in the list
369 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
)
372 for (ifap
= ifa0
; ifap
; ifap
= ifap
->ifa_next
) {
374 if (ifap
->ifa_addr
->sa_family
!= AF_INET
376 && ifap
->ifa_addr
->sa_family
!= AF_INET6
381 if (!suitable_ifaddr(ifap
->ifa_name
, ifap
->ifa_addr
)) {
382 plog(LLV_ERROR
, LOCATION
, NULL
,
383 "unsuitable address: %s %s\n",
385 saddrwop2str(ifap
->ifa_addr
));
389 p
= find_myaddr(lcconf
->myaddrs
, ifap
->ifa_addr
, 0);
393 q
= find_myaddr(lcconf
->myaddrs
, ifap
->ifa_addr
, 1);
400 plog(LLV_ERROR2
, LOCATION
, NULL
,
401 "unable to allocate space for addr.\n");
405 p
->addr
= dupsaddr(ifap
->ifa_addr
);
406 if (p
->addr
== NULL
) {
407 plog(LLV_ERROR2
, LOCATION
, NULL
,
408 "unable to duplicate addr.\n");
415 if (getnameinfo(p
->addr
, p
->addr
->sa_len
,
416 addr1
, sizeof(addr1
),
418 NI_NUMERICHOST
| niflags
))
419 strlcpy(addr1
, "(invalid)", sizeof(addr1
));
420 plog(LLV_DEBUG
, LOCATION
, NULL
,
421 "my interface: %s (%s)\n",
422 addr1
, ifap
->ifa_name
);
424 p
->next
= lcconf
->myaddrs
;
428 if (natt_enabled_in_rmconf ()) {
431 plog(LLV_ERROR2
, LOCATION
, NULL
,
432 "unable to allocate space for natt addr.\n");
445 #else /*!HAVE_GETIFADDRS*/
446 #error "NOT SUPPORTED"
447 #endif /*HAVE_GETIFADDRS*/
452 * check the interface is suitable or not
455 suitable_ifaddr(ifname
, ifaddr
)
457 const struct sockaddr
*ifaddr
;
460 /* Exclude any address we got through ISAKMP mode config */
461 if (exclude_cfg_addr(ifaddr
) == 0)
464 switch(ifaddr
->sa_family
) {
469 return suitable_ifaddr6(ifname
, ifaddr
);
479 suitable_ifaddr6(ifname
, ifaddr
)
481 const struct sockaddr
*ifaddr
;
484 struct in6_ifreq ifr6
;
488 if (ifaddr
->sa_family
!= AF_INET6
)
492 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
494 plog(LLV_ERROR
, LOCATION
, NULL
,
495 "socket(SOCK_DGRAM) failed:%s\n", strerror(errno
));
499 memset(&ifr6
, 0, sizeof(ifr6
));
500 strncpy(ifr6
.ifr_name
, ifname
, strlen(ifname
));
502 ifr6
.ifr_addr
= *(const struct sockaddr_in6
*)ifaddr
;
504 if (ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) < 0) {
505 plog(LLV_ERROR
, LOCATION
, NULL
,
506 "ioctl(SIOCGIFAFLAG_IN6) failed:%s\n", strerror(errno
));
513 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DUPLICATED
514 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DETACHED
515 || ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_ANYCAST
)
530 struct nlmsghdr
*h
= (void*)msg
;
531 len
= read(lcconf
->rtsock
, msg
, sizeof(msg
));
533 return errno
== ENOBUFS
;
534 if (len
< sizeof(*h
))
536 if (h
->nlmsg_pid
) /* not from kernel! */
538 if (h
->nlmsg_type
== RTM_NEWLINK
)
540 plog(LLV_DEBUG
, LOCATION
, NULL
,
541 "netlink signals update interface address list\n");
546 struct rt_msghdr
*rtm
;
548 len
= read(lcconf
->rtsock
, msg
, sizeof(msg
));
550 plog(LLV_ERROR
, LOCATION
, NULL
,
551 "read(PF_ROUTE) failed: %s\n",
555 rtm
= (struct rt_msghdr
*)msg
;
556 if (len
< rtm
->rtm_msglen
) {
557 plog(LLV_ERROR
, LOCATION
, NULL
,
558 "read(PF_ROUTE) short read\n");
561 if (rtm
->rtm_version
!= RTM_VERSION
) {
562 plog(LLV_ERROR
, LOCATION
, NULL
,
563 "routing socket version mismatch\n");
564 close(lcconf
->rtsock
);
568 switch (rtm
->rtm_type
) {
575 /* ignore this message silently */
578 plog(LLV_DEBUG
, LOCATION
, NULL
,
579 "msg %d not interesting\n", rtm
->rtm_type
);
582 /* XXX more filters here? */
584 plog(LLV_DEBUG
, LOCATION
, NULL
,
585 "caught rtm:%d, need update interface address list\n",
589 #endif /* __linux__ */
593 * initialize default port for ISAKMP to send, if no "listen"
594 * directive is specified in config file.
596 * DO NOT listen to wildcard addresses. if you receive packets to
597 * wildcard address, you'll be in trouble (DoS attack possible by
601 autoconf_myaddrsport()
606 plog(LLV_DEBUG
, LOCATION
, NULL
,
607 "configuring default isakmp port.\n");
609 for (p
= lcconf
->myaddrs
, n
= 0; p
; p
= p
->next
, n
++) {
610 set_port (p
->addr
, p
->udp_encap
? lcconf
->port_isakmp_natt
: lcconf
->port_isakmp
);
612 plog(LLV_DEBUG
, LOCATION
, NULL
,
613 "%d addrs are configured successfully\n", n
);
619 * get a port number to which racoon binded.
620 * NOTE: network byte order returned.
623 getmyaddrsport(local
)
624 struct sockaddr
*local
;
626 struct myaddrs
*p
, *bestmatch
= NULL
;
627 u_short bestmatch_port
= PORT_ISAKMP
;
629 /* get a relative port */
630 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
633 if (!cmpsaddrwop(local
, p
->addr
)) {
639 switch (p
->addr
->sa_family
) {
641 if (((struct sockaddr_in
*)p
->addr
)->sin_port
== PORT_ISAKMP
) {
643 bestmatch_port
= ((struct sockaddr_in
*)p
->addr
)->sin_port
;
649 if (((struct sockaddr_in6
*)p
->addr
)->sin6_port
== PORT_ISAKMP
) {
651 bestmatch_port
= ((struct sockaddr_in6
*)p
->addr
)->sin6_port
;
657 plog(LLV_ERROR
, LOCATION
, NULL
,
658 "unsupported AF %d\n", p
->addr
->sa_family
);
664 return htons(bestmatch_port
);
672 new = racoon_calloc(1, sizeof(*new));
674 plog(LLV_ERROR
, LOCATION
, NULL
,
675 "failed to allocate buffer for myaddrs.\n");
686 dupmyaddr(struct myaddrs
*old
)
690 new = racoon_calloc(1, sizeof(*new));
692 plog(LLV_ERROR
, LOCATION
, NULL
,
693 "failed to allocate buffer for myaddrs.\n");
697 /* Copy the whole structure and set the differences. */
698 memcpy (new, old
, sizeof (*new));
699 new->addr
= dupsaddr (old
->addr
);
700 if (new->addr
== NULL
) {
701 plog(LLV_ERROR
, LOCATION
, NULL
,
702 "failed to allocate buffer for duplicate addr.\n");
706 new->next
= old
->next
;
715 struct myaddrs
**head
;
723 struct myaddrs
*myaddr
;
726 racoon_free(myaddr
->addr
);
733 /* initialize routing socket */
734 lcconf
->rtsock
= socket(PF_ROUTE
, SOCK_RAW
, PF_UNSPEC
);
735 if (lcconf
->rtsock
< 0) {
736 plog(LLV_ERROR
, LOCATION
, NULL
,
737 "socket(PF_ROUTE) failed: %s",
744 struct sockaddr_nl nl
;
747 memset(&nl
, 0, sizeof(nl
));
748 nl
.nl_family
= AF_NETLINK
;
749 nl
.nl_groups
= RTMGRP_IPV4_IFADDR
|RTMGRP_LINK
;
751 if (bind(lcconf
->rtsock
, (struct sockaddr
*)&nl
, sizeof(nl
)) < 0) {
752 plog(LLV_ERROR
, LOCATION
, NULL
,
753 "bind(PF_NETLINK) failed: %s\n",
757 addr_len
= sizeof(nl
);
758 if (getsockname(lcconf
->rtsock
, (struct sockaddr
*)&nl
, &addr_len
) < 0) {
759 plog(LLV_ERROR
, LOCATION
, NULL
,
760 "getsockname(PF_NETLINK) failed: %s\n",
767 if (lcconf
->myaddrs
== NULL
&& lcconf
->autograbaddr
== 1) {
770 if (autoconf_myaddrsport() < 0)
777 /* select the socket to be sent */
778 /* should implement other method. */
783 struct myaddrs
*p
, *lastresort
= NULL
;
784 #if defined(INET6) && defined(__linux__)
785 struct myaddrs
*match_wo_scope_id
= NULL
;
786 int check_wo_scope_id
= (my
->sa_family
== AF_INET6
) &&
787 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6
*)my
)->sin6_addr
);
790 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
793 if (my
->sa_family
== p
->addr
->sa_family
) {
796 if (sysdep_sa_len(my
) == sysdep_sa_len(p
->addr
)
797 && memcmp(my
, p
->addr
, sysdep_sa_len(my
)) == 0) {
800 #if defined(INET6) && defined(__linux__)
801 if (check_wo_scope_id
&& IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
) &&
802 /* XXX: this depends on sin6_scope_id to be last
803 * item in struct sockaddr_in6 */
805 sysdep_sa_len(my
) - sizeof(uint32_t)) == 0) {
806 match_wo_scope_id
= p
;
810 #if defined(INET6) && defined(__linux__)
812 p
= match_wo_scope_id
;
817 plog(LLV_ERROR
, LOCATION
, NULL
,
818 "no socket matches address family %d\n",