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