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