d845a0f02342b7143ce135aaa42de31f03184f4a
[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 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1134 */
1135 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
1136 bool nexthop, host_t *candidate)
1137 {
1138 netlink_buf_t request;
1139 struct nlmsghdr *hdr, *out, *current;
1140 struct rtmsg *msg;
1141 chunk_t chunk;
1142 size_t len;
1143 int best = -1;
1144 enumerator_t *enumerator;
1145 host_t *src = NULL, *gtw = NULL;
1146
1147 DBG2(DBG_KNL, "getting address to reach %H", dest);
1148
1149 memset(&request, 0, sizeof(request));
1150
1151 hdr = (struct nlmsghdr*)request;
1152 hdr->nlmsg_flags = NLM_F_REQUEST;
1153 if (dest->get_family(dest) == AF_INET || this->rta_prefsrc_for_ipv6 ||
1154 this->routing_table)
1155 { /* kernels prior to 3.0 do not support RTA_PREFSRC for IPv6 routes.
1156 * as we want to ignore routes with virtual IPs we cannot use DUMP
1157 * if these routes are not installed in a separate table */
1158 hdr->nlmsg_flags |= NLM_F_DUMP;
1159 }
1160 hdr->nlmsg_type = RTM_GETROUTE;
1161 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1162
1163 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1164 msg->rtm_family = dest->get_family(dest);
1165 if (candidate)
1166 {
1167 chunk = candidate->get_address(candidate);
1168 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1169 }
1170 chunk = dest->get_address(dest);
1171 netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1172
1173 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
1174 {
1175 DBG1(DBG_KNL, "getting address to %H failed", dest);
1176 return NULL;
1177 }
1178 this->mutex->lock(this->mutex);
1179
1180 for (current = out; NLMSG_OK(current, len);
1181 current = NLMSG_NEXT(current, len))
1182 {
1183 if (!nexthop && candidate && src && src->ip_equals(src, candidate))
1184 { /* if we found a route that includes our preferred source address
1185 * we stop looking for any other routes. this is mainly for the
1186 * DUMP cases as there the RTA_PREFSRC attribute has no effect */
1187 break;
1188 }
1189
1190 switch (current->nlmsg_type)
1191 {
1192 case NLMSG_DONE:
1193 break;
1194 case RTM_NEWROUTE:
1195 {
1196 struct rtattr *rta;
1197 size_t rtasize;
1198 chunk_t rta_gtw, rta_src, rta_dst;
1199 u_int32_t rta_oif = 0, rta_table;
1200 host_t *new_src, *new_gtw;
1201 bool cont = FALSE;
1202 uintptr_t table;
1203
1204 rta_gtw = rta_src = rta_dst = chunk_empty;
1205 msg = (struct rtmsg*)(NLMSG_DATA(current));
1206 rta = RTM_RTA(msg);
1207 rtasize = RTM_PAYLOAD(current);
1208 rta_table = msg->rtm_table;
1209 while (RTA_OK(rta, rtasize))
1210 {
1211 switch (rta->rta_type)
1212 {
1213 case RTA_PREFSRC:
1214 rta_src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1215 break;
1216 case RTA_GATEWAY:
1217 rta_gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1218 break;
1219 case RTA_DST:
1220 rta_dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1221 break;
1222 case RTA_OIF:
1223 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
1224 {
1225 rta_oif = *(u_int32_t*)RTA_DATA(rta);
1226 }
1227 break;
1228 #ifdef HAVE_RTA_TABLE
1229 case RTA_TABLE:
1230 if (RTA_PAYLOAD(rta) == sizeof(rta_table))
1231 {
1232 rta_table = *(u_int32_t*)RTA_DATA(rta);
1233 }
1234 break;
1235 #endif /* HAVE_RTA_TABLE*/
1236 }
1237 rta = RTA_NEXT(rta, rtasize);
1238 }
1239 if (msg->rtm_dst_len <= best)
1240 { /* not better than a previous one */
1241 continue;
1242 }
1243 enumerator = this->rt_exclude->create_enumerator(this->rt_exclude);
1244 while (enumerator->enumerate(enumerator, &table))
1245 {
1246 if (table == rta_table)
1247 {
1248 cont = TRUE;
1249 break;
1250 }
1251 }
1252 enumerator->destroy(enumerator);
1253 if (cont)
1254 {
1255 continue;
1256 }
1257 if (this->routing_table != 0 &&
1258 rta_table == this->routing_table)
1259 { /* route is from our own ipsec routing table */
1260 continue;
1261 }
1262 if (rta_oif && !is_interface_up(this, rta_oif))
1263 { /* interface is down */
1264 continue;
1265 }
1266 if (!addr_in_subnet(chunk, rta_dst, msg->rtm_dst_len))
1267 { /* route destination does not contain dest */
1268 continue;
1269 }
1270
1271 if (nexthop)
1272 {
1273 /* nexthop lookup, return gateway if any */
1274 DESTROY_IF(gtw);
1275 gtw = host_create_from_chunk(msg->rtm_family, rta_gtw, 0);
1276 best = msg->rtm_dst_len;
1277 continue;
1278 }
1279 if (rta_src.ptr)
1280 { /* got a source address */
1281 new_src = host_create_from_chunk(msg->rtm_family, rta_src, 0);
1282 if (new_src)
1283 {
1284 if (get_vip_refcount(this, new_src))
1285 { /* skip source address if it is installed by us */
1286 new_src->destroy(new_src);
1287 }
1288 else
1289 {
1290 DESTROY_IF(src);
1291 src = new_src;
1292 best = msg->rtm_dst_len;
1293 }
1294 }
1295 continue;
1296 }
1297 if (rta_oif)
1298 { /* no src or gtw, but an interface. Get address from it. */
1299 new_src = get_interface_address(this, rta_oif,
1300 msg->rtm_family, candidate);
1301 if (new_src)
1302 {
1303 DESTROY_IF(src);
1304 src = new_src;
1305 best = msg->rtm_dst_len;
1306 }
1307 continue;
1308 }
1309 if (rta_gtw.ptr)
1310 { /* no source, but a gateway. Lookup source to reach gtw. */
1311 new_gtw = host_create_from_chunk(msg->rtm_family, rta_gtw, 0);
1312 new_src = get_route(this, new_gtw, FALSE, candidate);
1313 new_gtw->destroy(new_gtw);
1314 if (new_src)
1315 {
1316 DESTROY_IF(src);
1317 src = new_src;
1318 best = msg->rtm_dst_len;
1319 }
1320 continue;
1321 }
1322 continue;
1323 }
1324 default:
1325 continue;
1326 }
1327 break;
1328 }
1329 free(out);
1330 this->mutex->unlock(this->mutex);
1331
1332 if (nexthop)
1333 {
1334 if (gtw)
1335 {
1336 return gtw;
1337 }
1338 return dest->clone(dest);
1339 }
1340 return src;
1341 }
1342
1343 METHOD(kernel_net_t, get_source_addr, host_t*,
1344 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1345 {
1346 return get_route(this, dest, FALSE, src);
1347 }
1348
1349 METHOD(kernel_net_t, get_nexthop, host_t*,
1350 private_kernel_netlink_net_t *this, host_t *dest)
1351 {
1352 return get_route(this, dest, TRUE, NULL);
1353 }
1354
1355 /**
1356 * Manages the creation and deletion of ip addresses on an interface.
1357 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1358 */
1359 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
1360 int flags, int if_index, host_t *ip)
1361 {
1362 netlink_buf_t request;
1363 struct nlmsghdr *hdr;
1364 struct ifaddrmsg *msg;
1365 chunk_t chunk;
1366
1367 memset(&request, 0, sizeof(request));
1368
1369 chunk = ip->get_address(ip);
1370
1371 hdr = (struct nlmsghdr*)request;
1372 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1373 hdr->nlmsg_type = nlmsg_type;
1374 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1375
1376 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1377 msg->ifa_family = ip->get_family(ip);
1378 msg->ifa_flags = 0;
1379 msg->ifa_prefixlen = 8 * chunk.len;
1380 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1381 msg->ifa_index = if_index;
1382
1383 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1384
1385 return this->socket->send_ack(this->socket, hdr);
1386 }
1387
1388 METHOD(kernel_net_t, add_ip, status_t,
1389 private_kernel_netlink_net_t *this, host_t *virtual_ip, host_t *iface_ip)
1390 {
1391 iface_entry_t *iface;
1392 addr_entry_t *addr;
1393 enumerator_t *addrs, *ifaces;
1394 int ifindex;
1395
1396 if (!this->install_virtual_ip)
1397 { /* disabled by config */
1398 return SUCCESS;
1399 }
1400
1401 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
1402
1403 this->mutex->lock(this->mutex);
1404 ifaces = this->ifaces->create_enumerator(this->ifaces);
1405 while (ifaces->enumerate(ifaces, &iface))
1406 {
1407 bool iface_found = FALSE;
1408
1409 addrs = iface->addrs->create_enumerator(iface->addrs);
1410 while (addrs->enumerate(addrs, &addr))
1411 {
1412 if (iface_ip->ip_equals(iface_ip, addr->ip))
1413 {
1414 iface_found = TRUE;
1415 }
1416 else if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1417 {
1418 addr->refcount++;
1419 DBG2(DBG_KNL, "virtual IP %H already installed on %s",
1420 virtual_ip, iface->ifname);
1421 addrs->destroy(addrs);
1422 ifaces->destroy(ifaces);
1423 this->mutex->unlock(this->mutex);
1424 return SUCCESS;
1425 }
1426 }
1427 addrs->destroy(addrs);
1428
1429 if (iface_found)
1430 {
1431 ifindex = iface->ifindex;
1432 addr = malloc_thing(addr_entry_t);
1433 addr->ip = virtual_ip->clone(virtual_ip);
1434 addr->refcount = 0;
1435 addr->virtual = TRUE;
1436 addr->scope = RT_SCOPE_UNIVERSE;
1437 iface->addrs->insert_last(iface->addrs, addr);
1438
1439 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1440 ifindex, virtual_ip) == SUCCESS)
1441 {
1442 while (get_vip_refcount(this, virtual_ip) == 0)
1443 { /* wait until address appears */
1444 this->condvar->wait(this->condvar, this->mutex);
1445 }
1446 ifaces->destroy(ifaces);
1447 this->mutex->unlock(this->mutex);
1448 return SUCCESS;
1449 }
1450 ifaces->destroy(ifaces);
1451 this->mutex->unlock(this->mutex);
1452 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1453 return FAILED;
1454 }
1455 }
1456 ifaces->destroy(ifaces);
1457 this->mutex->unlock(this->mutex);
1458
1459 DBG1(DBG_KNL, "interface address %H not found, unable to install"
1460 "virtual IP %H", iface_ip, virtual_ip);
1461 return FAILED;
1462 }
1463
1464 METHOD(kernel_net_t, del_ip, status_t,
1465 private_kernel_netlink_net_t *this, host_t *virtual_ip)
1466 {
1467 iface_entry_t *iface;
1468 addr_entry_t *addr;
1469 enumerator_t *addrs, *ifaces;
1470 status_t status;
1471 int ifindex;
1472
1473 if (!this->install_virtual_ip)
1474 { /* disabled by config */
1475 return SUCCESS;
1476 }
1477
1478 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1479
1480 this->mutex->lock(this->mutex);
1481 ifaces = this->ifaces->create_enumerator(this->ifaces);
1482 while (ifaces->enumerate(ifaces, &iface))
1483 {
1484 addrs = iface->addrs->create_enumerator(iface->addrs);
1485 while (addrs->enumerate(addrs, &addr))
1486 {
1487 if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1488 {
1489 ifindex = iface->ifindex;
1490 if (addr->refcount == 1)
1491 {
1492 status = manage_ipaddr(this, RTM_DELADDR, 0,
1493 ifindex, virtual_ip);
1494 if (status == SUCCESS)
1495 { /* wait until the address is really gone */
1496 while (get_vip_refcount(this, virtual_ip) > 0)
1497 {
1498 this->condvar->wait(this->condvar, this->mutex);
1499 }
1500 }
1501 addrs->destroy(addrs);
1502 ifaces->destroy(ifaces);
1503 this->mutex->unlock(this->mutex);
1504 return status;
1505 }
1506 else
1507 {
1508 addr->refcount--;
1509 }
1510 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1511 virtual_ip);
1512 addrs->destroy(addrs);
1513 ifaces->destroy(ifaces);
1514 this->mutex->unlock(this->mutex);
1515 return SUCCESS;
1516 }
1517 }
1518 addrs->destroy(addrs);
1519 }
1520 ifaces->destroy(ifaces);
1521 this->mutex->unlock(this->mutex);
1522
1523 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1524 return FAILED;
1525 }
1526
1527 /**
1528 * Manages source routes in the routing table.
1529 * By setting the appropriate nlmsg_type, the route gets added or removed.
1530 */
1531 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
1532 int nlmsg_type, int flags, chunk_t dst_net,
1533 u_int8_t prefixlen, host_t *gateway,
1534 host_t *src_ip, char *if_name)
1535 {
1536 netlink_buf_t request;
1537 struct nlmsghdr *hdr;
1538 struct rtmsg *msg;
1539 int ifindex;
1540 chunk_t chunk;
1541
1542 /* if route is 0.0.0.0/0, we can't install it, as it would
1543 * overwrite the default route. Instead, we add two routes:
1544 * 0.0.0.0/1 and 128.0.0.0/1 */
1545 if (this->routing_table == 0 && prefixlen == 0)
1546 {
1547 chunk_t half_net;
1548 u_int8_t half_prefixlen;
1549 status_t status;
1550
1551 half_net = chunk_alloca(dst_net.len);
1552 memset(half_net.ptr, 0, half_net.len);
1553 half_prefixlen = 1;
1554
1555 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1556 gateway, src_ip, if_name);
1557 half_net.ptr[0] |= 0x80;
1558 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1559 gateway, src_ip, if_name);
1560 return status;
1561 }
1562
1563 memset(&request, 0, sizeof(request));
1564
1565 hdr = (struct nlmsghdr*)request;
1566 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1567 hdr->nlmsg_type = nlmsg_type;
1568 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1569
1570 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1571 msg->rtm_family = src_ip->get_family(src_ip);
1572 msg->rtm_dst_len = prefixlen;
1573 msg->rtm_table = this->routing_table;
1574 msg->rtm_protocol = RTPROT_STATIC;
1575 msg->rtm_type = RTN_UNICAST;
1576 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1577
1578 netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
1579 chunk = src_ip->get_address(src_ip);
1580 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1581 if (gateway && gateway->get_family(gateway) == src_ip->get_family(src_ip))
1582 {
1583 chunk = gateway->get_address(gateway);
1584 netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1585 }
1586 ifindex = get_interface_index(this, if_name);
1587 chunk.ptr = (char*)&ifindex;
1588 chunk.len = sizeof(ifindex);
1589 netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1590
1591 return this->socket->send_ack(this->socket, hdr);
1592 }
1593
1594 METHOD(kernel_net_t, add_route, status_t,
1595 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1596 host_t *gateway, host_t *src_ip, char *if_name)
1597 {
1598 status_t status;
1599 route_entry_t *found, route = {
1600 .dst_net = dst_net,
1601 .prefixlen = prefixlen,
1602 .gateway = gateway,
1603 .src_ip = src_ip,
1604 .if_name = if_name,
1605 };
1606
1607 this->mutex->lock(this->mutex);
1608 found = this->routes->get(this->routes, &route);
1609 if (found)
1610 {
1611 this->mutex->unlock(this->mutex);
1612 return ALREADY_DONE;
1613 }
1614 found = route_entry_clone(&route);
1615 this->routes->put(this->routes, found, found);
1616 status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1617 dst_net, prefixlen, gateway, src_ip, if_name);
1618 this->mutex->unlock(this->mutex);
1619 return status;
1620 }
1621
1622 METHOD(kernel_net_t, del_route, status_t,
1623 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1624 host_t *gateway, host_t *src_ip, char *if_name)
1625 {
1626 status_t status;
1627 route_entry_t *found, route = {
1628 .dst_net = dst_net,
1629 .prefixlen = prefixlen,
1630 .gateway = gateway,
1631 .src_ip = src_ip,
1632 .if_name = if_name,
1633 };
1634
1635 this->mutex->lock(this->mutex);
1636 found = this->routes->get(this->routes, &route);
1637 if (!found)
1638 {
1639 this->mutex->unlock(this->mutex);
1640 return NOT_FOUND;
1641 }
1642 this->routes->remove(this->routes, found);
1643 route_entry_destroy(found);
1644 status = manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
1645 gateway, src_ip, if_name);
1646 this->mutex->unlock(this->mutex);
1647 return status;
1648 }
1649
1650 /**
1651 * Initialize a list of local addresses.
1652 */
1653 static status_t init_address_list(private_kernel_netlink_net_t *this)
1654 {
1655 netlink_buf_t request;
1656 struct nlmsghdr *out, *current, *in;
1657 struct rtgenmsg *msg;
1658 size_t len;
1659 enumerator_t *ifaces, *addrs;
1660 iface_entry_t *iface;
1661 addr_entry_t *addr;
1662
1663 DBG2(DBG_KNL, "known interfaces and IP addresses:");
1664
1665 memset(&request, 0, sizeof(request));
1666
1667 in = (struct nlmsghdr*)&request;
1668 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1669 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1670 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1671 msg->rtgen_family = AF_UNSPEC;
1672
1673 /* get all links */
1674 in->nlmsg_type = RTM_GETLINK;
1675 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1676 {
1677 return FAILED;
1678 }
1679 current = out;
1680 while (NLMSG_OK(current, len))
1681 {
1682 switch (current->nlmsg_type)
1683 {
1684 case NLMSG_DONE:
1685 break;
1686 case RTM_NEWLINK:
1687 process_link(this, current, FALSE);
1688 /* fall through */
1689 default:
1690 current = NLMSG_NEXT(current, len);
1691 continue;
1692 }
1693 break;
1694 }
1695 free(out);
1696
1697 /* get all interface addresses */
1698 in->nlmsg_type = RTM_GETADDR;
1699 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1700 {
1701 return FAILED;
1702 }
1703 current = out;
1704 while (NLMSG_OK(current, len))
1705 {
1706 switch (current->nlmsg_type)
1707 {
1708 case NLMSG_DONE:
1709 break;
1710 case RTM_NEWADDR:
1711 process_addr(this, current, FALSE);
1712 /* fall through */
1713 default:
1714 current = NLMSG_NEXT(current, len);
1715 continue;
1716 }
1717 break;
1718 }
1719 free(out);
1720
1721 this->mutex->lock(this->mutex);
1722 ifaces = this->ifaces->create_enumerator(this->ifaces);
1723 while (ifaces->enumerate(ifaces, &iface))
1724 {
1725 if (iface->flags & IFF_UP)
1726 {
1727 DBG2(DBG_KNL, " %s", iface->ifname);
1728 addrs = iface->addrs->create_enumerator(iface->addrs);
1729 while (addrs->enumerate(addrs, (void**)&addr))
1730 {
1731 DBG2(DBG_KNL, " %H", addr->ip);
1732 }
1733 addrs->destroy(addrs);
1734 }
1735 }
1736 ifaces->destroy(ifaces);
1737 this->mutex->unlock(this->mutex);
1738 return SUCCESS;
1739 }
1740
1741 /**
1742 * create or delete a rule to use our routing table
1743 */
1744 static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
1745 int family, u_int32_t table, u_int32_t prio)
1746 {
1747 netlink_buf_t request;
1748 struct nlmsghdr *hdr;
1749 struct rtmsg *msg;
1750 chunk_t chunk;
1751
1752 memset(&request, 0, sizeof(request));
1753 hdr = (struct nlmsghdr*)request;
1754 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1755 hdr->nlmsg_type = nlmsg_type;
1756 if (nlmsg_type == RTM_NEWRULE)
1757 {
1758 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
1759 }
1760 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1761
1762 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1763 msg->rtm_table = table;
1764 msg->rtm_family = family;
1765 msg->rtm_protocol = RTPROT_BOOT;
1766 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1767 msg->rtm_type = RTN_UNICAST;
1768
1769 chunk = chunk_from_thing(prio);
1770 netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
1771
1772 return this->socket->send_ack(this->socket, hdr);
1773 }
1774
1775 /**
1776 * check for kernel features (currently only via version number)
1777 */
1778 static void check_kernel_features(private_kernel_netlink_net_t *this)
1779 {
1780 struct utsname utsname;
1781 int a, b, c;
1782
1783 if (uname(&utsname) == 0)
1784 {
1785 switch(sscanf(utsname.release, "%d.%d.%d", &a, &b, &c))
1786 {
1787 case 3:
1788 if (a == 2)
1789 {
1790 DBG2(DBG_KNL, "detected Linux %d.%d.%d, no support for "
1791 "RTA_PREFSRC for IPv6 routes", a, b, c);
1792 break;
1793 }
1794 /* fall-through */
1795 case 2:
1796 /* only 3.x+ uses two part version numbers */
1797 this->rta_prefsrc_for_ipv6 = TRUE;
1798 break;
1799 default:
1800 break;
1801 }
1802 }
1803 }
1804
1805 METHOD(kernel_net_t, destroy, void,
1806 private_kernel_netlink_net_t *this)
1807 {
1808 enumerator_t *enumerator;
1809 route_entry_t *route;
1810
1811 if (this->routing_table)
1812 {
1813 manage_rule(this, RTM_DELRULE, AF_INET, this->routing_table,
1814 this->routing_table_prio);
1815 manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
1816 this->routing_table_prio);
1817 }
1818 if (this->socket_events > 0)
1819 {
1820 close(this->socket_events);
1821 }
1822 enumerator = this->routes->create_enumerator(this->routes);
1823 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
1824 {
1825 manage_srcroute(this, RTM_DELROUTE, 0, route->dst_net, route->prefixlen,
1826 route->gateway, route->src_ip, route->if_name);
1827 route_entry_destroy(route);
1828 }
1829 enumerator->destroy(enumerator);
1830 this->routes->destroy(this->routes);
1831 DESTROY_IF(this->socket);
1832
1833 net_changes_clear(this);
1834 this->net_changes->destroy(this->net_changes);
1835 this->net_changes_lock->destroy(this->net_changes_lock);
1836
1837 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
1838 this->rt_exclude->destroy(this->rt_exclude);
1839 this->condvar->destroy(this->condvar);
1840 this->mutex->destroy(this->mutex);
1841 free(this);
1842 }
1843
1844 /*
1845 * Described in header.
1846 */
1847 kernel_netlink_net_t *kernel_netlink_net_create()
1848 {
1849 private_kernel_netlink_net_t *this;
1850 enumerator_t *enumerator;
1851 bool register_for_events = TRUE;
1852 char *exclude;
1853
1854 INIT(this,
1855 .public = {
1856 .interface = {
1857 .get_interface = _get_interface_name,
1858 .create_address_enumerator = _create_address_enumerator,
1859 .get_source_addr = _get_source_addr,
1860 .get_nexthop = _get_nexthop,
1861 .add_ip = _add_ip,
1862 .del_ip = _del_ip,
1863 .add_route = _add_route,
1864 .del_route = _del_route,
1865 .destroy = _destroy,
1866 },
1867 },
1868 .socket = netlink_socket_create(NETLINK_ROUTE),
1869 .rt_exclude = linked_list_create(),
1870 .routes = hashtable_create((hashtable_hash_t)route_entry_hash,
1871 (hashtable_equals_t)route_entry_equals, 16),
1872 .net_changes = hashtable_create(
1873 (hashtable_hash_t)net_change_hash,
1874 (hashtable_equals_t)net_change_equals, 16),
1875 .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
1876 .ifaces = linked_list_create(),
1877 .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
1878 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
1879 .routing_table = lib->settings->get_int(lib->settings,
1880 "%s.routing_table", ROUTING_TABLE, hydra->daemon),
1881 .routing_table_prio = lib->settings->get_int(lib->settings,
1882 "%s.routing_table_prio", ROUTING_TABLE_PRIO, hydra->daemon),
1883 .process_route = lib->settings->get_bool(lib->settings,
1884 "%s.process_route", TRUE, hydra->daemon),
1885 .install_virtual_ip = lib->settings->get_bool(lib->settings,
1886 "%s.install_virtual_ip", TRUE, hydra->daemon),
1887 );
1888 timerclear(&this->last_route_reinstall);
1889 timerclear(&this->last_roam);
1890
1891 check_kernel_features(this);
1892
1893 if (streq(hydra->daemon, "starter"))
1894 { /* starter has no threads, so we do not register for kernel events */
1895 register_for_events = FALSE;
1896 }
1897
1898 exclude = lib->settings->get_str(lib->settings,
1899 "%s.ignore_routing_tables", NULL, hydra->daemon);
1900 if (exclude)
1901 {
1902 char *token;
1903 uintptr_t table;
1904
1905 enumerator = enumerator_create_token(exclude, " ", " ");
1906 while (enumerator->enumerate(enumerator, &token))
1907 {
1908 errno = 0;
1909 table = strtoul(token, NULL, 10);
1910
1911 if (errno == 0)
1912 {
1913 this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
1914 }
1915 }
1916 enumerator->destroy(enumerator);
1917 }
1918
1919 if (register_for_events)
1920 {
1921 struct sockaddr_nl addr;
1922
1923 memset(&addr, 0, sizeof(addr));
1924 addr.nl_family = AF_NETLINK;
1925
1926 /* create and bind RT socket for events (address/interface/route changes) */
1927 this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1928 if (this->socket_events < 0)
1929 {
1930 DBG1(DBG_KNL, "unable to create RT event socket");
1931 destroy(this);
1932 return NULL;
1933 }
1934 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
1935 RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_LINK;
1936 if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
1937 {
1938 DBG1(DBG_KNL, "unable to bind RT event socket");
1939 destroy(this);
1940 return NULL;
1941 }
1942
1943 lib->processor->queue_job(lib->processor,
1944 (job_t*)callback_job_create_with_prio(
1945 (callback_job_cb_t)receive_events, this, NULL,
1946 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
1947 }
1948
1949 if (init_address_list(this) != SUCCESS)
1950 {
1951 DBG1(DBG_KNL, "unable to get interface list");
1952 destroy(this);
1953 return NULL;
1954 }
1955
1956 if (this->routing_table)
1957 {
1958 if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
1959 this->routing_table_prio) != SUCCESS)
1960 {
1961 DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
1962 }
1963 if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
1964 this->routing_table_prio) != SUCCESS)
1965 {
1966 DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
1967 }
1968 }
1969
1970 return &this->public;
1971 }