Use a lock to safely check and update the time for the next roam event
[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 <debug.h>
53 #include <threading/thread.h>
54 #include <threading/condvar.h>
55 #include <threading/mutex.h>
56 #include <threading/spinlock.h>
57 #include <utils/hashtable.h>
58 #include <utils/linked_list.h>
59 #include <processing/jobs/callback_job.h>
60
61 /** delay before firing roam events (ms) */
62 #define ROAM_DELAY 100
63
64 /** delay before reinstalling routes (ms) */
65 #define ROUTE_DELAY 100
66
67 typedef struct addr_entry_t addr_entry_t;
68
69 /**
70 * IP address in an iface_entry_t
71 */
72 struct addr_entry_t {
73
74 /** the ip address */
75 host_t *ip;
76
77 /** scope of the address */
78 u_char scope;
79
80 /** number of times this IP is used, if virtual (i.e. managed by us) */
81 u_int refcount;
82
83 /** TRUE once it is installed, if virtual */
84 bool installed;
85 };
86
87 /**
88 * destroy a addr_entry_t object
89 */
90 static void addr_entry_destroy(addr_entry_t *this)
91 {
92 this->ip->destroy(this->ip);
93 free(this);
94 }
95
96 typedef struct iface_entry_t iface_entry_t;
97
98 /**
99 * A network interface on this system, containing addr_entry_t's
100 */
101 struct iface_entry_t {
102
103 /** interface index */
104 int ifindex;
105
106 /** name of the interface */
107 char ifname[IFNAMSIZ];
108
109 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
110 u_int flags;
111
112 /** list of addresses as host_t */
113 linked_list_t *addrs;
114
115 /** TRUE if usable by config */
116 bool usable;
117 };
118
119 /**
120 * destroy an interface entry
121 */
122 static void iface_entry_destroy(iface_entry_t *this)
123 {
124 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
125 free(this);
126 }
127
128 /**
129 * find an interface entry by index
130 */
131 static bool iface_entry_by_index(iface_entry_t *this, int *ifindex)
132 {
133 return this->ifindex == *ifindex;
134 }
135
136 /**
137 * find an interface entry by name
138 */
139 static bool iface_entry_by_name(iface_entry_t *this, char *ifname)
140 {
141 return streq(this->ifname, ifname);
142 }
143
144 /**
145 * check if an interface is up
146 */
147 static inline bool iface_entry_up(iface_entry_t *iface)
148 {
149 return (iface->flags & IFF_UP) == IFF_UP;
150 }
151
152 /**
153 * check if an interface is up and usable
154 */
155 static inline bool iface_entry_up_and_usable(iface_entry_t *iface)
156 {
157 return iface->usable && iface_entry_up(iface);
158 }
159
160 typedef struct addr_map_entry_t addr_map_entry_t;
161
162 /**
163 * Entry that maps an IP address to an interface entry
164 */
165 struct addr_map_entry_t {
166 /** The IP address */
167 host_t *ip;
168
169 /** The address entry for this IP address */
170 addr_entry_t *addr;
171
172 /** The interface this address is installed on */
173 iface_entry_t *iface;
174 };
175
176 /**
177 * Hash a addr_map_entry_t object, all entries with the same IP address
178 * are stored in the same bucket
179 */
180 static u_int addr_map_entry_hash(addr_map_entry_t *this)
181 {
182 return chunk_hash(this->ip->get_address(this->ip));
183 }
184
185 /**
186 * Compare two addr_map_entry_t objects, two entries are equal if they are
187 * installed on the same interface
188 */
189 static bool addr_map_entry_equals(addr_map_entry_t *a, addr_map_entry_t *b)
190 {
191 return a->iface->ifindex == b->iface->ifindex &&
192 a->ip->ip_equals(a->ip, b->ip);
193 }
194
195 /**
196 * Used with get_match this finds an address entry if it is installed on
197 * an up and usable interface
198 */
199 static bool addr_map_entry_match_up_and_usable(addr_map_entry_t *a,
200 addr_map_entry_t *b)
201 {
202 return iface_entry_up_and_usable(b->iface) &&
203 a->ip->ip_equals(a->ip, b->ip);
204 }
205
206 /**
207 * Used with get_match this finds an address entry if it is installed on
208 * any active local interface
209 */
210 static bool addr_map_entry_match_up(addr_map_entry_t *a, addr_map_entry_t *b)
211 {
212 return iface_entry_up(b->iface) && a->ip->ip_equals(a->ip, b->ip);
213 }
214
215 /**
216 * Used with get_match this finds an address entry if it is installed on
217 * any local interface
218 */
219 static bool addr_map_entry_match(addr_map_entry_t *a, addr_map_entry_t *b)
220 {
221 return a->ip->ip_equals(a->ip, b->ip);
222 }
223
224 typedef struct route_entry_t route_entry_t;
225
226 /**
227 * Installed routing entry
228 */
229 struct route_entry_t {
230 /** Name of the interface the route is bound to */
231 char *if_name;
232
233 /** Source ip of the route */
234 host_t *src_ip;
235
236 /** Gateway for this route */
237 host_t *gateway;
238
239 /** Destination net */
240 chunk_t dst_net;
241
242 /** Destination net prefixlen */
243 u_int8_t prefixlen;
244 };
245
246 /**
247 * Clone a route_entry_t object.
248 */
249 static route_entry_t *route_entry_clone(route_entry_t *this)
250 {
251 route_entry_t *route;
252
253 INIT(route,
254 .if_name = strdup(this->if_name),
255 .src_ip = this->src_ip->clone(this->src_ip),
256 .gateway = this->gateway->clone(this->gateway),
257 .dst_net = chunk_clone(this->dst_net),
258 .prefixlen = this->prefixlen,
259 );
260 return route;
261 }
262
263 /**
264 * Destroy a route_entry_t object
265 */
266 static void route_entry_destroy(route_entry_t *this)
267 {
268 free(this->if_name);
269 DESTROY_IF(this->src_ip);
270 DESTROY_IF(this->gateway);
271 chunk_free(&this->dst_net);
272 free(this);
273 }
274
275 /**
276 * Hash a route_entry_t object
277 */
278 static u_int route_entry_hash(route_entry_t *this)
279 {
280 return chunk_hash_inc(chunk_from_thing(this->prefixlen),
281 chunk_hash(this->dst_net));
282 }
283
284 /**
285 * Compare two route_entry_t objects
286 */
287 static bool route_entry_equals(route_entry_t *a, route_entry_t *b)
288 {
289 return a->if_name && b->if_name && streq(a->if_name, b->if_name) &&
290 a->src_ip->ip_equals(a->src_ip, b->src_ip) &&
291 a->gateway->ip_equals(a->gateway, b->gateway) &&
292 chunk_equals(a->dst_net, b->dst_net) && a->prefixlen == b->prefixlen;
293 }
294
295 typedef struct net_change_t net_change_t;
296
297 /**
298 * Queued network changes
299 */
300 struct net_change_t {
301 /** Name of the interface that got activated (or an IP appeared on) */
302 char *if_name;
303 };
304
305 /**
306 * Destroy a net_change_t object
307 */
308 static void net_change_destroy(net_change_t *this)
309 {
310 free(this->if_name);
311 free(this);
312 }
313
314 /**
315 * Hash a net_change_t object
316 */
317 static u_int net_change_hash(net_change_t *this)
318 {
319 return chunk_hash(chunk_create(this->if_name, strlen(this->if_name)));
320 }
321
322 /**
323 * Compare two net_change_t objects
324 */
325 static bool net_change_equals(net_change_t *a, net_change_t *b)
326 {
327 return streq(a->if_name, b->if_name);
328 }
329
330 typedef struct private_kernel_netlink_net_t private_kernel_netlink_net_t;
331
332 /**
333 * Private variables and functions of kernel_netlink_net class.
334 */
335 struct private_kernel_netlink_net_t {
336 /**
337 * Public part of the kernel_netlink_net_t object.
338 */
339 kernel_netlink_net_t public;
340
341 /**
342 * mutex to lock access to various lists
343 */
344 mutex_t *mutex;
345
346 /**
347 * condition variable to signal virtual IP add/removal
348 */
349 condvar_t *condvar;
350
351 /**
352 * Cached list of interfaces and its addresses (iface_entry_t)
353 */
354 linked_list_t *ifaces;
355
356 /**
357 * Map for IP addresses to iface_entry_t objects (addr_map_entry_t)
358 */
359 hashtable_t *addrs;
360
361 /**
362 * Map for virtual IP addresses to iface_entry_t objects (addr_map_entry_t)
363 */
364 hashtable_t *vips;
365
366 /**
367 * netlink rt socket (routing)
368 */
369 netlink_socket_t *socket;
370
371 /**
372 * Netlink rt socket to receive address change events
373 */
374 int socket_events;
375
376 /**
377 * earliest time of the next roam event
378 */
379 timeval_t next_roam;
380
381 /**
382 * lock to check and update roam event time
383 */
384 spinlock_t *roam_lock;
385
386 /**
387 * routing table to install routes
388 */
389 int routing_table;
390
391 /**
392 * priority of used routing table
393 */
394 int routing_table_prio;
395
396 /**
397 * installed routes
398 */
399 hashtable_t *routes;
400
401 /**
402 * interface changes which may trigger route reinstallation
403 */
404 hashtable_t *net_changes;
405
406 /**
407 * mutex for route reinstallation triggers
408 */
409 mutex_t *net_changes_lock;
410
411 /**
412 * time of last route reinstallation
413 */
414 timeval_t last_route_reinstall;
415
416 /**
417 * whether to react to RTM_NEWROUTE or RTM_DELROUTE events
418 */
419 bool process_route;
420
421 /**
422 * whether to actually install virtual IPs
423 */
424 bool install_virtual_ip;
425
426 /**
427 * the name of the interface virtual IP addresses are installed on
428 */
429 char *install_virtual_ip_on;
430
431 /**
432 * whether preferred source addresses can be specified for IPv6 routes
433 */
434 bool rta_prefsrc_for_ipv6;
435
436 /**
437 * list with routing tables to be excluded from route lookup
438 */
439 linked_list_t *rt_exclude;
440 };
441
442 /**
443 * Forward declaration
444 */
445 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
446 int nlmsg_type, int flags, chunk_t dst_net,
447 u_int8_t prefixlen, host_t *gateway,
448 host_t *src_ip, char *if_name);
449
450 /**
451 * Clear the queued network changes.
452 */
453 static void net_changes_clear(private_kernel_netlink_net_t *this)
454 {
455 enumerator_t *enumerator;
456 net_change_t *change;
457
458 enumerator = this->net_changes->create_enumerator(this->net_changes);
459 while (enumerator->enumerate(enumerator, NULL, (void**)&change))
460 {
461 this->net_changes->remove_at(this->net_changes, enumerator);
462 net_change_destroy(change);
463 }
464 enumerator->destroy(enumerator);
465 }
466
467 /**
468 * Act upon queued network changes.
469 */
470 static job_requeue_t reinstall_routes(private_kernel_netlink_net_t *this)
471 {
472 enumerator_t *enumerator;
473 route_entry_t *route;
474
475 this->net_changes_lock->lock(this->net_changes_lock);
476 this->mutex->lock(this->mutex);
477
478 enumerator = this->routes->create_enumerator(this->routes);
479 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
480 {
481 net_change_t *change, lookup = {
482 .if_name = route->if_name,
483 };
484 /* check if a change for the outgoing interface is queued */
485 change = this->net_changes->get(this->net_changes, &lookup);
486 if (!change)
487 { /* in case src_ip is not on the outgoing interface */
488 if (this->public.interface.get_interface(&this->public.interface,
489 route->src_ip, &lookup.if_name))
490 {
491 if (!streq(lookup.if_name, route->if_name))
492 {
493 change = this->net_changes->get(this->net_changes, &lookup);
494 }
495 free(lookup.if_name);
496 }
497 }
498 if (change)
499 {
500 manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
501 route->dst_net, route->prefixlen, route->gateway,
502 route->src_ip, route->if_name);
503 }
504 }
505 enumerator->destroy(enumerator);
506 this->mutex->unlock(this->mutex);
507
508 net_changes_clear(this);
509 this->net_changes_lock->unlock(this->net_changes_lock);
510 return JOB_REQUEUE_NONE;
511 }
512
513 /**
514 * Queue route reinstallation caused by network changes for a given interface.
515 *
516 * The route reinstallation is delayed for a while and only done once for
517 * several calls during this delay, in order to avoid doing it too often.
518 * The interface name is freed.
519 */
520 static void queue_route_reinstall(private_kernel_netlink_net_t *this,
521 char *if_name)
522 {
523 net_change_t *update, *found;
524 timeval_t now;
525 job_t *job;
526
527 INIT(update,
528 .if_name = if_name
529 );
530
531 this->net_changes_lock->lock(this->net_changes_lock);
532 found = this->net_changes->put(this->net_changes, update, update);
533 if (found)
534 {
535 net_change_destroy(found);
536 }
537 time_monotonic(&now);
538 if (timercmp(&now, &this->last_route_reinstall, >))
539 {
540 now.tv_usec += ROUTE_DELAY * 1000;
541 while (now.tv_usec > 1000000)
542 {
543 now.tv_sec++;
544 now.tv_usec -= 1000000;
545 }
546 this->last_route_reinstall = now;
547
548 job = (job_t*)callback_job_create((callback_job_cb_t)reinstall_routes,
549 this, NULL, NULL);
550 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROUTE_DELAY);
551 }
552 this->net_changes_lock->unlock(this->net_changes_lock);
553 }
554
555 /**
556 * check if the given IP is known as virtual IP and currently installed
557 *
558 * this function will also return TRUE if the virtual IP entry disappeared.
559 * in that case the returned entry will be NULL.
560 *
561 * this->mutex must be locked when calling this function
562 */
563 static bool is_vip_installed_or_gone(private_kernel_netlink_net_t *this,
564 host_t *ip, addr_map_entry_t **entry)
565 {
566 addr_map_entry_t lookup = {
567 .ip = ip,
568 };
569
570 *entry = this->vips->get_match(this->vips, &lookup,
571 (void*)addr_map_entry_match);
572 if (*entry == NULL)
573 { /* the virtual IP disappeared */
574 return TRUE;
575 }
576 return (*entry)->addr->installed;
577 }
578
579 /**
580 * check if the given IP is known as virtual IP
581 *
582 * this->mutex must be locked when calling this function
583 */
584 static bool is_known_vip(private_kernel_netlink_net_t *this, host_t *ip)
585 {
586 addr_map_entry_t lookup = {
587 .ip = ip,
588 };
589
590 return this->vips->get_match(this->vips, &lookup,
591 (void*)addr_map_entry_match) != NULL;
592 }
593
594 /**
595 * Add an address map entry
596 */
597 static void addr_map_entry_add(hashtable_t *map, addr_entry_t *addr,
598 iface_entry_t *iface)
599 {
600 addr_map_entry_t *entry;
601
602 INIT(entry,
603 .ip = addr->ip,
604 .addr = addr,
605 .iface = iface,
606 );
607 entry = map->put(map, entry, entry);
608 free(entry);
609 }
610
611 /**
612 * Remove an address map entry
613 */
614 static void addr_map_entry_remove(hashtable_t *map, addr_entry_t *addr,
615 iface_entry_t *iface)
616 {
617 addr_map_entry_t *entry, lookup = {
618 .ip = addr->ip,
619 .addr = addr,
620 .iface = iface,
621 };
622
623 entry = map->remove(map, &lookup);
624 free(entry);
625 }
626
627 /**
628 * get the first non-virtual ip address on the given interface.
629 * if a candidate address is given, we first search for that address and if not
630 * found return the address as above.
631 * returned host is a clone, has to be freed by caller.
632 */
633 static host_t *get_interface_address(private_kernel_netlink_net_t *this,
634 int ifindex, int family, host_t *candidate)
635 {
636 iface_entry_t *iface;
637 enumerator_t *addrs;
638 addr_entry_t *addr;
639 host_t *ip = NULL;
640
641 this->mutex->lock(this->mutex);
642 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
643 (void**)&iface, &ifindex) == SUCCESS)
644 {
645 if (iface->usable)
646 { /* only use interfaces not excluded by config */
647 addrs = iface->addrs->create_enumerator(iface->addrs);
648 while (addrs->enumerate(addrs, &addr))
649 {
650 if (addr->refcount)
651 { /* ignore virtual IP addresses */
652 continue;
653 }
654 if (addr->ip->get_family(addr->ip) == family)
655 {
656 if (!candidate || candidate->ip_equals(candidate, addr->ip))
657 { /* stop at the first address if we don't search for a
658 * candidate or if the candidate matches */
659 ip = addr->ip;
660 break;
661 }
662 else if (!ip)
663 { /* store the first address as fallback if candidate is
664 * not found */
665 ip = addr->ip;
666 }
667 }
668 }
669 addrs->destroy(addrs);
670 }
671 }
672 if (ip)
673 {
674 ip = ip->clone(ip);
675 }
676 this->mutex->unlock(this->mutex);
677 return ip;
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 now.tv_usec += ROAM_DELAY * 1000;
706 while (now.tv_usec > 1000000)
707 {
708 now.tv_sec++;
709 now.tv_usec -= 1000000;
710 }
711 this->next_roam = now;
712 this->roam_lock->unlock(this->roam_lock);
713
714 job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
715 (void*)(uintptr_t)(address ? 1 : 0),
716 NULL, NULL);
717 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
718 }
719
720 /**
721 * check if an interface with a given index is up and usable
722 *
723 * this->mutex must be locked when calling this function
724 */
725 static bool is_interface_up_and_usable(private_kernel_netlink_net_t *this,
726 int index)
727 {
728 iface_entry_t *iface;
729
730 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
731 (void**)&iface, &index) == SUCCESS)
732 {
733 return iface_entry_up_and_usable(iface);
734 }
735 return FALSE;
736 }
737
738 /**
739 * unregister the current addr_entry_t from the hashtable it is stored in
740 *
741 * this->mutex must be locked when calling this function
742 */
743 static void addr_entry_unregister(addr_entry_t *addr, iface_entry_t *iface,
744 private_kernel_netlink_net_t *this)
745 {
746 if (addr->refcount)
747 {
748 addr_map_entry_remove(this->vips, addr, iface);
749 this->condvar->broadcast(this->condvar);
750 return;
751 }
752 addr_map_entry_remove(this->addrs, addr, iface);
753 }
754
755 /**
756 * process RTM_NEWLINK/RTM_DELLINK from kernel
757 */
758 static void process_link(private_kernel_netlink_net_t *this,
759 struct nlmsghdr *hdr, bool event)
760 {
761 struct ifinfomsg* msg = (struct ifinfomsg*)(NLMSG_DATA(hdr));
762 struct rtattr *rta = IFLA_RTA(msg);
763 size_t rtasize = IFLA_PAYLOAD (hdr);
764 enumerator_t *enumerator;
765 iface_entry_t *current, *entry = NULL;
766 char *name = NULL;
767 bool update = FALSE, update_routes = FALSE;
768
769 while (RTA_OK(rta, rtasize))
770 {
771 switch (rta->rta_type)
772 {
773 case IFLA_IFNAME:
774 name = RTA_DATA(rta);
775 break;
776 }
777 rta = RTA_NEXT(rta, rtasize);
778 }
779 if (!name)
780 {
781 name = "(unknown)";
782 }
783
784 this->mutex->lock(this->mutex);
785 switch (hdr->nlmsg_type)
786 {
787 case RTM_NEWLINK:
788 {
789 if (this->ifaces->find_first(this->ifaces,
790 (void*)iface_entry_by_index, (void**)&entry,
791 &msg->ifi_index) != SUCCESS)
792 {
793 INIT(entry,
794 .ifindex = msg->ifi_index,
795 .addrs = linked_list_create(),
796 .usable = hydra->kernel_interface->is_interface_usable(
797 hydra->kernel_interface, name),
798 );
799 this->ifaces->insert_last(this->ifaces, entry);
800 }
801 strncpy(entry->ifname, name, IFNAMSIZ);
802 entry->ifname[IFNAMSIZ-1] = '\0';
803 if (event && entry->usable)
804 {
805 if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
806 {
807 update = update_routes = TRUE;
808 DBG1(DBG_KNL, "interface %s activated", name);
809 }
810 if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
811 {
812 update = TRUE;
813 DBG1(DBG_KNL, "interface %s deactivated", name);
814 }
815 }
816 entry->flags = msg->ifi_flags;
817 break;
818 }
819 case RTM_DELLINK:
820 {
821 enumerator = this->ifaces->create_enumerator(this->ifaces);
822 while (enumerator->enumerate(enumerator, &current))
823 {
824 if (current->ifindex == msg->ifi_index)
825 {
826 if (event && current->usable)
827 {
828 update = TRUE;
829 DBG1(DBG_KNL, "interface %s deleted", current->ifname);
830 }
831 /* TODO: move virtual IPs installed on this interface to
832 * another interface? */
833 this->ifaces->remove_at(this->ifaces, enumerator);
834 current->addrs->invoke_function(current->addrs,
835 (void*)addr_entry_unregister, current, this);
836 iface_entry_destroy(current);
837 break;
838 }
839 }
840 enumerator->destroy(enumerator);
841 break;
842 }
843 }
844 this->mutex->unlock(this->mutex);
845
846 if (update_routes && event)
847 {
848 queue_route_reinstall(this, strdup(name));
849 }
850
851 if (update && event)
852 {
853 fire_roam_event(this, TRUE);
854 }
855 }
856
857 /**
858 * process RTM_NEWADDR/RTM_DELADDR from kernel
859 */
860 static void process_addr(private_kernel_netlink_net_t *this,
861 struct nlmsghdr *hdr, bool event)
862 {
863 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
864 struct rtattr *rta = IFA_RTA(msg);
865 size_t rtasize = IFA_PAYLOAD (hdr);
866 host_t *host = NULL;
867 iface_entry_t *iface;
868 chunk_t local = chunk_empty, address = chunk_empty;
869 char *route_ifname = NULL;
870 bool update = FALSE, found = FALSE, changed = FALSE;
871
872 while (RTA_OK(rta, rtasize))
873 {
874 switch (rta->rta_type)
875 {
876 case IFA_LOCAL:
877 local.ptr = RTA_DATA(rta);
878 local.len = RTA_PAYLOAD(rta);
879 break;
880 case IFA_ADDRESS:
881 address.ptr = RTA_DATA(rta);
882 address.len = RTA_PAYLOAD(rta);
883 break;
884 }
885 rta = RTA_NEXT(rta, rtasize);
886 }
887
888 /* For PPP interfaces, we need the IFA_LOCAL address,
889 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
890 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
891 if (local.ptr)
892 {
893 host = host_create_from_chunk(msg->ifa_family, local, 0);
894 }
895 else if (address.ptr)
896 {
897 host = host_create_from_chunk(msg->ifa_family, address, 0);
898 }
899
900 if (host == NULL)
901 { /* bad family? */
902 return;
903 }
904
905 this->mutex->lock(this->mutex);
906 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
907 (void**)&iface, &msg->ifa_index) == SUCCESS)
908 {
909 addr_map_entry_t *entry, lookup = {
910 .ip = host,
911 .iface = iface,
912 };
913 addr_entry_t *addr;
914
915 entry = this->vips->get(this->vips, &lookup);
916 if (entry)
917 {
918 if (hdr->nlmsg_type == RTM_NEWADDR)
919 { /* mark as installed and signal waiting threads */
920 entry->addr->installed = TRUE;
921 }
922 else
923 { /* the address was already marked as uninstalled */
924 addr = entry->addr;
925 iface->addrs->remove(iface->addrs, addr, NULL);
926 addr_map_entry_remove(this->vips, addr, iface);
927 addr_entry_destroy(addr);
928 }
929 /* no roam events etc. for virtual IPs */
930 this->condvar->broadcast(this->condvar);
931 this->mutex->unlock(this->mutex);
932 host->destroy(host);
933 return;
934 }
935 entry = this->addrs->get(this->addrs, &lookup);
936 if (entry)
937 {
938 if (hdr->nlmsg_type == RTM_DELADDR)
939 {
940 found = TRUE;
941 addr = entry->addr;
942 iface->addrs->remove(iface->addrs, addr, NULL);
943 if (iface->usable)
944 {
945 changed = TRUE;
946 DBG1(DBG_KNL, "%H disappeared from %s", host,
947 iface->ifname);
948 }
949 addr_map_entry_remove(this->addrs, addr, iface);
950 addr_entry_destroy(addr);
951 }
952 }
953 else
954 {
955 if (hdr->nlmsg_type == RTM_NEWADDR)
956 {
957 found = TRUE;
958 changed = TRUE;
959 route_ifname = strdup(iface->ifname);
960 INIT(addr,
961 .ip = host->clone(host),
962 .scope = msg->ifa_scope,
963 );
964 iface->addrs->insert_last(iface->addrs, addr);
965 addr_map_entry_add(this->addrs, addr, iface);
966 if (event && iface->usable)
967 {
968 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
969 }
970 }
971 }
972 if (found && (iface->flags & IFF_UP))
973 {
974 update = TRUE;
975 }
976 if (!iface->usable)
977 { /* ignore events for interfaces excluded by config */
978 update = changed = FALSE;
979 }
980 }
981 this->mutex->unlock(this->mutex);
982
983 if (update && event && route_ifname)
984 {
985 queue_route_reinstall(this, route_ifname);
986 }
987 else
988 {
989 free(route_ifname);
990 }
991 host->destroy(host);
992
993 /* send an update to all IKE_SAs */
994 if (update && event && changed)
995 {
996 fire_roam_event(this, TRUE);
997 }
998 }
999
1000 /**
1001 * process RTM_NEWROUTE and RTM_DELROUTE from kernel
1002 */
1003 static void process_route(private_kernel_netlink_net_t *this, struct nlmsghdr *hdr)
1004 {
1005 struct rtmsg* msg = (struct rtmsg*)(NLMSG_DATA(hdr));
1006 struct rtattr *rta = RTM_RTA(msg);
1007 size_t rtasize = RTM_PAYLOAD(hdr);
1008 u_int32_t rta_oif = 0;
1009 host_t *host = NULL;
1010
1011 /* ignore routes added by us or in the local routing table (local addrs) */
1012 if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
1013 msg->rtm_table == RT_TABLE_LOCAL))
1014 {
1015 return;
1016 }
1017 else if (msg->rtm_flags & RTM_F_CLONED)
1018 { /* ignore cached routes, seem to be created a lot for IPv6 */
1019 return;
1020 }
1021
1022 while (RTA_OK(rta, rtasize))
1023 {
1024 switch (rta->rta_type)
1025 {
1026 case RTA_PREFSRC:
1027 DESTROY_IF(host);
1028 host = host_create_from_chunk(msg->rtm_family,
1029 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)), 0);
1030 break;
1031 case RTA_OIF:
1032 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
1033 {
1034 rta_oif = *(u_int32_t*)RTA_DATA(rta);
1035 }
1036 break;
1037 }
1038 rta = RTA_NEXT(rta, rtasize);
1039 }
1040 this->mutex->lock(this->mutex);
1041 if (rta_oif && !is_interface_up_and_usable(this, rta_oif))
1042 { /* ignore route changes for interfaces that are ignored or down */
1043 this->mutex->unlock(this->mutex);
1044 DESTROY_IF(host);
1045 return;
1046 }
1047 if (!host && rta_oif)
1048 {
1049 host = get_interface_address(this, rta_oif, msg->rtm_family, NULL);
1050 }
1051 if (host)
1052 {
1053 if (!is_known_vip(this, host))
1054 { /* ignore routes added for virtual IPs */
1055 fire_roam_event(this, FALSE);
1056 }
1057 host->destroy(host);
1058 }
1059 this->mutex->unlock(this->mutex);
1060 }
1061
1062 /**
1063 * Receives events from kernel
1064 */
1065 static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
1066 {
1067 char response[1024];
1068 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
1069 struct sockaddr_nl addr;
1070 socklen_t addr_len = sizeof(addr);
1071 int len;
1072 bool oldstate;
1073
1074 oldstate = thread_cancelability(TRUE);
1075 len = recvfrom(this->socket_events, response, sizeof(response), 0,
1076 (struct sockaddr*)&addr, &addr_len);
1077 thread_cancelability(oldstate);
1078
1079 if (len < 0)
1080 {
1081 switch (errno)
1082 {
1083 case EINTR:
1084 /* interrupted, try again */
1085 return JOB_REQUEUE_DIRECT;
1086 case EAGAIN:
1087 /* no data ready, select again */
1088 return JOB_REQUEUE_DIRECT;
1089 default:
1090 DBG1(DBG_KNL, "unable to receive from rt event socket");
1091 sleep(1);
1092 return JOB_REQUEUE_FAIR;
1093 }
1094 }
1095
1096 if (addr.nl_pid != 0)
1097 { /* not from kernel. not interested, try another one */
1098 return JOB_REQUEUE_DIRECT;
1099 }
1100
1101 while (NLMSG_OK(hdr, len))
1102 {
1103 /* looks good so far, dispatch netlink message */
1104 switch (hdr->nlmsg_type)
1105 {
1106 case RTM_NEWADDR:
1107 case RTM_DELADDR:
1108 process_addr(this, hdr, TRUE);
1109 break;
1110 case RTM_NEWLINK:
1111 case RTM_DELLINK:
1112 process_link(this, hdr, TRUE);
1113 break;
1114 case RTM_NEWROUTE:
1115 case RTM_DELROUTE:
1116 if (this->process_route)
1117 {
1118 process_route(this, hdr);
1119 }
1120 break;
1121 default:
1122 break;
1123 }
1124 hdr = NLMSG_NEXT(hdr, len);
1125 }
1126 return JOB_REQUEUE_DIRECT;
1127 }
1128
1129 /** enumerator over addresses */
1130 typedef struct {
1131 private_kernel_netlink_net_t* this;
1132 /** which addresses to enumerate */
1133 kernel_address_type_t which;
1134 } address_enumerator_t;
1135
1136 /**
1137 * cleanup function for address enumerator
1138 */
1139 static void address_enumerator_destroy(address_enumerator_t *data)
1140 {
1141 data->this->mutex->unlock(data->this->mutex);
1142 free(data);
1143 }
1144
1145 /**
1146 * filter for addresses
1147 */
1148 static bool filter_addresses(address_enumerator_t *data,
1149 addr_entry_t** in, host_t** out)
1150 {
1151 if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->refcount)
1152 { /* skip virtual interfaces added by us */
1153 return FALSE;
1154 }
1155 if ((*in)->scope >= RT_SCOPE_LINK)
1156 { /* skip addresses with a unusable scope */
1157 return FALSE;
1158 }
1159 *out = (*in)->ip;
1160 return TRUE;
1161 }
1162
1163 /**
1164 * enumerator constructor for interfaces
1165 */
1166 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
1167 address_enumerator_t *data)
1168 {
1169 return enumerator_create_filter(
1170 iface->addrs->create_enumerator(iface->addrs),
1171 (void*)filter_addresses, data, NULL);
1172 }
1173
1174 /**
1175 * filter for interfaces
1176 */
1177 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
1178 iface_entry_t** out)
1179 {
1180 if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
1181 { /* skip interfaces excluded by config */
1182 return FALSE;
1183 }
1184 if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
1185 { /* ignore loopback devices */
1186 return FALSE;
1187 }
1188 if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
1189 { /* skip interfaces not up */
1190 return FALSE;
1191 }
1192 *out = *in;
1193 return TRUE;
1194 }
1195
1196 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
1197 private_kernel_netlink_net_t *this, kernel_address_type_t which)
1198 {
1199 address_enumerator_t *data = malloc_thing(address_enumerator_t);
1200 data->this = this;
1201 data->which = which;
1202
1203 this->mutex->lock(this->mutex);
1204 return enumerator_create_nested(
1205 enumerator_create_filter(
1206 this->ifaces->create_enumerator(this->ifaces),
1207 (void*)filter_interfaces, data, NULL),
1208 (void*)create_iface_enumerator, data,
1209 (void*)address_enumerator_destroy);
1210 }
1211
1212 METHOD(kernel_net_t, get_interface_name, bool,
1213 private_kernel_netlink_net_t *this, host_t* ip, char **name)
1214 {
1215 addr_map_entry_t *entry, lookup = {
1216 .ip = ip,
1217 };
1218
1219 if (ip->is_anyaddr(ip))
1220 {
1221 return FALSE;
1222 }
1223 this->mutex->lock(this->mutex);
1224 /* first try to find it on an up and usable interface */
1225 entry = this->addrs->get_match(this->addrs, &lookup,
1226 (void*)addr_map_entry_match_up_and_usable);
1227 if (entry)
1228 {
1229 if (name)
1230 {
1231 *name = strdup(entry->iface->ifname);
1232 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
1233 }
1234 this->mutex->unlock(this->mutex);
1235 return TRUE;
1236 }
1237 /* maybe it is installed on an ignored interface */
1238 entry = this->addrs->get_match(this->addrs, &lookup,
1239 (void*)addr_map_entry_match_up);
1240 if (!entry)
1241 {
1242 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
1243 }
1244 this->mutex->unlock(this->mutex);
1245 return FALSE;
1246 }
1247
1248 /**
1249 * get the index of an interface by name
1250 */
1251 static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
1252 {
1253 iface_entry_t *iface;
1254 int ifindex = 0;
1255
1256 DBG2(DBG_KNL, "getting iface index for %s", name);
1257
1258 this->mutex->lock(this->mutex);
1259 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_name,
1260 (void**)&iface, name) == SUCCESS)
1261 {
1262 ifindex = iface->ifindex;
1263 }
1264 this->mutex->unlock(this->mutex);
1265
1266 if (ifindex == 0)
1267 {
1268 DBG1(DBG_KNL, "unable to get interface index for %s", name);
1269 }
1270 return ifindex;
1271 }
1272
1273 /**
1274 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1275 */
1276 static bool addr_in_subnet(chunk_t addr, chunk_t net, int net_len)
1277 {
1278 static const u_char mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
1279 int byte = 0;
1280
1281 if (net_len == 0)
1282 { /* any address matches a /0 network */
1283 return TRUE;
1284 }
1285 if (addr.len != net.len || net_len > 8 * net.len )
1286 {
1287 return FALSE;
1288 }
1289 /* scan through all bytes in network order */
1290 while (net_len > 0)
1291 {
1292 if (net_len < 8)
1293 {
1294 return (mask[net_len] & addr.ptr[byte]) == (mask[net_len] & net.ptr[byte]);
1295 }
1296 else
1297 {
1298 if (addr.ptr[byte] != net.ptr[byte])
1299 {
1300 return FALSE;
1301 }
1302 byte++;
1303 net_len -= 8;
1304 }
1305 }
1306 return TRUE;
1307 }
1308
1309 /**
1310 * Store information about a route retrieved via RTNETLINK
1311 */
1312 typedef struct {
1313 chunk_t gtw;
1314 chunk_t src;
1315 chunk_t dst;
1316 host_t *src_host;
1317 u_int8_t dst_len;
1318 u_int32_t table;
1319 u_int32_t oif;
1320 } rt_entry_t;
1321
1322 /**
1323 * Free a route entry
1324 */
1325 static void rt_entry_destroy(rt_entry_t *this)
1326 {
1327 DESTROY_IF(this->src_host);
1328 free(this);
1329 }
1330
1331 /**
1332 * Parse route received with RTM_NEWROUTE. The given rt_entry_t object will be
1333 * reused if not NULL.
1334 *
1335 * Returned chunks point to internal data of the Netlink message.
1336 */
1337 static rt_entry_t *parse_route(struct nlmsghdr *hdr, rt_entry_t *route)
1338 {
1339 struct rtattr *rta;
1340 struct rtmsg *msg;
1341 size_t rtasize;
1342
1343 msg = (struct rtmsg*)(NLMSG_DATA(hdr));
1344 rta = RTM_RTA(msg);
1345 rtasize = RTM_PAYLOAD(hdr);
1346
1347 if (route)
1348 {
1349 route->gtw = chunk_empty;
1350 route->src = chunk_empty;
1351 route->dst = chunk_empty;
1352 route->dst_len = msg->rtm_dst_len;
1353 route->table = msg->rtm_table;
1354 route->oif = 0;
1355 }
1356 else
1357 {
1358 INIT(route,
1359 .dst_len = msg->rtm_dst_len,
1360 .table = msg->rtm_table,
1361 );
1362 }
1363
1364 while (RTA_OK(rta, rtasize))
1365 {
1366 switch (rta->rta_type)
1367 {
1368 case RTA_PREFSRC:
1369 route->src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1370 break;
1371 case RTA_GATEWAY:
1372 route->gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1373 break;
1374 case RTA_DST:
1375 route->dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1376 break;
1377 case RTA_OIF:
1378 if (RTA_PAYLOAD(rta) == sizeof(route->oif))
1379 {
1380 route->oif = *(u_int32_t*)RTA_DATA(rta);
1381 }
1382 break;
1383 #ifdef HAVE_RTA_TABLE
1384 case RTA_TABLE:
1385 if (RTA_PAYLOAD(rta) == sizeof(route->table))
1386 {
1387 route->table = *(u_int32_t*)RTA_DATA(rta);
1388 }
1389 break;
1390 #endif /* HAVE_RTA_TABLE*/
1391 }
1392 rta = RTA_NEXT(rta, rtasize);
1393 }
1394 return route;
1395 }
1396
1397 /**
1398 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1399 */
1400 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
1401 bool nexthop, host_t *candidate)
1402 {
1403 netlink_buf_t request;
1404 struct nlmsghdr *hdr, *out, *current;
1405 struct rtmsg *msg;
1406 chunk_t chunk;
1407 size_t len;
1408 linked_list_t *routes;
1409 rt_entry_t *route = NULL, *best = NULL;
1410 enumerator_t *enumerator;
1411 host_t *addr = NULL;
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->mutex->lock(this->mutex);
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 route->src_host = get_route(this, gtw, FALSE, candidate);
1569 gtw->destroy(gtw);
1570 if (route->src_host)
1571 { /* more of the same */
1572 if (!candidate ||
1573 candidate->ip_equals(candidate, route->src_host))
1574 {
1575 best = route;
1576 break;
1577 }
1578 best = best ?: route;
1579 }
1580 }
1581 }
1582 enumerator->destroy(enumerator);
1583
1584 if (nexthop)
1585 { /* nexthop lookup, return gateway if any */
1586 if (best || routes->get_first(routes, (void**)&best) == SUCCESS)
1587 {
1588 addr = host_create_from_chunk(msg->rtm_family, best->gtw, 0);
1589 }
1590 addr = addr ?: dest->clone(dest);
1591 }
1592 else
1593 {
1594 if (best)
1595 {
1596 addr = best->src_host->clone(best->src_host);
1597 }
1598 }
1599 this->mutex->unlock(this->mutex);
1600 routes->destroy_function(routes, (void*)rt_entry_destroy);
1601 free(out);
1602
1603 if (addr)
1604 {
1605 DBG2(DBG_KNL, "using %H as %s to reach %H", addr,
1606 nexthop ? "nexthop" : "address", dest);
1607 }
1608 else
1609 {
1610 DBG2(DBG_KNL, "no %s found to reach %H",
1611 nexthop ? "nexthop" : "address", dest);
1612 }
1613 return addr;
1614 }
1615
1616 METHOD(kernel_net_t, get_source_addr, host_t*,
1617 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1618 {
1619 return get_route(this, dest, FALSE, src);
1620 }
1621
1622 METHOD(kernel_net_t, get_nexthop, host_t*,
1623 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1624 {
1625 return get_route(this, dest, TRUE, src);
1626 }
1627
1628 /**
1629 * Manages the creation and deletion of ip addresses on an interface.
1630 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1631 */
1632 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
1633 int flags, int if_index, host_t *ip)
1634 {
1635 netlink_buf_t request;
1636 struct nlmsghdr *hdr;
1637 struct ifaddrmsg *msg;
1638 chunk_t chunk;
1639
1640 memset(&request, 0, sizeof(request));
1641
1642 chunk = ip->get_address(ip);
1643
1644 hdr = (struct nlmsghdr*)request;
1645 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1646 hdr->nlmsg_type = nlmsg_type;
1647 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1648
1649 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1650 msg->ifa_family = ip->get_family(ip);
1651 msg->ifa_flags = 0;
1652 msg->ifa_prefixlen = 8 * chunk.len;
1653 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1654 msg->ifa_index = if_index;
1655
1656 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1657
1658 return this->socket->send_ack(this->socket, hdr);
1659 }
1660
1661 METHOD(kernel_net_t, add_ip, status_t,
1662 private_kernel_netlink_net_t *this, host_t *virtual_ip, host_t *iface_ip)
1663 {
1664 addr_map_entry_t *entry, lookup = {
1665 .ip = virtual_ip,
1666 };
1667 iface_entry_t *iface = NULL;
1668
1669 if (!this->install_virtual_ip)
1670 { /* disabled by config */
1671 return SUCCESS;
1672 }
1673
1674 this->mutex->lock(this->mutex);
1675 /* the virtual IP might actually be installed as regular IP, in which case
1676 * we don't track it as virtual IP */
1677 entry = this->addrs->get_match(this->addrs, &lookup,
1678 (void*)addr_map_entry_match);
1679 if (!entry)
1680 { /* otherwise it might already be installed as virtual IP */
1681 entry = this->vips->get_match(this->vips, &lookup,
1682 (void*)addr_map_entry_match);
1683 if (entry)
1684 { /* the vip we found can be in one of three states: 1) installed and
1685 * ready, 2) just added by another thread, but not yet confirmed to
1686 * be installed by the kernel, 3) just deleted, but not yet gone.
1687 * Then while we wait below, several things could happen (as we
1688 * release the mutex). For instance, the interface could disappear,
1689 * or the IP is finally deleted, and it reappears on a different
1690 * interface. All these cases are handled by the call below. */
1691 while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
1692 {
1693 this->condvar->wait(this->condvar, this->mutex);
1694 }
1695 if (entry)
1696 {
1697 entry->addr->refcount++;
1698 }
1699 }
1700 }
1701 if (entry)
1702 {
1703 DBG2(DBG_KNL, "virtual IP %H is already installed on %s", virtual_ip,
1704 entry->iface->ifname);
1705 this->mutex->unlock(this->mutex);
1706 return SUCCESS;
1707 }
1708 /* try to find the target interface, either by config or via src ip */
1709 if (!this->install_virtual_ip_on ||
1710 this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_name,
1711 (void**)&iface, this->install_virtual_ip_on) != SUCCESS)
1712 {
1713 lookup.ip = iface_ip;
1714 entry = this->addrs->get_match(this->addrs, &lookup,
1715 (void*)addr_map_entry_match);
1716 if (!entry)
1717 { /* if we don't find the requested interface we just use the first */
1718 this->ifaces->get_first(this->ifaces, (void**)&iface);
1719 }
1720 else
1721 {
1722 iface = entry->iface;
1723 }
1724 }
1725 if (iface)
1726 {
1727 addr_entry_t *addr;
1728
1729 INIT(addr,
1730 .ip = virtual_ip->clone(virtual_ip),
1731 .refcount = 1,
1732 .scope = RT_SCOPE_UNIVERSE,
1733 );
1734 iface->addrs->insert_last(iface->addrs, addr);
1735 addr_map_entry_add(this->vips, addr, iface);
1736 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1737 iface->ifindex, virtual_ip) == SUCCESS)
1738 {
1739 while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
1740 { /* wait until address appears */
1741 this->condvar->wait(this->condvar, this->mutex);
1742 }
1743 if (entry)
1744 { /* we fail if the interface got deleted in the meantime */
1745 DBG2(DBG_KNL, "virtual IP %H installed on %s", virtual_ip,
1746 entry->iface->ifname);
1747 this->mutex->unlock(this->mutex);
1748 return SUCCESS;
1749 }
1750 }
1751 this->mutex->unlock(this->mutex);
1752 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1753 return FAILED;
1754 }
1755 this->mutex->unlock(this->mutex);
1756 DBG1(DBG_KNL, "no interface available, unable to install virtual IP %H",
1757 virtual_ip);
1758 return FAILED;
1759 }
1760
1761 METHOD(kernel_net_t, del_ip, status_t,
1762 private_kernel_netlink_net_t *this, host_t *virtual_ip)
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->mutex->lock(this->mutex);
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->mutex->unlock(this->mutex);
1787 return SUCCESS;
1788 }
1789 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1790 this->mutex->unlock(this->mutex);
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);
1802 if (status == SUCCESS)
1803 { /* wait until the address is really gone */
1804 while (is_known_vip(this, virtual_ip))
1805 {
1806 this->condvar->wait(this->condvar, this->mutex);
1807 }
1808 }
1809 this->mutex->unlock(this->mutex);
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->mutex->unlock(this->mutex);
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->mutex->lock(this->mutex);
1903 found = this->routes->get(this->routes, &route);
1904 if (found)
1905 {
1906 this->mutex->unlock(this->mutex);
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->mutex->unlock(this->mutex);
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->mutex->lock(this->mutex);
1931 found = this->routes->get(this->routes, &route);
1932 if (!found)
1933 {
1934 this->mutex->unlock(this->mutex);
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->mutex->unlock(this->mutex);
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->mutex->lock(this->mutex);
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->mutex->unlock(this->mutex);
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 DESTROY_IF(this->socket);
2144
2145 net_changes_clear(this);
2146 this->net_changes->destroy(this->net_changes);
2147 this->net_changes_lock->destroy(this->net_changes_lock);
2148
2149 addr_map_destroy(this->addrs);
2150 addr_map_destroy(this->vips);
2151
2152 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
2153 this->rt_exclude->destroy(this->rt_exclude);
2154 this->roam_lock->destroy(this->roam_lock);
2155 this->condvar->destroy(this->condvar);
2156 this->mutex->destroy(this->mutex);
2157 free(this);
2158 }
2159
2160 /*
2161 * Described in header.
2162 */
2163 kernel_netlink_net_t *kernel_netlink_net_create()
2164 {
2165 private_kernel_netlink_net_t *this;
2166 enumerator_t *enumerator;
2167 bool register_for_events = TRUE;
2168 char *exclude;
2169
2170 INIT(this,
2171 .public = {
2172 .interface = {
2173 .get_interface = _get_interface_name,
2174 .create_address_enumerator = _create_address_enumerator,
2175 .get_source_addr = _get_source_addr,
2176 .get_nexthop = _get_nexthop,
2177 .add_ip = _add_ip,
2178 .del_ip = _del_ip,
2179 .add_route = _add_route,
2180 .del_route = _del_route,
2181 .destroy = _destroy,
2182 },
2183 },
2184 .socket = netlink_socket_create(NETLINK_ROUTE),
2185 .rt_exclude = linked_list_create(),
2186 .routes = hashtable_create((hashtable_hash_t)route_entry_hash,
2187 (hashtable_equals_t)route_entry_equals, 16),
2188 .net_changes = hashtable_create(
2189 (hashtable_hash_t)net_change_hash,
2190 (hashtable_equals_t)net_change_equals, 16),
2191 .addrs = hashtable_create(
2192 (hashtable_hash_t)addr_map_entry_hash,
2193 (hashtable_equals_t)addr_map_entry_equals, 16),
2194 .vips = hashtable_create((hashtable_hash_t)addr_map_entry_hash,
2195 (hashtable_equals_t)addr_map_entry_equals, 16),
2196 .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
2197 .ifaces = linked_list_create(),
2198 .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
2199 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
2200 .roam_lock = spinlock_create(),
2201 .routing_table = lib->settings->get_int(lib->settings,
2202 "%s.routing_table", ROUTING_TABLE, hydra->daemon),
2203 .routing_table_prio = lib->settings->get_int(lib->settings,
2204 "%s.routing_table_prio", ROUTING_TABLE_PRIO, hydra->daemon),
2205 .process_route = lib->settings->get_bool(lib->settings,
2206 "%s.process_route", TRUE, hydra->daemon),
2207 .install_virtual_ip = lib->settings->get_bool(lib->settings,
2208 "%s.install_virtual_ip", TRUE, hydra->daemon),
2209 .install_virtual_ip_on = lib->settings->get_str(lib->settings,
2210 "%s.install_virtual_ip_on", NULL, hydra->daemon),
2211 );
2212 timerclear(&this->last_route_reinstall);
2213 timerclear(&this->next_roam);
2214
2215 check_kernel_features(this);
2216
2217 if (streq(hydra->daemon, "starter"))
2218 { /* starter has no threads, so we do not register for kernel events */
2219 register_for_events = FALSE;
2220 }
2221
2222 exclude = lib->settings->get_str(lib->settings,
2223 "%s.ignore_routing_tables", NULL, hydra->daemon);
2224 if (exclude)
2225 {
2226 char *token;
2227 uintptr_t table;
2228
2229 enumerator = enumerator_create_token(exclude, " ", " ");
2230 while (enumerator->enumerate(enumerator, &token))
2231 {
2232 errno = 0;
2233 table = strtoul(token, NULL, 10);
2234
2235 if (errno == 0)
2236 {
2237 this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
2238 }
2239 }
2240 enumerator->destroy(enumerator);
2241 }
2242
2243 if (register_for_events)
2244 {
2245 struct sockaddr_nl addr;
2246
2247 memset(&addr, 0, sizeof(addr));
2248 addr.nl_family = AF_NETLINK;
2249
2250 /* create and bind RT socket for events (address/interface/route changes) */
2251 this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2252 if (this->socket_events < 0)
2253 {
2254 DBG1(DBG_KNL, "unable to create RT event socket");
2255 destroy(this);
2256 return NULL;
2257 }
2258 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
2259 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_LINK;
2260 if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
2261 {
2262 DBG1(DBG_KNL, "unable to bind RT event socket");
2263 destroy(this);
2264 return NULL;
2265 }
2266
2267 lib->processor->queue_job(lib->processor,
2268 (job_t*)callback_job_create_with_prio(
2269 (callback_job_cb_t)receive_events, this, NULL,
2270 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
2271 }
2272
2273 if (init_address_list(this) != SUCCESS)
2274 {
2275 DBG1(DBG_KNL, "unable to get interface list");
2276 destroy(this);
2277 return NULL;
2278 }
2279
2280 if (this->routing_table)
2281 {
2282 if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
2283 this->routing_table_prio) != SUCCESS)
2284 {
2285 DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
2286 }
2287 if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
2288 this->routing_table_prio) != SUCCESS)
2289 {
2290 DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
2291 }
2292 }
2293
2294 return &this->public;
2295 }