2008-09-25 07:56:58 +00:00
|
|
|
/*
|
2019-10-04 13:34:04 +00:00
|
|
|
* Copyright (C) 2008-2019 Tobias Brunner
|
2008-10-08 08:23:46 +00:00
|
|
|
* Copyright (C) 2005-2008 Martin Willi
|
2016-03-11 18:07:10 +00:00
|
|
|
* HSR Hochschule fuer Technik Rapperswil
|
2008-09-25 07:56:58 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2010-03-12 08:37:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 secunet Security Networks AG
|
|
|
|
* Copyright (C) 2010 Thomas Egerer
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
#include <sys/socket.h>
|
2012-06-18 10:01:10 +00:00
|
|
|
#include <sys/utsname.h>
|
2008-09-25 07:56:58 +00:00
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
2018-04-03 13:01:20 +00:00
|
|
|
#include <linux/if_addrlabel.h>
|
2008-09-25 07:56:58 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <net/if.h>
|
2013-10-18 07:38:01 +00:00
|
|
|
#ifdef HAVE_LINUX_FIB_RULES_H
|
2013-08-13 14:53:06 +00:00
|
|
|
#include <linux/fib_rules.h>
|
2013-10-18 07:38:01 +00:00
|
|
|
#endif
|
2008-09-25 07:56:58 +00:00
|
|
|
|
|
|
|
#include "kernel_netlink_net.h"
|
|
|
|
#include "kernel_netlink_shared.h"
|
|
|
|
|
2016-02-12 14:30:18 +00:00
|
|
|
#include <daemon.h>
|
2012-10-16 14:03:21 +00:00
|
|
|
#include <utils/debug.h>
|
2009-12-08 15:53:01 +00:00
|
|
|
#include <threading/mutex.h>
|
2012-09-20 16:21:42 +00:00
|
|
|
#include <threading/rwlock.h>
|
|
|
|
#include <threading/rwlock_condvar.h>
|
2012-09-20 09:58:52 +00:00
|
|
|
#include <threading/spinlock.h>
|
2012-10-16 12:54:16 +00:00
|
|
|
#include <collections/hashtable.h>
|
|
|
|
#include <collections/linked_list.h>
|
2008-09-25 07:56:58 +00:00
|
|
|
#include <processing/jobs/callback_job.h>
|
|
|
|
|
2010-07-06 14:03:09 +00:00
|
|
|
/** delay before firing roam events (ms) */
|
2008-09-25 07:56:58 +00:00
|
|
|
#define ROAM_DELAY 100
|
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
/** delay before reinstalling routes (ms) */
|
|
|
|
#define ROUTE_DELAY 100
|
|
|
|
|
2012-11-12 18:39:34 +00:00
|
|
|
/** maximum recursion when searching for addresses in get_route() */
|
|
|
|
#define MAX_ROUTE_RECURSION 2
|
|
|
|
|
2013-04-16 11:46:32 +00:00
|
|
|
#ifndef ROUTING_TABLE
|
|
|
|
#define ROUTING_TABLE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ROUTING_TABLE_PRIO
|
|
|
|
#define ROUTING_TABLE_PRIO 0
|
|
|
|
#endif
|
|
|
|
|
2018-01-29 14:26:17 +00:00
|
|
|
/** multicast groups (for groups > 31 setsockopt has to be used) */
|
|
|
|
#define nl_group(group) (1 << (group - 1))
|
|
|
|
|
2014-07-10 16:01:21 +00:00
|
|
|
ENUM(rt_msg_names, RTM_NEWLINK, RTM_GETRULE,
|
|
|
|
"RTM_NEWLINK",
|
|
|
|
"RTM_DELLINK",
|
|
|
|
"RTM_GETLINK",
|
|
|
|
"RTM_SETLINK",
|
|
|
|
"RTM_NEWADDR",
|
|
|
|
"RTM_DELADDR",
|
|
|
|
"RTM_GETADDR",
|
2019-10-22 09:04:30 +00:00
|
|
|
"23",
|
2014-07-10 16:01:21 +00:00
|
|
|
"RTM_NEWROUTE",
|
|
|
|
"RTM_DELROUTE",
|
|
|
|
"RTM_GETROUTE",
|
2019-10-22 09:04:30 +00:00
|
|
|
"27",
|
2014-07-10 16:01:21 +00:00
|
|
|
"RTM_NEWNEIGH",
|
|
|
|
"RTM_DELNEIGH",
|
|
|
|
"RTM_GETNEIGH",
|
2019-10-22 09:04:30 +00:00
|
|
|
"31",
|
2014-07-10 16:01:21 +00:00
|
|
|
"RTM_NEWRULE",
|
|
|
|
"RTM_DELRULE",
|
|
|
|
"RTM_GETRULE",
|
|
|
|
);
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
typedef struct addr_entry_t addr_entry_t;
|
|
|
|
|
|
|
|
/**
|
2012-09-19 17:10:23 +00:00
|
|
|
* IP address in an iface_entry_t
|
2008-09-25 07:56:58 +00:00
|
|
|
*/
|
|
|
|
struct addr_entry_t {
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
/** the ip address */
|
2008-09-25 07:56:58 +00:00
|
|
|
host_t *ip;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2014-03-14 14:41:52 +00:00
|
|
|
/** address flags */
|
|
|
|
u_char flags;
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/** scope of the address */
|
|
|
|
u_char scope;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 09:07:15 +00:00
|
|
|
/** number of times this IP is used, if virtual (i.e. managed by us) */
|
2008-09-25 07:56:58 +00:00
|
|
|
u_int refcount;
|
2012-09-19 17:10:23 +00:00
|
|
|
|
|
|
|
/** TRUE once it is installed, if virtual */
|
|
|
|
bool installed;
|
2008-09-25 07:56:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* destroy a addr_entry_t object
|
|
|
|
*/
|
|
|
|
static void addr_entry_destroy(addr_entry_t *this)
|
|
|
|
{
|
|
|
|
this->ip->destroy(this->ip);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct iface_entry_t iface_entry_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A network interface on this system, containing addr_entry_t's
|
|
|
|
*/
|
|
|
|
struct iface_entry_t {
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/** interface index */
|
|
|
|
int ifindex;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/** name of the interface */
|
|
|
|
char ifname[IFNAMSIZ];
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/** interface flags, as in netdevice(7) SIOCGIFFLAGS */
|
|
|
|
u_int flags;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/** list of addresses as host_t */
|
|
|
|
linked_list_t *addrs;
|
2012-09-14 14:43:08 +00:00
|
|
|
|
|
|
|
/** TRUE if usable by config */
|
|
|
|
bool usable;
|
2008-09-25 07:56:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* destroy an interface entry
|
|
|
|
*/
|
|
|
|
static void iface_entry_destroy(iface_entry_t *this)
|
|
|
|
{
|
|
|
|
this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2017-05-16 10:11:24 +00:00
|
|
|
CALLBACK(iface_entry_by_index, bool,
|
|
|
|
iface_entry_t *this, va_list args)
|
2012-09-14 14:43:08 +00:00
|
|
|
{
|
2017-05-16 10:11:24 +00:00
|
|
|
int ifindex;
|
|
|
|
|
|
|
|
VA_ARGS_VGET(args, ifindex);
|
|
|
|
return this->ifindex == ifindex;
|
2012-09-14 14:43:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 10:11:24 +00:00
|
|
|
CALLBACK(iface_entry_by_name, bool,
|
|
|
|
iface_entry_t *this, va_list args)
|
2012-09-19 17:10:23 +00:00
|
|
|
{
|
2017-05-16 10:11:24 +00:00
|
|
|
char *ifname;
|
|
|
|
|
|
|
|
VA_ARGS_VGET(args, ifname);
|
2012-09-19 17:10:23 +00:00
|
|
|
return streq(this->ifname, ifname);
|
|
|
|
}
|
|
|
|
|
2012-09-17 16:09:51 +00:00
|
|
|
/**
|
|
|
|
* check if an interface is up
|
|
|
|
*/
|
|
|
|
static inline bool iface_entry_up(iface_entry_t *iface)
|
|
|
|
{
|
|
|
|
return (iface->flags & IFF_UP) == IFF_UP;
|
|
|
|
}
|
|
|
|
|
2012-09-14 14:43:08 +00:00
|
|
|
/**
|
|
|
|
* check if an interface is up and usable
|
|
|
|
*/
|
|
|
|
static inline bool iface_entry_up_and_usable(iface_entry_t *iface)
|
|
|
|
{
|
2012-09-17 16:09:51 +00:00
|
|
|
return iface->usable && iface_entry_up(iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct addr_map_entry_t addr_map_entry_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry that maps an IP address to an interface entry
|
|
|
|
*/
|
|
|
|
struct addr_map_entry_t {
|
|
|
|
/** The IP address */
|
|
|
|
host_t *ip;
|
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
/** The address entry for this IP address */
|
|
|
|
addr_entry_t *addr;
|
|
|
|
|
2012-09-17 16:09:51 +00:00
|
|
|
/** The interface this address is installed on */
|
|
|
|
iface_entry_t *iface;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash a addr_map_entry_t object, all entries with the same IP address
|
|
|
|
* are stored in the same bucket
|
|
|
|
*/
|
|
|
|
static u_int addr_map_entry_hash(addr_map_entry_t *this)
|
|
|
|
{
|
|
|
|
return chunk_hash(this->ip->get_address(this->ip));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two addr_map_entry_t objects, two entries are equal if they are
|
|
|
|
* installed on the same interface
|
|
|
|
*/
|
|
|
|
static bool addr_map_entry_equals(addr_map_entry_t *a, addr_map_entry_t *b)
|
|
|
|
{
|
|
|
|
return a->iface->ifindex == b->iface->ifindex &&
|
|
|
|
a->ip->ip_equals(a->ip, b->ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used with get_match this finds an address entry if it is installed on
|
|
|
|
* an up and usable interface
|
|
|
|
*/
|
|
|
|
static bool addr_map_entry_match_up_and_usable(addr_map_entry_t *a,
|
|
|
|
addr_map_entry_t *b)
|
|
|
|
{
|
|
|
|
return iface_entry_up_and_usable(b->iface) &&
|
|
|
|
a->ip->ip_equals(a->ip, b->ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used with get_match this finds an address entry if it is installed on
|
|
|
|
* any active local interface
|
|
|
|
*/
|
|
|
|
static bool addr_map_entry_match_up(addr_map_entry_t *a, addr_map_entry_t *b)
|
|
|
|
{
|
|
|
|
return iface_entry_up(b->iface) && a->ip->ip_equals(a->ip, b->ip);
|
2012-09-14 14:43:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
/**
|
|
|
|
* Used with get_match this finds an address entry if it is installed on
|
|
|
|
* any local interface
|
|
|
|
*/
|
|
|
|
static bool addr_map_entry_match(addr_map_entry_t *a, addr_map_entry_t *b)
|
|
|
|
{
|
|
|
|
return a->ip->ip_equals(a->ip, b->ip);
|
|
|
|
}
|
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
typedef struct net_change_t net_change_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queued network changes
|
|
|
|
*/
|
|
|
|
struct net_change_t {
|
|
|
|
/** Name of the interface that got activated (or an IP appeared on) */
|
|
|
|
char *if_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a net_change_t object
|
|
|
|
*/
|
|
|
|
static void net_change_destroy(net_change_t *this)
|
|
|
|
{
|
|
|
|
free(this->if_name);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash a net_change_t object
|
|
|
|
*/
|
|
|
|
static u_int net_change_hash(net_change_t *this)
|
|
|
|
{
|
|
|
|
return chunk_hash(chunk_create(this->if_name, strlen(this->if_name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two net_change_t objects
|
|
|
|
*/
|
|
|
|
static bool net_change_equals(net_change_t *a, net_change_t *b)
|
|
|
|
{
|
2012-05-07 09:24:07 +00:00
|
|
|
return streq(a->if_name, b->if_name);
|
2011-12-20 13:59:21 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
typedef struct private_kernel_netlink_net_t private_kernel_netlink_net_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private variables and functions of kernel_netlink_net class.
|
|
|
|
*/
|
|
|
|
struct private_kernel_netlink_net_t {
|
|
|
|
/**
|
|
|
|
* Public part of the kernel_netlink_net_t object.
|
|
|
|
*/
|
|
|
|
kernel_netlink_net_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
2012-09-20 16:21:42 +00:00
|
|
|
* lock to access various lists and maps
|
2008-09-25 07:56:58 +00:00
|
|
|
*/
|
2012-09-20 16:21:42 +00:00
|
|
|
rwlock_t *lock;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* condition variable to signal virtual IP add/removal
|
|
|
|
*/
|
2012-09-20 16:21:42 +00:00
|
|
|
rwlock_condvar_t *condvar;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* Cached list of interfaces and its addresses (iface_entry_t)
|
|
|
|
*/
|
|
|
|
linked_list_t *ifaces;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-17 16:09:51 +00:00
|
|
|
/**
|
|
|
|
* Map for IP addresses to iface_entry_t objects (addr_map_entry_t)
|
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
hashlist_t *addrs;
|
2012-09-17 16:09:51 +00:00
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
/**
|
|
|
|
* Map for virtual IP addresses to iface_entry_t objects (addr_map_entry_t)
|
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
hashlist_t *vips;
|
2012-09-19 17:10:23 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* netlink rt socket (routing)
|
|
|
|
*/
|
|
|
|
netlink_socket_t *socket;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* Netlink rt socket to receive address change events
|
|
|
|
*/
|
|
|
|
int socket_events;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
2012-09-20 09:58:52 +00:00
|
|
|
* earliest time of the next roam event
|
2008-09-25 07:56:58 +00:00
|
|
|
*/
|
2012-09-20 09:58:52 +00:00
|
|
|
timeval_t next_roam;
|
|
|
|
|
2013-08-12 09:23:34 +00:00
|
|
|
/**
|
|
|
|
* roam event due to address change
|
|
|
|
*/
|
|
|
|
bool roam_address;
|
|
|
|
|
2012-09-20 09:58:52 +00:00
|
|
|
/**
|
|
|
|
* lock to check and update roam event time
|
|
|
|
*/
|
|
|
|
spinlock_t *roam_lock;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* routing table to install routes
|
|
|
|
*/
|
2019-09-24 10:59:00 +00:00
|
|
|
uint32_t routing_table;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* priority of used routing table
|
|
|
|
*/
|
2019-09-24 10:59:00 +00:00
|
|
|
uint32_t routing_table_prio;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-12-20 13:30:10 +00:00
|
|
|
/**
|
|
|
|
* installed routes
|
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
hashlist_t *routes;
|
2011-12-20 13:30:10 +00:00
|
|
|
|
2012-09-20 16:06:01 +00:00
|
|
|
/**
|
|
|
|
* mutex for routes
|
|
|
|
*/
|
|
|
|
mutex_t *routes_lock;
|
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
/**
|
2012-05-07 09:24:07 +00:00
|
|
|
* interface changes which may trigger route reinstallation
|
2011-12-20 13:59:21 +00:00
|
|
|
*/
|
|
|
|
hashtable_t *net_changes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mutex for route reinstallation triggers
|
|
|
|
*/
|
|
|
|
mutex_t *net_changes_lock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* time of last route reinstallation
|
|
|
|
*/
|
|
|
|
timeval_t last_route_reinstall;
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* whether to react to RTM_NEWROUTE or RTM_DELROUTE events
|
|
|
|
*/
|
|
|
|
bool process_route;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2018-01-29 14:26:17 +00:00
|
|
|
/**
|
|
|
|
* whether to react to RTM_NEWRULE or RTM_DELRULE events
|
|
|
|
*/
|
|
|
|
bool process_rules;
|
|
|
|
|
2013-04-16 11:54:25 +00:00
|
|
|
/**
|
|
|
|
* whether to trigger roam events
|
|
|
|
*/
|
|
|
|
bool roam_events;
|
|
|
|
|
2017-01-19 10:03:55 +00:00
|
|
|
/**
|
|
|
|
* whether to install IPsec policy routes
|
|
|
|
*/
|
|
|
|
bool install_routes;
|
|
|
|
|
2009-06-02 11:34:03 +00:00
|
|
|
/**
|
|
|
|
* whether to actually install virtual IPs
|
|
|
|
*/
|
|
|
|
bool install_virtual_ip;
|
2010-03-12 08:37:51 +00:00
|
|
|
|
2012-09-20 09:07:15 +00:00
|
|
|
/**
|
|
|
|
* the name of the interface virtual IP addresses are installed on
|
|
|
|
*/
|
|
|
|
char *install_virtual_ip_on;
|
|
|
|
|
2012-06-18 10:01:10 +00:00
|
|
|
/**
|
|
|
|
* whether preferred source addresses can be specified for IPv6 routes
|
|
|
|
*/
|
|
|
|
bool rta_prefsrc_for_ipv6;
|
|
|
|
|
2015-08-05 14:51:38 +00:00
|
|
|
/**
|
|
|
|
* whether marks can be used in route lookups
|
|
|
|
*/
|
|
|
|
bool rta_mark;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the mark excluded from the routing rule used for virtual IPs
|
|
|
|
*/
|
|
|
|
mark_t routing_mark;
|
|
|
|
|
2014-03-14 14:41:52 +00:00
|
|
|
/**
|
|
|
|
* whether to prefer temporary IPv6 addresses over public ones
|
|
|
|
*/
|
|
|
|
bool prefer_temporary_addrs;
|
|
|
|
|
2010-03-12 08:37:51 +00:00
|
|
|
/**
|
|
|
|
* list with routing tables to be excluded from route lookup
|
|
|
|
*/
|
|
|
|
linked_list_t *rt_exclude;
|
2014-08-04 13:35:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MTU to set on installed routes
|
|
|
|
*/
|
2016-03-22 12:22:01 +00:00
|
|
|
uint32_t mtu;
|
2014-08-04 13:57:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MSS to set on installed routes
|
|
|
|
*/
|
2016-03-22 12:22:01 +00:00
|
|
|
uint32_t mss;
|
2008-09-25 07:56:58 +00:00
|
|
|
};
|
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
/**
|
|
|
|
* Forward declaration
|
|
|
|
*/
|
|
|
|
static status_t manage_srcroute(private_kernel_netlink_net_t *this,
|
|
|
|
int nlmsg_type, int flags, chunk_t dst_net,
|
2016-03-22 12:22:01 +00:00
|
|
|
uint8_t prefixlen, host_t *gateway,
|
2020-02-09 13:52:32 +00:00
|
|
|
host_t *src_ip, char *if_name, bool pass);
|
2011-12-20 13:59:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the queued network changes.
|
|
|
|
*/
|
|
|
|
static void net_changes_clear(private_kernel_netlink_net_t *this)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
net_change_t *change;
|
|
|
|
|
|
|
|
enumerator = this->net_changes->create_enumerator(this->net_changes);
|
|
|
|
while (enumerator->enumerate(enumerator, NULL, (void**)&change))
|
|
|
|
{
|
|
|
|
this->net_changes->remove_at(this->net_changes, enumerator);
|
|
|
|
net_change_destroy(change);
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act upon queued network changes.
|
|
|
|
*/
|
|
|
|
static job_requeue_t reinstall_routes(private_kernel_netlink_net_t *this)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
route_entry_t *route;
|
|
|
|
|
|
|
|
this->net_changes_lock->lock(this->net_changes_lock);
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->lock(this->routes_lock);
|
2011-12-20 13:59:21 +00:00
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
enumerator = this->routes->ht.create_enumerator(&this->routes->ht);
|
2011-12-20 13:59:21 +00:00
|
|
|
while (enumerator->enumerate(enumerator, NULL, (void**)&route))
|
|
|
|
{
|
|
|
|
net_change_t *change, lookup = {
|
|
|
|
.if_name = route->if_name,
|
|
|
|
};
|
2020-02-26 15:53:06 +00:00
|
|
|
if (route->pass || !route->if_name)
|
2020-02-09 13:52:32 +00:00
|
|
|
{ /* no need to reinstall these, they don't reference interfaces */
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-07 09:24:07 +00:00
|
|
|
/* check if a change for the outgoing interface is queued */
|
2011-12-20 13:59:21 +00:00
|
|
|
change = this->net_changes->get(this->net_changes, &lookup);
|
|
|
|
if (!change)
|
2012-05-07 09:24:07 +00:00
|
|
|
{ /* in case src_ip is not on the outgoing interface */
|
2012-09-14 14:27:33 +00:00
|
|
|
if (this->public.interface.get_interface(&this->public.interface,
|
|
|
|
route->src_ip, &lookup.if_name))
|
2012-05-07 09:24:07 +00:00
|
|
|
{
|
2012-09-14 14:27:33 +00:00
|
|
|
if (!streq(lookup.if_name, route->if_name))
|
|
|
|
{
|
|
|
|
change = this->net_changes->get(this->net_changes, &lookup);
|
|
|
|
}
|
|
|
|
free(lookup.if_name);
|
2012-05-07 09:24:07 +00:00
|
|
|
}
|
2011-12-20 13:59:21 +00:00
|
|
|
}
|
|
|
|
if (change)
|
|
|
|
{
|
|
|
|
manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
|
|
|
|
route->dst_net, route->prefixlen, route->gateway,
|
2020-02-09 13:52:32 +00:00
|
|
|
route->src_ip, route->if_name, route->pass);
|
2011-12-20 13:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->unlock(this->routes_lock);
|
2011-12-20 13:59:21 +00:00
|
|
|
|
|
|
|
net_changes_clear(this);
|
|
|
|
this->net_changes_lock->unlock(this->net_changes_lock);
|
|
|
|
return JOB_REQUEUE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue route reinstallation caused by network changes for a given interface.
|
|
|
|
*
|
|
|
|
* The route reinstallation is delayed for a while and only done once for
|
|
|
|
* several calls during this delay, in order to avoid doing it too often.
|
2012-05-07 09:24:07 +00:00
|
|
|
* The interface name is freed.
|
2011-12-20 13:59:21 +00:00
|
|
|
*/
|
|
|
|
static void queue_route_reinstall(private_kernel_netlink_net_t *this,
|
2012-05-07 09:24:07 +00:00
|
|
|
char *if_name)
|
2011-12-20 13:59:21 +00:00
|
|
|
{
|
|
|
|
net_change_t *update, *found;
|
|
|
|
timeval_t now;
|
|
|
|
job_t *job;
|
|
|
|
|
|
|
|
INIT(update,
|
2012-05-07 09:24:07 +00:00
|
|
|
.if_name = if_name
|
2011-12-20 13:59:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this->net_changes_lock->lock(this->net_changes_lock);
|
2012-05-07 09:24:07 +00:00
|
|
|
found = this->net_changes->put(this->net_changes, update, update);
|
2011-12-20 13:59:21 +00:00
|
|
|
if (found)
|
|
|
|
{
|
2012-05-07 09:24:07 +00:00
|
|
|
net_change_destroy(found);
|
2011-12-20 13:59:21 +00:00
|
|
|
}
|
|
|
|
time_monotonic(&now);
|
|
|
|
if (timercmp(&now, &this->last_route_reinstall, >))
|
|
|
|
{
|
2012-10-16 10:38:54 +00:00
|
|
|
timeval_add_ms(&now, ROUTE_DELAY);
|
2011-12-20 13:59:21 +00:00
|
|
|
this->last_route_reinstall = now;
|
|
|
|
|
|
|
|
job = (job_t*)callback_job_create((callback_job_cb_t)reinstall_routes,
|
|
|
|
this, NULL, NULL);
|
|
|
|
lib->scheduler->schedule_job_ms(lib->scheduler, job, ROUTE_DELAY);
|
|
|
|
}
|
|
|
|
this->net_changes_lock->unlock(this->net_changes_lock);
|
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
2012-09-19 17:10:23 +00:00
|
|
|
* check if the given IP is known as virtual IP and currently installed
|
|
|
|
*
|
|
|
|
* this function will also return TRUE if the virtual IP entry disappeared.
|
|
|
|
* in that case the returned entry will be NULL.
|
|
|
|
*
|
2012-09-20 16:21:42 +00:00
|
|
|
* this->lock must be held when calling this function
|
2008-09-25 07:56:58 +00:00
|
|
|
*/
|
2012-09-19 17:10:23 +00:00
|
|
|
static bool is_vip_installed_or_gone(private_kernel_netlink_net_t *this,
|
|
|
|
host_t *ip, addr_map_entry_t **entry)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
addr_map_entry_t lookup = {
|
|
|
|
.ip = ip,
|
|
|
|
};
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
*entry = this->vips->get_match(this->vips, &lookup,
|
|
|
|
(void*)addr_map_entry_match);
|
|
|
|
if (*entry == NULL)
|
|
|
|
{ /* the virtual IP disappeared */
|
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
return (*entry)->addr->installed;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
/**
|
|
|
|
* check if the given IP is known as virtual IP
|
|
|
|
*
|
2012-09-20 16:21:42 +00:00
|
|
|
* this->lock must be held when calling this function
|
2012-09-19 17:10:23 +00:00
|
|
|
*/
|
|
|
|
static bool is_known_vip(private_kernel_netlink_net_t *this, host_t *ip)
|
|
|
|
{
|
|
|
|
addr_map_entry_t lookup = {
|
|
|
|
.ip = ip,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this->vips->get_match(this->vips, &lookup,
|
|
|
|
(void*)addr_map_entry_match) != NULL;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 16:09:51 +00:00
|
|
|
/**
|
|
|
|
* Add an address map entry
|
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
static void addr_map_entry_add(hashlist_t *map, addr_entry_t *addr,
|
2012-09-19 17:10:23 +00:00
|
|
|
iface_entry_t *iface)
|
2012-09-17 16:09:51 +00:00
|
|
|
{
|
|
|
|
addr_map_entry_t *entry;
|
|
|
|
|
|
|
|
INIT(entry,
|
|
|
|
.ip = addr->ip,
|
2012-09-19 17:10:23 +00:00
|
|
|
.addr = addr,
|
2012-09-17 16:09:51 +00:00
|
|
|
.iface = iface,
|
|
|
|
);
|
2020-04-24 11:41:53 +00:00
|
|
|
entry = map->ht.put(&map->ht, entry, entry);
|
2012-09-17 16:09:51 +00:00
|
|
|
free(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-19 17:10:23 +00:00
|
|
|
* Remove an address map entry
|
2012-09-17 16:09:51 +00:00
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
static void addr_map_entry_remove(hashlist_t *map, addr_entry_t *addr,
|
2012-09-19 17:10:23 +00:00
|
|
|
iface_entry_t *iface)
|
2012-09-17 16:09:51 +00:00
|
|
|
{
|
|
|
|
addr_map_entry_t *entry, lookup = {
|
|
|
|
.ip = addr->ip,
|
2012-09-19 17:10:23 +00:00
|
|
|
.addr = addr,
|
2012-09-17 16:09:51 +00:00
|
|
|
.iface = iface,
|
|
|
|
};
|
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
entry = map->ht.remove(&map->ht, &lookup);
|
2012-09-17 16:09:51 +00:00
|
|
|
free(entry);
|
|
|
|
}
|
|
|
|
|
2016-08-23 10:48:37 +00:00
|
|
|
/**
|
|
|
|
* Check if an address or net (addr with prefix net bits) is in
|
|
|
|
* subnet (net with net_len net bits)
|
|
|
|
*/
|
|
|
|
static bool addr_in_subnet(chunk_t addr, int prefix, chunk_t net, int net_len)
|
|
|
|
{
|
|
|
|
static const u_char mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
|
|
|
|
int byte = 0;
|
|
|
|
|
|
|
|
if (net_len == 0)
|
|
|
|
{ /* any address matches a /0 network */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (addr.len != net.len || net_len > 8 * net.len || prefix < net_len)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* scan through all bytes in network order */
|
|
|
|
while (net_len > 0)
|
|
|
|
{
|
|
|
|
if (net_len < 8)
|
|
|
|
{
|
|
|
|
return (mask[net_len] & addr.ptr[byte]) == (mask[net_len] & net.ptr[byte]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (addr.ptr[byte] != net.ptr[byte])
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
byte++;
|
|
|
|
net_len -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given address is in subnet (net with net_len net bits)
|
|
|
|
*/
|
|
|
|
static bool host_in_subnet(host_t *host, chunk_t net, int net_len)
|
|
|
|
{
|
|
|
|
chunk_t addr;
|
|
|
|
|
|
|
|
addr = host->get_address(host);
|
|
|
|
return addr_in_subnet(addr, addr.len * 8, net, net_len);
|
|
|
|
}
|
|
|
|
|
2010-10-05 16:41:06 +00:00
|
|
|
/**
|
2014-03-14 14:41:52 +00:00
|
|
|
* Determine the type or scope of the given unicast IP address. This is not
|
|
|
|
* the same thing returned in rtm_scope/ifa_scope.
|
|
|
|
*
|
|
|
|
* We use return values as defined in RFC 6724 (referring to RFC 4291).
|
|
|
|
*/
|
|
|
|
static u_char get_scope(host_t *ip)
|
|
|
|
{
|
|
|
|
chunk_t addr;
|
|
|
|
|
|
|
|
addr = ip->get_address(ip);
|
|
|
|
switch (addr.len)
|
|
|
|
{
|
|
|
|
case 4:
|
|
|
|
/* we use the mapping defined in RFC 6724, 3.2 */
|
|
|
|
if (addr.ptr[0] == 127)
|
|
|
|
{ /* link-local, same as the IPv6 loopback address */
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (addr.ptr[0] == 169 && addr.ptr[1] == 254)
|
|
|
|
{ /* link-local */
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
2014-06-24 13:43:49 +00:00
|
|
|
if (IN6_IS_ADDR_LOOPBACK((struct in6_addr*)addr.ptr))
|
2014-03-14 14:41:52 +00:00
|
|
|
{ /* link-local, according to RFC 4291, 2.5.3 */
|
|
|
|
return 2;
|
|
|
|
}
|
2014-06-24 13:43:49 +00:00
|
|
|
if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr*)addr.ptr))
|
2014-03-14 14:41:52 +00:00
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
2014-06-24 13:43:49 +00:00
|
|
|
if (IN6_IS_ADDR_SITELOCAL((struct in6_addr*)addr.ptr))
|
2014-03-14 14:41:52 +00:00
|
|
|
{ /* deprecated, according to RFC 4291, 2.5.7 */
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* global */
|
|
|
|
return 14;
|
|
|
|
}
|
|
|
|
|
2016-10-10 08:00:19 +00:00
|
|
|
/**
|
|
|
|
* Determine the label of the given unicast IP address.
|
|
|
|
*
|
|
|
|
* We currently only support the default table given in RFC 6724:
|
|
|
|
*
|
|
|
|
* Prefix Precedence Label
|
|
|
|
* ::1/128 50 0
|
|
|
|
* ::/0 40 1
|
|
|
|
* ::ffff:0:0/96 35 4
|
|
|
|
* 2002::/16 30 2
|
|
|
|
* 2001::/32 5 5
|
|
|
|
* fc00::/7 3 13
|
|
|
|
* ::/96 1 3
|
|
|
|
* fec0::/10 1 11
|
|
|
|
* 3ffe::/16 1 12
|
|
|
|
*/
|
|
|
|
static u_char get_label(host_t *ip)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
chunk_t net;
|
|
|
|
u_char prefix;
|
|
|
|
u_char label;
|
|
|
|
} priorities[] = {
|
|
|
|
/* priority table ordered by prefix */
|
|
|
|
/* ::1/128 */
|
|
|
|
{ chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01), 128, 0 },
|
|
|
|
/* ::ffff:0:0/96 */
|
|
|
|
{ chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00), 96, 4 },
|
|
|
|
/* ::/96 */
|
|
|
|
{ chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 96, 3 },
|
|
|
|
/* 2001::/32 */
|
|
|
|
{ chunk_from_chars(0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 32, 5 },
|
|
|
|
/* 2002::/16 */
|
|
|
|
{ chunk_from_chars(0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 16, 2 },
|
|
|
|
/* 3ffe::/16 */
|
|
|
|
{ chunk_from_chars(0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 16, 12 },
|
|
|
|
/* fec0::/10 */
|
|
|
|
{ chunk_from_chars(0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 10, 11 },
|
|
|
|
/* fc00::/7 */
|
|
|
|
{ chunk_from_chars(0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 7, 13 },
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < countof(priorities); i++)
|
|
|
|
{
|
|
|
|
if (host_in_subnet(ip, priorities[i].net, priorities[i].prefix))
|
|
|
|
{
|
|
|
|
return priorities[i].label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* ::/0 */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-14 14:41:52 +00:00
|
|
|
/**
|
|
|
|
* Returns the length of the common prefix in bits up to the length of a's
|
|
|
|
* prefix, defined by RFC 6724 as the portion of the address not including the
|
|
|
|
* interface ID, which is 64-bit for most unicast addresses (see RFC 4291).
|
|
|
|
*/
|
|
|
|
static u_char common_prefix(host_t *a, host_t *b)
|
|
|
|
{
|
|
|
|
chunk_t aa, ba;
|
|
|
|
u_char byte, bits = 0, match;
|
|
|
|
|
|
|
|
aa = a->get_address(a);
|
|
|
|
ba = b->get_address(b);
|
|
|
|
for (byte = 0; byte < 8; byte++)
|
|
|
|
{
|
|
|
|
if (aa.ptr[byte] != ba.ptr[byte])
|
|
|
|
{
|
|
|
|
match = aa.ptr[byte] ^ ba.ptr[byte];
|
|
|
|
for (bits = 8; match; match >>= 1)
|
|
|
|
{
|
|
|
|
bits--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return byte * 8 + bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two IP addresses and return TRUE if the second address is the better
|
|
|
|
* choice of the two to reach the destination.
|
|
|
|
* For IPv6 we approximately follow RFC 6724.
|
|
|
|
*/
|
|
|
|
static bool is_address_better(private_kernel_netlink_net_t *this,
|
|
|
|
addr_entry_t *a, addr_entry_t *b, host_t *d)
|
|
|
|
{
|
2016-10-10 08:00:19 +00:00
|
|
|
u_char sa, sb, sd, la, lb, ld, pa, pb;
|
2014-03-14 14:41:52 +00:00
|
|
|
|
|
|
|
/* rule 2: prefer appropriate scope */
|
|
|
|
if (d)
|
|
|
|
{
|
|
|
|
sa = get_scope(a->ip);
|
|
|
|
sb = get_scope(b->ip);
|
|
|
|
sd = get_scope(d);
|
|
|
|
if (sa < sb)
|
|
|
|
{
|
|
|
|
return sa < sd;
|
|
|
|
}
|
|
|
|
else if (sb < sa)
|
|
|
|
{
|
|
|
|
return sb >= sd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (a->ip->get_family(a->ip) == AF_INET)
|
|
|
|
{ /* stop here for IPv4, default to addresses found earlier */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* rule 3: avoid deprecated addresses (RFC 4862) */
|
|
|
|
if ((a->flags & IFA_F_DEPRECATED) != (b->flags & IFA_F_DEPRECATED))
|
|
|
|
{
|
|
|
|
return a->flags & IFA_F_DEPRECATED;
|
|
|
|
}
|
|
|
|
/* rule 4 is not applicable as we don't know if an address is a home or
|
|
|
|
* care-of addresses.
|
|
|
|
* rule 5 does not apply as we only compare addresses from one interface
|
|
|
|
*/
|
2016-10-10 08:00:19 +00:00
|
|
|
/* rule 6: prefer matching label */
|
|
|
|
if (d)
|
|
|
|
{
|
|
|
|
la = get_label(a->ip);
|
|
|
|
lb = get_label(b->ip);
|
|
|
|
ld = get_label(d);
|
|
|
|
if (la == ld && lb != ld)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (lb == ld && la != ld)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 14:41:52 +00:00
|
|
|
/* rule 7: prefer temporary addresses (WE REVERSE THIS BY DEFAULT!) */
|
|
|
|
if ((a->flags & IFA_F_TEMPORARY) != (b->flags & IFA_F_TEMPORARY))
|
|
|
|
{
|
|
|
|
if (this->prefer_temporary_addrs)
|
|
|
|
{
|
|
|
|
return b->flags & IFA_F_TEMPORARY;
|
|
|
|
}
|
|
|
|
return a->flags & IFA_F_TEMPORARY;
|
|
|
|
}
|
|
|
|
/* rule 8: use longest matching prefix */
|
|
|
|
if (d)
|
|
|
|
{
|
|
|
|
pa = common_prefix(a->ip, d);
|
|
|
|
pb = common_prefix(b->ip, d);
|
|
|
|
if (pa != pb)
|
|
|
|
{
|
|
|
|
return pb > pa;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* default to addresses found earlier */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-23 10:48:37 +00:00
|
|
|
* Get a non-virtual IP address on the given interfaces and optionally in a
|
|
|
|
* given subnet.
|
2014-03-14 14:41:52 +00:00
|
|
|
*
|
|
|
|
* If a candidate address is given, we first search for that address and if not
|
2011-09-01 09:33:13 +00:00
|
|
|
* found return the address as above.
|
2014-03-14 14:41:52 +00:00
|
|
|
* Returned host is a clone, has to be freed by caller.
|
2012-09-20 16:21:42 +00:00
|
|
|
*
|
2014-03-14 14:41:52 +00:00
|
|
|
* this->lock must be held when calling this function.
|
2010-10-05 16:41:06 +00:00
|
|
|
*/
|
2016-08-23 10:48:37 +00:00
|
|
|
static host_t *get_matching_address(private_kernel_netlink_net_t *this,
|
|
|
|
int *ifindex, int family, chunk_t net,
|
|
|
|
uint8_t mask, host_t *dest,
|
|
|
|
host_t *candidate)
|
2010-10-05 16:41:06 +00:00
|
|
|
{
|
2016-08-23 10:48:37 +00:00
|
|
|
enumerator_t *ifaces, *addrs;
|
2010-10-05 16:41:06 +00:00
|
|
|
iface_entry_t *iface;
|
2014-03-14 14:41:52 +00:00
|
|
|
addr_entry_t *addr, *best = NULL;
|
2016-08-23 10:48:37 +00:00
|
|
|
bool candidate_matched = FALSE;
|
2010-10-05 16:41:06 +00:00
|
|
|
|
2016-08-23 10:48:37 +00:00
|
|
|
ifaces = this->ifaces->create_enumerator(this->ifaces);
|
|
|
|
while (ifaces->enumerate(ifaces, &iface))
|
2010-10-05 16:41:06 +00:00
|
|
|
{
|
2016-08-23 10:48:37 +00:00
|
|
|
if (iface->usable && (!ifindex || iface->ifindex == *ifindex))
|
|
|
|
{ /* only use matching interfaces not excluded by config */
|
2010-10-05 16:41:06 +00:00
|
|
|
addrs = iface->addrs->create_enumerator(iface->addrs);
|
|
|
|
while (addrs->enumerate(addrs, &addr))
|
|
|
|
{
|
2014-03-14 14:41:52 +00:00
|
|
|
if (addr->refcount ||
|
|
|
|
addr->ip->get_family(addr->ip) != family)
|
|
|
|
{ /* ignore virtual IP addresses and ensure family matches */
|
2011-09-01 09:33:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-23 10:48:37 +00:00
|
|
|
if (net.ptr && !host_in_subnet(addr->ip, net, mask))
|
|
|
|
{ /* optionally match a subnet */
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-20 14:20:24 +00:00
|
|
|
if (candidate && candidate->ip_equals(candidate, addr->ip) &&
|
|
|
|
!(addr->flags & IFA_F_DEPRECATED))
|
|
|
|
{ /* stop if we find the candidate and it's not deprecated */
|
2014-03-14 14:41:52 +00:00
|
|
|
best = addr;
|
2016-08-23 10:48:37 +00:00
|
|
|
candidate_matched = TRUE;
|
2014-03-14 14:41:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!best || is_address_better(this, best, addr, dest))
|
2011-09-01 09:33:13 +00:00
|
|
|
{
|
2014-03-14 14:41:52 +00:00
|
|
|
best = addr;
|
2010-10-05 16:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addrs->destroy(addrs);
|
2016-08-23 10:48:37 +00:00
|
|
|
if (ifindex || candidate_matched)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-10-05 16:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-23 10:48:37 +00:00
|
|
|
ifaces->destroy(ifaces);
|
2014-03-14 14:41:52 +00:00
|
|
|
return best ? best->ip->clone(best->ip) : NULL;
|
2010-10-05 16:41:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 10:48:37 +00:00
|
|
|
/**
|
|
|
|
* Get a non-virtual IP address on the given interface.
|
|
|
|
*
|
|
|
|
* If a candidate address is given, we first search for that address and if not
|
|
|
|
* found return the address as above.
|
|
|
|
* Returned host is a clone, has to be freed by caller.
|
|
|
|
*
|
|
|
|
* this->lock must be held when calling this function.
|
|
|
|
*/
|
|
|
|
static host_t *get_interface_address(private_kernel_netlink_net_t *this,
|
|
|
|
int ifindex, int family, host_t *dest,
|
|
|
|
host_t *candidate)
|
|
|
|
{
|
|
|
|
return get_matching_address(this, &ifindex, family, chunk_empty, 0, dest,
|
|
|
|
candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a non-virtual IP address in the given subnet.
|
|
|
|
*
|
|
|
|
* If a candidate address is given, we first search for that address and if not
|
|
|
|
* found return the address as above.
|
|
|
|
* Returned host is a clone, has to be freed by caller.
|
|
|
|
*
|
|
|
|
* this->lock must be held when calling this function.
|
|
|
|
*/
|
|
|
|
static host_t *get_subnet_address(private_kernel_netlink_net_t *this,
|
|
|
|
int family, chunk_t net, uint8_t mask,
|
|
|
|
host_t *dest, host_t *candidate)
|
|
|
|
{
|
|
|
|
return get_matching_address(this, NULL, family, net, mask, dest, candidate);
|
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
2010-07-06 14:03:09 +00:00
|
|
|
* callback function that raises the delayed roam event
|
2008-09-25 07:56:58 +00:00
|
|
|
*/
|
2013-08-12 09:23:34 +00:00
|
|
|
static job_requeue_t roam_event(private_kernel_netlink_net_t *this)
|
2010-07-06 14:03:09 +00:00
|
|
|
{
|
2013-08-12 09:23:34 +00:00
|
|
|
bool address;
|
|
|
|
|
|
|
|
this->roam_lock->lock(this->roam_lock);
|
|
|
|
address = this->roam_address;
|
|
|
|
this->roam_address = FALSE;
|
|
|
|
this->roam_lock->unlock(this->roam_lock);
|
2016-02-12 14:30:18 +00:00
|
|
|
charon->kernel->roam(charon->kernel, address);
|
2010-07-06 14:03:09 +00:00
|
|
|
return JOB_REQUEUE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fire a roaming event. we delay it for a bit and fire only one event
|
|
|
|
* for multiple calls. otherwise we would create too many events.
|
|
|
|
*/
|
|
|
|
static void fire_roam_event(private_kernel_netlink_net_t *this, bool address)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2009-08-31 13:25:03 +00:00
|
|
|
timeval_t now;
|
2010-07-06 14:03:09 +00:00
|
|
|
job_t *job;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2013-04-16 11:54:25 +00:00
|
|
|
if (!this->roam_events)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-31 13:25:03 +00:00
|
|
|
time_monotonic(&now);
|
2012-09-20 09:58:52 +00:00
|
|
|
this->roam_lock->lock(this->roam_lock);
|
2013-08-12 10:06:25 +00:00
|
|
|
this->roam_address |= address;
|
2012-09-20 09:58:52 +00:00
|
|
|
if (!timercmp(&now, &this->next_roam, >))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-20 09:58:52 +00:00
|
|
|
this->roam_lock->unlock(this->roam_lock);
|
|
|
|
return;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-10-16 10:38:54 +00:00
|
|
|
timeval_add_ms(&now, ROAM_DELAY);
|
2012-09-20 09:58:52 +00:00
|
|
|
this->next_roam = now;
|
|
|
|
this->roam_lock->unlock(this->roam_lock);
|
|
|
|
|
|
|
|
job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
|
2013-08-12 09:23:34 +00:00
|
|
|
this, NULL, NULL);
|
2012-09-20 09:58:52 +00:00
|
|
|
lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 14:43:08 +00:00
|
|
|
/**
|
|
|
|
* check if an interface with a given index is up and usable
|
2012-09-19 17:10:23 +00:00
|
|
|
*
|
2012-09-20 16:21:42 +00:00
|
|
|
* this->lock must be locked when calling this function
|
2012-09-14 14:43:08 +00:00
|
|
|
*/
|
|
|
|
static bool is_interface_up_and_usable(private_kernel_netlink_net_t *this,
|
|
|
|
int index)
|
|
|
|
{
|
|
|
|
iface_entry_t *iface;
|
|
|
|
|
2017-05-16 10:11:24 +00:00
|
|
|
if (this->ifaces->find_first(this->ifaces, iface_entry_by_index,
|
|
|
|
(void**)&iface, index))
|
2012-09-14 14:43:08 +00:00
|
|
|
{
|
|
|
|
return iface_entry_up_and_usable(iface);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
/**
|
|
|
|
* unregister the current addr_entry_t from the hashtable it is stored in
|
|
|
|
*
|
2012-09-20 16:21:42 +00:00
|
|
|
* this->lock must be locked when calling this function
|
2012-09-19 17:10:23 +00:00
|
|
|
*/
|
2017-05-15 15:51:19 +00:00
|
|
|
CALLBACK(addr_entry_unregister, void,
|
|
|
|
addr_entry_t *addr, va_list args)
|
2012-09-19 17:10:23 +00:00
|
|
|
{
|
2017-05-15 15:51:19 +00:00
|
|
|
private_kernel_netlink_net_t *this;
|
|
|
|
iface_entry_t *iface;
|
|
|
|
|
|
|
|
VA_ARGS_VGET(args, iface, this);
|
2012-09-19 17:10:23 +00:00
|
|
|
if (addr->refcount)
|
|
|
|
{
|
|
|
|
addr_map_entry_remove(this->vips, addr, iface);
|
|
|
|
this->condvar->broadcast(this->condvar);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addr_map_entry_remove(this->addrs, addr, iface);
|
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* process RTM_NEWLINK/RTM_DELLINK from kernel
|
|
|
|
*/
|
|
|
|
static void process_link(private_kernel_netlink_net_t *this,
|
|
|
|
struct nlmsghdr *hdr, bool event)
|
|
|
|
{
|
2014-07-09 13:53:14 +00:00
|
|
|
struct ifinfomsg* msg = NLMSG_DATA(hdr);
|
2008-09-25 07:56:58 +00:00
|
|
|
struct rtattr *rta = IFLA_RTA(msg);
|
|
|
|
size_t rtasize = IFLA_PAYLOAD (hdr);
|
2008-11-05 08:32:38 +00:00
|
|
|
enumerator_t *enumerator;
|
2008-09-25 07:56:58 +00:00
|
|
|
iface_entry_t *current, *entry = NULL;
|
|
|
|
char *name = NULL;
|
2011-12-20 13:59:21 +00:00
|
|
|
bool update = FALSE, update_routes = FALSE;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
while (RTA_OK(rta, rtasize))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
|
|
|
switch (rta->rta_type)
|
|
|
|
{
|
|
|
|
case IFLA_IFNAME:
|
|
|
|
name = RTA_DATA(rta);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rta = RTA_NEXT(rta, rtasize);
|
|
|
|
}
|
|
|
|
if (!name)
|
|
|
|
{
|
|
|
|
name = "(unknown)";
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
switch (hdr->nlmsg_type)
|
|
|
|
{
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
{
|
2017-05-16 10:11:24 +00:00
|
|
|
if (!this->ifaces->find_first(this->ifaces, iface_entry_by_index,
|
|
|
|
(void**)&entry, msg->ifi_index))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
INIT(entry,
|
|
|
|
.ifindex = msg->ifi_index,
|
|
|
|
.addrs = linked_list_create(),
|
|
|
|
);
|
2008-09-25 07:56:58 +00:00
|
|
|
this->ifaces->insert_last(this->ifaces, entry);
|
|
|
|
}
|
2011-04-18 14:10:36 +00:00
|
|
|
strncpy(entry->ifname, name, IFNAMSIZ);
|
2008-09-25 07:56:58 +00:00
|
|
|
entry->ifname[IFNAMSIZ-1] = '\0';
|
2017-08-14 15:26:08 +00:00
|
|
|
entry->usable = charon->kernel->is_interface_usable(charon->kernel,
|
|
|
|
name);
|
2012-09-14 14:43:08 +00:00
|
|
|
if (event && entry->usable)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
|
|
|
if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
|
|
|
|
{
|
2011-12-20 13:59:21 +00:00
|
|
|
update = update_routes = TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
DBG1(DBG_KNL, "interface %s activated", name);
|
|
|
|
}
|
|
|
|
if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
|
|
|
|
{
|
|
|
|
update = TRUE;
|
|
|
|
DBG1(DBG_KNL, "interface %s deactivated", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entry->flags = msg->ifi_flags;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RTM_DELLINK:
|
|
|
|
{
|
2008-11-05 08:32:38 +00:00
|
|
|
enumerator = this->ifaces->create_enumerator(this->ifaces);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
|
|
|
if (current->ifindex == msg->ifi_index)
|
|
|
|
{
|
2012-09-14 14:43:08 +00:00
|
|
|
if (event && current->usable)
|
2011-11-14 14:24:48 +00:00
|
|
|
{
|
|
|
|
update = TRUE;
|
|
|
|
DBG1(DBG_KNL, "interface %s deleted", current->ifname);
|
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
/* TODO: move virtual IPs installed on this interface to
|
|
|
|
* another interface? */
|
2011-11-14 14:24:48 +00:00
|
|
|
this->ifaces->remove_at(this->ifaces, enumerator);
|
2012-09-17 16:09:51 +00:00
|
|
|
current->addrs->invoke_function(current->addrs,
|
2017-05-15 15:51:19 +00:00
|
|
|
addr_entry_unregister, current, this);
|
2011-11-14 14:24:48 +00:00
|
|
|
iface_entry_destroy(current);
|
2008-09-25 07:56:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-05 08:32:38 +00:00
|
|
|
enumerator->destroy(enumerator);
|
2008-09-25 07:56:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
if (update_routes && event)
|
|
|
|
{
|
2012-05-07 09:24:07 +00:00
|
|
|
queue_route_reinstall(this, strdup(name));
|
2011-12-20 13:59:21 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
if (update && event)
|
|
|
|
{
|
2010-07-06 14:03:09 +00:00
|
|
|
fire_roam_event(this, TRUE);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process RTM_NEWADDR/RTM_DELADDR from kernel
|
|
|
|
*/
|
|
|
|
static void process_addr(private_kernel_netlink_net_t *this,
|
|
|
|
struct nlmsghdr *hdr, bool event)
|
|
|
|
{
|
2014-07-09 13:53:14 +00:00
|
|
|
struct ifaddrmsg* msg = NLMSG_DATA(hdr);
|
2008-09-25 07:56:58 +00:00
|
|
|
struct rtattr *rta = IFA_RTA(msg);
|
|
|
|
size_t rtasize = IFA_PAYLOAD (hdr);
|
|
|
|
host_t *host = NULL;
|
|
|
|
iface_entry_t *iface;
|
|
|
|
chunk_t local = chunk_empty, address = chunk_empty;
|
2011-12-20 13:59:21 +00:00
|
|
|
char *route_ifname = NULL;
|
2008-09-25 07:56:58 +00:00
|
|
|
bool update = FALSE, found = FALSE, changed = FALSE;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
while (RTA_OK(rta, rtasize))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
|
|
|
switch (rta->rta_type)
|
|
|
|
{
|
|
|
|
case IFA_LOCAL:
|
|
|
|
local.ptr = RTA_DATA(rta);
|
|
|
|
local.len = RTA_PAYLOAD(rta);
|
|
|
|
break;
|
|
|
|
case IFA_ADDRESS:
|
|
|
|
address.ptr = RTA_DATA(rta);
|
|
|
|
address.len = RTA_PAYLOAD(rta);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rta = RTA_NEXT(rta, rtasize);
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/* For PPP interfaces, we need the IFA_LOCAL address,
|
|
|
|
* IFA_ADDRESS is the peers address. But IFA_LOCAL is
|
|
|
|
* not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
|
|
|
|
if (local.ptr)
|
|
|
|
{
|
|
|
|
host = host_create_from_chunk(msg->ifa_family, local, 0);
|
|
|
|
}
|
|
|
|
else if (address.ptr)
|
|
|
|
{
|
|
|
|
host = host_create_from_chunk(msg->ifa_family, address, 0);
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
if (host == NULL)
|
|
|
|
{ /* bad family? */
|
|
|
|
return;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2017-05-16 10:11:24 +00:00
|
|
|
if (this->ifaces->find_first(this->ifaces, iface_entry_by_index,
|
|
|
|
(void**)&iface, msg->ifa_index))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
addr_map_entry_t *entry, lookup = {
|
|
|
|
.ip = host,
|
|
|
|
.iface = iface,
|
|
|
|
};
|
|
|
|
addr_entry_t *addr;
|
|
|
|
|
2020-04-24 11:41:53 +00:00
|
|
|
entry = this->vips->ht.get(&this->vips->ht, &lookup);
|
2012-09-19 17:10:23 +00:00
|
|
|
if (entry)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
if (hdr->nlmsg_type == RTM_NEWADDR)
|
|
|
|
{ /* mark as installed and signal waiting threads */
|
|
|
|
entry->addr->installed = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* the address was already marked as uninstalled */
|
|
|
|
addr = entry->addr;
|
|
|
|
iface->addrs->remove(iface->addrs, addr, NULL);
|
|
|
|
addr_map_entry_remove(this->vips, addr, iface);
|
|
|
|
addr_entry_destroy(addr);
|
|
|
|
}
|
|
|
|
/* no roam events etc. for virtual IPs */
|
|
|
|
this->condvar->broadcast(this->condvar);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
host->destroy(host);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 11:41:53 +00:00
|
|
|
entry = this->addrs->ht.get(&this->addrs->ht, &lookup);
|
2012-09-19 17:10:23 +00:00
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (hdr->nlmsg_type == RTM_DELADDR)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
found = TRUE;
|
|
|
|
addr = entry->addr;
|
|
|
|
iface->addrs->remove(iface->addrs, addr, NULL);
|
|
|
|
if (iface->usable)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
changed = TRUE;
|
|
|
|
DBG1(DBG_KNL, "%H disappeared from %s", host,
|
|
|
|
iface->ifname);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
addr_map_entry_remove(this->addrs, addr, iface);
|
|
|
|
addr_entry_destroy(addr);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2020-10-15 14:10:07 +00:00
|
|
|
else if (entry->addr->flags != msg->ifa_flags)
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
entry->addr->flags = msg->ifa_flags;
|
|
|
|
if (event && iface->usable)
|
|
|
|
{
|
|
|
|
changed = TRUE;
|
|
|
|
DBG1(DBG_KNL, "flags changed for %H on %s", host,
|
|
|
|
iface->ifname);
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-25 07:56:58 +00:00
|
|
|
if (hdr->nlmsg_type == RTM_NEWADDR)
|
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
found = TRUE;
|
|
|
|
changed = TRUE;
|
|
|
|
route_ifname = strdup(iface->ifname);
|
|
|
|
INIT(addr,
|
|
|
|
.ip = host->clone(host),
|
2014-03-14 14:41:52 +00:00
|
|
|
.flags = msg->ifa_flags,
|
2012-09-19 17:10:23 +00:00
|
|
|
.scope = msg->ifa_scope,
|
|
|
|
);
|
|
|
|
iface->addrs->insert_last(iface->addrs, addr);
|
|
|
|
addr_map_entry_add(this->addrs, addr, iface);
|
|
|
|
if (event && iface->usable)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
}
|
|
|
|
if (found && (iface->flags & IFF_UP))
|
|
|
|
{
|
|
|
|
update = TRUE;
|
|
|
|
}
|
|
|
|
if (!iface->usable)
|
|
|
|
{ /* ignore events for interfaces excluded by config */
|
|
|
|
update = changed = FALSE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2011-12-20 13:59:21 +00:00
|
|
|
|
|
|
|
if (update && event && route_ifname)
|
|
|
|
{
|
2012-05-07 09:24:07 +00:00
|
|
|
queue_route_reinstall(this, route_ifname);
|
2011-12-20 13:59:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(route_ifname);
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
host->destroy(host);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/* send an update to all IKE_SAs */
|
|
|
|
if (update && event && changed)
|
|
|
|
{
|
2010-07-06 14:03:09 +00:00
|
|
|
fire_roam_event(this, TRUE);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process RTM_NEWROUTE and RTM_DELROUTE from kernel
|
|
|
|
*/
|
2019-09-24 10:59:00 +00:00
|
|
|
static void process_route(private_kernel_netlink_net_t *this,
|
|
|
|
struct nlmsghdr *hdr)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2014-07-09 13:53:14 +00:00
|
|
|
struct rtmsg* msg = NLMSG_DATA(hdr);
|
2008-09-25 07:56:58 +00:00
|
|
|
struct rtattr *rta = RTM_RTA(msg);
|
|
|
|
size_t rtasize = RTM_PAYLOAD(hdr);
|
2016-03-22 12:22:01 +00:00
|
|
|
uint32_t rta_oif = 0;
|
2008-09-25 07:56:58 +00:00
|
|
|
host_t *host = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-10-05 14:18:35 +00:00
|
|
|
/* ignore routes added by us or in the local routing table (local addrs) */
|
|
|
|
if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
|
|
|
|
msg->rtm_table == RT_TABLE_LOCAL))
|
2008-10-08 08:29:49 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-06-15 10:27:26 +00:00
|
|
|
else if (msg->rtm_flags & RTM_F_CLONED)
|
|
|
|
{ /* ignore cached routes, seem to be created a lot for IPv6 */
|
|
|
|
return;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
while (RTA_OK(rta, rtasize))
|
|
|
|
{
|
|
|
|
switch (rta->rta_type)
|
|
|
|
{
|
2019-09-24 10:59:00 +00:00
|
|
|
#ifdef HAVE_RTA_TABLE
|
|
|
|
case RTA_TABLE:
|
|
|
|
/* also check against extended table ID */
|
|
|
|
if (RTA_PAYLOAD(rta) == sizeof(uint32_t) &&
|
|
|
|
this->routing_table == *(uint32_t*)RTA_DATA(rta))
|
|
|
|
{
|
2021-06-25 11:51:44 +00:00
|
|
|
DESTROY_IF(host);
|
2019-09-24 10:59:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_RTA_TABLE */
|
2008-09-25 07:56:58 +00:00
|
|
|
case RTA_PREFSRC:
|
2011-04-14 13:14:55 +00:00
|
|
|
DESTROY_IF(host);
|
2008-09-25 07:56:58 +00:00
|
|
|
host = host_create_from_chunk(msg->rtm_family,
|
|
|
|
chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)), 0);
|
|
|
|
break;
|
2010-10-05 16:41:06 +00:00
|
|
|
case RTA_OIF:
|
|
|
|
if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
|
|
|
|
{
|
2016-03-22 12:22:01 +00:00
|
|
|
rta_oif = *(uint32_t*)RTA_DATA(rta);
|
2010-10-05 16:41:06 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
rta = RTA_NEXT(rta, rtasize);
|
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2012-09-14 14:43:08 +00:00
|
|
|
if (rta_oif && !is_interface_up_and_usable(this, rta_oif))
|
|
|
|
{ /* ignore route changes for interfaces that are ignored or down */
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-14 14:43:08 +00:00
|
|
|
DESTROY_IF(host);
|
|
|
|
return;
|
|
|
|
}
|
2010-10-05 16:41:06 +00:00
|
|
|
if (!host && rta_oif)
|
|
|
|
{
|
2014-03-14 14:41:52 +00:00
|
|
|
host = get_interface_address(this, rta_oif, msg->rtm_family,
|
|
|
|
NULL, NULL);
|
2010-10-05 16:41:06 +00:00
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
if (!host || is_known_vip(this, host))
|
|
|
|
{ /* ignore routes added for virtual IPs */
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
DESTROY_IF(host);
|
|
|
|
return;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
fire_roam_event(this, FALSE);
|
|
|
|
host->destroy(host);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 14:26:17 +00:00
|
|
|
/**
|
|
|
|
* process RTM_NEW|DELRULE from kernel
|
|
|
|
*/
|
2019-09-24 10:59:00 +00:00
|
|
|
static void process_rule(private_kernel_netlink_net_t *this,
|
|
|
|
struct nlmsghdr *hdr)
|
2018-01-29 14:26:17 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_LINUX_FIB_RULES_H
|
|
|
|
struct rtmsg* msg = NLMSG_DATA(hdr);
|
|
|
|
struct rtattr *rta = RTM_RTA(msg);
|
|
|
|
size_t rtasize = RTM_PAYLOAD(hdr);
|
|
|
|
|
|
|
|
/* ignore rules added by us or in the local routing table (local addrs) */
|
|
|
|
if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
|
|
|
|
msg->rtm_table == RT_TABLE_LOCAL))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (RTA_OK(rta, rtasize))
|
|
|
|
{
|
|
|
|
switch (rta->rta_type)
|
|
|
|
{
|
|
|
|
case FRA_TABLE:
|
2019-09-24 10:59:00 +00:00
|
|
|
/* also check against extended table ID */
|
|
|
|
if (RTA_PAYLOAD(rta) == sizeof(uint32_t) &&
|
|
|
|
this->routing_table == *(uint32_t*)RTA_DATA(rta))
|
2018-01-29 14:26:17 +00:00
|
|
|
{
|
2019-09-24 10:59:00 +00:00
|
|
|
return;
|
2018-01-29 14:26:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rta = RTA_NEXT(rta, rtasize);
|
|
|
|
}
|
|
|
|
fire_roam_event(this, FALSE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* Receives events from kernel
|
|
|
|
*/
|
2013-07-01 13:42:22 +00:00
|
|
|
static bool receive_events(private_kernel_netlink_net_t *this, int fd,
|
|
|
|
watcher_event_t event)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2018-08-10 12:41:16 +00:00
|
|
|
char response[netlink_get_buflen()];
|
2008-09-25 07:56:58 +00:00
|
|
|
struct nlmsghdr *hdr = (struct nlmsghdr*)response;
|
|
|
|
struct sockaddr_nl addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
2009-12-17 14:58:12 +00:00
|
|
|
int len;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2013-07-01 13:42:22 +00:00
|
|
|
len = recvfrom(this->socket_events, response, sizeof(response),
|
|
|
|
MSG_DONTWAIT, (struct sockaddr*)&addr, &addr_len);
|
2008-09-25 07:56:58 +00:00
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINTR:
|
|
|
|
/* interrupted, try again */
|
2013-07-01 13:42:22 +00:00
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
case EAGAIN:
|
|
|
|
/* no data ready, select again */
|
2013-07-01 13:42:22 +00:00
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
default:
|
2016-05-27 11:43:41 +00:00
|
|
|
DBG1(DBG_KNL, "unable to receive from RT event socket %s (%d)",
|
|
|
|
strerror(errno), errno);
|
2008-09-25 07:56:58 +00:00
|
|
|
sleep(1);
|
2013-07-01 13:42:22 +00:00
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
if (addr.nl_pid != 0)
|
|
|
|
{ /* not from kernel. not interested, try another one */
|
2013-07-01 13:42:22 +00:00
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
while (NLMSG_OK(hdr, len))
|
|
|
|
{
|
|
|
|
/* looks good so far, dispatch netlink message */
|
|
|
|
switch (hdr->nlmsg_type)
|
|
|
|
{
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
case RTM_DELADDR:
|
|
|
|
process_addr(this, hdr, TRUE);
|
|
|
|
break;
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
case RTM_DELLINK:
|
|
|
|
process_link(this, hdr, TRUE);
|
|
|
|
break;
|
|
|
|
case RTM_NEWROUTE:
|
|
|
|
case RTM_DELROUTE:
|
|
|
|
if (this->process_route)
|
|
|
|
{
|
|
|
|
process_route(this, hdr);
|
|
|
|
}
|
|
|
|
break;
|
2018-01-29 14:26:17 +00:00
|
|
|
case RTM_NEWRULE:
|
|
|
|
case RTM_DELRULE:
|
|
|
|
if (this->process_rules)
|
|
|
|
{
|
|
|
|
process_rule(this, hdr);
|
|
|
|
}
|
|
|
|
break;
|
2008-09-25 07:56:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hdr = NLMSG_NEXT(hdr, len);
|
|
|
|
}
|
2013-07-01 13:42:22 +00:00
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** enumerator over addresses */
|
|
|
|
typedef struct {
|
|
|
|
private_kernel_netlink_net_t* this;
|
2012-09-17 17:04:51 +00:00
|
|
|
/** which addresses to enumerate */
|
|
|
|
kernel_address_type_t which;
|
2008-09-25 07:56:58 +00:00
|
|
|
} address_enumerator_t;
|
|
|
|
|
2017-05-12 10:10:27 +00:00
|
|
|
CALLBACK(address_enumerator_destroy, void,
|
|
|
|
address_enumerator_t *data)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-20 16:21:42 +00:00
|
|
|
data->this->lock->unlock(data->this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
2017-05-12 10:10:27 +00:00
|
|
|
CALLBACK(filter_addresses, bool,
|
|
|
|
address_enumerator_t *data, enumerator_t *orig, va_list args)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2017-05-12 10:10:27 +00:00
|
|
|
addr_entry_t *addr;
|
|
|
|
host_t **out;
|
|
|
|
|
|
|
|
VA_ARGS_VGET(args, out);
|
|
|
|
|
|
|
|
while (orig->enumerate(orig, &addr))
|
|
|
|
{
|
|
|
|
if (!(data->which & ADDR_TYPE_VIRTUAL) && addr->refcount)
|
|
|
|
{ /* skip virtual interfaces added by us */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(data->which & ADDR_TYPE_REGULAR) && !addr->refcount)
|
|
|
|
{ /* address is regular, but not requested */
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-04 08:39:26 +00:00
|
|
|
if (addr->flags & IFA_F_DEPRECATED ||
|
|
|
|
addr->scope >= RT_SCOPE_LINK)
|
|
|
|
{ /* skip deprecated addresses or those with an unusable scope */
|
2017-05-12 10:10:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-07 08:01:46 +00:00
|
|
|
if (!addr->refcount && addr->ip->get_family(addr->ip) == AF_INET6)
|
|
|
|
{ /* handle non-VIP temporary IPv6 addresses according to config */
|
2019-10-04 13:34:04 +00:00
|
|
|
bool temporary = (addr->flags & IFA_F_TEMPORARY) == IFA_F_TEMPORARY;
|
|
|
|
if (data->this->prefer_temporary_addrs != temporary)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 10:10:27 +00:00
|
|
|
*out = addr->ip;
|
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2017-05-12 10:10:27 +00:00
|
|
|
return FALSE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* enumerator constructor for interfaces
|
|
|
|
*/
|
2011-02-17 09:31:15 +00:00
|
|
|
static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
|
|
|
|
address_enumerator_t *data)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2011-02-17 09:31:15 +00:00
|
|
|
return enumerator_create_filter(
|
2017-05-12 10:10:27 +00:00
|
|
|
iface->addrs->create_enumerator(iface->addrs),
|
|
|
|
filter_addresses, data, NULL);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 10:10:27 +00:00
|
|
|
CALLBACK(filter_interfaces, bool,
|
|
|
|
address_enumerator_t *data, enumerator_t *orig, va_list args)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2017-05-12 10:10:27 +00:00
|
|
|
iface_entry_t *iface, **out;
|
|
|
|
|
|
|
|
VA_ARGS_VGET(args, out);
|
|
|
|
|
|
|
|
while (orig->enumerate(orig, &iface))
|
|
|
|
{
|
|
|
|
if (!(data->which & ADDR_TYPE_IGNORED) && !iface->usable)
|
|
|
|
{ /* skip interfaces excluded by config */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(data->which & ADDR_TYPE_LOOPBACK) && (iface->flags & IFF_LOOPBACK))
|
|
|
|
{ /* ignore loopback devices */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(data->which & ADDR_TYPE_DOWN) && !(iface->flags & IFF_UP))
|
|
|
|
{ /* skip interfaces not up */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*out = iface;
|
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2017-05-12 10:10:27 +00:00
|
|
|
return FALSE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
|
2012-09-17 17:04:51 +00:00
|
|
|
private_kernel_netlink_net_t *this, kernel_address_type_t which)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2013-04-19 12:52:29 +00:00
|
|
|
address_enumerator_t *data;
|
|
|
|
|
|
|
|
INIT(data,
|
|
|
|
.this = this,
|
|
|
|
.which = which,
|
|
|
|
);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
return enumerator_create_nested(
|
2011-02-17 09:31:15 +00:00
|
|
|
enumerator_create_filter(
|
|
|
|
this->ifaces->create_enumerator(this->ifaces),
|
2017-05-12 10:10:27 +00:00
|
|
|
filter_interfaces, data, NULL),
|
2011-02-17 09:31:15 +00:00
|
|
|
(void*)create_iface_enumerator, data,
|
2017-05-12 10:10:27 +00:00
|
|
|
address_enumerator_destroy);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 14:27:33 +00:00
|
|
|
METHOD(kernel_net_t, get_interface_name, bool,
|
|
|
|
private_kernel_netlink_net_t *this, host_t* ip, char **name)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-17 16:09:51 +00:00
|
|
|
addr_map_entry_t *entry, lookup = {
|
|
|
|
.ip = ip,
|
|
|
|
};
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-14 14:30:06 +00:00
|
|
|
if (ip->is_anyaddr(ip))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2012-09-17 16:09:51 +00:00
|
|
|
/* first try to find it on an up and usable interface */
|
|
|
|
entry = this->addrs->get_match(this->addrs, &lookup,
|
|
|
|
(void*)addr_map_entry_match_up_and_usable);
|
|
|
|
if (entry)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-17 16:09:51 +00:00
|
|
|
if (name)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-17 16:09:51 +00:00
|
|
|
*name = strdup(entry->iface->ifname);
|
|
|
|
DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-17 16:09:51 +00:00
|
|
|
return TRUE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-12-17 13:23:44 +00:00
|
|
|
/* in a second step, consider virtual IPs installed by us */
|
|
|
|
entry = this->vips->get_match(this->vips, &lookup,
|
|
|
|
(void*)addr_map_entry_match_up_and_usable);
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
*name = strdup(entry->iface->ifname);
|
2013-07-10 13:37:35 +00:00
|
|
|
DBG2(DBG_KNL, "virtual IP %H is on interface %s", ip, *name);
|
2012-12-17 13:23:44 +00:00
|
|
|
}
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-17 16:09:51 +00:00
|
|
|
/* maybe it is installed on an ignored interface */
|
|
|
|
entry = this->addrs->get_match(this->addrs, &lookup,
|
|
|
|
(void*)addr_map_entry_match_up);
|
|
|
|
if (!entry)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-17 16:09:51 +00:00
|
|
|
DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-17 16:09:51 +00:00
|
|
|
return FALSE;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the index of an interface by name
|
|
|
|
*/
|
|
|
|
static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
|
|
|
|
{
|
|
|
|
iface_entry_t *iface;
|
|
|
|
int ifindex = 0;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
DBG2(DBG_KNL, "getting iface index for %s", name);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2017-05-16 10:11:24 +00:00
|
|
|
if (this->ifaces->find_first(this->ifaces, iface_entry_by_name,
|
|
|
|
(void**)&iface, name))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
ifindex = iface->ifindex;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
|
|
|
|
if (ifindex == 0)
|
|
|
|
{
|
|
|
|
DBG1(DBG_KNL, "unable to get interface index for %s", name);
|
|
|
|
}
|
|
|
|
return ifindex;
|
|
|
|
}
|
|
|
|
|
2016-03-11 18:07:10 +00:00
|
|
|
/**
|
|
|
|
* get the name of an interface by index (allocated)
|
|
|
|
*/
|
|
|
|
static char *get_interface_name_by_index(private_kernel_netlink_net_t *this,
|
|
|
|
int index)
|
|
|
|
{
|
|
|
|
iface_entry_t *iface;
|
|
|
|
char *name = NULL;
|
|
|
|
|
|
|
|
DBG2(DBG_KNL, "getting iface name for index %d", index);
|
|
|
|
|
|
|
|
this->lock->read_lock(this->lock);
|
2017-05-16 10:11:24 +00:00
|
|
|
if (this->ifaces->find_first(this->ifaces, iface_entry_by_index,
|
|
|
|
(void**)&iface, index))
|
2016-03-11 18:07:10 +00:00
|
|
|
{
|
|
|
|
name = strdup(iface->ifname);
|
|
|
|
}
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
{
|
|
|
|
DBG1(DBG_KNL, "unable to get interface name for %d", index);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2011-10-12 13:52:18 +00:00
|
|
|
/**
|
|
|
|
* Store information about a route retrieved via RTNETLINK
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
chunk_t gtw;
|
2016-08-23 10:48:37 +00:00
|
|
|
chunk_t pref_src;
|
2011-10-12 13:52:18 +00:00
|
|
|
chunk_t dst;
|
2016-08-23 10:48:37 +00:00
|
|
|
chunk_t src;
|
2011-10-12 13:52:18 +00:00
|
|
|
host_t *src_host;
|
2016-03-22 12:22:01 +00:00
|
|
|
uint8_t dst_len;
|
2016-08-23 10:48:37 +00:00
|
|
|
uint8_t src_len;
|
2016-03-22 12:22:01 +00:00
|
|
|
uint32_t table;
|
|
|
|
uint32_t oif;
|
|
|
|
uint32_t priority;
|
2011-10-12 13:52:18 +00:00
|
|
|
} rt_entry_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free a route entry
|
|
|
|
*/
|
|
|
|
static void rt_entry_destroy(rt_entry_t *this)
|
|
|
|
{
|
|
|
|
DESTROY_IF(this->src_host);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2015-04-09 12:43:07 +00:00
|
|
|
/**
|
|
|
|
* Check if the route received with RTM_NEWROUTE is usable based on its type.
|
|
|
|
*/
|
2018-02-26 14:07:15 +00:00
|
|
|
static bool route_usable(struct nlmsghdr *hdr, bool allow_local)
|
2015-04-09 12:43:07 +00:00
|
|
|
{
|
|
|
|
struct rtmsg *msg;
|
|
|
|
|
|
|
|
msg = NLMSG_DATA(hdr);
|
|
|
|
switch (msg->rtm_type)
|
|
|
|
{
|
|
|
|
case RTN_BLACKHOLE:
|
|
|
|
case RTN_UNREACHABLE:
|
|
|
|
case RTN_PROHIBIT:
|
|
|
|
case RTN_THROW:
|
|
|
|
return FALSE;
|
2018-02-26 14:07:15 +00:00
|
|
|
case RTN_LOCAL:
|
|
|
|
return allow_local;
|
2015-04-09 12:43:07 +00:00
|
|
|
default:
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-12 13:52:18 +00:00
|
|
|
/**
|
|
|
|
* Parse route received with RTM_NEWROUTE. The given rt_entry_t object will be
|
|
|
|
* reused if not NULL.
|
|
|
|
*
|
|
|
|
* Returned chunks point to internal data of the Netlink message.
|
|
|
|
*/
|
|
|
|
static rt_entry_t *parse_route(struct nlmsghdr *hdr, rt_entry_t *route)
|
|
|
|
{
|
|
|
|
struct rtattr *rta;
|
|
|
|
struct rtmsg *msg;
|
|
|
|
size_t rtasize;
|
|
|
|
|
2014-07-09 13:53:14 +00:00
|
|
|
msg = NLMSG_DATA(hdr);
|
2011-10-12 13:52:18 +00:00
|
|
|
rta = RTM_RTA(msg);
|
|
|
|
rtasize = RTM_PAYLOAD(hdr);
|
|
|
|
|
|
|
|
if (route)
|
|
|
|
{
|
2018-02-22 08:49:57 +00:00
|
|
|
*route = (rt_entry_t){
|
|
|
|
.dst_len = msg->rtm_dst_len,
|
|
|
|
.src_len = msg->rtm_src_len,
|
|
|
|
.table = msg->rtm_table,
|
|
|
|
};
|
2011-10-12 13:52:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INIT(route,
|
|
|
|
.dst_len = msg->rtm_dst_len,
|
2016-08-23 10:48:37 +00:00
|
|
|
.src_len = msg->rtm_src_len,
|
2011-10-12 13:52:18 +00:00
|
|
|
.table = msg->rtm_table,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (RTA_OK(rta, rtasize))
|
|
|
|
{
|
|
|
|
switch (rta->rta_type)
|
|
|
|
{
|
|
|
|
case RTA_PREFSRC:
|
2016-08-23 10:48:37 +00:00
|
|
|
route->pref_src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
|
2011-10-12 13:52:18 +00:00
|
|
|
break;
|
|
|
|
case RTA_GATEWAY:
|
|
|
|
route->gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
|
|
|
|
break;
|
|
|
|
case RTA_DST:
|
|
|
|
route->dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
|
|
|
|
break;
|
2016-08-23 10:48:37 +00:00
|
|
|
case RTA_SRC:
|
|
|
|
route->src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
|
|
|
|
break;
|
2011-10-12 13:52:18 +00:00
|
|
|
case RTA_OIF:
|
|
|
|
if (RTA_PAYLOAD(rta) == sizeof(route->oif))
|
|
|
|
{
|
2016-03-22 12:22:01 +00:00
|
|
|
route->oif = *(uint32_t*)RTA_DATA(rta);
|
2011-10-12 13:52:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-01-15 14:05:42 +00:00
|
|
|
case RTA_PRIORITY:
|
|
|
|
if (RTA_PAYLOAD(rta) == sizeof(route->priority))
|
|
|
|
{
|
2016-03-22 12:22:01 +00:00
|
|
|
route->priority = *(uint32_t*)RTA_DATA(rta);
|
2015-01-15 14:05:42 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-10-12 13:52:18 +00:00
|
|
|
#ifdef HAVE_RTA_TABLE
|
|
|
|
case RTA_TABLE:
|
|
|
|
if (RTA_PAYLOAD(rta) == sizeof(route->table))
|
|
|
|
{
|
2016-03-22 12:22:01 +00:00
|
|
|
route->table = *(uint32_t*)RTA_DATA(rta);
|
2011-10-12 13:52:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_RTA_TABLE*/
|
|
|
|
}
|
|
|
|
rta = RTA_NEXT(rta, rtasize);
|
|
|
|
}
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
|
|
|
|
*/
|
|
|
|
static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
|
2014-06-18 07:58:01 +00:00
|
|
|
int prefix, bool nexthop, host_t *candidate,
|
2016-03-11 17:54:31 +00:00
|
|
|
char **iface, u_int recursion)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2008-11-14 14:23:11 +00:00
|
|
|
netlink_buf_t request;
|
2008-09-25 07:56:58 +00:00
|
|
|
struct nlmsghdr *hdr, *out, *current;
|
|
|
|
struct rtmsg *msg;
|
|
|
|
chunk_t chunk;
|
|
|
|
size_t len;
|
2011-10-12 13:52:18 +00:00
|
|
|
linked_list_t *routes;
|
|
|
|
rt_entry_t *route = NULL, *best = NULL;
|
2010-03-12 08:37:51 +00:00
|
|
|
enumerator_t *enumerator;
|
2011-10-12 13:52:18 +00:00
|
|
|
host_t *addr = NULL;
|
2014-06-18 07:58:01 +00:00
|
|
|
bool match_net;
|
|
|
|
int family;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-11-12 18:39:34 +00:00
|
|
|
if (recursion > MAX_ROUTE_RECURSION)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-18 07:58:01 +00:00
|
|
|
chunk = dest->get_address(dest);
|
|
|
|
len = chunk.len * 8;
|
|
|
|
prefix = prefix < 0 ? len : min(prefix, len);
|
|
|
|
match_net = prefix != len;
|
2012-11-12 18:39:34 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
memset(&request, 0, sizeof(request));
|
|
|
|
|
2014-06-18 07:58:01 +00:00
|
|
|
family = dest->get_family(dest);
|
2014-07-09 13:47:24 +00:00
|
|
|
hdr = &request.hdr;
|
2009-11-24 13:10:18 +00:00
|
|
|
hdr->nlmsg_flags = NLM_F_REQUEST;
|
2008-09-25 07:56:58 +00:00
|
|
|
hdr->nlmsg_type = RTM_GETROUTE;
|
|
|
|
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
|
2014-07-09 13:53:14 +00:00
|
|
|
msg = NLMSG_DATA(hdr);
|
2014-06-18 07:58:01 +00:00
|
|
|
msg->rtm_family = family;
|
2015-08-05 14:51:38 +00:00
|
|
|
if (!match_net && this->rta_mark && this->routing_mark.value)
|
|
|
|
{
|
|
|
|
/* if our routing rule excludes packets with a certain mark we can
|
|
|
|
* get the preferred route without having to dump all routes */
|
|
|
|
chunk = chunk_from_thing(this->routing_mark.value);
|
|
|
|
netlink_add_attribute(hdr, RTA_MARK, chunk, sizeof(request));
|
|
|
|
}
|
|
|
|
else if (family == AF_INET || this->rta_prefsrc_for_ipv6 ||
|
|
|
|
this->routing_table || match_net)
|
|
|
|
{ /* kernels prior to 3.0 do not support RTA_PREFSRC for IPv6 routes.
|
|
|
|
* as we want to ignore routes with virtual IPs we cannot use DUMP
|
|
|
|
* if these routes are not installed in a separate table */
|
2017-01-19 10:03:55 +00:00
|
|
|
if (this->install_routes)
|
|
|
|
{
|
|
|
|
hdr->nlmsg_flags |= NLM_F_DUMP;
|
|
|
|
}
|
2015-08-05 14:51:38 +00:00
|
|
|
}
|
2008-10-08 08:23:46 +00:00
|
|
|
if (candidate)
|
|
|
|
{
|
|
|
|
chunk = candidate->get_address(candidate);
|
2017-01-19 10:23:45 +00:00
|
|
|
if (hdr->nlmsg_flags & NLM_F_DUMP)
|
|
|
|
{
|
|
|
|
netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
netlink_add_attribute(hdr, RTA_SRC, chunk, sizeof(request));
|
|
|
|
}
|
2008-10-08 08:23:46 +00:00
|
|
|
}
|
2016-03-11 17:39:35 +00:00
|
|
|
/* we use this below to match against the routes */
|
|
|
|
chunk = dest->get_address(dest);
|
2014-06-18 07:58:01 +00:00
|
|
|
if (!match_net)
|
|
|
|
{
|
|
|
|
netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
|
|
|
|
{
|
2014-06-18 07:58:01 +00:00
|
|
|
DBG2(DBG_KNL, "getting %s to reach %H/%d failed",
|
|
|
|
nexthop ? "nexthop" : "address", dest, prefix);
|
2008-09-25 07:56:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
routes = linked_list_create();
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2009-09-16 11:32:47 +00:00
|
|
|
|
|
|
|
for (current = out; NLMSG_OK(current, len);
|
|
|
|
current = NLMSG_NEXT(current, len))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
|
|
|
switch (current->nlmsg_type)
|
|
|
|
{
|
|
|
|
case NLMSG_DONE:
|
|
|
|
break;
|
|
|
|
case RTM_NEWROUTE:
|
|
|
|
{
|
2011-10-12 13:52:18 +00:00
|
|
|
rt_entry_t *other;
|
2010-03-12 08:37:51 +00:00
|
|
|
uintptr_t table;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2018-02-26 14:07:15 +00:00
|
|
|
if (!route_usable(current, TRUE))
|
2015-04-09 12:43:07 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
route = parse_route(current, route);
|
|
|
|
|
|
|
|
table = (uintptr_t)route->table;
|
|
|
|
if (this->rt_exclude->find_first(this->rt_exclude, NULL,
|
2017-05-16 10:11:24 +00:00
|
|
|
(void**)&table))
|
2011-10-12 13:52:18 +00:00
|
|
|
{ /* route is from an excluded routing table */
|
2010-03-12 08:37:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-11-14 13:04:22 +00:00
|
|
|
if (this->routing_table != 0 &&
|
2011-10-12 13:52:18 +00:00
|
|
|
route->table == this->routing_table)
|
2008-11-14 13:04:22 +00:00
|
|
|
{ /* route is from our own ipsec routing table */
|
2009-09-16 11:32:47 +00:00
|
|
|
continue;
|
2008-11-14 13:04:22 +00:00
|
|
|
}
|
2012-09-14 14:43:08 +00:00
|
|
|
if (route->oif && !is_interface_up_and_usable(this, route->oif))
|
2009-09-02 09:47:14 +00:00
|
|
|
{ /* interface is down */
|
2009-09-16 11:32:47 +00:00
|
|
|
continue;
|
2008-11-14 13:04:22 +00:00
|
|
|
}
|
2014-06-18 07:58:01 +00:00
|
|
|
if (!addr_in_subnet(chunk, prefix, route->dst, route->dst_len))
|
2009-09-02 09:47:14 +00:00
|
|
|
{ /* route destination does not contain dest */
|
2009-09-16 11:32:47 +00:00
|
|
|
continue;
|
2008-11-14 13:04:22 +00:00
|
|
|
}
|
2016-08-23 10:48:37 +00:00
|
|
|
if (route->pref_src.ptr)
|
2011-10-12 13:52:18 +00:00
|
|
|
{ /* verify source address, if any */
|
|
|
|
host_t *src = host_create_from_chunk(msg->rtm_family,
|
2016-08-23 10:48:37 +00:00
|
|
|
route->pref_src, 0);
|
2012-09-19 17:10:23 +00:00
|
|
|
if (src && is_known_vip(this, src))
|
2011-10-12 13:52:18 +00:00
|
|
|
{ /* ignore routes installed by us */
|
|
|
|
src->destroy(src);
|
|
|
|
continue;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
route->src_host = src;
|
2008-11-14 13:04:22 +00:00
|
|
|
}
|
2016-04-18 16:39:35 +00:00
|
|
|
/* insert route, sorted by network prefix and priority */
|
2011-10-12 13:52:18 +00:00
|
|
|
enumerator = routes->create_enumerator(routes);
|
|
|
|
while (enumerator->enumerate(enumerator, &other))
|
|
|
|
{
|
2016-04-18 16:39:35 +00:00
|
|
|
if (route->dst_len > other->dst_len)
|
2015-01-15 14:05:42 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-04-18 16:39:35 +00:00
|
|
|
if (route->dst_len == other->dst_len &&
|
|
|
|
route->priority < other->priority)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2011-10-12 13:52:18 +00:00
|
|
|
break;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
routes->insert_before(routes, enumerator, route);
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
route = NULL;
|
2009-09-16 11:32:47 +00:00
|
|
|
continue;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
if (route)
|
|
|
|
{
|
|
|
|
rt_entry_destroy(route);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we have a list of routes matching dest, sorted by net prefix.
|
|
|
|
* we will look for source addresses for these routes and select the one
|
|
|
|
* with the preferred source address, if possible */
|
|
|
|
enumerator = routes->create_enumerator(routes);
|
|
|
|
while (enumerator->enumerate(enumerator, &route))
|
|
|
|
{
|
|
|
|
if (route->src_host)
|
|
|
|
{ /* got a source address with the route, if no preferred source
|
|
|
|
* is given or it matches we are done, as this is the best route */
|
|
|
|
if (!candidate || candidate->ip_equals(candidate, route->src_host))
|
|
|
|
{
|
|
|
|
best = route;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (route->oif)
|
|
|
|
{ /* no match yet, maybe it is assigned to the same interface */
|
|
|
|
host_t *src = get_interface_address(this, route->oif,
|
2014-03-14 14:41:52 +00:00
|
|
|
msg->rtm_family, dest, candidate);
|
2011-10-12 13:52:18 +00:00
|
|
|
if (src && src->ip_equals(src, candidate))
|
|
|
|
{
|
|
|
|
route->src_host->destroy(route->src_host);
|
|
|
|
route->src_host = src;
|
|
|
|
best = route;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DESTROY_IF(src);
|
|
|
|
}
|
|
|
|
/* no luck yet with the source address. if this is the best (first)
|
|
|
|
* route we store it as fallback in case we don't find a route with
|
|
|
|
* the preferred source */
|
|
|
|
best = best ?: route;
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-23 10:48:37 +00:00
|
|
|
if (route->src.ptr)
|
|
|
|
{ /* no src, but a source selector, try to find a matching address */
|
|
|
|
route->src_host = get_subnet_address(this, msg->rtm_family,
|
|
|
|
route->src, route->src_len, dest,
|
|
|
|
candidate);
|
|
|
|
if (route->src_host)
|
|
|
|
{ /* we handle this address the same as the one above */
|
|
|
|
if (!candidate ||
|
|
|
|
candidate->ip_equals(candidate, route->src_host))
|
|
|
|
{
|
|
|
|
best = route;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
best = best ?: route;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
if (route->oif)
|
|
|
|
{ /* no src, but an interface - get address from it */
|
|
|
|
route->src_host = get_interface_address(this, route->oif,
|
2014-03-14 14:41:52 +00:00
|
|
|
msg->rtm_family, dest, candidate);
|
2011-10-12 13:52:18 +00:00
|
|
|
if (route->src_host)
|
2016-08-23 10:48:37 +00:00
|
|
|
{ /* more of the same */
|
2011-10-12 13:52:18 +00:00
|
|
|
if (!candidate ||
|
|
|
|
candidate->ip_equals(candidate, route->src_host))
|
|
|
|
{
|
|
|
|
best = route;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
best = best ?: route;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (route->gtw.ptr)
|
|
|
|
{ /* no src, no iface, but a gateway - lookup src to reach gtw */
|
|
|
|
host_t *gtw;
|
|
|
|
|
|
|
|
gtw = host_create_from_chunk(msg->rtm_family, route->gtw, 0);
|
2012-11-12 16:12:18 +00:00
|
|
|
if (gtw && !gtw->ip_equals(gtw, dest))
|
|
|
|
{
|
2014-06-18 07:58:01 +00:00
|
|
|
route->src_host = get_route(this, gtw, -1, FALSE, candidate,
|
2016-03-11 17:54:31 +00:00
|
|
|
iface, recursion + 1);
|
2012-11-12 16:12:18 +00:00
|
|
|
}
|
|
|
|
DESTROY_IF(gtw);
|
2011-10-12 13:52:18 +00:00
|
|
|
if (route->src_host)
|
|
|
|
{ /* more of the same */
|
|
|
|
if (!candidate ||
|
|
|
|
candidate->ip_equals(candidate, route->src_host))
|
|
|
|
{
|
|
|
|
best = route;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
best = best ?: route;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
if (nexthop)
|
2016-03-11 18:07:10 +00:00
|
|
|
{ /* nexthop lookup, return gateway and oif if any */
|
2016-03-11 17:54:31 +00:00
|
|
|
if (iface)
|
|
|
|
{
|
|
|
|
*iface = NULL;
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
if (best || routes->get_first(routes, (void**)&best) == SUCCESS)
|
|
|
|
{
|
|
|
|
addr = host_create_from_chunk(msg->rtm_family, best->gtw, 0);
|
2016-10-12 16:12:08 +00:00
|
|
|
if (iface && best->oif)
|
2016-03-11 18:07:10 +00:00
|
|
|
{
|
2016-10-12 16:12:08 +00:00
|
|
|
*iface = get_interface_name_by_index(this, best->oif);
|
2016-03-11 18:07:10 +00:00
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
}
|
2014-06-18 07:58:01 +00:00
|
|
|
if (!addr && !match_net)
|
|
|
|
{ /* fallback to destination address */
|
|
|
|
addr = dest->clone(dest);
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
}
|
|
|
|
else
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2011-10-12 13:52:18 +00:00
|
|
|
if (best)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2011-10-12 13:52:18 +00:00
|
|
|
addr = best->src_host->clone(best->src_host);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2011-10-12 13:52:18 +00:00
|
|
|
routes->destroy_function(routes, (void*)rt_entry_destroy);
|
|
|
|
free(out);
|
|
|
|
|
|
|
|
if (addr)
|
|
|
|
{
|
2016-03-11 18:07:10 +00:00
|
|
|
if (nexthop && iface && *iface)
|
|
|
|
{
|
|
|
|
DBG2(DBG_KNL, "using %H as nexthop and %s as dev to reach %H/%d",
|
|
|
|
addr, *iface, dest, prefix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBG2(DBG_KNL, "using %H as %s to reach %H/%d", addr,
|
|
|
|
nexthop ? "nexthop" : "address", dest, prefix);
|
|
|
|
}
|
2011-10-12 13:52:18 +00:00
|
|
|
}
|
2012-11-12 18:39:34 +00:00
|
|
|
else if (!recursion)
|
2011-10-12 13:52:18 +00:00
|
|
|
{
|
2014-06-18 07:58:01 +00:00
|
|
|
DBG2(DBG_KNL, "no %s found to reach %H/%d",
|
|
|
|
nexthop ? "nexthop" : "address", dest, prefix);
|
2011-10-12 13:52:18 +00:00
|
|
|
}
|
|
|
|
return addr;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, get_source_addr, host_t*,
|
|
|
|
private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2016-03-11 17:54:31 +00:00
|
|
|
return get_route(this, dest, -1, FALSE, src, NULL, 0);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, get_nexthop, host_t*,
|
2016-03-11 17:54:31 +00:00
|
|
|
private_kernel_netlink_net_t *this, host_t *dest, int prefix, host_t *src,
|
|
|
|
char **iface)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2016-03-11 17:54:31 +00:00
|
|
|
return get_route(this, dest, prefix, TRUE, src, iface, 0);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:52:45 +00:00
|
|
|
/** enumerator over subnets */
|
|
|
|
typedef struct {
|
|
|
|
enumerator_t public;
|
|
|
|
private_kernel_netlink_net_t *private;
|
|
|
|
/** message from the kernel */
|
|
|
|
struct nlmsghdr *msg;
|
|
|
|
/** current message from the kernel */
|
|
|
|
struct nlmsghdr *current;
|
|
|
|
/** remaining length */
|
|
|
|
size_t len;
|
|
|
|
/** last subnet enumerated */
|
|
|
|
host_t *net;
|
2016-10-12 10:22:42 +00:00
|
|
|
/** interface of current net */
|
|
|
|
char ifname[IFNAMSIZ];
|
2016-10-12 07:52:45 +00:00
|
|
|
} subnet_enumerator_t;
|
|
|
|
|
|
|
|
METHOD(enumerator_t, destroy_subnet_enumerator, void,
|
|
|
|
subnet_enumerator_t *this)
|
|
|
|
{
|
|
|
|
DESTROY_IF(this->net);
|
|
|
|
free(this->msg);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(enumerator_t, enumerate_subnets, bool,
|
2017-05-11 07:17:02 +00:00
|
|
|
subnet_enumerator_t *this, va_list args)
|
2016-10-12 07:52:45 +00:00
|
|
|
{
|
2017-05-11 07:17:02 +00:00
|
|
|
host_t **net;
|
|
|
|
uint8_t *mask;
|
|
|
|
char **ifname;
|
|
|
|
|
|
|
|
VA_ARGS_VGET(args, net, mask, ifname);
|
|
|
|
|
2016-10-12 07:52:45 +00:00
|
|
|
if (!this->current)
|
|
|
|
{
|
|
|
|
this->current = this->msg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->current = NLMSG_NEXT(this->current, this->len);
|
|
|
|
DESTROY_IF(this->net);
|
|
|
|
this->net = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (NLMSG_OK(this->current, this->len))
|
|
|
|
{
|
|
|
|
switch (this->current->nlmsg_type)
|
|
|
|
{
|
|
|
|
case NLMSG_DONE:
|
|
|
|
break;
|
|
|
|
case RTM_NEWROUTE:
|
|
|
|
{
|
2018-02-22 08:50:52 +00:00
|
|
|
rt_entry_t route;
|
2016-10-12 07:52:45 +00:00
|
|
|
|
2018-02-26 14:07:15 +00:00
|
|
|
if (!route_usable(this->current, FALSE))
|
2016-10-12 07:52:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-02-22 08:50:52 +00:00
|
|
|
parse_route(this->current, &route);
|
|
|
|
|
|
|
|
if (route.table && (
|
|
|
|
route.table == RT_TABLE_LOCAL ||
|
|
|
|
route.table == this->private->routing_table))
|
2016-10-12 07:52:45 +00:00
|
|
|
{ /* ignore our own and the local routing tables */
|
|
|
|
break;
|
|
|
|
}
|
2018-02-22 08:50:52 +00:00
|
|
|
else if (route.gtw.ptr)
|
|
|
|
{ /* ignore routes via gateway/next hop */
|
|
|
|
break;
|
2016-10-12 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 08:50:52 +00:00
|
|
|
if (route.dst.ptr && route.oif &&
|
|
|
|
if_indextoname(route.oif, this->ifname))
|
2016-10-12 07:52:45 +00:00
|
|
|
{
|
2018-02-22 08:50:52 +00:00
|
|
|
this->net = host_create_from_chunk(AF_UNSPEC, route.dst, 0);
|
2016-10-12 07:52:45 +00:00
|
|
|
*net = this->net;
|
2018-02-22 08:50:52 +00:00
|
|
|
*mask = route.dst_len;
|
2016-10-12 10:22:42 +00:00
|
|
|
*ifname = this->ifname;
|
2016-10-12 07:52:45 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->current = NLMSG_NEXT(this->current, this->len);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(kernel_net_t, create_local_subnet_enumerator, enumerator_t*,
|
|
|
|
private_kernel_netlink_net_t *this)
|
|
|
|
{
|
|
|
|
netlink_buf_t request;
|
|
|
|
struct nlmsghdr *hdr, *out;
|
|
|
|
struct rtmsg *msg;
|
|
|
|
size_t len;
|
|
|
|
subnet_enumerator_t *enumerator;
|
|
|
|
|
|
|
|
memset(&request, 0, sizeof(request));
|
|
|
|
|
|
|
|
hdr = &request.hdr;
|
|
|
|
hdr->nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
hdr->nlmsg_type = RTM_GETROUTE;
|
|
|
|
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
hdr->nlmsg_flags |= NLM_F_DUMP;
|
|
|
|
|
|
|
|
msg = NLMSG_DATA(hdr);
|
|
|
|
msg->rtm_scope = RT_SCOPE_LINK;
|
|
|
|
|
|
|
|
if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
|
|
|
|
{
|
|
|
|
DBG2(DBG_KNL, "enumerating local subnets failed");
|
|
|
|
return enumerator_create_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT(enumerator,
|
|
|
|
.public = {
|
2017-05-11 07:17:02 +00:00
|
|
|
.enumerate = enumerator_enumerate_default,
|
|
|
|
.venumerate = _enumerate_subnets,
|
2016-10-12 07:52:45 +00:00
|
|
|
.destroy = _destroy_subnet_enumerator,
|
|
|
|
},
|
|
|
|
.private = this,
|
|
|
|
.msg = out,
|
|
|
|
.len = len,
|
|
|
|
);
|
|
|
|
return &enumerator->public;
|
|
|
|
}
|
|
|
|
|
2018-04-03 13:01:20 +00:00
|
|
|
/**
|
|
|
|
* Manages the creation and deletion of IPv6 address labels for virtual IPs.
|
|
|
|
* By setting the appropriate nlmsg_type the label is either added or removed.
|
|
|
|
*/
|
|
|
|
static status_t manage_addrlabel(private_kernel_netlink_net_t *this,
|
|
|
|
int nlmsg_type, host_t *ip)
|
|
|
|
{
|
|
|
|
netlink_buf_t request;
|
|
|
|
struct nlmsghdr *hdr;
|
|
|
|
struct ifaddrlblmsg *msg;
|
|
|
|
chunk_t chunk;
|
|
|
|
uint32_t label;
|
|
|
|
|
|
|
|
memset(&request, 0, sizeof(request));
|
|
|
|
|
|
|
|
chunk = ip->get_address(ip);
|
|
|
|
|
|
|
|
hdr = &request.hdr;
|
|
|
|
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
|
|
|
|
if (nlmsg_type == RTM_NEWADDRLABEL)
|
|
|
|
{
|
|
|
|
hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
|
|
|
|
}
|
|
|
|
hdr->nlmsg_type = nlmsg_type;
|
|
|
|
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg));
|
|
|
|
|
|
|
|
msg = NLMSG_DATA(hdr);
|
|
|
|
msg->ifal_family = ip->get_family(ip);
|
|
|
|
msg->ifal_prefixlen = chunk.len * 8;
|
|
|
|
|
|
|
|
netlink_add_attribute(hdr, IFAL_ADDRESS, chunk, sizeof(request));
|
|
|
|
/* doesn't really matter as default labels are < 20 but this makes it kinda
|
|
|
|
* recognizable */
|
|
|
|
label = 220;
|
|
|
|
netlink_add_attribute(hdr, IFAL_LABEL, chunk_from_thing(label),
|
|
|
|
sizeof(request));
|
|
|
|
|
|
|
|
return this->socket->send_ack(this->socket, hdr);
|
|
|
|
}
|
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/**
|
|
|
|
* Manages the creation and deletion of ip addresses on an interface.
|
|
|
|
* By setting the appropriate nlmsg_type, the ip will be set or unset.
|
|
|
|
*/
|
|
|
|
static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
|
2012-11-09 14:46:58 +00:00
|
|
|
int flags, int if_index, host_t *ip, int prefix)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2008-11-14 14:23:11 +00:00
|
|
|
netlink_buf_t request;
|
2008-09-25 07:56:58 +00:00
|
|
|
struct nlmsghdr *hdr;
|
|
|
|
struct ifaddrmsg *msg;
|
|
|
|
chunk_t chunk;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
memset(&request, 0, sizeof(request));
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
chunk = ip->get_address(ip);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2014-07-09 13:47:24 +00:00
|
|
|
hdr = &request.hdr;
|
2008-09-25 07:56:58 +00:00
|
|
|
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
|
2009-09-04 11:46:09 +00:00
|
|
|
hdr->nlmsg_type = nlmsg_type;
|
2008-09-25 07:56:58 +00:00
|
|
|
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2014-07-09 13:53:14 +00:00
|
|
|
msg = NLMSG_DATA(hdr);
|
2009-09-04 12:50:23 +00:00
|
|
|
msg->ifa_family = ip->get_family(ip);
|
|
|
|
msg->ifa_flags = 0;
|
2012-11-09 14:46:58 +00:00
|
|
|
msg->ifa_prefixlen = prefix < 0 ? chunk.len * 8 : prefix;
|
2009-09-04 12:50:23 +00:00
|
|
|
msg->ifa_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
msg->ifa_index = if_index;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
|
|
|
|
|
2016-12-13 16:27:26 +00:00
|
|
|
if (ip->get_family(ip) == AF_INET6)
|
|
|
|
{
|
2017-12-11 09:35:30 +00:00
|
|
|
#ifdef IFA_F_NODAD
|
2016-12-13 16:27:26 +00:00
|
|
|
msg->ifa_flags |= IFA_F_NODAD;
|
2017-12-11 09:35:30 +00:00
|
|
|
#endif
|
2016-12-13 16:27:26 +00:00
|
|
|
if (this->rta_prefsrc_for_ipv6)
|
|
|
|
{
|
2018-04-03 13:01:20 +00:00
|
|
|
/* if source routes are possible we set a label for this virtual IP
|
|
|
|
* so it gets only used if forced by our route, and not by the
|
|
|
|
* default IPv6 address selection */
|
|
|
|
int labelop = nlmsg_type == RTM_NEWADDR ? RTM_NEWADDRLABEL
|
|
|
|
: RTM_DELADDRLABEL;
|
|
|
|
if (manage_addrlabel(this, labelop, ip) != SUCCESS)
|
|
|
|
{
|
|
|
|
/* if we can't use address labels we let the virtual IP get
|
|
|
|
* deprecated immediately (but mark it as valid forever), which
|
|
|
|
* should also avoid that it gets used by the default address
|
|
|
|
* selection */
|
|
|
|
struct ifa_cacheinfo cache = {
|
|
|
|
.ifa_valid = 0xFFFFFFFF,
|
|
|
|
.ifa_prefered = 0,
|
|
|
|
};
|
|
|
|
netlink_add_attribute(hdr, IFA_CACHEINFO,
|
|
|
|
chunk_from_thing(cache), sizeof(request));
|
|
|
|
}
|
2016-12-13 16:27:26 +00:00
|
|
|
}
|
2014-06-20 06:51:07 +00:00
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->socket->send_ack(this->socket, hdr);
|
|
|
|
}
|
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, add_ip, status_t,
|
2012-11-09 14:46:58 +00:00
|
|
|
private_kernel_netlink_net_t *this, host_t *virtual_ip, int prefix,
|
2012-11-12 09:06:09 +00:00
|
|
|
char *iface_name)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
addr_map_entry_t *entry, lookup = {
|
|
|
|
.ip = virtual_ip,
|
|
|
|
};
|
2012-09-20 09:07:15 +00:00
|
|
|
iface_entry_t *iface = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-02 11:34:03 +00:00
|
|
|
if (!this->install_virtual_ip)
|
|
|
|
{ /* disabled by config */
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
/* the virtual IP might actually be installed as regular IP, in which case
|
|
|
|
* we don't track it as virtual IP */
|
|
|
|
entry = this->addrs->get_match(this->addrs, &lookup,
|
|
|
|
(void*)addr_map_entry_match);
|
|
|
|
if (!entry)
|
|
|
|
{ /* otherwise it might already be installed as virtual IP */
|
|
|
|
entry = this->vips->get_match(this->vips, &lookup,
|
|
|
|
(void*)addr_map_entry_match);
|
|
|
|
if (entry)
|
|
|
|
{ /* the vip we found can be in one of three states: 1) installed and
|
|
|
|
* ready, 2) just added by another thread, but not yet confirmed to
|
|
|
|
* be installed by the kernel, 3) just deleted, but not yet gone.
|
|
|
|
* Then while we wait below, several things could happen (as we
|
2012-09-20 16:21:42 +00:00
|
|
|
* release the lock). For instance, the interface could disappear,
|
2012-09-20 09:07:15 +00:00
|
|
|
* or the IP is finally deleted, and it reappears on a different
|
2012-09-19 17:10:23 +00:00
|
|
|
* interface. All these cases are handled by the call below. */
|
|
|
|
while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-20 16:21:42 +00:00
|
|
|
this->condvar->wait(this->condvar, this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
if (entry)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
entry->addr->refcount++;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
}
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
DBG2(DBG_KNL, "virtual IP %H is already installed on %s", virtual_ip,
|
|
|
|
entry->iface->ifname);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2012-09-20 09:07:15 +00:00
|
|
|
/* try to find the target interface, either by config or via src ip */
|
|
|
|
if (!this->install_virtual_ip_on ||
|
2017-05-16 10:11:24 +00:00
|
|
|
!this->ifaces->find_first(this->ifaces, iface_entry_by_name,
|
|
|
|
(void**)&iface, this->install_virtual_ip_on))
|
2012-09-20 09:07:15 +00:00
|
|
|
{
|
2017-05-16 10:11:24 +00:00
|
|
|
if (!this->ifaces->find_first(this->ifaces, iface_entry_by_name,
|
|
|
|
(void**)&iface, iface_name))
|
2012-09-20 09:07:15 +00:00
|
|
|
{ /* if we don't find the requested interface we just use the first */
|
|
|
|
this->ifaces->get_first(this->ifaces, (void**)&iface);
|
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
}
|
|
|
|
if (iface)
|
|
|
|
{
|
|
|
|
addr_entry_t *addr;
|
2014-07-16 09:59:59 +00:00
|
|
|
char *ifname;
|
|
|
|
int ifi;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
INIT(addr,
|
|
|
|
.ip = virtual_ip->clone(virtual_ip),
|
|
|
|
.refcount = 1,
|
|
|
|
.scope = RT_SCOPE_UNIVERSE,
|
|
|
|
);
|
|
|
|
iface->addrs->insert_last(iface->addrs, addr);
|
|
|
|
addr_map_entry_add(this->vips, addr, iface);
|
2014-07-16 09:59:59 +00:00
|
|
|
ifi = iface->ifindex;
|
|
|
|
this->lock->unlock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
|
2014-07-16 09:59:59 +00:00
|
|
|
ifi, virtual_ip, prefix) == SUCCESS)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2014-07-16 09:59:59 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
|
|
|
|
{ /* wait until address appears */
|
2012-09-20 16:21:42 +00:00
|
|
|
this->condvar->wait(this->condvar, this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
}
|
|
|
|
if (entry)
|
|
|
|
{ /* we fail if the interface got deleted in the meantime */
|
2014-07-16 09:59:59 +00:00
|
|
|
ifname = strdup(entry->iface->ifname);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2014-07-16 09:59:59 +00:00
|
|
|
DBG2(DBG_KNL, "virtual IP %H installed on %s",
|
|
|
|
virtual_ip, ifname);
|
2013-02-20 08:16:00 +00:00
|
|
|
/* during IKEv1 reauthentication, children get moved from
|
|
|
|
* old the new SA before the virtual IP is available. This
|
|
|
|
* kills the route for our virtual IP, reinstall. */
|
2014-07-16 09:59:59 +00:00
|
|
|
queue_route_reinstall(this, ifname);
|
2008-09-25 07:56:58 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2014-07-16 09:59:59 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
|
|
|
|
return FAILED;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
DBG1(DBG_KNL, "no interface available, unable to install virtual IP %H",
|
|
|
|
virtual_ip);
|
2008-09-25 07:56:58 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, del_ip, status_t,
|
2012-11-12 10:14:03 +00:00
|
|
|
private_kernel_netlink_net_t *this, host_t *virtual_ip, int prefix,
|
|
|
|
bool wait)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
addr_map_entry_t *entry, lookup = {
|
|
|
|
.ip = virtual_ip,
|
|
|
|
};
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-02 11:34:03 +00:00
|
|
|
if (!this->install_virtual_ip)
|
|
|
|
{ /* disabled by config */
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
entry = this->vips->get_match(this->vips, &lookup,
|
|
|
|
(void*)addr_map_entry_match);
|
|
|
|
if (!entry)
|
|
|
|
{ /* we didn't install this IP as virtual IP */
|
|
|
|
entry = this->addrs->get_match(this->addrs, &lookup,
|
|
|
|
(void*)addr_map_entry_match);
|
|
|
|
if (entry)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-19 17:10:23 +00:00
|
|
|
DBG2(DBG_KNL, "not deleting existing IP %H on %s", virtual_ip,
|
|
|
|
entry->iface->ifname);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
if (entry->addr->refcount == 1)
|
|
|
|
{
|
|
|
|
status_t status;
|
2014-07-16 09:59:59 +00:00
|
|
|
int ifi;
|
2012-09-19 17:10:23 +00:00
|
|
|
|
|
|
|
/* we set this flag so that threads calling add_ip will block and wait
|
|
|
|
* until the entry is gone, also so we can wait below */
|
|
|
|
entry->addr->installed = FALSE;
|
2014-07-16 09:59:59 +00:00
|
|
|
ifi = entry->iface->ifindex;
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
status = manage_ipaddr(this, RTM_DELADDR, 0, ifi, virtual_ip, prefix);
|
2012-11-12 10:14:03 +00:00
|
|
|
if (status == SUCCESS && wait)
|
2012-09-19 17:10:23 +00:00
|
|
|
{ /* wait until the address is really gone */
|
2014-07-16 09:59:59 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2021-06-10 09:34:37 +00:00
|
|
|
while (is_known_vip(this, virtual_ip) &&
|
|
|
|
lib->watcher->get_state(lib->watcher) != WATCHER_STOPPED)
|
|
|
|
{ /* don't wait during deinit when we can't get notified,
|
|
|
|
* re-evaluate watcher state if we have to wait longer */
|
|
|
|
this->condvar->timed_wait(this->condvar, this->lock, 1000);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2014-07-16 09:59:59 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
return status;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2012-09-19 17:10:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
entry->addr->refcount--;
|
|
|
|
}
|
|
|
|
DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
|
|
|
|
virtual_ip);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2012-09-19 17:10:23 +00:00
|
|
|
return SUCCESS;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages source routes in the routing table.
|
|
|
|
* By setting the appropriate nlmsg_type, the route gets added or removed.
|
|
|
|
*/
|
2011-12-20 13:30:10 +00:00
|
|
|
static status_t manage_srcroute(private_kernel_netlink_net_t *this,
|
|
|
|
int nlmsg_type, int flags, chunk_t dst_net,
|
2016-03-22 12:22:01 +00:00
|
|
|
uint8_t prefixlen, host_t *gateway,
|
2020-02-09 13:52:32 +00:00
|
|
|
host_t *src_ip, char *if_name, bool pass)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2008-11-14 14:23:11 +00:00
|
|
|
netlink_buf_t request;
|
2008-09-25 07:56:58 +00:00
|
|
|
struct nlmsghdr *hdr;
|
|
|
|
struct rtmsg *msg;
|
2014-08-04 13:35:18 +00:00
|
|
|
struct rtattr *rta;
|
2008-09-25 07:56:58 +00:00
|
|
|
int ifindex;
|
|
|
|
chunk_t chunk;
|
|
|
|
|
|
|
|
/* if route is 0.0.0.0/0, we can't install it, as it would
|
|
|
|
* overwrite the default route. Instead, we add two routes:
|
|
|
|
* 0.0.0.0/1 and 128.0.0.0/1 */
|
|
|
|
if (this->routing_table == 0 && prefixlen == 0)
|
|
|
|
{
|
|
|
|
chunk_t half_net;
|
2016-03-22 12:22:01 +00:00
|
|
|
uint8_t half_prefixlen;
|
2008-09-25 07:56:58 +00:00
|
|
|
status_t status;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
half_net = chunk_alloca(dst_net.len);
|
|
|
|
memset(half_net.ptr, 0, half_net.len);
|
|
|
|
half_prefixlen = 1;
|
2020-02-09 13:52:32 +00:00
|
|
|
/* no throw routes in the main table */
|
2018-09-17 13:56:48 +00:00
|
|
|
status = manage_srcroute(this, nlmsg_type, flags, half_net,
|
2020-02-09 13:52:32 +00:00
|
|
|
half_prefixlen, gateway, src_ip, if_name, FALSE);
|
2008-09-25 07:56:58 +00:00
|
|
|
half_net.ptr[0] |= 0x80;
|
2018-09-17 13:56:48 +00:00
|
|
|
status |= manage_srcroute(this, nlmsg_type, flags, half_net,
|
2020-02-09 13:52:32 +00:00
|
|
|
half_prefixlen, gateway, src_ip, if_name, FALSE);
|
2008-09-25 07:56:58 +00:00
|
|
|
return status;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
memset(&request, 0, sizeof(request));
|
|
|
|
|
2014-07-09 13:47:24 +00:00
|
|
|
hdr = &request.hdr;
|
2008-09-25 07:56:58 +00:00
|
|
|
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
|
|
|
|
hdr->nlmsg_type = nlmsg_type;
|
|
|
|
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
|
2014-07-09 13:53:14 +00:00
|
|
|
msg = NLMSG_DATA(hdr);
|
2020-02-09 13:52:32 +00:00
|
|
|
msg->rtm_family = (dst_net.len == 4) ? AF_INET : AF_INET6;
|
2008-09-25 07:56:58 +00:00
|
|
|
msg->rtm_dst_len = prefixlen;
|
|
|
|
msg->rtm_protocol = RTPROT_STATIC;
|
2020-02-09 13:52:32 +00:00
|
|
|
msg->rtm_type = pass ? RTN_THROW : RTN_UNICAST;
|
2008-09-25 07:56:58 +00:00
|
|
|
msg->rtm_scope = RT_SCOPE_UNIVERSE;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2019-09-24 10:59:00 +00:00
|
|
|
if (this->routing_table < 256)
|
|
|
|
{
|
|
|
|
msg->rtm_table = this->routing_table;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef HAVE_RTA_TABLE
|
|
|
|
chunk = chunk_from_thing(this->routing_table);
|
|
|
|
netlink_add_attribute(hdr, RTA_TABLE, chunk, sizeof(request));
|
|
|
|
#else
|
|
|
|
DBG1(DBG_KNL, "routing table IDs > 255 are not supported");
|
|
|
|
return FAILED;
|
|
|
|
#endif /* HAVE_RTA_TABLE */
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
|
|
|
|
|
2020-02-09 13:52:32 +00:00
|
|
|
/* only when installing regular routes do we need all the parameters,
|
|
|
|
* deletes are done by destination net (except if metrics are used, which
|
|
|
|
* we don't support), for throw routes we don't need any of them either */
|
|
|
|
if (nlmsg_type == RTM_NEWROUTE && !pass)
|
2014-08-04 13:35:18 +00:00
|
|
|
{
|
2020-02-09 13:52:32 +00:00
|
|
|
chunk = src_ip->get_address(src_ip);
|
|
|
|
netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
|
|
|
|
if (gateway && gateway->get_family(gateway) == src_ip->get_family(src_ip))
|
2014-08-04 13:57:46 +00:00
|
|
|
{
|
2020-02-09 13:52:32 +00:00
|
|
|
chunk = gateway->get_address(gateway);
|
|
|
|
netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
|
2014-08-04 13:57:46 +00:00
|
|
|
}
|
2020-02-09 13:52:32 +00:00
|
|
|
ifindex = get_interface_index(this, if_name);
|
|
|
|
chunk.ptr = (char*)&ifindex;
|
|
|
|
chunk.len = sizeof(ifindex);
|
|
|
|
netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
|
|
|
|
|
|
|
|
if (this->mtu || this->mss)
|
2014-08-04 13:57:46 +00:00
|
|
|
{
|
2020-02-09 13:52:32 +00:00
|
|
|
chunk = chunk_alloca(RTA_LENGTH((sizeof(struct rtattr) +
|
|
|
|
sizeof(uint32_t)) * 2));
|
|
|
|
chunk.len = 0;
|
|
|
|
rta = (struct rtattr*)chunk.ptr;
|
|
|
|
if (this->mtu)
|
|
|
|
{
|
|
|
|
rta->rta_type = RTAX_MTU;
|
|
|
|
rta->rta_len = RTA_LENGTH(sizeof(uint32_t));
|
|
|
|
memcpy(RTA_DATA(rta), &this->mtu, sizeof(uint32_t));
|
|
|
|
chunk.len = rta->rta_len;
|
|
|
|
}
|
|
|
|
if (this->mss)
|
|
|
|
{
|
|
|
|
rta = (struct rtattr*)(chunk.ptr + RTA_ALIGN(chunk.len));
|
|
|
|
rta->rta_type = RTAX_ADVMSS;
|
|
|
|
rta->rta_len = RTA_LENGTH(sizeof(uint32_t));
|
|
|
|
memcpy(RTA_DATA(rta), &this->mss, sizeof(uint32_t));
|
|
|
|
chunk.len = RTA_ALIGN(chunk.len) + rta->rta_len;
|
|
|
|
}
|
|
|
|
netlink_add_attribute(hdr, RTA_METRICS, chunk, sizeof(request));
|
2014-08-04 13:57:46 +00:00
|
|
|
}
|
2014-08-04 13:35:18 +00:00
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->socket->send_ack(this->socket, hdr);
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:15:24 +00:00
|
|
|
/**
|
|
|
|
* Helper struct used to check routes
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** the entry we look for */
|
|
|
|
route_entry_t route;
|
|
|
|
/** kernel interface */
|
|
|
|
private_kernel_netlink_net_t *this;
|
|
|
|
} route_entry_lookup_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a matching route entry has a VIP associated
|
|
|
|
*/
|
|
|
|
static bool route_with_vip(route_entry_lookup_t *a, route_entry_t *b)
|
|
|
|
{
|
|
|
|
if (chunk_equals(a->route.dst_net, b->dst_net) &&
|
|
|
|
a->route.prefixlen == b->prefixlen &&
|
|
|
|
is_known_vip(a->this, b->src_ip))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if there is any route entry with a matching destination
|
|
|
|
*/
|
|
|
|
static bool route_with_dst(route_entry_lookup_t *a, route_entry_t *b)
|
|
|
|
{
|
|
|
|
if (chunk_equals(a->route.dst_net, b->dst_net) &&
|
|
|
|
a->route.prefixlen == b->prefixlen)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, add_route, status_t,
|
2016-03-22 12:22:01 +00:00
|
|
|
private_kernel_netlink_net_t *this, chunk_t dst_net, uint8_t prefixlen,
|
2020-02-09 13:52:32 +00:00
|
|
|
host_t *gateway, host_t *src_ip, char *if_name, bool pass)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2011-12-20 13:30:10 +00:00
|
|
|
status_t status;
|
2018-04-19 16:15:24 +00:00
|
|
|
route_entry_t *found;
|
|
|
|
route_entry_lookup_t lookup = {
|
|
|
|
.route = {
|
|
|
|
.dst_net = dst_net,
|
|
|
|
.prefixlen = prefixlen,
|
|
|
|
.gateway = gateway,
|
|
|
|
.src_ip = src_ip,
|
|
|
|
.if_name = if_name,
|
2020-02-09 13:52:32 +00:00
|
|
|
.pass = pass,
|
2018-04-19 16:15:24 +00:00
|
|
|
},
|
|
|
|
.this = this,
|
2011-12-20 13:30:10 +00:00
|
|
|
};
|
|
|
|
|
2020-02-09 13:52:32 +00:00
|
|
|
if (!this->routing_table)
|
|
|
|
{ /* treat these as regular routes if installing in the main table */
|
|
|
|
pass = lookup.route.pass = FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->lock(this->routes_lock);
|
2020-04-24 11:41:53 +00:00
|
|
|
found = this->routes->ht.get(&this->routes->ht, &lookup.route);
|
2011-12-20 13:30:10 +00:00
|
|
|
if (found)
|
|
|
|
{
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->unlock(this->routes_lock);
|
2011-12-20 13:30:10 +00:00
|
|
|
return ALREADY_DONE;
|
|
|
|
}
|
2018-04-19 16:15:24 +00:00
|
|
|
|
|
|
|
/* don't replace the route if we already have one with a VIP installed,
|
|
|
|
* but keep track of it in case that other route is uninstalled */
|
|
|
|
this->lock->read_lock(this->lock);
|
|
|
|
if (!is_known_vip(this, src_ip))
|
|
|
|
{
|
|
|
|
found = this->routes->get_match(this->routes, &lookup,
|
|
|
|
(void*)route_with_vip);
|
|
|
|
}
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
status = SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
|
2020-02-09 13:52:32 +00:00
|
|
|
dst_net, prefixlen, gateway, src_ip, if_name,
|
|
|
|
pass);
|
2018-04-19 16:15:24 +00:00
|
|
|
}
|
2014-02-07 10:37:30 +00:00
|
|
|
if (status == SUCCESS)
|
|
|
|
{
|
2018-04-19 16:15:24 +00:00
|
|
|
found = route_entry_clone(&lookup.route);
|
2020-04-24 11:41:53 +00:00
|
|
|
this->routes->ht.put(&this->routes->ht, found, found);
|
2014-02-07 10:37:30 +00:00
|
|
|
}
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->unlock(this->routes_lock);
|
2011-12-20 13:30:10 +00:00
|
|
|
return status;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, del_route, status_t,
|
2016-03-22 12:22:01 +00:00
|
|
|
private_kernel_netlink_net_t *this, chunk_t dst_net, uint8_t prefixlen,
|
2020-02-09 13:52:32 +00:00
|
|
|
host_t *gateway, host_t *src_ip, char *if_name, bool pass)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2011-12-20 13:30:10 +00:00
|
|
|
status_t status;
|
2018-04-19 16:15:24 +00:00
|
|
|
route_entry_t *found;
|
|
|
|
route_entry_lookup_t lookup = {
|
|
|
|
.route = {
|
|
|
|
.dst_net = dst_net,
|
|
|
|
.prefixlen = prefixlen,
|
|
|
|
.gateway = gateway,
|
|
|
|
.src_ip = src_ip,
|
|
|
|
.if_name = if_name,
|
2020-02-09 13:52:32 +00:00
|
|
|
.pass = pass,
|
2018-04-19 16:15:24 +00:00
|
|
|
},
|
|
|
|
.this = this,
|
2011-12-20 13:30:10 +00:00
|
|
|
};
|
|
|
|
|
2020-02-09 13:52:32 +00:00
|
|
|
if (!this->routing_table)
|
|
|
|
{ /* treat these as regular routes if installing in the main table */
|
|
|
|
pass = lookup.route.pass = FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->lock(this->routes_lock);
|
2020-04-24 11:41:53 +00:00
|
|
|
found = this->routes->ht.remove(&this->routes->ht, &lookup.route);
|
2011-12-20 13:30:10 +00:00
|
|
|
if (!found)
|
|
|
|
{
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->unlock(this->routes_lock);
|
2011-12-20 13:30:10 +00:00
|
|
|
return NOT_FOUND;
|
|
|
|
}
|
|
|
|
route_entry_destroy(found);
|
2018-04-19 16:15:24 +00:00
|
|
|
|
|
|
|
/* check if there are any other routes for the same destination and if
|
|
|
|
* so update the route, otherwise uninstall it */
|
|
|
|
this->lock->read_lock(this->lock);
|
|
|
|
found = this->routes->get_match(this->routes, &lookup,
|
|
|
|
(void*)route_with_vip);
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
found = this->routes->get_match(this->routes, &lookup,
|
|
|
|
(void*)route_with_dst);
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
|
|
|
|
found->dst_net, found->prefixlen, found->gateway,
|
2020-02-09 13:52:32 +00:00
|
|
|
found->src_ip, found->if_name, found->pass);
|
2018-04-19 16:15:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
|
2020-02-09 13:52:32 +00:00
|
|
|
gateway, src_ip, if_name, pass);
|
2018-04-19 16:15:24 +00:00
|
|
|
}
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->unlock(this->routes_lock);
|
2011-12-20 13:30:10 +00:00
|
|
|
return status;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a list of local addresses.
|
|
|
|
*/
|
|
|
|
static status_t init_address_list(private_kernel_netlink_net_t *this)
|
|
|
|
{
|
2008-11-14 14:23:11 +00:00
|
|
|
netlink_buf_t request;
|
2008-09-25 07:56:58 +00:00
|
|
|
struct nlmsghdr *out, *current, *in;
|
|
|
|
struct rtgenmsg *msg;
|
|
|
|
size_t len;
|
2008-11-05 08:32:38 +00:00
|
|
|
enumerator_t *ifaces, *addrs;
|
2008-09-25 07:56:58 +00:00
|
|
|
iface_entry_t *iface;
|
|
|
|
addr_entry_t *addr;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-08-14 15:21:55 +00:00
|
|
|
DBG2(DBG_KNL, "known interfaces and IP addresses:");
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
memset(&request, 0, sizeof(request));
|
|
|
|
|
2014-07-09 13:47:24 +00:00
|
|
|
in = &request.hdr;
|
2008-09-25 07:56:58 +00:00
|
|
|
in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
|
|
|
|
in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
|
2014-07-09 13:53:14 +00:00
|
|
|
msg = NLMSG_DATA(in);
|
2008-09-25 07:56:58 +00:00
|
|
|
msg->rtgen_family = AF_UNSPEC;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/* get all links */
|
|
|
|
in->nlmsg_type = RTM_GETLINK;
|
|
|
|
if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
current = out;
|
|
|
|
while (NLMSG_OK(current, len))
|
|
|
|
{
|
|
|
|
switch (current->nlmsg_type)
|
|
|
|
{
|
|
|
|
case NLMSG_DONE:
|
|
|
|
break;
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
process_link(this, current, FALSE);
|
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
current = NLMSG_NEXT(current, len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(out);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
/* get all interface addresses */
|
|
|
|
in->nlmsg_type = RTM_GETADDR;
|
|
|
|
if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
current = out;
|
|
|
|
while (NLMSG_OK(current, len))
|
|
|
|
{
|
|
|
|
switch (current->nlmsg_type)
|
|
|
|
{
|
|
|
|
case NLMSG_DONE:
|
|
|
|
break;
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
process_addr(this, current, FALSE);
|
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
current = NLMSG_NEXT(current, len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(out);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2008-11-05 08:32:38 +00:00
|
|
|
ifaces = this->ifaces->create_enumerator(this->ifaces);
|
|
|
|
while (ifaces->enumerate(ifaces, &iface))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-09-14 14:43:08 +00:00
|
|
|
if (iface_entry_up_and_usable(iface))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-08-14 15:21:55 +00:00
|
|
|
DBG2(DBG_KNL, " %s", iface->ifname);
|
2008-11-05 08:32:38 +00:00
|
|
|
addrs = iface->addrs->create_enumerator(iface->addrs);
|
|
|
|
while (addrs->enumerate(addrs, (void**)&addr))
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-08-14 15:21:55 +00:00
|
|
|
DBG2(DBG_KNL, " %H", addr->ip);
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
addrs->destroy(addrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ifaces->destroy(ifaces);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create or delete a rule to use our routing table
|
|
|
|
*/
|
|
|
|
static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
|
2016-03-22 12:22:01 +00:00
|
|
|
int family, uint32_t table, uint32_t prio)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2008-11-14 14:23:11 +00:00
|
|
|
netlink_buf_t request;
|
2008-09-25 07:56:58 +00:00
|
|
|
struct nlmsghdr *hdr;
|
|
|
|
struct rtmsg *msg;
|
|
|
|
chunk_t chunk;
|
2013-08-13 14:53:06 +00:00
|
|
|
char *fwmark;
|
2008-09-25 07:56:58 +00:00
|
|
|
|
2009-09-04 11:46:09 +00:00
|
|
|
memset(&request, 0, sizeof(request));
|
2014-07-09 13:47:24 +00:00
|
|
|
hdr = &request.hdr;
|
2008-09-25 07:56:58 +00:00
|
|
|
hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
|
2009-09-04 11:46:09 +00:00
|
|
|
hdr->nlmsg_type = nlmsg_type;
|
2008-09-25 07:56:58 +00:00
|
|
|
if (nlmsg_type == RTM_NEWRULE)
|
|
|
|
{
|
|
|
|
hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
|
|
|
|
}
|
|
|
|
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
|
2014-07-09 13:53:14 +00:00
|
|
|
msg = NLMSG_DATA(hdr);
|
2009-11-24 13:10:18 +00:00
|
|
|
msg->rtm_family = family;
|
2008-09-25 07:56:58 +00:00
|
|
|
msg->rtm_protocol = RTPROT_BOOT;
|
|
|
|
msg->rtm_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
msg->rtm_type = RTN_UNICAST;
|
|
|
|
|
2019-09-24 10:59:00 +00:00
|
|
|
if (this->routing_table < 256)
|
|
|
|
{
|
|
|
|
msg->rtm_table = table;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LINUX_FIB_RULES_H
|
|
|
|
chunk = chunk_from_thing(table);
|
|
|
|
netlink_add_attribute(hdr, FRA_TABLE, chunk, sizeof(request));
|
|
|
|
#else
|
|
|
|
DBG1(DBG_KNL, "routing table IDs > 255 are not supported");
|
|
|
|
return FAILED;
|
|
|
|
#endif /* HAVE_LINUX_FIB_RULES_H */
|
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
chunk = chunk_from_thing(prio);
|
|
|
|
netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
|
|
|
|
|
2013-08-13 14:53:06 +00:00
|
|
|
fwmark = lib->settings->get_str(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.plugins.kernel-netlink.fwmark", NULL, lib->ns);
|
2013-08-13 14:53:06 +00:00
|
|
|
if (fwmark)
|
|
|
|
{
|
2013-10-18 07:38:01 +00:00
|
|
|
#ifdef HAVE_LINUX_FIB_RULES_H
|
|
|
|
mark_t mark;
|
|
|
|
|
2013-08-13 14:53:06 +00:00
|
|
|
if (fwmark[0] == '!')
|
|
|
|
{
|
|
|
|
msg->rtm_flags |= FIB_RULE_INVERT;
|
|
|
|
fwmark++;
|
|
|
|
}
|
2018-05-14 11:42:53 +00:00
|
|
|
if (mark_from_string(fwmark, MARK_OP_NONE, &mark))
|
2013-08-13 14:53:06 +00:00
|
|
|
{
|
|
|
|
chunk = chunk_from_thing(mark.value);
|
|
|
|
netlink_add_attribute(hdr, FRA_FWMARK, chunk, sizeof(request));
|
|
|
|
chunk = chunk_from_thing(mark.mask);
|
|
|
|
netlink_add_attribute(hdr, FRA_FWMASK, chunk, sizeof(request));
|
2015-08-05 14:51:38 +00:00
|
|
|
if (msg->rtm_flags & FIB_RULE_INVERT)
|
|
|
|
{
|
|
|
|
this->routing_mark = mark;
|
|
|
|
}
|
2013-08-13 14:53:06 +00:00
|
|
|
}
|
2013-10-18 07:38:01 +00:00
|
|
|
#else
|
|
|
|
DBG1(DBG_KNL, "setting firewall mark on routing rule is not supported");
|
2019-09-24 10:59:00 +00:00
|
|
|
#endif /* HAVE_LINUX_FIB_RULES_H */
|
2013-08-13 14:53:06 +00:00
|
|
|
}
|
2008-09-25 07:56:58 +00:00
|
|
|
return this->socket->send_ack(this->socket, hdr);
|
|
|
|
}
|
|
|
|
|
2012-06-18 10:01:10 +00:00
|
|
|
/**
|
|
|
|
* check for kernel features (currently only via version number)
|
|
|
|
*/
|
|
|
|
static void check_kernel_features(private_kernel_netlink_net_t *this)
|
|
|
|
{
|
|
|
|
struct utsname utsname;
|
|
|
|
int a, b, c;
|
|
|
|
|
|
|
|
if (uname(&utsname) == 0)
|
|
|
|
{
|
|
|
|
switch(sscanf(utsname.release, "%d.%d.%d", &a, &b, &c))
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
if (a == 2)
|
|
|
|
{
|
2015-08-05 14:51:38 +00:00
|
|
|
if (b == 6 && c >= 36)
|
|
|
|
{
|
|
|
|
this->rta_mark = TRUE;
|
|
|
|
}
|
2012-06-18 10:01:10 +00:00
|
|
|
DBG2(DBG_KNL, "detected Linux %d.%d.%d, no support for "
|
|
|
|
"RTA_PREFSRC for IPv6 routes", a, b, c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall-through */
|
|
|
|
case 2:
|
|
|
|
/* only 3.x+ uses two part version numbers */
|
|
|
|
this->rta_prefsrc_for_ipv6 = TRUE;
|
2015-08-05 14:51:38 +00:00
|
|
|
this->rta_mark = TRUE;
|
2012-06-18 10:01:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
/**
|
|
|
|
* Destroy an address to iface map
|
|
|
|
*/
|
2020-04-24 11:41:53 +00:00
|
|
|
static void addr_map_destroy(hashlist_t *map)
|
2012-09-19 17:10:23 +00:00
|
|
|
{
|
2020-04-24 11:41:53 +00:00
|
|
|
map->ht.destroy_function(&map->ht, (void*)free);
|
2012-09-19 17:10:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
METHOD(kernel_net_t, destroy, void,
|
|
|
|
private_kernel_netlink_net_t *this)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2011-12-20 13:30:10 +00:00
|
|
|
enumerator_t *enumerator;
|
|
|
|
route_entry_t *route;
|
|
|
|
|
2020-11-03 09:38:38 +00:00
|
|
|
if (this->routing_table && this->socket)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2009-11-24 13:10:18 +00:00
|
|
|
manage_rule(this, RTM_DELRULE, AF_INET, this->routing_table,
|
|
|
|
this->routing_table_prio);
|
|
|
|
manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
|
2008-09-25 07:56:58 +00:00
|
|
|
this->routing_table_prio);
|
|
|
|
}
|
2010-02-23 16:49:34 +00:00
|
|
|
if (this->socket_events > 0)
|
|
|
|
{
|
2013-07-01 13:42:22 +00:00
|
|
|
lib->watcher->remove(lib->watcher, this->socket_events);
|
2010-02-23 16:49:34 +00:00
|
|
|
close(this->socket_events);
|
|
|
|
}
|
2020-04-24 11:41:53 +00:00
|
|
|
enumerator = this->routes->ht.create_enumerator(&this->routes->ht);
|
2011-12-20 13:30:10 +00:00
|
|
|
while (enumerator->enumerate(enumerator, NULL, (void**)&route))
|
|
|
|
{
|
|
|
|
manage_srcroute(this, RTM_DELROUTE, 0, route->dst_net, route->prefixlen,
|
2020-02-09 13:52:32 +00:00
|
|
|
route->gateway, route->src_ip, route->if_name,
|
|
|
|
route->pass);
|
2011-12-20 13:30:10 +00:00
|
|
|
route_entry_destroy(route);
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->routes->destroy(this->routes);
|
2012-09-20 16:06:01 +00:00
|
|
|
this->routes_lock->destroy(this->routes_lock);
|
2012-05-21 11:19:57 +00:00
|
|
|
DESTROY_IF(this->socket);
|
2011-12-20 13:30:10 +00:00
|
|
|
|
2011-12-20 13:59:21 +00:00
|
|
|
net_changes_clear(this);
|
|
|
|
this->net_changes->destroy(this->net_changes);
|
|
|
|
this->net_changes_lock->destroy(this->net_changes_lock);
|
|
|
|
|
2012-09-19 17:10:23 +00:00
|
|
|
addr_map_destroy(this->addrs);
|
|
|
|
addr_map_destroy(this->vips);
|
2012-09-17 16:09:51 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
|
2010-03-12 08:37:51 +00:00
|
|
|
this->rt_exclude->destroy(this->rt_exclude);
|
2012-09-20 09:58:52 +00:00
|
|
|
this->roam_lock->destroy(this->roam_lock);
|
2008-11-05 11:29:56 +00:00
|
|
|
this->condvar->destroy(this->condvar);
|
2012-09-20 16:21:42 +00:00
|
|
|
this->lock->destroy(this->lock);
|
2008-09-25 07:56:58 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
kernel_netlink_net_t *kernel_netlink_net_create()
|
|
|
|
{
|
2011-02-17 09:31:15 +00:00
|
|
|
private_kernel_netlink_net_t *this;
|
2010-03-12 08:37:51 +00:00
|
|
|
enumerator_t *enumerator;
|
2012-06-07 10:00:37 +00:00
|
|
|
bool register_for_events = TRUE;
|
2010-03-12 08:37:51 +00:00
|
|
|
char *exclude;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-02-17 09:31:15 +00:00
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.interface = {
|
|
|
|
.get_interface = _get_interface_name,
|
|
|
|
.create_address_enumerator = _create_address_enumerator,
|
2016-10-12 07:52:45 +00:00
|
|
|
.create_local_subnet_enumerator = _create_local_subnet_enumerator,
|
2011-02-17 09:31:15 +00:00
|
|
|
.get_source_addr = _get_source_addr,
|
|
|
|
.get_nexthop = _get_nexthop,
|
|
|
|
.add_ip = _add_ip,
|
|
|
|
.del_ip = _del_ip,
|
|
|
|
.add_route = _add_route,
|
|
|
|
.del_route = _del_route,
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
},
|
2014-07-16 10:38:30 +00:00
|
|
|
.socket = netlink_socket_create(NETLINK_ROUTE, rt_msg_names,
|
|
|
|
lib->settings->get_bool(lib->settings,
|
|
|
|
"%s.plugins.kernel-netlink.parallel_route", FALSE, lib->ns)),
|
2011-02-17 09:31:15 +00:00
|
|
|
.rt_exclude = linked_list_create(),
|
2020-04-24 11:41:53 +00:00
|
|
|
.routes = hashlist_create((hashtable_hash_t)route_entry_hash,
|
|
|
|
(hashtable_equals_t)route_entry_equals, 16),
|
2011-12-20 13:59:21 +00:00
|
|
|
.net_changes = hashtable_create(
|
|
|
|
(hashtable_hash_t)net_change_hash,
|
|
|
|
(hashtable_equals_t)net_change_equals, 16),
|
2020-04-24 11:41:53 +00:00
|
|
|
.addrs = hashlist_create(
|
2012-09-17 16:09:51 +00:00
|
|
|
(hashtable_hash_t)addr_map_entry_hash,
|
|
|
|
(hashtable_equals_t)addr_map_entry_equals, 16),
|
2020-04-24 11:41:53 +00:00
|
|
|
.vips = hashlist_create((hashtable_hash_t)addr_map_entry_hash,
|
2012-09-19 17:10:23 +00:00
|
|
|
(hashtable_equals_t)addr_map_entry_equals, 16),
|
2012-09-20 16:06:01 +00:00
|
|
|
.routes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
|
2011-12-20 13:59:21 +00:00
|
|
|
.net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
|
2011-02-17 09:31:15 +00:00
|
|
|
.ifaces = linked_list_create(),
|
2012-09-20 16:21:42 +00:00
|
|
|
.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
|
|
|
|
.condvar = rwlock_condvar_create(),
|
2012-09-20 09:58:52 +00:00
|
|
|
.roam_lock = spinlock_create(),
|
2011-02-17 09:31:15 +00:00
|
|
|
.routing_table = lib->settings->get_int(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.routing_table", ROUTING_TABLE, lib->ns),
|
2011-02-17 09:31:15 +00:00
|
|
|
.routing_table_prio = lib->settings->get_int(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.routing_table_prio", ROUTING_TABLE_PRIO, lib->ns),
|
2011-02-17 09:31:15 +00:00
|
|
|
.process_route = lib->settings->get_bool(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.process_route", TRUE, lib->ns),
|
2017-01-19 10:03:55 +00:00
|
|
|
.install_routes = lib->settings->get_bool(lib->settings,
|
|
|
|
"%s.install_routes", TRUE, lib->ns),
|
2011-02-17 09:31:15 +00:00
|
|
|
.install_virtual_ip = lib->settings->get_bool(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.install_virtual_ip", TRUE, lib->ns),
|
2012-09-20 09:07:15 +00:00
|
|
|
.install_virtual_ip_on = lib->settings->get_str(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.install_virtual_ip_on", NULL, lib->ns),
|
2014-03-14 14:41:52 +00:00
|
|
|
.prefer_temporary_addrs = lib->settings->get_bool(lib->settings,
|
|
|
|
"%s.prefer_temporary_addrs", FALSE, lib->ns),
|
2013-04-16 11:54:25 +00:00
|
|
|
.roam_events = lib->settings->get_bool(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.plugins.kernel-netlink.roam_events", TRUE, lib->ns),
|
2018-01-29 14:26:17 +00:00
|
|
|
.process_rules = lib->settings->get_bool(lib->settings,
|
|
|
|
"%s.plugins.kernel-netlink.process_rules", FALSE, lib->ns),
|
2014-08-04 13:35:18 +00:00
|
|
|
.mtu = lib->settings->get_int(lib->settings,
|
|
|
|
"%s.plugins.kernel-netlink.mtu", 0, lib->ns),
|
2014-08-04 13:57:46 +00:00
|
|
|
.mss = lib->settings->get_int(lib->settings,
|
|
|
|
"%s.plugins.kernel-netlink.mss", 0, lib->ns),
|
2011-02-17 09:31:15 +00:00
|
|
|
);
|
2011-12-20 13:59:21 +00:00
|
|
|
timerclear(&this->last_route_reinstall);
|
2012-09-20 09:58:52 +00:00
|
|
|
timerclear(&this->next_roam);
|
2011-02-17 09:31:15 +00:00
|
|
|
|
2012-06-18 10:01:10 +00:00
|
|
|
check_kernel_features(this);
|
|
|
|
|
2020-11-17 16:55:24 +00:00
|
|
|
if (!this->socket)
|
|
|
|
{
|
|
|
|
destroy(this);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-22 13:35:41 +00:00
|
|
|
if (streq(lib->ns, "starter"))
|
2012-06-07 10:00:37 +00:00
|
|
|
{ /* starter has no threads, so we do not register for kernel events */
|
|
|
|
register_for_events = FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:37:51 +00:00
|
|
|
exclude = lib->settings->get_str(lib->settings,
|
2014-01-22 13:35:41 +00:00
|
|
|
"%s.ignore_routing_tables", NULL, lib->ns);
|
2010-03-12 08:37:51 +00:00
|
|
|
if (exclude)
|
|
|
|
{
|
|
|
|
char *token;
|
|
|
|
uintptr_t table;
|
|
|
|
|
|
|
|
enumerator = enumerator_create_token(exclude, " ", " ");
|
|
|
|
while (enumerator->enumerate(enumerator, &token))
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
table = strtoul(token, NULL, 10);
|
|
|
|
|
|
|
|
if (errno == 0)
|
|
|
|
{
|
|
|
|
this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
}
|
|
|
|
|
2012-06-07 10:00:37 +00:00
|
|
|
if (register_for_events)
|
2008-09-25 07:56:58 +00:00
|
|
|
{
|
2012-06-07 10:00:37 +00:00
|
|
|
struct sockaddr_nl addr;
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.nl_family = AF_NETLINK;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-06-07 10:00:37 +00:00
|
|
|
/* create and bind RT socket for events (address/interface/route changes) */
|
|
|
|
this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
if (this->socket_events < 0)
|
|
|
|
{
|
2016-05-27 11:43:41 +00:00
|
|
|
DBG1(DBG_KNL, "unable to create RT event socket: %s (%d)",
|
|
|
|
strerror(errno), errno);
|
2012-06-07 10:00:37 +00:00
|
|
|
destroy(this);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-29 14:26:17 +00:00
|
|
|
addr.nl_groups = nl_group(RTNLGRP_IPV4_IFADDR) |
|
|
|
|
nl_group(RTNLGRP_IPV6_IFADDR) |
|
|
|
|
nl_group(RTNLGRP_LINK);
|
|
|
|
if (this->process_route)
|
|
|
|
{
|
|
|
|
addr.nl_groups |= nl_group(RTNLGRP_IPV4_ROUTE) |
|
|
|
|
nl_group(RTNLGRP_IPV6_ROUTE);
|
|
|
|
}
|
|
|
|
if (this->process_rules)
|
|
|
|
{
|
|
|
|
addr.nl_groups |= nl_group(RTNLGRP_IPV4_RULE) |
|
|
|
|
nl_group(RTNLGRP_IPV6_RULE);
|
|
|
|
}
|
2012-06-07 10:00:37 +00:00
|
|
|
if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
|
|
|
|
{
|
2016-05-27 11:43:41 +00:00
|
|
|
DBG1(DBG_KNL, "unable to bind RT event socket: %s (%d)",
|
|
|
|
strerror(errno), errno);
|
2012-06-07 10:00:37 +00:00
|
|
|
destroy(this);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-01 13:42:22 +00:00
|
|
|
lib->watcher->add(lib->watcher, this->socket_events, WATCHER_READ,
|
|
|
|
(watcher_cb_t)receive_events, this);
|
2012-06-07 10:00:37 +00:00
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
if (init_address_list(this) != SUCCESS)
|
|
|
|
{
|
2010-02-23 16:49:34 +00:00
|
|
|
DBG1(DBG_KNL, "unable to get interface list");
|
|
|
|
destroy(this);
|
|
|
|
return NULL;
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
if (this->routing_table)
|
|
|
|
{
|
2009-11-24 13:10:18 +00:00
|
|
|
if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
|
|
|
|
this->routing_table_prio) != SUCCESS)
|
|
|
|
{
|
|
|
|
DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
|
|
|
|
}
|
|
|
|
if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
|
2008-09-25 07:56:58 +00:00
|
|
|
this->routing_table_prio) != SUCCESS)
|
|
|
|
{
|
2009-11-24 13:10:18 +00:00
|
|
|
DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
|
2008-09-25 07:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-09-25 07:56:58 +00:00
|
|
|
return &this->public;
|
|
|
|
}
|