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