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