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