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