Rebuild library.lo after changing ./configure options
[strongswan.git] / src / starter / interfaces.c
1 /* strongSwan IPsec interfaces management
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 * 2009 Heiko Hund - Astaro AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <errno.h>
20
21 #include <freeswan.h>
22
23 #include <constants.h>
24 #include <defs.h>
25 #include <log.h>
26
27 #include "interfaces.h"
28 #include "exec.h"
29 #include "files.h"
30
31 #ifdef START_PLUTO
32
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <linux/rtnetlink.h>
36 #ifdef HAVE_SYS_SOCKIO_H
37 #include <sys/sockio.h>
38 #endif
39
40 /*
41 * Get the default route information via rtnetlink
42 */
43 void
44 get_defaultroute(defaultroute_t *defaultroute)
45 {
46 union {
47 struct {
48 struct nlmsghdr nh;
49 struct rtmsg rt;
50 } m;
51 char buf[4096];
52 } rtu;
53
54 struct nlmsghdr *nh;
55 uint32_t best_metric = ~0;
56 ssize_t msglen;
57 int fd;
58
59 memset(&rtu, 0, sizeof(rtu));
60 rtu.m.nh.nlmsg_len = NLMSG_LENGTH(sizeof(rtu.m.rt));
61 rtu.m.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
62 rtu.m.nh.nlmsg_type = RTM_GETROUTE;
63 rtu.m.rt.rtm_family = AF_INET;
64 rtu.m.rt.rtm_table = RT_TABLE_UNSPEC;
65 rtu.m.rt.rtm_protocol = RTPROT_UNSPEC;
66 rtu.m.rt.rtm_type = RTN_UNICAST;
67
68 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
69 if (fd == -1)
70 {
71 plog("could not create rtnetlink socket");
72 return;
73 }
74
75 if (send(fd, &rtu, rtu.m.nh.nlmsg_len, 0) == -1)
76 {
77 plog("could not write to rtnetlink socket");
78 close(fd);
79 return;
80 }
81
82 msglen = recv(fd, &rtu, sizeof(rtu), MSG_WAITALL);
83 if (msglen == -1)
84 {
85 plog("could not read from rtnetlink socket");
86 close(fd);
87 return;
88 }
89
90 close(fd);
91
92 for (nh = &rtu.m.nh; NLMSG_OK(nh, msglen); nh = NLMSG_NEXT(nh, msglen))
93 {
94 struct rtmsg *rt;
95 struct rtattr *rta;
96 uint32_t rtalen, metric = 0;
97 struct in_addr gw = { .s_addr = INADDR_ANY };
98 int iface_idx = -1;
99
100 if (nh->nlmsg_type == NLMSG_ERROR)
101 {
102 plog("error from rtnetlink");
103 return;
104 }
105
106 if (nh->nlmsg_type == NLMSG_DONE)
107 break;
108
109 rt = NLMSG_DATA(nh);
110 if ( rt->rtm_dst_len != 0
111 || (rt->rtm_table != RT_TABLE_MAIN
112 && rt->rtm_table != RT_TABLE_DEFAULT) )
113 continue;
114
115 rta = RTM_RTA(rt);
116 rtalen = RTM_PAYLOAD(nh);
117 while ( RTA_OK(rta, rtalen) )
118 {
119 switch (rta->rta_type)
120 {
121 case RTA_GATEWAY:
122 gw = *(struct in_addr *) RTA_DATA(rta);
123 break;
124 case RTA_OIF:
125 iface_idx = *(int *) RTA_DATA(rta);
126 break;
127 case RTA_PRIORITY:
128 metric = *(uint32_t *) RTA_DATA(rta);
129 break;
130 }
131 rta = RTA_NEXT(rta, rtalen);
132 }
133
134 if (metric < best_metric
135 && iface_idx != -1)
136 {
137 struct ifreq req;
138
139 fd = socket(AF_INET, SOCK_DGRAM, 0);
140 if (fd < 0)
141 {
142 plog("could not open AF_INET socket");
143 break;
144 }
145 memset(&req, 0, sizeof(req));
146 req.ifr_ifindex = iface_idx;
147 if (ioctl(fd, SIOCGIFNAME, &req) < 0 ||
148 ioctl(fd, SIOCGIFADDR, &req) < 0)
149 {
150 plog("could not read interface data, ignoring route");
151 close(fd);
152 break;
153 }
154
155 strncpy(defaultroute->iface, req.ifr_name, IFNAMSIZ);
156 defaultroute->addr.u.v4 = *((struct sockaddr_in *) &req.ifr_addr);
157 defaultroute->nexthop.u.v4.sin_family = AF_INET;
158
159 if (gw.s_addr == INADDR_ANY)
160 {
161 if (ioctl(fd, SIOCGIFDSTADDR, &req) < 0 ||
162 ((struct sockaddr_in*) &req.ifr_dstaddr)->sin_addr.s_addr == INADDR_ANY)
163 {
164 DBG_log("Ignoring default route to device %s because we can't get it's destination",
165 req.ifr_name);
166 close(fd);
167 break;
168 }
169
170 defaultroute->nexthop.u.v4 = *((struct sockaddr_in *) &req.ifr_dstaddr);
171 }
172 else
173 defaultroute->nexthop.u.v4.sin_addr = gw;
174
175 close(fd);
176
177 DBG(DBG_CONTROL,
178 char addr[20];
179 char nexthop[20];
180 addrtot(&defaultroute->addr, 0, addr, sizeof(addr));
181 addrtot(&defaultroute->nexthop, 0, nexthop, sizeof(nexthop));
182
183 DBG_log(
184 ( !defaultroute->defined
185 ? "Default route found: iface=%s, addr=%s, nexthop=%s"
186 : "Better default route: iface=%s, addr=%s, nexthop=%s"
187 ), defaultroute->iface, addr, nexthop
188 )
189 );
190
191 best_metric = metric;
192 defaultroute->defined = TRUE;
193 }
194 }
195 defaultroute->supported = TRUE;
196
197 if (!defaultroute->defined)
198 plog("no default route - cannot cope with %%defaultroute!!!");
199 }
200
201 #else /* !START_PLUTO */
202
203 /**
204 * Pluto disabled, fall back to %any
205 */
206 void
207 get_defaultroute(defaultroute_t *defaultroute)
208 {
209 defaultroute->supported = FALSE;
210 }
211 #endif /* START_PLUTO */
212