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