Use a hashtable to quickly check for usable IP addresses/interfaces
[strongswan.git] / src / libhydra / plugins / kernel_pfroute / kernel_pfroute_net.c
1 /*
2 * Copyright (C) 2009-2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <net/if.h>
19 #include <ifaddrs.h>
20 #include <net/route.h>
21 #include <unistd.h>
22 #include <errno.h>
23
24 #include "kernel_pfroute_net.h"
25
26 #include <hydra.h>
27 #include <debug.h>
28 #include <utils/host.h>
29 #include <threading/thread.h>
30 #include <threading/mutex.h>
31 #include <utils/linked_list.h>
32 #include <processing/jobs/callback_job.h>
33
34 #ifndef HAVE_STRUCT_SOCKADDR_SA_LEN
35 #error Cannot compile this plugin on systems where 'struct sockaddr' has no sa_len member.
36 #endif
37
38 /** delay before firing roam events (ms) */
39 #define ROAM_DELAY 100
40
41 /** buffer size for PF_ROUTE messages */
42 #define PFROUTE_BUFFER_SIZE 4096
43
44 typedef struct addr_entry_t addr_entry_t;
45
46 /**
47 * IP address in an inface_entry_t
48 */
49 struct addr_entry_t {
50
51 /** The ip address */
52 host_t *ip;
53
54 /** virtual IP managed by us */
55 bool virtual;
56
57 /** Number of times this IP is used, if virtual */
58 u_int refcount;
59 };
60
61 /**
62 * destroy a addr_entry_t object
63 */
64 static void addr_entry_destroy(addr_entry_t *this)
65 {
66 this->ip->destroy(this->ip);
67 free(this);
68 }
69
70 typedef struct iface_entry_t iface_entry_t;
71
72 /**
73 * A network interface on this system, containing addr_entry_t's
74 */
75 struct iface_entry_t {
76
77 /** interface index */
78 int ifindex;
79
80 /** name of the interface */
81 char ifname[IFNAMSIZ];
82
83 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
84 u_int flags;
85
86 /** list of addresses as host_t */
87 linked_list_t *addrs;
88
89 /** TRUE if usable by config */
90 bool usable;
91 };
92
93 /**
94 * destroy an interface entry
95 */
96 static void iface_entry_destroy(iface_entry_t *this)
97 {
98 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
99 free(this);
100 }
101
102 /**
103 * check if an interface is up
104 */
105 static inline bool iface_entry_up(iface_entry_t *iface)
106 {
107 return (iface->flags & IFF_UP) == IFF_UP;
108 }
109
110 /**
111 * check if an interface is up and usable
112 */
113 static inline bool iface_entry_up_and_usable(iface_entry_t *iface)
114 {
115 return iface->usable && iface_entry_up(iface);
116 }
117
118 typedef struct addr_map_entry_t addr_map_entry_t;
119
120 /**
121 * Entry that maps an IP address to an interface entry
122 */
123 struct addr_map_entry_t {
124 /** The IP address */
125 host_t *ip;
126
127 /** The interface this address is installed on */
128 iface_entry_t *iface;
129 };
130
131 /**
132 * Hash a addr_map_entry_t object, all entries with the same IP address
133 * are stored in the same bucket
134 */
135 static u_int addr_map_entry_hash(addr_map_entry_t *this)
136 {
137 return chunk_hash(this->ip->get_address(this->ip));
138 }
139
140 /**
141 * Compare two addr_map_entry_t objects, two entries are equal if they are
142 * installed on the same interface
143 */
144 static bool addr_map_entry_equals(addr_map_entry_t *a, addr_map_entry_t *b)
145 {
146 return a->iface->ifindex == b->iface->ifindex &&
147 a->ip->ip_equals(a->ip, b->ip);
148 }
149
150 /**
151 * Used with get_match this finds an address entry if it is installed on
152 * an up and usable interface
153 */
154 static bool addr_map_entry_match_up_and_usable(addr_map_entry_t *a,
155 addr_map_entry_t *b)
156 {
157 return iface_entry_up_and_usable(b->iface) &&
158 a->ip->ip_equals(a->ip, b->ip);
159 }
160
161 /**
162 * Used with get_match this finds an address entry if it is installed on
163 * any active local interface
164 */
165 static bool addr_map_entry_match_up(addr_map_entry_t *a, addr_map_entry_t *b)
166 {
167 return iface_entry_up(b->iface) && a->ip->ip_equals(a->ip, b->ip);
168 }
169
170 typedef struct private_kernel_pfroute_net_t private_kernel_pfroute_net_t;
171
172 /**
173 * Private variables and functions of kernel_pfroute class.
174 */
175 struct private_kernel_pfroute_net_t
176 {
177 /**
178 * Public part of the kernel_pfroute_t object.
179 */
180 kernel_pfroute_net_t public;
181
182 /**
183 * mutex to lock access to various lists
184 */
185 mutex_t *mutex;
186
187 /**
188 * Cached list of interfaces and their addresses (iface_entry_t)
189 */
190 linked_list_t *ifaces;
191
192 /**
193 * Map for IP addresses to iface_entry_t objects (addr_map_entry_t)
194 */
195 hashtable_t *addrs;
196
197 /**
198 * mutex to lock access to the PF_ROUTE socket
199 */
200 mutex_t *mutex_pfroute;
201
202 /**
203 * PF_ROUTE socket to communicate with the kernel
204 */
205 int socket;
206
207 /**
208 * PF_ROUTE socket to receive events
209 */
210 int socket_events;
211
212 /**
213 * sequence number for messages sent to the kernel
214 */
215 int seq;
216
217 /**
218 * time of last roam event
219 */
220 timeval_t last_roam;
221 };
222
223 /**
224 * Add an address map entry
225 */
226 static void addr_map_entry_add(private_kernel_netlink_net_t *this,
227 addr_entry_t *addr, iface_entry_t *iface)
228 {
229 addr_map_entry_t *entry;
230
231 if (addr->virtual)
232 { /* don't map virtual IPs */
233 return;
234 }
235
236 INIT(entry,
237 .ip = addr->ip,
238 .iface = iface,
239 );
240 entry = this->addrs->put(this->addrs, entry, entry);
241 free(entry);
242 }
243
244 /**
245 * Remove an address map entry (the argument order is a bit strange because
246 * it is also used with linked_list_t.invoke_function)
247 */
248 static void addr_map_entry_remove(addr_entry_t *addr, iface_entry_t *iface,
249 private_kernel_netlink_net_t *this)
250 {
251 addr_map_entry_t *entry, lookup = {
252 .ip = addr->ip,
253 .iface = iface,
254 };
255
256 if (addr->virtual)
257 { /* these are never mapped, but this check avoid problems if a virtual IP
258 * equals a regular one */
259 return;
260 }
261 entry = this->addrs->remove(this->addrs, &lookup);
262 free(entry);
263 }
264
265 /**
266 * callback function that raises the delayed roam event
267 */
268 static job_requeue_t roam_event(uintptr_t address)
269 {
270 hydra->kernel_interface->roam(hydra->kernel_interface, address != 0);
271 return JOB_REQUEUE_NONE;
272 }
273
274 /**
275 * fire a roaming event. we delay it for a bit and fire only one event
276 * for multiple calls. otherwise we would create too many events.
277 */
278 static void fire_roam_event(private_kernel_pfroute_net_t *this, bool address)
279 {
280 timeval_t now;
281 job_t *job;
282
283 time_monotonic(&now);
284 if (timercmp(&now, &this->last_roam, >))
285 {
286 now.tv_usec += ROAM_DELAY * 1000;
287 while (now.tv_usec > 1000000)
288 {
289 now.tv_sec++;
290 now.tv_usec -= 1000000;
291 }
292 this->last_roam = now;
293
294 job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
295 (void*)(uintptr_t)(address ? 1 : 0),
296 NULL, NULL);
297 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
298 }
299 }
300
301 /**
302 * Process an RTM_*ADDR message from the kernel
303 */
304 static void process_addr(private_kernel_pfroute_net_t *this,
305 struct rt_msghdr *msg)
306 {
307 struct ifa_msghdr *ifa = (struct ifa_msghdr*)msg;
308 sockaddr_t *sockaddr = (sockaddr_t*)(ifa + 1);
309 host_t *host = NULL;
310 enumerator_t *ifaces, *addrs;
311 iface_entry_t *iface;
312 addr_entry_t *addr;
313 bool found = FALSE, changed = FALSE, roam = FALSE;
314 int i;
315
316 for (i = 1; i < (1 << RTAX_MAX); i <<= 1)
317 {
318 if (ifa->ifam_addrs & i)
319 {
320 if (RTA_IFA & i)
321 {
322 host = host_create_from_sockaddr(sockaddr);
323 break;
324 }
325 sockaddr = (sockaddr_t*)((char*)sockaddr + sockaddr->sa_len);
326 }
327 }
328
329 if (!host)
330 {
331 return;
332 }
333
334 this->mutex->lock(this->mutex);
335 ifaces = this->ifaces->create_enumerator(this->ifaces);
336 while (ifaces->enumerate(ifaces, &iface))
337 {
338 if (iface->ifindex == ifa->ifam_index)
339 {
340 addrs = iface->addrs->create_enumerator(iface->addrs);
341 while (addrs->enumerate(addrs, &addr))
342 {
343 if (host->ip_equals(host, addr->ip))
344 {
345 found = TRUE;
346 if (ifa->ifam_type == RTM_DELADDR)
347 {
348 iface->addrs->remove_at(iface->addrs, addrs);
349 if (!addr->virtual && iface->usable)
350 {
351 changed = TRUE;
352 DBG1(DBG_KNL, "%H disappeared from %s",
353 host, iface->ifname);
354 }
355 addr_map_entry_remove(addr, iface, this);
356 addr_entry_destroy(addr);
357 }
358 else if (ifa->ifam_type == RTM_NEWADDR && addr->virtual)
359 {
360 addr->refcount = 1;
361 }
362 }
363 }
364 addrs->destroy(addrs);
365
366 if (!found && ifa->ifam_type == RTM_NEWADDR)
367 {
368 changed = TRUE;
369 addr = malloc_thing(addr_entry_t);
370 addr->ip = host->clone(host);
371 addr->virtual = FALSE;
372 addr->refcount = 1;
373 iface->addrs->insert_last(iface->addrs, addr);
374 addr_map_entry_add(this, addr, iface);
375 if (iface->usable)
376 {
377 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
378 }
379 }
380
381 if (changed && iface_entry_up_and_usable(iface))
382 {
383 roam = TRUE;
384 }
385 break;
386 }
387 }
388 ifaces->destroy(ifaces);
389 this->mutex->unlock(this->mutex);
390 host->destroy(host);
391
392 if (roam)
393 {
394 fire_roam_event(this, TRUE);
395 }
396 }
397
398 /**
399 * Process an RTM_IFINFO message from the kernel
400 */
401 static void process_link(private_kernel_pfroute_net_t *this,
402 struct rt_msghdr *hdr)
403 {
404 struct if_msghdr *msg = (struct if_msghdr*)hdr;
405 enumerator_t *enumerator;
406 iface_entry_t *iface;
407 bool roam = FALSE;
408
409 this->mutex->lock(this->mutex);
410 enumerator = this->ifaces->create_enumerator(this->ifaces);
411 while (enumerator->enumerate(enumerator, &iface))
412 {
413 if (iface->ifindex == msg->ifm_index)
414 {
415 if (iface->usable)
416 {
417 if (!(iface->flags & IFF_UP) && (msg->ifm_flags & IFF_UP))
418 {
419 roam = TRUE;
420 DBG1(DBG_KNL, "interface %s activated", iface->ifname);
421 }
422 else if ((iface->flags & IFF_UP) && !(msg->ifm_flags & IFF_UP))
423 {
424 roam = TRUE;
425 DBG1(DBG_KNL, "interface %s deactivated", iface->ifname);
426 }
427 }
428 iface->flags = msg->ifm_flags;
429 break;
430 }
431 }
432 enumerator->destroy(enumerator);
433 this->mutex->unlock(this->mutex);
434
435 if (roam)
436 {
437 fire_roam_event(this, TRUE);
438 }
439 }
440
441 /**
442 * Process an RTM_*ROUTE message from the kernel
443 */
444 static void process_route(private_kernel_pfroute_net_t *this,
445 struct rt_msghdr *msg)
446 {
447
448 }
449
450 /**
451 * Receives events from kernel
452 */
453 static job_requeue_t receive_events(private_kernel_pfroute_net_t *this)
454 {
455 unsigned char buf[PFROUTE_BUFFER_SIZE];
456 struct rt_msghdr *msg = (struct rt_msghdr*)buf;
457 int len;
458 bool oldstate;
459
460 oldstate = thread_cancelability(TRUE);
461 len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0);
462 thread_cancelability(oldstate);
463
464 if (len < 0)
465 {
466 switch (errno)
467 {
468 case EINTR:
469 /* interrupted, try again */
470 return JOB_REQUEUE_DIRECT;
471 case EAGAIN:
472 /* no data ready, select again */
473 return JOB_REQUEUE_DIRECT;
474 default:
475 DBG1(DBG_KNL, "unable to receive from PF_ROUTE event socket");
476 sleep(1);
477 return JOB_REQUEUE_FAIR;
478 }
479 }
480
481 if (len < sizeof(msg->rtm_msglen) || len < msg->rtm_msglen ||
482 msg->rtm_version != RTM_VERSION)
483 {
484 DBG2(DBG_KNL, "received corrupted PF_ROUTE message");
485 return JOB_REQUEUE_DIRECT;
486 }
487
488 switch (msg->rtm_type)
489 {
490 case RTM_NEWADDR:
491 case RTM_DELADDR:
492 process_addr(this, msg);
493 break;
494 case RTM_IFINFO:
495 /*case RTM_IFANNOUNCE <- what about this*/
496 process_link(this, msg);
497 break;
498 case RTM_ADD:
499 case RTM_DELETE:
500 process_route(this, msg);
501 default:
502 break;
503 }
504
505 return JOB_REQUEUE_DIRECT;
506 }
507
508
509 /** enumerator over addresses */
510 typedef struct {
511 private_kernel_pfroute_net_t* this;
512 /** whether to enumerate down interfaces */
513 bool include_down_ifaces;
514 /** whether to enumerate virtual ip addresses */
515 bool include_virtual_ips;
516 /** whether to enumerate loopback interfaces */
517 bool include_loopback;
518 } address_enumerator_t;
519
520 /**
521 * cleanup function for address enumerator
522 */
523 static void address_enumerator_destroy(address_enumerator_t *data)
524 {
525 data->this->mutex->unlock(data->this->mutex);
526 free(data);
527 }
528
529 /**
530 * filter for addresses
531 */
532 static bool filter_addresses(address_enumerator_t *data,
533 addr_entry_t** in, host_t** out)
534 {
535 host_t *ip;
536 if (!data->include_virtual_ips && (*in)->virtual)
537 { /* skip virtual interfaces added by us */
538 return FALSE;
539 }
540 ip = (*in)->ip;
541 if (ip->get_family(ip) == AF_INET6)
542 {
543 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ip->get_sockaddr(ip);
544 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
545 { /* skip addresses with a unusable scope */
546 return FALSE;
547 }
548 }
549 *out = ip;
550 return TRUE;
551 }
552
553 /**
554 * enumerator constructor for interfaces
555 */
556 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
557 address_enumerator_t *data)
558 {
559 return enumerator_create_filter(iface->addrs->create_enumerator(iface->addrs),
560 (void*)filter_addresses, data, NULL);
561 }
562
563 /**
564 * filter for interfaces
565 */
566 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
567 iface_entry_t** out)
568 {
569 if (!(*in)->usable)
570 { /* skip interfaces excluded by config */
571 return FALSE;
572 }
573 if (!data->include_loopback && ((*in)->flags & IFF_LOOPBACK))
574 { /* ignore loopback devices */
575 return FALSE;
576 }
577 if (!data->include_down_ifaces && !((*in)->flags & IFF_UP))
578 { /* skip interfaces not up */
579 return FALSE;
580 }
581 *out = *in;
582 return TRUE;
583 }
584
585 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
586 private_kernel_pfroute_net_t *this,
587 bool include_down_ifaces, bool include_virtual_ips, bool include_loopback)
588 {
589 address_enumerator_t *data = malloc_thing(address_enumerator_t);
590 data->this = this;
591 data->include_down_ifaces = include_down_ifaces;
592 data->include_virtual_ips = include_virtual_ips;
593 data->include_loopback = include_loopback;
594
595 this->mutex->lock(this->mutex);
596 return enumerator_create_nested(
597 enumerator_create_filter(
598 this->ifaces->create_enumerator(this->ifaces),
599 (void*)filter_interfaces, data, NULL),
600 (void*)create_iface_enumerator, data,
601 (void*)address_enumerator_destroy);
602 }
603
604 METHOD(kernel_net_t, get_interface_name, bool,
605 private_kernel_pfroute_net_t *this, host_t* ip, char **name)
606 {
607 addr_map_entry_t *entry, lookup = {
608 .ip = ip,
609 };
610
611 if (ip->is_anyaddr(ip))
612 {
613 return FALSE;
614 }
615 this->mutex->lock(this->mutex);
616 /* first try to find it on an up and usable interface */
617 entry = this->addrs->get_match(this->addrs, &lookup,
618 (void*)addr_map_entry_match_up_and_usable);
619 if (entry)
620 {
621 if (name)
622 {
623 *name = strdup(entry->iface->ifname);
624 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
625 }
626 this->mutex->unlock(this->mutex);
627 return TRUE;
628 }
629 /* maybe it is installed on an ignored interface */
630 entry = this->addrs->get_match(this->addrs, &lookup,
631 (void*)addr_map_entry_match_up);
632 if (!entry)
633 { /* the address does not exist, is on a down interface */
634 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
635 }
636 this->mutex->unlock(this->mutex);
637 return FALSE;
638 }
639
640 METHOD(kernel_net_t, get_source_addr, host_t*,
641 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
642 {
643 return NULL;
644 }
645
646 METHOD(kernel_net_t, get_nexthop, host_t*,
647 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
648 {
649 return NULL;
650 }
651
652 METHOD(kernel_net_t, add_ip, status_t,
653 private_kernel_pfroute_net_t *this, host_t *virtual_ip, host_t *iface_ip)
654 {
655 return FAILED;
656 }
657
658 METHOD(kernel_net_t, del_ip, status_t,
659 private_kernel_pfroute_net_t *this, host_t *virtual_ip)
660 {
661 return FAILED;
662 }
663
664 METHOD(kernel_net_t, add_route, status_t,
665 private_kernel_pfroute_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
666 host_t *gateway, host_t *src_ip, char *if_name)
667 {
668 return FAILED;
669 }
670
671 METHOD(kernel_net_t, del_route, status_t,
672 private_kernel_pfroute_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
673 host_t *gateway, host_t *src_ip, char *if_name)
674 {
675 return FAILED;
676 }
677
678 /**
679 * Initialize a list of local addresses.
680 */
681 static status_t init_address_list(private_kernel_pfroute_net_t *this)
682 {
683 struct ifaddrs *ifap, *ifa;
684 iface_entry_t *iface, *current;
685 addr_entry_t *addr;
686 enumerator_t *ifaces, *addrs;
687
688 DBG2(DBG_KNL, "known interfaces and IP addresses:");
689
690 if (getifaddrs(&ifap) < 0)
691 {
692 DBG1(DBG_KNL, " failed to get interfaces!");
693 return FAILED;
694 }
695
696 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
697 {
698 if (ifa->ifa_addr == NULL)
699 {
700 continue;
701 }
702 switch(ifa->ifa_addr->sa_family)
703 {
704 case AF_LINK:
705 case AF_INET:
706 case AF_INET6:
707 {
708 iface = NULL;
709 ifaces = this->ifaces->create_enumerator(this->ifaces);
710 while (ifaces->enumerate(ifaces, &current))
711 {
712 if (streq(current->ifname, ifa->ifa_name))
713 {
714 iface = current;
715 break;
716 }
717 }
718 ifaces->destroy(ifaces);
719
720 if (!iface)
721 {
722 iface = malloc_thing(iface_entry_t);
723 memcpy(iface->ifname, ifa->ifa_name, IFNAMSIZ);
724 iface->ifindex = if_nametoindex(ifa->ifa_name);
725 iface->flags = ifa->ifa_flags;
726 iface->addrs = linked_list_create();
727 iface->usable = hydra->kernel_interface->is_interface_usable(
728 hydra->kernel_interface, ifa->ifa_name);
729 this->ifaces->insert_last(this->ifaces, iface);
730 }
731
732 if (ifa->ifa_addr->sa_family != AF_LINK)
733 {
734 addr = malloc_thing(addr_entry_t);
735 addr->ip = host_create_from_sockaddr(ifa->ifa_addr);
736 addr->virtual = FALSE;
737 addr->refcount = 1;
738 iface->addrs->insert_last(iface->addrs, addr);
739 }
740 }
741 }
742 }
743 freeifaddrs(ifap);
744
745 ifaces = this->ifaces->create_enumerator(this->ifaces);
746 while (ifaces->enumerate(ifaces, &iface))
747 {
748 if (iface->usable && iface->flags & IFF_UP)
749 {
750 DBG2(DBG_KNL, " %s", iface->ifname);
751 addrs = iface->addrs->create_enumerator(iface->addrs);
752 while (addrs->enumerate(addrs, (void**)&addr))
753 {
754 DBG2(DBG_KNL, " %H", addr->ip);
755 }
756 addrs->destroy(addrs);
757 }
758 }
759 ifaces->destroy(ifaces);
760
761 return SUCCESS;
762 }
763
764 METHOD(kernel_net_t, destroy, void,
765 private_kernel_pfroute_net_t *this)
766 {
767 enumerator_t *enumerator;
768
769 if (this->socket > 0)
770 {
771 close(this->socket);
772 }
773 if (this->socket_events)
774 {
775 close(this->socket_events);
776 }
777 enumerator = this->addrs->create_enumerator(this->addrs);
778 while (enumerator->enumerate(enumerator, NULL, (void**)&addr))
779 {
780 free(addr);
781 }
782 enumerator->destroy(enumerator);
783 this->addrs->destroy(this->addrs);
784 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
785 this->mutex->destroy(this->mutex);
786 this->mutex_pfroute->destroy(this->mutex_pfroute);
787 free(this);
788 }
789
790 /*
791 * Described in header.
792 */
793 kernel_pfroute_net_t *kernel_pfroute_net_create()
794 {
795 private_kernel_pfroute_net_t *this;
796 bool register_for_events = TRUE;
797
798 INIT(this,
799 .public = {
800 .interface = {
801 .get_interface = _get_interface_name,
802 .create_address_enumerator = _create_address_enumerator,
803 .get_source_addr = _get_source_addr,
804 .get_nexthop = _get_nexthop,
805 .add_ip = _add_ip,
806 .del_ip = _del_ip,
807 .add_route = _add_route,
808 .del_route = _del_route,
809 .destroy = _destroy,
810 },
811 },
812 .ifaces = linked_list_create(),
813 .addrs = hashtable_create(
814 (hashtable_hash_t)addr_map_entry_hash,
815 (hashtable_equals_t)addr_map_entry_equals, 16),
816 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
817 .mutex_pfroute = mutex_create(MUTEX_TYPE_DEFAULT),
818 );
819
820 if (streq(hydra->daemon, "starter"))
821 { /* starter has no threads, so we do not register for kernel events */
822 register_for_events = FALSE;
823 }
824
825 /* create a PF_ROUTE socket to communicate with the kernel */
826 this->socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
827 if (this->socket < 0)
828 {
829 DBG1(DBG_KNL, "unable to create PF_ROUTE socket");
830 destroy(this);
831 return NULL;
832 }
833
834 if (register_for_events)
835 {
836 /* create a PF_ROUTE socket to receive events */
837 this->socket_events = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
838 if (this->socket_events < 0)
839 {
840 DBG1(DBG_KNL, "unable to create PF_ROUTE event socket");
841 destroy(this);
842 return NULL;
843 }
844
845 lib->processor->queue_job(lib->processor,
846 (job_t*)callback_job_create_with_prio(
847 (callback_job_cb_t)receive_events, this, NULL,
848 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
849 }
850
851 if (init_address_list(this) != SUCCESS)
852 {
853 DBG1(DBG_KNL, "unable to get interface list");
854 destroy(this);
855 return NULL;
856 }
857
858 return &this->public;
859 }