summaryrefslogtreecommitdiff
path: root/src/pluto/virtual.c
blob: 2067bde01f73ee08ebcb93e5323b18670b99b927 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* FreeS/WAN Virtual IP Management
 * Copyright (C) 2002 Mathieu Lafon - Arkoon Network Security
 *
 * 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 <freeswan.h>

#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>

#include "constants.h"
#include "defs.h"
#include "log.h"
#include "connections.h"
#include "whack.h"
#include "virtual.h"

#define F_VIRTUAL_NO          1
#define F_VIRTUAL_DHCP        2
#define F_VIRTUAL_IKE_CONFIG  4
#define F_VIRTUAL_PRIVATE     8
#define F_VIRTUAL_ALL         16
#define F_VIRTUAL_HOST        32

struct virtual_t {
	unsigned short flags;
	unsigned short n_net;
	ip_subnet net[0];
};

static ip_subnet *private_net_ok=NULL, *private_net_ko=NULL;
static unsigned short private_net_ok_len=0, private_net_ko_len=0;

/**
 * read %v4:x.x.x.x/y or %v6:xxxxxxxxx/yy
 * or %v4:!x.x.x.x/y if dstko not NULL
 */
static bool
_read_subnet(const char *src, size_t len, ip_subnet *dst, ip_subnet *dstko,
	bool *isok)
{
	bool ok;
	int af;

	if ((len > 4) && (strneq(src, "%v4:", 4)))
	{
		af = AF_INET;
	}
	else if ((len > 4) && (strneq(src, "%v6:", 4)))
	{
		af = AF_INET6;
	}
	else
	{
		return FALSE;
	}

	ok = (src[4] != '!');
	src += ok ? 4 : 5;
	len -= ok ? 4 : 5;

	if (!len)
		return FALSE;
	if (!ok && !dstko)
		return FALSE;

	passert ( ((ok)?(dst):(dstko))!=NULL );

	if (ttosubnet(src, len, af, ((ok)?(dst):(dstko))))
	{
		return FALSE;
	}
	if (isok)
		*isok = ok;
	return TRUE;
}

void
init_virtual_ip(const char *private_list)
{
	const char *next, *str=private_list;
	unsigned short ign = 0, i_ok, i_ko;
	ip_subnet sub;
	bool ok;

	/** Count **/
	private_net_ok_len=0;
	private_net_ko_len=0;

	while (str)
	{
		next = strchr(str,',');
		if (!next)
			next = str + strlen(str);
		if (_read_subnet(str, next-str, &sub, &sub, &ok))
			if (ok)
				private_net_ok_len++;
			else
				private_net_ko_len++;
		else
			ign++;
		str = *next ? next+1 : NULL;
	}

	if (!ign)
	{
		/** Allocate **/
		if (private_net_ok_len)
		{
			private_net_ok = (ip_subnet *)malloc(private_net_ok_len * sizeof(ip_subnet));
		}
		if (private_net_ko_len)
		{
			private_net_ko = (ip_subnet *)malloc(private_net_ko_len * sizeof(ip_subnet));
		}
		if ((private_net_ok_len && !private_net_ok)
		||  (private_net_ko_len && !private_net_ko))
		{
			loglog(RC_LOG_SERIOUS,
				"can't alloc in init_virtual_ip");
			free(private_net_ok);
			private_net_ok = NULL;
			free(private_net_ko);
			private_net_ko = NULL;
		}
		else
		{
			/** Fill **/
			str = private_list;
			i_ok = 0;
			i_ko = 0;

			while (str)
			{
				next = strchr(str,',');
				if (!next)
					next = str + strlen(str);
				if (_read_subnet(str, next-str,
				   &(private_net_ok[i_ok]), &(private_net_ko[i_ko]), &ok))
				{
					if (ok)
						i_ok++;
					else
						i_ko++;
				}
				str = *next ? next+1 : NULL;
			}
		}
	}
	else
		loglog(RC_LOG_SERIOUS,
			"%d bad entries in virtual_private - none loaded", ign);
}

/**
 * virtual string must be :
 * {vhost,vnet}:[%method]*
 *
 * vhost = accept only a host (/32)
 * vnet  = accept any network
 *
 * %no   = no virtual IP (accept public IP)
 * %dhcp = accept DHCP SA (0.0.0.0/0) of affected IP  [not implemented]
 * %ike  = accept affected IKE Config Mode IP         [not implemented]
 * %priv = accept system-wide private net list
 * %v4:x = accept ipv4 in list 'x'
 * %v6:x = accept ipv6 in list 'x'
 * %all  = accept all ips                             [only for testing]
 *
 * ex: vhost:%no,%dhcp,%priv,%v4:192.168.1.0/24
 */
struct virtual_t
*create_virtual(const struct connection *c, const char *string)
{
	unsigned short flags=0, n_net=0, i;
	const char *str = string, *next, *first_net=NULL;
	ip_subnet sub;
	struct virtual_t *v;

	if (!string || string[0] == '\0')
		return NULL;

	if (strlen(string) >= 6 && strneq(string,"vhost:",6))
	{
		flags |= F_VIRTUAL_HOST;
		str += 6;
	}
	else if (strlen(string) >= 5 && strneq(string,"vnet:",5))
		str += 5;
	else
		goto fail;

	/**
	 * Parse string : fill flags & count subnets
	 */
	while ((str) && (*str))
	{
		next = strchr(str,',');
		if (!next) next = str + strlen(str);
		if (next-str == 3 && strneq(str, "%no", 3))
			flags |= F_VIRTUAL_NO;
#if 0
		else if (next-str == 4 && strneq(str, "%ike", 4))
			flags |= F_VIRTUAL_IKE_CONFIG;
		else if (next-str == 5 && strneq(str, "%dhcp", 5))
			flags |= F_VIRTUAL_DHCP;
#endif
		else if (next-str == 5 && strneq(str, "%priv", 5))
			flags |= F_VIRTUAL_PRIVATE;
		else if (next-str == 4 && strneq(str, "%all", 4))
			flags |= F_VIRTUAL_ALL;
		else if (_read_subnet(str, next-str, &sub, NULL, NULL))
		{
			n_net++;
			if (!first_net)
				first_net = str;
		}
		else
			goto fail;
		
		str = *next ? next+1 : NULL;
	}

	v = (struct virtual_t *)malloc(sizeof(struct virtual_t) +
								  (n_net * sizeof(ip_subnet)));
	if (!v) goto fail;

	v->flags = flags;
	v->n_net = n_net;
	if (n_net && first_net)
	{
		/**
		 * Save subnets in newly allocated struct
		 */
		for (str = first_net, i = 0; str && *str; )
		{
			next = strchr(str,',');
			if (!next) next = str + strlen(str);
			if (_read_subnet(str, next-str, &(v->net[i]), NULL, NULL))
				i++;
			str = *next ? next+1 : NULL;
		}
	}

	return v;

fail:
	plog("invalid virtual string [%s] - "
		"virtual selection disabled for connection '%s'", string, c->name);
	return NULL;
}

bool
is_virtual_end(const struct end *that)
{
	return ((that->virt)?TRUE:FALSE);
}

bool
is_virtual_connection(const struct connection *c)
{
	return ((c->spd.that.virt)?TRUE:FALSE);
}

static bool
net_in_list(const ip_subnet *peer_net, const ip_subnet *list,
	unsigned short len)
{
	unsigned short i;

	if (!list || !len)
		return FALSE;

	for (i = 0; i < len; i++)
	{
		if (subnetinsubnet(peer_net, &(list[i])))
			return TRUE;
	}
	return FALSE;
}

bool
is_virtual_net_allowed(const struct connection *c, const ip_subnet *peer_net,
		const ip_address *his_addr)
{
	if (c->spd.that.virt == NULL)
		return FALSE;

	if ((c->spd.that.virt->flags & F_VIRTUAL_HOST)
	&&  !subnetishost(peer_net))
		return FALSE;

	if ((c->spd.that.virt->flags & F_VIRTUAL_NO)
	&&  subnetishost(peer_net) &&  addrinsubnet(his_addr, peer_net))
		return TRUE;

	if ((c->spd.that.virt->flags & F_VIRTUAL_PRIVATE)
	&&   net_in_list(peer_net, private_net_ok, private_net_ok_len)
	&&  !net_in_list(peer_net, private_net_ko, private_net_ko_len))
		return TRUE;

	if (c->spd.that.virt->n_net
	&&  net_in_list(peer_net, c->spd.that.virt->net, c->spd.that.virt->n_net))
		return TRUE;
	
	if (c->spd.that.virt->flags & F_VIRTUAL_ALL)
	{
		/** %all must only be used for testing - log it **/
		loglog(RC_LOG_SERIOUS, "Warning - "
			"v%s:%%all must only be used for testing",
			(c->spd.that.virt->flags & F_VIRTUAL_HOST) ? "host" : "net");
		return TRUE;
	}

	return FALSE;
}