summaryrefslogtreecommitdiff
path: root/src/libipsec/ipsec_policy_mgr.c
blob: 8570e07a8fb4af066085c3374878702521bdc6fd (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
/*
 * Copyright (C) 2012 Tobias Brunner
 * Copyright (C) 2012 Giuliano Grassi
 * Copyright (C) 2012 Ralf Sager
 * 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 "ipsec_policy_mgr.h"

#include <utils/debug.h>
#include <threading/rwlock.h>
#include <collections/linked_list.h>

/** Base priority for installed policies */
#define PRIO_BASE 384

typedef struct private_ipsec_policy_mgr_t private_ipsec_policy_mgr_t;

/**
 * Private additions to ipsec_policy_mgr_t.
 */
struct private_ipsec_policy_mgr_t {

	/**
	 * Public members of ipsec_policy_mgr_t.
	 */
	ipsec_policy_mgr_t public;

	/**
	 * Installed policies (ipsec_policy_entry_t*)
	 */
	linked_list_t *policies;

	/**
	 * Lock to safely access the list of policies
	 */
	rwlock_t *lock;

};

/**
 * Helper struct to store policies in a list sorted by the same pseudo-priority
 * used by the NETLINK kernel interface.
 */
typedef struct {

	/**
	 * Priority used to sort policies
	 */
	uint32_t priority;

	/**
	 * The policy
	 */
	ipsec_policy_t *policy;

} ipsec_policy_entry_t;

/**
 * Calculate the pseudo-priority to sort policies.  This is the same algorithm
 * used by the NETLINK kernel interface (i.e. high priority -> low value).
 */
static uint32_t calculate_priority(policy_priority_t policy_priority,
									traffic_selector_t *src,
									traffic_selector_t *dst)
{
	uint32_t priority = PRIO_BASE;
	uint16_t port;
	uint8_t mask, proto;
	host_t *net;

	switch (policy_priority)
	{
		case POLICY_PRIORITY_FALLBACK:
			priority <<= 1;
			/* fall-through */
		case POLICY_PRIORITY_ROUTED:
			priority <<= 1;
			/* fall-through */
		case POLICY_PRIORITY_DEFAULT:
			priority <<= 1;
			/* fall-through */
		case POLICY_PRIORITY_PASS:
			break;
	}
	/* calculate priority based on selector size, small size = high prio */
	src->to_subnet(src, &net, &mask);
	priority -= mask;
	proto = src->get_protocol(src);
	port = net->get_port(net);
	net->destroy(net);

	dst->to_subnet(dst, &net, &mask);
	priority -= mask;
	proto = max(proto, dst->get_protocol(dst));
	port = max(port, net->get_port(net));
	net->destroy(net);

	priority <<= 2; /* make some room for the two flags */
	priority += port ? 0 : 2;
	priority += proto ? 0 : 1;
	return priority;
}

/**
 * Create a policy entry
 */
static ipsec_policy_entry_t *policy_entry_create(ipsec_policy_t *policy)
{
	ipsec_policy_entry_t *this;

	INIT(this,
		.policy = policy,
		.priority = calculate_priority(policy->get_priority(policy),
									   policy->get_source_ts(policy),
									   policy->get_destination_ts(policy)),
	);
	return this;
}

/**
 * Destroy a policy entry
 */
static void policy_entry_destroy(ipsec_policy_entry_t *this)
{
	this->policy->destroy(this->policy);
	free(this);
}

METHOD(ipsec_policy_mgr_t, add_policy, status_t,
	private_ipsec_policy_mgr_t *this, host_t *src, host_t *dst,
	traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
	policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa, mark_t mark,
	policy_priority_t priority)
{
	enumerator_t *enumerator;
	ipsec_policy_entry_t *entry, *current;
	ipsec_policy_t *policy;

	if (type != POLICY_IPSEC || direction == POLICY_FWD)
	{	/* we ignore these policies as we currently have no use for them */
		return SUCCESS;
	}

	DBG2(DBG_ESP, "adding policy %R === %R %N", src_ts, dst_ts,
		 policy_dir_names, direction);

	policy = ipsec_policy_create(src, dst, src_ts, dst_ts, direction, type, sa,
								 mark, priority);
	entry = policy_entry_create(policy);

	this->lock->write_lock(this->lock);
	enumerator = this->policies->create_enumerator(this->policies);
	while (enumerator->enumerate(enumerator, (void**)&current))
	{
		if (current->priority >= entry->priority)
		{
			break;
		}
	}
	this->policies->insert_before(this->policies, enumerator, entry);
	enumerator->destroy(enumerator);
	this->lock->unlock(this->lock);
	return SUCCESS;
}

METHOD(ipsec_policy_mgr_t, del_policy, status_t,
	private_ipsec_policy_mgr_t *this, host_t *src, host_t *dst,
	traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
	policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa, mark_t mark,
	policy_priority_t policy_priority)
{
	enumerator_t *enumerator;
	ipsec_policy_entry_t *current, *found = NULL;
	uint32_t priority;

	if (type != POLICY_IPSEC || direction == POLICY_FWD)
	{	/* we ignore these policies as we currently have no use for them */
		return SUCCESS;
	}
	DBG2(DBG_ESP, "deleting policy %R === %R %N", src_ts, dst_ts,
		 policy_dir_names, direction);

	priority = calculate_priority(policy_priority, src_ts, dst_ts);

	this->lock->write_lock(this->lock);
	enumerator = this->policies->create_enumerator(this->policies);
	while (enumerator->enumerate(enumerator, (void**)&current))
	{
		if (current->priority == priority &&
			current->policy->match(current->policy, src_ts, dst_ts, direction,
								   sa->reqid, mark, policy_priority))
		{
			this->policies->remove_at(this->policies, enumerator);
			found = current;
			break;
		}
	}
	enumerator->destroy(enumerator);
	this->lock->unlock(this->lock);
	if (found)
	{
		policy_entry_destroy(found);
		return SUCCESS;
	}
	return FAILED;
}

METHOD(ipsec_policy_mgr_t, flush_policies, status_t,
	private_ipsec_policy_mgr_t *this)
{
	ipsec_policy_entry_t *entry;

	DBG2(DBG_ESP, "flushing policies");

	this->lock->write_lock(this->lock);
	while (this->policies->remove_last(this->policies,
									  (void**)&entry) == SUCCESS)
	{
		policy_entry_destroy(entry);
	}
	this->lock->unlock(this->lock);
	return SUCCESS;
}

METHOD(ipsec_policy_mgr_t, find_by_packet, ipsec_policy_t*,
	private_ipsec_policy_mgr_t *this, ip_packet_t *packet, bool inbound,
	uint32_t reqid)
{
	enumerator_t *enumerator;
	ipsec_policy_entry_t *current;
	ipsec_policy_t *found = NULL;

	this->lock->read_lock(this->lock);
	enumerator = this->policies->create_enumerator(this->policies);
	while (enumerator->enumerate(enumerator, (void**)&current))
	{
		ipsec_policy_t *policy = current->policy;

		if ((inbound == (policy->get_direction(policy) == POLICY_IN)) &&
			 policy->match_packet(policy, packet))
		{
			if (reqid == 0 || reqid == policy->get_reqid(policy))
			{
				found = policy->get_ref(policy);
				break;
			}
		}
	}
	enumerator->destroy(enumerator);
	this->lock->unlock(this->lock);
	return found;
}

METHOD(ipsec_policy_mgr_t, destroy, void,
	private_ipsec_policy_mgr_t *this)
{
	flush_policies(this);
	this->policies->destroy(this->policies);
	this->lock->destroy(this->lock);
	free(this);
}

/**
 * Described in header.
 */
ipsec_policy_mgr_t *ipsec_policy_mgr_create()
{
	private_ipsec_policy_mgr_t *this;

	INIT(this,
		.public = {
			.add_policy = _add_policy,
			.del_policy = _del_policy,
			.flush_policies = _flush_policies,
			.find_by_packet = _find_by_packet,
			.destroy = _destroy,
		},
		.policies = linked_list_create(),
		.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
	);

	return &this->public;
}