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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
/**
* @file child_sa.c
*
* @brief Implementation of child_sa_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 "child_sa.h"
#include <utils/allocator.h>
#include <daemon.h>
typedef struct policy_t policy_t;
/**
* Struct used to store information for a policy. This
* is needed since we must provide all this information
* for deleting a policy...
*/
struct policy_t {
/**
* Network on local side
*/
host_t *my_net;
/**
* Network on remote side
*/
host_t *other_net;
/**
* Number of bits for local network (subnet size)
*/
u_int8_t my_net_mask;
/**
* Number of bits for remote network (subnet size)
*/
u_int8_t other_net_mask;
/**
* Protocol for this policy, such as TCP/UDP/ICMP...
*/
int upper_proto;
};
typedef struct private_child_sa_t private_child_sa_t;
/**
* Private data of a child_sa_t object.
*/
struct private_child_sa_t {
/**
* Public interface of child_sa_t.
*/
child_sa_t public;
/**
* IP of this peer
*/
host_t *me;
/**
* IP of other peer
*/
host_t *other;
/**
* Local security parameter index for AH protocol, 0 if not used
*/
u_int32_t my_ah_spi;
/**
* Local security parameter index for ESP protocol, 0 if not used
*/
u_int32_t my_esp_spi;
/**
* Remote security parameter index for AH protocol, 0 if not used
*/
u_int32_t other_ah_spi;
/**
* Remote security parameter index for ESP protocol, 0 if not used
*/
u_int32_t other_esp_spi;
/**
* List containing policy_id_t objects
*/
linked_list_t *policies;
/**
* reqid used for this child_sa
*/
u_int32_t reqid;
/**
* CHILD_SAs own logger
*/
logger_t *logger;
};
/**
* Implements child_sa_t.alloc
*/
static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
{
protocol_id_t protocols[2];
iterator_t *iterator;
proposal_t *proposal;
status_t status;
u_int i;
/* iterator through proposals */
iterator = proposals->create_iterator(proposals, TRUE);
while(iterator->has_next(iterator))
{
iterator->current(iterator, (void**)&proposal);
proposal->get_protocols(proposal, protocols);
/* check all protocols */
for (i = 0; i<2; i++)
{
switch (protocols[i])
{
case AH:
/* do we already have an spi for AH?*/
if (this->my_ah_spi == 0)
{
/* nope, get one */
status = charon->kernel_interface->get_spi(
charon->kernel_interface,
this->me, this->other,
AH, FALSE,
&(this->my_ah_spi));
}
/* update proposal */
proposal->set_spi(proposal, AH, (u_int64_t)this->my_ah_spi);
break;
case ESP:
/* do we already have an spi for ESP?*/
if (this->my_esp_spi == 0)
{
/* nope, get one */
status = charon->kernel_interface->get_spi(
charon->kernel_interface,
this->me, this->other,
ESP, FALSE,
&(this->my_esp_spi));
}
/* update proposal */
proposal->set_spi(proposal, ESP, (u_int64_t)this->my_esp_spi);
break;
default:
break;
}
if (status != SUCCESS)
{
iterator->destroy(iterator);
return FAILED;
}
}
}
iterator->destroy(iterator);
return SUCCESS;
}
static status_t install(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus, bool mine)
{
protocol_id_t protocols[2];
u_int32_t spi;
encryption_algorithm_t enc_algo;
integrity_algorithm_t int_algo;
chunk_t enc_key, int_key;
algorithm_t *algo;
crypter_t *crypter;
signer_t *signer;
size_t key_size;
host_t *src;
host_t *dst;
status_t status;
u_int i;
/* we must assign the roles to correctly set up the SAs */
if (mine)
{
src = this->me;
dst = this->other;
}
else
{
dst = this->me;
src = this->other;
}
proposal->get_protocols(proposal, protocols);
/* derive keys in order as protocols appear */
for (i = 0; i<2; i++)
{
if (protocols[i] != UNDEFINED_PROTOCOL_ID)
{
/* now we have to decide which spi to use. Use self allocated, if "mine",
* or the one in the proposal, if not "mine" (others). */
if (mine)
{
if (protocols[i] == AH)
{
spi = this->my_ah_spi;
}
else
{
spi = this->my_esp_spi;
}
}
else /* use proposals spi */
{
spi = proposal->get_spi(proposal, protocols[i]);
if (protocols[i] == AH)
{
this->other_ah_spi = spi;
}
else
{
this->other_esp_spi = spi;
}
}
/* derive encryption key first */
if (proposal->get_algorithm(proposal, protocols[i], ENCRYPTION_ALGORITHM, &algo))
{
enc_algo = algo->algorithm;
this->logger->log(this->logger, CONTROL|LEVEL1, "%s for %s: using %s %s, ",
mapping_find(protocol_id_m, protocols[i]),
mine ? "me" : "other",
mapping_find(transform_type_m, ENCRYPTION_ALGORITHM),
mapping_find(encryption_algorithm_m, enc_algo));
/* we must create a (unused) crypter, since its the only way to get the size
* of the key. This is not so nice, since charon must support all algorithms
* the kernel supports...
* TODO: build something of a encryption algorithm lookup function
*/
crypter = crypter_create(enc_algo, algo->key_size);
key_size = crypter->get_key_size(crypter);
crypter->destroy(crypter);
prf_plus->allocate_bytes(prf_plus, key_size, &enc_key);
this->logger->log_chunk(this->logger, PRIVATE, "key:", &enc_key);
}
else
{
enc_algo = ENCR_UNDEFINED;
}
/* derive integrity key */
if (proposal->get_algorithm(proposal, protocols[i], INTEGRITY_ALGORITHM, &algo))
{
int_algo = algo->algorithm;
this->logger->log(this->logger, CONTROL|LEVEL1, "%s for %s: using %s %s,",
mapping_find(protocol_id_m, protocols[i]),
mine ? "me" : "other",
mapping_find(transform_type_m, INTEGRITY_ALGORITHM),
mapping_find(integrity_algorithm_m, algo->algorithm));
signer = signer_create(int_algo);
key_size = signer->get_key_size(signer);
signer->destroy(signer);
prf_plus->allocate_bytes(prf_plus, key_size, &int_key);
this->logger->log_chunk(this->logger, PRIVATE, "key:", &int_key);
}
else
{
int_algo = AUTH_UNDEFINED;
}
/* send keys down to kernel */
this->logger->log(this->logger, CONTROL|LEVEL1,
"installing 0x%.8x for %s, src %s dst %s",
ntohl(spi), mapping_find(protocol_id_m, protocols[i]),
src->get_address(src), dst->get_address(dst));
status = charon->kernel_interface->add_sa(charon->kernel_interface,
src, dst,
spi, protocols[i],
this->reqid,
enc_algo, enc_key,
int_algo, int_key, mine);
/* clean up for next round */
if (enc_algo != ENCR_UNDEFINED)
{
allocator_free_chunk(&enc_key);
}
if (int_algo != AUTH_UNDEFINED)
{
allocator_free_chunk(&int_key);
}
if (status != SUCCESS)
{
return FAILED;
}
}
}
return SUCCESS;
}
static status_t add(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
{
linked_list_t *list;
/* install others (initiators) SAs*/
if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
{
return FAILED;
}
/* get SPIs for our SAs */
list = linked_list_create();
list->insert_last(list, proposal);
if (alloc(this, list) != SUCCESS)
{
list->destroy(list);
return FAILED;
}
list->destroy(list);
/* install our (responders) SAs */
if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
{
return FAILED;
}
return SUCCESS;
}
static status_t update(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
{
/* install our (initator) SAs */
if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
{
return FAILED;
}
/* install his (responder) SAs */
if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
{
return FAILED;
}
return SUCCESS;
}
static u_int8_t get_mask(chunk_t start, chunk_t end)
{
int byte, bit, mask = 0;
if (start.len != end.len)
{
return 0;
}
for (byte = 0; byte < start.len; byte++)
{
for (bit = 7; bit >= 0; bit--)
{
if ((*(start.ptr + byte) | (1<<bit)) ==
(*(end.ptr + byte) | (1<<bit)))
{
mask++;
}
else
{
return mask;
}
}
}
return start.len * 8;
}
static status_t add_policies(private_child_sa_t *this, linked_list_t *my_ts_list, linked_list_t *other_ts_list)
{
iterator_t *my_iter, *other_iter;
traffic_selector_t *my_ts, *other_ts;
/* iterate over both lists */
my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
while (my_iter->has_next(my_iter))
{
my_iter->current(my_iter, (void**)&my_ts);
other_iter->reset(other_iter);
while (other_iter->has_next(other_iter))
{
/* set up policies for every entry in my_ts_list to every entry in other_ts_list */
int family;
chunk_t from_addr, to_addr;
u_int16_t from_port, to_port;
policy_t *policy;
status_t status;
other_iter->current(other_iter, (void**)&other_ts);
/* only set up policies if protocol matches */
if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts))
{
continue;
}
policy = allocator_alloc_thing(policy_t);
policy->upper_proto = my_ts->get_protocol(my_ts);
/* calculate net and ports for local side */
family = my_ts->get_type(my_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
from_addr = my_ts->get_from_address(my_ts);
to_addr = my_ts->get_to_address(my_ts);
from_port = my_ts->get_from_port(my_ts);
to_port = my_ts->get_to_port(my_ts);
from_port = (from_port != to_port) ? 0 : from_port;
policy->my_net = host_create_from_chunk(family, from_addr, from_port);
policy->my_net_mask = get_mask(from_addr, to_addr);
allocator_free_chunk(&from_addr);
allocator_free_chunk(&to_addr);
/* calculate net and ports for remote side */
family = other_ts->get_type(other_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
from_addr = other_ts->get_from_address(other_ts);
to_addr = other_ts->get_to_address(other_ts);
from_port = other_ts->get_from_port(other_ts);
to_port = other_ts->get_to_port(other_ts);
from_port = (from_port != to_port) ? 0 : from_port;
policy->other_net = host_create_from_chunk(family, from_addr, from_port);
policy->other_net_mask = get_mask(from_addr, to_addr);
allocator_free_chunk(&from_addr);
allocator_free_chunk(&to_addr);
/* install 3 policies: out, in and forward */
status = charon->kernel_interface->add_policy(charon->kernel_interface,
this->me, this->other,
policy->my_net, policy->other_net,
policy->my_net_mask, policy->other_net_mask,
XFRM_POLICY_OUT, policy->upper_proto,
this->my_ah_spi, this->my_esp_spi,
this->reqid);
status |= charon->kernel_interface->add_policy(charon->kernel_interface,
this->other, this->me,
policy->other_net, policy->my_net,
policy->other_net_mask, policy->my_net_mask,
XFRM_POLICY_IN, policy->upper_proto,
this->my_ah_spi, this->my_esp_spi,
this->reqid);
status |= charon->kernel_interface->add_policy(charon->kernel_interface,
this->other, this->me,
policy->other_net, policy->my_net,
policy->other_net_mask, policy->my_net_mask,
XFRM_POLICY_FWD, policy->upper_proto,
this->my_ah_spi, this->my_esp_spi,
this->reqid);
if (status != SUCCESS)
{
my_iter->destroy(my_iter);
other_iter->destroy(other_iter);
allocator_free(policy);
return status;
}
/* add it to the policy list, since we want to know which policies we own */
this->policies->insert_last(this->policies, policy);
}
}
my_iter->destroy(my_iter);
other_iter->destroy(other_iter);
return SUCCESS;
}
/**
* Implementation of child_sa_t.destroy.
*/
static void destroy(private_child_sa_t *this)
{
/* delete all policys in the kernel */
policy_t *policy;
while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
{
charon->kernel_interface->del_policy(charon->kernel_interface,
this->me, this->other,
policy->my_net, policy->other_net,
policy->my_net_mask, policy->other_net_mask,
XFRM_POLICY_OUT, policy->upper_proto);
charon->kernel_interface->del_policy(charon->kernel_interface,
this->other, this->me,
policy->other_net, policy->my_net,
policy->other_net_mask, policy->my_net_mask,
XFRM_POLICY_IN, policy->upper_proto);
charon->kernel_interface->del_policy(charon->kernel_interface,
this->other, this->me,
policy->other_net, policy->my_net,
policy->other_net_mask, policy->my_net_mask,
XFRM_POLICY_FWD, policy->upper_proto);
policy->my_net->destroy(policy->my_net);
policy->other_net->destroy(policy->other_net);
allocator_free(policy);
}
this->policies->destroy(this->policies);
/* delete SAs in the kernel, if they are set up */
if (this->my_ah_spi)
{
charon->kernel_interface->del_sa(charon->kernel_interface,
this->other, this->my_ah_spi, AH);
charon->kernel_interface->del_sa(charon->kernel_interface,
this->me, this->other_ah_spi, AH);
}
if (this->my_esp_spi)
{
charon->kernel_interface->del_sa(charon->kernel_interface,
this->other, this->my_esp_spi, ESP);
charon->kernel_interface->del_sa(charon->kernel_interface,
this->me, this->other_esp_spi, ESP);
}
charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
allocator_free(this);
}
/*
* Described in header.
*/
child_sa_t * child_sa_create(host_t *me, host_t* other)
{
static u_int32_t reqid = 0xc0000000;
private_child_sa_t *this = allocator_alloc_thing(private_child_sa_t);
/* public functions */
this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
this->public.add = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))add;
this->public.update = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))update;
this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*))add_policies;
this->public.destroy = (void(*)(child_sa_t*))destroy;
/* private data */
this->logger = charon->logger_manager->create_logger(charon->logger_manager, CHILD_SA, NULL);
this->me = me;
this->other = other;
this->my_ah_spi = 0;
this->my_esp_spi = 0;
this->other_ah_spi = 0;
this->other_esp_spi = 0;
this->reqid = reqid++;
this->policies = linked_list_create();
return (&this->public);
}
|