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