Use a separate mutex for cached routes in kernel-netlink plugin
[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 * mutex for routes
403 */
404 mutex_t *routes_lock;
405
406 /**
407 * interface changes which may trigger route reinstallation
408 */
409 hashtable_t *net_changes;
410
411 /**
412 * mutex for route reinstallation triggers
413 */
414 mutex_t *net_changes_lock;
415
416 /**
417 * time of last route reinstallation
418 */
419 timeval_t last_route_reinstall;
420
421 /**
422 * whether to react to RTM_NEWROUTE or RTM_DELROUTE events
423 */
424 bool process_route;
425
426 /**
427 * whether to actually install virtual IPs
428 */
429 bool install_virtual_ip;
430
431 /**
432 * the name of the interface virtual IP addresses are installed on
433 */
434 char *install_virtual_ip_on;
435
436 /**
437 * whether preferred source addresses can be specified for IPv6 routes
438 */
439 bool rta_prefsrc_for_ipv6;
440
441 /**
442 * list with routing tables to be excluded from route lookup
443 */
444 linked_list_t *rt_exclude;
445 };
446
447 /**
448 * Forward declaration
449 */
450 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
451 int nlmsg_type, int flags, chunk_t dst_net,
452 u_int8_t prefixlen, host_t *gateway,
453 host_t *src_ip, char *if_name);
454
455 /**
456 * Clear the queued network changes.
457 */
458 static void net_changes_clear(private_kernel_netlink_net_t *this)
459 {
460 enumerator_t *enumerator;
461 net_change_t *change;
462
463 enumerator = this->net_changes->create_enumerator(this->net_changes);
464 while (enumerator->enumerate(enumerator, NULL, (void**)&change))
465 {
466 this->net_changes->remove_at(this->net_changes, enumerator);
467 net_change_destroy(change);
468 }
469 enumerator->destroy(enumerator);
470 }
471
472 /**
473 * Act upon queued network changes.
474 */
475 static job_requeue_t reinstall_routes(private_kernel_netlink_net_t *this)
476 {
477 enumerator_t *enumerator;
478 route_entry_t *route;
479
480 this->net_changes_lock->lock(this->net_changes_lock);
481 this->routes_lock->lock(this->routes_lock);
482
483 enumerator = this->routes->create_enumerator(this->routes);
484 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
485 {
486 net_change_t *change, lookup = {
487 .if_name = route->if_name,
488 };
489 /* check if a change for the outgoing interface is queued */
490 change = this->net_changes->get(this->net_changes, &lookup);
491 if (!change)
492 { /* in case src_ip is not on the outgoing interface */
493 if (this->public.interface.get_interface(&this->public.interface,
494 route->src_ip, &lookup.if_name))
495 {
496 if (!streq(lookup.if_name, route->if_name))
497 {
498 change = this->net_changes->get(this->net_changes, &lookup);
499 }
500 free(lookup.if_name);
501 }
502 }
503 if (change)
504 {
505 manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
506 route->dst_net, route->prefixlen, route->gateway,
507 route->src_ip, route->if_name);
508 }
509 }
510 enumerator->destroy(enumerator);
511 this->routes_lock->unlock(this->routes_lock);
512
513 net_changes_clear(this);
514 this->net_changes_lock->unlock(this->net_changes_lock);
515 return JOB_REQUEUE_NONE;
516 }
517
518 /**
519 * Queue route reinstallation caused by network changes for a given interface.
520 *
521 * The route reinstallation is delayed for a while and only done once for
522 * several calls during this delay, in order to avoid doing it too often.
523 * The interface name is freed.
524 */
525 static void queue_route_reinstall(private_kernel_netlink_net_t *this,
526 char *if_name)
527 {
528 net_change_t *update, *found;
529 timeval_t now;
530 job_t *job;
531
532 INIT(update,
533 .if_name = if_name
534 );
535
536 this->net_changes_lock->lock(this->net_changes_lock);
537 found = this->net_changes->put(this->net_changes, update, update);
538 if (found)
539 {
540 net_change_destroy(found);
541 }
542 time_monotonic(&now);
543 if (timercmp(&now, &this->last_route_reinstall, >))
544 {
545 now.tv_usec += ROUTE_DELAY * 1000;
546 while (now.tv_usec > 1000000)
547 {
548 now.tv_sec++;
549 now.tv_usec -= 1000000;
550 }
551 this->last_route_reinstall = now;
552
553 job = (job_t*)callback_job_create((callback_job_cb_t)reinstall_routes,
554 this, NULL, NULL);
555 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROUTE_DELAY);
556 }
557 this->net_changes_lock->unlock(this->net_changes_lock);
558 }
559
560 /**
561 * check if the given IP is known as virtual IP and currently installed
562 *
563 * this function will also return TRUE if the virtual IP entry disappeared.
564 * in that case the returned entry will be NULL.
565 *
566 * this->mutex must be locked when calling this function
567 */
568 static bool is_vip_installed_or_gone(private_kernel_netlink_net_t *this,
569 host_t *ip, addr_map_entry_t **entry)
570 {
571 addr_map_entry_t lookup = {
572 .ip = ip,
573 };
574
575 *entry = this->vips->get_match(this->vips, &lookup,
576 (void*)addr_map_entry_match);
577 if (*entry == NULL)
578 { /* the virtual IP disappeared */
579 return TRUE;
580 }
581 return (*entry)->addr->installed;
582 }
583
584 /**
585 * check if the given IP is known as virtual IP
586 *
587 * this->mutex must be locked when calling this function
588 */
589 static bool is_known_vip(private_kernel_netlink_net_t *this, host_t *ip)
590 {
591 addr_map_entry_t lookup = {
592 .ip = ip,
593 };
594
595 return this->vips->get_match(this->vips, &lookup,
596 (void*)addr_map_entry_match) != NULL;
597 }
598
599 /**
600 * Add an address map entry
601 */
602 static void addr_map_entry_add(hashtable_t *map, addr_entry_t *addr,
603 iface_entry_t *iface)
604 {
605 addr_map_entry_t *entry;
606
607 INIT(entry,
608 .ip = addr->ip,
609 .addr = addr,
610 .iface = iface,
611 );
612 entry = map->put(map, entry, entry);
613 free(entry);
614 }
615
616 /**
617 * Remove an address map entry
618 */
619 static void addr_map_entry_remove(hashtable_t *map, addr_entry_t *addr,
620 iface_entry_t *iface)
621 {
622 addr_map_entry_t *entry, lookup = {
623 .ip = addr->ip,
624 .addr = addr,
625 .iface = iface,
626 };
627
628 entry = map->remove(map, &lookup);
629 free(entry);
630 }
631
632 /**
633 * get the first non-virtual ip address on the given interface.
634 * if a candidate address is given, we first search for that address and if not
635 * found return the address as above.
636 * returned host is a clone, has to be freed by caller.
637 */
638 static host_t *get_interface_address(private_kernel_netlink_net_t *this,
639 int ifindex, int family, host_t *candidate)
640 {
641 iface_entry_t *iface;
642 enumerator_t *addrs;
643 addr_entry_t *addr;
644 host_t *ip = NULL;
645
646 this->mutex->lock(this->mutex);
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 if (ip)
678 {
679 ip = ip->clone(ip);
680 }
681 this->mutex->unlock(this->mutex);
682 return ip;
683 }
684
685 /**
686 * callback function that raises the delayed roam event
687 */
688 static job_requeue_t roam_event(uintptr_t address)
689 {
690 hydra->kernel_interface->roam(hydra->kernel_interface, address != 0);
691 return JOB_REQUEUE_NONE;
692 }
693
694 /**
695 * fire a roaming event. we delay it for a bit and fire only one event
696 * for multiple calls. otherwise we would create too many events.
697 */
698 static void fire_roam_event(private_kernel_netlink_net_t *this, bool address)
699 {
700 timeval_t now;
701 job_t *job;
702
703 time_monotonic(&now);
704 this->roam_lock->lock(this->roam_lock);
705 if (!timercmp(&now, &this->next_roam, >))
706 {
707 this->roam_lock->unlock(this->roam_lock);
708 return;
709 }
710 now.tv_usec += ROAM_DELAY * 1000;
711 while (now.tv_usec > 1000000)
712 {
713 now.tv_sec++;
714 now.tv_usec -= 1000000;
715 }
716 this->next_roam = now;
717 this->roam_lock->unlock(this->roam_lock);
718
719 job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
720 (void*)(uintptr_t)(address ? 1 : 0),
721 NULL, NULL);
722 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
723 }
724
725 /**
726 * check if an interface with a given index is up and usable
727 *
728 * this->mutex must be locked when calling this function
729 */
730 static bool is_interface_up_and_usable(private_kernel_netlink_net_t *this,
731 int index)
732 {
733 iface_entry_t *iface;
734
735 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
736 (void**)&iface, &index) == SUCCESS)
737 {
738 return iface_entry_up_and_usable(iface);
739 }
740 return FALSE;
741 }
742
743 /**
744 * unregister the current addr_entry_t from the hashtable it is stored in
745 *
746 * this->mutex must be locked when calling this function
747 */
748 static void addr_entry_unregister(addr_entry_t *addr, iface_entry_t *iface,
749 private_kernel_netlink_net_t *this)
750 {
751 if (addr->refcount)
752 {
753 addr_map_entry_remove(this->vips, addr, iface);
754 this->condvar->broadcast(this->condvar);
755 return;
756 }
757 addr_map_entry_remove(this->addrs, addr, iface);
758 }
759
760 /**
761 * process RTM_NEWLINK/RTM_DELLINK from kernel
762 */
763 static void process_link(private_kernel_netlink_net_t *this,
764 struct nlmsghdr *hdr, bool event)
765 {
766 struct ifinfomsg* msg = (struct ifinfomsg*)(NLMSG_DATA(hdr));
767 struct rtattr *rta = IFLA_RTA(msg);
768 size_t rtasize = IFLA_PAYLOAD (hdr);
769 enumerator_t *enumerator;
770 iface_entry_t *current, *entry = NULL;
771 char *name = NULL;
772 bool update = FALSE, update_routes = FALSE;
773
774 while (RTA_OK(rta, rtasize))
775 {
776 switch (rta->rta_type)
777 {
778 case IFLA_IFNAME:
779 name = RTA_DATA(rta);
780 break;
781 }
782 rta = RTA_NEXT(rta, rtasize);
783 }
784 if (!name)
785 {
786 name = "(unknown)";
787 }
788
789 this->mutex->lock(this->mutex);
790 switch (hdr->nlmsg_type)
791 {
792 case RTM_NEWLINK:
793 {
794 if (this->ifaces->find_first(this->ifaces,
795 (void*)iface_entry_by_index, (void**)&entry,
796 &msg->ifi_index) != SUCCESS)
797 {
798 INIT(entry,
799 .ifindex = msg->ifi_index,
800 .addrs = linked_list_create(),
801 .usable = hydra->kernel_interface->is_interface_usable(
802 hydra->kernel_interface, name),
803 );
804 this->ifaces->insert_last(this->ifaces, entry);
805 }
806 strncpy(entry->ifname, name, IFNAMSIZ);
807 entry->ifname[IFNAMSIZ-1] = '\0';
808 if (event && entry->usable)
809 {
810 if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
811 {
812 update = update_routes = TRUE;
813 DBG1(DBG_KNL, "interface %s activated", name);
814 }
815 if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
816 {
817 update = TRUE;
818 DBG1(DBG_KNL, "interface %s deactivated", name);
819 }
820 }
821 entry->flags = msg->ifi_flags;
822 break;
823 }
824 case RTM_DELLINK:
825 {
826 enumerator = this->ifaces->create_enumerator(this->ifaces);
827 while (enumerator->enumerate(enumerator, &current))
828 {
829 if (current->ifindex == msg->ifi_index)
830 {
831 if (event && current->usable)
832 {
833 update = TRUE;
834 DBG1(DBG_KNL, "interface %s deleted", current->ifname);
835 }
836 /* TODO: move virtual IPs installed on this interface to
837 * another interface? */
838 this->ifaces->remove_at(this->ifaces, enumerator);
839 current->addrs->invoke_function(current->addrs,
840 (void*)addr_entry_unregister, current, this);
841 iface_entry_destroy(current);
842 break;
843 }
844 }
845 enumerator->destroy(enumerator);
846 break;
847 }
848 }
849 this->mutex->unlock(this->mutex);
850
851 if (update_routes && event)
852 {
853 queue_route_reinstall(this, strdup(name));
854 }
855
856 if (update && event)
857 {
858 fire_roam_event(this, TRUE);
859 }
860 }
861
862 /**
863 * process RTM_NEWADDR/RTM_DELADDR from kernel
864 */
865 static void process_addr(private_kernel_netlink_net_t *this,
866 struct nlmsghdr *hdr, bool event)
867 {
868 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
869 struct rtattr *rta = IFA_RTA(msg);
870 size_t rtasize = IFA_PAYLOAD (hdr);
871 host_t *host = NULL;
872 iface_entry_t *iface;
873 chunk_t local = chunk_empty, address = chunk_empty;
874 char *route_ifname = NULL;
875 bool update = FALSE, found = FALSE, changed = FALSE;
876
877 while (RTA_OK(rta, rtasize))
878 {
879 switch (rta->rta_type)
880 {
881 case IFA_LOCAL:
882 local.ptr = RTA_DATA(rta);
883 local.len = RTA_PAYLOAD(rta);
884 break;
885 case IFA_ADDRESS:
886 address.ptr = RTA_DATA(rta);
887 address.len = RTA_PAYLOAD(rta);
888 break;
889 }
890 rta = RTA_NEXT(rta, rtasize);
891 }
892
893 /* For PPP interfaces, we need the IFA_LOCAL address,
894 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
895 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
896 if (local.ptr)
897 {
898 host = host_create_from_chunk(msg->ifa_family, local, 0);
899 }
900 else if (address.ptr)
901 {
902 host = host_create_from_chunk(msg->ifa_family, address, 0);
903 }
904
905 if (host == NULL)
906 { /* bad family? */
907 return;
908 }
909
910 this->mutex->lock(this->mutex);
911 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_index,
912 (void**)&iface, &msg->ifa_index) == SUCCESS)
913 {
914 addr_map_entry_t *entry, lookup = {
915 .ip = host,
916 .iface = iface,
917 };
918 addr_entry_t *addr;
919
920 entry = this->vips->get(this->vips, &lookup);
921 if (entry)
922 {
923 if (hdr->nlmsg_type == RTM_NEWADDR)
924 { /* mark as installed and signal waiting threads */
925 entry->addr->installed = TRUE;
926 }
927 else
928 { /* the address was already marked as uninstalled */
929 addr = entry->addr;
930 iface->addrs->remove(iface->addrs, addr, NULL);
931 addr_map_entry_remove(this->vips, addr, iface);
932 addr_entry_destroy(addr);
933 }
934 /* no roam events etc. for virtual IPs */
935 this->condvar->broadcast(this->condvar);
936 this->mutex->unlock(this->mutex);
937 host->destroy(host);
938 return;
939 }
940 entry = this->addrs->get(this->addrs, &lookup);
941 if (entry)
942 {
943 if (hdr->nlmsg_type == RTM_DELADDR)
944 {
945 found = TRUE;
946 addr = entry->addr;
947 iface->addrs->remove(iface->addrs, addr, NULL);
948 if (iface->usable)
949 {
950 changed = TRUE;
951 DBG1(DBG_KNL, "%H disappeared from %s", host,
952 iface->ifname);
953 }
954 addr_map_entry_remove(this->addrs, addr, iface);
955 addr_entry_destroy(addr);
956 }
957 }
958 else
959 {
960 if (hdr->nlmsg_type == RTM_NEWADDR)
961 {
962 found = TRUE;
963 changed = TRUE;
964 route_ifname = strdup(iface->ifname);
965 INIT(addr,
966 .ip = host->clone(host),
967 .scope = msg->ifa_scope,
968 );
969 iface->addrs->insert_last(iface->addrs, addr);
970 addr_map_entry_add(this->addrs, addr, iface);
971 if (event && iface->usable)
972 {
973 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
974 }
975 }
976 }
977 if (found && (iface->flags & IFF_UP))
978 {
979 update = TRUE;
980 }
981 if (!iface->usable)
982 { /* ignore events for interfaces excluded by config */
983 update = changed = FALSE;
984 }
985 }
986 this->mutex->unlock(this->mutex);
987
988 if (update && event && route_ifname)
989 {
990 queue_route_reinstall(this, route_ifname);
991 }
992 else
993 {
994 free(route_ifname);
995 }
996 host->destroy(host);
997
998 /* send an update to all IKE_SAs */
999 if (update && event && changed)
1000 {
1001 fire_roam_event(this, TRUE);
1002 }
1003 }
1004
1005 /**
1006 * process RTM_NEWROUTE and RTM_DELROUTE from kernel
1007 */
1008 static void process_route(private_kernel_netlink_net_t *this, struct nlmsghdr *hdr)
1009 {
1010 struct rtmsg* msg = (struct rtmsg*)(NLMSG_DATA(hdr));
1011 struct rtattr *rta = RTM_RTA(msg);
1012 size_t rtasize = RTM_PAYLOAD(hdr);
1013 u_int32_t rta_oif = 0;
1014 host_t *host = NULL;
1015
1016 /* ignore routes added by us or in the local routing table (local addrs) */
1017 if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
1018 msg->rtm_table == RT_TABLE_LOCAL))
1019 {
1020 return;
1021 }
1022 else if (msg->rtm_flags & RTM_F_CLONED)
1023 { /* ignore cached routes, seem to be created a lot for IPv6 */
1024 return;
1025 }
1026
1027 while (RTA_OK(rta, rtasize))
1028 {
1029 switch (rta->rta_type)
1030 {
1031 case RTA_PREFSRC:
1032 DESTROY_IF(host);
1033 host = host_create_from_chunk(msg->rtm_family,
1034 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)), 0);
1035 break;
1036 case RTA_OIF:
1037 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
1038 {
1039 rta_oif = *(u_int32_t*)RTA_DATA(rta);
1040 }
1041 break;
1042 }
1043 rta = RTA_NEXT(rta, rtasize);
1044 }
1045 this->mutex->lock(this->mutex);
1046 if (rta_oif && !is_interface_up_and_usable(this, rta_oif))
1047 { /* ignore route changes for interfaces that are ignored or down */
1048 this->mutex->unlock(this->mutex);
1049 DESTROY_IF(host);
1050 return;
1051 }
1052 if (!host && rta_oif)
1053 {
1054 host = get_interface_address(this, rta_oif, msg->rtm_family, NULL);
1055 }
1056 if (host)
1057 {
1058 if (!is_known_vip(this, host))
1059 { /* ignore routes added for virtual IPs */
1060 fire_roam_event(this, FALSE);
1061 }
1062 host->destroy(host);
1063 }
1064 this->mutex->unlock(this->mutex);
1065 }
1066
1067 /**
1068 * Receives events from kernel
1069 */
1070 static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
1071 {
1072 char response[1024];
1073 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
1074 struct sockaddr_nl addr;
1075 socklen_t addr_len = sizeof(addr);
1076 int len;
1077 bool oldstate;
1078
1079 oldstate = thread_cancelability(TRUE);
1080 len = recvfrom(this->socket_events, response, sizeof(response), 0,
1081 (struct sockaddr*)&addr, &addr_len);
1082 thread_cancelability(oldstate);
1083
1084 if (len < 0)
1085 {
1086 switch (errno)
1087 {
1088 case EINTR:
1089 /* interrupted, try again */
1090 return JOB_REQUEUE_DIRECT;
1091 case EAGAIN:
1092 /* no data ready, select again */
1093 return JOB_REQUEUE_DIRECT;
1094 default:
1095 DBG1(DBG_KNL, "unable to receive from rt event socket");
1096 sleep(1);
1097 return JOB_REQUEUE_FAIR;
1098 }
1099 }
1100
1101 if (addr.nl_pid != 0)
1102 { /* not from kernel. not interested, try another one */
1103 return JOB_REQUEUE_DIRECT;
1104 }
1105
1106 while (NLMSG_OK(hdr, len))
1107 {
1108 /* looks good so far, dispatch netlink message */
1109 switch (hdr->nlmsg_type)
1110 {
1111 case RTM_NEWADDR:
1112 case RTM_DELADDR:
1113 process_addr(this, hdr, TRUE);
1114 break;
1115 case RTM_NEWLINK:
1116 case RTM_DELLINK:
1117 process_link(this, hdr, TRUE);
1118 break;
1119 case RTM_NEWROUTE:
1120 case RTM_DELROUTE:
1121 if (this->process_route)
1122 {
1123 process_route(this, hdr);
1124 }
1125 break;
1126 default:
1127 break;
1128 }
1129 hdr = NLMSG_NEXT(hdr, len);
1130 }
1131 return JOB_REQUEUE_DIRECT;
1132 }
1133
1134 /** enumerator over addresses */
1135 typedef struct {
1136 private_kernel_netlink_net_t* this;
1137 /** which addresses to enumerate */
1138 kernel_address_type_t which;
1139 } address_enumerator_t;
1140
1141 /**
1142 * cleanup function for address enumerator
1143 */
1144 static void address_enumerator_destroy(address_enumerator_t *data)
1145 {
1146 data->this->mutex->unlock(data->this->mutex);
1147 free(data);
1148 }
1149
1150 /**
1151 * filter for addresses
1152 */
1153 static bool filter_addresses(address_enumerator_t *data,
1154 addr_entry_t** in, host_t** out)
1155 {
1156 if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->refcount)
1157 { /* skip virtual interfaces added by us */
1158 return FALSE;
1159 }
1160 if ((*in)->scope >= RT_SCOPE_LINK)
1161 { /* skip addresses with a unusable scope */
1162 return FALSE;
1163 }
1164 *out = (*in)->ip;
1165 return TRUE;
1166 }
1167
1168 /**
1169 * enumerator constructor for interfaces
1170 */
1171 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
1172 address_enumerator_t *data)
1173 {
1174 return enumerator_create_filter(
1175 iface->addrs->create_enumerator(iface->addrs),
1176 (void*)filter_addresses, data, NULL);
1177 }
1178
1179 /**
1180 * filter for interfaces
1181 */
1182 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
1183 iface_entry_t** out)
1184 {
1185 if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
1186 { /* skip interfaces excluded by config */
1187 return FALSE;
1188 }
1189 if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
1190 { /* ignore loopback devices */
1191 return FALSE;
1192 }
1193 if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
1194 { /* skip interfaces not up */
1195 return FALSE;
1196 }
1197 *out = *in;
1198 return TRUE;
1199 }
1200
1201 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
1202 private_kernel_netlink_net_t *this, kernel_address_type_t which)
1203 {
1204 address_enumerator_t *data = malloc_thing(address_enumerator_t);
1205 data->this = this;
1206 data->which = which;
1207
1208 this->mutex->lock(this->mutex);
1209 return enumerator_create_nested(
1210 enumerator_create_filter(
1211 this->ifaces->create_enumerator(this->ifaces),
1212 (void*)filter_interfaces, data, NULL),
1213 (void*)create_iface_enumerator, data,
1214 (void*)address_enumerator_destroy);
1215 }
1216
1217 METHOD(kernel_net_t, get_interface_name, bool,
1218 private_kernel_netlink_net_t *this, host_t* ip, char **name)
1219 {
1220 addr_map_entry_t *entry, lookup = {
1221 .ip = ip,
1222 };
1223
1224 if (ip->is_anyaddr(ip))
1225 {
1226 return FALSE;
1227 }
1228 this->mutex->lock(this->mutex);
1229 /* first try to find it on an up and usable interface */
1230 entry = this->addrs->get_match(this->addrs, &lookup,
1231 (void*)addr_map_entry_match_up_and_usable);
1232 if (entry)
1233 {
1234 if (name)
1235 {
1236 *name = strdup(entry->iface->ifname);
1237 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
1238 }
1239 this->mutex->unlock(this->mutex);
1240 return TRUE;
1241 }
1242 /* maybe it is installed on an ignored interface */
1243 entry = this->addrs->get_match(this->addrs, &lookup,
1244 (void*)addr_map_entry_match_up);
1245 if (!entry)
1246 {
1247 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
1248 }
1249 this->mutex->unlock(this->mutex);
1250 return FALSE;
1251 }
1252
1253 /**
1254 * get the index of an interface by name
1255 */
1256 static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
1257 {
1258 iface_entry_t *iface;
1259 int ifindex = 0;
1260
1261 DBG2(DBG_KNL, "getting iface index for %s", name);
1262
1263 this->mutex->lock(this->mutex);
1264 if (this->ifaces->find_first(this->ifaces, (void*)iface_entry_by_name,
1265 (void**)&iface, name) == SUCCESS)
1266 {
1267 ifindex = iface->ifindex;
1268 }
1269 this->mutex->unlock(this->mutex);
1270
1271 if (ifindex == 0)
1272 {
1273 DBG1(DBG_KNL, "unable to get interface index for %s", name);
1274 }
1275 return ifindex;
1276 }
1277
1278 /**
1279 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1280 */
1281 static bool addr_in_subnet(chunk_t addr, chunk_t net, int net_len)
1282 {
1283 static const u_char mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
1284 int byte = 0;
1285
1286 if (net_len == 0)
1287 { /* any address matches a /0 network */
1288 return TRUE;
1289 }
1290 if (addr.len != net.len || net_len > 8 * net.len )
1291 {
1292 return FALSE;
1293 }
1294 /* scan through all bytes in network order */
1295 while (net_len > 0)
1296 {
1297 if (net_len < 8)
1298 {
1299 return (mask[net_len] & addr.ptr[byte]) == (mask[net_len] & net.ptr[byte]);
1300 }
1301 else
1302 {
1303 if (addr.ptr[byte] != net.ptr[byte])
1304 {
1305 return FALSE;
1306 }
1307 byte++;
1308 net_len -= 8;
1309 }
1310 }
1311 return TRUE;
1312 }
1313
1314 /**
1315 * Store information about a route retrieved via RTNETLINK
1316 */
1317 typedef struct {
1318 chunk_t gtw;
1319 chunk_t src;
1320 chunk_t dst;
1321 host_t *src_host;
1322 u_int8_t dst_len;
1323 u_int32_t table;
1324 u_int32_t oif;
1325 } rt_entry_t;
1326
1327 /**
1328 * Free a route entry
1329 */
1330 static void rt_entry_destroy(rt_entry_t *this)
1331 {
1332 DESTROY_IF(this->src_host);
1333 free(this);
1334 }
1335
1336 /**
1337 * Parse route received with RTM_NEWROUTE. The given rt_entry_t object will be
1338 * reused if not NULL.
1339 *
1340 * Returned chunks point to internal data of the Netlink message.
1341 */
1342 static rt_entry_t *parse_route(struct nlmsghdr *hdr, rt_entry_t *route)
1343 {
1344 struct rtattr *rta;
1345 struct rtmsg *msg;
1346 size_t rtasize;
1347
1348 msg = (struct rtmsg*)(NLMSG_DATA(hdr));
1349 rta = RTM_RTA(msg);
1350 rtasize = RTM_PAYLOAD(hdr);
1351
1352 if (route)
1353 {
1354 route->gtw = chunk_empty;
1355 route->src = chunk_empty;
1356 route->dst = chunk_empty;
1357 route->dst_len = msg->rtm_dst_len;
1358 route->table = msg->rtm_table;
1359 route->oif = 0;
1360 }
1361 else
1362 {
1363 INIT(route,
1364 .dst_len = msg->rtm_dst_len,
1365 .table = msg->rtm_table,
1366 );
1367 }
1368
1369 while (RTA_OK(rta, rtasize))
1370 {
1371 switch (rta->rta_type)
1372 {
1373 case RTA_PREFSRC:
1374 route->src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1375 break;
1376 case RTA_GATEWAY:
1377 route->gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1378 break;
1379 case RTA_DST:
1380 route->dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1381 break;
1382 case RTA_OIF:
1383 if (RTA_PAYLOAD(rta) == sizeof(route->oif))
1384 {
1385 route->oif = *(u_int32_t*)RTA_DATA(rta);
1386 }
1387 break;
1388 #ifdef HAVE_RTA_TABLE
1389 case RTA_TABLE:
1390 if (RTA_PAYLOAD(rta) == sizeof(route->table))
1391 {
1392 route->table = *(u_int32_t*)RTA_DATA(rta);
1393 }
1394 break;
1395 #endif /* HAVE_RTA_TABLE*/
1396 }
1397 rta = RTA_NEXT(rta, rtasize);
1398 }
1399 return route;
1400 }
1401
1402 /**
1403 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1404 */
1405 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
1406 bool nexthop, host_t *candidate)
1407 {
1408 netlink_buf_t request;
1409 struct nlmsghdr *hdr, *out, *current;
1410 struct rtmsg *msg;
1411 chunk_t chunk;
1412 size_t len;
1413 linked_list_t *routes;
1414 rt_entry_t *route = NULL, *best = NULL;
1415 enumerator_t *enumerator;
1416 host_t *addr = NULL;
1417
1418 memset(&request, 0, sizeof(request));
1419
1420 hdr = (struct nlmsghdr*)request;
1421 hdr->nlmsg_flags = NLM_F_REQUEST;
1422 if (dest->get_family(dest) == AF_INET || this->rta_prefsrc_for_ipv6 ||
1423 this->routing_table)
1424 { /* kernels prior to 3.0 do not support RTA_PREFSRC for IPv6 routes.
1425 * as we want to ignore routes with virtual IPs we cannot use DUMP
1426 * if these routes are not installed in a separate table */
1427 hdr->nlmsg_flags |= NLM_F_DUMP;
1428 }
1429 hdr->nlmsg_type = RTM_GETROUTE;
1430 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1431
1432 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1433 msg->rtm_family = dest->get_family(dest);
1434 if (candidate)
1435 {
1436 chunk = candidate->get_address(candidate);
1437 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1438 }
1439 chunk = dest->get_address(dest);
1440 netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1441
1442 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
1443 {
1444 DBG2(DBG_KNL, "getting %s to reach %H failed",
1445 nexthop ? "nexthop" : "address", dest);
1446 return NULL;
1447 }
1448 routes = linked_list_create();
1449 this->mutex->lock(this->mutex);
1450
1451 for (current = out; NLMSG_OK(current, len);
1452 current = NLMSG_NEXT(current, len))
1453 {
1454 switch (current->nlmsg_type)
1455 {
1456 case NLMSG_DONE:
1457 break;
1458 case RTM_NEWROUTE:
1459 {
1460 rt_entry_t *other;
1461 uintptr_t table;
1462
1463 route = parse_route(current, route);
1464
1465 table = (uintptr_t)route->table;
1466 if (this->rt_exclude->find_first(this->rt_exclude, NULL,
1467 (void**)&table) == SUCCESS)
1468 { /* route is from an excluded routing table */
1469 continue;
1470 }
1471 if (this->routing_table != 0 &&
1472 route->table == this->routing_table)
1473 { /* route is from our own ipsec routing table */
1474 continue;
1475 }
1476 if (route->oif && !is_interface_up_and_usable(this, route->oif))
1477 { /* interface is down */
1478 continue;
1479 }
1480 if (!addr_in_subnet(chunk, route->dst, route->dst_len))
1481 { /* route destination does not contain dest */
1482 continue;
1483 }
1484 if (route->src.ptr)
1485 { /* verify source address, if any */
1486 host_t *src = host_create_from_chunk(msg->rtm_family,
1487 route->src, 0);
1488 if (src && is_known_vip(this, src))
1489 { /* ignore routes installed by us */
1490 src->destroy(src);
1491 continue;
1492 }
1493 route->src_host = src;
1494 }
1495 /* insert route, sorted by decreasing network prefix */
1496 enumerator = routes->create_enumerator(routes);
1497 while (enumerator->enumerate(enumerator, &other))
1498 {
1499 if (route->dst_len > other->dst_len)
1500 {
1501 break;
1502 }
1503 }
1504 routes->insert_before(routes, enumerator, route);
1505 enumerator->destroy(enumerator);
1506 route = NULL;
1507 continue;
1508 }
1509 default:
1510 continue;
1511 }
1512 break;
1513 }
1514 if (route)
1515 {
1516 rt_entry_destroy(route);
1517 }
1518
1519 /* now we have a list of routes matching dest, sorted by net prefix.
1520 * we will look for source addresses for these routes and select the one
1521 * with the preferred source address, if possible */
1522 enumerator = routes->create_enumerator(routes);
1523 while (enumerator->enumerate(enumerator, &route))
1524 {
1525 if (route->src_host)
1526 { /* got a source address with the route, if no preferred source
1527 * is given or it matches we are done, as this is the best route */
1528 if (!candidate || candidate->ip_equals(candidate, route->src_host))
1529 {
1530 best = route;
1531 break;
1532 }
1533 else if (route->oif)
1534 { /* no match yet, maybe it is assigned to the same interface */
1535 host_t *src = get_interface_address(this, route->oif,
1536 msg->rtm_family, candidate);
1537 if (src && src->ip_equals(src, candidate))
1538 {
1539 route->src_host->destroy(route->src_host);
1540 route->src_host = src;
1541 best = route;
1542 break;
1543 }
1544 DESTROY_IF(src);
1545 }
1546 /* no luck yet with the source address. if this is the best (first)
1547 * route we store it as fallback in case we don't find a route with
1548 * the preferred source */
1549 best = best ?: route;
1550 continue;
1551 }
1552 if (route->oif)
1553 { /* no src, but an interface - get address from it */
1554 route->src_host = get_interface_address(this, route->oif,
1555 msg->rtm_family, candidate);
1556 if (route->src_host)
1557 { /* we handle this address the same as the one above */
1558 if (!candidate ||
1559 candidate->ip_equals(candidate, route->src_host))
1560 {
1561 best = route;
1562 break;
1563 }
1564 best = best ?: route;
1565 continue;
1566 }
1567 }
1568 if (route->gtw.ptr)
1569 { /* no src, no iface, but a gateway - lookup src to reach gtw */
1570 host_t *gtw;
1571
1572 gtw = host_create_from_chunk(msg->rtm_family, route->gtw, 0);
1573 route->src_host = get_route(this, gtw, FALSE, candidate);
1574 gtw->destroy(gtw);
1575 if (route->src_host)
1576 { /* more of the same */
1577 if (!candidate ||
1578 candidate->ip_equals(candidate, route->src_host))
1579 {
1580 best = route;
1581 break;
1582 }
1583 best = best ?: route;
1584 }
1585 }
1586 }
1587 enumerator->destroy(enumerator);
1588
1589 if (nexthop)
1590 { /* nexthop lookup, return gateway if any */
1591 if (best || routes->get_first(routes, (void**)&best) == SUCCESS)
1592 {
1593 addr = host_create_from_chunk(msg->rtm_family, best->gtw, 0);
1594 }
1595 addr = addr ?: dest->clone(dest);
1596 }
1597 else
1598 {
1599 if (best)
1600 {
1601 addr = best->src_host->clone(best->src_host);
1602 }
1603 }
1604 this->mutex->unlock(this->mutex);
1605 routes->destroy_function(routes, (void*)rt_entry_destroy);
1606 free(out);
1607
1608 if (addr)
1609 {
1610 DBG2(DBG_KNL, "using %H as %s to reach %H", addr,
1611 nexthop ? "nexthop" : "address", dest);
1612 }
1613 else
1614 {
1615 DBG2(DBG_KNL, "no %s found to reach %H",
1616 nexthop ? "nexthop" : "address", dest);
1617 }
1618 return addr;
1619 }
1620
1621 METHOD(kernel_net_t, get_source_addr, host_t*,
1622 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1623 {
1624 return get_route(this, dest, FALSE, src);
1625 }
1626
1627 METHOD(kernel_net_t, get_nexthop, host_t*,
1628 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1629 {
1630 return get_route(this, dest, TRUE, src);
1631 }
1632
1633 /**
1634 * Manages the creation and deletion of ip addresses on an interface.
1635 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1636 */
1637 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
1638 int flags, int if_index, host_t *ip)
1639 {
1640 netlink_buf_t request;
1641 struct nlmsghdr *hdr;
1642 struct ifaddrmsg *msg;
1643 chunk_t chunk;
1644
1645 memset(&request, 0, sizeof(request));
1646
1647 chunk = ip->get_address(ip);
1648
1649 hdr = (struct nlmsghdr*)request;
1650 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1651 hdr->nlmsg_type = nlmsg_type;
1652 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1653
1654 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1655 msg->ifa_family = ip->get_family(ip);
1656 msg->ifa_flags = 0;
1657 msg->ifa_prefixlen = 8 * chunk.len;
1658 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1659 msg->ifa_index = if_index;
1660
1661 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1662
1663 return this->socket->send_ack(this->socket, hdr);
1664 }
1665
1666 METHOD(kernel_net_t, add_ip, status_t,
1667 private_kernel_netlink_net_t *this, host_t *virtual_ip, host_t *iface_ip)
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->mutex->lock(this->mutex);
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 mutex). 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->mutex);
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->mutex->unlock(this->mutex);
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 lookup.ip = iface_ip;
1719 entry = this->addrs->get_match(this->addrs, &lookup,
1720 (void*)addr_map_entry_match);
1721 if (!entry)
1722 { /* if we don't find the requested interface we just use the first */
1723 this->ifaces->get_first(this->ifaces, (void**)&iface);
1724 }
1725 else
1726 {
1727 iface = entry->iface;
1728 }
1729 }
1730 if (iface)
1731 {
1732 addr_entry_t *addr;
1733
1734 INIT(addr,
1735 .ip = virtual_ip->clone(virtual_ip),
1736 .refcount = 1,
1737 .scope = RT_SCOPE_UNIVERSE,
1738 );
1739 iface->addrs->insert_last(iface->addrs, addr);
1740 addr_map_entry_add(this->vips, addr, iface);
1741 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1742 iface->ifindex, virtual_ip) == SUCCESS)
1743 {
1744 while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
1745 { /* wait until address appears */
1746 this->condvar->wait(this->condvar, this->mutex);
1747 }
1748 if (entry)
1749 { /* we fail if the interface got deleted in the meantime */
1750 DBG2(DBG_KNL, "virtual IP %H installed on %s", virtual_ip,
1751 entry->iface->ifname);
1752 this->mutex->unlock(this->mutex);
1753 return SUCCESS;
1754 }
1755 }
1756 this->mutex->unlock(this->mutex);
1757 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1758 return FAILED;
1759 }
1760 this->mutex->unlock(this->mutex);
1761 DBG1(DBG_KNL, "no interface available, unable to install virtual IP %H",
1762 virtual_ip);
1763 return FAILED;
1764 }
1765
1766 METHOD(kernel_net_t, del_ip, status_t,
1767 private_kernel_netlink_net_t *this, host_t *virtual_ip)
1768 {
1769 addr_map_entry_t *entry, lookup = {
1770 .ip = virtual_ip,
1771 };
1772
1773 if (!this->install_virtual_ip)
1774 { /* disabled by config */
1775 return SUCCESS;
1776 }
1777
1778 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1779
1780 this->mutex->lock(this->mutex);
1781 entry = this->vips->get_match(this->vips, &lookup,
1782 (void*)addr_map_entry_match);
1783 if (!entry)
1784 { /* we didn't install this IP as virtual IP */
1785 entry = this->addrs->get_match(this->addrs, &lookup,
1786 (void*)addr_map_entry_match);
1787 if (entry)
1788 {
1789 DBG2(DBG_KNL, "not deleting existing IP %H on %s", virtual_ip,
1790 entry->iface->ifname);
1791 this->mutex->unlock(this->mutex);
1792 return SUCCESS;
1793 }
1794 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1795 this->mutex->unlock(this->mutex);
1796 return FAILED;
1797 }
1798 if (entry->addr->refcount == 1)
1799 {
1800 status_t status;
1801
1802 /* we set this flag so that threads calling add_ip will block and wait
1803 * until the entry is gone, also so we can wait below */
1804 entry->addr->installed = FALSE;
1805 status = manage_ipaddr(this, RTM_DELADDR, 0, entry->iface->ifindex,
1806 virtual_ip);
1807 if (status == SUCCESS)
1808 { /* wait until the address is really gone */
1809 while (is_known_vip(this, virtual_ip))
1810 {
1811 this->condvar->wait(this->condvar, this->mutex);
1812 }
1813 }
1814 this->mutex->unlock(this->mutex);
1815 return status;
1816 }
1817 else
1818 {
1819 entry->addr->refcount--;
1820 }
1821 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1822 virtual_ip);
1823 this->mutex->unlock(this->mutex);
1824 return SUCCESS;
1825 }
1826
1827 /**
1828 * Manages source routes in the routing table.
1829 * By setting the appropriate nlmsg_type, the route gets added or removed.
1830 */
1831 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
1832 int nlmsg_type, int flags, chunk_t dst_net,
1833 u_int8_t prefixlen, host_t *gateway,
1834 host_t *src_ip, char *if_name)
1835 {
1836 netlink_buf_t request;
1837 struct nlmsghdr *hdr;
1838 struct rtmsg *msg;
1839 int ifindex;
1840 chunk_t chunk;
1841
1842 /* if route is 0.0.0.0/0, we can't install it, as it would
1843 * overwrite the default route. Instead, we add two routes:
1844 * 0.0.0.0/1 and 128.0.0.0/1 */
1845 if (this->routing_table == 0 && prefixlen == 0)
1846 {
1847 chunk_t half_net;
1848 u_int8_t half_prefixlen;
1849 status_t status;
1850
1851 half_net = chunk_alloca(dst_net.len);
1852 memset(half_net.ptr, 0, half_net.len);
1853 half_prefixlen = 1;
1854
1855 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1856 gateway, src_ip, if_name);
1857 half_net.ptr[0] |= 0x80;
1858 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1859 gateway, src_ip, if_name);
1860 return status;
1861 }
1862
1863 memset(&request, 0, sizeof(request));
1864
1865 hdr = (struct nlmsghdr*)request;
1866 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1867 hdr->nlmsg_type = nlmsg_type;
1868 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1869
1870 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1871 msg->rtm_family = src_ip->get_family(src_ip);
1872 msg->rtm_dst_len = prefixlen;
1873 msg->rtm_table = this->routing_table;
1874 msg->rtm_protocol = RTPROT_STATIC;
1875 msg->rtm_type = RTN_UNICAST;
1876 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1877
1878 netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
1879 chunk = src_ip->get_address(src_ip);
1880 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1881 if (gateway && gateway->get_family(gateway) == src_ip->get_family(src_ip))
1882 {
1883 chunk = gateway->get_address(gateway);
1884 netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1885 }
1886 ifindex = get_interface_index(this, if_name);
1887 chunk.ptr = (char*)&ifindex;
1888 chunk.len = sizeof(ifindex);
1889 netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1890
1891 return this->socket->send_ack(this->socket, hdr);
1892 }
1893
1894 METHOD(kernel_net_t, add_route, status_t,
1895 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1896 host_t *gateway, host_t *src_ip, char *if_name)
1897 {
1898 status_t status;
1899 route_entry_t *found, route = {
1900 .dst_net = dst_net,
1901 .prefixlen = prefixlen,
1902 .gateway = gateway,
1903 .src_ip = src_ip,
1904 .if_name = if_name,
1905 };
1906
1907 this->routes_lock->lock(this->routes_lock);
1908 found = this->routes->get(this->routes, &route);
1909 if (found)
1910 {
1911 this->routes_lock->unlock(this->routes_lock);
1912 return ALREADY_DONE;
1913 }
1914 found = route_entry_clone(&route);
1915 this->routes->put(this->routes, found, found);
1916 status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1917 dst_net, prefixlen, gateway, src_ip, if_name);
1918 this->routes_lock->unlock(this->routes_lock);
1919 return status;
1920 }
1921
1922 METHOD(kernel_net_t, del_route, status_t,
1923 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1924 host_t *gateway, host_t *src_ip, char *if_name)
1925 {
1926 status_t status;
1927 route_entry_t *found, route = {
1928 .dst_net = dst_net,
1929 .prefixlen = prefixlen,
1930 .gateway = gateway,
1931 .src_ip = src_ip,
1932 .if_name = if_name,
1933 };
1934
1935 this->routes_lock->lock(this->routes_lock);
1936 found = this->routes->get(this->routes, &route);
1937 if (!found)
1938 {
1939 this->routes_lock->unlock(this->routes_lock);
1940 return NOT_FOUND;
1941 }
1942 this->routes->remove(this->routes, found);
1943 route_entry_destroy(found);
1944 status = manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
1945 gateway, src_ip, if_name);
1946 this->routes_lock->unlock(this->routes_lock);
1947 return status;
1948 }
1949
1950 /**
1951 * Initialize a list of local addresses.
1952 */
1953 static status_t init_address_list(private_kernel_netlink_net_t *this)
1954 {
1955 netlink_buf_t request;
1956 struct nlmsghdr *out, *current, *in;
1957 struct rtgenmsg *msg;
1958 size_t len;
1959 enumerator_t *ifaces, *addrs;
1960 iface_entry_t *iface;
1961 addr_entry_t *addr;
1962
1963 DBG2(DBG_KNL, "known interfaces and IP addresses:");
1964
1965 memset(&request, 0, sizeof(request));
1966
1967 in = (struct nlmsghdr*)&request;
1968 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1969 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1970 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1971 msg->rtgen_family = AF_UNSPEC;
1972
1973 /* get all links */
1974 in->nlmsg_type = RTM_GETLINK;
1975 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1976 {
1977 return FAILED;
1978 }
1979 current = out;
1980 while (NLMSG_OK(current, len))
1981 {
1982 switch (current->nlmsg_type)
1983 {
1984 case NLMSG_DONE:
1985 break;
1986 case RTM_NEWLINK:
1987 process_link(this, current, FALSE);
1988 /* fall through */
1989 default:
1990 current = NLMSG_NEXT(current, len);
1991 continue;
1992 }
1993 break;
1994 }
1995 free(out);
1996
1997 /* get all interface addresses */
1998 in->nlmsg_type = RTM_GETADDR;
1999 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
2000 {
2001 return FAILED;
2002 }
2003 current = out;
2004 while (NLMSG_OK(current, len))
2005 {
2006 switch (current->nlmsg_type)
2007 {
2008 case NLMSG_DONE:
2009 break;
2010 case RTM_NEWADDR:
2011 process_addr(this, current, FALSE);
2012 /* fall through */
2013 default:
2014 current = NLMSG_NEXT(current, len);
2015 continue;
2016 }
2017 break;
2018 }
2019 free(out);
2020
2021 this->mutex->lock(this->mutex);
2022 ifaces = this->ifaces->create_enumerator(this->ifaces);
2023 while (ifaces->enumerate(ifaces, &iface))
2024 {
2025 if (iface_entry_up_and_usable(iface))
2026 {
2027 DBG2(DBG_KNL, " %s", iface->ifname);
2028 addrs = iface->addrs->create_enumerator(iface->addrs);
2029 while (addrs->enumerate(addrs, (void**)&addr))
2030 {
2031 DBG2(DBG_KNL, " %H", addr->ip);
2032 }
2033 addrs->destroy(addrs);
2034 }
2035 }
2036 ifaces->destroy(ifaces);
2037 this->mutex->unlock(this->mutex);
2038 return SUCCESS;
2039 }
2040
2041 /**
2042 * create or delete a rule to use our routing table
2043 */
2044 static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
2045 int family, u_int32_t table, u_int32_t prio)
2046 {
2047 netlink_buf_t request;
2048 struct nlmsghdr *hdr;
2049 struct rtmsg *msg;
2050 chunk_t chunk;
2051
2052 memset(&request, 0, sizeof(request));
2053 hdr = (struct nlmsghdr*)request;
2054 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2055 hdr->nlmsg_type = nlmsg_type;
2056 if (nlmsg_type == RTM_NEWRULE)
2057 {
2058 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
2059 }
2060 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2061
2062 msg = (struct rtmsg*)NLMSG_DATA(hdr);
2063 msg->rtm_table = table;
2064 msg->rtm_family = family;
2065 msg->rtm_protocol = RTPROT_BOOT;
2066 msg->rtm_scope = RT_SCOPE_UNIVERSE;
2067 msg->rtm_type = RTN_UNICAST;
2068
2069 chunk = chunk_from_thing(prio);
2070 netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
2071
2072 return this->socket->send_ack(this->socket, hdr);
2073 }
2074
2075 /**
2076 * check for kernel features (currently only via version number)
2077 */
2078 static void check_kernel_features(private_kernel_netlink_net_t *this)
2079 {
2080 struct utsname utsname;
2081 int a, b, c;
2082
2083 if (uname(&utsname) == 0)
2084 {
2085 switch(sscanf(utsname.release, "%d.%d.%d", &a, &b, &c))
2086 {
2087 case 3:
2088 if (a == 2)
2089 {
2090 DBG2(DBG_KNL, "detected Linux %d.%d.%d, no support for "
2091 "RTA_PREFSRC for IPv6 routes", a, b, c);
2092 break;
2093 }
2094 /* fall-through */
2095 case 2:
2096 /* only 3.x+ uses two part version numbers */
2097 this->rta_prefsrc_for_ipv6 = TRUE;
2098 break;
2099 default:
2100 break;
2101 }
2102 }
2103 }
2104
2105 /**
2106 * Destroy an address to iface map
2107 */
2108 static void addr_map_destroy(hashtable_t *map)
2109 {
2110 enumerator_t *enumerator;
2111 addr_map_entry_t *addr;
2112
2113 enumerator = map->create_enumerator(map);
2114 while (enumerator->enumerate(enumerator, NULL, (void**)&addr))
2115 {
2116 free(addr);
2117 }
2118 enumerator->destroy(enumerator);
2119 map->destroy(map);
2120 }
2121
2122 METHOD(kernel_net_t, destroy, void,
2123 private_kernel_netlink_net_t *this)
2124 {
2125 enumerator_t *enumerator;
2126 route_entry_t *route;
2127
2128 if (this->routing_table)
2129 {
2130 manage_rule(this, RTM_DELRULE, AF_INET, this->routing_table,
2131 this->routing_table_prio);
2132 manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
2133 this->routing_table_prio);
2134 }
2135 if (this->socket_events > 0)
2136 {
2137 close(this->socket_events);
2138 }
2139 enumerator = this->routes->create_enumerator(this->routes);
2140 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
2141 {
2142 manage_srcroute(this, RTM_DELROUTE, 0, route->dst_net, route->prefixlen,
2143 route->gateway, route->src_ip, route->if_name);
2144 route_entry_destroy(route);
2145 }
2146 enumerator->destroy(enumerator);
2147 this->routes->destroy(this->routes);
2148 this->routes_lock->destroy(this->routes_lock);
2149 DESTROY_IF(this->socket);
2150
2151 net_changes_clear(this);
2152 this->net_changes->destroy(this->net_changes);
2153 this->net_changes_lock->destroy(this->net_changes_lock);
2154
2155 addr_map_destroy(this->addrs);
2156 addr_map_destroy(this->vips);
2157
2158 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
2159 this->rt_exclude->destroy(this->rt_exclude);
2160 this->roam_lock->destroy(this->roam_lock);
2161 this->condvar->destroy(this->condvar);
2162 this->mutex->destroy(this->mutex);
2163 free(this);
2164 }
2165
2166 /*
2167 * Described in header.
2168 */
2169 kernel_netlink_net_t *kernel_netlink_net_create()
2170 {
2171 private_kernel_netlink_net_t *this;
2172 enumerator_t *enumerator;
2173 bool register_for_events = TRUE;
2174 char *exclude;
2175
2176 INIT(this,
2177 .public = {
2178 .interface = {
2179 .get_interface = _get_interface_name,
2180 .create_address_enumerator = _create_address_enumerator,
2181 .get_source_addr = _get_source_addr,
2182 .get_nexthop = _get_nexthop,
2183 .add_ip = _add_ip,
2184 .del_ip = _del_ip,
2185 .add_route = _add_route,
2186 .del_route = _del_route,
2187 .destroy = _destroy,
2188 },
2189 },
2190 .socket = netlink_socket_create(NETLINK_ROUTE),
2191 .rt_exclude = linked_list_create(),
2192 .routes = hashtable_create((hashtable_hash_t)route_entry_hash,
2193 (hashtable_equals_t)route_entry_equals, 16),
2194 .net_changes = hashtable_create(
2195 (hashtable_hash_t)net_change_hash,
2196 (hashtable_equals_t)net_change_equals, 16),
2197 .addrs = hashtable_create(
2198 (hashtable_hash_t)addr_map_entry_hash,
2199 (hashtable_equals_t)addr_map_entry_equals, 16),
2200 .vips = hashtable_create((hashtable_hash_t)addr_map_entry_hash,
2201 (hashtable_equals_t)addr_map_entry_equals, 16),
2202 .routes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
2203 .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
2204 .ifaces = linked_list_create(),
2205 .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
2206 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
2207 .roam_lock = spinlock_create(),
2208 .routing_table = lib->settings->get_int(lib->settings,
2209 "%s.routing_table", ROUTING_TABLE, hydra->daemon),
2210 .routing_table_prio = lib->settings->get_int(lib->settings,
2211 "%s.routing_table_prio", ROUTING_TABLE_PRIO, hydra->daemon),
2212 .process_route = lib->settings->get_bool(lib->settings,
2213 "%s.process_route", TRUE, hydra->daemon),
2214 .install_virtual_ip = lib->settings->get_bool(lib->settings,
2215 "%s.install_virtual_ip", TRUE, hydra->daemon),
2216 .install_virtual_ip_on = lib->settings->get_str(lib->settings,
2217 "%s.install_virtual_ip_on", NULL, hydra->daemon),
2218 );
2219 timerclear(&this->last_route_reinstall);
2220 timerclear(&this->next_roam);
2221
2222 check_kernel_features(this);
2223
2224 if (streq(hydra->daemon, "starter"))
2225 { /* starter has no threads, so we do not register for kernel events */
2226 register_for_events = FALSE;
2227 }
2228
2229 exclude = lib->settings->get_str(lib->settings,
2230 "%s.ignore_routing_tables", NULL, hydra->daemon);
2231 if (exclude)
2232 {
2233 char *token;
2234 uintptr_t table;
2235
2236 enumerator = enumerator_create_token(exclude, " ", " ");
2237 while (enumerator->enumerate(enumerator, &token))
2238 {
2239 errno = 0;
2240 table = strtoul(token, NULL, 10);
2241
2242 if (errno == 0)
2243 {
2244 this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
2245 }
2246 }
2247 enumerator->destroy(enumerator);
2248 }
2249
2250 if (register_for_events)
2251 {
2252 struct sockaddr_nl addr;
2253
2254 memset(&addr, 0, sizeof(addr));
2255 addr.nl_family = AF_NETLINK;
2256
2257 /* create and bind RT socket for events (address/interface/route changes) */
2258 this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2259 if (this->socket_events < 0)
2260 {
2261 DBG1(DBG_KNL, "unable to create RT event socket");
2262 destroy(this);
2263 return NULL;
2264 }
2265 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
2266 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_LINK;
2267 if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
2268 {
2269 DBG1(DBG_KNL, "unable to bind RT event socket");
2270 destroy(this);
2271 return NULL;
2272 }
2273
2274 lib->processor->queue_job(lib->processor,
2275 (job_t*)callback_job_create_with_prio(
2276 (callback_job_cb_t)receive_events, this, NULL,
2277 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
2278 }
2279
2280 if (init_address_list(this) != SUCCESS)
2281 {
2282 DBG1(DBG_KNL, "unable to get interface list");
2283 destroy(this);
2284 return NULL;
2285 }
2286
2287 if (this->routing_table)
2288 {
2289 if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
2290 this->routing_table_prio) != SUCCESS)
2291 {
2292 DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
2293 }
2294 if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
2295 this->routing_table_prio) != SUCCESS)
2296 {
2297 DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
2298 }
2299 }
2300
2301 return &this->public;
2302 }