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