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