libhydra: Move kernel interface to libcharon
[strongswan.git] / src / libcharon / plugins / kernel_libipsec / kernel_libipsec_ipsec.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "kernel_libipsec_ipsec.h"
16 #include "kernel_libipsec_router.h"
17
18 #include <library.h>
19 #include <ipsec.h>
20 #include <daemon.h>
21 #include <networking/tun_device.h>
22 #include <threading/mutex.h>
23 #include <utils/debug.h>
24
25 typedef struct private_kernel_libipsec_ipsec_t private_kernel_libipsec_ipsec_t;
26
27 struct private_kernel_libipsec_ipsec_t {
28
29 /**
30 * Public libipsec_ipsec interface
31 */
32 kernel_libipsec_ipsec_t public;
33
34 /**
35 * Listener for lifetime expire events
36 */
37 ipsec_event_listener_t ipsec_listener;
38
39 /**
40 * Mutex to lock access to various lists
41 */
42 mutex_t *mutex;
43
44 /**
45 * List of installed policies (policy_entry_t)
46 */
47 linked_list_t *policies;
48
49 /**
50 * List of exclude routes (exclude_route_t)
51 */
52 linked_list_t *excludes;
53
54 /**
55 * Whether the remote TS may equal the IKE peer
56 */
57 bool allow_peer_ts;
58 };
59
60 typedef struct exclude_route_t exclude_route_t;
61
62 /**
63 * Exclude route definition
64 */
65 struct exclude_route_t {
66 /** Destination address to exclude */
67 host_t *dst;
68 /** Source address for route */
69 host_t *src;
70 /** Nexthop exclude has been installed */
71 host_t *gtw;
72 /** References to this route */
73 int refs;
74 };
75
76 /**
77 * Clean up an exclude route entry
78 */
79 static void exclude_route_destroy(exclude_route_t *this)
80 {
81 this->dst->destroy(this->dst);
82 this->src->destroy(this->src);
83 this->gtw->destroy(this->gtw);
84 free(this);
85 }
86
87 /**
88 * Find an exclude route entry by destination address
89 */
90 static bool exclude_route_match(exclude_route_t *current,
91 host_t *dst)
92 {
93 return dst->ip_equals(dst, current->dst);
94 }
95
96 typedef struct route_entry_t route_entry_t;
97
98 /**
99 * Installed routing entry
100 */
101 struct route_entry_t {
102 /** Name of the interface the route is bound to */
103 char *if_name;
104 /** Source IP of the route */
105 host_t *src_ip;
106 /** Gateway of the route */
107 host_t *gateway;
108 /** Destination net */
109 chunk_t dst_net;
110 /** Destination net prefixlen */
111 u_int8_t prefixlen;
112 /** Reference to exclude route, if any */
113 exclude_route_t *exclude;
114 };
115
116 /**
117 * Destroy a route_entry_t object
118 */
119 static void route_entry_destroy(route_entry_t *this)
120 {
121 free(this->if_name);
122 DESTROY_IF(this->src_ip);
123 DESTROY_IF(this->gateway);
124 chunk_free(&this->dst_net);
125 free(this);
126 }
127
128 /**
129 * Compare two route_entry_t objects
130 */
131 static bool route_entry_equals(route_entry_t *a, route_entry_t *b)
132 {
133 if ((!a->src_ip && !b->src_ip) || (a->src_ip && b->src_ip &&
134 a->src_ip->ip_equals(a->src_ip, b->src_ip)))
135 {
136 if ((!a->gateway && !b->gateway) || (a->gateway && b->gateway &&
137 a->gateway->ip_equals(a->gateway, b->gateway)))
138 {
139 return a->if_name && b->if_name && streq(a->if_name, b->if_name) &&
140 chunk_equals(a->dst_net, b->dst_net) &&
141 a->prefixlen == b->prefixlen;
142 }
143 }
144 return FALSE;
145 }
146
147 typedef struct policy_entry_t policy_entry_t;
148
149 /**
150 * Installed policy
151 */
152 struct policy_entry_t {
153 /** Direction of this policy: in, out, forward */
154 u_int8_t direction;
155 /** Parameters of installed policy */
156 struct {
157 /** Subnet and port */
158 host_t *net;
159 /** Subnet mask */
160 u_int8_t mask;
161 /** Protocol */
162 u_int8_t proto;
163 } src, dst;
164 /** Associated route installed for this policy */
165 route_entry_t *route;
166 /** References to this policy */
167 int refs;
168 };
169
170 /**
171 * Create a policy_entry_t object
172 */
173 static policy_entry_t *create_policy_entry(traffic_selector_t *src_ts,
174 traffic_selector_t *dst_ts,
175 policy_dir_t dir)
176 {
177 policy_entry_t *this;
178 INIT(this,
179 .direction = dir,
180 );
181
182 src_ts->to_subnet(src_ts, &this->src.net, &this->src.mask);
183 dst_ts->to_subnet(dst_ts, &this->dst.net, &this->dst.mask);
184
185 /* src or dest proto may be "any" (0), use more restrictive one */
186 this->src.proto = max(src_ts->get_protocol(src_ts),
187 dst_ts->get_protocol(dst_ts));
188 this->src.proto = this->src.proto ? this->src.proto : 0;
189 this->dst.proto = this->src.proto;
190 return this;
191 }
192
193 /**
194 * Destroy a policy_entry_t object
195 */
196 static void policy_entry_destroy(policy_entry_t *this)
197 {
198 if (this->route)
199 {
200 route_entry_destroy(this->route);
201 }
202 DESTROY_IF(this->src.net);
203 DESTROY_IF(this->dst.net);
204 free(this);
205 }
206
207 /**
208 * Compare two policy_entry_t objects
209 */
210 static inline bool policy_entry_equals(policy_entry_t *a,
211 policy_entry_t *b)
212 {
213 return a->direction == b->direction &&
214 a->src.proto == b->src.proto &&
215 a->dst.proto == b->dst.proto &&
216 a->src.mask == b->src.mask &&
217 a->dst.mask == b->dst.mask &&
218 a->src.net->equals(a->src.net, b->src.net) &&
219 a->dst.net->equals(a->dst.net, b->dst.net);
220 }
221
222 /**
223 * Expiration callback
224 */
225 static void expire(u_int8_t protocol, u_int32_t spi, host_t *dst, bool hard)
226 {
227 charon->kernel->expire(charon->kernel, protocol, spi, dst, hard);
228 }
229
230 METHOD(kernel_ipsec_t, get_features, kernel_feature_t,
231 private_kernel_libipsec_ipsec_t *this)
232 {
233 return KERNEL_REQUIRE_UDP_ENCAPSULATION | KERNEL_ESP_V3_TFC;
234 }
235
236 METHOD(kernel_ipsec_t, get_spi, status_t,
237 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
238 u_int8_t protocol, u_int32_t *spi)
239 {
240 return ipsec->sas->get_spi(ipsec->sas, src, dst, protocol, spi);
241 }
242
243 METHOD(kernel_ipsec_t, get_cpi, status_t,
244 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
245 u_int16_t *cpi)
246 {
247 return NOT_SUPPORTED;
248 }
249
250 METHOD(kernel_ipsec_t, add_sa, status_t,
251 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
252 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
253 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
254 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
255 u_int16_t ipcomp, u_int16_t cpi, u_int32_t replay_window,
256 bool initiator, bool encap, bool esn, bool inbound, bool update,
257 linked_list_t *src_ts, linked_list_t *dst_ts)
258 {
259 return ipsec->sas->add_sa(ipsec->sas, src, dst, spi, protocol, reqid, mark,
260 tfc, lifetime, enc_alg, enc_key, int_alg, int_key,
261 mode, ipcomp, cpi, initiator, encap, esn,
262 inbound, update);
263 }
264
265 METHOD(kernel_ipsec_t, update_sa, status_t,
266 private_kernel_libipsec_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
267 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
268 bool encap, bool new_encap, mark_t mark)
269 {
270 return NOT_SUPPORTED;
271 }
272
273 METHOD(kernel_ipsec_t, query_sa, status_t,
274 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
275 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes,
276 u_int64_t *packets, time_t *time)
277 {
278 return ipsec->sas->query_sa(ipsec->sas, src, dst, spi, protocol, mark,
279 bytes, packets, time);
280 }
281
282 METHOD(kernel_ipsec_t, del_sa, status_t,
283 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
284 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
285 {
286 return ipsec->sas->del_sa(ipsec->sas, src, dst, spi, protocol, cpi, mark);
287 }
288
289 METHOD(kernel_ipsec_t, flush_sas, status_t,
290 private_kernel_libipsec_ipsec_t *this)
291 {
292 return ipsec->sas->flush_sas(ipsec->sas);
293 }
294
295 /**
296 * Add an explicit exclude route to a routing entry
297 */
298 static void add_exclude_route(private_kernel_libipsec_ipsec_t *this,
299 route_entry_t *route, host_t *src, host_t *dst)
300 {
301 exclude_route_t *exclude;
302 host_t *gtw;
303
304 if (this->excludes->find_first(this->excludes,
305 (linked_list_match_t)exclude_route_match,
306 (void**)&exclude, dst) == SUCCESS)
307 {
308 route->exclude = exclude;
309 exclude->refs++;
310 }
311
312 if (!route->exclude)
313 {
314 DBG2(DBG_KNL, "installing new exclude route for %H src %H", dst, src);
315 gtw = charon->kernel->get_nexthop(charon->kernel, dst, -1, NULL);
316 if (gtw)
317 {
318 char *if_name = NULL;
319
320 if (charon->kernel->get_interface(charon->kernel, src, &if_name) &&
321 charon->kernel->add_route(charon->kernel, dst->get_address(dst),
322 dst->get_family(dst) == AF_INET ? 32 : 128,
323 gtw, src, if_name) == SUCCESS)
324 {
325 INIT(exclude,
326 .dst = dst->clone(dst),
327 .src = src->clone(src),
328 .gtw = gtw->clone(gtw),
329 .refs = 1,
330 );
331 route->exclude = exclude;
332 this->excludes->insert_last(this->excludes, exclude);
333 }
334 else
335 {
336 DBG1(DBG_KNL, "installing exclude route for %H failed", dst);
337 }
338 gtw->destroy(gtw);
339 free(if_name);
340 }
341 else
342 {
343 DBG1(DBG_KNL, "gateway lookup for %H failed", dst);
344 }
345 }
346 }
347
348 /**
349 * Remove an exclude route attached to a routing entry
350 */
351 static void remove_exclude_route(private_kernel_libipsec_ipsec_t *this,
352 route_entry_t *route)
353 {
354 char *if_name = NULL;
355 host_t *dst;
356
357 if (!route->exclude || --route->exclude->refs > 0)
358 {
359 return;
360 }
361 this->excludes->remove(this->excludes, route->exclude, NULL);
362
363 dst = route->exclude->dst;
364 DBG2(DBG_KNL, "uninstalling exclude route for %H src %H",
365 dst, route->exclude->src);
366 if (charon->kernel->get_interface(charon->kernel, route->exclude->src,
367 &if_name) &&
368 charon->kernel->del_route(charon->kernel, dst->get_address(dst),
369 dst->get_family(dst) == AF_INET ? 32 : 128,
370 route->exclude->gtw, route->exclude->src,
371 if_name) != SUCCESS)
372 {
373 DBG1(DBG_KNL, "uninstalling exclude route for %H failed", dst);
374 }
375 exclude_route_destroy(route->exclude);
376 route->exclude = NULL;
377 free(if_name);
378 }
379
380 /**
381 * Install a route for the given policy
382 *
383 * this->mutex is released by this function
384 */
385 static bool install_route(private_kernel_libipsec_ipsec_t *this,
386 host_t *src, host_t *dst, traffic_selector_t *src_ts,
387 traffic_selector_t *dst_ts, policy_entry_t *policy)
388 {
389 route_entry_t *route, *old;
390 host_t *src_ip;
391 bool is_virtual;
392
393 if (policy->direction != POLICY_OUT)
394 {
395 this->mutex->unlock(this->mutex);
396 return TRUE;
397 }
398
399 if (charon->kernel->get_address_by_ts(charon->kernel, src_ts, &src_ip,
400 &is_virtual) != SUCCESS)
401 {
402 traffic_selector_t *multicast, *broadcast = NULL;
403 bool ignore = FALSE;
404
405 this->mutex->unlock(this->mutex);
406 switch (src_ts->get_type(src_ts))
407 {
408 case TS_IPV4_ADDR_RANGE:
409 multicast = traffic_selector_create_from_cidr("224.0.0.0/4",
410 0, 0, 0xffff);
411 broadcast = traffic_selector_create_from_cidr("255.255.255.255/32",
412 0, 0, 0xffff);
413 break;
414 case TS_IPV6_ADDR_RANGE:
415 multicast = traffic_selector_create_from_cidr("ff00::/8",
416 0, 0, 0xffff);
417 break;
418 default:
419 return FALSE;
420 }
421 ignore = src_ts->is_contained_in(src_ts, multicast);
422 ignore |= broadcast && src_ts->is_contained_in(src_ts, broadcast);
423 multicast->destroy(multicast);
424 DESTROY_IF(broadcast);
425 if (!ignore)
426 {
427 DBG1(DBG_KNL, "error installing route with policy %R === %R %N",
428 src_ts, dst_ts, policy_dir_names, policy->direction);
429 }
430 return ignore;
431 }
432
433 INIT(route,
434 .if_name = router->get_tun_name(router, is_virtual ? src_ip : NULL),
435 .src_ip = src_ip,
436 .dst_net = chunk_clone(policy->dst.net->get_address(policy->dst.net)),
437 .prefixlen = policy->dst.mask,
438 );
439 #ifndef __linux__
440 /* on Linux we cant't install a gateway */
441 route->gateway = charon->kernel->get_nexthop(charon->kernel, dst, -1, src);
442 #endif
443
444 if (policy->route)
445 {
446 old = policy->route;
447
448 if (route_entry_equals(old, route))
449 { /* such a route already exists */
450 route_entry_destroy(route);
451 this->mutex->unlock(this->mutex);
452 return TRUE;
453 }
454 /* uninstall previously installed route */
455 if (charon->kernel->del_route(charon->kernel, old->dst_net,
456 old->prefixlen, old->gateway,
457 old->src_ip, old->if_name) != SUCCESS)
458 {
459 DBG1(DBG_KNL, "error uninstalling route installed with policy "
460 "%R === %R %N", src_ts, dst_ts, policy_dir_names,
461 policy->direction);
462 }
463 route_entry_destroy(old);
464 policy->route = NULL;
465 }
466
467 if (!this->allow_peer_ts && dst_ts->is_host(dst_ts, dst))
468 {
469 DBG1(DBG_KNL, "can't install route for %R === %R %N, conflicts with "
470 "IKE traffic", src_ts, dst_ts, policy_dir_names,
471 policy->direction);
472 route_entry_destroy(route);
473 this->mutex->unlock(this->mutex);
474 return FALSE;
475 }
476 /* if remote traffic selector covers the IKE peer, add an exclude route */
477 if (!this->allow_peer_ts && dst_ts->includes(dst_ts, dst))
478 {
479 /* add exclude route for peer */
480 add_exclude_route(this, route, src, dst);
481 }
482
483 DBG2(DBG_KNL, "installing route: %R src %H dev %s",
484 dst_ts, route->src_ip, route->if_name);
485
486 switch (charon->kernel->add_route(charon->kernel, route->dst_net,
487 route->prefixlen, route->gateway,
488 route->src_ip, route->if_name))
489 {
490 case ALREADY_DONE:
491 /* route exists, do not uninstall */
492 remove_exclude_route(this, route);
493 route_entry_destroy(route);
494 this->mutex->unlock(this->mutex);
495 return TRUE;
496 case SUCCESS:
497 /* cache the installed route */
498 policy->route = route;
499 this->mutex->unlock(this->mutex);
500 return TRUE;
501 default:
502 DBG1(DBG_KNL, "installing route failed: %R src %H dev %s",
503 dst_ts, route->src_ip, route->if_name);
504 remove_exclude_route(this, route);
505 route_entry_destroy(route);
506 this->mutex->unlock(this->mutex);
507 return FALSE;
508 }
509 }
510
511 METHOD(kernel_ipsec_t, add_policy, status_t,
512 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
513 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
514 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa, mark_t mark,
515 policy_priority_t priority)
516 {
517 policy_entry_t *policy, *found = NULL;
518 status_t status;
519
520 status = ipsec->policies->add_policy(ipsec->policies, src, dst, src_ts,
521 dst_ts, direction, type, sa, mark, priority);
522 if (status != SUCCESS)
523 {
524 return status;
525 }
526 /* we track policies in order to install routes */
527 policy = create_policy_entry(src_ts, dst_ts, direction);
528
529 this->mutex->lock(this->mutex);
530 if (this->policies->find_first(this->policies,
531 (linked_list_match_t)policy_entry_equals,
532 (void**)&found, policy) == SUCCESS)
533 {
534 policy_entry_destroy(policy);
535 policy = found;
536 }
537 else
538 { /* use the new one, if we have no such policy */
539 this->policies->insert_last(this->policies, policy);
540 }
541 policy->refs++;
542
543 if (!install_route(this, src, dst, src_ts, dst_ts, policy))
544 {
545 return FAILED;
546 }
547 return SUCCESS;
548 }
549
550 METHOD(kernel_ipsec_t, query_policy, status_t,
551 private_kernel_libipsec_ipsec_t *this, traffic_selector_t *src_ts,
552 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
553 time_t *use_time)
554 {
555 return NOT_SUPPORTED;
556 }
557
558 METHOD(kernel_ipsec_t, del_policy, status_t,
559 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
560 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
561 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
562 mark_t mark, policy_priority_t priority)
563 {
564 policy_entry_t *policy, *found = NULL;
565 status_t status;
566
567 status = ipsec->policies->del_policy(ipsec->policies, src, dst, src_ts,
568 dst_ts, direction, type, sa, mark, priority);
569
570 policy = create_policy_entry(src_ts, dst_ts, direction);
571
572 this->mutex->lock(this->mutex);
573 if (this->policies->find_first(this->policies,
574 (linked_list_match_t)policy_entry_equals,
575 (void**)&found, policy) != SUCCESS)
576 {
577 policy_entry_destroy(policy);
578 this->mutex->unlock(this->mutex);
579 return status;
580 }
581 policy_entry_destroy(policy);
582 policy = found;
583
584 if (--policy->refs > 0)
585 { /* policy is still in use */
586 this->mutex->unlock(this->mutex);
587 return status;
588 }
589
590 if (policy->route)
591 {
592 route_entry_t *route = policy->route;
593
594 if (charon->kernel->del_route(charon->kernel, route->dst_net,
595 route->prefixlen, route->gateway,
596 route->src_ip, route->if_name) != SUCCESS)
597 {
598 DBG1(DBG_KNL, "error uninstalling route installed with "
599 "policy %R === %R %N", src_ts, dst_ts,
600 policy_dir_names, direction);
601 }
602 remove_exclude_route(this, route);
603 }
604 this->policies->remove(this->policies, policy, NULL);
605 policy_entry_destroy(policy);
606 this->mutex->unlock(this->mutex);
607 return status;
608 }
609
610 METHOD(kernel_ipsec_t, flush_policies, status_t,
611 private_kernel_libipsec_ipsec_t *this)
612 {
613 policy_entry_t *pol;
614 status_t status;
615
616 status = ipsec->policies->flush_policies(ipsec->policies);
617
618 this->mutex->lock(this->mutex);
619 while (this->policies->remove_first(this->policies, (void*)&pol) == SUCCESS)
620 {
621 if (pol->route)
622 {
623 route_entry_t *route = pol->route;
624
625 charon->kernel->del_route(charon->kernel, route->dst_net,
626 route->prefixlen, route->gateway,
627 route->src_ip, route->if_name);
628 remove_exclude_route(this, route);
629 }
630 policy_entry_destroy(pol);
631 }
632 this->mutex->unlock(this->mutex);
633 return status;
634 }
635
636 METHOD(kernel_ipsec_t, bypass_socket, bool,
637 private_kernel_libipsec_ipsec_t *this, int fd, int family)
638 {
639 /* we use exclude routes for this */
640 return NOT_SUPPORTED;
641 }
642
643 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
644 private_kernel_libipsec_ipsec_t *this, int fd, int family, u_int16_t port)
645 {
646 return NOT_SUPPORTED;
647 }
648
649 METHOD(kernel_ipsec_t, destroy, void,
650 private_kernel_libipsec_ipsec_t *this)
651 {
652 ipsec->events->unregister_listener(ipsec->events, &this->ipsec_listener);
653 this->policies->destroy_function(this->policies, (void*)policy_entry_destroy);
654 this->excludes->destroy(this->excludes);
655 this->mutex->destroy(this->mutex);
656 free(this);
657 }
658
659 /*
660 * Described in header.
661 */
662 kernel_libipsec_ipsec_t *kernel_libipsec_ipsec_create()
663 {
664 private_kernel_libipsec_ipsec_t *this;
665
666 INIT(this,
667 .public = {
668 .interface = {
669 .get_features = _get_features,
670 .get_spi = _get_spi,
671 .get_cpi = _get_cpi,
672 .add_sa = _add_sa,
673 .update_sa = _update_sa,
674 .query_sa = _query_sa,
675 .del_sa = _del_sa,
676 .flush_sas = _flush_sas,
677 .add_policy = _add_policy,
678 .query_policy = _query_policy,
679 .del_policy = _del_policy,
680 .flush_policies = _flush_policies,
681 .bypass_socket = _bypass_socket,
682 .enable_udp_decap = _enable_udp_decap,
683 .destroy = _destroy,
684 },
685 },
686 .ipsec_listener = {
687 .expire = expire,
688 },
689 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
690 .policies = linked_list_create(),
691 .excludes = linked_list_create(),
692 .allow_peer_ts = lib->settings->get_bool(lib->settings,
693 "%s.plugins.kernel-libipsec.allow_peer_ts", FALSE, lib->ns),
694 );
695
696 ipsec->events->register_listener(ipsec->events, &this->ipsec_listener);
697
698 return &this->public;
699 };