Made IP address enumeration more flexible
[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 /** which addresses to enumerate */
1084 kernel_address_type_t which;
1085 } address_enumerator_t;
1086
1087 /**
1088 * cleanup function for address enumerator
1089 */
1090 static void address_enumerator_destroy(address_enumerator_t *data)
1091 {
1092 data->this->mutex->unlock(data->this->mutex);
1093 free(data);
1094 }
1095
1096 /**
1097 * filter for addresses
1098 */
1099 static bool filter_addresses(address_enumerator_t *data,
1100 addr_entry_t** in, host_t** out)
1101 {
1102 if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->virtual)
1103 { /* skip virtual interfaces added by us */
1104 return FALSE;
1105 }
1106 if ((*in)->scope >= RT_SCOPE_LINK)
1107 { /* skip addresses with a unusable scope */
1108 return FALSE;
1109 }
1110 *out = (*in)->ip;
1111 return TRUE;
1112 }
1113
1114 /**
1115 * enumerator constructor for interfaces
1116 */
1117 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
1118 address_enumerator_t *data)
1119 {
1120 return enumerator_create_filter(
1121 iface->addrs->create_enumerator(iface->addrs),
1122 (void*)filter_addresses, data, NULL);
1123 }
1124
1125 /**
1126 * filter for interfaces
1127 */
1128 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
1129 iface_entry_t** out)
1130 {
1131 if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
1132 { /* skip interfaces excluded by config */
1133 return FALSE;
1134 }
1135 if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
1136 { /* ignore loopback devices */
1137 return FALSE;
1138 }
1139 if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
1140 { /* skip interfaces not up */
1141 return FALSE;
1142 }
1143 *out = *in;
1144 return TRUE;
1145 }
1146
1147 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
1148 private_kernel_netlink_net_t *this, kernel_address_type_t which)
1149 {
1150 address_enumerator_t *data = malloc_thing(address_enumerator_t);
1151 data->this = this;
1152 data->which = which;
1153
1154 this->mutex->lock(this->mutex);
1155 return enumerator_create_nested(
1156 enumerator_create_filter(
1157 this->ifaces->create_enumerator(this->ifaces),
1158 (void*)filter_interfaces, data, NULL),
1159 (void*)create_iface_enumerator, data,
1160 (void*)address_enumerator_destroy);
1161 }
1162
1163 METHOD(kernel_net_t, get_interface_name, bool,
1164 private_kernel_netlink_net_t *this, host_t* ip, char **name)
1165 {
1166 addr_map_entry_t *entry, lookup = {
1167 .ip = ip,
1168 };
1169
1170 if (ip->is_anyaddr(ip))
1171 {
1172 return FALSE;
1173 }
1174 this->mutex->lock(this->mutex);
1175 /* first try to find it on an up and usable interface */
1176 entry = this->addrs->get_match(this->addrs, &lookup,
1177 (void*)addr_map_entry_match_up_and_usable);
1178 if (entry)
1179 {
1180 if (name)
1181 {
1182 *name = strdup(entry->iface->ifname);
1183 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
1184 }
1185 this->mutex->unlock(this->mutex);
1186 return TRUE;
1187 }
1188 /* maybe it is installed on an ignored interface */
1189 entry = this->addrs->get_match(this->addrs, &lookup,
1190 (void*)addr_map_entry_match_up);
1191 if (!entry)
1192 {
1193 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
1194 }
1195 this->mutex->unlock(this->mutex);
1196 return FALSE;
1197 }
1198
1199 /**
1200 * get the index of an interface by name
1201 */
1202 static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
1203 {
1204 enumerator_t *ifaces;
1205 iface_entry_t *iface;
1206 int ifindex = 0;
1207
1208 DBG2(DBG_KNL, "getting iface index for %s", name);
1209
1210 this->mutex->lock(this->mutex);
1211 ifaces = this->ifaces->create_enumerator(this->ifaces);
1212 while (ifaces->enumerate(ifaces, &iface))
1213 {
1214 if (streq(name, iface->ifname))
1215 {
1216 ifindex = iface->ifindex;
1217 break;
1218 }
1219 }
1220 ifaces->destroy(ifaces);
1221 this->mutex->unlock(this->mutex);
1222
1223 if (ifindex == 0)
1224 {
1225 DBG1(DBG_KNL, "unable to get interface index for %s", name);
1226 }
1227 return ifindex;
1228 }
1229
1230 /**
1231 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1232 */
1233 static bool addr_in_subnet(chunk_t addr, chunk_t net, int net_len)
1234 {
1235 static const u_char mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
1236 int byte = 0;
1237
1238 if (net_len == 0)
1239 { /* any address matches a /0 network */
1240 return TRUE;
1241 }
1242 if (addr.len != net.len || net_len > 8 * net.len )
1243 {
1244 return FALSE;
1245 }
1246 /* scan through all bytes in network order */
1247 while (net_len > 0)
1248 {
1249 if (net_len < 8)
1250 {
1251 return (mask[net_len] & addr.ptr[byte]) == (mask[net_len] & net.ptr[byte]);
1252 }
1253 else
1254 {
1255 if (addr.ptr[byte] != net.ptr[byte])
1256 {
1257 return FALSE;
1258 }
1259 byte++;
1260 net_len -= 8;
1261 }
1262 }
1263 return TRUE;
1264 }
1265
1266 /**
1267 * Store information about a route retrieved via RTNETLINK
1268 */
1269 typedef struct {
1270 chunk_t gtw;
1271 chunk_t src;
1272 chunk_t dst;
1273 host_t *src_host;
1274 u_int8_t dst_len;
1275 u_int32_t table;
1276 u_int32_t oif;
1277 } rt_entry_t;
1278
1279 /**
1280 * Free a route entry
1281 */
1282 static void rt_entry_destroy(rt_entry_t *this)
1283 {
1284 DESTROY_IF(this->src_host);
1285 free(this);
1286 }
1287
1288 /**
1289 * Parse route received with RTM_NEWROUTE. The given rt_entry_t object will be
1290 * reused if not NULL.
1291 *
1292 * Returned chunks point to internal data of the Netlink message.
1293 */
1294 static rt_entry_t *parse_route(struct nlmsghdr *hdr, rt_entry_t *route)
1295 {
1296 struct rtattr *rta;
1297 struct rtmsg *msg;
1298 size_t rtasize;
1299
1300 msg = (struct rtmsg*)(NLMSG_DATA(hdr));
1301 rta = RTM_RTA(msg);
1302 rtasize = RTM_PAYLOAD(hdr);
1303
1304 if (route)
1305 {
1306 route->gtw = chunk_empty;
1307 route->src = chunk_empty;
1308 route->dst = chunk_empty;
1309 route->dst_len = msg->rtm_dst_len;
1310 route->table = msg->rtm_table;
1311 route->oif = 0;
1312 }
1313 else
1314 {
1315 INIT(route,
1316 .dst_len = msg->rtm_dst_len,
1317 .table = msg->rtm_table,
1318 );
1319 }
1320
1321 while (RTA_OK(rta, rtasize))
1322 {
1323 switch (rta->rta_type)
1324 {
1325 case RTA_PREFSRC:
1326 route->src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1327 break;
1328 case RTA_GATEWAY:
1329 route->gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1330 break;
1331 case RTA_DST:
1332 route->dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1333 break;
1334 case RTA_OIF:
1335 if (RTA_PAYLOAD(rta) == sizeof(route->oif))
1336 {
1337 route->oif = *(u_int32_t*)RTA_DATA(rta);
1338 }
1339 break;
1340 #ifdef HAVE_RTA_TABLE
1341 case RTA_TABLE:
1342 if (RTA_PAYLOAD(rta) == sizeof(route->table))
1343 {
1344 route->table = *(u_int32_t*)RTA_DATA(rta);
1345 }
1346 break;
1347 #endif /* HAVE_RTA_TABLE*/
1348 }
1349 rta = RTA_NEXT(rta, rtasize);
1350 }
1351 return route;
1352 }
1353
1354 /**
1355 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1356 */
1357 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
1358 bool nexthop, host_t *candidate)
1359 {
1360 netlink_buf_t request;
1361 struct nlmsghdr *hdr, *out, *current;
1362 struct rtmsg *msg;
1363 chunk_t chunk;
1364 size_t len;
1365 linked_list_t *routes;
1366 rt_entry_t *route = NULL, *best = NULL;
1367 enumerator_t *enumerator;
1368 host_t *addr = NULL;
1369
1370 memset(&request, 0, sizeof(request));
1371
1372 hdr = (struct nlmsghdr*)request;
1373 hdr->nlmsg_flags = NLM_F_REQUEST;
1374 if (dest->get_family(dest) == AF_INET || this->rta_prefsrc_for_ipv6 ||
1375 this->routing_table)
1376 { /* kernels prior to 3.0 do not support RTA_PREFSRC for IPv6 routes.
1377 * as we want to ignore routes with virtual IPs we cannot use DUMP
1378 * if these routes are not installed in a separate table */
1379 hdr->nlmsg_flags |= NLM_F_DUMP;
1380 }
1381 hdr->nlmsg_type = RTM_GETROUTE;
1382 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1383
1384 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1385 msg->rtm_family = dest->get_family(dest);
1386 if (candidate)
1387 {
1388 chunk = candidate->get_address(candidate);
1389 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1390 }
1391 chunk = dest->get_address(dest);
1392 netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1393
1394 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
1395 {
1396 DBG2(DBG_KNL, "getting %s to reach %H failed",
1397 nexthop ? "nexthop" : "address", dest);
1398 return NULL;
1399 }
1400 routes = linked_list_create();
1401 this->mutex->lock(this->mutex);
1402
1403 for (current = out; NLMSG_OK(current, len);
1404 current = NLMSG_NEXT(current, len))
1405 {
1406 switch (current->nlmsg_type)
1407 {
1408 case NLMSG_DONE:
1409 break;
1410 case RTM_NEWROUTE:
1411 {
1412 rt_entry_t *other;
1413 uintptr_t table;
1414
1415 route = parse_route(current, route);
1416
1417 table = (uintptr_t)route->table;
1418 if (this->rt_exclude->find_first(this->rt_exclude, NULL,
1419 (void**)&table) == SUCCESS)
1420 { /* route is from an excluded routing table */
1421 continue;
1422 }
1423 if (this->routing_table != 0 &&
1424 route->table == this->routing_table)
1425 { /* route is from our own ipsec routing table */
1426 continue;
1427 }
1428 if (route->oif && !is_interface_up_and_usable(this, route->oif))
1429 { /* interface is down */
1430 continue;
1431 }
1432 if (!addr_in_subnet(chunk, route->dst, route->dst_len))
1433 { /* route destination does not contain dest */
1434 continue;
1435 }
1436 if (route->src.ptr)
1437 { /* verify source address, if any */
1438 host_t *src = host_create_from_chunk(msg->rtm_family,
1439 route->src, 0);
1440 if (src && get_vip_refcount(this, src))
1441 { /* ignore routes installed by us */
1442 src->destroy(src);
1443 continue;
1444 }
1445 route->src_host = src;
1446 }
1447 /* insert route, sorted by decreasing network prefix */
1448 enumerator = routes->create_enumerator(routes);
1449 while (enumerator->enumerate(enumerator, &other))
1450 {
1451 if (route->dst_len > other->dst_len)
1452 {
1453 break;
1454 }
1455 }
1456 routes->insert_before(routes, enumerator, route);
1457 enumerator->destroy(enumerator);
1458 route = NULL;
1459 continue;
1460 }
1461 default:
1462 continue;
1463 }
1464 break;
1465 }
1466 if (route)
1467 {
1468 rt_entry_destroy(route);
1469 }
1470
1471 /* now we have a list of routes matching dest, sorted by net prefix.
1472 * we will look for source addresses for these routes and select the one
1473 * with the preferred source address, if possible */
1474 enumerator = routes->create_enumerator(routes);
1475 while (enumerator->enumerate(enumerator, &route))
1476 {
1477 if (route->src_host)
1478 { /* got a source address with the route, if no preferred source
1479 * is given or it matches we are done, as this is the best route */
1480 if (!candidate || candidate->ip_equals(candidate, route->src_host))
1481 {
1482 best = route;
1483 break;
1484 }
1485 else if (route->oif)
1486 { /* no match yet, maybe it is assigned to the same interface */
1487 host_t *src = get_interface_address(this, route->oif,
1488 msg->rtm_family, candidate);
1489 if (src && src->ip_equals(src, candidate))
1490 {
1491 route->src_host->destroy(route->src_host);
1492 route->src_host = src;
1493 best = route;
1494 break;
1495 }
1496 DESTROY_IF(src);
1497 }
1498 /* no luck yet with the source address. if this is the best (first)
1499 * route we store it as fallback in case we don't find a route with
1500 * the preferred source */
1501 best = best ?: route;
1502 continue;
1503 }
1504 if (route->oif)
1505 { /* no src, but an interface - get address from it */
1506 route->src_host = get_interface_address(this, route->oif,
1507 msg->rtm_family, candidate);
1508 if (route->src_host)
1509 { /* we handle this address the same as the one above */
1510 if (!candidate ||
1511 candidate->ip_equals(candidate, route->src_host))
1512 {
1513 best = route;
1514 break;
1515 }
1516 best = best ?: route;
1517 continue;
1518 }
1519 }
1520 if (route->gtw.ptr)
1521 { /* no src, no iface, but a gateway - lookup src to reach gtw */
1522 host_t *gtw;
1523
1524 gtw = host_create_from_chunk(msg->rtm_family, route->gtw, 0);
1525 route->src_host = get_route(this, gtw, FALSE, candidate);
1526 gtw->destroy(gtw);
1527 if (route->src_host)
1528 { /* more of the same */
1529 if (!candidate ||
1530 candidate->ip_equals(candidate, route->src_host))
1531 {
1532 best = route;
1533 break;
1534 }
1535 best = best ?: route;
1536 }
1537 }
1538 }
1539 enumerator->destroy(enumerator);
1540
1541 if (nexthop)
1542 { /* nexthop lookup, return gateway if any */
1543 if (best || routes->get_first(routes, (void**)&best) == SUCCESS)
1544 {
1545 addr = host_create_from_chunk(msg->rtm_family, best->gtw, 0);
1546 }
1547 addr = addr ?: dest->clone(dest);
1548 }
1549 else
1550 {
1551 if (best)
1552 {
1553 addr = best->src_host->clone(best->src_host);
1554 }
1555 }
1556 this->mutex->unlock(this->mutex);
1557 routes->destroy_function(routes, (void*)rt_entry_destroy);
1558 free(out);
1559
1560 if (addr)
1561 {
1562 DBG2(DBG_KNL, "using %H as %s to reach %H", addr,
1563 nexthop ? "nexthop" : "address", dest);
1564 }
1565 else
1566 {
1567 DBG2(DBG_KNL, "no %s found to reach %H",
1568 nexthop ? "nexthop" : "address", dest);
1569 }
1570 return addr;
1571 }
1572
1573 METHOD(kernel_net_t, get_source_addr, host_t*,
1574 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1575 {
1576 return get_route(this, dest, FALSE, src);
1577 }
1578
1579 METHOD(kernel_net_t, get_nexthop, host_t*,
1580 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1581 {
1582 return get_route(this, dest, TRUE, src);
1583 }
1584
1585 /**
1586 * Manages the creation and deletion of ip addresses on an interface.
1587 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1588 */
1589 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
1590 int flags, int if_index, host_t *ip)
1591 {
1592 netlink_buf_t request;
1593 struct nlmsghdr *hdr;
1594 struct ifaddrmsg *msg;
1595 chunk_t chunk;
1596
1597 memset(&request, 0, sizeof(request));
1598
1599 chunk = ip->get_address(ip);
1600
1601 hdr = (struct nlmsghdr*)request;
1602 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1603 hdr->nlmsg_type = nlmsg_type;
1604 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1605
1606 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1607 msg->ifa_family = ip->get_family(ip);
1608 msg->ifa_flags = 0;
1609 msg->ifa_prefixlen = 8 * chunk.len;
1610 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1611 msg->ifa_index = if_index;
1612
1613 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1614
1615 return this->socket->send_ack(this->socket, hdr);
1616 }
1617
1618 METHOD(kernel_net_t, add_ip, status_t,
1619 private_kernel_netlink_net_t *this, host_t *virtual_ip, host_t *iface_ip)
1620 {
1621 iface_entry_t *iface;
1622 addr_entry_t *addr;
1623 enumerator_t *addrs, *ifaces;
1624 int ifindex;
1625
1626 if (!this->install_virtual_ip)
1627 { /* disabled by config */
1628 return SUCCESS;
1629 }
1630
1631 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
1632
1633 this->mutex->lock(this->mutex);
1634 ifaces = this->ifaces->create_enumerator(this->ifaces);
1635 while (ifaces->enumerate(ifaces, &iface))
1636 {
1637 bool iface_found = FALSE;
1638
1639 addrs = iface->addrs->create_enumerator(iface->addrs);
1640 while (addrs->enumerate(addrs, &addr))
1641 {
1642 if (iface_ip->ip_equals(iface_ip, addr->ip))
1643 {
1644 iface_found = TRUE;
1645 }
1646 else if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1647 {
1648 addr->refcount++;
1649 DBG2(DBG_KNL, "virtual IP %H already installed on %s",
1650 virtual_ip, iface->ifname);
1651 addrs->destroy(addrs);
1652 ifaces->destroy(ifaces);
1653 this->mutex->unlock(this->mutex);
1654 return SUCCESS;
1655 }
1656 }
1657 addrs->destroy(addrs);
1658
1659 if (iface_found)
1660 {
1661 ifindex = iface->ifindex;
1662 addr = malloc_thing(addr_entry_t);
1663 addr->ip = virtual_ip->clone(virtual_ip);
1664 addr->refcount = 0;
1665 addr->virtual = TRUE;
1666 addr->scope = RT_SCOPE_UNIVERSE;
1667 iface->addrs->insert_last(iface->addrs, addr);
1668
1669 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1670 ifindex, virtual_ip) == SUCCESS)
1671 {
1672 while (get_vip_refcount(this, virtual_ip) == 0)
1673 { /* wait until address appears */
1674 this->condvar->wait(this->condvar, this->mutex);
1675 }
1676 ifaces->destroy(ifaces);
1677 this->mutex->unlock(this->mutex);
1678 return SUCCESS;
1679 }
1680 ifaces->destroy(ifaces);
1681 this->mutex->unlock(this->mutex);
1682 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1683 return FAILED;
1684 }
1685 }
1686 ifaces->destroy(ifaces);
1687 this->mutex->unlock(this->mutex);
1688
1689 DBG1(DBG_KNL, "interface address %H not found, unable to install"
1690 "virtual IP %H", iface_ip, virtual_ip);
1691 return FAILED;
1692 }
1693
1694 METHOD(kernel_net_t, del_ip, status_t,
1695 private_kernel_netlink_net_t *this, host_t *virtual_ip)
1696 {
1697 iface_entry_t *iface;
1698 addr_entry_t *addr;
1699 enumerator_t *addrs, *ifaces;
1700 status_t status;
1701 int ifindex;
1702
1703 if (!this->install_virtual_ip)
1704 { /* disabled by config */
1705 return SUCCESS;
1706 }
1707
1708 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1709
1710 this->mutex->lock(this->mutex);
1711 ifaces = this->ifaces->create_enumerator(this->ifaces);
1712 while (ifaces->enumerate(ifaces, &iface))
1713 {
1714 addrs = iface->addrs->create_enumerator(iface->addrs);
1715 while (addrs->enumerate(addrs, &addr))
1716 {
1717 if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1718 {
1719 ifindex = iface->ifindex;
1720 if (addr->refcount == 1)
1721 {
1722 status = manage_ipaddr(this, RTM_DELADDR, 0,
1723 ifindex, virtual_ip);
1724 if (status == SUCCESS)
1725 { /* wait until the address is really gone */
1726 while (get_vip_refcount(this, virtual_ip) > 0)
1727 {
1728 this->condvar->wait(this->condvar, this->mutex);
1729 }
1730 }
1731 addrs->destroy(addrs);
1732 ifaces->destroy(ifaces);
1733 this->mutex->unlock(this->mutex);
1734 return status;
1735 }
1736 else
1737 {
1738 addr->refcount--;
1739 }
1740 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1741 virtual_ip);
1742 addrs->destroy(addrs);
1743 ifaces->destroy(ifaces);
1744 this->mutex->unlock(this->mutex);
1745 return SUCCESS;
1746 }
1747 }
1748 addrs->destroy(addrs);
1749 }
1750 ifaces->destroy(ifaces);
1751 this->mutex->unlock(this->mutex);
1752
1753 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1754 return FAILED;
1755 }
1756
1757 /**
1758 * Manages source routes in the routing table.
1759 * By setting the appropriate nlmsg_type, the route gets added or removed.
1760 */
1761 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
1762 int nlmsg_type, int flags, chunk_t dst_net,
1763 u_int8_t prefixlen, host_t *gateway,
1764 host_t *src_ip, char *if_name)
1765 {
1766 netlink_buf_t request;
1767 struct nlmsghdr *hdr;
1768 struct rtmsg *msg;
1769 int ifindex;
1770 chunk_t chunk;
1771
1772 /* if route is 0.0.0.0/0, we can't install it, as it would
1773 * overwrite the default route. Instead, we add two routes:
1774 * 0.0.0.0/1 and 128.0.0.0/1 */
1775 if (this->routing_table == 0 && prefixlen == 0)
1776 {
1777 chunk_t half_net;
1778 u_int8_t half_prefixlen;
1779 status_t status;
1780
1781 half_net = chunk_alloca(dst_net.len);
1782 memset(half_net.ptr, 0, half_net.len);
1783 half_prefixlen = 1;
1784
1785 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1786 gateway, src_ip, if_name);
1787 half_net.ptr[0] |= 0x80;
1788 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1789 gateway, src_ip, if_name);
1790 return status;
1791 }
1792
1793 memset(&request, 0, sizeof(request));
1794
1795 hdr = (struct nlmsghdr*)request;
1796 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1797 hdr->nlmsg_type = nlmsg_type;
1798 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1799
1800 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1801 msg->rtm_family = src_ip->get_family(src_ip);
1802 msg->rtm_dst_len = prefixlen;
1803 msg->rtm_table = this->routing_table;
1804 msg->rtm_protocol = RTPROT_STATIC;
1805 msg->rtm_type = RTN_UNICAST;
1806 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1807
1808 netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
1809 chunk = src_ip->get_address(src_ip);
1810 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1811 if (gateway && gateway->get_family(gateway) == src_ip->get_family(src_ip))
1812 {
1813 chunk = gateway->get_address(gateway);
1814 netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1815 }
1816 ifindex = get_interface_index(this, if_name);
1817 chunk.ptr = (char*)&ifindex;
1818 chunk.len = sizeof(ifindex);
1819 netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1820
1821 return this->socket->send_ack(this->socket, hdr);
1822 }
1823
1824 METHOD(kernel_net_t, add_route, status_t,
1825 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1826 host_t *gateway, host_t *src_ip, char *if_name)
1827 {
1828 status_t status;
1829 route_entry_t *found, route = {
1830 .dst_net = dst_net,
1831 .prefixlen = prefixlen,
1832 .gateway = gateway,
1833 .src_ip = src_ip,
1834 .if_name = if_name,
1835 };
1836
1837 this->mutex->lock(this->mutex);
1838 found = this->routes->get(this->routes, &route);
1839 if (found)
1840 {
1841 this->mutex->unlock(this->mutex);
1842 return ALREADY_DONE;
1843 }
1844 found = route_entry_clone(&route);
1845 this->routes->put(this->routes, found, found);
1846 status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1847 dst_net, prefixlen, gateway, src_ip, if_name);
1848 this->mutex->unlock(this->mutex);
1849 return status;
1850 }
1851
1852 METHOD(kernel_net_t, del_route, status_t,
1853 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1854 host_t *gateway, host_t *src_ip, char *if_name)
1855 {
1856 status_t status;
1857 route_entry_t *found, route = {
1858 .dst_net = dst_net,
1859 .prefixlen = prefixlen,
1860 .gateway = gateway,
1861 .src_ip = src_ip,
1862 .if_name = if_name,
1863 };
1864
1865 this->mutex->lock(this->mutex);
1866 found = this->routes->get(this->routes, &route);
1867 if (!found)
1868 {
1869 this->mutex->unlock(this->mutex);
1870 return NOT_FOUND;
1871 }
1872 this->routes->remove(this->routes, found);
1873 route_entry_destroy(found);
1874 status = manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
1875 gateway, src_ip, if_name);
1876 this->mutex->unlock(this->mutex);
1877 return status;
1878 }
1879
1880 /**
1881 * Initialize a list of local addresses.
1882 */
1883 static status_t init_address_list(private_kernel_netlink_net_t *this)
1884 {
1885 netlink_buf_t request;
1886 struct nlmsghdr *out, *current, *in;
1887 struct rtgenmsg *msg;
1888 size_t len;
1889 enumerator_t *ifaces, *addrs;
1890 iface_entry_t *iface;
1891 addr_entry_t *addr;
1892
1893 DBG2(DBG_KNL, "known interfaces and IP addresses:");
1894
1895 memset(&request, 0, sizeof(request));
1896
1897 in = (struct nlmsghdr*)&request;
1898 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1899 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1900 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1901 msg->rtgen_family = AF_UNSPEC;
1902
1903 /* get all links */
1904 in->nlmsg_type = RTM_GETLINK;
1905 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1906 {
1907 return FAILED;
1908 }
1909 current = out;
1910 while (NLMSG_OK(current, len))
1911 {
1912 switch (current->nlmsg_type)
1913 {
1914 case NLMSG_DONE:
1915 break;
1916 case RTM_NEWLINK:
1917 process_link(this, current, FALSE);
1918 /* fall through */
1919 default:
1920 current = NLMSG_NEXT(current, len);
1921 continue;
1922 }
1923 break;
1924 }
1925 free(out);
1926
1927 /* get all interface addresses */
1928 in->nlmsg_type = RTM_GETADDR;
1929 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1930 {
1931 return FAILED;
1932 }
1933 current = out;
1934 while (NLMSG_OK(current, len))
1935 {
1936 switch (current->nlmsg_type)
1937 {
1938 case NLMSG_DONE:
1939 break;
1940 case RTM_NEWADDR:
1941 process_addr(this, current, FALSE);
1942 /* fall through */
1943 default:
1944 current = NLMSG_NEXT(current, len);
1945 continue;
1946 }
1947 break;
1948 }
1949 free(out);
1950
1951 this->mutex->lock(this->mutex);
1952 ifaces = this->ifaces->create_enumerator(this->ifaces);
1953 while (ifaces->enumerate(ifaces, &iface))
1954 {
1955 if (iface_entry_up_and_usable(iface))
1956 {
1957 DBG2(DBG_KNL, " %s", iface->ifname);
1958 addrs = iface->addrs->create_enumerator(iface->addrs);
1959 while (addrs->enumerate(addrs, (void**)&addr))
1960 {
1961 DBG2(DBG_KNL, " %H", addr->ip);
1962 }
1963 addrs->destroy(addrs);
1964 }
1965 }
1966 ifaces->destroy(ifaces);
1967 this->mutex->unlock(this->mutex);
1968 return SUCCESS;
1969 }
1970
1971 /**
1972 * create or delete a rule to use our routing table
1973 */
1974 static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
1975 int family, u_int32_t table, u_int32_t prio)
1976 {
1977 netlink_buf_t request;
1978 struct nlmsghdr *hdr;
1979 struct rtmsg *msg;
1980 chunk_t chunk;
1981
1982 memset(&request, 0, sizeof(request));
1983 hdr = (struct nlmsghdr*)request;
1984 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1985 hdr->nlmsg_type = nlmsg_type;
1986 if (nlmsg_type == RTM_NEWRULE)
1987 {
1988 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
1989 }
1990 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1991
1992 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1993 msg->rtm_table = table;
1994 msg->rtm_family = family;
1995 msg->rtm_protocol = RTPROT_BOOT;
1996 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1997 msg->rtm_type = RTN_UNICAST;
1998
1999 chunk = chunk_from_thing(prio);
2000 netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
2001
2002 return this->socket->send_ack(this->socket, hdr);
2003 }
2004
2005 /**
2006 * check for kernel features (currently only via version number)
2007 */
2008 static void check_kernel_features(private_kernel_netlink_net_t *this)
2009 {
2010 struct utsname utsname;
2011 int a, b, c;
2012
2013 if (uname(&utsname) == 0)
2014 {
2015 switch(sscanf(utsname.release, "%d.%d.%d", &a, &b, &c))
2016 {
2017 case 3:
2018 if (a == 2)
2019 {
2020 DBG2(DBG_KNL, "detected Linux %d.%d.%d, no support for "
2021 "RTA_PREFSRC for IPv6 routes", a, b, c);
2022 break;
2023 }
2024 /* fall-through */
2025 case 2:
2026 /* only 3.x+ uses two part version numbers */
2027 this->rta_prefsrc_for_ipv6 = TRUE;
2028 break;
2029 default:
2030 break;
2031 }
2032 }
2033 }
2034
2035 METHOD(kernel_net_t, destroy, void,
2036 private_kernel_netlink_net_t *this)
2037 {
2038 enumerator_t *enumerator;
2039 route_entry_t *route;
2040 addr_map_entry_t *addr;
2041
2042 if (this->routing_table)
2043 {
2044 manage_rule(this, RTM_DELRULE, AF_INET, this->routing_table,
2045 this->routing_table_prio);
2046 manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
2047 this->routing_table_prio);
2048 }
2049 if (this->socket_events > 0)
2050 {
2051 close(this->socket_events);
2052 }
2053 enumerator = this->routes->create_enumerator(this->routes);
2054 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
2055 {
2056 manage_srcroute(this, RTM_DELROUTE, 0, route->dst_net, route->prefixlen,
2057 route->gateway, route->src_ip, route->if_name);
2058 route_entry_destroy(route);
2059 }
2060 enumerator->destroy(enumerator);
2061 this->routes->destroy(this->routes);
2062 DESTROY_IF(this->socket);
2063
2064 net_changes_clear(this);
2065 this->net_changes->destroy(this->net_changes);
2066 this->net_changes_lock->destroy(this->net_changes_lock);
2067
2068 enumerator = this->addrs->create_enumerator(this->addrs);
2069 while (enumerator->enumerate(enumerator, NULL, (void**)&addr))
2070 {
2071 free(addr);
2072 }
2073 enumerator->destroy(enumerator);
2074 this->addrs->destroy(this->addrs);
2075
2076 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
2077 this->rt_exclude->destroy(this->rt_exclude);
2078 this->condvar->destroy(this->condvar);
2079 this->mutex->destroy(this->mutex);
2080 free(this);
2081 }
2082
2083 /*
2084 * Described in header.
2085 */
2086 kernel_netlink_net_t *kernel_netlink_net_create()
2087 {
2088 private_kernel_netlink_net_t *this;
2089 enumerator_t *enumerator;
2090 bool register_for_events = TRUE;
2091 char *exclude;
2092
2093 INIT(this,
2094 .public = {
2095 .interface = {
2096 .get_interface = _get_interface_name,
2097 .create_address_enumerator = _create_address_enumerator,
2098 .get_source_addr = _get_source_addr,
2099 .get_nexthop = _get_nexthop,
2100 .add_ip = _add_ip,
2101 .del_ip = _del_ip,
2102 .add_route = _add_route,
2103 .del_route = _del_route,
2104 .destroy = _destroy,
2105 },
2106 },
2107 .socket = netlink_socket_create(NETLINK_ROUTE),
2108 .rt_exclude = linked_list_create(),
2109 .routes = hashtable_create((hashtable_hash_t)route_entry_hash,
2110 (hashtable_equals_t)route_entry_equals, 16),
2111 .net_changes = hashtable_create(
2112 (hashtable_hash_t)net_change_hash,
2113 (hashtable_equals_t)net_change_equals, 16),
2114 .addrs = hashtable_create(
2115 (hashtable_hash_t)addr_map_entry_hash,
2116 (hashtable_equals_t)addr_map_entry_equals, 16),
2117 .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
2118 .ifaces = linked_list_create(),
2119 .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
2120 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
2121 .routing_table = lib->settings->get_int(lib->settings,
2122 "%s.routing_table", ROUTING_TABLE, hydra->daemon),
2123 .routing_table_prio = lib->settings->get_int(lib->settings,
2124 "%s.routing_table_prio", ROUTING_TABLE_PRIO, hydra->daemon),
2125 .process_route = lib->settings->get_bool(lib->settings,
2126 "%s.process_route", TRUE, hydra->daemon),
2127 .install_virtual_ip = lib->settings->get_bool(lib->settings,
2128 "%s.install_virtual_ip", TRUE, hydra->daemon),
2129 );
2130 timerclear(&this->last_route_reinstall);
2131 timerclear(&this->last_roam);
2132
2133 check_kernel_features(this);
2134
2135 if (streq(hydra->daemon, "starter"))
2136 { /* starter has no threads, so we do not register for kernel events */
2137 register_for_events = FALSE;
2138 }
2139
2140 exclude = lib->settings->get_str(lib->settings,
2141 "%s.ignore_routing_tables", NULL, hydra->daemon);
2142 if (exclude)
2143 {
2144 char *token;
2145 uintptr_t table;
2146
2147 enumerator = enumerator_create_token(exclude, " ", " ");
2148 while (enumerator->enumerate(enumerator, &token))
2149 {
2150 errno = 0;
2151 table = strtoul(token, NULL, 10);
2152
2153 if (errno == 0)
2154 {
2155 this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
2156 }
2157 }
2158 enumerator->destroy(enumerator);
2159 }
2160
2161 if (register_for_events)
2162 {
2163 struct sockaddr_nl addr;
2164
2165 memset(&addr, 0, sizeof(addr));
2166 addr.nl_family = AF_NETLINK;
2167
2168 /* create and bind RT socket for events (address/interface/route changes) */
2169 this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2170 if (this->socket_events < 0)
2171 {
2172 DBG1(DBG_KNL, "unable to create RT event socket");
2173 destroy(this);
2174 return NULL;
2175 }
2176 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
2177 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_LINK;
2178 if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
2179 {
2180 DBG1(DBG_KNL, "unable to bind RT event socket");
2181 destroy(this);
2182 return NULL;
2183 }
2184
2185 lib->processor->queue_job(lib->processor,
2186 (job_t*)callback_job_create_with_prio(
2187 (callback_job_cb_t)receive_events, this, NULL,
2188 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
2189 }
2190
2191 if (init_address_list(this) != SUCCESS)
2192 {
2193 DBG1(DBG_KNL, "unable to get interface list");
2194 destroy(this);
2195 return NULL;
2196 }
2197
2198 if (this->routing_table)
2199 {
2200 if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
2201 this->routing_table_prio) != SUCCESS)
2202 {
2203 DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
2204 }
2205 if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
2206 this->routing_table_prio) != SUCCESS)
2207 {
2208 DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
2209 }
2210 }
2211
2212 return &this->public;
2213 }