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