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