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