libipsec: Pass the same data to del_policy() as to add_policy()
[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 <hydra.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 hydra->kernel_interface->expire(hydra->kernel_interface, protocol,
228 spi, dst, hard);
229 }
230
231 METHOD(kernel_ipsec_t, get_features, kernel_feature_t,
232 private_kernel_libipsec_ipsec_t *this)
233 {
234 return KERNEL_REQUIRE_UDP_ENCAPSULATION | KERNEL_ESP_V3_TFC;
235 }
236
237 METHOD(kernel_ipsec_t, get_spi, status_t,
238 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
239 u_int8_t protocol, u_int32_t *spi)
240 {
241 return ipsec->sas->get_spi(ipsec->sas, src, dst, protocol, spi);
242 }
243
244 METHOD(kernel_ipsec_t, get_cpi, status_t,
245 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
246 u_int16_t *cpi)
247 {
248 return NOT_SUPPORTED;
249 }
250
251 METHOD(kernel_ipsec_t, add_sa, status_t,
252 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
253 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
254 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
255 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
256 u_int16_t ipcomp, u_int16_t cpi, u_int32_t replay_window,
257 bool initiator, bool encap, bool esn, bool inbound, bool update,
258 linked_list_t *src_ts, linked_list_t *dst_ts)
259 {
260 return ipsec->sas->add_sa(ipsec->sas, src, dst, spi, protocol, reqid, mark,
261 tfc, lifetime, enc_alg, enc_key, int_alg, int_key,
262 mode, ipcomp, cpi, initiator, encap, esn,
263 inbound, update);
264 }
265
266 METHOD(kernel_ipsec_t, update_sa, status_t,
267 private_kernel_libipsec_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
268 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
269 bool encap, bool new_encap, mark_t mark)
270 {
271 return NOT_SUPPORTED;
272 }
273
274 METHOD(kernel_ipsec_t, query_sa, status_t,
275 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
276 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes,
277 u_int64_t *packets, time_t *time)
278 {
279 return ipsec->sas->query_sa(ipsec->sas, src, dst, spi, protocol, mark,
280 bytes, packets, time);
281 }
282
283 METHOD(kernel_ipsec_t, del_sa, status_t,
284 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
285 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
286 {
287 return ipsec->sas->del_sa(ipsec->sas, src, dst, spi, protocol, cpi, mark);
288 }
289
290 METHOD(kernel_ipsec_t, flush_sas, status_t,
291 private_kernel_libipsec_ipsec_t *this)
292 {
293 return ipsec->sas->flush_sas(ipsec->sas);
294 }
295
296 /**
297 * Add an explicit exclude route to a routing entry
298 */
299 static void add_exclude_route(private_kernel_libipsec_ipsec_t *this,
300 route_entry_t *route, host_t *src, host_t *dst)
301 {
302 exclude_route_t *exclude;
303 host_t *gtw;
304
305 if (this->excludes->find_first(this->excludes,
306 (linked_list_match_t)exclude_route_match,
307 (void**)&exclude, dst) == SUCCESS)
308 {
309 route->exclude = exclude;
310 exclude->refs++;
311 }
312
313 if (!route->exclude)
314 {
315 DBG2(DBG_KNL, "installing new exclude route for %H src %H", dst, src);
316 gtw = hydra->kernel_interface->get_nexthop(hydra->kernel_interface,
317 dst, -1, NULL);
318 if (gtw)
319 {
320 char *if_name = NULL;
321
322 if (hydra->kernel_interface->get_interface(
323 hydra->kernel_interface, src, &if_name) &&
324 hydra->kernel_interface->add_route(hydra->kernel_interface,
325 dst->get_address(dst),
326 dst->get_family(dst) == AF_INET ? 32 : 128,
327 gtw, src, if_name) == SUCCESS)
328 {
329 INIT(exclude,
330 .dst = dst->clone(dst),
331 .src = src->clone(src),
332 .gtw = gtw->clone(gtw),
333 .refs = 1,
334 );
335 route->exclude = exclude;
336 this->excludes->insert_last(this->excludes, exclude);
337 }
338 else
339 {
340 DBG1(DBG_KNL, "installing exclude route for %H failed", dst);
341 }
342 gtw->destroy(gtw);
343 free(if_name);
344 }
345 else
346 {
347 DBG1(DBG_KNL, "gateway lookup for %H failed", dst);
348 }
349 }
350 }
351
352 /**
353 * Remove an exclude route attached to a routing entry
354 */
355 static void remove_exclude_route(private_kernel_libipsec_ipsec_t *this,
356 route_entry_t *route)
357 {
358 char *if_name = NULL;
359 host_t *dst;
360
361 if (!route->exclude || --route->exclude->refs > 0)
362 {
363 return;
364 }
365 this->excludes->remove(this->excludes, route->exclude, NULL);
366
367 dst = route->exclude->dst;
368 DBG2(DBG_KNL, "uninstalling exclude route for %H src %H",
369 dst, route->exclude->src);
370 if (hydra->kernel_interface->get_interface(
371 hydra->kernel_interface,
372 route->exclude->src, &if_name) &&
373 hydra->kernel_interface->del_route(hydra->kernel_interface,
374 dst->get_address(dst),
375 dst->get_family(dst) == AF_INET ? 32 : 128,
376 route->exclude->gtw, route->exclude->src,
377 if_name) != SUCCESS)
378 {
379 DBG1(DBG_KNL, "uninstalling exclude route for %H failed", dst);
380 }
381 exclude_route_destroy(route->exclude);
382 route->exclude = NULL;
383 free(if_name);
384 }
385
386 /**
387 * Install a route for the given policy
388 *
389 * this->mutex is released by this function
390 */
391 static bool install_route(private_kernel_libipsec_ipsec_t *this,
392 host_t *src, host_t *dst, traffic_selector_t *src_ts,
393 traffic_selector_t *dst_ts, policy_entry_t *policy)
394 {
395 route_entry_t *route, *old;
396 host_t *src_ip;
397 bool is_virtual;
398
399 if (policy->direction != POLICY_OUT)
400 {
401 this->mutex->unlock(this->mutex);
402 return TRUE;
403 }
404
405 if (hydra->kernel_interface->get_address_by_ts(hydra->kernel_interface,
406 src_ts, &src_ip, &is_virtual) != SUCCESS)
407 {
408 traffic_selector_t *multicast, *broadcast = NULL;
409 bool ignore = FALSE;
410
411 this->mutex->unlock(this->mutex);
412 switch (src_ts->get_type(src_ts))
413 {
414 case TS_IPV4_ADDR_RANGE:
415 multicast = traffic_selector_create_from_cidr("224.0.0.0/4",
416 0, 0, 0xffff);
417 broadcast = traffic_selector_create_from_cidr("255.255.255.255/32",
418 0, 0, 0xffff);
419 break;
420 case TS_IPV6_ADDR_RANGE:
421 multicast = traffic_selector_create_from_cidr("ff00::/8",
422 0, 0, 0xffff);
423 break;
424 default:
425 return FALSE;
426 }
427 ignore = src_ts->is_contained_in(src_ts, multicast);
428 ignore |= broadcast && src_ts->is_contained_in(src_ts, broadcast);
429 multicast->destroy(multicast);
430 DESTROY_IF(broadcast);
431 if (!ignore)
432 {
433 DBG1(DBG_KNL, "error installing route with policy %R === %R %N",
434 src_ts, dst_ts, policy_dir_names, policy->direction);
435 }
436 return ignore;
437 }
438
439 INIT(route,
440 .if_name = router->get_tun_name(router, is_virtual ? src_ip : NULL),
441 .src_ip = src_ip,
442 .dst_net = chunk_clone(policy->dst.net->get_address(policy->dst.net)),
443 .prefixlen = policy->dst.mask,
444 );
445 #ifndef __linux__
446 /* on Linux we cant't install a gateway */
447 route->gateway = hydra->kernel_interface->get_nexthop(
448 hydra->kernel_interface, dst, -1, src);
449 #endif
450
451 if (policy->route)
452 {
453 old = policy->route;
454
455 if (route_entry_equals(old, route))
456 { /* such a route already exists */
457 route_entry_destroy(route);
458 this->mutex->unlock(this->mutex);
459 return TRUE;
460 }
461 /* uninstall previously installed route */
462 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
463 old->dst_net, old->prefixlen, old->gateway,
464 old->src_ip, old->if_name) != SUCCESS)
465 {
466 DBG1(DBG_KNL, "error uninstalling route installed with policy "
467 "%R === %R %N", src_ts, dst_ts, policy_dir_names,
468 policy->direction);
469 }
470 route_entry_destroy(old);
471 policy->route = NULL;
472 }
473
474 if (!this->allow_peer_ts && dst_ts->is_host(dst_ts, dst))
475 {
476 DBG1(DBG_KNL, "can't install route for %R === %R %N, conflicts with "
477 "IKE traffic", src_ts, dst_ts, policy_dir_names,
478 policy->direction);
479 route_entry_destroy(route);
480 this->mutex->unlock(this->mutex);
481 return FALSE;
482 }
483 /* if remote traffic selector covers the IKE peer, add an exclude route */
484 if (!this->allow_peer_ts && dst_ts->includes(dst_ts, dst))
485 {
486 /* add exclude route for peer */
487 add_exclude_route(this, route, src, dst);
488 }
489
490 DBG2(DBG_KNL, "installing route: %R src %H dev %s",
491 dst_ts, route->src_ip, route->if_name);
492
493 switch (hydra->kernel_interface->add_route(hydra->kernel_interface,
494 route->dst_net, route->prefixlen, route->gateway,
495 route->src_ip, route->if_name))
496 {
497 case ALREADY_DONE:
498 /* route exists, do not uninstall */
499 remove_exclude_route(this, route);
500 route_entry_destroy(route);
501 this->mutex->unlock(this->mutex);
502 return TRUE;
503 case SUCCESS:
504 /* cache the installed route */
505 policy->route = route;
506 this->mutex->unlock(this->mutex);
507 return TRUE;
508 default:
509 DBG1(DBG_KNL, "installing route failed: %R src %H dev %s",
510 dst_ts, route->src_ip, route->if_name);
511 remove_exclude_route(this, route);
512 route_entry_destroy(route);
513 this->mutex->unlock(this->mutex);
514 return FALSE;
515 }
516 }
517
518 METHOD(kernel_ipsec_t, add_policy, status_t,
519 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
520 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
521 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa, mark_t mark,
522 policy_priority_t priority)
523 {
524 policy_entry_t *policy, *found = NULL;
525 status_t status;
526
527 status = ipsec->policies->add_policy(ipsec->policies, src, dst, src_ts,
528 dst_ts, direction, type, sa, mark, priority);
529 if (status != SUCCESS)
530 {
531 return status;
532 }
533 /* we track policies in order to install routes */
534 policy = create_policy_entry(src_ts, dst_ts, direction);
535
536 this->mutex->lock(this->mutex);
537 if (this->policies->find_first(this->policies,
538 (linked_list_match_t)policy_entry_equals,
539 (void**)&found, policy) == SUCCESS)
540 {
541 policy_entry_destroy(policy);
542 policy = found;
543 }
544 else
545 { /* use the new one, if we have no such policy */
546 this->policies->insert_last(this->policies, policy);
547 }
548 policy->refs++;
549
550 if (!install_route(this, src, dst, src_ts, dst_ts, policy))
551 {
552 return FAILED;
553 }
554 return SUCCESS;
555 }
556
557 METHOD(kernel_ipsec_t, query_policy, status_t,
558 private_kernel_libipsec_ipsec_t *this, traffic_selector_t *src_ts,
559 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
560 time_t *use_time)
561 {
562 return NOT_SUPPORTED;
563 }
564
565 METHOD(kernel_ipsec_t, del_policy, status_t,
566 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
567 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
568 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
569 mark_t mark, policy_priority_t priority)
570 {
571 policy_entry_t *policy, *found = NULL;
572 status_t status;
573
574 status = ipsec->policies->del_policy(ipsec->policies, src, dst, src_ts,
575 dst_ts, direction, type, sa, mark, priority);
576
577 policy = create_policy_entry(src_ts, dst_ts, direction);
578
579 this->mutex->lock(this->mutex);
580 if (this->policies->find_first(this->policies,
581 (linked_list_match_t)policy_entry_equals,
582 (void**)&found, policy) != SUCCESS)
583 {
584 policy_entry_destroy(policy);
585 this->mutex->unlock(this->mutex);
586 return status;
587 }
588 policy_entry_destroy(policy);
589 policy = found;
590
591 if (--policy->refs > 0)
592 { /* policy is still in use */
593 this->mutex->unlock(this->mutex);
594 return status;
595 }
596
597 if (policy->route)
598 {
599 route_entry_t *route = policy->route;
600
601 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
602 route->dst_net, route->prefixlen, route->gateway, route->src_ip,
603 route->if_name) != SUCCESS)
604 {
605 DBG1(DBG_KNL, "error uninstalling route installed with "
606 "policy %R === %R %N", src_ts, dst_ts,
607 policy_dir_names, direction);
608 }
609 remove_exclude_route(this, route);
610 }
611 this->policies->remove(this->policies, policy, NULL);
612 policy_entry_destroy(policy);
613 this->mutex->unlock(this->mutex);
614 return status;
615 }
616
617 METHOD(kernel_ipsec_t, flush_policies, status_t,
618 private_kernel_libipsec_ipsec_t *this)
619 {
620 policy_entry_t *pol;
621 status_t status;
622
623 status = ipsec->policies->flush_policies(ipsec->policies);
624
625 this->mutex->lock(this->mutex);
626 while (this->policies->remove_first(this->policies, (void*)&pol) == SUCCESS)
627 {
628 if (pol->route)
629 {
630 route_entry_t *route = pol->route;
631
632 hydra->kernel_interface->del_route(hydra->kernel_interface,
633 route->dst_net, route->prefixlen, route->gateway,
634 route->src_ip, route->if_name);
635 remove_exclude_route(this, route);
636 }
637 policy_entry_destroy(pol);
638 }
639 this->mutex->unlock(this->mutex);
640 return status;
641 }
642
643 METHOD(kernel_ipsec_t, bypass_socket, bool,
644 private_kernel_libipsec_ipsec_t *this, int fd, int family)
645 {
646 /* we use exclude routes for this */
647 return NOT_SUPPORTED;
648 }
649
650 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
651 private_kernel_libipsec_ipsec_t *this, int fd, int family, u_int16_t port)
652 {
653 return NOT_SUPPORTED;
654 }
655
656 METHOD(kernel_ipsec_t, destroy, void,
657 private_kernel_libipsec_ipsec_t *this)
658 {
659 ipsec->events->unregister_listener(ipsec->events, &this->ipsec_listener);
660 this->policies->destroy_function(this->policies, (void*)policy_entry_destroy);
661 this->excludes->destroy(this->excludes);
662 this->mutex->destroy(this->mutex);
663 free(this);
664 }
665
666 /*
667 * Described in header.
668 */
669 kernel_libipsec_ipsec_t *kernel_libipsec_ipsec_create()
670 {
671 private_kernel_libipsec_ipsec_t *this;
672
673 INIT(this,
674 .public = {
675 .interface = {
676 .get_features = _get_features,
677 .get_spi = _get_spi,
678 .get_cpi = _get_cpi,
679 .add_sa = _add_sa,
680 .update_sa = _update_sa,
681 .query_sa = _query_sa,
682 .del_sa = _del_sa,
683 .flush_sas = _flush_sas,
684 .add_policy = _add_policy,
685 .query_policy = _query_policy,
686 .del_policy = _del_policy,
687 .flush_policies = _flush_policies,
688 .bypass_socket = _bypass_socket,
689 .enable_udp_decap = _enable_udp_decap,
690 .destroy = _destroy,
691 },
692 },
693 .ipsec_listener = {
694 .expire = expire,
695 },
696 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
697 .policies = linked_list_create(),
698 .excludes = linked_list_create(),
699 .allow_peer_ts = lib->settings->get_bool(lib->settings,
700 "%s.plugins.kernel-libipsec.allow_peer_ts", FALSE, lib->ns),
701 );
702
703 ipsec->events->register_listener(ipsec->events, &this->ipsec_listener);
704
705 return &this->public;
706 };