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