c1224cc983067ab1f13c1a61c5d7307e33672444
[strongswan.git] / src / libhydra / plugins / kernel_pfroute / kernel_pfroute_net.c
1 /*
2 * Copyright (C) 2009-2013 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 <net/if_dl.h>
20 #include <ifaddrs.h>
21 #include <net/route.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include "kernel_pfroute_net.h"
26
27 #include <hydra.h>
28 #include <utils/debug.h>
29 #include <networking/host.h>
30 #include <networking/tun_device.h>
31 #include <threading/thread.h>
32 #include <threading/mutex.h>
33 #include <threading/condvar.h>
34 #include <threading/rwlock.h>
35 #include <collections/hashtable.h>
36 #include <collections/linked_list.h>
37 #include <processing/jobs/callback_job.h>
38
39 #ifndef HAVE_STRUCT_SOCKADDR_SA_LEN
40 #error Cannot compile this plugin on systems where 'struct sockaddr' has no sa_len member.
41 #endif
42
43 /** properly align sockaddrs */
44 #ifdef __APPLE__
45 /* Apple always uses 4 bytes */
46 #define SA_ALIGN 4
47 #else
48 /* while on other platforms like FreeBSD it depends on the architecture */
49 #define SA_ALIGN sizeof(long)
50 #endif
51 #define SA_LEN(len) ((len) > 0 ? (((len)+SA_ALIGN-1) & ~(SA_ALIGN-1)) : SA_ALIGN)
52
53 /** delay before firing roam events (ms) */
54 #define ROAM_DELAY 100
55
56 /** delay before reinstalling routes (ms) */
57 #define ROUTE_DELAY 100
58
59 typedef struct addr_entry_t addr_entry_t;
60
61 /**
62 * IP address in an inface_entry_t
63 */
64 struct addr_entry_t {
65
66 /** The ip address */
67 host_t *ip;
68
69 /** virtual IP managed by us */
70 bool virtual;
71 };
72
73 /**
74 * destroy a addr_entry_t object
75 */
76 static void addr_entry_destroy(addr_entry_t *this)
77 {
78 this->ip->destroy(this->ip);
79 free(this);
80 }
81
82 typedef struct iface_entry_t iface_entry_t;
83
84 /**
85 * A network interface on this system, containing addr_entry_t's
86 */
87 struct iface_entry_t {
88
89 /** interface index */
90 int ifindex;
91
92 /** name of the interface */
93 char ifname[IFNAMSIZ];
94
95 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
96 u_int flags;
97
98 /** list of addresses as host_t */
99 linked_list_t *addrs;
100
101 /** TRUE if usable by config */
102 bool usable;
103 };
104
105 /**
106 * destroy an interface entry
107 */
108 static void iface_entry_destroy(iface_entry_t *this)
109 {
110 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
111 free(this);
112 }
113
114 /**
115 * check if an interface is up
116 */
117 static inline bool iface_entry_up(iface_entry_t *iface)
118 {
119 return (iface->flags & IFF_UP) == IFF_UP;
120 }
121
122 /**
123 * check if an interface is up and usable
124 */
125 static inline bool iface_entry_up_and_usable(iface_entry_t *iface)
126 {
127 return iface->usable && iface_entry_up(iface);
128 }
129
130 typedef struct addr_map_entry_t addr_map_entry_t;
131
132 /**
133 * Entry that maps an IP address to an interface entry
134 */
135 struct addr_map_entry_t {
136 /** The IP address */
137 host_t *ip;
138
139 /** The interface this address is installed on */
140 iface_entry_t *iface;
141 };
142
143 /**
144 * Hash a addr_map_entry_t object, all entries with the same IP address
145 * are stored in the same bucket
146 */
147 static u_int addr_map_entry_hash(addr_map_entry_t *this)
148 {
149 return chunk_hash(this->ip->get_address(this->ip));
150 }
151
152 /**
153 * Compare two addr_map_entry_t objects, two entries are equal if they are
154 * installed on the same interface
155 */
156 static bool addr_map_entry_equals(addr_map_entry_t *a, addr_map_entry_t *b)
157 {
158 return a->iface->ifindex == b->iface->ifindex &&
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 * an up and usable interface
165 */
166 static bool addr_map_entry_match_up_and_usable(addr_map_entry_t *a,
167 addr_map_entry_t *b)
168 {
169 return iface_entry_up_and_usable(b->iface) &&
170 a->ip->ip_equals(a->ip, b->ip);
171 }
172
173 /**
174 * Used with get_match this finds an address entry if it is installed on
175 * any active local interface
176 */
177 static bool addr_map_entry_match_up(addr_map_entry_t *a, addr_map_entry_t *b)
178 {
179 return iface_entry_up(b->iface) && a->ip->ip_equals(a->ip, b->ip);
180 }
181
182 typedef struct route_entry_t route_entry_t;
183
184 /**
185 * Installed routing entry
186 */
187 struct route_entry_t {
188 /** Name of the interface the route is bound to */
189 char *if_name;
190
191 /** Gateway for this route */
192 host_t *gateway;
193
194 /** Destination net */
195 chunk_t dst_net;
196
197 /** Destination net prefixlen */
198 u_int8_t prefixlen;
199 };
200
201 /**
202 * Clone a route_entry_t object.
203 */
204 static route_entry_t *route_entry_clone(route_entry_t *this)
205 {
206 route_entry_t *route;
207
208 INIT(route,
209 .if_name = strdup(this->if_name),
210 .gateway = this->gateway ? this->gateway->clone(this->gateway) : NULL,
211 .dst_net = chunk_clone(this->dst_net),
212 .prefixlen = this->prefixlen,
213 );
214 return route;
215 }
216
217 /**
218 * Destroy a route_entry_t object
219 */
220 static void route_entry_destroy(route_entry_t *this)
221 {
222 free(this->if_name);
223 DESTROY_IF(this->gateway);
224 chunk_free(&this->dst_net);
225 free(this);
226 }
227
228 /**
229 * Hash a route_entry_t object
230 */
231 static u_int route_entry_hash(route_entry_t *this)
232 {
233 return chunk_hash_inc(chunk_from_thing(this->prefixlen),
234 chunk_hash(this->dst_net));
235 }
236
237 /**
238 * Compare two route_entry_t objects
239 */
240 static bool route_entry_equals(route_entry_t *a, route_entry_t *b)
241 {
242 if (a->if_name && b->if_name && streq(a->if_name, b->if_name) &&
243 chunk_equals(a->dst_net, b->dst_net) && a->prefixlen == b->prefixlen)
244 {
245 return (!a->gateway && !b->gateway) || (a->gateway && b->gateway &&
246 a->gateway->ip_equals(a->gateway, b->gateway));
247 }
248 return FALSE;
249 }
250
251 typedef struct net_change_t net_change_t;
252
253 /**
254 * Queued network changes
255 */
256 struct net_change_t {
257 /** Name of the interface that got activated (or an IP appeared on) */
258 char *if_name;
259 };
260
261 /**
262 * Destroy a net_change_t object
263 */
264 static void net_change_destroy(net_change_t *this)
265 {
266 free(this->if_name);
267 free(this);
268 }
269
270 /**
271 * Hash a net_change_t object
272 */
273 static u_int net_change_hash(net_change_t *this)
274 {
275 return chunk_hash(chunk_create(this->if_name, strlen(this->if_name)));
276 }
277
278 /**
279 * Compare two net_change_t objects
280 */
281 static bool net_change_equals(net_change_t *a, net_change_t *b)
282 {
283 return streq(a->if_name, b->if_name);
284 }
285
286 typedef struct private_kernel_pfroute_net_t private_kernel_pfroute_net_t;
287
288 /**
289 * Private variables and functions of kernel_pfroute class.
290 */
291 struct private_kernel_pfroute_net_t
292 {
293 /**
294 * Public part of the kernel_pfroute_t object.
295 */
296 kernel_pfroute_net_t public;
297
298 /**
299 * lock to access lists and maps
300 */
301 rwlock_t *lock;
302
303 /**
304 * Cached list of interfaces and their addresses (iface_entry_t)
305 */
306 linked_list_t *ifaces;
307
308 /**
309 * Map for IP addresses to iface_entry_t objects (addr_map_entry_t)
310 */
311 hashtable_t *addrs;
312
313 /**
314 * List of tun devices we installed for virtual IPs
315 */
316 linked_list_t *tuns;
317
318 /**
319 * mutex to communicate exclusively with PF_KEY
320 */
321 mutex_t *mutex;
322
323 /**
324 * condvar to signal if PF_KEY query got a response
325 */
326 condvar_t *condvar;
327
328 /**
329 * installed routes
330 */
331 hashtable_t *routes;
332
333 /**
334 * mutex for routes
335 */
336 mutex_t *routes_lock;
337
338 /**
339 * interface changes which may trigger route reinstallation
340 */
341 hashtable_t *net_changes;
342
343 /**
344 * mutex for route reinstallation triggers
345 */
346 mutex_t *net_changes_lock;
347
348 /**
349 * time of last route reinstallation
350 */
351 timeval_t last_route_reinstall;
352
353 /**
354 * pid to send PF_ROUTE messages with
355 */
356 pid_t pid;
357
358 /**
359 * PF_ROUTE socket to communicate with the kernel
360 */
361 int socket;
362
363 /**
364 * sequence number for messages sent to the kernel
365 */
366 int seq;
367
368 /**
369 * Sequence number a query is waiting for
370 */
371 int waiting_seq;
372
373 /**
374 * Allocated reply message from kernel
375 */
376 struct rt_msghdr *reply;
377
378 /**
379 * time of last roam event
380 */
381 timeval_t last_roam;
382
383 /**
384 * Time in ms to wait for IP addresses to appear/disappear
385 */
386 int vip_wait;
387 };
388
389
390 /**
391 * Forward declaration
392 */
393 static status_t manage_route(private_kernel_pfroute_net_t *this, int op,
394 chunk_t dst_net, u_int8_t prefixlen,
395 host_t *gateway, char *if_name);
396
397 /**
398 * Clear the queued network changes.
399 */
400 static void net_changes_clear(private_kernel_pfroute_net_t *this)
401 {
402 enumerator_t *enumerator;
403 net_change_t *change;
404
405 enumerator = this->net_changes->create_enumerator(this->net_changes);
406 while (enumerator->enumerate(enumerator, NULL, (void**)&change))
407 {
408 this->net_changes->remove_at(this->net_changes, enumerator);
409 net_change_destroy(change);
410 }
411 enumerator->destroy(enumerator);
412 }
413
414 /**
415 * Act upon queued network changes.
416 */
417 static job_requeue_t reinstall_routes(private_kernel_pfroute_net_t *this)
418 {
419 enumerator_t *enumerator;
420 route_entry_t *route;
421
422 this->net_changes_lock->lock(this->net_changes_lock);
423 this->routes_lock->lock(this->routes_lock);
424
425 enumerator = this->routes->create_enumerator(this->routes);
426 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
427 {
428 net_change_t *change, lookup = {
429 .if_name = route->if_name,
430 };
431 /* check if a change for the outgoing interface is queued */
432 change = this->net_changes->get(this->net_changes, &lookup);
433 if (change)
434 {
435 manage_route(this, RTM_ADD, route->dst_net, route->prefixlen,
436 route->gateway, route->if_name);
437 }
438 }
439 enumerator->destroy(enumerator);
440 this->routes_lock->unlock(this->routes_lock);
441
442 net_changes_clear(this);
443 this->net_changes_lock->unlock(this->net_changes_lock);
444 return JOB_REQUEUE_NONE;
445 }
446
447 /**
448 * Queue route reinstallation caused by network changes for a given interface.
449 *
450 * The route reinstallation is delayed for a while and only done once for
451 * several calls during this delay, in order to avoid doing it too often.
452 * The interface name is freed.
453 */
454 static void queue_route_reinstall(private_kernel_pfroute_net_t *this,
455 char *if_name)
456 {
457 net_change_t *update, *found;
458 timeval_t now;
459 job_t *job;
460
461 INIT(update,
462 .if_name = if_name
463 );
464
465 this->net_changes_lock->lock(this->net_changes_lock);
466 found = this->net_changes->put(this->net_changes, update, update);
467 if (found)
468 {
469 net_change_destroy(found);
470 }
471 time_monotonic(&now);
472 if (timercmp(&now, &this->last_route_reinstall, >))
473 {
474 timeval_add_ms(&now, ROUTE_DELAY);
475 this->last_route_reinstall = now;
476
477 job = (job_t*)callback_job_create((callback_job_cb_t)reinstall_routes,
478 this, NULL, NULL);
479 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROUTE_DELAY);
480 }
481 this->net_changes_lock->unlock(this->net_changes_lock);
482 }
483
484 /**
485 * Add an address map entry
486 */
487 static void addr_map_entry_add(private_kernel_pfroute_net_t *this,
488 addr_entry_t *addr, iface_entry_t *iface)
489 {
490 addr_map_entry_t *entry;
491
492 if (addr->virtual)
493 { /* don't map virtual IPs */
494 return;
495 }
496
497 INIT(entry,
498 .ip = addr->ip,
499 .iface = iface,
500 );
501 entry = this->addrs->put(this->addrs, entry, entry);
502 free(entry);
503 }
504
505 /**
506 * Remove an address map entry (the argument order is a bit strange because
507 * it is also used with linked_list_t.invoke_function)
508 */
509 static void addr_map_entry_remove(addr_entry_t *addr, iface_entry_t *iface,
510 private_kernel_pfroute_net_t *this)
511 {
512 addr_map_entry_t *entry, lookup = {
513 .ip = addr->ip,
514 .iface = iface,
515 };
516
517 if (addr->virtual)
518 { /* these are never mapped, but this check avoid problems if a virtual IP
519 * equals a regular one */
520 return;
521 }
522 entry = this->addrs->remove(this->addrs, &lookup);
523 free(entry);
524 }
525
526 /**
527 * callback function that raises the delayed roam event
528 */
529 static job_requeue_t roam_event(uintptr_t address)
530 {
531 hydra->kernel_interface->roam(hydra->kernel_interface, address != 0);
532 return JOB_REQUEUE_NONE;
533 }
534
535 /**
536 * fire a roaming event. we delay it for a bit and fire only one event
537 * for multiple calls. otherwise we would create too many events.
538 */
539 static void fire_roam_event(private_kernel_pfroute_net_t *this, bool address)
540 {
541 timeval_t now;
542 job_t *job;
543
544 time_monotonic(&now);
545 if (timercmp(&now, &this->last_roam, >))
546 {
547 timeval_add_ms(&now, ROAM_DELAY);
548 this->last_roam = now;
549
550 job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
551 (void*)(uintptr_t)(address ? 1 : 0),
552 NULL, NULL);
553 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
554 }
555 }
556
557 /**
558 * Data for enumerator over rtmsg sockaddrs
559 */
560 typedef struct {
561 /** implements enumerator */
562 enumerator_t public;
563 /** copy of attribute bitfield */
564 int types;
565 /** bytes remaining in buffer */
566 int remaining;
567 /** next sockaddr to enumerate */
568 struct sockaddr *addr;
569 } rt_enumerator_t;
570
571 METHOD(enumerator_t, rt_enumerate, bool,
572 rt_enumerator_t *this, int *xtype, struct sockaddr **addr)
573 {
574 int i, type;
575
576 if (this->remaining < sizeof(this->addr->sa_len) ||
577 this->remaining < this->addr->sa_len)
578 {
579 return FALSE;
580 }
581 for (i = 0; i < RTAX_MAX; i++)
582 {
583 type = (1 << i);
584 if (this->types & type)
585 {
586 this->types &= ~type;
587 *addr = this->addr;
588 *xtype = i;
589 this->remaining -= SA_LEN(this->addr->sa_len);
590 this->addr = (struct sockaddr*)((char*)this->addr +
591 SA_LEN(this->addr->sa_len));
592 return TRUE;
593 }
594 }
595 return FALSE;
596 }
597
598 /**
599 * Create a safe enumerator over sockaddrs in ifa/ifam/rt_msg
600 */
601 static enumerator_t *create_rtmsg_enumerator(void *hdr, size_t hdrlen)
602 {
603 struct rt_msghdr *rthdr = hdr;
604 rt_enumerator_t *this;
605
606 INIT(this,
607 .public = {
608 .enumerate = (void*)_rt_enumerate,
609 .destroy = (void*)free,
610 },
611 .types = rthdr->rtm_addrs,
612 .remaining = rthdr->rtm_msglen - hdrlen,
613 .addr = hdr + hdrlen,
614 );
615 return &this->public;
616 }
617
618 /**
619 * Process an RTM_*ADDR message from the kernel
620 */
621 static void process_addr(private_kernel_pfroute_net_t *this,
622 struct ifa_msghdr *ifa)
623 {
624 struct sockaddr *sockaddr;
625 host_t *host = NULL;
626 enumerator_t *ifaces, *addrs;
627 iface_entry_t *iface;
628 addr_entry_t *addr;
629 bool found = FALSE, changed = FALSE, roam = FALSE;
630 enumerator_t *enumerator;
631 char *ifname = NULL;
632 int type;
633
634 enumerator = create_rtmsg_enumerator(ifa, sizeof(*ifa));
635 while (enumerator->enumerate(enumerator, &type, &sockaddr))
636 {
637 if (type == RTAX_IFA)
638 {
639 host = host_create_from_sockaddr(sockaddr);
640 break;
641 }
642 }
643 enumerator->destroy(enumerator);
644
645 if (!host)
646 {
647 return;
648 }
649
650 this->lock->write_lock(this->lock);
651 ifaces = this->ifaces->create_enumerator(this->ifaces);
652 while (ifaces->enumerate(ifaces, &iface))
653 {
654 if (iface->ifindex == ifa->ifam_index)
655 {
656 addrs = iface->addrs->create_enumerator(iface->addrs);
657 while (addrs->enumerate(addrs, &addr))
658 {
659 if (host->ip_equals(host, addr->ip))
660 {
661 found = TRUE;
662 if (ifa->ifam_type == RTM_DELADDR)
663 {
664 iface->addrs->remove_at(iface->addrs, addrs);
665 if (!addr->virtual && iface->usable)
666 {
667 changed = TRUE;
668 DBG1(DBG_KNL, "%H disappeared from %s",
669 host, iface->ifname);
670 }
671 addr_map_entry_remove(addr, iface, this);
672 addr_entry_destroy(addr);
673 }
674 }
675 }
676 addrs->destroy(addrs);
677
678 if (!found && ifa->ifam_type == RTM_NEWADDR)
679 {
680 INIT(addr,
681 .ip = host->clone(host),
682 );
683 changed = TRUE;
684 ifname = strdup(iface->ifname);
685 iface->addrs->insert_last(iface->addrs, addr);
686 addr_map_entry_add(this, addr, iface);
687 if (iface->usable)
688 {
689 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
690 }
691 }
692
693 if (changed && iface_entry_up_and_usable(iface))
694 {
695 roam = TRUE;
696 }
697 break;
698 }
699 }
700 ifaces->destroy(ifaces);
701 this->lock->unlock(this->lock);
702 host->destroy(host);
703
704 if (roam && ifname)
705 {
706 queue_route_reinstall(this, ifname);
707 }
708 else
709 {
710 free(ifname);
711 }
712
713 if (roam)
714 {
715 fire_roam_event(this, TRUE);
716 }
717 }
718
719 /**
720 * Re-initialize address list of an interface if it changes state
721 */
722 static void repopulate_iface(private_kernel_pfroute_net_t *this,
723 iface_entry_t *iface)
724 {
725 struct ifaddrs *ifap, *ifa;
726 addr_entry_t *addr;
727
728 while (iface->addrs->remove_last(iface->addrs, (void**)&addr) == SUCCESS)
729 {
730 addr_map_entry_remove(addr, iface, this);
731 addr_entry_destroy(addr);
732 }
733
734 if (getifaddrs(&ifap) == 0)
735 {
736 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
737 {
738 if (ifa->ifa_addr && streq(ifa->ifa_name, iface->ifname))
739 {
740 switch (ifa->ifa_addr->sa_family)
741 {
742 case AF_INET:
743 case AF_INET6:
744 INIT(addr,
745 .ip = host_create_from_sockaddr(ifa->ifa_addr),
746 );
747 iface->addrs->insert_last(iface->addrs, addr);
748 addr_map_entry_add(this, addr, iface);
749 break;
750 default:
751 break;
752 }
753 }
754 }
755 freeifaddrs(ifap);
756 }
757 }
758
759 /**
760 * Process an RTM_IFINFO message from the kernel
761 */
762 static void process_link(private_kernel_pfroute_net_t *this,
763 struct if_msghdr *msg)
764 {
765 enumerator_t *enumerator;
766 iface_entry_t *iface;
767 bool roam = FALSE, found = FALSE, update_routes = FALSE;
768
769 this->lock->write_lock(this->lock);
770 enumerator = this->ifaces->create_enumerator(this->ifaces);
771 while (enumerator->enumerate(enumerator, &iface))
772 {
773 if (iface->ifindex == msg->ifm_index)
774 {
775 if (iface->usable)
776 {
777 if (!(iface->flags & IFF_UP) && (msg->ifm_flags & IFF_UP))
778 {
779 roam = update_routes = TRUE;
780 DBG1(DBG_KNL, "interface %s activated", iface->ifname);
781 }
782 else if ((iface->flags & IFF_UP) && !(msg->ifm_flags & IFF_UP))
783 {
784 roam = TRUE;
785 DBG1(DBG_KNL, "interface %s deactivated", iface->ifname);
786 }
787 }
788 iface->flags = msg->ifm_flags;
789 repopulate_iface(this, iface);
790 found = TRUE;
791 break;
792 }
793 }
794 enumerator->destroy(enumerator);
795
796 if (!found)
797 {
798 INIT(iface,
799 .ifindex = msg->ifm_index,
800 .flags = msg->ifm_flags,
801 .addrs = linked_list_create(),
802 );
803 if (if_indextoname(iface->ifindex, iface->ifname))
804 {
805 DBG1(DBG_KNL, "interface %s appeared", iface->ifname);
806 iface->usable = hydra->kernel_interface->is_interface_usable(
807 hydra->kernel_interface, iface->ifname);
808 repopulate_iface(this, iface);
809 this->ifaces->insert_last(this->ifaces, iface);
810 if (iface->usable)
811 {
812 roam = update_routes = TRUE;
813 }
814 }
815 else
816 {
817 free(iface);
818 }
819 }
820 this->lock->unlock(this->lock);
821
822 if (update_routes)
823 {
824 queue_route_reinstall(this, strdup(iface->ifname));
825 }
826
827 if (roam)
828 {
829 fire_roam_event(this, TRUE);
830 }
831 }
832
833 /**
834 * Process an RTM_*ROUTE message from the kernel
835 */
836 static void process_route(private_kernel_pfroute_net_t *this,
837 struct rt_msghdr *msg)
838 {
839
840 }
841
842 /**
843 * Receives PF_ROUTE messages from kernel
844 */
845 static job_requeue_t receive_events(private_kernel_pfroute_net_t *this)
846 {
847 struct {
848 union {
849 struct rt_msghdr rtm;
850 struct if_msghdr ifm;
851 struct ifa_msghdr ifam;
852 };
853 char buf[sizeof(struct sockaddr_storage) * RTAX_MAX];
854 } msg;
855 int len, hdrlen;
856 bool oldstate;
857
858 oldstate = thread_cancelability(TRUE);
859 len = recv(this->socket, &msg, sizeof(msg), 0);
860 thread_cancelability(oldstate);
861
862 if (len < 0)
863 {
864 switch (errno)
865 {
866 case EINTR:
867 case EAGAIN:
868 return JOB_REQUEUE_DIRECT;
869 default:
870 DBG1(DBG_KNL, "unable to receive from PF_ROUTE event socket");
871 sleep(1);
872 return JOB_REQUEUE_FAIR;
873 }
874 }
875
876 if (len < offsetof(struct rt_msghdr, rtm_flags) || len < msg.rtm.rtm_msglen)
877 {
878 DBG1(DBG_KNL, "received invalid PF_ROUTE message");
879 return JOB_REQUEUE_DIRECT;
880 }
881 if (msg.rtm.rtm_version != RTM_VERSION)
882 {
883 DBG1(DBG_KNL, "received PF_ROUTE message with unsupported version: %d",
884 msg.rtm.rtm_version);
885 return JOB_REQUEUE_DIRECT;
886 }
887 switch (msg.rtm.rtm_type)
888 {
889 case RTM_NEWADDR:
890 case RTM_DELADDR:
891 hdrlen = sizeof(msg.ifam);
892 break;
893 case RTM_IFINFO:
894 hdrlen = sizeof(msg.ifm);
895 break;
896 case RTM_ADD:
897 case RTM_DELETE:
898 case RTM_GET:
899 hdrlen = sizeof(msg.rtm);
900 break;
901 default:
902 return JOB_REQUEUE_DIRECT;
903 }
904 if (msg.rtm.rtm_msglen < hdrlen)
905 {
906 DBG1(DBG_KNL, "ignoring short PF_ROUTE message");
907 return JOB_REQUEUE_DIRECT;
908 }
909 switch (msg.rtm.rtm_type)
910 {
911 case RTM_NEWADDR:
912 case RTM_DELADDR:
913 process_addr(this, &msg.ifam);
914 break;
915 case RTM_IFINFO:
916 process_link(this, &msg.ifm);
917 break;
918 case RTM_ADD:
919 case RTM_DELETE:
920 process_route(this, &msg.rtm);
921 break;
922 default:
923 break;
924 }
925
926 this->mutex->lock(this->mutex);
927 if (msg.rtm.rtm_pid == this->pid && msg.rtm.rtm_seq == this->waiting_seq)
928 {
929 /* seems like the message someone is waiting for, deliver */
930 this->reply = realloc(this->reply, msg.rtm.rtm_msglen);
931 memcpy(this->reply, &msg, msg.rtm.rtm_msglen);
932 }
933 /* signal on any event, add_ip()/del_ip() might wait for it */
934 this->condvar->broadcast(this->condvar);
935 this->mutex->unlock(this->mutex);
936
937 return JOB_REQUEUE_DIRECT;
938 }
939
940
941 /** enumerator over addresses */
942 typedef struct {
943 private_kernel_pfroute_net_t* this;
944 /** which addresses to enumerate */
945 kernel_address_type_t which;
946 } address_enumerator_t;
947
948 /**
949 * cleanup function for address enumerator
950 */
951 static void address_enumerator_destroy(address_enumerator_t *data)
952 {
953 data->this->lock->unlock(data->this->lock);
954 free(data);
955 }
956
957 /**
958 * filter for addresses
959 */
960 static bool filter_addresses(address_enumerator_t *data,
961 addr_entry_t** in, host_t** out)
962 {
963 host_t *ip;
964 if (!(data->which & ADDR_TYPE_VIRTUAL) && (*in)->virtual)
965 { /* skip virtual interfaces added by us */
966 return FALSE;
967 }
968 if (!(data->which & ADDR_TYPE_REGULAR) && !(*in)->virtual)
969 { /* address is regular, but not requested */
970 return FALSE;
971 }
972 ip = (*in)->ip;
973 if (ip->get_family(ip) == AF_INET6)
974 {
975 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ip->get_sockaddr(ip);
976 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
977 { /* skip addresses with a unusable scope */
978 return FALSE;
979 }
980 }
981 *out = ip;
982 return TRUE;
983 }
984
985 /**
986 * enumerator constructor for interfaces
987 */
988 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
989 address_enumerator_t *data)
990 {
991 return enumerator_create_filter(iface->addrs->create_enumerator(iface->addrs),
992 (void*)filter_addresses, data, NULL);
993 }
994
995 /**
996 * filter for interfaces
997 */
998 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in,
999 iface_entry_t** out)
1000 {
1001 if (!(data->which & ADDR_TYPE_IGNORED) && !(*in)->usable)
1002 { /* skip interfaces excluded by config */
1003 return FALSE;
1004 }
1005 if (!(data->which & ADDR_TYPE_LOOPBACK) && ((*in)->flags & IFF_LOOPBACK))
1006 { /* ignore loopback devices */
1007 return FALSE;
1008 }
1009 if (!(data->which & ADDR_TYPE_DOWN) && !((*in)->flags & IFF_UP))
1010 { /* skip interfaces not up */
1011 return FALSE;
1012 }
1013 *out = *in;
1014 return TRUE;
1015 }
1016
1017 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
1018 private_kernel_pfroute_net_t *this, kernel_address_type_t which)
1019 {
1020 address_enumerator_t *data;
1021
1022 INIT(data,
1023 .this = this,
1024 .which = which,
1025 );
1026
1027 this->lock->read_lock(this->lock);
1028 return enumerator_create_nested(
1029 enumerator_create_filter(
1030 this->ifaces->create_enumerator(this->ifaces),
1031 (void*)filter_interfaces, data, NULL),
1032 (void*)create_iface_enumerator, data,
1033 (void*)address_enumerator_destroy);
1034 }
1035
1036 METHOD(kernel_net_t, get_features, kernel_feature_t,
1037 private_kernel_pfroute_net_t *this)
1038 {
1039 return KERNEL_REQUIRE_EXCLUDE_ROUTE;
1040 }
1041
1042 METHOD(kernel_net_t, get_interface_name, bool,
1043 private_kernel_pfroute_net_t *this, host_t* ip, char **name)
1044 {
1045 addr_map_entry_t *entry, lookup = {
1046 .ip = ip,
1047 };
1048
1049 if (ip->is_anyaddr(ip))
1050 {
1051 return FALSE;
1052 }
1053 this->lock->read_lock(this->lock);
1054 /* first try to find it on an up and usable interface */
1055 entry = this->addrs->get_match(this->addrs, &lookup,
1056 (void*)addr_map_entry_match_up_and_usable);
1057 if (entry)
1058 {
1059 if (name)
1060 {
1061 *name = strdup(entry->iface->ifname);
1062 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
1063 }
1064 this->lock->unlock(this->lock);
1065 return TRUE;
1066 }
1067 /* maybe it is installed on an ignored interface */
1068 entry = this->addrs->get_match(this->addrs, &lookup,
1069 (void*)addr_map_entry_match_up);
1070 if (!entry)
1071 { /* the address does not exist, is on a down interface */
1072 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
1073 }
1074 this->lock->unlock(this->lock);
1075 return FALSE;
1076 }
1077
1078 METHOD(kernel_net_t, add_ip, status_t,
1079 private_kernel_pfroute_net_t *this, host_t *vip, int prefix,
1080 char *ifname)
1081 {
1082 enumerator_t *ifaces, *addrs;
1083 iface_entry_t *iface;
1084 addr_entry_t *addr;
1085 tun_device_t *tun;
1086 bool timeout = FALSE;
1087
1088 tun = tun_device_create(NULL);
1089 if (!tun)
1090 {
1091 return FAILED;
1092 }
1093 if (prefix == -1)
1094 {
1095 prefix = vip->get_address(vip).len * 8;
1096 }
1097 if (!tun->up(tun) || !tun->set_address(tun, vip, prefix))
1098 {
1099 tun->destroy(tun);
1100 return FAILED;
1101 }
1102
1103 /* wait until address appears */
1104 this->mutex->lock(this->mutex);
1105 while (!timeout && !get_interface_name(this, vip, NULL))
1106 {
1107 timeout = this->condvar->timed_wait(this->condvar, this->mutex,
1108 this->vip_wait);
1109 }
1110 this->mutex->unlock(this->mutex);
1111 if (timeout)
1112 {
1113 DBG1(DBG_KNL, "virtual IP %H did not appear on %s",
1114 vip, tun->get_name(tun));
1115 tun->destroy(tun);
1116 return FAILED;
1117 }
1118
1119 this->lock->write_lock(this->lock);
1120 this->tuns->insert_last(this->tuns, tun);
1121
1122 ifaces = this->ifaces->create_enumerator(this->ifaces);
1123 while (ifaces->enumerate(ifaces, &iface))
1124 {
1125 if (streq(iface->ifname, tun->get_name(tun)))
1126 {
1127 addrs = iface->addrs->create_enumerator(iface->addrs);
1128 while (addrs->enumerate(addrs, &addr))
1129 {
1130 if (addr->ip->ip_equals(addr->ip, vip))
1131 {
1132 addr->virtual = TRUE;
1133 }
1134 }
1135 addrs->destroy(addrs);
1136 /* during IKEv1 reauthentication, children get moved from
1137 * old the new SA before the virtual IP is available. This
1138 * kills the route for our virtual IP, reinstall. */
1139 queue_route_reinstall(this, strdup(iface->ifname));
1140 break;
1141 }
1142 }
1143 ifaces->destroy(ifaces);
1144 /* lets do this while holding the lock, thus preventing another thread
1145 * from deleting the TUN device concurrently, hopefully listeners are quick
1146 * and cause no deadlocks */
1147 hydra->kernel_interface->tun(hydra->kernel_interface, tun, TRUE);
1148 this->lock->unlock(this->lock);
1149
1150 return SUCCESS;
1151 }
1152
1153 METHOD(kernel_net_t, del_ip, status_t,
1154 private_kernel_pfroute_net_t *this, host_t *vip, int prefix,
1155 bool wait)
1156 {
1157 enumerator_t *enumerator;
1158 tun_device_t *tun;
1159 host_t *addr;
1160 bool timeout = FALSE, found = FALSE;
1161
1162 this->lock->write_lock(this->lock);
1163 enumerator = this->tuns->create_enumerator(this->tuns);
1164 while (enumerator->enumerate(enumerator, &tun))
1165 {
1166 addr = tun->get_address(tun, NULL);
1167 if (addr && addr->ip_equals(addr, vip))
1168 {
1169 this->tuns->remove_at(this->tuns, enumerator);
1170 hydra->kernel_interface->tun(hydra->kernel_interface, tun,
1171 FALSE);
1172 tun->destroy(tun);
1173 found = TRUE;
1174 break;
1175 }
1176 }
1177 enumerator->destroy(enumerator);
1178 this->lock->unlock(this->lock);
1179
1180 if (!found)
1181 {
1182 return NOT_FOUND;
1183 }
1184 /* wait until address disappears */
1185 if (wait)
1186 {
1187 this->mutex->lock(this->mutex);
1188 while (!timeout && get_interface_name(this, vip, NULL))
1189 {
1190 timeout = this->condvar->timed_wait(this->condvar, this->mutex,
1191 this->vip_wait);
1192 }
1193 this->mutex->unlock(this->mutex);
1194 if (timeout)
1195 {
1196 DBG1(DBG_KNL, "virtual IP %H did not disappear from tun", vip);
1197 return FAILED;
1198 }
1199 }
1200 return SUCCESS;
1201 }
1202
1203 /**
1204 * Append a sockaddr_in/in6 of given type to routing message
1205 */
1206 static void add_rt_addr(struct rt_msghdr *hdr, int type, host_t *addr)
1207 {
1208 if (addr)
1209 {
1210 int len;
1211
1212 len = *addr->get_sockaddr_len(addr);
1213 memcpy((char*)hdr + hdr->rtm_msglen, addr->get_sockaddr(addr), len);
1214 hdr->rtm_msglen += SA_LEN(len);
1215 hdr->rtm_addrs |= type;
1216 }
1217 }
1218
1219 /**
1220 * Append a subnet mask sockaddr using the given prefix to routing message
1221 */
1222 static void add_rt_mask(struct rt_msghdr *hdr, int type, int family, int prefix)
1223 {
1224 host_t *mask;
1225
1226 mask = host_create_netmask(family, prefix);
1227 if (mask)
1228 {
1229 add_rt_addr(hdr, type, mask);
1230 mask->destroy(mask);
1231 }
1232 }
1233
1234 /**
1235 * Append an interface name sockaddr_dl to routing message
1236 */
1237 static void add_rt_ifname(struct rt_msghdr *hdr, int type, char *name)
1238 {
1239 struct sockaddr_dl sdl = {
1240 .sdl_len = sizeof(struct sockaddr_dl),
1241 .sdl_family = AF_LINK,
1242 .sdl_nlen = strlen(name),
1243 };
1244
1245 if (strlen(name) <= sizeof(sdl.sdl_data))
1246 {
1247 memcpy(sdl.sdl_data, name, sdl.sdl_nlen);
1248 memcpy((char*)hdr + hdr->rtm_msglen, &sdl, sdl.sdl_len);
1249 hdr->rtm_msglen += SA_LEN(sdl.sdl_len);
1250 hdr->rtm_addrs |= type;
1251 }
1252 }
1253
1254 /**
1255 * Add or remove a route
1256 */
1257 static status_t manage_route(private_kernel_pfroute_net_t *this, int op,
1258 chunk_t dst_net, u_int8_t prefixlen,
1259 host_t *gateway, char *if_name)
1260 {
1261 struct {
1262 struct rt_msghdr hdr;
1263 char buf[sizeof(struct sockaddr_storage) * RTAX_MAX];
1264 } msg = {
1265 .hdr = {
1266 .rtm_version = RTM_VERSION,
1267 .rtm_type = op,
1268 .rtm_flags = RTF_UP | RTF_STATIC,
1269 .rtm_pid = this->pid,
1270 .rtm_seq = ref_get(&this->seq),
1271 },
1272 };
1273 host_t *dst;
1274 int type;
1275
1276 if (prefixlen == 0 && dst_net.len)
1277 {
1278 status_t status;
1279 chunk_t half;
1280
1281 half = chunk_clonea(dst_net);
1282 half.ptr[0] |= 0x80;
1283 prefixlen = 1;
1284 status = manage_route(this, op, half, prefixlen, gateway, if_name);
1285 if (status != SUCCESS)
1286 {
1287 return status;
1288 }
1289 }
1290
1291 dst = host_create_from_chunk(AF_UNSPEC, dst_net, 0);
1292 if (!dst)
1293 {
1294 return FAILED;
1295 }
1296
1297 if ((dst->get_family(dst) == AF_INET && prefixlen == 32) ||
1298 (dst->get_family(dst) == AF_INET6 && prefixlen == 128))
1299 {
1300 msg.hdr.rtm_flags |= RTF_HOST | RTF_GATEWAY;
1301 }
1302
1303 msg.hdr.rtm_msglen = sizeof(struct rt_msghdr);
1304 for (type = 0; type < RTAX_MAX; type++)
1305 {
1306 switch (type)
1307 {
1308 case RTAX_DST:
1309 add_rt_addr(&msg.hdr, RTA_DST, dst);
1310 break;
1311 case RTAX_NETMASK:
1312 if (!(msg.hdr.rtm_flags & RTF_HOST))
1313 {
1314 add_rt_mask(&msg.hdr, RTA_NETMASK,
1315 dst->get_family(dst), prefixlen);
1316 }
1317 break;
1318 case RTAX_IFP:
1319 if (if_name)
1320 {
1321 add_rt_ifname(&msg.hdr, RTA_IFP, if_name);
1322 }
1323 break;
1324 case RTAX_GATEWAY:
1325 if (gateway)
1326 {
1327 add_rt_addr(&msg.hdr, RTA_GATEWAY, gateway);
1328 }
1329 break;
1330 default:
1331 break;
1332 }
1333 }
1334 dst->destroy(dst);
1335
1336 if (send(this->socket, &msg, msg.hdr.rtm_msglen, 0) != msg.hdr.rtm_msglen)
1337 {
1338 if (errno == EEXIST)
1339 {
1340 return ALREADY_DONE;
1341 }
1342 DBG1(DBG_KNL, "%s PF_ROUTE route failed: %s",
1343 op == RTM_ADD ? "adding" : "deleting", strerror(errno));
1344 return FAILED;
1345 }
1346 return SUCCESS;
1347 }
1348
1349 METHOD(kernel_net_t, add_route, status_t,
1350 private_kernel_pfroute_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1351 host_t *gateway, host_t *src_ip, char *if_name)
1352 {
1353 status_t status;
1354 route_entry_t *found, route = {
1355 .dst_net = dst_net,
1356 .prefixlen = prefixlen,
1357 .gateway = gateway,
1358 .if_name = if_name,
1359 };
1360
1361 this->routes_lock->lock(this->routes_lock);
1362 found = this->routes->get(this->routes, &route);
1363 if (found)
1364 {
1365 this->routes_lock->unlock(this->routes_lock);
1366 return ALREADY_DONE;
1367 }
1368 found = route_entry_clone(&route);
1369 this->routes->put(this->routes, found, found);
1370 status = manage_route(this, RTM_ADD, dst_net, prefixlen, gateway, if_name);
1371 this->routes_lock->unlock(this->routes_lock);
1372 return status;
1373 }
1374
1375 METHOD(kernel_net_t, del_route, status_t,
1376 private_kernel_pfroute_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
1377 host_t *gateway, host_t *src_ip, char *if_name)
1378 {
1379 status_t status;
1380 route_entry_t *found, route = {
1381 .dst_net = dst_net,
1382 .prefixlen = prefixlen,
1383 .gateway = gateway,
1384 .if_name = if_name,
1385 };
1386
1387 this->routes_lock->lock(this->routes_lock);
1388 found = this->routes->get(this->routes, &route);
1389 if (!found)
1390 {
1391 this->routes_lock->unlock(this->routes_lock);
1392 return NOT_FOUND;
1393 }
1394 this->routes->remove(this->routes, found);
1395 route_entry_destroy(found);
1396 status = manage_route(this, RTM_DELETE, dst_net, prefixlen, gateway,
1397 if_name);
1398 this->routes_lock->unlock(this->routes_lock);
1399 return status;
1400 }
1401
1402 /**
1403 * Do a route lookup for dest and return either the nexthop or the source
1404 * address.
1405 */
1406 static host_t *get_route(private_kernel_pfroute_net_t *this, bool nexthop,
1407 host_t *dest, host_t *src)
1408 {
1409 struct {
1410 struct rt_msghdr hdr;
1411 char buf[sizeof(struct sockaddr_storage) * RTAX_MAX];
1412 } msg = {
1413 .hdr = {
1414 .rtm_version = RTM_VERSION,
1415 .rtm_type = RTM_GET,
1416 .rtm_pid = this->pid,
1417 .rtm_seq = ref_get(&this->seq),
1418 },
1419 };
1420 host_t *host = NULL;
1421 enumerator_t *enumerator;
1422 struct sockaddr *addr;
1423 bool failed = FALSE;
1424 int type;
1425
1426 retry:
1427 msg.hdr.rtm_msglen = sizeof(struct rt_msghdr);
1428 for (type = 0; type < RTAX_MAX; type++)
1429 {
1430 switch (type)
1431 {
1432 case RTAX_DST:
1433 add_rt_addr(&msg.hdr, RTA_DST, dest);
1434 break;
1435 case RTAX_IFA:
1436 add_rt_addr(&msg.hdr, RTA_IFA, src);
1437 break;
1438 case RTAX_IFP:
1439 if (!nexthop)
1440 { /* add an empty IFP to ensure we get a source address */
1441 add_rt_ifname(&msg.hdr, RTA_IFP, "");
1442 }
1443 break;
1444 default:
1445 break;
1446 }
1447 }
1448 this->mutex->lock(this->mutex);
1449
1450 while (this->waiting_seq)
1451 {
1452 this->condvar->wait(this->condvar, this->mutex);
1453 }
1454 this->waiting_seq = msg.hdr.rtm_seq;
1455 if (send(this->socket, &msg, msg.hdr.rtm_msglen, 0) == msg.hdr.rtm_msglen)
1456 {
1457 while (TRUE)
1458 {
1459 if (this->condvar->timed_wait(this->condvar, this->mutex, 1000))
1460 { /* timed out? */
1461 break;
1462 }
1463 if (this->reply->rtm_msglen < sizeof(*this->reply) ||
1464 msg.hdr.rtm_seq != this->reply->rtm_seq)
1465 {
1466 continue;
1467 }
1468 enumerator = create_rtmsg_enumerator(this->reply,
1469 sizeof(*this->reply));
1470 while (enumerator->enumerate(enumerator, &type, &addr))
1471 {
1472 if (nexthop)
1473 {
1474 if (type == RTAX_DST && this->reply->rtm_flags & RTF_HOST)
1475 { /* probably a cloned/cached direct route, only use that
1476 * as fallback if no gateway is found */
1477 host = host ?: host_create_from_sockaddr(addr);
1478 }
1479 if (type == RTAX_GATEWAY)
1480 { /* could actually be a MAC address */
1481 host_t *gtw = host_create_from_sockaddr(addr);
1482 if (gtw)
1483 {
1484 DESTROY_IF(host);
1485 host = gtw;
1486 }
1487 }
1488 }
1489 else
1490 {
1491 if (type == RTAX_IFA)
1492 {
1493 host = host_create_from_sockaddr(addr);
1494 }
1495 }
1496 }
1497 enumerator->destroy(enumerator);
1498 break;
1499 }
1500 }
1501 else
1502 {
1503 failed = TRUE;
1504 }
1505 /* signal completion of query to a waiting thread */
1506 this->waiting_seq = 0;
1507 this->condvar->signal(this->condvar);
1508 this->mutex->unlock(this->mutex);
1509
1510 if (failed)
1511 {
1512 if (src)
1513 { /* the given source address might be gone, try again without */
1514 src = NULL;
1515 msg.hdr.rtm_seq = ref_get(&this->seq);
1516 msg.hdr.rtm_addrs = 0;
1517 memset(msg.buf, sizeof(msg.buf), 0);
1518 goto retry;
1519 }
1520 DBG1(DBG_KNL, "PF_ROUTE lookup failed: %s", strerror(errno));
1521 }
1522 if (!host)
1523 {
1524 return NULL;
1525 }
1526 if (!nexthop)
1527 { /* make sure the source address is not virtual and usable */
1528 addr_entry_t *entry, lookup = {
1529 .ip = host,
1530 };
1531
1532 this->lock->read_lock(this->lock);
1533 entry = this->addrs->get_match(this->addrs, &lookup,
1534 (void*)addr_map_entry_match_up_and_usable);
1535 this->lock->unlock(this->lock);
1536 if (!entry)
1537 {
1538 host->destroy(host);
1539 return NULL;
1540 }
1541 }
1542 DBG2(DBG_KNL, "using %H as %s to reach %H", host,
1543 nexthop ? "nexthop" : "address", dest);
1544 return host;
1545 }
1546
1547 METHOD(kernel_net_t, get_source_addr, host_t*,
1548 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
1549 {
1550 return get_route(this, FALSE, dest, src);
1551 }
1552
1553 METHOD(kernel_net_t, get_nexthop, host_t*,
1554 private_kernel_pfroute_net_t *this, host_t *dest, host_t *src)
1555 {
1556 return get_route(this, TRUE, dest, src);
1557 }
1558
1559 /**
1560 * Initialize a list of local addresses.
1561 */
1562 static status_t init_address_list(private_kernel_pfroute_net_t *this)
1563 {
1564 struct ifaddrs *ifap, *ifa;
1565 iface_entry_t *iface, *current;
1566 addr_entry_t *addr;
1567 enumerator_t *ifaces, *addrs;
1568
1569 DBG2(DBG_KNL, "known interfaces and IP addresses:");
1570
1571 if (getifaddrs(&ifap) < 0)
1572 {
1573 DBG1(DBG_KNL, " failed to get interfaces!");
1574 return FAILED;
1575 }
1576
1577 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
1578 {
1579 if (ifa->ifa_addr == NULL)
1580 {
1581 continue;
1582 }
1583 switch(ifa->ifa_addr->sa_family)
1584 {
1585 case AF_LINK:
1586 case AF_INET:
1587 case AF_INET6:
1588 {
1589 iface = NULL;
1590 ifaces = this->ifaces->create_enumerator(this->ifaces);
1591 while (ifaces->enumerate(ifaces, &current))
1592 {
1593 if (streq(current->ifname, ifa->ifa_name))
1594 {
1595 iface = current;
1596 break;
1597 }
1598 }
1599 ifaces->destroy(ifaces);
1600
1601 if (!iface)
1602 {
1603 INIT(iface,
1604 .ifindex = if_nametoindex(ifa->ifa_name),
1605 .flags = ifa->ifa_flags,
1606 .addrs = linked_list_create(),
1607 .usable = hydra->kernel_interface->is_interface_usable(
1608 hydra->kernel_interface, ifa->ifa_name),
1609 );
1610 memcpy(iface->ifname, ifa->ifa_name, IFNAMSIZ);
1611 this->ifaces->insert_last(this->ifaces, iface);
1612 }
1613
1614 if (ifa->ifa_addr->sa_family != AF_LINK)
1615 {
1616 INIT(addr,
1617 .ip = host_create_from_sockaddr(ifa->ifa_addr),
1618 );
1619 iface->addrs->insert_last(iface->addrs, addr);
1620 addr_map_entry_add(this, addr, iface);
1621 }
1622 }
1623 }
1624 }
1625 freeifaddrs(ifap);
1626
1627 ifaces = this->ifaces->create_enumerator(this->ifaces);
1628 while (ifaces->enumerate(ifaces, &iface))
1629 {
1630 if (iface->usable && iface->flags & IFF_UP)
1631 {
1632 DBG2(DBG_KNL, " %s", iface->ifname);
1633 addrs = iface->addrs->create_enumerator(iface->addrs);
1634 while (addrs->enumerate(addrs, (void**)&addr))
1635 {
1636 DBG2(DBG_KNL, " %H", addr->ip);
1637 }
1638 addrs->destroy(addrs);
1639 }
1640 }
1641 ifaces->destroy(ifaces);
1642
1643 return SUCCESS;
1644 }
1645
1646 METHOD(kernel_net_t, destroy, void,
1647 private_kernel_pfroute_net_t *this)
1648 {
1649 enumerator_t *enumerator;
1650 route_entry_t *route;
1651 addr_entry_t *addr;
1652
1653 enumerator = this->routes->create_enumerator(this->routes);
1654 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
1655 {
1656 manage_route(this, RTM_DELETE, route->dst_net, route->prefixlen,
1657 route->gateway, route->if_name);
1658 route_entry_destroy(route);
1659 }
1660 enumerator->destroy(enumerator);
1661 this->routes->destroy(this->routes);
1662 this->routes_lock->destroy(this->routes_lock);
1663
1664 if (this->socket != -1)
1665 {
1666 close(this->socket);
1667 }
1668
1669 net_changes_clear(this);
1670 this->net_changes->destroy(this->net_changes);
1671 this->net_changes_lock->destroy(this->net_changes_lock);
1672
1673 enumerator = this->addrs->create_enumerator(this->addrs);
1674 while (enumerator->enumerate(enumerator, NULL, (void**)&addr))
1675 {
1676 free(addr);
1677 }
1678 enumerator->destroy(enumerator);
1679 this->addrs->destroy(this->addrs);
1680 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
1681 this->tuns->destroy(this->tuns);
1682 this->lock->destroy(this->lock);
1683 this->mutex->destroy(this->mutex);
1684 this->condvar->destroy(this->condvar);
1685 free(this->reply);
1686 free(this);
1687 }
1688
1689 /*
1690 * Described in header.
1691 */
1692 kernel_pfroute_net_t *kernel_pfroute_net_create()
1693 {
1694 private_kernel_pfroute_net_t *this;
1695
1696 INIT(this,
1697 .public = {
1698 .interface = {
1699 .get_features = _get_features,
1700 .get_interface = _get_interface_name,
1701 .create_address_enumerator = _create_address_enumerator,
1702 .get_source_addr = _get_source_addr,
1703 .get_nexthop = _get_nexthop,
1704 .add_ip = _add_ip,
1705 .del_ip = _del_ip,
1706 .add_route = _add_route,
1707 .del_route = _del_route,
1708 .destroy = _destroy,
1709 },
1710 },
1711 .pid = getpid(),
1712 .ifaces = linked_list_create(),
1713 .addrs = hashtable_create(
1714 (hashtable_hash_t)addr_map_entry_hash,
1715 (hashtable_equals_t)addr_map_entry_equals, 16),
1716 .routes = hashtable_create((hashtable_hash_t)route_entry_hash,
1717 (hashtable_equals_t)route_entry_equals, 16),
1718 .net_changes = hashtable_create(
1719 (hashtable_hash_t)net_change_hash,
1720 (hashtable_equals_t)net_change_equals, 16),
1721 .tuns = linked_list_create(),
1722 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
1723 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1724 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
1725 .routes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
1726 .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
1727 .vip_wait = lib->settings->get_int(lib->settings,
1728 "%s.plugins.kernel-pfroute.vip_wait", 1000, hydra->daemon),
1729 );
1730 timerclear(&this->last_route_reinstall);
1731 timerclear(&this->last_roam);
1732
1733 /* create a PF_ROUTE socket to communicate with the kernel */
1734 this->socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
1735 if (this->socket == -1)
1736 {
1737 DBG1(DBG_KNL, "unable to create PF_ROUTE socket");
1738 destroy(this);
1739 return NULL;
1740 }
1741
1742 if (streq(hydra->daemon, "starter"))
1743 {
1744 /* starter has no threads, so we do not register for kernel events */
1745 if (shutdown(this->socket, SHUT_RD) != 0)
1746 {
1747 DBG1(DBG_KNL, "closing read end of PF_ROUTE socket failed: %s",
1748 strerror(errno));
1749 }
1750 }
1751 else
1752 {
1753 lib->processor->queue_job(lib->processor,
1754 (job_t*)callback_job_create_with_prio(
1755 (callback_job_cb_t)receive_events, this, NULL,
1756 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
1757 }
1758 if (init_address_list(this) != SUCCESS)
1759 {
1760 DBG1(DBG_KNL, "unable to get interface list");
1761 destroy(this);
1762 return NULL;
1763 }
1764
1765 return &this->public;
1766 }