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