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