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