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