2 * Copyright (C) 2009-2016 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/sysctl.h>
20 #include <net/if_dl.h>
22 #include <net/route.h>
26 #include "kernel_pfroute_net.h"
29 #include <utils/debug.h>
30 #include <networking/host.h>
31 #include <networking/tun_device.h>
32 #include <threading/thread.h>
33 #include <threading/mutex.h>
34 #include <threading/condvar.h>
35 #include <threading/rwlock.h>
36 #include <threading/spinlock.h>
37 #include <collections/hashtable.h>
38 #include <collections/linked_list.h>
39 #include <processing/jobs/callback_job.h>
41 #ifndef HAVE_STRUCT_SOCKADDR_SA_LEN
42 #error Cannot compile this plugin on systems where 'struct sockaddr' has no sa_len member.
45 /** properly align sockaddrs */
47 /* Apple always uses 4 bytes */
50 /* while on other platforms like FreeBSD it depends on the architecture */
51 #define SA_ALIGN sizeof(long)
53 #define SA_LEN(len) ((len) > 0 ? (((len)+SA_ALIGN-1) & ~(SA_ALIGN-1)) : SA_ALIGN)
55 /** delay before firing roam events (ms) */
56 #define ROAM_DELAY 100
58 /** delay before reinstalling routes (ms) */
59 #define ROUTE_DELAY 100
61 typedef struct addr_entry_t addr_entry_t
;
64 * IP address in an inface_entry_t
71 /** virtual IP managed by us */
76 * destroy a addr_entry_t object
78 static void addr_entry_destroy(addr_entry_t
*this)
80 this->ip
->destroy(this->ip
);
84 typedef struct iface_entry_t iface_entry_t
;
87 * A network interface on this system, containing addr_entry_t's
89 struct iface_entry_t
{
91 /** interface index */
94 /** name of the interface */
95 char ifname
[IFNAMSIZ
];
97 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
100 /** list of addresses as host_t */
101 linked_list_t
*addrs
;
103 /** TRUE if usable by config */
108 * destroy an interface entry
110 static void iface_entry_destroy(iface_entry_t
*this)
112 this->addrs
->destroy_function(this->addrs
, (void*)addr_entry_destroy
);
117 * check if an interface is up
119 static inline bool iface_entry_up(iface_entry_t
*iface
)
121 return (iface
->flags
& IFF_UP
) == IFF_UP
;
125 * check if an interface is up and usable
127 static inline bool iface_entry_up_and_usable(iface_entry_t
*iface
)
129 return iface
->usable
&& iface_entry_up(iface
);
132 typedef struct addr_map_entry_t addr_map_entry_t
;
135 * Entry that maps an IP address to an interface entry
137 struct addr_map_entry_t
{
138 /** The IP address */
141 /** The address entry for this IP address */
144 /** The interface this address is installed on */
145 iface_entry_t
*iface
;
149 * Hash a addr_map_entry_t object, all entries with the same IP address
150 * are stored in the same bucket
152 static u_int
addr_map_entry_hash(addr_map_entry_t
*this)
154 return chunk_hash(this->ip
->get_address(this->ip
));
158 * Compare two addr_map_entry_t objects, two entries are equal if they are
159 * installed on the same interface
161 static bool addr_map_entry_equals(addr_map_entry_t
*a
, addr_map_entry_t
*b
)
163 return a
->iface
->ifindex
== b
->iface
->ifindex
&&
164 a
->ip
->ip_equals(a
->ip
, b
->ip
);
168 * Used with get_match this finds an address entry if it is installed on
169 * an up and usable interface
171 static bool addr_map_entry_match_up_and_usable(addr_map_entry_t
*a
,
174 return !b
->addr
->virtual && iface_entry_up_and_usable(b
->iface
) &&
175 a
->ip
->ip_equals(a
->ip
, b
->ip
);
179 * Used with get_match this finds an address entry if it is installed as virtual
182 static bool addr_map_entry_match_virtual(addr_map_entry_t
*a
, addr_map_entry_t
*b
)
184 return b
->addr
->virtual && a
->ip
->ip_equals(a
->ip
, b
->ip
);
188 * Used with get_match this finds an address entry if it is installed on
189 * any active local interface
191 static bool addr_map_entry_match_up(addr_map_entry_t
*a
, addr_map_entry_t
*b
)
193 return !b
->addr
->virtual && iface_entry_up(b
->iface
) &&
194 a
->ip
->ip_equals(a
->ip
, b
->ip
);
197 typedef struct route_entry_t route_entry_t
;
200 * Installed routing entry
202 struct route_entry_t
{
203 /** Name of the interface the route is bound to */
206 /** Gateway for this route */
209 /** Destination net */
212 /** Destination net prefixlen */
217 * Clone a route_entry_t object.
219 static route_entry_t
*route_entry_clone(route_entry_t
*this)
221 route_entry_t
*route
;
224 .if_name
= strdup(this->if_name
),
225 .gateway
= this->gateway
? this->gateway
->clone(this->gateway
) : NULL
,
226 .dst_net
= chunk_clone(this->dst_net
),
227 .prefixlen
= this->prefixlen
,
233 * Destroy a route_entry_t object
235 static void route_entry_destroy(route_entry_t
*this)
238 DESTROY_IF(this->gateway
);
239 chunk_free(&this->dst_net
);
244 * Hash a route_entry_t object
246 static u_int
route_entry_hash(route_entry_t
*this)
248 return chunk_hash_inc(chunk_from_thing(this->prefixlen
),
249 chunk_hash(this->dst_net
));
253 * Compare two route_entry_t objects
255 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
257 if (a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
258 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
)
260 return (!a
->gateway
&& !b
->gateway
) || (a
->gateway
&& b
->gateway
&&
261 a
->gateway
->ip_equals(a
->gateway
, b
->gateway
));
266 typedef struct net_change_t net_change_t
;
269 * Queued network changes
271 struct net_change_t
{
272 /** Name of the interface that got activated (or an IP appeared on) */
277 * Destroy a net_change_t object
279 static void net_change_destroy(net_change_t
*this)
286 * Hash a net_change_t object
288 static u_int
net_change_hash(net_change_t
*this)
290 return chunk_hash(chunk_create(this->if_name
, strlen(this->if_name
)));
294 * Compare two net_change_t objects
296 static bool net_change_equals(net_change_t
*a
, net_change_t
*b
)
298 return streq(a
->if_name
, b
->if_name
);
301 typedef struct private_kernel_pfroute_net_t private_kernel_pfroute_net_t
;
304 * Private variables and functions of kernel_pfroute class.
306 struct private_kernel_pfroute_net_t
309 * Public part of the kernel_pfroute_t object.
311 kernel_pfroute_net_t
public;
314 * lock to access lists and maps
319 * Cached list of interfaces and their addresses (iface_entry_t)
321 linked_list_t
*ifaces
;
324 * Map for IP addresses to iface_entry_t objects (addr_map_entry_t)
329 * List of tun devices we installed for virtual IPs
334 * mutex to communicate exclusively with PF_KEY
339 * condvar to signal if PF_KEY query got a response
351 mutex_t
*routes_lock
;
354 * interface changes which may trigger route reinstallation
356 hashtable_t
*net_changes
;
359 * mutex for route reinstallation triggers
361 mutex_t
*net_changes_lock
;
364 * time of last route reinstallation
366 timeval_t last_route_reinstall
;
369 * pid to send PF_ROUTE messages with
374 * PF_ROUTE socket to communicate with the kernel
379 * sequence number for messages sent to the kernel
384 * Sequence number a query is waiting for
389 * Allocated reply message from kernel
391 struct rt_msghdr
*reply
;
394 * earliest time of the next roam event
399 * roam event due to address change
404 * lock to check and update roam event time
406 spinlock_t
*roam_lock
;
409 * Time in ms to wait for IP addresses to appear/disappear
414 * whether to actually install virtual IPs
416 bool install_virtual_ip
;
421 * Forward declaration
423 static status_t
manage_route(private_kernel_pfroute_net_t
*this, int op
,
424 chunk_t dst_net
, uint8_t prefixlen
,
425 host_t
*gateway
, char *if_name
);
428 * Clear the queued network changes.
430 static void net_changes_clear(private_kernel_pfroute_net_t
*this)
432 enumerator_t
*enumerator
;
433 net_change_t
*change
;
435 enumerator
= this->net_changes
->create_enumerator(this->net_changes
);
436 while (enumerator
->enumerate(enumerator
, NULL
, (void**)&change
))
438 this->net_changes
->remove_at(this->net_changes
, enumerator
);
439 net_change_destroy(change
);
441 enumerator
->destroy(enumerator
);
445 * Act upon queued network changes.
447 static job_requeue_t
reinstall_routes(private_kernel_pfroute_net_t
*this)
449 enumerator_t
*enumerator
;
450 route_entry_t
*route
;
452 this->net_changes_lock
->lock(this->net_changes_lock
);
453 this->routes_lock
->lock(this->routes_lock
);
455 enumerator
= this->routes
->create_enumerator(this->routes
);
456 while (enumerator
->enumerate(enumerator
, NULL
, (void**)&route
))
458 net_change_t
*change
, lookup
= {
459 .if_name
= route
->if_name
,
461 /* check if a change for the outgoing interface is queued */
462 change
= this->net_changes
->get(this->net_changes
, &lookup
);
465 manage_route(this, RTM_ADD
, route
->dst_net
, route
->prefixlen
,
466 route
->gateway
, route
->if_name
);
469 enumerator
->destroy(enumerator
);
470 this->routes_lock
->unlock(this->routes_lock
);
472 net_changes_clear(this);
473 this->net_changes_lock
->unlock(this->net_changes_lock
);
474 return JOB_REQUEUE_NONE
;
478 * Queue route reinstallation caused by network changes for a given interface.
480 * The route reinstallation is delayed for a while and only done once for
481 * several calls during this delay, in order to avoid doing it too often.
482 * The interface name is freed.
484 static void queue_route_reinstall(private_kernel_pfroute_net_t
*this,
487 net_change_t
*update
, *found
;
495 this->net_changes_lock
->lock(this->net_changes_lock
);
496 found
= this->net_changes
->put(this->net_changes
, update
, update
);
499 net_change_destroy(found
);
501 time_monotonic(&now
);
502 if (timercmp(&now
, &this->last_route_reinstall
, >))
504 timeval_add_ms(&now
, ROUTE_DELAY
);
505 this->last_route_reinstall
= now
;
507 job
= (job_t
*)callback_job_create((callback_job_cb_t
)reinstall_routes
,
509 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, job
, ROUTE_DELAY
);
511 this->net_changes_lock
->unlock(this->net_changes_lock
);
515 * Add an address map entry
517 static void addr_map_entry_add(private_kernel_pfroute_net_t
*this,
518 addr_entry_t
*addr
, iface_entry_t
*iface
)
520 addr_map_entry_t
*entry
;
527 entry
= this->addrs
->put(this->addrs
, entry
, entry
);
532 * Remove an address map entry (the argument order is a bit strange because
533 * it is also used with linked_list_t.invoke_function)
535 static void addr_map_entry_remove(addr_entry_t
*addr
, iface_entry_t
*iface
,
536 private_kernel_pfroute_net_t
*this)
538 addr_map_entry_t
*entry
, lookup
= {
544 entry
= this->addrs
->remove(this->addrs
, &lookup
);
549 * callback function that raises the delayed roam event
551 static job_requeue_t
roam_event(private_kernel_pfroute_net_t
*this)
555 this->roam_lock
->lock(this->roam_lock
);
556 address
= this->roam_address
;
557 this->roam_address
= FALSE
;
558 this->roam_lock
->unlock(this->roam_lock
);
559 charon
->kernel
->roam(charon
->kernel
, address
);
560 return JOB_REQUEUE_NONE
;
564 * fire a roaming event. we delay it for a bit and fire only one event
565 * for multiple calls. otherwise we would create too many events.
567 static void fire_roam_event(private_kernel_pfroute_net_t
*this, bool address
)
572 time_monotonic(&now
);
573 this->roam_lock
->lock(this->roam_lock
);
574 this->roam_address
|= address
;
575 if (!timercmp(&now
, &this->next_roam
, >))
577 this->roam_lock
->unlock(this->roam_lock
);
580 timeval_add_ms(&now
, ROAM_DELAY
);
581 this->next_roam
= now
;
582 this->roam_lock
->unlock(this->roam_lock
);
584 job
= (job_t
*)callback_job_create((callback_job_cb_t
)roam_event
,
586 lib
->scheduler
->schedule_job_ms(lib
->scheduler
, job
, ROAM_DELAY
);
590 * Data for enumerator over rtmsg sockaddrs
593 /** implements enumerator */
595 /** copy of attribute bitfield */
597 /** bytes remaining in buffer */
599 /** next sockaddr to enumerate */
600 struct sockaddr
*addr
;
603 METHOD(enumerator_t
, rt_enumerate
, bool,
604 rt_enumerator_t
*this, va_list args
)
606 struct sockaddr
**addr
;
609 VA_ARGS_VGET(args
, xtype
, addr
);
611 if (this->remaining
< sizeof(this->addr
->sa_len
) ||
612 this->remaining
< this->addr
->sa_len
)
616 for (i
= 0; i
< RTAX_MAX
; i
++)
619 if (this->types
& type
)
621 this->types
&= ~type
;
624 this->remaining
-= SA_LEN(this->addr
->sa_len
);
625 this->addr
= (struct sockaddr
*)((char*)this->addr
+
626 SA_LEN(this->addr
->sa_len
));
634 * Create an enumerator over sockaddrs in rt/if messages
636 static enumerator_t
*create_rt_enumerator(int types
, int remaining
,
637 struct sockaddr
*addr
)
639 rt_enumerator_t
*this;
643 .enumerate
= enumerator_enumerate_default
,
644 .venumerate
= _rt_enumerate
,
645 .destroy
= (void*)free
,
648 .remaining
= remaining
,
651 return &this->public;
655 * Create a safe enumerator over sockaddrs in rt_msghdr
657 static enumerator_t
*create_rtmsg_enumerator(struct rt_msghdr
*hdr
)
659 return create_rt_enumerator(hdr
->rtm_addrs
, hdr
->rtm_msglen
- sizeof(*hdr
),
660 (struct sockaddr
*)(hdr
+ 1));
664 * Create a safe enumerator over sockaddrs in ifa_msghdr
666 static enumerator_t
*create_ifamsg_enumerator(struct ifa_msghdr
*hdr
)
668 return create_rt_enumerator(hdr
->ifam_addrs
, hdr
->ifam_msglen
- sizeof(*hdr
),
669 (struct sockaddr
*)(hdr
+ 1));
673 * Process an RTM_*ADDR message from the kernel
675 static void process_addr(private_kernel_pfroute_net_t
*this,
676 struct ifa_msghdr
*ifa
)
678 struct sockaddr
*sockaddr
;
680 enumerator_t
*ifaces
, *addrs
;
681 iface_entry_t
*iface
;
683 bool found
= FALSE
, changed
= FALSE
, roam
= FALSE
;
684 enumerator_t
*enumerator
;
688 enumerator
= create_ifamsg_enumerator(ifa
);
689 while (enumerator
->enumerate(enumerator
, &type
, &sockaddr
))
691 if (type
== RTAX_IFA
)
693 host
= host_create_from_sockaddr(sockaddr
);
697 enumerator
->destroy(enumerator
);
699 if (!host
|| host
->is_anyaddr(host
))
705 this->lock
->write_lock(this->lock
);
706 ifaces
= this->ifaces
->create_enumerator(this->ifaces
);
707 while (ifaces
->enumerate(ifaces
, &iface
))
709 if (iface
->ifindex
== ifa
->ifam_index
)
711 addrs
= iface
->addrs
->create_enumerator(iface
->addrs
);
712 while (addrs
->enumerate(addrs
, &addr
))
714 if (host
->ip_equals(host
, addr
->ip
))
717 if (ifa
->ifam_type
== RTM_DELADDR
)
719 iface
->addrs
->remove_at(iface
->addrs
, addrs
);
720 if (!addr
->virtual && iface
->usable
)
723 DBG1(DBG_KNL
, "%H disappeared from %s",
724 host
, iface
->ifname
);
726 addr_map_entry_remove(addr
, iface
, this);
727 addr_entry_destroy(addr
);
731 addrs
->destroy(addrs
);
733 if (!found
&& ifa
->ifam_type
== RTM_NEWADDR
)
736 .ip
= host
->clone(host
),
739 ifname
= strdup(iface
->ifname
);
740 iface
->addrs
->insert_last(iface
->addrs
, addr
);
741 addr_map_entry_add(this, addr
, iface
);
744 DBG1(DBG_KNL
, "%H appeared on %s", host
, iface
->ifname
);
748 if (changed
&& iface_entry_up_and_usable(iface
))
755 ifaces
->destroy(ifaces
);
756 this->lock
->unlock(this->lock
);
761 queue_route_reinstall(this, ifname
);
770 fire_roam_event(this, TRUE
);
775 * Re-initialize address list of an interface if it changes state
777 static void repopulate_iface(private_kernel_pfroute_net_t
*this,
778 iface_entry_t
*iface
)
780 struct ifaddrs
*ifap
, *ifa
;
783 while (iface
->addrs
->remove_last(iface
->addrs
, (void**)&addr
) == SUCCESS
)
785 addr_map_entry_remove(addr
, iface
, this);
786 addr_entry_destroy(addr
);
789 if (getifaddrs(&ifap
) == 0)
791 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
793 if (ifa
->ifa_addr
&& streq(ifa
->ifa_name
, iface
->ifname
))
795 switch (ifa
->ifa_addr
->sa_family
)
800 .ip
= host_create_from_sockaddr(ifa
->ifa_addr
),
802 iface
->addrs
->insert_last(iface
->addrs
, addr
);
803 addr_map_entry_add(this, addr
, iface
);
815 * Process an RTM_IFINFO message from the kernel
817 static void process_link(private_kernel_pfroute_net_t
*this,
818 struct if_msghdr
*msg
)
820 enumerator_t
*enumerator
;
821 iface_entry_t
*iface
;
822 bool roam
= FALSE
, found
= FALSE
, update_routes
= FALSE
;
824 this->lock
->write_lock(this->lock
);
825 enumerator
= this->ifaces
->create_enumerator(this->ifaces
);
826 while (enumerator
->enumerate(enumerator
, &iface
))
828 if (iface
->ifindex
== msg
->ifm_index
)
832 if (!(iface
->flags
& IFF_UP
) && (msg
->ifm_flags
& IFF_UP
))
834 roam
= update_routes
= TRUE
;
835 DBG1(DBG_KNL
, "interface %s activated", iface
->ifname
);
837 else if ((iface
->flags
& IFF_UP
) && !(msg
->ifm_flags
& IFF_UP
))
840 DBG1(DBG_KNL
, "interface %s deactivated", iface
->ifname
);
844 /* There seems to be a race condition on 10.10, where we get
845 * the RTM_IFINFO, but getifaddrs() does not return the virtual
846 * IP installed on a tun device, but we also don't get a
847 * RTM_NEWADDR. We therefore could miss the new address, letting
848 * virtual IP installation fail. Delaying getifaddrs() helps,
849 * but is obviously not a clean fix. */
852 iface
->flags
= msg
->ifm_flags
;
853 repopulate_iface(this, iface
);
858 enumerator
->destroy(enumerator
);
863 .ifindex
= msg
->ifm_index
,
864 .flags
= msg
->ifm_flags
,
865 .addrs
= linked_list_create(),
868 /* Similar to the issue described above, on 10.13 we need this delay as
869 * we might otherwise not be able to convert the index to a name yet. */
872 if (if_indextoname(iface
->ifindex
, iface
->ifname
))
874 DBG1(DBG_KNL
, "interface %s appeared", iface
->ifname
);
875 iface
->usable
= charon
->kernel
->is_interface_usable(charon
->kernel
,
877 repopulate_iface(this, iface
);
878 this->ifaces
->insert_last(this->ifaces
, iface
);
881 roam
= update_routes
= TRUE
;
889 this->lock
->unlock(this->lock
);
893 queue_route_reinstall(this, strdup(iface
->ifname
));
898 fire_roam_event(this, TRUE
);
902 #ifdef HAVE_RTM_IFANNOUNCE
905 * Process an RTM_IFANNOUNCE message from the kernel
907 static void process_announce(private_kernel_pfroute_net_t
*this,
908 struct if_announcemsghdr
*msg
)
910 enumerator_t
*enumerator
;
911 iface_entry_t
*iface
;
913 if (msg
->ifan_what
!= IFAN_DEPARTURE
)
915 /* we handle new interfaces in process_link() */
919 this->lock
->write_lock(this->lock
);
920 enumerator
= this->ifaces
->create_enumerator(this->ifaces
);
921 while (enumerator
->enumerate(enumerator
, &iface
))
923 if (iface
->ifindex
== msg
->ifan_index
)
925 DBG1(DBG_KNL
, "interface %s disappeared", iface
->ifname
);
926 this->ifaces
->remove_at(this->ifaces
, enumerator
);
927 iface_entry_destroy(iface
);
931 enumerator
->destroy(enumerator
);
932 this->lock
->unlock(this->lock
);
935 #endif /* HAVE_RTM_IFANNOUNCE */
938 * Process an RTM_*ROUTE message from the kernel
940 static void process_route(private_kernel_pfroute_net_t
*this,
941 struct rt_msghdr
*msg
)
947 * Receives PF_ROUTE messages from kernel
949 static bool receive_events(private_kernel_pfroute_net_t
*this, int fd
,
950 watcher_event_t event
)
954 struct rt_msghdr rtm
;
955 struct if_msghdr ifm
;
956 struct ifa_msghdr ifam
;
957 #ifdef HAVE_RTM_IFANNOUNCE
958 struct if_announcemsghdr ifanm
;
961 char buf
[sizeof(struct sockaddr_storage
) * RTAX_MAX
];
965 len
= recv(this->socket
, &msg
, sizeof(msg
), MSG_DONTWAIT
);
974 DBG1(DBG_KNL
, "unable to receive from PF_ROUTE event socket");
980 if (len
< offsetof(struct rt_msghdr
, rtm_flags
) || len
< msg
.rtm
.rtm_msglen
)
982 DBG1(DBG_KNL
, "received invalid PF_ROUTE message");
985 if (msg
.rtm
.rtm_version
!= RTM_VERSION
)
987 DBG1(DBG_KNL
, "received PF_ROUTE message with unsupported version: %d",
988 msg
.rtm
.rtm_version
);
991 switch (msg
.rtm
.rtm_type
)
995 hdrlen
= sizeof(msg
.ifam
);
998 hdrlen
= sizeof(msg
.ifm
);
1000 #ifdef HAVE_RTM_IFANNOUNCE
1001 case RTM_IFANNOUNCE
:
1002 hdrlen
= sizeof(msg
.ifanm
);
1004 #endif /* HAVE_RTM_IFANNOUNCE */
1008 hdrlen
= sizeof(msg
.rtm
);
1013 if (msg
.rtm
.rtm_msglen
< hdrlen
)
1015 DBG1(DBG_KNL
, "ignoring short PF_ROUTE message");
1018 switch (msg
.rtm
.rtm_type
)
1022 process_addr(this, &msg
.ifam
);
1025 process_link(this, &msg
.ifm
);
1027 #ifdef HAVE_RTM_IFANNOUNCE
1028 case RTM_IFANNOUNCE
:
1029 process_announce(this, &msg
.ifanm
);
1031 #endif /* HAVE_RTM_IFANNOUNCE */
1034 process_route(this, &msg
.rtm
);
1040 this->mutex
->lock(this->mutex
);
1041 if (msg
.rtm
.rtm_pid
== this->pid
&& msg
.rtm
.rtm_seq
== this->waiting_seq
)
1043 /* seems like the message someone is waiting for, deliver */
1044 this->reply
= realloc(this->reply
, msg
.rtm
.rtm_msglen
);
1045 memcpy(this->reply
, &msg
, msg
.rtm
.rtm_msglen
);
1047 /* signal on any event, add_ip()/del_ip() might wait for it */
1048 this->condvar
->broadcast(this->condvar
);
1049 this->mutex
->unlock(this->mutex
);
1055 /** enumerator over addresses */
1057 private_kernel_pfroute_net_t
* this;
1058 /** which addresses to enumerate */
1059 kernel_address_type_t which
;
1060 } address_enumerator_t
;
1062 CALLBACK(address_enumerator_destroy
, void,
1063 address_enumerator_t
*data
)
1065 data
->this->lock
->unlock(data
->this->lock
);
1069 CALLBACK(filter_addresses
, bool,
1070 address_enumerator_t
*data
, enumerator_t
*orig
, va_list args
)
1074 struct sockaddr_in6
*sin6
;
1076 VA_ARGS_VGET(args
, out
);
1078 while (orig
->enumerate(orig
, &addr
))
1080 if (!(data
->which
& ADDR_TYPE_VIRTUAL
) && addr
->virtual)
1081 { /* skip virtual interfaces added by us */
1084 if (!(data
->which
& ADDR_TYPE_REGULAR
) && !addr
->virtual)
1085 { /* address is regular, but not requested */
1089 if (ip
->get_family(ip
) == AF_INET6
)
1091 sin6
= (struct sockaddr_in6
*)ip
->get_sockaddr(ip
);
1092 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
1093 { /* skip addresses with a unusable scope */
1104 * enumerator constructor for interfaces
1106 static enumerator_t
*create_iface_enumerator(iface_entry_t
*iface
,
1107 address_enumerator_t
*data
)
1109 return enumerator_create_filter(iface
->addrs
->create_enumerator(iface
->addrs
),
1110 filter_addresses
, data
, NULL
);
1113 CALLBACK(filter_interfaces
, bool,
1114 address_enumerator_t
*data
, enumerator_t
*orig
, va_list args
)
1116 iface_entry_t
*iface
, **out
;
1118 VA_ARGS_VGET(args
, out
);
1120 while (orig
->enumerate(orig
, &iface
))
1122 if (!(data
->which
& ADDR_TYPE_IGNORED
) && !iface
->usable
)
1123 { /* skip interfaces excluded by config */
1126 if (!(data
->which
& ADDR_TYPE_LOOPBACK
) && (iface
->flags
& IFF_LOOPBACK
))
1127 { /* ignore loopback devices */
1130 if (!(data
->which
& ADDR_TYPE_DOWN
) && !(iface
->flags
& IFF_UP
))
1131 { /* skip interfaces not up */
1140 METHOD(kernel_net_t
, create_address_enumerator
, enumerator_t
*,
1141 private_kernel_pfroute_net_t
*this, kernel_address_type_t which
)
1143 address_enumerator_t
*data
;
1150 this->lock
->read_lock(this->lock
);
1151 return enumerator_create_nested(
1152 enumerator_create_filter(
1153 this->ifaces
->create_enumerator(this->ifaces
),
1154 filter_interfaces
, data
, NULL
),
1155 (void*)create_iface_enumerator
, data
,
1156 address_enumerator_destroy
);
1159 METHOD(kernel_net_t
, get_features
, kernel_feature_t
,
1160 private_kernel_pfroute_net_t
*this)
1162 return KERNEL_REQUIRE_EXCLUDE_ROUTE
;
1165 METHOD(kernel_net_t
, get_interface_name
, bool,
1166 private_kernel_pfroute_net_t
*this, host_t
* ip
, char **name
)
1168 addr_map_entry_t
*entry
, lookup
= {
1172 if (ip
->is_anyaddr(ip
))
1176 this->lock
->read_lock(this->lock
);
1177 /* first try to find it on an up and usable interface */
1178 entry
= this->addrs
->get_match(this->addrs
, &lookup
,
1179 (void*)addr_map_entry_match_up_and_usable
);
1184 *name
= strdup(entry
->iface
->ifname
);
1185 DBG2(DBG_KNL
, "%H is on interface %s", ip
, *name
);
1187 this->lock
->unlock(this->lock
);
1190 /* check if it is a virtual IP */
1191 entry
= this->addrs
->get_match(this->addrs
, &lookup
,
1192 (void*)addr_map_entry_match_virtual
);
1197 *name
= strdup(entry
->iface
->ifname
);
1198 DBG2(DBG_KNL
, "virtual IP %H is on interface %s", ip
, *name
);
1200 this->lock
->unlock(this->lock
);
1203 /* maybe it is installed on an ignored interface */
1204 entry
= this->addrs
->get_match(this->addrs
, &lookup
,
1205 (void*)addr_map_entry_match_up
);
1207 { /* the address does not exist, is on a down interface */
1208 DBG2(DBG_KNL
, "%H is not a local address or the interface is down", ip
);
1210 this->lock
->unlock(this->lock
);
1214 METHOD(kernel_net_t
, add_ip
, status_t
,
1215 private_kernel_pfroute_net_t
*this, host_t
*vip
, int prefix
,
1218 enumerator_t
*ifaces
, *addrs
;
1219 iface_entry_t
*iface
;
1222 bool timeout
= FALSE
;
1224 if (!this->install_virtual_ip
)
1225 { /* disabled by config */
1229 tun
= tun_device_create(NULL
);
1236 prefix
= vip
->get_address(vip
).len
* 8;
1238 if (!tun
->up(tun
) || !tun
->set_address(tun
, vip
, prefix
))
1244 /* wait until address appears */
1245 this->mutex
->lock(this->mutex
);
1246 while (!timeout
&& !get_interface_name(this, vip
, NULL
))
1248 timeout
= this->condvar
->timed_wait(this->condvar
, this->mutex
,
1251 this->mutex
->unlock(this->mutex
);
1254 DBG1(DBG_KNL
, "virtual IP %H did not appear on %s",
1255 vip
, tun
->get_name(tun
));
1260 this->lock
->write_lock(this->lock
);
1261 this->tuns
->insert_last(this->tuns
, tun
);
1263 ifaces
= this->ifaces
->create_enumerator(this->ifaces
);
1264 while (ifaces
->enumerate(ifaces
, &iface
))
1266 if (streq(iface
->ifname
, tun
->get_name(tun
)))
1268 addrs
= iface
->addrs
->create_enumerator(iface
->addrs
);
1269 while (addrs
->enumerate(addrs
, &addr
))
1271 if (addr
->ip
->ip_equals(addr
->ip
, vip
))
1273 addr
->virtual = TRUE
;
1276 addrs
->destroy(addrs
);
1277 /* during IKEv1 reauthentication, children get moved from
1278 * old the new SA before the virtual IP is available. This
1279 * kills the route for our virtual IP, reinstall. */
1280 queue_route_reinstall(this, strdup(iface
->ifname
));
1284 ifaces
->destroy(ifaces
);
1285 /* lets do this while holding the lock, thus preventing another thread
1286 * from deleting the TUN device concurrently, hopefully listeners are quick
1287 * and cause no deadlocks */
1288 charon
->kernel
->tun(charon
->kernel
, tun
, TRUE
);
1289 this->lock
->unlock(this->lock
);
1294 METHOD(kernel_net_t
, del_ip
, status_t
,
1295 private_kernel_pfroute_net_t
*this, host_t
*vip
, int prefix
,
1298 enumerator_t
*enumerator
;
1301 bool timeout
= FALSE
, found
= FALSE
;
1303 if (!this->install_virtual_ip
)
1304 { /* disabled by config */
1308 this->lock
->write_lock(this->lock
);
1309 enumerator
= this->tuns
->create_enumerator(this->tuns
);
1310 while (enumerator
->enumerate(enumerator
, &tun
))
1312 addr
= tun
->get_address(tun
, NULL
);
1313 if (addr
&& addr
->ip_equals(addr
, vip
))
1315 this->tuns
->remove_at(this->tuns
, enumerator
);
1316 charon
->kernel
->tun(charon
->kernel
, tun
, FALSE
);
1322 enumerator
->destroy(enumerator
);
1323 this->lock
->unlock(this->lock
);
1329 /* wait until address disappears */
1332 this->mutex
->lock(this->mutex
);
1333 while (!timeout
&& get_interface_name(this, vip
, NULL
))
1335 timeout
= this->condvar
->timed_wait(this->condvar
, this->mutex
,
1338 this->mutex
->unlock(this->mutex
);
1341 DBG1(DBG_KNL
, "virtual IP %H did not disappear from tun", vip
);
1349 * Append a sockaddr_in/in6 of given type to routing message
1351 static void add_rt_addr(struct rt_msghdr
*hdr
, int type
, host_t
*addr
)
1357 len
= *addr
->get_sockaddr_len(addr
);
1358 memcpy((char*)hdr
+ hdr
->rtm_msglen
, addr
->get_sockaddr(addr
), len
);
1359 hdr
->rtm_msglen
+= SA_LEN(len
);
1360 hdr
->rtm_addrs
|= type
;
1365 * Append a subnet mask sockaddr using the given prefix to routing message
1367 static void add_rt_mask(struct rt_msghdr
*hdr
, int type
, int family
, int prefix
)
1371 mask
= host_create_netmask(family
, prefix
);
1374 add_rt_addr(hdr
, type
, mask
);
1375 mask
->destroy(mask
);
1380 * Append an interface name sockaddr_dl to routing message
1382 static void add_rt_ifname(struct rt_msghdr
*hdr
, int type
, char *name
)
1384 struct sockaddr_dl sdl
= {
1385 .sdl_len
= sizeof(struct sockaddr_dl
),
1386 .sdl_family
= AF_LINK
,
1387 .sdl_nlen
= strlen(name
),
1390 if (strlen(name
) <= sizeof(sdl
.sdl_data
))
1392 memcpy(sdl
.sdl_data
, name
, sdl
.sdl_nlen
);
1393 memcpy((char*)hdr
+ hdr
->rtm_msglen
, &sdl
, sdl
.sdl_len
);
1394 hdr
->rtm_msglen
+= SA_LEN(sdl
.sdl_len
);
1395 hdr
->rtm_addrs
|= type
;
1400 * Add or remove a route
1402 static status_t
manage_route(private_kernel_pfroute_net_t
*this, int op
,
1403 chunk_t dst_net
, uint8_t prefixlen
,
1404 host_t
*gateway
, char *if_name
)
1407 struct rt_msghdr hdr
;
1408 char buf
[sizeof(struct sockaddr_storage
) * RTAX_MAX
];
1411 .rtm_version
= RTM_VERSION
,
1413 .rtm_flags
= RTF_UP
| RTF_STATIC
,
1414 .rtm_pid
= this->pid
,
1415 .rtm_seq
= ref_get(&this->seq
),
1421 if (prefixlen
== 0 && dst_net
.len
)
1426 half
= chunk_clonea(dst_net
);
1427 half
.ptr
[0] |= 0x80;
1429 status
= manage_route(this, op
, half
, prefixlen
, gateway
, if_name
);
1430 if (status
!= SUCCESS
)
1436 dst
= host_create_from_chunk(AF_UNSPEC
, dst_net
, 0);
1442 if ((dst
->get_family(dst
) == AF_INET
&& prefixlen
== 32) ||
1443 (dst
->get_family(dst
) == AF_INET6
&& prefixlen
== 128))
1445 msg
.hdr
.rtm_flags
|= RTF_HOST
| RTF_GATEWAY
;
1448 msg
.hdr
.rtm_msglen
= sizeof(struct rt_msghdr
);
1449 for (type
= 0; type
< RTAX_MAX
; type
++)
1454 add_rt_addr(&msg
.hdr
, RTA_DST
, dst
);
1457 if (!(msg
.hdr
.rtm_flags
& RTF_HOST
))
1459 add_rt_mask(&msg
.hdr
, RTA_NETMASK
,
1460 dst
->get_family(dst
), prefixlen
);
1466 add_rt_ifname(&msg
.hdr
, RTA_IFP
, if_name
);
1471 gateway
->get_family(gateway
) == dst
->get_family(dst
))
1473 add_rt_addr(&msg
.hdr
, RTA_GATEWAY
, gateway
);
1482 if (send(this->socket
, &msg
, msg
.hdr
.rtm_msglen
, 0) != msg
.hdr
.rtm_msglen
)
1484 if (errno
== EEXIST
)
1486 return ALREADY_DONE
;
1488 DBG1(DBG_KNL
, "%s PF_ROUTE route failed: %s",
1489 op
== RTM_ADD
? "adding" : "deleting", strerror(errno
));
1495 METHOD(kernel_net_t
, add_route
, status_t
,
1496 private_kernel_pfroute_net_t
*this, chunk_t dst_net
, uint8_t prefixlen
,
1497 host_t
*gateway
, host_t
*src_ip
, char *if_name
, bool pass
)
1500 route_entry_t
*found
, route
= {
1502 .prefixlen
= prefixlen
,
1507 this->routes_lock
->lock(this->routes_lock
);
1508 found
= this->routes
->get(this->routes
, &route
);
1511 this->routes_lock
->unlock(this->routes_lock
);
1512 return ALREADY_DONE
;
1514 status
= manage_route(this, RTM_ADD
, dst_net
, prefixlen
, gateway
, if_name
);
1515 if (status
== SUCCESS
)
1517 found
= route_entry_clone(&route
);
1518 this->routes
->put(this->routes
, found
, found
);
1520 this->routes_lock
->unlock(this->routes_lock
);
1524 METHOD(kernel_net_t
, del_route
, status_t
,
1525 private_kernel_pfroute_net_t
*this, chunk_t dst_net
, uint8_t prefixlen
,
1526 host_t
*gateway
, host_t
*src_ip
, char *if_name
, bool pass
)
1529 route_entry_t
*found
, route
= {
1531 .prefixlen
= prefixlen
,
1536 this->routes_lock
->lock(this->routes_lock
);
1537 found
= this->routes
->get(this->routes
, &route
);
1540 this->routes_lock
->unlock(this->routes_lock
);
1543 this->routes
->remove(this->routes
, found
);
1544 route_entry_destroy(found
);
1545 status
= manage_route(this, RTM_DELETE
, dst_net
, prefixlen
, gateway
,
1547 this->routes_lock
->unlock(this->routes_lock
);
1552 * Do a route lookup for dest and return either the nexthop or the source
1555 static host_t
*get_route(private_kernel_pfroute_net_t
*this, bool nexthop
,
1556 host_t
*dest
, host_t
*src
, char **iface
)
1559 struct rt_msghdr hdr
;
1560 char buf
[sizeof(struct sockaddr_storage
) * RTAX_MAX
];
1563 .rtm_version
= RTM_VERSION
,
1564 .rtm_type
= RTM_GET
,
1565 .rtm_pid
= this->pid
,
1566 .rtm_seq
= ref_get(&this->seq
),
1569 host_t
*host
= NULL
;
1570 enumerator_t
*enumerator
;
1571 struct sockaddr
*addr
;
1572 bool failed
= FALSE
;
1576 msg
.hdr
.rtm_msglen
= sizeof(struct rt_msghdr
);
1577 for (type
= 0; type
< RTAX_MAX
; type
++)
1582 add_rt_addr(&msg
.hdr
, RTA_DST
, dest
);
1585 add_rt_addr(&msg
.hdr
, RTA_IFA
, src
);
1589 { /* add an empty IFP to ensure we get a source address */
1590 add_rt_ifname(&msg
.hdr
, RTA_IFP
, "");
1597 this->mutex
->lock(this->mutex
);
1599 while (this->waiting_seq
)
1601 this->condvar
->wait(this->condvar
, this->mutex
);
1603 this->waiting_seq
= msg
.hdr
.rtm_seq
;
1604 if (send(this->socket
, &msg
, msg
.hdr
.rtm_msglen
, 0) == msg
.hdr
.rtm_msglen
)
1608 if (this->condvar
->timed_wait(this->condvar
, this->mutex
, 1000))
1616 enumerator
= create_rtmsg_enumerator(this->reply
);
1617 while (enumerator
->enumerate(enumerator
, &type
, &addr
))
1621 if (type
== RTAX_DST
&& this->reply
->rtm_flags
& RTF_HOST
)
1622 { /* probably a cloned/cached direct route, only use that
1623 * as fallback if no gateway is found */
1624 host
= host
?: host_create_from_sockaddr(addr
);
1626 if (type
== RTAX_GATEWAY
)
1627 { /* could actually be a MAC address */
1628 host_t
*gtw
= host_create_from_sockaddr(addr
);
1635 if (type
== RTAX_IFP
&& addr
->sa_family
== AF_LINK
)
1637 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
1641 *iface
= strndup(sdl
->sdl_data
, sdl
->sdl_nlen
);
1647 if (type
== RTAX_IFA
)
1649 host
= host_create_from_sockaddr(addr
);
1653 enumerator
->destroy(enumerator
);
1663 /* signal completion of query to a waiting thread */
1664 this->waiting_seq
= 0;
1665 this->condvar
->signal(this->condvar
);
1666 this->mutex
->unlock(this->mutex
);
1671 { /* the given source address might be gone, try again without */
1673 msg
.hdr
.rtm_seq
= ref_get(&this->seq
);
1674 msg
.hdr
.rtm_addrs
= 0;
1675 memset(msg
.buf
, 0, sizeof(msg
.buf
));
1678 DBG1(DBG_KNL
, "PF_ROUTE lookup failed: %s", strerror(errno
));
1682 host
= host
?: dest
->clone(dest
);
1685 { /* make sure the source address is not virtual and usable */
1686 addr_entry_t
*entry
, lookup
= {
1694 this->lock
->read_lock(this->lock
);
1695 entry
= this->addrs
->get_match(this->addrs
, &lookup
,
1696 (void*)addr_map_entry_match_up_and_usable
);
1697 this->lock
->unlock(this->lock
);
1700 host
->destroy(host
);
1704 DBG2(DBG_KNL
, "using %H as %s to reach %H", host
,
1705 nexthop
? "nexthop" : "address", dest
);
1709 METHOD(kernel_net_t
, get_source_addr
, host_t
*,
1710 private_kernel_pfroute_net_t
*this, host_t
*dest
, host_t
*src
)
1712 return get_route(this, FALSE
, dest
, src
, NULL
);
1715 METHOD(kernel_net_t
, get_nexthop
, host_t
*,
1716 private_kernel_pfroute_net_t
*this, host_t
*dest
, int prefix
, host_t
*src
,
1723 return get_route(this, TRUE
, dest
, src
, iface
);
1727 * Get the number of set bits in the given netmask
1729 static uint8_t sockaddr_to_netmask(sockaddr_t
*sockaddr
, host_t
*dst
)
1731 uint8_t len
= 0, i
, byte
, mask
= 0;
1732 struct sockaddr_storage ss
;
1735 /* at least some older FreeBSD versions send us shorter sockaddrs
1736 * with the family set to -1 (255) */
1737 if (sockaddr
->sa_family
== 255)
1739 memset(&ss
, 0, sizeof(ss
));
1740 memcpy(&ss
, sockaddr
, sockaddr
->sa_len
);
1741 /* use the address family and length of the destination as hint */
1742 ss
.ss_len
= *dst
->get_sockaddr_len(dst
);
1743 ss
.ss_family
= dst
->get_family(dst
);
1744 sockaddr
= (sockaddr_t
*)&ss
;
1747 switch (sockaddr
->sa_family
)
1751 addr
= (char*)&((struct sockaddr_in
*)sockaddr
)->sin_addr
;
1755 addr
= (char*)&((struct sockaddr_in6
*)sockaddr
)->sin6_addr
;
1761 for (i
= 0; i
< len
; i
++)
1785 /** enumerator over subnets */
1787 enumerator_t
public;
1788 /** sysctl result */
1790 /** length of the complete result */
1792 /** start of the current route entry */
1794 /** last subnet enumerated */
1796 /** interface of current net */
1798 } subnet_enumerator_t
;
1800 METHOD(enumerator_t
, destroy_subnet_enumerator
, void,
1801 subnet_enumerator_t
*this)
1803 DESTROY_IF(this->net
);
1809 METHOD(enumerator_t
, enumerate_subnets
, bool,
1810 subnet_enumerator_t
*this, va_list args
)
1812 enumerator_t
*enumerator
;
1814 struct rt_msghdr
*rtm
;
1815 struct sockaddr
*addr
;
1820 VA_ARGS_VGET(args
, net
, mask
, ifname
);
1824 this->current
= this->buf
;
1828 rtm
= (struct rt_msghdr
*)this->current
;
1829 this->current
+= rtm
->rtm_msglen
;
1830 DESTROY_IF(this->net
);
1833 this->ifname
= NULL
;
1836 for (; this->current
< this->buf
+ this->len
;
1837 this->current
+= rtm
->rtm_msglen
)
1839 struct sockaddr
*netmask
= NULL
;
1840 uint8_t netbits
= 0;
1842 rtm
= (struct rt_msghdr
*)this->current
;
1844 if (rtm
->rtm_version
!= RTM_VERSION
)
1848 if (rtm
->rtm_flags
& RTF_GATEWAY
||
1849 rtm
->rtm_flags
& RTF_HOST
||
1850 rtm
->rtm_flags
& RTF_REJECT
)
1854 enumerator
= create_rtmsg_enumerator(rtm
);
1855 while (enumerator
->enumerate(enumerator
, &type
, &addr
))
1857 if (type
== RTAX_DST
)
1859 this->net
= this->net
?: host_create_from_sockaddr(addr
);
1861 if (type
== RTAX_NETMASK
)
1865 if (type
== RTAX_IFP
&& addr
->sa_family
== AF_LINK
)
1867 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
1869 this->ifname
= strndup(sdl
->sdl_data
, sdl
->sdl_nlen
);
1872 if (this->net
&& netmask
)
1874 netbits
= sockaddr_to_netmask(netmask
, this->net
);
1876 enumerator
->destroy(enumerator
);
1878 if (this->net
&& this->ifname
)
1881 *mask
= netbits
?: this->net
->get_address(this->net
).len
* 8;
1882 *ifname
= this->ifname
;
1889 METHOD(kernel_net_t
, create_local_subnet_enumerator
, enumerator_t
*,
1890 private_kernel_pfroute_net_t
*this)
1892 subnet_enumerator_t
*enumerator
;
1896 CTL_NET
, PF_ROUTE
, 0, AF_UNSPEC
, NET_RT_DUMP
, 0, 0
1899 if (sysctl(mib
, countof(mib
), NULL
, &len
, NULL
, 0) < 0)
1901 DBG2(DBG_KNL
, "enumerating local subnets failed");
1902 return enumerator_create_empty();
1905 if (sysctl(mib
, countof(mib
), buf
, &len
, NULL
, 0) < 0)
1907 DBG2(DBG_KNL
, "enumerating local subnets failed");
1909 return enumerator_create_empty();
1914 .enumerate
= enumerator_enumerate_default
,
1915 .venumerate
= _enumerate_subnets
,
1916 .destroy
= _destroy_subnet_enumerator
,
1921 return &enumerator
->public;
1925 * Initialize a list of local addresses.
1927 static status_t
init_address_list(private_kernel_pfroute_net_t
*this)
1929 struct ifaddrs
*ifap
, *ifa
;
1930 iface_entry_t
*iface
, *current
;
1932 enumerator_t
*ifaces
, *addrs
;
1934 DBG2(DBG_KNL
, "known interfaces and IP addresses:");
1936 if (getifaddrs(&ifap
) < 0)
1938 DBG1(DBG_KNL
, " failed to get interfaces!");
1942 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
1944 if (ifa
->ifa_addr
== NULL
)
1948 switch(ifa
->ifa_addr
->sa_family
)
1955 ifaces
= this->ifaces
->create_enumerator(this->ifaces
);
1956 while (ifaces
->enumerate(ifaces
, ¤t
))
1958 if (streq(current
->ifname
, ifa
->ifa_name
))
1964 ifaces
->destroy(ifaces
);
1969 .ifindex
= if_nametoindex(ifa
->ifa_name
),
1970 .flags
= ifa
->ifa_flags
,
1971 .addrs
= linked_list_create(),
1972 .usable
= charon
->kernel
->is_interface_usable(
1973 charon
->kernel
, ifa
->ifa_name
),
1975 memcpy(iface
->ifname
, ifa
->ifa_name
, IFNAMSIZ
);
1976 this->ifaces
->insert_last(this->ifaces
, iface
);
1979 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
1982 .ip
= host_create_from_sockaddr(ifa
->ifa_addr
),
1984 iface
->addrs
->insert_last(iface
->addrs
, addr
);
1985 addr_map_entry_add(this, addr
, iface
);
1992 ifaces
= this->ifaces
->create_enumerator(this->ifaces
);
1993 while (ifaces
->enumerate(ifaces
, &iface
))
1995 if (iface
->usable
&& iface
->flags
& IFF_UP
)
1997 DBG2(DBG_KNL
, " %s", iface
->ifname
);
1998 addrs
= iface
->addrs
->create_enumerator(iface
->addrs
);
1999 while (addrs
->enumerate(addrs
, (void**)&addr
))
2001 DBG2(DBG_KNL
, " %H", addr
->ip
);
2003 addrs
->destroy(addrs
);
2006 ifaces
->destroy(ifaces
);
2011 METHOD(kernel_net_t
, destroy
, void,
2012 private_kernel_pfroute_net_t
*this)
2014 enumerator_t
*enumerator
;
2015 route_entry_t
*route
;
2018 enumerator
= this->routes
->create_enumerator(this->routes
);
2019 while (enumerator
->enumerate(enumerator
, NULL
, (void**)&route
))
2021 manage_route(this, RTM_DELETE
, route
->dst_net
, route
->prefixlen
,
2022 route
->gateway
, route
->if_name
);
2023 route_entry_destroy(route
);
2025 enumerator
->destroy(enumerator
);
2026 this->routes
->destroy(this->routes
);
2027 this->routes_lock
->destroy(this->routes_lock
);
2029 if (this->socket
!= -1)
2031 lib
->watcher
->remove(lib
->watcher
, this->socket
);
2032 close(this->socket
);
2035 net_changes_clear(this);
2036 this->net_changes
->destroy(this->net_changes
);
2037 this->net_changes_lock
->destroy(this->net_changes_lock
);
2039 enumerator
= this->addrs
->create_enumerator(this->addrs
);
2040 while (enumerator
->enumerate(enumerator
, NULL
, (void**)&addr
))
2044 enumerator
->destroy(enumerator
);
2045 this->addrs
->destroy(this->addrs
);
2046 this->ifaces
->destroy_function(this->ifaces
, (void*)iface_entry_destroy
);
2047 this->tuns
->destroy(this->tuns
);
2048 this->lock
->destroy(this->lock
);
2049 this->mutex
->destroy(this->mutex
);
2050 this->condvar
->destroy(this->condvar
);
2051 this->roam_lock
->destroy(this->roam_lock
);
2057 * Described in header.
2059 kernel_pfroute_net_t
*kernel_pfroute_net_create()
2061 private_kernel_pfroute_net_t
*this;
2066 .get_features
= _get_features
,
2067 .get_interface
= _get_interface_name
,
2068 .create_address_enumerator
= _create_address_enumerator
,
2069 .create_local_subnet_enumerator
= _create_local_subnet_enumerator
,
2070 .get_source_addr
= _get_source_addr
,
2071 .get_nexthop
= _get_nexthop
,
2074 .add_route
= _add_route
,
2075 .del_route
= _del_route
,
2076 .destroy
= _destroy
,
2080 .ifaces
= linked_list_create(),
2081 .addrs
= hashtable_create(
2082 (hashtable_hash_t
)addr_map_entry_hash
,
2083 (hashtable_equals_t
)addr_map_entry_equals
, 16),
2084 .routes
= hashtable_create((hashtable_hash_t
)route_entry_hash
,
2085 (hashtable_equals_t
)route_entry_equals
, 16),
2086 .net_changes
= hashtable_create(
2087 (hashtable_hash_t
)net_change_hash
,
2088 (hashtable_equals_t
)net_change_equals
, 16),
2089 .tuns
= linked_list_create(),
2090 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
2091 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2092 .condvar
= condvar_create(CONDVAR_TYPE_DEFAULT
),
2093 .routes_lock
= mutex_create(MUTEX_TYPE_DEFAULT
),
2094 .net_changes_lock
= mutex_create(MUTEX_TYPE_DEFAULT
),
2095 .roam_lock
= spinlock_create(),
2096 .vip_wait
= lib
->settings
->get_int(lib
->settings
,
2097 "%s.plugins.kernel-pfroute.vip_wait", 1000, lib
->ns
),
2098 .install_virtual_ip
= lib
->settings
->get_bool(lib
->settings
,
2099 "%s.install_virtual_ip", TRUE
, lib
->ns
),
2101 timerclear(&this->last_route_reinstall
);
2102 timerclear(&this->next_roam
);
2104 /* create a PF_ROUTE socket to communicate with the kernel */
2105 this->socket
= socket(PF_ROUTE
, SOCK_RAW
, AF_UNSPEC
);
2106 if (this->socket
== -1)
2108 DBG1(DBG_KNL
, "unable to create PF_ROUTE socket");
2113 if (streq(lib
->ns
, "starter"))
2115 /* starter has no threads, so we do not register for kernel events */
2116 if (shutdown(this->socket
, SHUT_RD
) != 0)
2118 DBG1(DBG_KNL
, "closing read end of PF_ROUTE socket failed: %s",
2124 lib
->watcher
->add(lib
->watcher
, this->socket
, WATCHER_READ
,
2125 (watcher_cb_t
)receive_events
, this);
2127 if (init_address_list(this) != SUCCESS
)
2129 DBG1(DBG_KNL
, "unable to get interface list");
2134 return &this->public;