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