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