aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcharon/plugins/kernel_wfp/kernel_wfp_ipsec.c
blob: caf955d559119e68d02718229620d3f3bfe71f38 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 * Copyright (C) 2013 Martin Willi
 * Copyright (C) 2013 revosec 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.
 */

/* Windows 7, for some fwpmu.h functionality */
#define _WIN32_WINNT 0x0601

#include "kernel_wfp_compat.h"
#include "kernel_wfp_ipsec.h"

#include <daemon.h>
#include <threading/mutex.h>
#include <collections/array.h>
#include <collections/hashtable.h>


typedef struct private_kernel_wfp_ipsec_t private_kernel_wfp_ipsec_t;

struct private_kernel_wfp_ipsec_t {

	/**
	 * Public interface
	 */
	kernel_wfp_ipsec_t public;

	/**
	 * Next SPI to allocate
	 */
	refcount_t nextspi;

	/**
	 * SAD/SPD entries, as reqid => entry_t
	 */
	hashtable_t *entries;

	/**
	 * SAD entry lookup, as sa_entry_t => entry_t
	 */
	hashtable_t *sas;

	/**
	 * Mutex for accessing entries
	 */
	mutex_t *mutex;

	/**
	 * WFP session handle
	 */
	HANDLE handle;
};

/**
 * Security association entry
 */
typedef struct {
	/** SPI for this SA */
	u_int32_t spi;
	/** destination host address for this SPI */
	host_t *dst;
	/** inbound or outbound SA? */
	bool inbound;
	struct {
		/** algorithm */
		u_int16_t alg;
		/** key */
		chunk_t key;
	} integ, encr;
} sa_entry_t;

/**
 * Destroy an SA entry
 */
static void sa_entry_destroy(sa_entry_t *sa)
{
	chunk_clear(&sa->integ.key);
	chunk_clear(&sa->encr.key);
	free(sa);
}

/**
 * Hash function for sas lookup table
 */
static u_int hash_sa(sa_entry_t *key)
{
	return chunk_hash_inc(chunk_from_thing(key->spi),
						  chunk_hash(key->dst->get_address(key->dst)));
}

/**
 * equals function for sas lookup table
 */
static bool equals_sa(sa_entry_t *a, sa_entry_t *b)
{
	return a->spi == b->spi && a->dst->ip_equals(a->dst, b->dst);
}

/**
 * Security policy entry
 */
typedef struct {
	/** policy source addresses */
	traffic_selector_t *src;
	/** policy destinaiton addresses */
	traffic_selector_t *dst;
	/** direction of policy, in|out */
	policy_dir_t direction;
} sp_entry_t;

/**
 * Destroy an SP entry
 */
static void sp_entry_destroy(sp_entry_t *sp)
{
	sp->src->destroy(sp->src);
	sp->dst->destroy(sp->dst);
	free(sp);
}

/**
 * Collection of SA/SP database entries for a reqid
 */
typedef struct {
	/** reqid of entry */
	u_int32_t reqid;
	/** outer address on local host */
	host_t *local;
	/** outer address on remote host */
	host_t *remote;
	/** associated security associations, as sa_entry_t* */
	array_t *sas;
	/** associated policies, as sp_entry_t* */
	array_t *sps;
	/** IPsec protocol, ESP|AH */
	u_int8_t protocol;
	/** IPsec mode, tunnel|transport */
	ipsec_mode_t mode;
	/** UDP encapsulation */
	bool encap;
} entry_t;

/**
 * Create a SA/SP entry set
 */
static entry_t *entry_create(u_int32_t reqid, host_t *local, host_t *remote,
							 u_int8_t protocol, ipsec_mode_t mode, bool encap)
{
	entry_t *entry;

	INIT(entry,
		.reqid = reqid,
		.sas = array_create(0, 0),
		.sps = array_create(0, 0),
		.local = local->clone(local),
		.remote = remote->clone(remote),
		.protocol = protocol,
		.mode = mode,
		.encap = encap,
	);
	return entry;
}

/**
 * Destroy a SA/SP entry set
 */
static void entry_destroy(entry_t *entry)
{
	array_destroy(entry->sas);
	array_destroy(entry->sps);
	entry->local->destroy(entry->local);
	entry->remote->destroy(entry->remote);
	free(entry);
}

/**
 * Get an entry, create if not exists. May fail if non-matching entry found
 */
static entry_t *get_or_create_entry(private_kernel_wfp_ipsec_t *this,
							u_int32_t reqid, host_t *local, host_t *remote,
							u_int8_t protocol, ipsec_mode_t mode, bool encap)
{
	entry_t *entry;

	entry = this->entries->get(this->entries, (void*)(uintptr_t)reqid);
	if (!entry)
	{
		entry = entry_create(reqid, local, remote, protocol, mode, encap);
		this->entries->put(this->entries, (void*)(uintptr_t)reqid, entry);
		return entry;
	}
	if (entry->protocol == protocol &&
		entry->mode == mode &&
		entry->local->ip_equals(entry->local, local) &&
		entry->remote->ip_equals(entry->remote, remote))
	{
		return entry;
	}
	return NULL;
}

METHOD(kernel_ipsec_t, get_features, kernel_feature_t,
	private_kernel_wfp_ipsec_t *this)
{
	return KERNEL_ESP_V3_TFC;
}

METHOD(kernel_ipsec_t, get_spi, status_t,
	private_kernel_wfp_ipsec_t *this, host_t *src, host_t *dst,
	u_int8_t protocol, u_int32_t reqid, u_int32_t *spi)
{
	*spi = ref_get(&this->nextspi);
	return SUCCESS;
}

METHOD(kernel_ipsec_t, get_cpi, status_t,
	private_kernel_wfp_ipsec_t *this, host_t *src, host_t *dst,
	u_int32_t reqid, u_int16_t *cpi)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, add_sa, status_t,
	private_kernel_wfp_ipsec_t *this, host_t *src, host_t *dst,
	u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
	u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
	u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode, u_int16_t ipcomp,
	u_int16_t cpi, bool initiator, bool encap, bool esn, bool inbound,
	traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
{
	status_t status = SUCCESS;
	host_t *local, *remote;
	entry_t *entry;
	sa_entry_t *sa;

	if (inbound)
	{
		local = dst;
		remote = src;
	}
	else
	{
		local = src;
		remote = dst;
	}

	this->mutex->lock(this->mutex);
	entry = get_or_create_entry(this, reqid, local, remote,
								protocol, mode, encap);
	if (entry)
	{
		INIT(sa,
			.spi = spi,
			.inbound = inbound,
			.dst = inbound ? entry->local : entry->remote,
			.encr = {
				.alg = enc_alg,
				.key = chunk_clone(enc_key),
			},
			.integ = {
				.alg = int_alg,
				.key = chunk_clone(int_key),
			},
		);
		array_insert(entry->sas, -1, sa);
		this->sas->put(this->sas, sa, entry);
	}
	else
	{
		DBG1(DBG_KNL, "adding SA failed, a different SA with reqid %u exists",
			 reqid);
		status = FAILED;
	}
	this->mutex->unlock(this->mutex);

	return status;
}

METHOD(kernel_ipsec_t, update_sa, status_t,
	private_kernel_wfp_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
	u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
	bool encap, bool new_encap, mark_t mark)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, query_sa, status_t,
	private_kernel_wfp_ipsec_t *this, host_t *src, host_t *dst,
	u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes,
	u_int64_t *packets, time_t *time)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, del_sa, status_t,
	private_kernel_wfp_ipsec_t *this, host_t *src, host_t *dst,
	u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
{
	status_t status = NOT_FOUND;
	entry_t *entry;
	host_t *local, *remote;
	enumerator_t *enumerator;
	sa_entry_t *sa, key = {
		.dst = dst,
		.spi = spi,
	};

	this->mutex->lock(this->mutex);

	entry = this->sas->get(this->sas, &key);
	if (entry)
	{
		enumerator = array_create_enumerator(entry->sas);
		while (enumerator->enumerate(enumerator, &sa))
		{
			if (sa->inbound)
			{
				local = dst;
				remote = src;
			}
			else
			{
				local = src;
				remote = dst;
			}
			if (sa->spi == spi && entry->protocol == protocol &&
				local->ip_equals(local, entry->local) &&
				remote->ip_equals(remote, entry->remote))
			{
				array_remove_at(entry->sas, enumerator);
				this->sas->remove(this->sas, sa);
				/* TODO: uninstall SA from kernel */
				sa_entry_destroy(sa);
				status = SUCCESS;
				break;
			}
		}
		enumerator->destroy(enumerator);

		if (!array_count(entry->sas) && !array_count(entry->sps))
		{
			entry = this->entries->remove(this->entries,
										  (void*)(uintptr_t)entry->reqid);
			if (entry)
			{
				entry_destroy(entry);
			}
		}
	}

	this->mutex->unlock(this->mutex);

	return status;
}

METHOD(kernel_ipsec_t, flush_sas, status_t,
	private_kernel_wfp_ipsec_t *this)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, add_policy, status_t,
	private_kernel_wfp_ipsec_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)
{
	status_t status = SUCCESS;
	host_t *local, *remote;
	entry_t *entry;
	sp_entry_t *sp;

	if (direction == POLICY_FWD || priority != POLICY_PRIORITY_DEFAULT)
	{
		return SUCCESS;
	}

	if (direction == POLICY_IN)
	{
		local = dst;
		remote = src;
	}
	else
	{
		local = src;
		remote = dst;
	}

	this->mutex->lock(this->mutex);
	entry = get_or_create_entry(this, sa->reqid, local, remote,
								sa->esp.use ? IPPROTO_ESP : IPPROTO_AH,
								sa->mode, FALSE);
	if (entry)
	{
		INIT(sp,
			.src = src_ts->clone(src_ts),
			.dst = dst_ts->clone(dst_ts),
			.direction = direction,
		);
		array_insert(entry->sps, -1, sp);
	}
	else
	{
		DBG1(DBG_KNL, "adding SP failed, a different SP with reqid %u exists",
			 sa->reqid);
		status = FAILED;
	}
	this->mutex->unlock(this->mutex);

	return status;
}

METHOD(kernel_ipsec_t, query_policy, status_t,
	private_kernel_wfp_ipsec_t *this, traffic_selector_t *src_ts,
	traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
	time_t *use_time)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, del_policy, status_t,
	private_kernel_wfp_ipsec_t *this, traffic_selector_t *src_ts,
	traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
	mark_t mark, policy_priority_t priority)
{
	status_t status = NOT_FOUND;
	entry_t *entry;
	sp_entry_t *sp;
	enumerator_t *enumerator;

	this->mutex->lock(this->mutex);

	entry = this->entries->get(this->entries, (void*)(uintptr_t)reqid);
	if (entry)
	{
		enumerator = array_create_enumerator(entry->sps);
		while (enumerator->enumerate(enumerator, &sp))
		{
			if (sp->direction == direction &&
				src_ts->equals(src_ts, sp->src) &&
				dst_ts->equals(dst_ts, sp->dst))
			{
				array_remove_at(entry->sps, enumerator);
				/* TODO: uninstall SP from kernel */
				sp_entry_destroy(sp);
				status = SUCCESS;
				break;
			}
		}
		enumerator->destroy(enumerator);

		if (!array_count(entry->sas) && !array_count(entry->sps))
		{
			entry = this->entries->remove(this->entries,
										  (void*)(uintptr_t)reqid);
			if (entry)
			{
				entry_destroy(entry);
			}
		}
	}

	this->mutex->unlock(this->mutex);

	return status;
}

METHOD(kernel_ipsec_t, flush_policies, status_t,
	private_kernel_wfp_ipsec_t *this)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, bypass_socket, bool,
	private_kernel_wfp_ipsec_t *this, int fd, int family)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, enable_udp_decap, bool,
	private_kernel_wfp_ipsec_t *this, int fd, int family, u_int16_t port)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_ipsec_t, destroy, void,
	private_kernel_wfp_ipsec_t *this)
{
	if (this->handle)
	{
		FwpmEngineClose0(this->handle);
	}
	this->entries->destroy(this->entries);
	this->sas->destroy(this->sas);
	this->mutex->destroy(this->mutex);
	free(this);
}

/*
 * Described in header.
 */
kernel_wfp_ipsec_t *kernel_wfp_ipsec_create()
{
	private_kernel_wfp_ipsec_t *this;
	DWORD res;
	FWPM_SESSION0 session = {
		.displayData = {
			.name = L"charon",
			.description = L"strongSwan IKE kernel-wfp backend",
		},
	};

	INIT(this,
		.public = {
			.interface = {
				.get_features = _get_features,
				.get_spi = _get_spi,
				.get_cpi = _get_cpi,
				.add_sa  = _add_sa,
				.update_sa = _update_sa,
				.query_sa = _query_sa,
				.del_sa = _del_sa,
				.flush_sas = _flush_sas,
				.add_policy = _add_policy,
				.query_policy = _query_policy,
				.del_policy = _del_policy,
				.flush_policies = _flush_policies,
				.bypass_socket = _bypass_socket,
				.enable_udp_decap = _enable_udp_decap,
				.destroy = _destroy,
			},
		},
		.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
		.entries = hashtable_create(hashtable_hash_ptr,
									hashtable_equals_ptr, 4),
		.sas = hashtable_create((void*)hash_sa, (void*)equals_sa, 4),
	);

	res = FwpmEngineOpen0(NULL, RPC_C_AUTHN_WINNT, NULL, &session,
						  &this->handle);
	if (res != ERROR_SUCCESS)
	{
		DBG1(DBG_KNL, "opening WFP engine failed: 0x%08x", res);
		destroy(this);
		return NULL;
	}

	return &this->public;
}