Disable rtnetlink defaultroute lookup if pluto is disabled
[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 bzero(&rtu, 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 && gw.s_addr != INADDR_ANY
136 && iface_idx != -1)
137 {
138 struct ifreq req;
139
140 fd = socket(AF_INET, SOCK_DGRAM, 0);
141 if (fd < 0)
142 {
143 plog("could not open AF_INET socket");
144 defaultroute->defined = FALSE;
145 break;
146 }
147 bzero(&req, sizeof(req));
148 req.ifr_ifindex = iface_idx;
149 ioctl(fd, SIOCGIFNAME, &req);
150 ioctl(fd, SIOCGIFADDR, &req);
151 close(fd);
152
153 strncpy(defaultroute->iface, req.ifr_name, IFNAMSIZ);
154 defaultroute->addr.u.v4 = *((struct sockaddr_in *) &req.ifr_addr);
155 defaultroute->nexthop.u.v4.sin_family = AF_INET;
156 defaultroute->nexthop.u.v4.sin_addr = gw;
157
158 DBG(DBG_CONTROL,
159 char addr[20];
160 char nexthop[20];
161 addrtot(&defaultroute->addr, 0, addr, sizeof(addr));
162 addrtot(&defaultroute->nexthop, 0, nexthop, sizeof(nexthop));
163
164 DBG_log(
165 ( !defaultroute->defined
166 ? "Default route found: iface=%s, addr=%s, nexthop=%s"
167 : "Better default route: iface=%s, addr=%s, nexthop=%s"
168 ), defaultroute->iface, addr, nexthop
169 )
170 );
171
172 best_metric = metric;
173 defaultroute->defined = TRUE;
174 }
175 }
176
177 if (!defaultroute->defined)
178 plog("no default route - cannot cope with %%defaultroute!!!");
179 }
180
181 #else /* !START_PLUTO */
182
183 /**
184 * Pluto disabled, fall back to %any
185 */
186 void
187 get_defaultroute(defaultroute_t *defaultroute)
188 {
189 memset(defaultroute, 0, sizeof(defaultroute_t));
190 defaultroute->defined = TRUE;
191 plog("%%defaultroute not supported, fallback to %%any");
192 }
193 #endif /* START_PLUTO */
194