dc1e85e8376a9662fe77fa519a72b69fe448499c
[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/condvar.h>
32 #include <threading/rwlock.h>
33 #include <collections/hashtable.h>
34 #include <collections/linked_list.h>
35 #include <processing/jobs/callback_job.h>
36
37 #ifndef HAVE_STRUCT_SOCKADDR_SA_LEN
38 #error Cannot compile this plugin on systems where 'struct sockaddr' has no sa_len member.
39 #endif
40
41 /** delay before firing roam events (ms) */
42 #define ROAM_DELAY 100
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 * lock to access lists and maps
184 */
185 rwlock_t *lock;
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 communicate exclusively with PF_KEY
199 */
200 mutex_t *mutex;
201
202 /**
203 * condvar to signal if PF_KEY query got a response
204 */
205 condvar_t *condvar;
206
207 /**
208 * pid to send PF_ROUTE messages with
209 */
210 pid_t pid;
211
212 /**
213 * PF_ROUTE socket to communicate with the kernel
214 */
215 int socket;
216
217 /**
218 * sequence number for messages sent to the kernel
219 */
220 int seq;
221
222 /**
223 * Sequence number a query is waiting for
224 */
225 int waiting_seq;
226
227 /**
228 * Allocated reply message from kernel
229 */
230 struct rt_msghdr *reply;
231
232 /**
233 * time of last roam event
234 */
235 timeval_t last_roam;
236 };
237
238 /**
239 * Add an address map entry
240 */
241 static void addr_map_entry_add(private_kernel_pfroute_net_t *this,
242 addr_entry_t *addr, iface_entry_t *iface)
243 {
244 addr_map_entry_t *entry;
245
246 if (addr->virtual)
247 { /* don't map virtual IPs */
248 return;
249 }
250
251 INIT(entry,
252 .ip = addr->ip,
253 .iface = iface,
254 );
255 entry = this->addrs->put(this->addrs, entry, entry);
256 free(entry);
257 }
258
259 /**
260 * Remove an address map entry (the argument order is a bit strange because
261 * it is also used with linked_list_t.invoke_function)
262 */
263 static void addr_map_entry_remove(addr_entry_t *addr, iface_entry_t *iface,
264 private_kernel_pfroute_net_t *this)
265 {
266 addr_map_entry_t *entry, lookup = {
267 .ip = addr->ip,
268 .iface = iface,
269 };
270
271 if (addr->virtual)
272 { /* these are never mapped, but this check avoid problems if a virtual IP
273 * equals a regular one */
274 return;
275 }
276 entry = this->addrs->remove(this->addrs, &lookup);
277 free(entry);
278 }
279
280 /**
281 * callback function that raises the delayed roam event
282 */
283 static job_requeue_t roam_event(uintptr_t address)
284 {
285 hydra->kernel_interface->roam(hydra->kernel_interface, address != 0);
286 return JOB_REQUEUE_NONE;
287 }
288
289 /**
290 * fire a roaming event. we delay it for a bit and fire only one event
291 * for multiple calls. otherwise we would create too many events.
292 */
293 static void fire_roam_event(private_kernel_pfroute_net_t *this, bool address)
294 {
295 timeval_t now;
296 job_t *job;
297
298 time_monotonic(&now);
299 if (timercmp(&now, &this->last_roam, >))
300 {
301 timeval_add_ms(&now, ROAM_DELAY);
302 this->last_roam = now;
303
304 job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
305 (void*)(uintptr_t)(address ? 1 : 0),
306 NULL, NULL);
307 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
308 }
309 }
310
311 /**
312 * Data for enumerator over rtmsg sockaddrs
313 */
314 typedef struct {
315 /** implements enumerator */
316 enumerator_t public;
317 /** copy of attribute bitfield */
318 int types;
319 /** bytes remaining in buffer */
320 int remaining;
321 /** next sockaddr to enumerate */
322 struct sockaddr *addr;
323 } rt_enumerator_t;
324
325 METHOD(enumerator_t, rt_enumerate, bool,
326 rt_enumerator_t *this, int *xtype, struct sockaddr **addr)
327 {
328 int i, type;
329
330 if (this->remaining < sizeof(this->addr->sa_len) ||
331 this->remaining < this->addr->sa_len)
332 {
333 return FALSE;
334 }
335 for (i = 0; i < RTAX_MAX; i++)
336 {
337 type = (1 << i);
338 if (this->types & type)
339 {
340 this->types &= ~type;
341 *addr = this->addr;
342 *xtype = i;
343 this->remaining -= this->addr->sa_len;
344 this->addr = (void*)this->addr + this->addr->sa_len;
345 return TRUE;
346 }
347 }
348 return FALSE;
349 }
350
351 /**
352 * Create a safe enumerator over sockaddrs in ifa/ifam/rt_msg
353 */
354 static enumerator_t *create_rtmsg_enumerator(void *hdr, size_t hdrlen)
355 {
356 struct rt_msghdr *rthdr = hdr;
357 rt_enumerator_t *this;
358
359 INIT(this,
360 .public = {
361 .enumerate = (void*)_rt_enumerate,
362 .destroy = (void*)free,
363 },
364 .types = rthdr->rtm_addrs,
365 .remaining = rthdr->rtm_msglen - hdrlen,
366 .addr = hdr + hdrlen,
367 );
368 return &this->public;
369 }
370
371 /**
372 * Process an RTM_*ADDR message from the kernel
373 */
374 static void process_addr(private_kernel_pfroute_net_t *this,
375 struct ifa_msghdr *ifa)
376 {
377 struct sockaddr *sockaddr;
378 host_t *host = NULL;
379 enumerator_t *ifaces, *addrs;
380 iface_entry_t *iface;
381 addr_entry_t *addr;
382 bool found = FALSE, changed = FALSE, roam = FALSE;
383 enumerator_t *enumerator;
384 int type;
385
386 enumerator = create_rtmsg_enumerator(ifa, sizeof(*ifa));
387 while (enumerator->enumerate(enumerator, &type, &sockaddr))
388 {
389 if (type == RTAX_IFA)
390 {
391 host = host_create_from_sockaddr(sockaddr);
392 break;
393 }
394 }
395 enumerator->destroy(enumerator);
396
397 if (!host)
398 {
399 return;
400 }
401
402 this->lock->write_lock(this->lock);
403 ifaces = this->ifaces->create_enumerator(this->ifaces);
404 while (ifaces->enumerate(ifaces, &iface))
405 {
406 if (iface->ifindex == ifa->ifam_index)
407 {
408 addrs = iface->addrs->create_enumerator(iface->addrs);
409 while (addrs->enumerate(addrs, &addr))
410 {
411 if (host->ip_equals(host, addr->ip))
412 {
413 found = TRUE;
414 if (ifa->ifam_type == RTM_DELADDR)
415 {
416 iface->addrs->remove_at(iface->addrs, addrs);
417 if (!addr->virtual && iface->usable)
418 {
419 changed = TRUE;
420 DBG1(DBG_KNL, "%H disappeared from %s",
421 host, iface->ifname);
422 }
423 addr_map_entry_remove(addr, iface, this);
424 addr_entry_destroy(addr);
425 }
426 else if (ifa->ifam_type == RTM_NEWADDR && addr->virtual)
427 {
428 addr->refcount = 1;
429 }
430 }
431 }
432 addrs->destroy(addrs);
433
434 if (!found && ifa->ifam_type == RTM_NEWADDR)
435 {
436 INIT(addr,
437 .ip = host->clone(host),
438 .refcount = 1,
439 );
440 changed = TRUE;
441 iface->addrs->insert_last(iface->addrs, addr);
442 addr_map_entry_add(this, addr, iface);
443 if (iface->usable)
444 {
445 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
446 }
447 }
448
449 if (changed && iface_entry_up_and_usable(iface))
450 {
451 roam = TRUE;
452 }
453 break;
454 }
455 }
456 ifaces->destroy(ifaces);
457 this->lock->unlock(this->lock);
458 host->destroy(host);
459
460 if (roam)
461 {
462 fire_roam_event(this, TRUE);
463 }
464 }
465
466 /**
467 * Process an RTM_IFINFO message from the kernel
468 */
469 static void process_link(private_kernel_pfroute_net_t *this,
470 struct if_msghdr *msg)
471 {
472 enumerator_t *enumerator;
473 iface_entry_t *iface;
474 bool roam = FALSE;
475
476 this->lock->write_lock(this->lock);
477 enumerator = this->ifaces->create_enumerator(this->ifaces);
478 while (enumerator->enumerate(enumerator, &iface))
479 {
480 if (iface->ifindex == msg->ifm_index)
481 {
482 if (iface->usable)
483 {
484 if (!(iface->flags & IFF_UP) && (msg->ifm_flags & IFF_UP))
485 {
486 roam = TRUE;
487 DBG1(DBG_KNL, "interface %s activated", iface->ifname);
488 }
489 else if ((iface->flags & IFF_UP) && !(msg->ifm_flags & IFF_UP))
490 {
491 roam = TRUE;
492 DBG1(DBG_KNL, "interface %s deactivated", iface->ifname);
493 }
494 }
495 iface->flags = msg->ifm_flags;
496 break;
497 }
498 }
499 enumerator->destroy(enumerator);
500 this->lock->unlock(this->lock);
501
502 if (roam)
503 {
504 fire_roam_event(this, TRUE);
505 }
506 }
507
508 /**
509 * Process an RTM_*ROUTE message from the kernel
510 */
511 static void process_route(private_kernel_pfroute_net_t *this,
512 struct rt_msghdr *msg)
513 {
514
515 }
516
517 /**
518 * Receives PF_ROUTE messages from kernel
519 */
520 static job_requeue_t receive_events(private_kernel_pfroute_net_t *this)
521 {
522 struct {
523 union {
524 struct rt_msghdr rtm;
525 struct if_msghdr ifm;
526 struct ifa_msghdr ifam;
527 };
528 char buf[sizeof(struct sockaddr_storage) * RTAX_MAX];
529 } msg;
530 int len, hdrlen;
531 bool oldstate;
532
533 oldstate = thread_cancelability(TRUE);
534 len = recv(this->socket, &msg, sizeof(msg), 0);
535 thread_cancelability(oldstate);
536
537 if (len < 0)
538 {
539 switch (errno)
540 {
541 case EINTR:
542 case EAGAIN:
543 return JOB_REQUEUE_DIRECT;
544 default:
545 DBG1(DBG_KNL, "unable to receive from PF_ROUTE event socket");
546 sleep(1);
547 return JOB_REQUEUE_FAIR;
548 }
549 }
550
551 if (len < offsetof(struct rt_msghdr, rtm_flags) || len < msg.rtm.rtm_msglen)
552 {
553 DBG1(DBG_KNL, "received invalid PF_ROUTE message");
554 return JOB_REQUEUE_DIRECT;
555 }
556 if (msg.rtm.rtm_version != RTM_VERSION)
557 {
558 DBG1(DBG_KNL, "received PF_ROUTE message with unsupported version: %d",
559 msg.rtm.rtm_version);
560 return JOB_REQUEUE_DIRECT;
561 }
562 switch (msg.rtm.rtm_type)
563 {
564 case RTM_NEWADDR:
565 case RTM_DELADDR:
566 hdrlen = sizeof(msg.ifam);
567 break;
568 case RTM_IFINFO:
569 hdrlen = sizeof(msg.ifm);
570 break;
571 case RTM_ADD:
572 case RTM_DELETE:
573 case RTM_GET:
574 hdrlen = sizeof(msg.rtm);
575 break;
576 default:
577 return JOB_REQUEUE_DIRECT;
578 }
579 if (msg.rtm.rtm_msglen < hdrlen)
580 {
581 DBG1(DBG_KNL, "ignoring short PF_ROUTE message");
582 return JOB_REQUEUE_DIRECT;
583 }
584 switch (msg.rtm.rtm_type)
585 {
586 case RTM_NEWADDR:
587 case RTM_DELADDR:
588 process_addr(this, &msg.ifam);
589 break;
590 case RTM_IFINFO:
591 process_link(this, &msg.ifm);
592 break;
593 case RTM_ADD:
594 case RTM_DELETE:
595 process_route(this, &msg.rtm);
596 break;
597 default:
598 break;
599 }
600
601 this->mutex->lock(this->mutex);
602 if (msg.rtm.rtm_pid == this->pid && msg.rtm.rtm_seq == this->waiting_seq)
603 {
604 /* seems like the message someone is waiting for, deliver */
605 this->reply = realloc(this->reply, msg.rtm.rtm_msglen);
606 memcpy(this->reply, &msg, msg.rtm.rtm_msglen);
607 this->condvar->signal(this->condvar);
608 }
609 this->mutex->unlock(this->mutex);
610
611 return JOB_REQUEUE_DIRECT;
612 }
613
614
615 /** enumerator over addresses */
616 typedef struct {
617 private_kernel_pfroute_net_t* this;
618 /** which addresses to enumerate */
619 kernel_address_type_t which;
620 } address_enumerator_t;
621
622 /**
623 * cleanup function for address enumerator
624 */
625 static void address_enumerator_destroy(address_enumerator_t *data)
626 {
627 data->this->lock->unlock(data->this->lock);
628 free(data);
629 }
630
631 /**
632 * filter for addresses
633 */
634 static bool filter_addresses(address_enumerator_t *data,
635 addr_entry_t** in, host_t** out)
636 {
637 host_t *ip;
638 if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->virtual)
639 { /* skip virtual interfaces added by us */
640 return FALSE;
641 }
642 ip = (*in)->ip;
643 if (ip->get_family(ip) == AF_INET6)
644 {
645 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ip->get_sockaddr(ip);
646 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
647 { /* skip addresses with a unusable scope */
648 return FALSE;
649 }
650 }
651 *out = ip;
652 return TRUE;
653 }
654
655 /**
656 * enumerator constructor for interfaces
657 */
658 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
659 address_enumerator_t *data)
660 {
661 return enumerator_create_filter(iface->addrs->create_enumerator(iface->addrs),
662 (void*)filter_addresses, data, NULL);
663 }
664
665 /**
666 * filter for interfaces
667 */
668 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
669 iface_entry_t** out)
670 {
671 if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
672 { /* skip interfaces excluded by config */
673 return FALSE;
674 }
675 if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
676 { /* ignore loopback devices */
677 return FALSE;
678 }
679 if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
680 { /* skip interfaces not up */
681 return FALSE;
682 }
683 *out = *in;
684 return TRUE;
685 }
686
687 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
688 private_kernel_pfroute_net_t *this, kernel_address_type_t which)
689 {
690 address_enumerator_t *data;
691
692 INIT(data,
693 .this = this,
694 .which = which,
695 );
696
697 this->lock->read_lock(this->lock);
698 return enumerator_create_nested(
699 enumerator_create_filter(
700 this->ifaces->create_enumerator(this->ifaces),
701 (void*)filter_interfaces, data, NULL),
702 (void*)create_iface_enumerator, data,
703 (void*)address_enumerator_destroy);
704 }
705
706 METHOD(kernel_net_t, get_interface_name, bool,
707 private_kernel_pfroute_net_t *this, host_t* ip, char **name)
708 {
709 addr_map_entry_t *entry, lookup = {
710 .ip = ip,
711 };
712
713 if (ip->is_anyaddr(ip))
714 {
715 return FALSE;
716 }
717 this->lock->read_lock(this->lock);
718 /* first try to find it on an up and usable interface */
719 entry = this->addrs->get_match(this->addrs, &lookup,
720 (void*)addr_map_entry_match_up_and_usable);
721 if (entry)
722 {
723 if (name)
724 {
725 *name = strdup(entry->iface->ifname);
726 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
727 }
728 this->lock->unlock(this->lock);
729 return TRUE;
730 }
731 /* maybe it is installed on an ignored interface */
732 entry = this->addrs->get_match(this->addrs, &lookup,
733 (void*)addr_map_entry_match_up);
734 if (!entry)
735 { /* the address does not exist, is on a down interface */
736 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
737 }
738 this->lock->unlock(this->lock);
739 return FALSE;
740 }
741
742 METHOD(kernel_net_t, get_source_addr, host_t*,
743 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
744 {
745 return NULL;
746 }
747
748 METHOD(kernel_net_t, get_nexthop, host_t*,
749 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
750 {
751 return NULL;
752 }
753
754 METHOD(kernel_net_t, add_ip, status_t,
755 private_kernel_pfroute_net_t *this, host_t *virtual_ip, int prefix,
756 char *iface)
757 {
758 return FAILED;
759 }
760
761 METHOD(kernel_net_t, del_ip, status_t,
762 private_kernel_pfroute_net_t *this, host_t *virtual_ip, int prefix,
763 bool wait)
764 {
765 return FAILED;
766 }
767
768 METHOD(kernel_net_t, add_route, status_t,
769 private_kernel_pfroute_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
770 host_t *gateway, host_t *src_ip, char *if_name)
771 {
772 return FAILED;
773 }
774
775 METHOD(kernel_net_t, del_route, status_t,
776 private_kernel_pfroute_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
777 host_t *gateway, host_t *src_ip, char *if_name)
778 {
779 return FAILED;
780 }
781
782 /**
783 * Initialize a list of local addresses.
784 */
785 static status_t init_address_list(private_kernel_pfroute_net_t *this)
786 {
787 struct ifaddrs *ifap, *ifa;
788 iface_entry_t *iface, *current;
789 addr_entry_t *addr;
790 enumerator_t *ifaces, *addrs;
791
792 DBG2(DBG_KNL, "known interfaces and IP addresses:");
793
794 if (getifaddrs(&ifap) < 0)
795 {
796 DBG1(DBG_KNL, " failed to get interfaces!");
797 return FAILED;
798 }
799
800 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
801 {
802 if (ifa->ifa_addr == NULL)
803 {
804 continue;
805 }
806 switch(ifa->ifa_addr->sa_family)
807 {
808 case AF_LINK:
809 case AF_INET:
810 case AF_INET6:
811 {
812 iface = NULL;
813 ifaces = this->ifaces->create_enumerator(this->ifaces);
814 while (ifaces->enumerate(ifaces, &current))
815 {
816 if (streq(current->ifname, ifa->ifa_name))
817 {
818 iface = current;
819 break;
820 }
821 }
822 ifaces->destroy(ifaces);
823
824 if (!iface)
825 {
826 INIT(iface,
827 .ifindex = if_nametoindex(ifa->ifa_name),
828 .flags = ifa->ifa_flags,
829 .addrs = linked_list_create(),
830 .usable = hydra->kernel_interface->is_interface_usable(
831 hydra->kernel_interface, ifa->ifa_name),
832 );
833 memcpy(iface->ifname, ifa->ifa_name, IFNAMSIZ);
834 this->ifaces->insert_last(this->ifaces, iface);
835 }
836
837 if (ifa->ifa_addr->sa_family != AF_LINK)
838 {
839 INIT(addr,
840 .ip = host_create_from_sockaddr(ifa->ifa_addr),
841 .refcount = 1,
842 );
843 iface->addrs->insert_last(iface->addrs, addr);
844 addr_map_entry_add(this, addr, iface);
845 }
846 }
847 }
848 }
849 freeifaddrs(ifap);
850
851 ifaces = this->ifaces->create_enumerator(this->ifaces);
852 while (ifaces->enumerate(ifaces, &iface))
853 {
854 if (iface->usable && iface->flags & IFF_UP)
855 {
856 DBG2(DBG_KNL, " %s", iface->ifname);
857 addrs = iface->addrs->create_enumerator(iface->addrs);
858 while (addrs->enumerate(addrs, (void**)&addr))
859 {
860 DBG2(DBG_KNL, " %H", addr->ip);
861 }
862 addrs->destroy(addrs);
863 }
864 }
865 ifaces->destroy(ifaces);
866
867 return SUCCESS;
868 }
869
870 METHOD(kernel_net_t, destroy, void,
871 private_kernel_pfroute_net_t *this)
872 {
873 enumerator_t *enumerator;
874 addr_entry_t *addr;
875
876 if (this->socket != -1)
877 {
878 close(this->socket);
879 }
880 enumerator = this->addrs->create_enumerator(this->addrs);
881 while (enumerator->enumerate(enumerator, NULL, (void**)&addr))
882 {
883 free(addr);
884 }
885 enumerator->destroy(enumerator);
886 this->addrs->destroy(this->addrs);
887 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
888 this->lock->destroy(this->lock);
889 this->mutex->destroy(this->mutex);
890 this->condvar->destroy(this->condvar);
891 free(this->reply);
892 free(this);
893 }
894
895 /*
896 * Described in header.
897 */
898 kernel_pfroute_net_t *kernel_pfroute_net_create()
899 {
900 private_kernel_pfroute_net_t *this;
901
902 INIT(this,
903 .public = {
904 .interface = {
905 .get_interface = _get_interface_name,
906 .create_address_enumerator = _create_address_enumerator,
907 .get_source_addr = _get_source_addr,
908 .get_nexthop = _get_nexthop,
909 .add_ip = _add_ip,
910 .del_ip = _del_ip,
911 .add_route = _add_route,
912 .del_route = _del_route,
913 .destroy = _destroy,
914 },
915 },
916 .pid = getpid(),
917 .ifaces = linked_list_create(),
918 .addrs = hashtable_create(
919 (hashtable_hash_t)addr_map_entry_hash,
920 (hashtable_equals_t)addr_map_entry_equals, 16),
921 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
922 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
923 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
924 );
925
926 /* create a PF_ROUTE socket to communicate with the kernel */
927 this->socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
928 if (this->socket == -1)
929 {
930 DBG1(DBG_KNL, "unable to create PF_ROUTE socket");
931 destroy(this);
932 return NULL;
933 }
934
935 if (streq(hydra->daemon, "starter"))
936 {
937 /* starter has no threads, so we do not register for kernel events */
938 if (shutdown(this->socket, SHUT_RD) != 0)
939 {
940 DBG1(DBG_KNL, "closing read end of PF_ROUTE socket failed: %s",
941 strerror(errno));
942 }
943 }
944 else
945 {
946 lib->processor->queue_job(lib->processor,
947 (job_t*)callback_job_create_with_prio(
948 (callback_job_cb_t)receive_events, this, NULL,
949 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
950 }
951 if (init_address_list(this) != SUCCESS)
952 {
953 DBG1(DBG_KNL, "unable to get interface list");
954 destroy(this);
955 return NULL;
956 }
957
958 return &this->public;
959 }