Made IP address enumeration more flexible
[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 /** which addresses to enumerate */
513 address_type_t which;
514 } address_enumerator_t;
515
516 /**
517 * cleanup function for address enumerator
518 */
519 static void address_enumerator_destroy(address_enumerator_t *data)
520 {
521 data->this->mutex->unlock(data->this->mutex);
522 free(data);
523 }
524
525 /**
526 * filter for addresses
527 */
528 static bool filter_addresses(address_enumerator_t *data,
529 addr_entry_t** in, host_t** out)
530 {
531 host_t *ip;
532 if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->virtual)
533 { /* skip virtual interfaces added by us */
534 return FALSE;
535 }
536 ip = (*in)->ip;
537 if (ip->get_family(ip) == AF_INET6)
538 {
539 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ip->get_sockaddr(ip);
540 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
541 { /* skip addresses with a unusable scope */
542 return FALSE;
543 }
544 }
545 *out = ip;
546 return TRUE;
547 }
548
549 /**
550 * enumerator constructor for interfaces
551 */
552 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
553 address_enumerator_t *data)
554 {
555 return enumerator_create_filter(iface->addrs->create_enumerator(iface->addrs),
556 (void*)filter_addresses, data, NULL);
557 }
558
559 /**
560 * filter for interfaces
561 */
562 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
563 iface_entry_t** out)
564 {
565 if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
566 { /* skip interfaces excluded by config */
567 return FALSE;
568 }
569 if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
570 { /* ignore loopback devices */
571 return FALSE;
572 }
573 if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
574 { /* skip interfaces not up */
575 return FALSE;
576 }
577 *out = *in;
578 return TRUE;
579 }
580
581 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
582 private_kernel_pfroute_net_t *this, address_type_t which)
583 {
584 address_enumerator_t *data = malloc_thing(address_enumerator_t);
585 data->this = this;
586 data->which = which;
587
588 this->mutex->lock(this->mutex);
589 return enumerator_create_nested(
590 enumerator_create_filter(
591 this->ifaces->create_enumerator(this->ifaces),
592 (void*)filter_interfaces, data, NULL),
593 (void*)create_iface_enumerator, data,
594 (void*)address_enumerator_destroy);
595 }
596
597 METHOD(kernel_net_t, get_interface_name, bool,
598 private_kernel_pfroute_net_t *this, host_t* ip, char **name)
599 {
600 addr_map_entry_t *entry, lookup = {
601 .ip = ip,
602 };
603
604 if (ip->is_anyaddr(ip))
605 {
606 return FALSE;
607 }
608 this->mutex->lock(this->mutex);
609 /* first try to find it on an up and usable interface */
610 entry = this->addrs->get_match(this->addrs, &lookup,
611 (void*)addr_map_entry_match_up_and_usable);
612 if (entry)
613 {
614 if (name)
615 {
616 *name = strdup(entry->iface->ifname);
617 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
618 }
619 this->mutex->unlock(this->mutex);
620 return TRUE;
621 }
622 /* maybe it is installed on an ignored interface */
623 entry = this->addrs->get_match(this->addrs, &lookup,
624 (void*)addr_map_entry_match_up);
625 if (!entry)
626 { /* the address does not exist, is on a down interface */
627 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
628 }
629 this->mutex->unlock(this->mutex);
630 return FALSE;
631 }
632
633 METHOD(kernel_net_t, get_source_addr, host_t*,
634 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
635 {
636 return NULL;
637 }
638
639 METHOD(kernel_net_t, get_nexthop, host_t*,
640 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
641 {
642 return NULL;
643 }
644
645 METHOD(kernel_net_t, add_ip, status_t,
646 private_kernel_pfroute_net_t *this, host_t *virtual_ip, host_t *iface_ip)
647 {
648 return FAILED;
649 }
650
651 METHOD(kernel_net_t, del_ip, status_t,
652 private_kernel_pfroute_net_t *this, host_t *virtual_ip)
653 {
654 return FAILED;
655 }
656
657 METHOD(kernel_net_t, add_route, status_t,
658 private_kernel_pfroute_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
659 host_t *gateway, host_t *src_ip, char *if_name)
660 {
661 return FAILED;
662 }
663
664 METHOD(kernel_net_t, del_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 /**
672 * Initialize a list of local addresses.
673 */
674 static status_t init_address_list(private_kernel_pfroute_net_t *this)
675 {
676 struct ifaddrs *ifap, *ifa;
677 iface_entry_t *iface, *current;
678 addr_entry_t *addr;
679 enumerator_t *ifaces, *addrs;
680
681 DBG2(DBG_KNL, "known interfaces and IP addresses:");
682
683 if (getifaddrs(&ifap) < 0)
684 {
685 DBG1(DBG_KNL, " failed to get interfaces!");
686 return FAILED;
687 }
688
689 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
690 {
691 if (ifa->ifa_addr == NULL)
692 {
693 continue;
694 }
695 switch(ifa->ifa_addr->sa_family)
696 {
697 case AF_LINK:
698 case AF_INET:
699 case AF_INET6:
700 {
701 iface = NULL;
702 ifaces = this->ifaces->create_enumerator(this->ifaces);
703 while (ifaces->enumerate(ifaces, &current))
704 {
705 if (streq(current->ifname, ifa->ifa_name))
706 {
707 iface = current;
708 break;
709 }
710 }
711 ifaces->destroy(ifaces);
712
713 if (!iface)
714 {
715 iface = malloc_thing(iface_entry_t);
716 memcpy(iface->ifname, ifa->ifa_name, IFNAMSIZ);
717 iface->ifindex = if_nametoindex(ifa->ifa_name);
718 iface->flags = ifa->ifa_flags;
719 iface->addrs = linked_list_create();
720 iface->usable = hydra->kernel_interface->is_interface_usable(
721 hydra->kernel_interface, ifa->ifa_name);
722 this->ifaces->insert_last(this->ifaces, iface);
723 }
724
725 if (ifa->ifa_addr->sa_family != AF_LINK)
726 {
727 addr = malloc_thing(addr_entry_t);
728 addr->ip = host_create_from_sockaddr(ifa->ifa_addr);
729 addr->virtual = FALSE;
730 addr->refcount = 1;
731 iface->addrs->insert_last(iface->addrs, addr);
732 }
733 }
734 }
735 }
736 freeifaddrs(ifap);
737
738 ifaces = this->ifaces->create_enumerator(this->ifaces);
739 while (ifaces->enumerate(ifaces, &iface))
740 {
741 if (iface->usable && iface->flags & IFF_UP)
742 {
743 DBG2(DBG_KNL, " %s", iface->ifname);
744 addrs = iface->addrs->create_enumerator(iface->addrs);
745 while (addrs->enumerate(addrs, (void**)&addr))
746 {
747 DBG2(DBG_KNL, " %H", addr->ip);
748 }
749 addrs->destroy(addrs);
750 }
751 }
752 ifaces->destroy(ifaces);
753
754 return SUCCESS;
755 }
756
757 METHOD(kernel_net_t, destroy, void,
758 private_kernel_pfroute_net_t *this)
759 {
760 enumerator_t *enumerator;
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->mutex->destroy(this->mutex);
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 .mutex = mutex_create(MUTEX_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 }