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