summaryrefslogtreecommitdiff
path: root/src/starter/interfaces.c
blob: 4a2ae0a5752fe16d85dffe50956e57fd3d586909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* strongSwan IPsec interfaces management
 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
 *               2009 Heiko Hund - Astaro AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <freeswan.h>

#include <constants.h>
#include <defs.h>
#include <log.h>

#include "interfaces.h"
#include "exec.h"
#include "files.h"

#ifdef START_PLUTO

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/rtnetlink.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif

/*
 * Get the default route information via rtnetlink
 */
void
get_defaultroute(defaultroute_t *defaultroute)
{
	union {
		struct {
			struct nlmsghdr nh;
			struct rtmsg    rt;
		} m;
		char buf[4096];
	} rtu;

	struct nlmsghdr *nh;
	uint32_t best_metric = ~0;
	ssize_t msglen;
	int fd;

	memset(&rtu, 0, sizeof(rtu));
	rtu.m.nh.nlmsg_len = NLMSG_LENGTH(sizeof(rtu.m.rt));
	rtu.m.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
	rtu.m.nh.nlmsg_type = RTM_GETROUTE;
	rtu.m.rt.rtm_family = AF_INET;
	rtu.m.rt.rtm_table = RT_TABLE_UNSPEC;
	rtu.m.rt.rtm_protocol = RTPROT_UNSPEC;
	rtu.m.rt.rtm_type = RTN_UNICAST;

	fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
	if (fd == -1)
	{
		plog("could not create rtnetlink socket");
		return;
	}

	if (send(fd, &rtu, rtu.m.nh.nlmsg_len, 0) == -1)
	{
		plog("could not write to rtnetlink socket");
		close(fd);
		return;
	}

	msglen = recv(fd, &rtu, sizeof(rtu), MSG_WAITALL);
	if (msglen == -1)
	{
		plog("could not read from rtnetlink socket");
		close(fd);
		return;
	}

	close(fd);

	for (nh = &rtu.m.nh; NLMSG_OK(nh, msglen); nh = NLMSG_NEXT(nh, msglen))
	{
		struct rtmsg *rt;
		struct rtattr *rta;
		uint32_t rtalen, metric = 0;
		struct in_addr gw = { .s_addr = INADDR_ANY };
		int iface_idx = -1;

		if (nh->nlmsg_type == NLMSG_ERROR)
		{
			plog("error from rtnetlink");
			return;
		}

		if (nh->nlmsg_type == NLMSG_DONE)
			break;

		rt = NLMSG_DATA(nh);
		if ( rt->rtm_dst_len != 0
		||  (rt->rtm_table != RT_TABLE_MAIN
		  && rt->rtm_table != RT_TABLE_DEFAULT) )
			continue;

		rta = RTM_RTA(rt);
		rtalen = RTM_PAYLOAD(nh);
		while ( RTA_OK(rta, rtalen) )
		{
			switch (rta->rta_type)
			{
			case RTA_GATEWAY:
				gw = *(struct in_addr *) RTA_DATA(rta);
				break;
			case RTA_OIF:
				iface_idx = *(int *) RTA_DATA(rta);
				break;
			case RTA_PRIORITY:
				metric = *(uint32_t *) RTA_DATA(rta);
				break;
			}
			rta = RTA_NEXT(rta, rtalen);
		}

		if (metric < best_metric
		&&  iface_idx != -1)
		{
			struct ifreq req;

			fd = socket(AF_INET, SOCK_DGRAM, 0);
			if (fd < 0)
			{
				plog("could not open AF_INET socket");
				break;
			}
			memset(&req, 0, sizeof(req));
			req.ifr_ifindex = iface_idx;
			if (ioctl(fd, SIOCGIFNAME, &req) < 0 ||
				ioctl(fd, SIOCGIFADDR, &req) < 0)
			{
				plog("could not read interface data, ignoring route");
				close(fd);
				break;
			}

			strncpy(defaultroute->iface, req.ifr_name, IFNAMSIZ);
			defaultroute->iface[IFNAMSIZ-1] = '\0';
			defaultroute->addr.u.v4 = *((struct sockaddr_in *) &req.ifr_addr);
			defaultroute->nexthop.u.v4.sin_family = AF_INET;

			if (gw.s_addr == INADDR_ANY)
			{
				if (ioctl(fd, SIOCGIFDSTADDR, &req) < 0 ||
					((struct sockaddr_in*) &req.ifr_dstaddr)->sin_addr.s_addr == INADDR_ANY)
				{
					DBG_log("Ignoring default route to device %s because we can't get it's destination",
							req.ifr_name);
					close(fd);
					break;
				}

				defaultroute->nexthop.u.v4 = *((struct sockaddr_in *) &req.ifr_dstaddr);
			}
			else
				defaultroute->nexthop.u.v4.sin_addr = gw;

			close(fd);

			DBG(DBG_CONTROL,
				char addr[20];
				char nexthop[20];
				addrtot(&defaultroute->addr, 0, addr, sizeof(addr));
				addrtot(&defaultroute->nexthop, 0, nexthop, sizeof(nexthop));

				DBG_log(
					( !defaultroute->defined
					? "Default route found: iface=%s, addr=%s, nexthop=%s"
					: "Better default route: iface=%s, addr=%s, nexthop=%s"
					), defaultroute->iface, addr, nexthop
				)
			);

			best_metric = metric;
			defaultroute->defined = TRUE;
		}
	}
	defaultroute->supported = TRUE;

	if (!defaultroute->defined)
		plog("no default route - cannot cope with %%defaultroute!!!");
}

#else /* !START_PLUTO */

/**
 * Pluto disabled, fall back to %any
 */
void
get_defaultroute(defaultroute_t *defaultroute)
{
	defaultroute->supported = FALSE;
}
#endif /* START_PLUTO */