Don't wait while removing external IPs used for load testing
[strongswan.git] / src / libhydra / plugins / kernel_netlink / kernel_netlink_net.c
1 /*
2 * Copyright (C) 2008-2012 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /*
18 * Copyright (C) 2010 secunet Security Networks AG
19 * Copyright (C) 2010 Thomas Egerer
20 *
21 * Permission is hereby granted, free of charge, to any person obtaining a copy
22 * of this software and associated documentation files (the "Software"), to deal
23 * in the Software without restriction, including without limitation the rights
24 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 * copies of the Software, and to permit persons to whom the Software is
26 * furnished to do so, subject to the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 * THE SOFTWARE.
38 */
39
40 #include <sys/socket.h>
41 #include <sys/utsname.h>
42 #include <linux/netlink.h>
43 #include <linux/rtnetlink.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <net/if.h>
47
48 #include "kernel_netlink_net.h"
49 #include "kernel_netlink_shared.h"
50
51 #include <hydra.h>
52 #include <utils/debug.h>
53 #include <threading/thread.h>
54 #include <threading/mutex.h>
55 #include <threading/rwlock.h>
56 #include <threading/rwlock_condvar.h>
57 #include <threading/spinlock.h>
58 #include <collections/hashtable.h>
59 #include <collections/linked_list.h>
60 #include <processing/jobs/callback_job.h>
61
62 /** delay before firing roam events (ms) */
63 #define ROAM_DELAY 100
64
65 /** delay before reinstalling routes (ms) */
66 #define ROUTE_DELAY 100
67
68 /** maximum recursion when searching for addresses in get_route() */
69 #define MAX_ROUTE_RECURSION 2
70
71 typedef struct addr_entry_t addr_entry_t;
72
73 /**
74 * IP address in an iface_entry_t
75 */
76 struct addr_entry_t {
77
78 /** the ip address */
79 host_t *ip;
80
81 /** scope of the address */
82 u_char scope;
83
84 /** number of times this IP is used, if virtual (i.e. managed by us) */
85 u_int refcount;
86
87 /** TRUE once it is installed, if virtual */
88 bool installed;
89 };
90
91 /**
92 * destroy a addr_entry_t object
93 */
94 static void addr_entry_destroy(addr_entry_t *this)
95 {
96 this->ip->destroy(this->ip);
97 free(this);
98 }
99
100 typedef struct iface_entry_t iface_entry_t;
101
102 /**
103 * A network interface on this system, containing addr_entry_t's
104 */
105 struct iface_entry_t {
106
107 /** interface index */
108 int ifindex;
109
110 /** name of the interface */
111 char ifname[IFNAMSIZ];
112
113 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
114 u_int flags;
115
116 /** list of addresses as host_t */
117 linked_list_t *addrs;
118
119 /** TRUE if usable by config */
120 bool usable;
121 };
122
123 /**
124 * destroy an interface entry
125 */
126 static void iface_entry_destroy(iface_entry_t *this)
127 {
128 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
129 free(this);
130 }
131
132 /**
133 * find an interface entry by index
134 */
135 static bool iface_entry_by_index(iface_entry_t *this, int *ifindex)
136 {
137 return this->ifindex == *ifindex;
138 }
139
140 /**
141 * find an interface entry by name
142 */
143 static bool iface_entry_by_name(iface_entry_t *this, char *ifname)
144 {
145 return streq(this->ifname, ifname);
146 }
147
148 /**
149 * check if an interface is up
150 */
151 static inline bool iface_entry_up(iface_entry_t *iface)
152 {
153 return (iface->flags & IFF_UP) == IFF_UP;
154 }
155
156 /**
157 * check if an interface is up and usable
158 */
159 static inline bool iface_entry_up_and_usable(iface_entry_t *iface)
160 {
161 return iface->usable && iface_entry_up(iface);
162 }
163
164 typedef struct addr_map_entry_t addr_map_entry_t;
165
166 /**
167 * Entry that maps an IP address to an interface entry
168 */
169 struct addr_map_entry_t {
170 /** The IP address */
171 host_t *ip;
172
173 /** The address entry for this IP address */
174 addr_entry_t *addr;
175
176 /** The interface this address is installed on */
177 iface_entry_t *iface;
178 };
179
180 /**
181 * Hash a addr_map_entry_t object, all entries with the same IP address
182 * are stored in the same bucket
183 */
184 static u_int addr_map_entry_hash(addr_map_entry_t *this)
185 {
186 return chunk_hash(this->ip->get_address(this->ip));
187 }
188
189 /**
190 * Compare two addr_map_entry_t objects, two entries are equal if they are
191 * installed on the same interface
192 */
193 static bool addr_map_entry_equals(addr_map_entry_t *a, addr_map_entry_t *b)
194 {
195 return a->iface->ifindex == b->iface->ifindex &&
196 a->ip->ip_equals(a->ip, b->ip);
197 }
198
199 /**
200 * Used with get_match this finds an address entry if it is installed on
201 * an up and usable interface
202 */
203 static bool addr_map_entry_match_up_and_usable(addr_map_entry_t *a,
204 addr_map_entry_t *b)
205 {
206 return iface_entry_up_and_usable(b->iface) &&
207 a->ip->ip_equals(a->ip, b->ip);
208 }
209
210 /**
211 * Used with get_match this finds an address entry if it is installed on
212 * any active local interface
213 */
214 static bool addr_map_entry_match_up(addr_map_entry_t *a, addr_map_entry_t *b)
215 {
216 return iface_entry_up(b->iface) && a->ip->ip_equals(a->ip, b->ip);
217 }
218
219 /**
220 * Used with get_match this finds an address entry if it is installed on
221 * any local interface
222 */
223 static bool addr_map_entry_match(addr_map_entry_t *a, addr_map_entry_t *b)
224 {
225 return a->ip->ip_equals(a->ip, b->ip);
226 }
227
228 typedef struct route_entry_t route_entry_t;
229
230 /**
231 * Installed routing entry
232 */
233 struct route_entry_t {
234 /** Name of the interface the route is bound to */
235 char *if_name;
236
237 /** Source ip of the route */
238 host_t *src_ip;
239
240 /** Gateway for this route */
241 host_t *gateway;
242
243 /** Destination net */
244 chunk_t dst_net;
245
246 /** Destination net prefixlen */
247 u_int8_t prefixlen;
248 };
249
250 /**
251 * Clone a route_entry_t object.
252 */
253 static route_entry_t *route_entry_clone(route_entry_t *this)
254 {
255 route_entry_t *route;
256
257 INIT(route,
258 .if_name = strdup(this->if_name),
259 .src_ip = this->src_ip->clone(this->src_ip),
260 .gateway = this->gateway->clone(this->gateway),
261 .dst_net = chunk_clone(this->dst_net),
262 .prefixlen = this->prefixlen,
263 );
264 return route;
265 }
266
267 /**
268 * Destroy a route_entry_t object
269 */
270 static void route_entry_destroy(route_entry_t *this)
271 {
272 free(this->if_name);
273 DESTROY_IF(this->src_ip);
274 DESTROY_IF(this->gateway);
275 chunk_free(&this->dst_net);
276 free(this);
277 }
278
279 /**
280 * Hash a route_entry_t object
281 */
282 static u_int route_entry_hash(route_entry_t *this)
283 {
284 return chunk_hash_inc(chunk_from_thing(this->prefixlen),
285 chunk_hash(this->dst_net));
286 }
287
288 /**
289 * Compare two route_entry_t objects
290 */
291 static bool route_entry_equals(route_entry_t *a, route_entry_t *b)
292 {
293 return a->if_name && b->if_name && streq(a->if_name, b->if_name) &&
294 a->src_ip->ip_equals(a->src_ip, b->src_ip) &&
295 a->gateway->ip_equals(a->gateway, b->gateway) &&
296 chunk_equals(a->dst_net, b->dst_net) && a->prefixlen == b->prefixlen;
297 }
298
299 typedef struct net_change_t net_change_t;
300
301 /**
302 * Queued network changes
303 */
304 struct net_change_t {
305 /** Name of the interface that got activated (or an IP appeared on) */
306 char *if_name;
307 };
308
309 /**
310 * Destroy a net_change_t object
311 */
312 static void net_change_destroy(net_change_t *this)
313 {
314 free(this->if_name);
315 free(this);
316 }
317
318 /**
319 * Hash a net_change_t object
320 */
321 static u_int net_change_hash(net_change_t *this)
322 {
323 return chunk_hash(chunk_create(this->if_name, strlen(this->if_name)));
324 }
325
326 /**
327 * Compare two net_change_t objects
328 */
329 static bool net_change_equals(net_change_t *a, net_change_t *b)
330 {
331 return streq(a->if_name, b->if_name);
332 }
333
334 typedef struct private_kernel_netlink_net_t private_kernel_netlink_net_t;
335
336 /**
337 * Private variables and functions of kernel_netlink_net class.
338 */
339 struct private_kernel_netlink_net_t {
340 /**
341 * Public part of the kernel_netlink_net_t object.
342 */
343 kernel_netlink_net_t public;
344
345 /**
346 * lock to access various lists and maps
347 */
348 rwlock_t *lock;
349
350 /**
351 * condition variable to signal virtual IP add/removal
352 */
353 rwlock_condvar_t *condvar;
354
355 /**
356 * Cached list of interfaces and its addresses (iface_entry_t)
357 */
358 linked_list_t *ifaces;
359
360 /**
361 * Map for IP addresses to iface_entry_t objects (addr_map_entry_t)
362 */
363 hashtable_t *addrs;
364
365 /**
366 * Map for virtual IP addresses to iface_entry_t objects (addr_map_entry_t)
367 */
368 hashtable_t *vips;
369
370 /**
371 * netlink rt socket (routing)
372 */
373 netlink_socket_t *socket;
374
375 /**
376 * Netlink rt socket to receive address change events
377 */
378 int socket_events;
379
380 /**
381 * earliest time of the next roam event
382 */
383 timeval_t next_roam;
384
385 /**
386 * lock to check and update roam event time
387 */
388 spinlock_t *roam_lock;
389
390 /**
391 * routing table to install routes
392 */
393 int routing_table;
394
395 /**
396 * priority of used routing table
397 */
398 int routing_table_prio;
399
400 /**
401 * installed routes
402 */
403 hashtable_t *routes;
404
405 /**
406 * mutex for routes
407 */
408 mutex_t *routes_lock;
409
410 /**
411 * interface changes which may trigger route reinstallation
412 */
413 hashtable_t *net_changes;
414
415 /**
416 * mutex for route reinstallation triggers
417 */
418 mutex_t *net_changes_lock;
419
420 /**
421 * time of last route reinstallation
422 */
423 timeval_t last_route_reinstall;
424
425 /**
426 * whether to react to RTM_NEWROUTE or RTM_DELROUTE events
427 */
428 bool process_route;
429
430 /**
431 * whether to actually install virtual IPs
432 */
433 bool install_virtual_ip;
434
435 /**
436 * the name of the interface virtual IP addresses are installed on
437 */
438 char *install_virtual_ip_on;
439
440 /**
441 * whether preferred source addresses can be specified for IPv6 routes
442 */
443 bool rta_prefsrc_for_ipv6;
444
445 /**
446 * list with routing tables to be excluded from route lookup
447 */
448 linked_list_t *rt_exclude;
449 };
450
451 /**
452 * Forward declaration
453 */
454 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
455 int nlmsg_type, int flags, chunk_t dst_net,
456 u_int8_t prefixlen, host_t *gateway,
457 host_t *src_ip, char *if_name);
458
459 /**
460 * Clear the queued network changes.
461 */
462 static void net_changes_clear(private_kernel_netlink_net_t *this)
463 {
464 enumerator_t *enumerator;
465 net_change_t *change;
466
467 enumerator = this->net_changes->create_enumerator(this->net_changes);
468 while (enumerator->enumerate(enumerator, NULL, (void**)&change))
469 {
470 this->net_changes->remove_at(this->net_changes, enumerator);
471 net_change_destroy(change);
472 }
473 enumerator->destroy(enumerator);
474 }
475
476 /**
477 * Act upon queued network changes.
478 */
479 static job_requeue_t reinstall_routes(private_kernel_netlink_net_t *this)
480 {
481 enumerator_t *enumerator;
482 route_entry_t *route;
483
484 this->net_changes_lock->lock(this->net_changes_lock);
485 this->routes_lock->lock(this->routes_lock);
486
487 enumerator = this->routes->create_enumerator(this->routes);
488 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
489 {
490 net_change_t *change, lookup = {
491 .if_name = route->if_name,
492 };
493 /* check if a change for the outgoing interface is queued */
494 change = this->net_changes->get(this->net_changes, &lookup);
495 if (!change)
496 { /* in case src_ip is not on the outgoing interface */
497 if (this->public.interface.get_interface(&this->public.interface,
498 route->src_ip, &lookup.if_name))
499 {
500 if (!streq(lookup.if_name, route->if_name))
501 {
502 change = this->net_changes->get(this->net_changes, &lookup);
503 }
504 free(lookup.if_name);
505 }
506 }
507 if (change)
508 {
509 manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
510 route->dst_net, route->prefixlen, route->gateway,
511 route->src_ip, route->if_name);
512 }
513 }
514 enumerator->destroy(enumerator);
515 this->routes_lock->unlock(this->routes_lock);
516
517 net_changes_clear(this);
518 this->net_changes_lock->unlock(this->net_changes_lock);
519 return JOB_REQUEUE_NONE;
520 }
521
522 /**
523 * Queue route reinstallation caused by network changes for a given interface.
524 *
525 * The route reinstallation is delayed for a while and only done once for
526 * several calls during this delay, in order to avoid doing it too often.
527 * The interface name is freed.
528 */
529 static void queue_route_reinstall(private_kernel_netlink_net_t *this,
530 char *if_name)
531 {
532 net_change_t *update, *found;
533 timeval_t now;
534 job_t *job;
535
536 INIT(update,
537 .if_name = if_name
538 );
539
540 this->net_changes_lock->lock(this->net_changes_lock);
541 found = this->net_changes->put(this->net_changes, update, update);
542 if (found)
543 {
544 net_change_destroy(found);
545 }
546 time_monotonic(&now);
547 if (timercmp(&now, &this->last_route_reinstall, >))
548 {
549 timeval_add_ms(&now, ROUTE_DELAY);
550 this->last_route_reinstall = now;
551
552 job = (job_t*)callback_job_create((callback_job_cb_t)reinstall_routes,
553 this, NULL, NULL);
554 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROUTE_DELAY);
555 }
556 this->net_changes_lock->unlock(this->net_changes_lock);
557 }
558
559 /**
560 * check if the given IP is known as virtual IP and currently installed
561 *
562 * this function will also return TRUE if the virtual IP entry disappeared.
563 * in that case the returned entry will be NULL.
564 *
565 * this->lock must be held when calling this function
566 */
567 static bool is_vip_installed_or_gone(private_kernel_netlink_net_t *this,
568 host_t *ip, addr_map_entry_t **entry)
569 {
570 addr_map_entry_t lookup = {
571 .ip = ip,
572 };
573
574 *entry = this->vips->get_match(this->vips, &lookup,
575 (void*)addr_map_entry_match);
576 if (*entry == NULL)
577 { /* the virtual IP disappeared */
578 return TRUE;
579 }
580 return (*entry)->addr->installed;
581 }
582
583 /**
584 * check if the given IP is known as virtual IP
585 *
586 * this->lock must be held when calling this function
587 */
588 static bool is_known_vip(private_kernel_netlink_net_t *this, host_t *ip)
589 {
590 addr_map_entry_t lookup = {
591 .ip = ip,
592 };
593
594 return this->vips->get_match(this->vips, &lookup,
595 (void*)addr_map_entry_match) != NULL;
596 }
597
598 /**
599 * Add an address map entry
600 */
601 static void addr_map_entry_add(hashtable_t *map, addr_entry_t *addr,
602 iface_entry_t *iface)
603 {
604 addr_map_entry_t *entry;
605
606 INIT(entry,
607 .ip = addr->ip,
608 .addr = addr,
609 .iface = iface,
610 );
611 entry = map->put(map, entry, entry);
612 free(entry);
613 }
614
615 /**
616 * Remove an address map entry
617 */
618 static void addr_map_entry_remove(hashtable_t *map, addr_entry_t *addr,
619 iface_entry_t *iface)
620 {
621 addr_map_entry_t *entry, lookup = {
622 .ip = addr->ip,
623 .addr = addr,
624 .iface = iface,
625 };
626
627 entry = map->remove(map, &lookup);
628 free(entry);
629 }
630
631 /**
632 * get the first non-virtual ip address on the given interface.
633 * if a candidate address is given, we first search for that address and if not
634 * found return the address as above.
635 * returned host is a clone, has to be freed by caller.
636 *
637 * this->lock must be held when calling this function
638 */
639 static host_t *get_interface_address(private_kernel_netlink_net_t *this,
640 int ifindex, int family, host_t *candidate)
641 {
642 iface_entry_t *iface;
643 enumerator_t *addrs;
644 addr_entry_t *addr;
645 host_t *ip = NULL;
646
647 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
648 (void**)&iface, &ifindex) == SUCCESS)
649 {
650 if (iface->usable)
651 { /* only use interfaces not excluded by config */
652 addrs = iface->addrs->create_enumerator(iface->addrs);
653 while (addrs->enumerate(addrs, &addr))
654 {
655 if (addr->refcount)
656 { /* ignore virtual IP addresses */
657 continue;
658 }
659 if (addr->ip->get_family(addr->ip) == family)
660 {
661 if (!candidate || candidate->ip_equals(candidate, addr->ip))
662 { /* stop at the first address if we don't search for a
663 * candidate or if the candidate matches */
664 ip = addr->ip;
665 break;
666 }
667 else if (!ip)
668 { /* store the first address as fallback if candidate is
669 * not found */
670 ip = addr->ip;
671 }
672 }
673 }
674 addrs->destroy(addrs);
675 }
676 }
677 return ip ? ip->clone(ip) : NULL;
678 }
679
680 /**
681 * callback function that raises the delayed roam event
682 */
683 static job_requeue_t roam_event(uintptr_t address)
684 {
685 hydra->kernel_interface->roam(hydra->kernel_interface, address != 0);
686 return JOB_REQUEUE_NONE;
687 }
688
689 /**
690 * fire a roaming event. we delay it for a bit and fire only one event
691 * for multiple calls. otherwise we would create too many events.
692 */
693 static void fire_roam_event(private_kernel_netlink_net_t *this, bool address)
694 {
695 timeval_t now;
696 job_t *job;
697
698 time_monotonic(&now);
699 this->roam_lock->lock(this->roam_lock);
700 if (!timercmp(&now, &this->next_roam, >))
701 {
702 this->roam_lock->unlock(this->roam_lock);
703 return;
704 }
705 timeval_add_ms(&now, ROAM_DELAY);
706 this->next_roam = now;
707 this->roam_lock->unlock(this->roam_lock);
708
709 job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
710 (void*)(uintptr_t)(address ? 1 : 0),
711 NULL, NULL);
712 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
713 }
714
715 /**
716 * check if an interface with a given index is up and usable
717 *
718 * this->lock must be locked when calling this function
719 */
720 static bool is_interface_up_and_usable(private_kernel_netlink_net_t *this,
721 int index)
722 {
723 iface_entry_t *iface;
724
725 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
726 (void**)&iface, &index) == SUCCESS)
727 {
728 return iface_entry_up_and_usable(iface);
729 }
730 return FALSE;
731 }
732
733 /**
734 * unregister the current addr_entry_t from the hashtable it is stored in
735 *
736 * this->lock must be locked when calling this function
737 */
738 static void addr_entry_unregister(addr_entry_t *addr, iface_entry_t *iface,
739 private_kernel_netlink_net_t *this)
740 {
741 if (addr->refcount)
742 {
743 addr_map_entry_remove(this->vips, addr, iface);
744 this->condvar->broadcast(this->condvar);
745 return;
746 }
747 addr_map_entry_remove(this->addrs, addr, iface);
748 }
749
750 /**
751 * process RTM_NEWLINK/RTM_DELLINK from kernel
752 */
753 static void process_link(private_kernel_netlink_net_t *this,
754 struct nlmsghdr *hdr, bool event)
755 {
756 struct ifinfomsg* msg = (struct ifinfomsg*)(NLMSG_DATA(hdr));
757 struct rtattr *rta = IFLA_RTA(msg);
758 size_t rtasize = IFLA_PAYLOAD (hdr);
759 enumerator_t *enumerator;
760 iface_entry_t *current, *entry = NULL;
761 char *name = NULL;
762 bool update = FALSE, update_routes = FALSE;
763
764 while (RTA_OK(rta, rtasize))
765 {
766 switch (rta->rta_type)
767 {
768 case IFLA_IFNAME:
769 name = RTA_DATA(rta);
770 break;
771 }
772 rta = RTA_NEXT(rta, rtasize);
773 }
774 if (!name)
775 {
776 name = "(unknown)";
777 }
778
779 this->lock->write_lock(this->lock);
780 switch (hdr->nlmsg_type)
781 {
782 case RTM_NEWLINK:
783 {
784 if (this->ifaces->find_first(this->ifaces,
785 (void*)iface_entry_by_index, (void**)&entry,
786 &msg->ifi_index) != SUCCESS)
787 {
788 INIT(entry,
789 .ifindex = msg->ifi_index,
790 .addrs = linked_list_create(),
791 .usable = hydra->kernel_interface->is_interface_usable(
792 hydra->kernel_interface, name),
793 );
794 this->ifaces->insert_last(this->ifaces, entry);
795 }
796 strncpy(entry->ifname, name, IFNAMSIZ);
797 entry->ifname[IFNAMSIZ-1] = '\0';
798 if (event && entry->usable)
799 {
800 if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
801 {
802 update = update_routes = TRUE;
803 DBG1(DBG_KNL, "interface %s activated", name);
804 }
805 if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
806 {
807 update = TRUE;
808 DBG1(DBG_KNL, "interface %s deactivated", name);
809 }
810 }
811 entry->flags = msg->ifi_flags;
812 break;
813 }
814 case RTM_DELLINK:
815 {
816 enumerator = this->ifaces->create_enumerator(this->ifaces);
817 while (enumerator->enumerate(enumerator, &current))
818 {
819 if (current->ifindex == msg->ifi_index)
820 {
821 if (event && current->usable)
822 {
823 update = TRUE;
824 DBG1(DBG_KNL, "interface %s deleted", current->ifname);
825 }
826 /* TODO: move virtual IPs installed on this interface to
827 * another interface? */
828 this->ifaces->remove_at(this->ifaces, enumerator);
829 current->addrs->invoke_function(current->addrs,
830 (void*)addr_entry_unregister, current, this);
831 iface_entry_destroy(current);
832 break;
833 }
834 }
835 enumerator->destroy(enumerator);
836 break;
837 }
838 }
839 this->lock->unlock(this->lock);
840
841 if (update_routes && event)
842 {
843 queue_route_reinstall(this, strdup(name));
844 }
845
846 if (update && event)
847 {
848 fire_roam_event(this, TRUE);
849 }
850 }
851
852 /**
853 * process RTM_NEWADDR/RTM_DELADDR from kernel
854 */
855 static void process_addr(private_kernel_netlink_net_t *this,
856 struct nlmsghdr *hdr, bool event)
857 {
858 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
859 struct rtattr *rta = IFA_RTA(msg);
860 size_t rtasize = IFA_PAYLOAD (hdr);
861 host_t *host = NULL;
862 iface_entry_t *iface;
863 chunk_t local = chunk_empty, address = chunk_empty;
864 char *route_ifname = NULL;
865 bool update = FALSE, found = FALSE, changed = FALSE;
866
867 while (RTA_OK(rta, rtasize))
868 {
869 switch (rta->rta_type)
870 {
871 case IFA_LOCAL:
872 local.ptr = RTA_DATA(rta);
873 local.len = RTA_PAYLOAD(rta);
874 break;
875 case IFA_ADDRESS:
876 address.ptr = RTA_DATA(rta);
877 address.len = RTA_PAYLOAD(rta);
878 break;
879 }
880 rta = RTA_NEXT(rta, rtasize);
881 }
882
883 /* For PPP interfaces, we need the IFA_LOCAL address,
884 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
885 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
886 if (local.ptr)
887 {
888 host = host_create_from_chunk(msg->ifa_family, local, 0);
889 }
890 else if (address.ptr)
891 {
892 host = host_create_from_chunk(msg->ifa_family, address, 0);
893 }
894
895 if (host == NULL)
896 { /* bad family? */
897 return;
898 }
899
900 this->lock->write_lock(this->lock);
901 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
902 (void**)&iface, &msg->ifa_index) == SUCCESS)
903 {
904 addr_map_entry_t *entry, lookup = {
905 .ip = host,
906 .iface = iface,
907 };
908 addr_entry_t *addr;
909
910 entry = this->vips->get(this->vips, &lookup);
911 if (entry)
912 {
913 if (hdr->nlmsg_type == RTM_NEWADDR)
914 { /* mark as installed and signal waiting threads */
915 entry->addr->installed = TRUE;
916 }
917 else
918 { /* the address was already marked as uninstalled */
919 addr = entry->addr;
920 iface->addrs->remove(iface->addrs, addr, NULL);
921 addr_map_entry_remove(this->vips, addr, iface);
922 addr_entry_destroy(addr);
923 }
924 /* no roam events etc. for virtual IPs */
925 this->condvar->broadcast(this->condvar);
926 this->lock->unlock(this->lock);
927 host->destroy(host);
928 return;
929 }
930 entry = this->addrs->get(this->addrs, &lookup);
931 if (entry)
932 {
933 if (hdr->nlmsg_type == RTM_DELADDR)
934 {
935 found = TRUE;
936 addr = entry->addr;
937 iface->addrs->remove(iface->addrs, addr, NULL);
938 if (iface->usable)
939 {
940 changed = TRUE;
941 DBG1(DBG_KNL, "%H disappeared from %s", host,
942 iface->ifname);
943 }
944 addr_map_entry_remove(this->addrs, addr, iface);
945 addr_entry_destroy(addr);
946 }
947 }
948 else
949 {
950 if (hdr->nlmsg_type == RTM_NEWADDR)
951 {
952 found = TRUE;
953 changed = TRUE;
954 route_ifname = strdup(iface->ifname);
955 INIT(addr,
956 .ip = host->clone(host),
957 .scope = msg->ifa_scope,
958 );
959 iface->addrs->insert_last(iface->addrs, addr);
960 addr_map_entry_add(this->addrs, addr, iface);
961 if (event && iface->usable)
962 {
963 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
964 }
965 }
966 }
967 if (found && (iface->flags & IFF_UP))
968 {
969 update = TRUE;
970 }
971 if (!iface->usable)
972 { /* ignore events for interfaces excluded by config */
973 update = changed = FALSE;
974 }
975 }
976 this->lock->unlock(this->lock);
977
978 if (update && event && route_ifname)
979 {
980 queue_route_reinstall(this, route_ifname);
981 }
982 else
983 {
984 free(route_ifname);
985 }
986 host->destroy(host);
987
988 /* send an update to all IKE_SAs */
989 if (update && event && changed)
990 {
991 fire_roam_event(this, TRUE);
992 }
993 }
994
995 /**
996 * process RTM_NEWROUTE and RTM_DELROUTE from kernel
997 */
998 static void process_route(private_kernel_netlink_net_t *this, struct nlmsghdr *hdr)
999 {
1000 struct rtmsg* msg = (struct rtmsg*)(NLMSG_DATA(hdr));
1001 struct rtattr *rta = RTM_RTA(msg);
1002 size_t rtasize = RTM_PAYLOAD(hdr);
1003 u_int32_t rta_oif = 0;
1004 host_t *host = NULL;
1005
1006 /* ignore routes added by us or in the local routing table (local addrs) */
1007 if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
1008 msg->rtm_table == RT_TABLE_LOCAL))
1009 {
1010 return;
1011 }
1012 else if (msg->rtm_flags & RTM_F_CLONED)
1013 { /* ignore cached routes, seem to be created a lot for IPv6 */
1014 return;
1015 }
1016
1017 while (RTA_OK(rta, rtasize))
1018 {
1019 switch (rta->rta_type)
1020 {
1021 case RTA_PREFSRC:
1022 DESTROY_IF(host);
1023 host = host_create_from_chunk(msg->rtm_family,
1024 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)), 0);
1025 break;
1026 case RTA_OIF:
1027 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
1028 {
1029 rta_oif = *(u_int32_t*)RTA_DATA(rta);
1030 }
1031 break;
1032 }
1033 rta = RTA_NEXT(rta, rtasize);
1034 }
1035 this->lock->read_lock(this->lock);
1036 if (rta_oif && !is_interface_up_and_usable(this, rta_oif))
1037 { /* ignore route changes for interfaces that are ignored or down */
1038 this->lock->unlock(this->lock);
1039 DESTROY_IF(host);
1040 return;
1041 }
1042 if (!host && rta_oif)
1043 {
1044 host = get_interface_address(this, rta_oif, msg->rtm_family, NULL);
1045 }
1046 if (!host || is_known_vip(this, host))
1047 { /* ignore routes added for virtual IPs */
1048 this->lock->unlock(this->lock);
1049 DESTROY_IF(host);
1050 return;
1051 }
1052 this->lock->unlock(this->lock);
1053 fire_roam_event(this, FALSE);
1054 host->destroy(host);
1055 }
1056
1057 /**
1058 * Receives events from kernel
1059 */
1060 static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
1061 {
1062 char response[1024];
1063 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
1064 struct sockaddr_nl addr;
1065 socklen_t addr_len = sizeof(addr);
1066 int len;
1067 bool oldstate;
1068
1069 oldstate = thread_cancelability(TRUE);
1070 len = recvfrom(this->socket_events, response, sizeof(response), 0,
1071 (struct sockaddr*)&addr, &addr_len);
1072 thread_cancelability(oldstate);
1073
1074 if (len < 0)
1075 {
1076 switch (errno)
1077 {
1078 case EINTR:
1079 /* interrupted, try again */
1080 return JOB_REQUEUE_DIRECT;
1081 case EAGAIN:
1082 /* no data ready, select again */
1083 return JOB_REQUEUE_DIRECT;
1084 default:
1085 DBG1(DBG_KNL, "unable to receive from rt event socket");
1086 sleep(1);
1087 return JOB_REQUEUE_FAIR;
1088 }
1089 }
1090
1091 if (addr.nl_pid != 0)
1092 { /* not from kernel. not interested, try another one */
1093 return JOB_REQUEUE_DIRECT;
1094 }
1095
1096 while (NLMSG_OK(hdr, len))
1097 {
1098 /* looks good so far, dispatch netlink message */
1099 switch (hdr->nlmsg_type)
1100 {
1101 case RTM_NEWADDR:
1102 case RTM_DELADDR:
1103 process_addr(this, hdr, TRUE);
1104 break;
1105 case RTM_NEWLINK:
1106 case RTM_DELLINK:
1107 process_link(this, hdr, TRUE);
1108 break;
1109 case RTM_NEWROUTE:
1110 case RTM_DELROUTE:
1111 if (this->process_route)
1112 {
1113 process_route(this, hdr);
1114 }
1115 break;
1116 default:
1117 break;
1118 }
1119 hdr = NLMSG_NEXT(hdr, len);
1120 }
1121 return JOB_REQUEUE_DIRECT;
1122 }
1123
1124 /** enumerator over addresses */
1125 typedef struct {
1126 private_kernel_netlink_net_t* this;
1127 /** which addresses to enumerate */
1128 kernel_address_type_t which;
1129 } address_enumerator_t;
1130
1131 /**
1132 * cleanup function for address enumerator
1133 */
1134 static void address_enumerator_destroy(address_enumerator_t *data)
1135 {
1136 data->this->lock->unlock(data->this->lock);
1137 free(data);
1138 }
1139
1140 /**
1141 * filter for addresses
1142 */
1143 static bool filter_addresses(address_enumerator_t *data,
1144 addr_entry_t** in, host_t** out)
1145 {
1146 if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->refcount)
1147 { /* skip virtual interfaces added by us */
1148 return FALSE;
1149 }
1150 if ((*in)->scope >= RT_SCOPE_LINK)
1151 { /* skip addresses with a unusable scope */
1152 return FALSE;
1153 }
1154 *out = (*in)->ip;
1155 return TRUE;
1156 }
1157
1158 /**
1159 * enumerator constructor for interfaces
1160 */
1161 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
1162 address_enumerator_t *data)
1163 {
1164 return enumerator_create_filter(
1165 iface->addrs->create_enumerator(iface->addrs),
1166 (void*)filter_addresses, data, NULL);
1167 }
1168
1169 /**
1170 * filter for interfaces
1171 */
1172 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
1173 iface_entry_t** out)
1174 {
1175 if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
1176 { /* skip interfaces excluded by config */
1177 return FALSE;
1178 }
1179 if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
1180 { /* ignore loopback devices */
1181 return FALSE;
1182 }
1183 if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
1184 { /* skip interfaces not up */
1185 return FALSE;
1186 }
1187 *out = *in;
1188 return TRUE;
1189 }
1190
1191 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
1192 private_kernel_netlink_net_t *this, kernel_address_type_t which)
1193 {
1194 address_enumerator_t *data = malloc_thing(address_enumerator_t);
1195 data->this = this;
1196 data->which = which;
1197
1198 this->lock->read_lock(this->lock);
1199 return enumerator_create_nested(
1200 enumerator_create_filter(
1201 this->ifaces->create_enumerator(this->ifaces),
1202 (void*)filter_interfaces, data, NULL),
1203 (void*)create_iface_enumerator, data,
1204 (void*)address_enumerator_destroy);
1205 }
1206
1207 METHOD(kernel_net_t, get_interface_name, bool,
1208 private_kernel_netlink_net_t *this, host_t* ip, char **name)
1209 {
1210 addr_map_entry_t *entry, lookup = {
1211 .ip = ip,
1212 };
1213
1214 if (ip->is_anyaddr(ip))
1215 {
1216 return FALSE;
1217 }
1218 this->lock->read_lock(this->lock);
1219 /* first try to find it on an up and usable interface */
1220 entry = this->addrs->get_match(this->addrs, &lookup,
1221 (void*)addr_map_entry_match_up_and_usable);
1222 if (entry)
1223 {
1224 if (name)
1225 {
1226 *name = strdup(entry->iface->ifname);
1227 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
1228 }
1229 this->lock->unlock(this->lock);
1230 return TRUE;
1231 }
1232 /* maybe it is installed on an ignored interface */
1233 entry = this->addrs->get_match(this->addrs, &lookup,
1234 (void*)addr_map_entry_match_up);
1235 if (!entry)
1236 {
1237 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
1238 }
1239 this->lock->unlock(this->lock);
1240 return FALSE;
1241 }
1242
1243 /**
1244 * get the index of an interface by name
1245 */
1246 static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
1247 {
1248 iface_entry_t *iface;
1249 int ifindex = 0;
1250
1251 DBG2(DBG_KNL, "getting iface index for %s", name);
1252
1253 this->lock->read_lock(this->lock);
1254 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_name,
1255 (void**)&iface, name) == SUCCESS)
1256 {
1257 ifindex = iface->ifindex;
1258 }
1259 this->lock->unlock(this->lock);
1260
1261 if (ifindex == 0)
1262 {
1263 DBG1(DBG_KNL, "unable to get interface index for %s", name);
1264 }
1265 return ifindex;
1266 }
1267
1268 /**
1269 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1270 */
1271 static bool addr_in_subnet(chunk_t addr, chunk_t net, int net_len)
1272 {
1273 static const u_char mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
1274 int byte = 0;
1275
1276 if (net_len == 0)
1277 { /* any address matches a /0 network */
1278 return TRUE;
1279 }
1280 if (addr.len != net.len || net_len > 8 * net.len )
1281 {
1282 return FALSE;
1283 }
1284 /* scan through all bytes in network order */
1285 while (net_len > 0)
1286 {
1287 if (net_len < 8)
1288 {
1289 return (mask[net_len] & addr.ptr[byte]) == (mask[net_len] & net.ptr[byte]);
1290 }
1291 else
1292 {
1293 if (addr.ptr[byte] != net.ptr[byte])
1294 {
1295 return FALSE;
1296 }
1297 byte++;
1298 net_len -= 8;
1299 }
1300 }
1301 return TRUE;
1302 }
1303
1304 /**
1305 * Store information about a route retrieved via RTNETLINK
1306 */
1307 typedef struct {
1308 chunk_t gtw;
1309 chunk_t src;
1310 chunk_t dst;
1311 host_t *src_host;
1312 u_int8_t dst_len;
1313 u_int32_t table;
1314 u_int32_t oif;
1315 } rt_entry_t;
1316
1317 /**
1318 * Free a route entry
1319 */
1320 static void rt_entry_destroy(rt_entry_t *this)
1321 {
1322 DESTROY_IF(this->src_host);
1323 free(this);
1324 }
1325
1326 /**
1327 * Parse route received with RTM_NEWROUTE. The given rt_entry_t object will be
1328 * reused if not NULL.
1329 *
1330 * Returned chunks point to internal data of the Netlink message.
1331 */
1332 static rt_entry_t *parse_route(struct nlmsghdr *hdr, rt_entry_t *route)
1333 {
1334 struct rtattr *rta;
1335 struct rtmsg *msg;
1336 size_t rtasize;
1337
1338 msg = (struct rtmsg*)(NLMSG_DATA(hdr));
1339 rta = RTM_RTA(msg);
1340 rtasize = RTM_PAYLOAD(hdr);
1341
1342 if (route)
1343 {
1344 route->gtw = chunk_empty;
1345 route->src = chunk_empty;
1346 route->dst = chunk_empty;
1347 route->dst_len = msg->rtm_dst_len;
1348 route->table = msg->rtm_table;
1349 route->oif = 0;
1350 }
1351 else
1352 {
1353 INIT(route,
1354 .dst_len = msg->rtm_dst_len,
1355 .table = msg->rtm_table,
1356 );
1357 }
1358
1359 while (RTA_OK(rta, rtasize))
1360 {
1361 switch (rta->rta_type)
1362 {
1363 case RTA_PREFSRC:
1364 route->src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1365 break;
1366 case RTA_GATEWAY:
1367 route->gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1368 break;
1369 case RTA_DST:
1370 route->dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1371 break;
1372 case RTA_OIF:
1373 if (RTA_PAYLOAD(rta) == sizeof(route->oif))
1374 {
1375 route->oif = *(u_int32_t*)RTA_DATA(rta);
1376 }
1377 break;
1378 #ifdef HAVE_RTA_TABLE
1379 case RTA_TABLE:
1380 if (RTA_PAYLOAD(rta) == sizeof(route->table))
1381 {
1382 route->table = *(u_int32_t*)RTA_DATA(rta);
1383 }
1384 break;
1385 #endif /* HAVE_RTA_TABLE*/
1386 }
1387 rta = RTA_NEXT(rta, rtasize);
1388 }
1389 return route;
1390 }
1391
1392 /**
1393 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1394 */
1395 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
1396 bool nexthop, host_t *candidate, u_int recursion)
1397 {
1398 netlink_buf_t request;
1399 struct nlmsghdr *hdr, *out, *current;
1400 struct rtmsg *msg;
1401 chunk_t chunk;
1402 size_t len;
1403 linked_list_t *routes;
1404 rt_entry_t *route = NULL, *best = NULL;
1405 enumerator_t *enumerator;
1406 host_t *addr = NULL;
1407
1408 if (recursion > MAX_ROUTE_RECURSION)
1409 {
1410 return NULL;
1411 }
1412
1413 memset(&request, 0, sizeof(request));
1414
1415 hdr = (struct nlmsghdr*)request;
1416 hdr->nlmsg_flags = NLM_F_REQUEST;
1417 if (dest->get_family(dest) == AF_INET || this->rta_prefsrc_for_ipv6 ||
1418 this->routing_table)
1419 { /* kernels prior to 3.0 do not support RTA_PREFSRC for IPv6 routes.
1420 * as we want to ignore routes with virtual IPs we cannot use DUMP
1421 * if these routes are not installed in a separate table */
1422 hdr->nlmsg_flags |= NLM_F_DUMP;
1423 }
1424 hdr->nlmsg_type = RTM_GETROUTE;
1425 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1426
1427 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1428 msg->rtm_family = dest->get_family(dest);
1429 if (candidate)
1430 {
1431 chunk = candidate->get_address(candidate);
1432 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1433 }
1434 chunk = dest->get_address(dest);
1435 netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1436
1437 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
1438 {
1439 DBG2(DBG_KNL, "getting %s to reach %H failed",
1440 nexthop ? "nexthop" : "address", dest);
1441 return NULL;
1442 }
1443 routes = linked_list_create();
1444 this->lock->read_lock(this->lock);
1445
1446 for (current = out; NLMSG_OK(current, len);
1447 current = NLMSG_NEXT(current, len))
1448 {
1449 switch (current->nlmsg_type)
1450 {
1451 case NLMSG_DONE:
1452 break;
1453 case RTM_NEWROUTE:
1454 {
1455 rt_entry_t *other;
1456 uintptr_t table;
1457
1458 route = parse_route(current, route);
1459
1460 table = (uintptr_t)route->table;
1461 if (this->rt_exclude->find_first(this->rt_exclude, NULL,
1462 (void**)&table) == SUCCESS)
1463 { /* route is from an excluded routing table */
1464 continue;
1465 }
1466 if (this->routing_table != 0 &&
1467 route->table == this->routing_table)
1468 { /* route is from our own ipsec routing table */
1469 continue;
1470 }
1471 if (route->oif && !is_interface_up_and_usable(this, route->oif))
1472 { /* interface is down */
1473 continue;
1474 }
1475 if (!addr_in_subnet(chunk, route->dst, route->dst_len))
1476 { /* route destination does not contain dest */
1477 continue;
1478 }
1479 if (route->src.ptr)
1480 { /* verify source address, if any */
1481 host_t *src = host_create_from_chunk(msg->rtm_family,
1482 route->src, 0);
1483 if (src && is_known_vip(this, src))
1484 { /* ignore routes installed by us */
1485 src->destroy(src);
1486 continue;
1487 }
1488 route->src_host = src;
1489 }
1490 /* insert route, sorted by decreasing network prefix */
1491 enumerator = routes->create_enumerator(routes);
1492 while (enumerator->enumerate(enumerator, &other))
1493 {
1494 if (route->dst_len > other->dst_len)
1495 {
1496 break;
1497 }
1498 }
1499 routes->insert_before(routes, enumerator, route);
1500 enumerator->destroy(enumerator);
1501 route = NULL;
1502 continue;
1503 }
1504 default:
1505 continue;
1506 }
1507 break;
1508 }
1509 if (route)
1510 {
1511 rt_entry_destroy(route);
1512 }
1513
1514 /* now we have a list of routes matching dest, sorted by net prefix.
1515 * we will look for source addresses for these routes and select the one
1516 * with the preferred source address, if possible */
1517 enumerator = routes->create_enumerator(routes);
1518 while (enumerator->enumerate(enumerator, &route))
1519 {
1520 if (route->src_host)
1521 { /* got a source address with the route, if no preferred source
1522 * is given or it matches we are done, as this is the best route */
1523 if (!candidate || candidate->ip_equals(candidate, route->src_host))
1524 {
1525 best = route;
1526 break;
1527 }
1528 else if (route->oif)
1529 { /* no match yet, maybe it is assigned to the same interface */
1530 host_t *src = get_interface_address(this, route->oif,
1531 msg->rtm_family, candidate);
1532 if (src && src->ip_equals(src, candidate))
1533 {
1534 route->src_host->destroy(route->src_host);
1535 route->src_host = src;
1536 best = route;
1537 break;
1538 }
1539 DESTROY_IF(src);
1540 }
1541 /* no luck yet with the source address. if this is the best (first)
1542 * route we store it as fallback in case we don't find a route with
1543 * the preferred source */
1544 best = best ?: route;
1545 continue;
1546 }
1547 if (route->oif)
1548 { /* no src, but an interface - get address from it */
1549 route->src_host = get_interface_address(this, route->oif,
1550 msg->rtm_family, candidate);
1551 if (route->src_host)
1552 { /* we handle this address the same as the one above */
1553 if (!candidate ||
1554 candidate->ip_equals(candidate, route->src_host))
1555 {
1556 best = route;
1557 break;
1558 }
1559 best = best ?: route;
1560 continue;
1561 }
1562 }
1563 if (route->gtw.ptr)
1564 { /* no src, no iface, but a gateway - lookup src to reach gtw */
1565 host_t *gtw;
1566
1567 gtw = host_create_from_chunk(msg->rtm_family, route->gtw, 0);
1568 if (gtw && !gtw->ip_equals(gtw, dest))
1569 {
1570 route->src_host = get_route(this, gtw, FALSE, candidate,
1571 recursion + 1);
1572 }
1573 DESTROY_IF(gtw);
1574 if (route->src_host)
1575 { /* more of the same */
1576 if (!candidate ||
1577 candidate->ip_equals(candidate, route->src_host))
1578 {
1579 best = route;
1580 break;
1581 }
1582 best = best ?: route;
1583 }
1584 }
1585 }
1586 enumerator->destroy(enumerator);
1587
1588 if (nexthop)
1589 { /* nexthop lookup, return gateway if any */
1590 if (best || routes->get_first(routes, (void**)&best) == SUCCESS)
1591 {
1592 addr = host_create_from_chunk(msg->rtm_family, best->gtw, 0);
1593 }
1594 addr = addr ?: dest->clone(dest);
1595 }
1596 else
1597 {
1598 if (best)
1599 {
1600 addr = best->src_host->clone(best->src_host);
1601 }
1602 }
1603 this->lock->unlock(this->lock);
1604 routes->destroy_function(routes, (void*)rt_entry_destroy);
1605 free(out);
1606
1607 if (addr)
1608 {
1609 DBG2(DBG_KNL, "using %H as %s to reach %H", addr,
1610 nexthop ? "nexthop" : "address", dest);
1611 }
1612 else if (!recursion)
1613 {
1614 DBG2(DBG_KNL, "no %s found to reach %H",
1615 nexthop ? "nexthop" : "address", dest);
1616 }
1617 return addr;
1618 }
1619
1620 METHOD(kernel_net_t, get_source_addr, host_t*,
1621 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1622 {
1623 return get_route(this, dest, FALSE, src, 0);
1624 }
1625
1626 METHOD(kernel_net_t, get_nexthop, host_t*,
1627 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1628 {
1629 return get_route(this, dest, TRUE, src, 0);
1630 }
1631
1632 /**
1633 * Manages the creation and deletion of ip addresses on an interface.
1634 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1635 */
1636 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
1637 int flags, int if_index, host_t *ip, int prefix)
1638 {
1639 netlink_buf_t request;
1640 struct nlmsghdr *hdr;
1641 struct ifaddrmsg *msg;
1642 chunk_t chunk;
1643
1644 memset(&request, 0, sizeof(request));
1645
1646 chunk = ip->get_address(ip);
1647
1648 hdr = (struct nlmsghdr*)request;
1649 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1650 hdr->nlmsg_type = nlmsg_type;
1651 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1652
1653 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1654 msg->ifa_family = ip->get_family(ip);
1655 msg->ifa_flags = 0;
1656 msg->ifa_prefixlen = prefix < 0 ? chunk.len * 8 : prefix;
1657 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1658 msg->ifa_index = if_index;
1659
1660 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1661
1662 return this->socket->send_ack(this->socket, hdr);
1663 }
1664
1665 METHOD(kernel_net_t, add_ip, status_t,
1666 private_kernel_netlink_net_t *this, host_t *virtual_ip, int prefix,
1667 char *iface_name)
1668 {
1669 addr_map_entry_t *entry, lookup = {
1670 .ip = virtual_ip,
1671 };
1672 iface_entry_t *iface = NULL;
1673
1674 if (!this->install_virtual_ip)
1675 { /* disabled by config */
1676 return SUCCESS;
1677 }
1678
1679 this->lock->write_lock(this->lock);
1680 /* the virtual IP might actually be installed as regular IP, in which case
1681 * we don't track it as virtual IP */
1682 entry = this->addrs->get_match(this->addrs, &lookup,
1683 (void*)addr_map_entry_match);
1684 if (!entry)
1685 { /* otherwise it might already be installed as virtual IP */
1686 entry = this->vips->get_match(this->vips, &lookup,
1687 (void*)addr_map_entry_match);
1688 if (entry)
1689 { /* the vip we found can be in one of three states: 1) installed and
1690 * ready, 2) just added by another thread, but not yet confirmed to
1691 * be installed by the kernel, 3) just deleted, but not yet gone.
1692 * Then while we wait below, several things could happen (as we
1693 * release the lock). For instance, the interface could disappear,
1694 * or the IP is finally deleted, and it reappears on a different
1695 * interface. All these cases are handled by the call below. */
1696 while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
1697 {
1698 this->condvar->wait(this->condvar, this->lock);
1699 }
1700 if (entry)
1701 {
1702 entry->addr->refcount++;
1703 }
1704 }
1705 }
1706 if (entry)
1707 {
1708 DBG2(DBG_KNL, "virtual IP %H is already installed on %s", virtual_ip,
1709 entry->iface->ifname);
1710 this->lock->unlock(this->lock);
1711 return SUCCESS;
1712 }
1713 /* try to find the target interface, either by config or via src ip */
1714 if (!this->install_virtual_ip_on ||
1715 this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_name,
1716 (void**)&iface, this->install_virtual_ip_on) != SUCCESS)
1717 {
1718 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_name,
1719 (void**)&iface, iface_name) != SUCCESS)
1720 { /* if we don't find the requested interface we just use the first */
1721 this->ifaces->get_first(this->ifaces, (void**)&iface);
1722 }
1723 }
1724 if (iface)
1725 {
1726 addr_entry_t *addr;
1727
1728 INIT(addr,
1729 .ip = virtual_ip->clone(virtual_ip),
1730 .refcount = 1,
1731 .scope = RT_SCOPE_UNIVERSE,
1732 );
1733 iface->addrs->insert_last(iface->addrs, addr);
1734 addr_map_entry_add(this->vips, addr, iface);
1735 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1736 iface->ifindex, virtual_ip, prefix) == SUCCESS)
1737 {
1738 while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
1739 { /* wait until address appears */
1740 this->condvar->wait(this->condvar, this->lock);
1741 }
1742 if (entry)
1743 { /* we fail if the interface got deleted in the meantime */
1744 DBG2(DBG_KNL, "virtual IP %H installed on %s", virtual_ip,
1745 entry->iface->ifname);
1746 this->lock->unlock(this->lock);
1747 return SUCCESS;
1748 }
1749 }
1750 this->lock->unlock(this->lock);
1751 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1752 return FAILED;
1753 }
1754 this->lock->unlock(this->lock);
1755 DBG1(DBG_KNL, "no interface available, unable to install virtual IP %H",
1756 virtual_ip);
1757 return FAILED;
1758 }
1759
1760 METHOD(kernel_net_t, del_ip, status_t,
1761 private_kernel_netlink_net_t *this, host_t *virtual_ip, int prefix,
1762 bool wait)
1763 {
1764 addr_map_entry_t *entry, lookup = {
1765 .ip = virtual_ip,
1766 };
1767
1768 if (!this->install_virtual_ip)
1769 { /* disabled by config */
1770 return SUCCESS;
1771 }
1772
1773 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1774
1775 this->lock->write_lock(this->lock);
1776 entry = this->vips->get_match(this->vips, &lookup,
1777 (void*)addr_map_entry_match);
1778 if (!entry)
1779 { /* we didn't install this IP as virtual IP */
1780 entry = this->addrs->get_match(this->addrs, &lookup,
1781 (void*)addr_map_entry_match);
1782 if (entry)
1783 {
1784 DBG2(DBG_KNL, "not deleting existing IP %H on %s", virtual_ip,
1785 entry->iface->ifname);
1786 this->lock->unlock(this->lock);
1787 return SUCCESS;
1788 }
1789 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1790 this->lock->unlock(this->lock);
1791 return FAILED;
1792 }
1793 if (entry->addr->refcount == 1)
1794 {
1795 status_t status;
1796
1797 /* we set this flag so that threads calling add_ip will block and wait
1798 * until the entry is gone, also so we can wait below */
1799 entry->addr->installed = FALSE;
1800 status = manage_ipaddr(this, RTM_DELADDR, 0, entry->iface->ifindex,
1801 virtual_ip, prefix);
1802 if (status == SUCCESS && wait)
1803 { /* wait until the address is really gone */
1804 while (is_known_vip(this, virtual_ip))
1805 {
1806 this->condvar->wait(this->condvar, this->lock);
1807 }
1808 }
1809 this->lock->unlock(this->lock);
1810 return status;
1811 }
1812 else
1813 {
1814 entry->addr->refcount--;
1815 }
1816 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1817 virtual_ip);
1818 this->lock->unlock(this->lock);
1819 return SUCCESS;
1820 }
1821
1822 /**
1823 * Manages source routes in the routing table.
1824 * By setting the appropriate nlmsg_type, the route gets added or removed.
1825 */
1826 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
1827 int nlmsg_type, int flags, chunk_t dst_net,
1828 u_int8_t prefixlen, host_t *gateway,
1829 host_t *src_ip, char *if_name)
1830 {
1831 netlink_buf_t request;
1832 struct nlmsghdr *hdr;
1833 struct rtmsg *msg;
1834 int ifindex;
1835 chunk_t chunk;
1836
1837 /* if route is 0.0.0.0/0, we can't install it, as it would
1838 * overwrite the default route. Instead, we add two routes:
1839 * 0.0.0.0/1 and 128.0.0.0/1 */
1840 if (this->routing_table == 0 && prefixlen == 0)
1841 {
1842 chunk_t half_net;
1843 u_int8_t half_prefixlen;
1844 status_t status;
1845
1846 half_net = chunk_alloca(dst_net.len);
1847 memset(half_net.ptr, 0, half_net.len);
1848 half_prefixlen = 1;
1849
1850 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1851 gateway, src_ip, if_name);
1852 half_net.ptr[0] |= 0x80;
1853 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1854 gateway, src_ip, if_name);
1855 return status;
1856 }
1857
1858 memset(&request, 0, sizeof(request));
1859
1860 hdr = (struct nlmsghdr*)request;
1861 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1862 hdr->nlmsg_type = nlmsg_type;
1863 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1864
1865 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1866 msg->rtm_family = src_ip->get_family(src_ip);
1867 msg->rtm_dst_len = prefixlen;
1868 msg->rtm_table = this->routing_table;
1869 msg->rtm_protocol = RTPROT_STATIC;
1870 msg->rtm_type = RTN_UNICAST;
1871 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1872
1873 netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
1874 chunk = src_ip->get_address(src_ip);
1875 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1876 if (gateway && gateway->get_family(gateway) == src_ip->get_family(src_ip))
1877 {
1878 chunk = gateway->get_address(gateway);
1879 netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1880 }
1881 ifindex = get_interface_index(this, if_name);
1882 chunk.ptr = (char*)&ifindex;
1883 chunk.len = sizeof(ifindex);
1884 netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1885
1886 return this->socket->send_ack(this->socket, hdr);
1887 }
1888
1889 METHOD(kernel_net_t, add_route, status_t,
1890 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1891 host_t *gateway, host_t *src_ip, char *if_name)
1892 {
1893 status_t status;
1894 route_entry_t *found, route = {
1895 .dst_net = dst_net,
1896 .prefixlen = prefixlen,
1897 .gateway = gateway,
1898 .src_ip = src_ip,
1899 .if_name = if_name,
1900 };
1901
1902 this->routes_lock->lock(this->routes_lock);
1903 found = this->routes->get(this->routes, &route);
1904 if (found)
1905 {
1906 this->routes_lock->unlock(this->routes_lock);
1907 return ALREADY_DONE;
1908 }
1909 found = route_entry_clone(&route);
1910 this->routes->put(this->routes, found, found);
1911 status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1912 dst_net, prefixlen, gateway, src_ip, if_name);
1913 this->routes_lock->unlock(this->routes_lock);
1914 return status;
1915 }
1916
1917 METHOD(kernel_net_t, del_route, status_t,
1918 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1919 host_t *gateway, host_t *src_ip, char *if_name)
1920 {
1921 status_t status;
1922 route_entry_t *found, route = {
1923 .dst_net = dst_net,
1924 .prefixlen = prefixlen,
1925 .gateway = gateway,
1926 .src_ip = src_ip,
1927 .if_name = if_name,
1928 };
1929
1930 this->routes_lock->lock(this->routes_lock);
1931 found = this->routes->get(this->routes, &route);
1932 if (!found)
1933 {
1934 this->routes_lock->unlock(this->routes_lock);
1935 return NOT_FOUND;
1936 }
1937 this->routes->remove(this->routes, found);
1938 route_entry_destroy(found);
1939 status = manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
1940 gateway, src_ip, if_name);
1941 this->routes_lock->unlock(this->routes_lock);
1942 return status;
1943 }
1944
1945 /**
1946 * Initialize a list of local addresses.
1947 */
1948 static status_t init_address_list(private_kernel_netlink_net_t *this)
1949 {
1950 netlink_buf_t request;
1951 struct nlmsghdr *out, *current, *in;
1952 struct rtgenmsg *msg;
1953 size_t len;
1954 enumerator_t *ifaces, *addrs;
1955 iface_entry_t *iface;
1956 addr_entry_t *addr;
1957
1958 DBG2(DBG_KNL, "known interfaces and IP addresses:");
1959
1960 memset(&request, 0, sizeof(request));
1961
1962 in = (struct nlmsghdr*)&request;
1963 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1964 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1965 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1966 msg->rtgen_family = AF_UNSPEC;
1967
1968 /* get all links */
1969 in->nlmsg_type = RTM_GETLINK;
1970 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1971 {
1972 return FAILED;
1973 }
1974 current = out;
1975 while (NLMSG_OK(current, len))
1976 {
1977 switch (current->nlmsg_type)
1978 {
1979 case NLMSG_DONE:
1980 break;
1981 case RTM_NEWLINK:
1982 process_link(this, current, FALSE);
1983 /* fall through */
1984 default:
1985 current = NLMSG_NEXT(current, len);
1986 continue;
1987 }
1988 break;
1989 }
1990 free(out);
1991
1992 /* get all interface addresses */
1993 in->nlmsg_type = RTM_GETADDR;
1994 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1995 {
1996 return FAILED;
1997 }
1998 current = out;
1999 while (NLMSG_OK(current, len))
2000 {
2001 switch (current->nlmsg_type)
2002 {
2003 case NLMSG_DONE:
2004 break;
2005 case RTM_NEWADDR:
2006 process_addr(this, current, FALSE);
2007 /* fall through */
2008 default:
2009 current = NLMSG_NEXT(current, len);
2010 continue;
2011 }
2012 break;
2013 }
2014 free(out);
2015
2016 this->lock->read_lock(this->lock);
2017 ifaces = this->ifaces->create_enumerator(this->ifaces);
2018 while (ifaces->enumerate(ifaces, &iface))
2019 {
2020 if (iface_entry_up_and_usable(iface))
2021 {
2022 DBG2(DBG_KNL, " %s", iface->ifname);
2023 addrs = iface->addrs->create_enumerator(iface->addrs);
2024 while (addrs->enumerate(addrs, (void**)&addr))
2025 {
2026 DBG2(DBG_KNL, " %H", addr->ip);
2027 }
2028 addrs->destroy(addrs);
2029 }
2030 }
2031 ifaces->destroy(ifaces);
2032 this->lock->unlock(this->lock);
2033 return SUCCESS;
2034 }
2035
2036 /**
2037 * create or delete a rule to use our routing table
2038 */
2039 static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
2040 int family, u_int32_t table, u_int32_t prio)
2041 {
2042 netlink_buf_t request;
2043 struct nlmsghdr *hdr;
2044 struct rtmsg *msg;
2045 chunk_t chunk;
2046
2047 memset(&request, 0, sizeof(request));
2048 hdr = (struct nlmsghdr*)request;
2049 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2050 hdr->nlmsg_type = nlmsg_type;
2051 if (nlmsg_type == RTM_NEWRULE)
2052 {
2053 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
2054 }
2055 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2056
2057 msg = (struct rtmsg*)NLMSG_DATA(hdr);
2058 msg->rtm_table = table;
2059 msg->rtm_family = family;
2060 msg->rtm_protocol = RTPROT_BOOT;
2061 msg->rtm_scope = RT_SCOPE_UNIVERSE;
2062 msg->rtm_type = RTN_UNICAST;
2063
2064 chunk = chunk_from_thing(prio);
2065 netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
2066
2067 return this->socket->send_ack(this->socket, hdr);
2068 }
2069
2070 /**
2071 * check for kernel features (currently only via version number)
2072 */
2073 static void check_kernel_features(private_kernel_netlink_net_t *this)
2074 {
2075 struct utsname utsname;
2076 int a, b, c;
2077
2078 if (uname(&utsname) == 0)
2079 {
2080 switch(sscanf(utsname.release, "%d.%d.%d", &a, &b, &c))
2081 {
2082 case 3:
2083 if (a == 2)
2084 {
2085 DBG2(DBG_KNL, "detected Linux %d.%d.%d, no support for "
2086 "RTA_PREFSRC for IPv6 routes", a, b, c);
2087 break;
2088 }
2089 /* fall-through */
2090 case 2:
2091 /* only 3.x+ uses two part version numbers */
2092 this->rta_prefsrc_for_ipv6 = TRUE;
2093 break;
2094 default:
2095 break;
2096 }
2097 }
2098 }
2099
2100 /**
2101 * Destroy an address to iface map
2102 */
2103 static void addr_map_destroy(hashtable_t *map)
2104 {
2105 enumerator_t *enumerator;
2106 addr_map_entry_t *addr;
2107
2108 enumerator = map->create_enumerator(map);
2109 while (enumerator->enumerate(enumerator, NULL, (void**)&addr))
2110 {
2111 free(addr);
2112 }
2113 enumerator->destroy(enumerator);
2114 map->destroy(map);
2115 }
2116
2117 METHOD(kernel_net_t, destroy, void,
2118 private_kernel_netlink_net_t *this)
2119 {
2120 enumerator_t *enumerator;
2121 route_entry_t *route;
2122
2123 if (this->routing_table)
2124 {
2125 manage_rule(this, RTM_DELRULE, AF_INET, this->routing_table,
2126 this->routing_table_prio);
2127 manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
2128 this->routing_table_prio);
2129 }
2130 if (this->socket_events > 0)
2131 {
2132 close(this->socket_events);
2133 }
2134 enumerator = this->routes->create_enumerator(this->routes);
2135 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
2136 {
2137 manage_srcroute(this, RTM_DELROUTE, 0, route->dst_net, route->prefixlen,
2138 route->gateway, route->src_ip, route->if_name);
2139 route_entry_destroy(route);
2140 }
2141 enumerator->destroy(enumerator);
2142 this->routes->destroy(this->routes);
2143 this->routes_lock->destroy(this->routes_lock);
2144 DESTROY_IF(this->socket);
2145
2146 net_changes_clear(this);
2147 this->net_changes->destroy(this->net_changes);
2148 this->net_changes_lock->destroy(this->net_changes_lock);
2149
2150 addr_map_destroy(this->addrs);
2151 addr_map_destroy(this->vips);
2152
2153 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
2154 this->rt_exclude->destroy(this->rt_exclude);
2155 this->roam_lock->destroy(this->roam_lock);
2156 this->condvar->destroy(this->condvar);
2157 this->lock->destroy(this->lock);
2158 free(this);
2159 }
2160
2161 /*
2162 * Described in header.
2163 */
2164 kernel_netlink_net_t *kernel_netlink_net_create()
2165 {
2166 private_kernel_netlink_net_t *this;
2167 enumerator_t *enumerator;
2168 bool register_for_events = TRUE;
2169 char *exclude;
2170
2171 INIT(this,
2172 .public = {
2173 .interface = {
2174 .get_interface = _get_interface_name,
2175 .create_address_enumerator = _create_address_enumerator,
2176 .get_source_addr = _get_source_addr,
2177 .get_nexthop = _get_nexthop,
2178 .add_ip = _add_ip,
2179 .del_ip = _del_ip,
2180 .add_route = _add_route,
2181 .del_route = _del_route,
2182 .destroy = _destroy,
2183 },
2184 },
2185 .socket = netlink_socket_create(NETLINK_ROUTE),
2186 .rt_exclude = linked_list_create(),
2187 .routes = hashtable_create((hashtable_hash_t)route_entry_hash,
2188 (hashtable_equals_t)route_entry_equals, 16),
2189 .net_changes = hashtable_create(
2190 (hashtable_hash_t)net_change_hash,
2191 (hashtable_equals_t)net_change_equals, 16),
2192 .addrs = hashtable_create(
2193 (hashtable_hash_t)addr_map_entry_hash,
2194 (hashtable_equals_t)addr_map_entry_equals, 16),
2195 .vips = hashtable_create((hashtable_hash_t)addr_map_entry_hash,
2196 (hashtable_equals_t)addr_map_entry_equals, 16),
2197 .routes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
2198 .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
2199 .ifaces = linked_list_create(),
2200 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
2201 .condvar = rwlock_condvar_create(),
2202 .roam_lock = spinlock_create(),
2203 .routing_table = lib->settings->get_int(lib->settings,
2204 "%s.routing_table", ROUTING_TABLE, hydra->daemon),
2205 .routing_table_prio = lib->settings->get_int(lib->settings,
2206 "%s.routing_table_prio", ROUTING_TABLE_PRIO, hydra->daemon),
2207 .process_route = lib->settings->get_bool(lib->settings,
2208 "%s.process_route", TRUE, hydra->daemon),
2209 .install_virtual_ip = lib->settings->get_bool(lib->settings,
2210 "%s.install_virtual_ip", TRUE, hydra->daemon),
2211 .install_virtual_ip_on = lib->settings->get_str(lib->settings,
2212 "%s.install_virtual_ip_on", NULL, hydra->daemon),
2213 );
2214 timerclear(&this->last_route_reinstall);
2215 timerclear(&this->next_roam);
2216
2217 check_kernel_features(this);
2218
2219 if (streq(hydra->daemon, "starter"))
2220 { /* starter has no threads, so we do not register for kernel events */
2221 register_for_events = FALSE;
2222 }
2223
2224 exclude = lib->settings->get_str(lib->settings,
2225 "%s.ignore_routing_tables", NULL, hydra->daemon);
2226 if (exclude)
2227 {
2228 char *token;
2229 uintptr_t table;
2230
2231 enumerator = enumerator_create_token(exclude, " ", " ");
2232 while (enumerator->enumerate(enumerator, &token))
2233 {
2234 errno = 0;
2235 table = strtoul(token, NULL, 10);
2236
2237 if (errno == 0)
2238 {
2239 this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
2240 }
2241 }
2242 enumerator->destroy(enumerator);
2243 }
2244
2245 if (register_for_events)
2246 {
2247 struct sockaddr_nl addr;
2248
2249 memset(&addr, 0, sizeof(addr));
2250 addr.nl_family = AF_NETLINK;
2251
2252 /* create and bind RT socket for events (address/interface/route changes) */
2253 this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2254 if (this->socket_events < 0)
2255 {
2256 DBG1(DBG_KNL, "unable to create RT event socket");
2257 destroy(this);
2258 return NULL;
2259 }
2260 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
2261 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_LINK;
2262 if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
2263 {
2264 DBG1(DBG_KNL, "unable to bind RT event socket");
2265 destroy(this);
2266 return NULL;
2267 }
2268
2269 lib->processor->queue_job(lib->processor,
2270 (job_t*)callback_job_create_with_prio(
2271 (callback_job_cb_t)receive_events, this, NULL,
2272 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
2273 }
2274
2275 if (init_address_list(this) != SUCCESS)
2276 {
2277 DBG1(DBG_KNL, "unable to get interface list");
2278 destroy(this);
2279 return NULL;
2280 }
2281
2282 if (this->routing_table)
2283 {
2284 if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
2285 this->routing_table_prio) != SUCCESS)
2286 {
2287 DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
2288 }
2289 if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
2290 this->routing_table_prio) != SUCCESS)
2291 {
2292 DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
2293 }
2294 }
2295
2296 return &this->public;
2297 }