aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/config/traffic_selector.c
blob: 0b819313592da7daf8dc93086690ef99fa9da15a (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
 * @file traffic_selector.c
 * 
 * @brief Implementation of traffic_selector_t.
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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 "traffic_selector.h"

#include <utils/linked_list.h>
#include <utils/allocator.h>
#include <utils/identification.h>
#include <arpa/inet.h>

typedef struct private_traffic_selector_t private_traffic_selector_t;

/**
 * Private data of an traffic_selector_t object
 */
struct private_traffic_selector_t {

	/**
	 * Public part
	 */
	traffic_selector_t public;
	
	/**
	 * Type of address
	 */
	ts_type_t type;
	
	/**
	 * IP protocol (UDP, TCP, ICMP, ...)
	 */
	u_int8_t protocol;
	
	/** 
	 * begin of address range, host order
	 */
	union {
		u_int32_t from_addr_ipv4;
	};
	
	/**
	 * end of address range, host order
	 */
	union {
		u_int32_t to_addr_ipv4;
	};
	
	/**
	 * begin of port range 
	 */
	u_int16_t from_port;
	
	/**
	 * end of port range 
	 */
	u_int16_t to_port;
};

/**
 * internal generic constructor
 */
static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts_type_t type, u_int16_t from_port, u_int16_t to_port);

/**
 * implements traffic_selector_t.get_subset
 */
static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_traffic_selector_t *other)
{
	if ((this->type == TS_IPV4_ADDR_RANGE) &&
		(other->type == TS_IPV4_ADDR_RANGE) &&
		(this->protocol == other->protocol))
	{
		u_int32_t from_addr, to_addr;
		u_int16_t from_port, to_port;
		private_traffic_selector_t *new_ts;
		
		/* calculate the maximum address range allowed for both */
		from_addr = max(this->from_addr_ipv4, other->from_addr_ipv4);
		to_addr = min(this->to_addr_ipv4, other->to_addr_ipv4);
		if (from_addr > to_addr)
		{
			/* no match */
			return NULL;	
		}
		
		/* calculate the maximum port range allowed for both */
		from_port = max(this->from_port, other->from_port);
		to_port = min(this->to_port, other->to_port);
		if (from_port > to_port)
		{
			/* no match */
			return NULL;	
		}
		
		/* got a match, return it */
		new_ts = traffic_selector_create(this->protocol, this->type, from_port, to_port); 
		new_ts->from_addr_ipv4 = from_addr;
		new_ts->to_addr_ipv4 = to_addr;
		new_ts->type = TS_IPV4_ADDR_RANGE;
		return &(new_ts->public);
	}
	return NULL;
}

/**
 * Implements traffic_selector_t.get_from_address.
 */
static chunk_t get_from_address(private_traffic_selector_t *this)
{
	chunk_t from_addr = CHUNK_INITIALIZER;
	
	switch (this->type)
	{
		case TS_IPV4_ADDR_RANGE:
		{
			u_int32_t network;
			from_addr.len = sizeof(network);
			from_addr.ptr = allocator_alloc(from_addr.len);
			/* chunk must contain network order, convert! */
			network = htonl(this->from_addr_ipv4);
			memcpy(from_addr.ptr, &network, from_addr.len);
			break;	
		}
		case TS_IPV6_ADDR_RANGE:
		{
			break;
		}
	}
	return from_addr;
}
	
/**
 * Implements traffic_selector_t.get_to_address.
 */
static chunk_t get_to_address(private_traffic_selector_t *this)
{
	chunk_t to_addr = CHUNK_INITIALIZER;
	
	switch (this->type)
	{
		case TS_IPV4_ADDR_RANGE:
		{
			u_int32_t network;
			to_addr.len = sizeof(network);
			to_addr.ptr = allocator_alloc(to_addr.len);
			/* chunk must contain network order, convert! */
			network = htonl(this->to_addr_ipv4);
			memcpy(to_addr.ptr, &network, to_addr.len);
			break;	
		}
		case TS_IPV6_ADDR_RANGE:
		{
			break;
		}
	}
	return to_addr;
}
	
/**
 * Implements traffic_selector_t.get_from_port.
 */
static u_int16_t get_from_port(private_traffic_selector_t *this)
{
	return this->from_port;
}
	
/**
 * Implements traffic_selector_t.get_to_port.
 */
static u_int16_t get_to_port(private_traffic_selector_t *this)
{
	return this->to_port;
}

/**
 * Implements traffic_selector_t.get_type.
 */
static ts_type_t get_type(private_traffic_selector_t *this)
{
	return this->type;
}

/**
 * Implements traffic_selector_t.get_protocol.
 */
static u_int8_t get_protocol(private_traffic_selector_t *this)
{
	return this->protocol;
}

/**
 * Implements traffic_selector_t.get_netmask.
 */
static u_int8_t get_netmask(private_traffic_selector_t *this)
{
	switch (this->type)
	{
		case TS_IPV4_ADDR_RANGE:
		{
			u_int32_t from, to, bit;
			from = htonl(this->from_addr_ipv4);
			to = htonl(this->to_addr_ipv4);
			for (bit = 0; bit < 32; bit++)
			{				
				if ((1<<bit & from) != (1<<bit & to))
				{
					return bit;
				}
			}
			return 32;
		}
		case TS_IPV6_ADDR_RANGE:
		default:
		{
			return 0;
		}
	}
}

/**
 * Implements traffic_selector_t.update_address_range.
 */
static void update_address_range(private_traffic_selector_t *this, host_t *host)
{
	if (host->get_family(host) == AF_INET &&
		this->type == TS_IPV4_ADDR_RANGE)
	{
		if (this->from_addr_ipv4 == 0)
		{
			chunk_t from = host->get_address_as_chunk(host);
			this->from_addr_ipv4 = ntohl(*((u_int32_t*)from.ptr));
			this->to_addr_ipv4 = this->from_addr_ipv4;
			allocator_free_chunk(&from);
		}
	}
}

/**
 * Implements traffic_selector_t.clone.
 */
static traffic_selector_t *clone(private_traffic_selector_t *this)
{
	private_traffic_selector_t *clone = traffic_selector_create(this->protocol, this->type, this->from_port, this->to_port);
	clone->type = this->type;
	switch (clone->type)
	{
		case TS_IPV4_ADDR_RANGE:
		{
			clone->from_addr_ipv4 = this->from_addr_ipv4;
			clone->to_addr_ipv4 = this->to_addr_ipv4;
			return &(clone->public);	
		}
		case TS_IPV6_ADDR_RANGE:
		default:
		{
			allocator_free(this);
			return NULL;	
		}
	}
}

/**
 * Implements traffic_selector_t.destroy.
 */
static void destroy(private_traffic_selector_t *this)
{	
	allocator_free(this);
}

/*
 * see header
 */
traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_type_t type, chunk_t from_addr, int16_t from_port, chunk_t to_addr, u_int16_t to_port)
{
	private_traffic_selector_t *this = traffic_selector_create(protocol, type, from_port, to_port);

	this->type = type;
	switch (type)
	{
		case TS_IPV4_ADDR_RANGE:
		{
			if (from_addr.len != 4 || to_addr.len != 4)
			{
				allocator_free(this);
				return NULL;	
			}
			/* chunk contains network order, convert! */
			this->from_addr_ipv4 = ntohl(*((u_int32_t*)from_addr.ptr));
			this->to_addr_ipv4 = ntohl(*((u_int32_t*)to_addr.ptr));
			break;	
		}
		case TS_IPV6_ADDR_RANGE:
		default:
		{
			allocator_free(this);
			return NULL;	
		}
	}
	return (&this->public);
}

/*
 * see header
 */
traffic_selector_t *traffic_selector_create_from_subnet(host_t *net, u_int8_t netbits)
{
	private_traffic_selector_t *this = traffic_selector_create(0, 0, 0, 65535);

	switch (net->get_family(net))
	{
		case AF_INET:
		{
			chunk_t from;
			
			this->type = TS_IPV4_ADDR_RANGE;
			from = net->get_address_as_chunk(net);
			this->from_addr_ipv4 = ntohl(*((u_int32_t*)from.ptr));
			if (this->from_addr_ipv4 == 0)
			{
				/* use /32 for 0.0.0.0 */
				this->to_addr_ipv4 = 0xFFFFFF;
			}
			else
			{
				this->to_addr_ipv4 = this->from_addr_ipv4 | ((1 << (32 - netbits)) - 1);
			}
			allocator_free_chunk(&from);
			break;	
		}
		case AF_INET6:
		default:
		{
			allocator_free(this);
			return NULL;	
		}
	}
	return (&this->public);
}

/*
 * see header
 */
traffic_selector_t *traffic_selector_create_from_string(u_int8_t protocol, ts_type_t type, char *from_addr, u_int16_t from_port, char *to_addr, u_int16_t to_port)
{
	private_traffic_selector_t *this = traffic_selector_create(protocol, type, from_port, to_port);

	/* public functions */
	this->public.get_subset = (traffic_selector_t*(*)(traffic_selector_t*,traffic_selector_t*))get_subset;
	this->public.destroy = (void(*)(traffic_selector_t*))destroy;

	this->type = type;
	switch (type)
	{
		case TS_IPV4_ADDR_RANGE:
		{
			if (inet_aton(from_addr, (struct in_addr*)&(this->from_addr_ipv4)) == 0)
			{
				allocator_free(this);
				return NULL;
			}
			if (inet_aton(to_addr, (struct in_addr*)&(this->to_addr_ipv4)) == 0)
			{
				allocator_free(this);
				return NULL;
			}
			/* convert to host order, inet_aton has network order */
			this->from_addr_ipv4 = ntohl(this->from_addr_ipv4);
			this->to_addr_ipv4 = ntohl(this->to_addr_ipv4);
			break;	
		}
		case TS_IPV6_ADDR_RANGE:
		{
			allocator_free(this);
			return NULL;	
		}
	}

	return (&this->public);
}

/*
 * see declaration
 */
static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts_type_t type, u_int16_t from_port, u_int16_t to_port)
{
	private_traffic_selector_t *this = allocator_alloc_thing(private_traffic_selector_t);

	/* public functions */
	this->public.get_subset = (traffic_selector_t*(*)(traffic_selector_t*,traffic_selector_t*))get_subset;
	this->public.get_from_address = (chunk_t(*)(traffic_selector_t*))get_from_address;
	this->public.get_to_address = (chunk_t(*)(traffic_selector_t*))get_to_address;
	this->public.get_from_port = (u_int16_t(*)(traffic_selector_t*))get_from_port;
	this->public.get_to_port = (u_int16_t(*)(traffic_selector_t*))get_to_port;	
	this->public.get_type = (ts_type_t(*)(traffic_selector_t*))get_type;	
	this->public.get_protocol = (u_int8_t(*)(traffic_selector_t*))get_protocol;
	this->public.get_netmask = (u_int8_t(*)(traffic_selector_t*))get_netmask;
	this->public.update_address_range = (void(*)(traffic_selector_t*,host_t*))update_address_range;
	this->public.clone = (traffic_selector_t*(*)(traffic_selector_t*))clone;
	this->public.destroy = (void(*)(traffic_selector_t*))destroy;
	
	this->from_port = from_port;
	this->to_port = to_port;
	this->protocol = protocol;
	this->type = type;
	
	return this;
}