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