d12bf0bdfc9f2db633caa6fd8344c3096fe78029
[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 <sys/socket.h>
17 #include <sys/ioctl.h>
18 #include <linux/rtnetlink.h>
19 #ifdef HAVE_SYS_SOCKIO_H
20 #include <sys/sockio.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27
28 #include <freeswan.h>
29
30 #include <constants.h>
31 #include <defs.h>
32 #include <log.h>
33
34 #include "interfaces.h"
35 #include "exec.h"
36 #include "files.h"
37
38 /*
39 * Get the default route information via rtnetlink
40 */
41 void
42 get_defaultroute(defaultroute_t *defaultroute)
43 {
44 union {
45 struct {
46 struct nlmsghdr nh;
47 struct rtmsg rt;
48 } m;
49 char buf[4096];
50 } rtu;
51
52 struct nlmsghdr *nh;
53 uint32_t best_metric = ~0;
54 ssize_t msglen;
55 int fd;
56
57 bzero(&rtu, sizeof(rtu));
58 rtu.m.nh.nlmsg_len = NLMSG_LENGTH(sizeof(rtu.m.rt));
59 rtu.m.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
60 rtu.m.nh.nlmsg_type = RTM_GETROUTE;
61 rtu.m.rt.rtm_family = AF_INET;
62 rtu.m.rt.rtm_table = RT_TABLE_UNSPEC;
63 rtu.m.rt.rtm_protocol = RTPROT_UNSPEC;
64 rtu.m.rt.rtm_type = RTN_UNICAST;
65
66 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
67 if (fd == -1)
68 {
69 plog("could not create rtnetlink socket");
70 return;
71 }
72
73 if (send(fd, &rtu, rtu.m.nh.nlmsg_len, 0) == -1)
74 {
75 plog("could not write to rtnetlink socket");
76 close(fd);
77 return;
78 }
79
80 msglen = recv(fd, &rtu, sizeof(rtu), MSG_WAITALL);
81 if (msglen == -1)
82 {
83 plog("could not read from rtnetlink socket");
84 close(fd);
85 return;
86 }
87
88 close(fd);
89
90 for (nh = &rtu.m.nh; NLMSG_OK(nh, msglen); nh = NLMSG_NEXT(nh, msglen))
91 {
92 struct rtmsg *rt;
93 struct rtattr *rta;
94 uint32_t rtalen, metric = 0;
95 struct in_addr gw = { .s_addr = INADDR_ANY };
96 int iface_idx = -1;
97
98 if (nh->nlmsg_type == NLMSG_ERROR)
99 {
100 plog("error from rtnetlink");
101 return;
102 }
103
104 if (nh->nlmsg_type == NLMSG_DONE)
105 break;
106
107 rt = NLMSG_DATA(nh);
108 if ( rt->rtm_dst_len != 0
109 || (rt->rtm_table != RT_TABLE_MAIN
110 && rt->rtm_table != RT_TABLE_DEFAULT) )
111 continue;
112
113 rta = RTM_RTA(rt);
114 rtalen = RTM_PAYLOAD(nh);
115 while ( RTA_OK(rta, rtalen) )
116 {
117 switch (rta->rta_type)
118 {
119 case RTA_GATEWAY:
120 gw = *(struct in_addr *) RTA_DATA(rta);
121 break;
122 case RTA_OIF:
123 iface_idx = *(int *) RTA_DATA(rta);
124 break;
125 case RTA_PRIORITY:
126 metric = *(uint32_t *) RTA_DATA(rta);
127 break;
128 }
129 rta = RTA_NEXT(rta, rtalen);
130 }
131
132 if (metric < best_metric
133 && gw.s_addr != INADDR_ANY
134 && iface_idx != -1)
135 {
136 struct ifreq req;
137
138 fd = socket(AF_INET, SOCK_DGRAM, 0);
139 if (fd < 0)
140 {
141 plog("could not open AF_INET socket");
142 defaultroute->defined = FALSE;
143 break;
144 }
145 bzero(&req, sizeof(req));
146 req.ifr_ifindex = iface_idx;
147 ioctl(fd, SIOCGIFNAME, &req);
148 ioctl(fd, SIOCGIFADDR, &req);
149 close(fd);
150
151 strncpy(defaultroute->iface, req.ifr_name, IFNAMSIZ);
152 defaultroute->addr.u.v4 = *((struct sockaddr_in *) &req.ifr_addr);
153 defaultroute->nexthop.u.v4.sin_family = AF_INET;
154 defaultroute->nexthop.u.v4.sin_addr = gw;
155
156 DBG(DBG_CONTROL,
157 char addr[20];
158 char nexthop[20];
159 addrtot(&defaultroute->addr, 0, addr, sizeof(addr));
160 addrtot(&defaultroute->nexthop, 0, nexthop, sizeof(nexthop));
161
162 DBG_log(
163 ( !defaultroute->defined
164 ? "Default route found: iface=%s, addr=%s, nexthop=%s"
165 : "Better default route: iface=%s, addr=%s, nexthop=%s"
166 ), defaultroute->iface, addr, nexthop
167 )
168 );
169
170 best_metric = metric;
171 defaultroute->defined = TRUE;
172 }
173 }
174
175 if (!defaultroute->defined)
176 plog("no default route - cannot cope with %%defaultroute!!!");
177 }