21965754102be52d1b708a2cfeb2f0a196849759
[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 enumerator_t *ifaces, *addrs;
197 iface_entry_t *iface;
198 addr_entry_t *addr;
199 int refcount = 0;
200
201 ifaces = this->ifaces->create_enumerator(this->ifaces);
202 while (ifaces->enumerate(ifaces, (void**)&iface))
203 {
204 addrs = iface->addrs->create_enumerator(iface->addrs);
205 while (addrs->enumerate(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 strncpy(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 or in the local routing table (local addrs) */
526 if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
527 msg->rtm_table == RT_TABLE_LOCAL))
528 {
529 return;
530 }
531
532 while (RTA_OK(rta, rtasize))
533 {
534 switch (rta->rta_type)
535 {
536 case RTA_PREFSRC:
537 DESTROY_IF(host);
538 host = host_create_from_chunk(msg->rtm_family,
539 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)), 0);
540 break;
541 case RTA_OIF:
542 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
543 {
544 rta_oif = *(u_int32_t*)RTA_DATA(rta);
545 }
546 break;
547 }
548 rta = RTA_NEXT(rta, rtasize);
549 }
550 if (!host && rta_oif)
551 {
552 host = get_interface_address(this, rta_oif, msg->rtm_family);
553 }
554 if (host)
555 {
556 this->mutex->lock(this->mutex);
557 if (!get_vip_refcount(this, host))
558 { /* ignore routes added for virtual IPs */
559 fire_roam_event(this, FALSE);
560 }
561 this->mutex->unlock(this->mutex);
562 host->destroy(host);
563 }
564 }
565
566 /**
567 * Receives events from kernel
568 */
569 static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
570 {
571 char response[1024];
572 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
573 struct sockaddr_nl addr;
574 socklen_t addr_len = sizeof(addr);
575 int len;
576 bool oldstate;
577
578 oldstate = thread_cancelability(TRUE);
579 len = recvfrom(this->socket_events, response, sizeof(response), 0,
580 (struct sockaddr*)&addr, &addr_len);
581 thread_cancelability(oldstate);
582
583 if (len < 0)
584 {
585 switch (errno)
586 {
587 case EINTR:
588 /* interrupted, try again */
589 return JOB_REQUEUE_DIRECT;
590 case EAGAIN:
591 /* no data ready, select again */
592 return JOB_REQUEUE_DIRECT;
593 default:
594 DBG1(DBG_KNL, "unable to receive from rt event socket");
595 sleep(1);
596 return JOB_REQUEUE_FAIR;
597 }
598 }
599
600 if (addr.nl_pid != 0)
601 { /* not from kernel. not interested, try another one */
602 return JOB_REQUEUE_DIRECT;
603 }
604
605 while (NLMSG_OK(hdr, len))
606 {
607 /* looks good so far, dispatch netlink message */
608 switch (hdr->nlmsg_type)
609 {
610 case RTM_NEWADDR:
611 case RTM_DELADDR:
612 process_addr(this, hdr, TRUE);
613 this->condvar->broadcast(this->condvar);
614 break;
615 case RTM_NEWLINK:
616 case RTM_DELLINK:
617 process_link(this, hdr, TRUE);
618 this->condvar->broadcast(this->condvar);
619 break;
620 case RTM_NEWROUTE:
621 case RTM_DELROUTE:
622 if (this->process_route)
623 {
624 process_route(this, hdr);
625 }
626 break;
627 default:
628 break;
629 }
630 hdr = NLMSG_NEXT(hdr, len);
631 }
632 return JOB_REQUEUE_DIRECT;
633 }
634
635 /** enumerator over addresses */
636 typedef struct {
637 private_kernel_netlink_net_t* this;
638 /** whether to enumerate down interfaces */
639 bool include_down_ifaces;
640 /** whether to enumerate virtual ip addresses */
641 bool include_virtual_ips;
642 } address_enumerator_t;
643
644 /**
645 * cleanup function for address enumerator
646 */
647 static void address_enumerator_destroy(address_enumerator_t *data)
648 {
649 data->this->mutex->unlock(data->this->mutex);
650 free(data);
651 }
652
653 /**
654 * filter for addresses
655 */
656 static bool filter_addresses(address_enumerator_t *data,
657 addr_entry_t** in, host_t** out)
658 {
659 if (!data->include_virtual_ips && (*in)->virtual)
660 { /* skip virtual interfaces added by us */
661 return FALSE;
662 }
663 if ((*in)->scope >= RT_SCOPE_LINK)
664 { /* skip addresses with a unusable scope */
665 return FALSE;
666 }
667 *out = (*in)->ip;
668 return TRUE;
669 }
670
671 /**
672 * enumerator constructor for interfaces
673 */
674 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
675 address_enumerator_t *data)
676 {
677 return enumerator_create_filter(
678 iface->addrs->create_enumerator(iface->addrs),
679 (void*)filter_addresses, data, NULL);
680 }
681
682 /**
683 * filter for interfaces
684 */
685 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
686 iface_entry_t** out)
687 {
688 if (!data->include_down_ifaces && !((*in)->flags & IFF_UP))
689 { /* skip interfaces not up */
690 return FALSE;
691 }
692 *out = *in;
693 return TRUE;
694 }
695
696 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
697 private_kernel_netlink_net_t *this,
698 bool include_down_ifaces, bool include_virtual_ips)
699 {
700 address_enumerator_t *data = malloc_thing(address_enumerator_t);
701 data->this = this;
702 data->include_down_ifaces = include_down_ifaces;
703 data->include_virtual_ips = include_virtual_ips;
704
705 this->mutex->lock(this->mutex);
706 return enumerator_create_nested(
707 enumerator_create_filter(
708 this->ifaces->create_enumerator(this->ifaces),
709 (void*)filter_interfaces, data, NULL),
710 (void*)create_iface_enumerator, data,
711 (void*)address_enumerator_destroy);
712 }
713
714 METHOD(kernel_net_t, get_interface_name, char*,
715 private_kernel_netlink_net_t *this, host_t* ip)
716 {
717 enumerator_t *ifaces, *addrs;
718 iface_entry_t *iface;
719 addr_entry_t *addr;
720 char *name = NULL;
721
722 DBG2(DBG_KNL, "getting interface name for %H", ip);
723
724 this->mutex->lock(this->mutex);
725 ifaces = this->ifaces->create_enumerator(this->ifaces);
726 while (ifaces->enumerate(ifaces, &iface))
727 {
728 addrs = iface->addrs->create_enumerator(iface->addrs);
729 while (addrs->enumerate(addrs, &addr))
730 {
731 if (ip->ip_equals(ip, addr->ip))
732 {
733 name = strdup(iface->ifname);
734 break;
735 }
736 }
737 addrs->destroy(addrs);
738 if (name)
739 {
740 break;
741 }
742 }
743 ifaces->destroy(ifaces);
744 this->mutex->unlock(this->mutex);
745
746 if (name)
747 {
748 DBG2(DBG_KNL, "%H is on interface %s", ip, name);
749 }
750 else
751 {
752 DBG2(DBG_KNL, "%H is not a local address", ip);
753 }
754 return name;
755 }
756
757 /**
758 * get the index of an interface by name
759 */
760 static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
761 {
762 enumerator_t *ifaces;
763 iface_entry_t *iface;
764 int ifindex = 0;
765
766 DBG2(DBG_KNL, "getting iface index for %s", name);
767
768 this->mutex->lock(this->mutex);
769 ifaces = this->ifaces->create_enumerator(this->ifaces);
770 while (ifaces->enumerate(ifaces, &iface))
771 {
772 if (streq(name, iface->ifname))
773 {
774 ifindex = iface->ifindex;
775 break;
776 }
777 }
778 ifaces->destroy(ifaces);
779 this->mutex->unlock(this->mutex);
780
781 if (ifindex == 0)
782 {
783 DBG1(DBG_KNL, "unable to get interface index for %s", name);
784 }
785 return ifindex;
786 }
787
788 /**
789 * Check if an interface with a given index is up
790 */
791 static bool is_interface_up(private_kernel_netlink_net_t *this, int index)
792 {
793 enumerator_t *ifaces;
794 iface_entry_t *iface;
795 /* default to TRUE for interface we do not monitor (e.g. lo) */
796 bool up = TRUE;
797
798 ifaces = this->ifaces->create_enumerator(this->ifaces);
799 while (ifaces->enumerate(ifaces, &iface))
800 {
801 if (iface->ifindex == index)
802 {
803 up = iface->flags & IFF_UP;
804 break;
805 }
806 }
807 ifaces->destroy(ifaces);
808 return up;
809 }
810
811 /**
812 * check if an address (chunk) addr is in subnet (net with net_len net bits)
813 */
814 static bool addr_in_subnet(chunk_t addr, chunk_t net, int net_len)
815 {
816 static const u_char mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
817 int byte = 0;
818
819 if (net_len == 0)
820 { /* any address matches a /0 network */
821 return TRUE;
822 }
823 if (addr.len != net.len || net_len > 8 * net.len )
824 {
825 return FALSE;
826 }
827 /* scan through all bytes in network order */
828 while (net_len > 0)
829 {
830 if (net_len < 8)
831 {
832 return (mask[net_len] & addr.ptr[byte]) == (mask[net_len] & net.ptr[byte]);
833 }
834 else
835 {
836 if (addr.ptr[byte] != net.ptr[byte])
837 {
838 return FALSE;
839 }
840 byte++;
841 net_len -= 8;
842 }
843 }
844 return TRUE;
845 }
846
847 /**
848 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
849 */
850 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
851 bool nexthop, host_t *candidate)
852 {
853 netlink_buf_t request;
854 struct nlmsghdr *hdr, *out, *current;
855 struct rtmsg *msg;
856 chunk_t chunk;
857 size_t len;
858 int best = -1;
859 enumerator_t *enumerator;
860 host_t *src = NULL, *gtw = NULL;
861
862 DBG2(DBG_KNL, "getting address to reach %H", dest);
863
864 memset(&request, 0, sizeof(request));
865
866 hdr = (struct nlmsghdr*)request;
867 hdr->nlmsg_flags = NLM_F_REQUEST;
868 if (dest->get_family(dest) == AF_INET)
869 {
870 /* We dump all addresses for IPv4, as we want to ignore IPsec specific
871 * routes installed by us. But the kernel does not return source
872 * addresses in a IPv6 dump, so fall back to get() for v6 routes. */
873 hdr->nlmsg_flags |= NLM_F_ROOT | NLM_F_DUMP;
874 }
875 hdr->nlmsg_type = RTM_GETROUTE;
876 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
877
878 msg = (struct rtmsg*)NLMSG_DATA(hdr);
879 msg->rtm_family = dest->get_family(dest);
880 if (candidate)
881 {
882 chunk = candidate->get_address(candidate);
883 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
884 }
885 chunk = dest->get_address(dest);
886 netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
887
888 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
889 {
890 DBG1(DBG_KNL, "getting address to %H failed", dest);
891 return NULL;
892 }
893 this->mutex->lock(this->mutex);
894
895 for (current = out; NLMSG_OK(current, len);
896 current = NLMSG_NEXT(current, len))
897 {
898 switch (current->nlmsg_type)
899 {
900 case NLMSG_DONE:
901 break;
902 case RTM_NEWROUTE:
903 {
904 struct rtattr *rta;
905 size_t rtasize;
906 chunk_t rta_gtw, rta_src, rta_dst;
907 u_int32_t rta_oif = 0, rta_table;
908 host_t *new_src, *new_gtw;
909 bool cont = FALSE;
910 uintptr_t table;
911
912 rta_gtw = rta_src = rta_dst = chunk_empty;
913 msg = (struct rtmsg*)(NLMSG_DATA(current));
914 rta = RTM_RTA(msg);
915 rtasize = RTM_PAYLOAD(current);
916 rta_table = msg->rtm_table;
917 while (RTA_OK(rta, rtasize))
918 {
919 switch (rta->rta_type)
920 {
921 case RTA_PREFSRC:
922 rta_src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
923 break;
924 case RTA_GATEWAY:
925 rta_gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
926 break;
927 case RTA_DST:
928 rta_dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
929 break;
930 case RTA_OIF:
931 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
932 {
933 rta_oif = *(u_int32_t*)RTA_DATA(rta);
934 }
935 break;
936 #ifdef HAVE_RTA_TABLE
937 case RTA_TABLE:
938 if (RTA_PAYLOAD(rta) == sizeof(rta_table))
939 {
940 rta_table = *(u_int32_t*)RTA_DATA(rta);
941 }
942 break;
943 #endif /* HAVE_RTA_TABLE*/
944 }
945 rta = RTA_NEXT(rta, rtasize);
946 }
947 if (msg->rtm_dst_len <= best)
948 { /* not better than a previous one */
949 continue;
950 }
951 enumerator = this->rt_exclude->create_enumerator(this->rt_exclude);
952 while (enumerator->enumerate(enumerator, &table))
953 {
954 if (table == rta_table)
955 {
956 cont = TRUE;
957 break;
958 }
959 }
960 enumerator->destroy(enumerator);
961 if (cont)
962 {
963 continue;
964 }
965 if (this->routing_table != 0 &&
966 rta_table == this->routing_table)
967 { /* route is from our own ipsec routing table */
968 continue;
969 }
970 if (rta_oif && !is_interface_up(this, rta_oif))
971 { /* interface is down */
972 continue;
973 }
974 if (!addr_in_subnet(chunk, rta_dst, msg->rtm_dst_len))
975 { /* route destination does not contain dest */
976 continue;
977 }
978
979 if (nexthop)
980 {
981 /* nexthop lookup, return gateway if any */
982 DESTROY_IF(gtw);
983 gtw = host_create_from_chunk(msg->rtm_family, rta_gtw, 0);
984 best = msg->rtm_dst_len;
985 continue;
986 }
987 if (rta_src.ptr)
988 { /* got a source address */
989 new_src = host_create_from_chunk(msg->rtm_family, rta_src, 0);
990 if (new_src)
991 {
992 if (get_vip_refcount(this, new_src))
993 { /* skip source address if it is installed by us */
994 new_src->destroy(new_src);
995 }
996 else
997 {
998 DESTROY_IF(src);
999 src = new_src;
1000 best = msg->rtm_dst_len;
1001 }
1002 }
1003 continue;
1004 }
1005 if (rta_oif)
1006 { /* no src or gtw, but an interface. Get address from it. */
1007 new_src = get_interface_address(this, rta_oif,
1008 msg->rtm_family);
1009 if (new_src)
1010 {
1011 DESTROY_IF(src);
1012 src = new_src;
1013 best = msg->rtm_dst_len;
1014 }
1015 continue;
1016 }
1017 if (rta_gtw.ptr)
1018 { /* no source, but a gateway. Lookup source to reach gtw. */
1019 new_gtw = host_create_from_chunk(msg->rtm_family, rta_gtw, 0);
1020 new_src = get_route(this, new_gtw, FALSE, candidate);
1021 new_gtw->destroy(new_gtw);
1022 if (new_src)
1023 {
1024 DESTROY_IF(src);
1025 src = new_src;
1026 best = msg->rtm_dst_len;
1027 }
1028 continue;
1029 }
1030 continue;
1031 }
1032 default:
1033 continue;
1034 }
1035 break;
1036 }
1037 free(out);
1038 this->mutex->unlock(this->mutex);
1039
1040 if (nexthop)
1041 {
1042 if (gtw)
1043 {
1044 return gtw;
1045 }
1046 return dest->clone(dest);
1047 }
1048 return src;
1049 }
1050
1051 METHOD(kernel_net_t, get_source_addr, host_t*,
1052 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
1053 {
1054 return get_route(this, dest, FALSE, src);
1055 }
1056
1057 METHOD(kernel_net_t, get_nexthop, host_t*,
1058 private_kernel_netlink_net_t *this, host_t *dest)
1059 {
1060 return get_route(this, dest, TRUE, NULL);
1061 }
1062
1063 /**
1064 * Manages the creation and deletion of ip addresses on an interface.
1065 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1066 */
1067 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
1068 int flags, int if_index, host_t *ip)
1069 {
1070 netlink_buf_t request;
1071 struct nlmsghdr *hdr;
1072 struct ifaddrmsg *msg;
1073 chunk_t chunk;
1074
1075 memset(&request, 0, sizeof(request));
1076
1077 chunk = ip->get_address(ip);
1078
1079 hdr = (struct nlmsghdr*)request;
1080 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1081 hdr->nlmsg_type = nlmsg_type;
1082 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1083
1084 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1085 msg->ifa_family = ip->get_family(ip);
1086 msg->ifa_flags = 0;
1087 msg->ifa_prefixlen = 8 * chunk.len;
1088 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1089 msg->ifa_index = if_index;
1090
1091 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1092
1093 return this->socket->send_ack(this->socket, hdr);
1094 }
1095
1096 METHOD(kernel_net_t, add_ip, status_t,
1097 private_kernel_netlink_net_t *this, host_t *virtual_ip, host_t *iface_ip)
1098 {
1099 iface_entry_t *iface;
1100 addr_entry_t *addr;
1101 enumerator_t *addrs, *ifaces;
1102 int ifindex;
1103
1104 if (!this->install_virtual_ip)
1105 { /* disabled by config */
1106 return SUCCESS;
1107 }
1108
1109 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
1110
1111 this->mutex->lock(this->mutex);
1112 ifaces = this->ifaces->create_enumerator(this->ifaces);
1113 while (ifaces->enumerate(ifaces, &iface))
1114 {
1115 bool iface_found = FALSE;
1116
1117 addrs = iface->addrs->create_enumerator(iface->addrs);
1118 while (addrs->enumerate(addrs, &addr))
1119 {
1120 if (iface_ip->ip_equals(iface_ip, addr->ip))
1121 {
1122 iface_found = TRUE;
1123 }
1124 else if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1125 {
1126 addr->refcount++;
1127 DBG2(DBG_KNL, "virtual IP %H already installed on %s",
1128 virtual_ip, iface->ifname);
1129 addrs->destroy(addrs);
1130 ifaces->destroy(ifaces);
1131 this->mutex->unlock(this->mutex);
1132 return SUCCESS;
1133 }
1134 }
1135 addrs->destroy(addrs);
1136
1137 if (iface_found)
1138 {
1139 ifindex = iface->ifindex;
1140 addr = malloc_thing(addr_entry_t);
1141 addr->ip = virtual_ip->clone(virtual_ip);
1142 addr->refcount = 0;
1143 addr->virtual = TRUE;
1144 addr->scope = RT_SCOPE_UNIVERSE;
1145 iface->addrs->insert_last(iface->addrs, addr);
1146
1147 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1148 ifindex, virtual_ip) == SUCCESS)
1149 {
1150 while (get_vip_refcount(this, virtual_ip) == 0)
1151 { /* wait until address appears */
1152 this->condvar->wait(this->condvar, this->mutex);
1153 }
1154 ifaces->destroy(ifaces);
1155 this->mutex->unlock(this->mutex);
1156 return SUCCESS;
1157 }
1158 ifaces->destroy(ifaces);
1159 this->mutex->unlock(this->mutex);
1160 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1161 return FAILED;
1162 }
1163 }
1164 ifaces->destroy(ifaces);
1165 this->mutex->unlock(this->mutex);
1166
1167 DBG1(DBG_KNL, "interface address %H not found, unable to install"
1168 "virtual IP %H", iface_ip, virtual_ip);
1169 return FAILED;
1170 }
1171
1172 METHOD(kernel_net_t, del_ip, status_t,
1173 private_kernel_netlink_net_t *this, host_t *virtual_ip)
1174 {
1175 iface_entry_t *iface;
1176 addr_entry_t *addr;
1177 enumerator_t *addrs, *ifaces;
1178 status_t status;
1179 int ifindex;
1180
1181 if (!this->install_virtual_ip)
1182 { /* disabled by config */
1183 return SUCCESS;
1184 }
1185
1186 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1187
1188 this->mutex->lock(this->mutex);
1189 ifaces = this->ifaces->create_enumerator(this->ifaces);
1190 while (ifaces->enumerate(ifaces, &iface))
1191 {
1192 addrs = iface->addrs->create_enumerator(iface->addrs);
1193 while (addrs->enumerate(addrs, &addr))
1194 {
1195 if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1196 {
1197 ifindex = iface->ifindex;
1198 if (addr->refcount == 1)
1199 {
1200 status = manage_ipaddr(this, RTM_DELADDR, 0,
1201 ifindex, virtual_ip);
1202 if (status == SUCCESS)
1203 { /* wait until the address is really gone */
1204 while (get_vip_refcount(this, virtual_ip) > 0)
1205 {
1206 this->condvar->wait(this->condvar, this->mutex);
1207 }
1208 }
1209 addrs->destroy(addrs);
1210 ifaces->destroy(ifaces);
1211 this->mutex->unlock(this->mutex);
1212 return status;
1213 }
1214 else
1215 {
1216 addr->refcount--;
1217 }
1218 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1219 virtual_ip);
1220 addrs->destroy(addrs);
1221 ifaces->destroy(ifaces);
1222 this->mutex->unlock(this->mutex);
1223 return SUCCESS;
1224 }
1225 }
1226 addrs->destroy(addrs);
1227 }
1228 ifaces->destroy(ifaces);
1229 this->mutex->unlock(this->mutex);
1230
1231 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1232 return FAILED;
1233 }
1234
1235 /**
1236 * Manages source routes in the routing table.
1237 * By setting the appropriate nlmsg_type, the route gets added or removed.
1238 */
1239 static status_t manage_srcroute(private_kernel_netlink_net_t *this, int nlmsg_type,
1240 int flags, chunk_t dst_net, u_int8_t prefixlen,
1241 host_t *gateway, host_t *src_ip, char *if_name)
1242 {
1243 netlink_buf_t request;
1244 struct nlmsghdr *hdr;
1245 struct rtmsg *msg;
1246 int ifindex;
1247 chunk_t chunk;
1248
1249 /* if route is 0.0.0.0/0, we can't install it, as it would
1250 * overwrite the default route. Instead, we add two routes:
1251 * 0.0.0.0/1 and 128.0.0.0/1 */
1252 if (this->routing_table == 0 && prefixlen == 0)
1253 {
1254 chunk_t half_net;
1255 u_int8_t half_prefixlen;
1256 status_t status;
1257
1258 half_net = chunk_alloca(dst_net.len);
1259 memset(half_net.ptr, 0, half_net.len);
1260 half_prefixlen = 1;
1261
1262 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1263 gateway, src_ip, if_name);
1264 half_net.ptr[0] |= 0x80;
1265 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1266 gateway, src_ip, if_name);
1267 return status;
1268 }
1269
1270 memset(&request, 0, sizeof(request));
1271
1272 hdr = (struct nlmsghdr*)request;
1273 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1274 hdr->nlmsg_type = nlmsg_type;
1275 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1276
1277 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1278 msg->rtm_family = src_ip->get_family(src_ip);
1279 msg->rtm_dst_len = prefixlen;
1280 msg->rtm_table = this->routing_table;
1281 msg->rtm_protocol = RTPROT_STATIC;
1282 msg->rtm_type = RTN_UNICAST;
1283 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1284
1285 netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
1286 chunk = src_ip->get_address(src_ip);
1287 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1288 if (gateway && gateway->get_family(gateway) == src_ip->get_family(src_ip))
1289 {
1290 chunk = gateway->get_address(gateway);
1291 netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1292 }
1293 ifindex = get_interface_index(this, if_name);
1294 chunk.ptr = (char*)&ifindex;
1295 chunk.len = sizeof(ifindex);
1296 netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1297
1298 return this->socket->send_ack(this->socket, hdr);
1299 }
1300
1301 METHOD(kernel_net_t, add_route, status_t,
1302 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1303 host_t *gateway, host_t *src_ip, char *if_name)
1304 {
1305 return manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1306 dst_net, prefixlen, gateway, src_ip, if_name);
1307 }
1308
1309 METHOD(kernel_net_t, del_route, status_t,
1310 private_kernel_netlink_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1311 host_t *gateway, host_t *src_ip, char *if_name)
1312 {
1313 return manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
1314 gateway, src_ip, if_name);
1315 }
1316
1317 /**
1318 * Initialize a list of local addresses.
1319 */
1320 static status_t init_address_list(private_kernel_netlink_net_t *this)
1321 {
1322 netlink_buf_t request;
1323 struct nlmsghdr *out, *current, *in;
1324 struct rtgenmsg *msg;
1325 size_t len;
1326 enumerator_t *ifaces, *addrs;
1327 iface_entry_t *iface;
1328 addr_entry_t *addr;
1329
1330 DBG1(DBG_KNL, "listening on interfaces:");
1331
1332 memset(&request, 0, sizeof(request));
1333
1334 in = (struct nlmsghdr*)&request;
1335 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1336 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1337 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1338 msg->rtgen_family = AF_UNSPEC;
1339
1340 /* get all links */
1341 in->nlmsg_type = RTM_GETLINK;
1342 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1343 {
1344 return FAILED;
1345 }
1346 current = out;
1347 while (NLMSG_OK(current, len))
1348 {
1349 switch (current->nlmsg_type)
1350 {
1351 case NLMSG_DONE:
1352 break;
1353 case RTM_NEWLINK:
1354 process_link(this, current, FALSE);
1355 /* fall through */
1356 default:
1357 current = NLMSG_NEXT(current, len);
1358 continue;
1359 }
1360 break;
1361 }
1362 free(out);
1363
1364 /* get all interface addresses */
1365 in->nlmsg_type = RTM_GETADDR;
1366 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1367 {
1368 return FAILED;
1369 }
1370 current = out;
1371 while (NLMSG_OK(current, len))
1372 {
1373 switch (current->nlmsg_type)
1374 {
1375 case NLMSG_DONE:
1376 break;
1377 case RTM_NEWADDR:
1378 process_addr(this, current, FALSE);
1379 /* fall through */
1380 default:
1381 current = NLMSG_NEXT(current, len);
1382 continue;
1383 }
1384 break;
1385 }
1386 free(out);
1387
1388 this->mutex->lock(this->mutex);
1389 ifaces = this->ifaces->create_enumerator(this->ifaces);
1390 while (ifaces->enumerate(ifaces, &iface))
1391 {
1392 if (iface->flags & IFF_UP)
1393 {
1394 DBG1(DBG_KNL, " %s", iface->ifname);
1395 addrs = iface->addrs->create_enumerator(iface->addrs);
1396 while (addrs->enumerate(addrs, (void**)&addr))
1397 {
1398 DBG1(DBG_KNL, " %H", addr->ip);
1399 }
1400 addrs->destroy(addrs);
1401 }
1402 }
1403 ifaces->destroy(ifaces);
1404 this->mutex->unlock(this->mutex);
1405 return SUCCESS;
1406 }
1407
1408 /**
1409 * create or delete a rule to use our routing table
1410 */
1411 static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
1412 int family, u_int32_t table, u_int32_t prio)
1413 {
1414 netlink_buf_t request;
1415 struct nlmsghdr *hdr;
1416 struct rtmsg *msg;
1417 chunk_t chunk;
1418
1419 memset(&request, 0, sizeof(request));
1420 hdr = (struct nlmsghdr*)request;
1421 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1422 hdr->nlmsg_type = nlmsg_type;
1423 if (nlmsg_type == RTM_NEWRULE)
1424 {
1425 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
1426 }
1427 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1428
1429 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1430 msg->rtm_table = table;
1431 msg->rtm_family = family;
1432 msg->rtm_protocol = RTPROT_BOOT;
1433 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1434 msg->rtm_type = RTN_UNICAST;
1435
1436 chunk = chunk_from_thing(prio);
1437 netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
1438
1439 return this->socket->send_ack(this->socket, hdr);
1440 }
1441
1442 METHOD(kernel_net_t, destroy, void,
1443 private_kernel_netlink_net_t *this)
1444 {
1445 if (this->routing_table)
1446 {
1447 manage_rule(this, RTM_DELRULE, AF_INET, this->routing_table,
1448 this->routing_table_prio);
1449 manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
1450 this->routing_table_prio);
1451 }
1452 if (this->job)
1453 {
1454 this->job->cancel(this->job);
1455 }
1456 if (this->socket_events > 0)
1457 {
1458 close(this->socket_events);
1459 }
1460 DESTROY_IF(this->socket);
1461 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
1462 this->rt_exclude->destroy(this->rt_exclude);
1463 this->condvar->destroy(this->condvar);
1464 this->mutex->destroy(this->mutex);
1465 free(this);
1466 }
1467
1468 /*
1469 * Described in header.
1470 */
1471 kernel_netlink_net_t *kernel_netlink_net_create()
1472 {
1473 private_kernel_netlink_net_t *this;
1474 struct sockaddr_nl addr;
1475 enumerator_t *enumerator;
1476 char *exclude;
1477
1478 INIT(this,
1479 .public = {
1480 .interface = {
1481 .get_interface = _get_interface_name,
1482 .create_address_enumerator = _create_address_enumerator,
1483 .get_source_addr = _get_source_addr,
1484 .get_nexthop = _get_nexthop,
1485 .add_ip = _add_ip,
1486 .del_ip = _del_ip,
1487 .add_route = _add_route,
1488 .del_route = _del_route,
1489 .destroy = _destroy,
1490 },
1491 },
1492 .socket = netlink_socket_create(NETLINK_ROUTE),
1493 .rt_exclude = linked_list_create(),
1494 .ifaces = linked_list_create(),
1495 .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
1496 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
1497 .routing_table = lib->settings->get_int(lib->settings,
1498 "%s.routing_table", ROUTING_TABLE, hydra->daemon),
1499 .routing_table_prio = lib->settings->get_int(lib->settings,
1500 "%s.routing_table_prio", ROUTING_TABLE_PRIO, hydra->daemon),
1501 .process_route = lib->settings->get_bool(lib->settings,
1502 "%s.process_route", TRUE, hydra->daemon),
1503 .install_virtual_ip = lib->settings->get_bool(lib->settings,
1504 "%s.install_virtual_ip", TRUE, hydra->daemon),
1505 );
1506 timerclear(&this->last_roam);
1507
1508 exclude = lib->settings->get_str(lib->settings,
1509 "%s.ignore_routing_tables", NULL, hydra->daemon);
1510 if (exclude)
1511 {
1512 char *token;
1513 uintptr_t table;
1514
1515 enumerator = enumerator_create_token(exclude, " ", " ");
1516 while (enumerator->enumerate(enumerator, &token))
1517 {
1518 errno = 0;
1519 table = strtoul(token, NULL, 10);
1520
1521 if (errno == 0)
1522 {
1523 this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
1524 }
1525 }
1526 enumerator->destroy(enumerator);
1527 }
1528
1529 memset(&addr, 0, sizeof(addr));
1530 addr.nl_family = AF_NETLINK;
1531
1532 /* create and bind RT socket for events (address/interface/route changes) */
1533 this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1534 if (this->socket_events < 0)
1535 {
1536 DBG1(DBG_KNL, "unable to create RT event socket");
1537 destroy(this);
1538 return NULL;
1539 }
1540 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
1541 RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK;
1542 if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
1543 {
1544 DBG1(DBG_KNL, "unable to bind RT event socket");
1545 destroy(this);
1546 return NULL;
1547 }
1548
1549 this->job = callback_job_create_with_prio((callback_job_cb_t)receive_events,
1550 this, NULL, NULL, JOB_PRIO_CRITICAL);
1551 lib->processor->queue_job(lib->processor, (job_t*)this->job);
1552
1553 if (init_address_list(this) != SUCCESS)
1554 {
1555 DBG1(DBG_KNL, "unable to get interface list");
1556 destroy(this);
1557 return NULL;
1558 }
1559
1560 if (this->routing_table)
1561 {
1562 if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
1563 this->routing_table_prio) != SUCCESS)
1564 {
1565 DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
1566 }
1567 if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
1568 this->routing_table_prio) != SUCCESS)
1569 {
1570 DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
1571 }
1572 }
1573
1574 return &this->public;
1575 }