2019-06-26 00:20:52 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
2019-06-26 00:20:56 +02:00
|
|
|
#include <arpa/inet.h>
|
2019-08-02 20:33:43 +02:00
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <netinet/in.h>
|
2019-06-26 00:20:52 +02:00
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "src/iwd.h"
|
2019-11-07 23:33:51 +01:00
|
|
|
#include "src/module.h"
|
2019-06-26 00:20:56 +02:00
|
|
|
#include "src/netdev.h"
|
|
|
|
#include "src/station.h"
|
2019-06-26 00:21:00 +02:00
|
|
|
#include "src/common.h"
|
|
|
|
#include "src/network.h"
|
2019-06-26 00:20:54 +02:00
|
|
|
#include "src/rtnlutil.h"
|
2019-07-03 01:14:50 +02:00
|
|
|
#include "src/resolve.h"
|
2019-06-26 00:20:52 +02:00
|
|
|
#include "src/netconfig.h"
|
|
|
|
|
|
|
|
struct netconfig {
|
|
|
|
uint32_t ifindex;
|
2019-06-26 00:20:56 +02:00
|
|
|
struct l_dhcp_client *dhcp_client;
|
2019-06-26 00:20:54 +02:00
|
|
|
struct l_queue *ifaddr_list;
|
2019-07-31 11:38:54 +02:00
|
|
|
uint8_t rtm_protocol;
|
2019-10-02 01:44:23 +02:00
|
|
|
uint8_t rtm_v6_protocol;
|
2019-09-30 18:35:51 +02:00
|
|
|
|
|
|
|
const struct l_settings *active_settings;
|
2019-10-04 21:48:57 +02:00
|
|
|
|
|
|
|
netconfig_notify_func_t notify;
|
|
|
|
void *user_data;
|
2019-06-26 00:20:52 +02:00
|
|
|
};
|
|
|
|
|
2019-06-26 00:20:54 +02:00
|
|
|
struct netconfig_ifaddr {
|
|
|
|
uint8_t family;
|
|
|
|
uint8_t prefix_len;
|
|
|
|
char *ip;
|
|
|
|
char *broadcast;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct l_netlink *rtnl;
|
2019-06-26 00:20:52 +02:00
|
|
|
static struct l_queue *netconfig_list;
|
|
|
|
|
2019-07-31 19:38:40 +02:00
|
|
|
/*
|
|
|
|
* Routing priority offset, configurable in main.conf. The route with lower
|
|
|
|
* priority offset is preferred.
|
|
|
|
*/
|
|
|
|
static uint32_t ROUTE_PRIORITY_OFFSET;
|
|
|
|
|
2019-06-26 00:20:54 +02:00
|
|
|
static void do_debug(const char *str, void *user_data)
|
|
|
|
{
|
|
|
|
const char *prefix = user_data;
|
|
|
|
|
|
|
|
l_info("%s%s", prefix, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ifaddr_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr = data;
|
|
|
|
|
|
|
|
l_free(ifaddr->ip);
|
|
|
|
l_free(ifaddr->broadcast);
|
|
|
|
|
|
|
|
l_free(ifaddr);
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:52:16 +02:00
|
|
|
static void netconfig_free(void *data)
|
2019-06-26 00:20:52 +02:00
|
|
|
{
|
|
|
|
struct netconfig *netconfig = data;
|
|
|
|
|
2019-06-26 00:20:56 +02:00
|
|
|
l_dhcp_client_destroy(netconfig->dhcp_client);
|
|
|
|
|
2019-06-26 00:20:54 +02:00
|
|
|
l_queue_destroy(netconfig->ifaddr_list, netconfig_ifaddr_destroy);
|
|
|
|
|
2019-06-26 00:20:52 +02:00
|
|
|
l_free(netconfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct netconfig *netconfig_find(uint32_t ifindex)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(netconfig_list); entry;
|
|
|
|
entry = entry->next) {
|
|
|
|
struct netconfig *netconfig = entry->data;
|
|
|
|
|
|
|
|
if (netconfig->ifindex != ifindex)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return netconfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:05 +02:00
|
|
|
static struct netconfig_ifaddr *netconfig_ipv4_get_ifaddr(
|
|
|
|
struct netconfig *netconfig,
|
|
|
|
uint8_t proto)
|
|
|
|
{
|
|
|
|
const struct l_dhcp_lease *lease;
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
struct in_addr in_addr;
|
|
|
|
char *netmask;
|
|
|
|
char *ip;
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case RTPROT_STATIC:
|
|
|
|
|
2019-09-30 18:35:53 +02:00
|
|
|
ip = l_settings_get_string(netconfig->active_settings, "IPv4",
|
2019-12-11 00:38:32 +01:00
|
|
|
"Address");
|
|
|
|
if (!ip) {
|
|
|
|
ip = l_settings_get_string(netconfig->active_settings,
|
|
|
|
"IPv4", "ip");
|
|
|
|
if (!ip)
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-31 03:16:05 +02:00
|
|
|
|
|
|
|
ifaddr = l_new(struct netconfig_ifaddr, 1);
|
|
|
|
ifaddr->ip = ip;
|
|
|
|
|
2019-09-30 18:35:53 +02:00
|
|
|
netmask = l_settings_get_string(netconfig->active_settings,
|
2019-12-11 00:38:32 +01:00
|
|
|
"IPv4", "Netmask");
|
|
|
|
if (!netmask)
|
|
|
|
netmask = l_settings_get_string(
|
|
|
|
netconfig->active_settings,
|
|
|
|
"IPv4", "netmask");
|
|
|
|
|
2019-07-31 03:16:05 +02:00
|
|
|
if (netmask && inet_pton(AF_INET, netmask, &in_addr) > 0)
|
|
|
|
ifaddr->prefix_len = __builtin_popcountl(
|
|
|
|
L_BE32_TO_CPU(in_addr.s_addr));
|
|
|
|
else
|
|
|
|
ifaddr->prefix_len = 24;
|
|
|
|
|
|
|
|
l_free(netmask);
|
|
|
|
|
2019-09-30 18:35:53 +02:00
|
|
|
ifaddr->broadcast =
|
|
|
|
l_settings_get_string(netconfig->active_settings,
|
2019-12-11 00:38:32 +01:00
|
|
|
"IPv4", "Broadcast");
|
|
|
|
if (!ifaddr->broadcast)
|
|
|
|
ifaddr->broadcast =
|
|
|
|
l_settings_get_string(
|
|
|
|
netconfig->active_settings,
|
|
|
|
"IPv4", "broadcast");
|
|
|
|
|
2019-07-31 03:16:05 +02:00
|
|
|
ifaddr->family = AF_INET;
|
|
|
|
|
|
|
|
return ifaddr;
|
|
|
|
|
|
|
|
case RTPROT_DHCP:
|
|
|
|
lease = l_dhcp_client_get_lease(netconfig->dhcp_client);
|
|
|
|
if (!lease)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ip = l_dhcp_lease_get_address(lease);
|
|
|
|
if (!ip)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ifaddr = l_new(struct netconfig_ifaddr, 1);
|
|
|
|
ifaddr->ip = ip;
|
|
|
|
|
|
|
|
netmask = l_dhcp_lease_get_netmask(lease);
|
|
|
|
|
|
|
|
if (netmask && inet_pton(AF_INET, netmask, &in_addr) > 0)
|
|
|
|
ifaddr->prefix_len = __builtin_popcountl(
|
|
|
|
L_BE32_TO_CPU(in_addr.s_addr));
|
|
|
|
else
|
|
|
|
ifaddr->prefix_len = 24;
|
|
|
|
|
|
|
|
l_free(netmask);
|
|
|
|
|
|
|
|
ifaddr->broadcast = l_dhcp_lease_get_broadcast(lease);
|
|
|
|
ifaddr->family = AF_INET;
|
|
|
|
|
|
|
|
return ifaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:38:54 +02:00
|
|
|
static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig)
|
2019-07-31 03:16:12 +02:00
|
|
|
{
|
|
|
|
const struct l_dhcp_lease *lease;
|
2019-12-11 00:38:33 +01:00
|
|
|
char *gateway;
|
2019-07-31 03:16:12 +02:00
|
|
|
|
2019-07-31 11:38:54 +02:00
|
|
|
switch (netconfig->rtm_protocol) {
|
2019-07-31 03:16:12 +02:00
|
|
|
case RTPROT_STATIC:
|
2019-12-11 00:38:33 +01:00
|
|
|
gateway = l_settings_get_string(netconfig->active_settings,
|
|
|
|
"IPv4", "Gateway");
|
|
|
|
if (!gateway)
|
|
|
|
gateway = l_settings_get_string(
|
|
|
|
netconfig->active_settings,
|
|
|
|
"IPv4", "gateway");
|
2019-07-31 03:16:12 +02:00
|
|
|
|
2019-12-11 00:38:33 +01:00
|
|
|
return gateway;
|
2019-07-31 03:16:12 +02:00
|
|
|
|
|
|
|
case RTPROT_DHCP:
|
|
|
|
lease = l_dhcp_client_get_lease(netconfig->dhcp_client);
|
|
|
|
if (!lease)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return l_dhcp_lease_get_gateway(lease);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:10 +02:00
|
|
|
static char **netconfig_ipv4_get_dns(struct netconfig *netconfig, uint8_t proto)
|
|
|
|
{
|
|
|
|
const struct l_dhcp_lease *lease;
|
2019-10-03 22:48:50 +02:00
|
|
|
struct in_addr in_addr;
|
|
|
|
char **dns_list;
|
2019-07-31 03:16:10 +02:00
|
|
|
|
2019-12-11 00:38:34 +01:00
|
|
|
dns_list = l_settings_get_string_list(netconfig->active_settings,
|
|
|
|
"IPv4", "DNS", ' ');
|
|
|
|
if (!dns_list)
|
|
|
|
dns_list = l_settings_get_string_list(
|
|
|
|
netconfig->active_settings,
|
|
|
|
"IPv4", "dns", ' ');
|
|
|
|
|
2019-10-03 22:48:50 +02:00
|
|
|
if (dns_list && *dns_list) {
|
2019-12-11 00:38:34 +01:00
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (p = dns_list; *p; p++) {
|
2019-10-03 22:48:50 +02:00
|
|
|
if (inet_pton(AF_INET, *p, &in_addr) == 1)
|
|
|
|
continue;
|
2019-07-31 03:16:10 +02:00
|
|
|
|
2019-10-03 22:48:50 +02:00
|
|
|
l_error("netconfig: Invalid IPv4 DNS address '%s' is "
|
|
|
|
"provided in network configuration file.", *p);
|
|
|
|
|
|
|
|
l_strv_free(dns_list);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow to override the DHCP DNSs with static addressing. */
|
|
|
|
return dns_list;
|
|
|
|
} else if (dns_list) {
|
|
|
|
l_error("netconfig: No IPv4 DNS address is provided in network "
|
|
|
|
"configuration file.");
|
|
|
|
|
|
|
|
l_strv_free(dns_list);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proto == RTPROT_DHCP) {
|
2019-07-31 03:16:10 +02:00
|
|
|
lease = l_dhcp_client_get_lease(netconfig->dhcp_client);
|
|
|
|
if (!lease)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return l_dhcp_lease_get_dns(lease);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
static struct netconfig_ifaddr *netconfig_ipv6_get_ifaddr(
|
|
|
|
struct netconfig *netconfig,
|
|
|
|
uint8_t proto)
|
|
|
|
{
|
|
|
|
struct in6_addr in6_addr;
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
char *ip;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case RTPROT_STATIC:
|
|
|
|
ip = l_settings_get_string(netconfig->active_settings, "IPv6",
|
2019-12-11 00:38:35 +01:00
|
|
|
"Address");
|
|
|
|
if (!ip) {
|
|
|
|
ip = l_settings_get_string(netconfig->active_settings,
|
|
|
|
"IPv6", "ip");
|
|
|
|
if (!ip)
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-10-02 01:44:23 +02:00
|
|
|
|
|
|
|
ifaddr = l_new(struct netconfig_ifaddr, 1);
|
|
|
|
ifaddr->ip = ip;
|
|
|
|
|
|
|
|
p = strrchr(ifaddr->ip, '/');
|
|
|
|
if (!p)
|
|
|
|
goto no_prefix_len;
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, ifaddr->ip, &in6_addr) < 1) {
|
|
|
|
l_error("netconfig: Invalid IPv6 address %s is "
|
|
|
|
"provided in network configuration file.",
|
|
|
|
ifaddr->ip);
|
|
|
|
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*++p == '\0')
|
|
|
|
goto no_prefix_len;
|
|
|
|
|
|
|
|
ifaddr->prefix_len = strtoul(p, NULL, 10);
|
|
|
|
|
|
|
|
if (!unlikely(errno == EINVAL || errno == ERANGE ||
|
|
|
|
!ifaddr->prefix_len ||
|
|
|
|
ifaddr->prefix_len > 128))
|
|
|
|
goto proceed;
|
|
|
|
|
|
|
|
no_prefix_len:
|
|
|
|
ifaddr->prefix_len = 128;
|
|
|
|
proceed:
|
|
|
|
ifaddr->family = AF_INET6;
|
|
|
|
|
|
|
|
return ifaddr;
|
|
|
|
|
|
|
|
case RTPROT_DHCP:
|
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:24 +02:00
|
|
|
static char *netconfig_ipv6_get_gateway(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
struct in6_addr in6_addr;
|
|
|
|
char *gateway;
|
|
|
|
|
|
|
|
switch (netconfig->rtm_v6_protocol) {
|
|
|
|
case RTPROT_STATIC:
|
|
|
|
gateway = l_settings_get_string(netconfig->active_settings,
|
2019-12-11 00:38:36 +01:00
|
|
|
"IPv6", "Gateway");
|
|
|
|
if (!gateway)
|
|
|
|
gateway = l_settings_get_string(
|
|
|
|
netconfig->active_settings,
|
|
|
|
"IPv6", "gateway");
|
2019-10-02 01:44:24 +02:00
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, gateway, &in6_addr) < 1) {
|
|
|
|
l_error("netconfig: Invalid IPv6 gateway address %s is "
|
|
|
|
"provided in network configuration file.",
|
|
|
|
gateway);
|
|
|
|
|
|
|
|
l_free(gateway);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gateway;
|
|
|
|
|
|
|
|
case RTPROT_DHCP:
|
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:25 +02:00
|
|
|
static char **netconfig_ipv6_get_dns(struct netconfig *netconfig, uint8_t proto)
|
|
|
|
{
|
|
|
|
struct in6_addr in6_addr;
|
|
|
|
char **dns_list;
|
|
|
|
|
2019-12-11 00:38:37 +01:00
|
|
|
dns_list = l_settings_get_string_list(netconfig->active_settings,
|
|
|
|
"IPv6", "DNS", ' ');
|
|
|
|
|
|
|
|
if (!dns_list)
|
|
|
|
dns_list = l_settings_get_string_list(
|
|
|
|
netconfig->active_settings,
|
|
|
|
"IPv6", "dns", ' ');
|
|
|
|
|
2019-10-03 22:48:51 +02:00
|
|
|
if (dns_list && *dns_list) {
|
2019-12-11 00:38:37 +01:00
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (p = dns_list; *p; p++) {
|
2019-10-02 01:44:25 +02:00
|
|
|
if (inet_pton(AF_INET6, *p, &in6_addr) == 1)
|
|
|
|
continue;
|
|
|
|
|
2019-10-03 22:48:51 +02:00
|
|
|
l_error("netconfig: Invalid IPv6 DNS address '%s' is "
|
2019-10-02 01:44:25 +02:00
|
|
|
"provided in network configuration file.", *p);
|
|
|
|
|
|
|
|
l_strv_free(dns_list);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:48:51 +02:00
|
|
|
/* Allow to override the DHCP DNSs with static addressing. */
|
2019-10-02 01:44:25 +02:00
|
|
|
return dns_list;
|
2019-10-03 22:48:51 +02:00
|
|
|
} else if (dns_list) {
|
|
|
|
l_error("netconfig: No IPv6 DNS address is provided in network "
|
|
|
|
"configuration file.");
|
2019-10-02 01:44:25 +02:00
|
|
|
|
2019-10-03 22:48:51 +02:00
|
|
|
l_strv_free(dns_list);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proto == RTPROT_DHCP) {
|
2019-10-02 01:44:25 +02:00
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:21 +02:00
|
|
|
static bool netconfig_ifaddr_match(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct netconfig_ifaddr *entry = a;
|
|
|
|
const struct netconfig_ifaddr *query = b;
|
|
|
|
|
|
|
|
if (entry->family != query->family)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (entry->prefix_len != query->prefix_len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strcmp(entry->ip, query->ip))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:20:54 +02:00
|
|
|
static struct netconfig_ifaddr *netconfig_ifaddr_find(
|
|
|
|
const struct netconfig *netconfig,
|
|
|
|
uint8_t family, uint8_t prefix_len,
|
|
|
|
const char *ip)
|
|
|
|
{
|
|
|
|
const struct l_queue_entry *entry;
|
|
|
|
|
|
|
|
for (entry = l_queue_get_entries(netconfig->ifaddr_list); entry;
|
|
|
|
entry = entry->next) {
|
|
|
|
struct netconfig_ifaddr *ifaddr = entry->data;
|
|
|
|
|
|
|
|
if (ifaddr->family != family)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ifaddr->prefix_len != prefix_len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(ifaddr->ip, ip))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return ifaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ifaddr_added(struct netconfig *netconfig,
|
2019-07-20 14:02:23 +02:00
|
|
|
const struct ifaddrmsg *ifa,
|
|
|
|
uint32_t len)
|
2019-06-26 00:20:54 +02:00
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
char *label;
|
|
|
|
|
|
|
|
ifaddr = l_new(struct netconfig_ifaddr, 1);
|
|
|
|
ifaddr->family = ifa->ifa_family;
|
|
|
|
ifaddr->prefix_len = ifa->ifa_prefixlen;
|
|
|
|
|
2019-07-20 14:02:23 +02:00
|
|
|
rtnl_ifaddr_extract(ifa, len, &label, &ifaddr->ip,
|
2019-06-26 00:20:54 +02:00
|
|
|
&ifaddr->broadcast);
|
|
|
|
|
|
|
|
l_debug("%s: ifaddr %s/%u broadcast %s", label, ifaddr->ip,
|
|
|
|
ifaddr->prefix_len, ifaddr->broadcast);
|
|
|
|
l_free(label);
|
|
|
|
|
|
|
|
l_queue_push_tail(netconfig->ifaddr_list, ifaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ifaddr_deleted(struct netconfig *netconfig,
|
2019-07-20 14:02:23 +02:00
|
|
|
const struct ifaddrmsg *ifa,
|
|
|
|
uint32_t len)
|
2019-06-26 00:20:54 +02:00
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
2019-10-02 22:39:58 +02:00
|
|
|
struct netconfig_ifaddr query;
|
2019-06-26 00:20:54 +02:00
|
|
|
|
2019-10-02 22:39:58 +02:00
|
|
|
rtnl_ifaddr_extract(ifa, len, NULL, &query.ip, NULL);
|
2019-06-26 00:20:54 +02:00
|
|
|
|
2019-10-02 22:39:58 +02:00
|
|
|
query.family = ifa->ifa_family;
|
|
|
|
query.prefix_len = ifa->ifa_prefixlen;
|
2019-06-26 00:20:54 +02:00
|
|
|
|
2019-10-02 22:39:58 +02:00
|
|
|
ifaddr = l_queue_remove_if(netconfig->ifaddr_list,
|
|
|
|
netconfig_ifaddr_match, &query);
|
|
|
|
l_free(query.ip);
|
2019-06-26 00:20:54 +02:00
|
|
|
|
|
|
|
if (!ifaddr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_debug("ifaddr %s/%u", ifaddr->ip, ifaddr->prefix_len);
|
|
|
|
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ifaddr_notify(uint16_t type, const void *data,
|
|
|
|
uint32_t len, void *user_data)
|
|
|
|
{
|
|
|
|
const struct ifaddrmsg *ifa = data;
|
|
|
|
struct netconfig *netconfig;
|
2019-07-20 14:04:13 +02:00
|
|
|
uint32_t bytes;
|
2019-06-26 00:20:54 +02:00
|
|
|
|
|
|
|
netconfig = netconfig_find(ifa->ifa_index);
|
|
|
|
if (!netconfig)
|
|
|
|
/* Ignore the interfaces which aren't managed by iwd. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
bytes = len - NLMSG_ALIGN(sizeof(struct ifaddrmsg));
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
netconfig_ifaddr_added(netconfig, ifa, bytes);
|
|
|
|
break;
|
|
|
|
case RTM_DELADDR:
|
|
|
|
netconfig_ifaddr_deleted(netconfig, ifa, bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:20:55 +02:00
|
|
|
static void netconfig_ifaddr_cmd_cb(int error, uint16_t type,
|
|
|
|
const void *data, uint32_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (error) {
|
|
|
|
l_error("netconfig: ifaddr command failure. "
|
|
|
|
"Error %d: %s", error, strerror(-error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != RTM_NEWADDR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
netconfig_ifaddr_notify(type, data, len, user_data);
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:21 +02:00
|
|
|
static void netconfig_ifaddr_ipv6_added(struct netconfig *netconfig,
|
|
|
|
const struct ifaddrmsg *ifa,
|
|
|
|
uint32_t len)
|
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
|
|
|
|
ifaddr = l_new(struct netconfig_ifaddr, 1);
|
|
|
|
ifaddr->family = ifa->ifa_family;
|
|
|
|
ifaddr->prefix_len = ifa->ifa_prefixlen;
|
|
|
|
|
|
|
|
rtnl_ifaddr_ipv6_extract(ifa, len, &ifaddr->ip);
|
|
|
|
|
|
|
|
l_debug("ifindex %u: ifaddr %s/%u", netconfig->ifindex, ifaddr->ip,
|
|
|
|
ifaddr->prefix_len);
|
|
|
|
|
|
|
|
l_queue_push_tail(netconfig->ifaddr_list, ifaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ifaddr_ipv6_deleted(struct netconfig *netconfig,
|
|
|
|
const struct ifaddrmsg *ifa,
|
|
|
|
uint32_t len)
|
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
struct netconfig_ifaddr query;
|
|
|
|
|
|
|
|
rtnl_ifaddr_ipv6_extract(ifa, len, &query.ip);
|
|
|
|
|
|
|
|
query.family = ifa->ifa_family;
|
|
|
|
query.prefix_len = ifa->ifa_prefixlen;
|
|
|
|
|
|
|
|
ifaddr = l_queue_remove_if(netconfig->ifaddr_list,
|
|
|
|
netconfig_ifaddr_match, &query);
|
|
|
|
|
|
|
|
l_free(query.ip);
|
|
|
|
|
|
|
|
if (!ifaddr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_debug("ifaddr %s/%u", ifaddr->ip, ifaddr->prefix_len);
|
|
|
|
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ifaddr_ipv6_notify(uint16_t type, const void *data,
|
|
|
|
uint32_t len, void *user_data)
|
|
|
|
{
|
|
|
|
const struct ifaddrmsg *ifa = data;
|
|
|
|
struct netconfig *netconfig;
|
|
|
|
uint32_t bytes;
|
|
|
|
|
|
|
|
netconfig = netconfig_find(ifa->ifa_index);
|
|
|
|
if (!netconfig)
|
|
|
|
/* Ignore the interfaces which aren't managed by iwd. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
bytes = len - NLMSG_ALIGN(sizeof(struct ifaddrmsg));
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
netconfig_ifaddr_ipv6_added(netconfig, ifa, bytes);
|
|
|
|
break;
|
|
|
|
case RTM_DELADDR:
|
|
|
|
netconfig_ifaddr_ipv6_deleted(netconfig, ifa, bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:22 +02:00
|
|
|
static void netconfig_ifaddr_ipv6_cmd_cb(int error, uint16_t type,
|
|
|
|
const void *data, uint32_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (error) {
|
|
|
|
l_error("netconfig: ifaddr IPv6 command failure. "
|
|
|
|
"Error %d: %s", error, strerror(-error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != RTM_NEWADDR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
netconfig_ifaddr_ipv6_notify(type, data, len, user_data);
|
|
|
|
}
|
|
|
|
|
2019-10-08 23:48:20 +02:00
|
|
|
static void netconfig_route_add_cmd_cb(int error, uint16_t type,
|
2019-07-31 03:16:12 +02:00
|
|
|
const void *data, uint32_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2019-10-04 21:48:57 +02:00
|
|
|
struct netconfig *netconfig = user_data;
|
2019-07-31 03:16:12 +02:00
|
|
|
|
2019-10-04 21:48:57 +02:00
|
|
|
if (error) {
|
|
|
|
l_error("netconfig: Failed to add route. Error %d: %s",
|
2019-10-08 23:48:20 +02:00
|
|
|
error, strerror(-error));
|
2019-10-04 21:48:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!netconfig->notify)
|
|
|
|
return;
|
|
|
|
|
|
|
|
netconfig->notify(NETCONFIG_EVENT_CONNECTED, netconfig->user_data);
|
|
|
|
netconfig->notify = NULL;
|
2019-10-08 23:48:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_route_del_cmd_cb(int error, uint16_t type,
|
|
|
|
const void *data, uint32_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (!error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_error("netconfig: Failed to delete route. Error %d: %s",
|
2019-07-31 03:16:12 +02:00
|
|
|
error, strerror(-error));
|
2019-10-04 21:48:57 +02:00
|
|
|
|
2019-07-31 03:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool netconfig_ipv4_routes_install(struct netconfig *netconfig,
|
|
|
|
struct netconfig_ifaddr *ifaddr)
|
|
|
|
{
|
|
|
|
L_AUTO_FREE_VAR(char *, gateway) = NULL;
|
|
|
|
struct in_addr in_addr;
|
|
|
|
char *network;
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, ifaddr->ip, &in_addr) < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
in_addr.s_addr = in_addr.s_addr &
|
|
|
|
htonl(0xFFFFFFFFLU << (32 - ifaddr->prefix_len));
|
|
|
|
|
|
|
|
network = inet_ntoa(in_addr);
|
|
|
|
if (!network)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!rtnl_route_ipv4_add_connected(rtnl, netconfig->ifindex,
|
|
|
|
ifaddr->prefix_len, network,
|
2019-07-31 11:38:54 +02:00
|
|
|
ifaddr->ip,
|
|
|
|
netconfig->rtm_protocol,
|
2019-10-08 23:48:20 +02:00
|
|
|
netconfig_route_add_cmd_cb,
|
2019-10-04 21:48:57 +02:00
|
|
|
netconfig, NULL)) {
|
2019-07-31 03:16:12 +02:00
|
|
|
l_error("netconfig: Failed to add subnet route.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:38:54 +02:00
|
|
|
gateway = netconfig_ipv4_get_gateway(netconfig);
|
2019-07-31 03:16:12 +02:00
|
|
|
if (!gateway) {
|
|
|
|
l_error("netconfig: Failed to obtain gateway from %s.",
|
2019-07-31 11:38:54 +02:00
|
|
|
netconfig->rtm_protocol == RTPROT_STATIC ?
|
|
|
|
"setting file" : "DHCPv4 lease");
|
2019-07-31 03:16:12 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rtnl_route_ipv4_add_gateway(rtnl, netconfig->ifindex, gateway,
|
|
|
|
ifaddr->ip,
|
|
|
|
ROUTE_PRIORITY_OFFSET,
|
2019-07-31 11:38:54 +02:00
|
|
|
netconfig->rtm_protocol,
|
2019-10-08 23:48:20 +02:00
|
|
|
netconfig_route_add_cmd_cb,
|
2019-10-04 21:48:57 +02:00
|
|
|
netconfig, NULL)) {
|
2019-07-31 03:16:12 +02:00
|
|
|
l_error("netconfig: Failed to add route for: %s gateway.",
|
|
|
|
gateway);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:10 +02:00
|
|
|
static void netconfig_ipv4_ifaddr_add_cmd_cb(int error, uint16_t type,
|
|
|
|
const void *data, uint32_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct netconfig *netconfig = user_data;
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
char **dns;
|
|
|
|
|
|
|
|
if (error && error != -EEXIST) {
|
|
|
|
l_error("netconfig: Failed to add IP address. "
|
|
|
|
"Error %d: %s", error, strerror(-error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:38:54 +02:00
|
|
|
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, netconfig->rtm_protocol);
|
2019-07-31 03:16:10 +02:00
|
|
|
if (!ifaddr) {
|
|
|
|
l_error("netconfig: Failed to obtain IP address from %s.",
|
2019-07-31 11:38:54 +02:00
|
|
|
netconfig->rtm_protocol == RTPROT_STATIC ?
|
|
|
|
"setting file" : "DHCPv4 lease");
|
2019-07-31 03:16:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:12 +02:00
|
|
|
if (!netconfig_ipv4_routes_install(netconfig, ifaddr)) {
|
|
|
|
l_error("netconfig: Failed to install IPv4 routes.");
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
2019-07-31 03:16:10 +02:00
|
|
|
|
2019-07-31 11:38:54 +02:00
|
|
|
dns = netconfig_ipv4_get_dns(netconfig, netconfig->rtm_protocol);
|
2019-07-31 03:16:10 +02:00
|
|
|
if (!dns) {
|
2019-10-03 22:48:50 +02:00
|
|
|
l_error("netconfig: Failed to obtain DNS addresses.");
|
2019-07-31 03:16:10 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve_add_dns(netconfig->ifindex, ifaddr->family, dns);
|
|
|
|
l_strv_free(dns);
|
|
|
|
|
|
|
|
done:
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:24 +02:00
|
|
|
static bool netconfig_ipv6_routes_install(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
L_AUTO_FREE_VAR(char *, gateway) = NULL;
|
|
|
|
|
|
|
|
gateway = netconfig_ipv6_get_gateway(netconfig);
|
|
|
|
if (!gateway) {
|
|
|
|
l_error("netconfig: Failed to obtain gateway from %s.",
|
|
|
|
netconfig->rtm_v6_protocol == RTPROT_STATIC ?
|
|
|
|
"settings file" : "DHCPv6 lease");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rtnl_route_ipv6_add_gateway(rtnl, netconfig->ifindex, gateway,
|
|
|
|
ROUTE_PRIORITY_OFFSET,
|
|
|
|
netconfig->rtm_v6_protocol,
|
2019-10-08 23:48:20 +02:00
|
|
|
netconfig_route_add_cmd_cb,
|
2019-10-04 21:48:57 +02:00
|
|
|
netconfig, NULL)) {
|
2019-10-02 01:44:24 +02:00
|
|
|
l_error("netconfig: Failed to add route for: %s gateway.",
|
|
|
|
gateway);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
static void netconfig_ipv6_ifaddr_add_cmd_cb(int error, uint16_t type,
|
|
|
|
const void *data, uint32_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2019-10-02 01:44:24 +02:00
|
|
|
struct netconfig *netconfig = user_data;
|
2019-10-02 01:44:25 +02:00
|
|
|
char **dns;
|
2019-10-02 01:44:24 +02:00
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
if (error && error != -EEXIST) {
|
|
|
|
l_error("netconfig: Failed to add IPv6 address. "
|
|
|
|
"Error %d: %s", error, strerror(-error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:24 +02:00
|
|
|
if (!netconfig_ipv6_routes_install(netconfig)) {
|
|
|
|
l_error("netconfig: Failed to install IPv6 routes.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:25 +02:00
|
|
|
dns = netconfig_ipv6_get_dns(netconfig, netconfig->rtm_v6_protocol);
|
|
|
|
if (!dns) {
|
|
|
|
l_error("netconfig: Failed to obtain the DNS addresses from "
|
|
|
|
"%s.", netconfig->rtm_v6_protocol == RTPROT_STATIC ?
|
|
|
|
"setting file" : "DHCPv6 lease");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve_add_dns(netconfig->ifindex, AF_INET6, dns);
|
|
|
|
l_strv_free(dns);
|
2019-10-02 01:44:23 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:09 +02:00
|
|
|
static void netconfig_install_address(struct netconfig *netconfig,
|
|
|
|
struct netconfig_ifaddr *ifaddr)
|
2019-06-26 00:20:57 +02:00
|
|
|
{
|
2019-07-31 03:16:09 +02:00
|
|
|
if (netconfig_ifaddr_find(netconfig, ifaddr->family, ifaddr->prefix_len,
|
|
|
|
ifaddr->ip))
|
|
|
|
return;
|
2019-06-26 00:20:59 +02:00
|
|
|
|
|
|
|
switch (ifaddr->family) {
|
|
|
|
case AF_INET:
|
2019-07-31 03:16:09 +02:00
|
|
|
if (rtnl_ifaddr_add(rtnl, netconfig->ifindex,
|
2019-06-26 00:20:59 +02:00
|
|
|
ifaddr->prefix_len, ifaddr->ip,
|
|
|
|
ifaddr->broadcast,
|
2019-07-31 03:16:10 +02:00
|
|
|
netconfig_ipv4_ifaddr_add_cmd_cb,
|
2019-07-31 03:16:09 +02:00
|
|
|
netconfig, NULL))
|
|
|
|
return;
|
2019-06-26 00:20:59 +02:00
|
|
|
|
2019-07-31 03:16:09 +02:00
|
|
|
l_error("netconfig: Failed to set IP %s/%u.", ifaddr->ip,
|
|
|
|
ifaddr->prefix_len);
|
2019-06-26 00:20:59 +02:00
|
|
|
break;
|
2019-10-02 01:44:23 +02:00
|
|
|
case AF_INET6:
|
|
|
|
if (rtnl_ifaddr_ipv6_add(rtnl, netconfig->ifindex,
|
|
|
|
ifaddr->prefix_len, ifaddr->ip,
|
|
|
|
netconfig_ipv6_ifaddr_add_cmd_cb,
|
|
|
|
netconfig, NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_error("netconfig: Failed to set IPv6 address %s/%u.",
|
|
|
|
ifaddr->ip, ifaddr->prefix_len);
|
|
|
|
break;
|
2019-06-26 00:20:59 +02:00
|
|
|
default:
|
|
|
|
l_error("netconfig: Unsupported address family: %u",
|
|
|
|
ifaddr->family);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-26 00:20:57 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:10 +02:00
|
|
|
static void netconfig_ifaddr_del_cmd_cb(int error, uint16_t type,
|
|
|
|
const void *data, uint32_t len,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if (error == -ENODEV)
|
|
|
|
/* The device is unplugged, we are done. */
|
|
|
|
return;
|
|
|
|
|
2019-08-16 20:25:36 +02:00
|
|
|
if (!error)
|
|
|
|
/*
|
|
|
|
* The kernel removes all of the routes associated with the
|
|
|
|
* deleted IP on its own. There is no need to explicitly remove
|
|
|
|
* them.
|
|
|
|
*/
|
2019-07-31 03:16:10 +02:00
|
|
|
return;
|
|
|
|
|
2019-08-16 20:25:36 +02:00
|
|
|
l_error("netconfig: Failed to delete IP address. "
|
|
|
|
"Error %d: %s", error, strerror(-error));
|
2019-07-31 03:16:10 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:09 +02:00
|
|
|
static void netconfig_uninstall_address(struct netconfig *netconfig,
|
|
|
|
struct netconfig_ifaddr *ifaddr)
|
2019-06-26 00:20:56 +02:00
|
|
|
{
|
2019-07-31 03:16:09 +02:00
|
|
|
if (!netconfig_ifaddr_find(netconfig, ifaddr->family,
|
|
|
|
ifaddr->prefix_len, ifaddr->ip))
|
|
|
|
return;
|
2019-06-26 00:20:59 +02:00
|
|
|
|
|
|
|
switch (ifaddr->family) {
|
|
|
|
case AF_INET:
|
2019-07-31 03:16:09 +02:00
|
|
|
if (rtnl_ifaddr_delete(rtnl, netconfig->ifindex,
|
2019-06-26 00:20:59 +02:00
|
|
|
ifaddr->prefix_len, ifaddr->ip,
|
|
|
|
ifaddr->broadcast,
|
2019-07-31 03:16:10 +02:00
|
|
|
netconfig_ifaddr_del_cmd_cb, netconfig,
|
2019-06-26 00:20:59 +02:00
|
|
|
NULL))
|
2019-07-31 03:16:09 +02:00
|
|
|
return;
|
2019-06-26 00:20:59 +02:00
|
|
|
|
2019-07-31 03:16:09 +02:00
|
|
|
l_error("netconfig: Failed to delete IP %s/%u.",
|
|
|
|
ifaddr->ip, ifaddr->prefix_len);
|
|
|
|
break;
|
2019-10-02 01:44:23 +02:00
|
|
|
case AF_INET6:
|
|
|
|
if (rtnl_ifaddr_ipv6_delete(rtnl, netconfig->ifindex,
|
|
|
|
ifaddr->prefix_len, ifaddr->ip,
|
|
|
|
netconfig_ifaddr_del_cmd_cb, netconfig,
|
|
|
|
NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_error("netconfig: Failed to delete IPv6 address %s/%u.",
|
|
|
|
ifaddr->ip, ifaddr->prefix_len);
|
|
|
|
break;
|
2019-06-26 00:20:59 +02:00
|
|
|
default:
|
|
|
|
l_error("netconfig: Unsupported address family: %u",
|
|
|
|
ifaddr->family);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-26 00:20:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:04 +02:00
|
|
|
static void netconfig_ipv4_dhcp_event_handler(struct l_dhcp_client *client,
|
2019-06-26 00:20:56 +02:00
|
|
|
enum l_dhcp_client_event event,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
struct netconfig *netconfig = userdata;
|
2019-07-31 03:16:08 +02:00
|
|
|
struct netconfig_ifaddr *ifaddr;
|
2019-06-26 00:20:56 +02:00
|
|
|
|
|
|
|
l_debug("DHCPv4 event %d", event);
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case L_DHCP_CLIENT_EVENT_LEASE_RENEWED:
|
|
|
|
case L_DHCP_CLIENT_EVENT_LEASE_OBTAINED:
|
|
|
|
case L_DHCP_CLIENT_EVENT_IP_CHANGED:
|
2019-07-31 03:16:08 +02:00
|
|
|
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, RTPROT_DHCP);
|
|
|
|
if (!ifaddr) {
|
|
|
|
l_error("netconfig: Failed to obtain IP addresses from "
|
|
|
|
"DHCPv4 lease.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:09 +02:00
|
|
|
netconfig_install_address(netconfig, ifaddr);
|
2019-07-31 03:16:08 +02:00
|
|
|
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
2019-06-26 00:20:56 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
case L_DHCP_CLIENT_EVENT_LEASE_EXPIRED:
|
2019-07-31 03:16:08 +02:00
|
|
|
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, RTPROT_DHCP);
|
|
|
|
if (!ifaddr) {
|
|
|
|
l_error("netconfig: Failed to obtain IP addresses from "
|
|
|
|
"DHCPv4 lease.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:09 +02:00
|
|
|
netconfig_uninstall_address(netconfig, ifaddr);
|
2019-07-31 03:16:08 +02:00
|
|
|
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
|
2019-06-26 00:20:56 +02:00
|
|
|
/* Fall through. */
|
|
|
|
case L_DHCP_CLIENT_EVENT_NO_LEASE:
|
|
|
|
/*
|
|
|
|
* The requested address is no longer available, try to restart
|
|
|
|
* the client.
|
|
|
|
*/
|
|
|
|
if (!l_dhcp_client_start(client))
|
|
|
|
l_error("netconfig: Failed to re-start DHCPv4 client "
|
|
|
|
"for interface %u", netconfig->ifindex);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
l_error("netconfig: Received unsupported DHCPv4 event: %d",
|
|
|
|
event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
static bool netconfig_ipv4_dhcp_create(struct netconfig *netconfig)
|
2019-06-26 00:20:56 +02:00
|
|
|
{
|
|
|
|
netconfig->dhcp_client = l_dhcp_client_new(netconfig->ifindex);
|
|
|
|
|
|
|
|
l_dhcp_client_set_event_handler(netconfig->dhcp_client,
|
2019-07-31 03:16:04 +02:00
|
|
|
netconfig_ipv4_dhcp_event_handler,
|
2019-06-26 00:20:56 +02:00
|
|
|
netconfig, NULL);
|
|
|
|
|
|
|
|
if (getenv("IWD_DHCP_DEBUG"))
|
|
|
|
l_dhcp_client_set_debug(netconfig->dhcp_client, do_debug,
|
|
|
|
"[DHCPv4] ", NULL);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-31 03:16:06 +02:00
|
|
|
static void netconfig_ipv4_select_and_install(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
|
|
|
|
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, RTPROT_STATIC);
|
|
|
|
if (ifaddr) {
|
2019-07-31 11:38:54 +02:00
|
|
|
netconfig->rtm_protocol = RTPROT_STATIC;
|
2019-07-31 03:16:09 +02:00
|
|
|
netconfig_install_address(netconfig, ifaddr);
|
2019-07-31 03:16:06 +02:00
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:38:54 +02:00
|
|
|
netconfig->rtm_protocol = RTPROT_DHCP;
|
|
|
|
|
2019-07-31 03:16:06 +02:00
|
|
|
if (l_dhcp_client_start(netconfig->dhcp_client))
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_error("netconfig: Failed to start DHCPv4 client for interface %u",
|
|
|
|
netconfig->ifindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ipv4_select_and_uninstall(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
|
2019-07-31 11:38:54 +02:00
|
|
|
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, netconfig->rtm_protocol);
|
2019-07-31 03:16:06 +02:00
|
|
|
if (ifaddr) {
|
2019-07-31 03:16:09 +02:00
|
|
|
netconfig_uninstall_address(netconfig, ifaddr);
|
2019-07-31 03:16:06 +02:00
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dhcp_client_stop(netconfig->dhcp_client);
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
static void netconfig_ipv6_select_and_install(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
|
|
|
|
|
|
|
ifaddr = netconfig_ipv6_get_ifaddr(netconfig, RTPROT_STATIC);
|
|
|
|
if (ifaddr) {
|
|
|
|
netconfig->rtm_v6_protocol = RTPROT_STATIC;
|
|
|
|
netconfig_install_address(netconfig, ifaddr);
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO
|
|
|
|
*
|
|
|
|
* netconfig->rtm_v6_protocol = RTPROT_DHCP;
|
|
|
|
*
|
|
|
|
* if (l_dhcp_v6_client_start(netconfig->l_dhcp_v6_client))
|
|
|
|
* return;
|
|
|
|
*
|
|
|
|
* l_error("netconfig: Failed to start DHCPv6 client for "
|
|
|
|
* "interface %u", netconfig->ifindex);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_ipv6_select_and_uninstall(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
struct netconfig_ifaddr *ifaddr;
|
2019-10-02 22:39:57 +02:00
|
|
|
char *gateway;
|
2019-10-02 01:44:23 +02:00
|
|
|
|
|
|
|
ifaddr = netconfig_ipv6_get_ifaddr(netconfig,
|
|
|
|
netconfig->rtm_v6_protocol);
|
|
|
|
if (ifaddr) {
|
|
|
|
netconfig_uninstall_address(netconfig, ifaddr);
|
|
|
|
netconfig_ifaddr_destroy(ifaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO
|
|
|
|
* l_dhcp_v6_client_stop(netconfig->l_dhcp_v6_client);
|
|
|
|
*/
|
2019-10-02 22:39:57 +02:00
|
|
|
|
|
|
|
gateway = netconfig_ipv6_get_gateway(netconfig);
|
|
|
|
if (!gateway)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!rtnl_route_ipv6_delete_gateway(rtnl, netconfig->ifindex,
|
|
|
|
gateway, ROUTE_PRIORITY_OFFSET,
|
|
|
|
netconfig->rtm_v6_protocol,
|
2019-10-08 23:48:20 +02:00
|
|
|
netconfig_route_del_cmd_cb, NULL, NULL)) {
|
2019-10-02 22:39:57 +02:00
|
|
|
l_error("netconfig: Failed to delete route for: %s gateway.",
|
|
|
|
gateway);
|
|
|
|
}
|
|
|
|
|
|
|
|
l_free(gateway);
|
2019-10-02 01:44:23 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
bool netconfig_configure(struct netconfig *netconfig,
|
|
|
|
const struct l_settings *active_settings,
|
2019-10-04 21:48:57 +02:00
|
|
|
const uint8_t *mac_address,
|
|
|
|
netconfig_notify_func_t notify, void *user_data)
|
2019-06-26 00:20:57 +02:00
|
|
|
{
|
2019-09-30 18:35:51 +02:00
|
|
|
netconfig->active_settings = active_settings;
|
2019-10-04 21:48:57 +02:00
|
|
|
netconfig->notify = notify;
|
|
|
|
netconfig->user_data = user_data;
|
2019-06-26 00:20:57 +02:00
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
l_dhcp_client_set_address(netconfig->dhcp_client, ARPHRD_ETHER,
|
|
|
|
mac_address, ETH_ALEN);
|
2019-06-26 00:20:57 +02:00
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
netconfig_ipv4_select_and_install(netconfig);
|
2019-06-26 00:21:00 +02:00
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
netconfig_ipv6_select_and_install(netconfig);
|
2019-06-26 00:20:57 +02:00
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-17 02:49:47 +02:00
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
bool netconfig_reconfigure(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
if (netconfig->rtm_protocol == RTPROT_DHCP) {
|
2019-10-02 01:39:39 +02:00
|
|
|
/* TODO l_dhcp_client sending a DHCP inform request */
|
2019-09-30 18:35:51 +02:00
|
|
|
}
|
2019-06-26 00:20:57 +02:00
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
if (netconfig->rtm_v6_protocol == RTPROT_DHCP) {
|
|
|
|
/* TODO l_dhcp_v6_client sending a DHCP inform request */
|
|
|
|
}
|
2019-06-26 00:20:57 +02:00
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool netconfig_reset(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
netconfig_ipv4_select_and_uninstall(netconfig);
|
2019-10-02 01:44:23 +02:00
|
|
|
netconfig->rtm_protocol = 0;
|
2019-09-30 18:35:51 +02:00
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
netconfig_ipv6_select_and_uninstall(netconfig);
|
|
|
|
netconfig->rtm_v6_protocol = 0;
|
2019-09-30 18:35:51 +02:00
|
|
|
|
|
|
|
resolve_remove(netconfig->ifindex);
|
|
|
|
|
|
|
|
return true;
|
2019-06-26 00:20:57 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 21:52:17 +02:00
|
|
|
struct netconfig *netconfig_new(uint32_t ifindex)
|
2019-06-26 00:20:52 +02:00
|
|
|
{
|
|
|
|
struct netconfig *netconfig;
|
|
|
|
|
|
|
|
if (!netconfig_list)
|
2019-09-27 21:52:17 +02:00
|
|
|
return NULL;
|
2019-06-26 00:20:52 +02:00
|
|
|
|
|
|
|
l_debug("Starting netconfig for interface: %d", ifindex);
|
|
|
|
|
|
|
|
netconfig = netconfig_find(ifindex);
|
|
|
|
if (netconfig)
|
2019-09-27 21:52:17 +02:00
|
|
|
return netconfig;
|
2019-06-26 00:20:52 +02:00
|
|
|
|
|
|
|
netconfig = l_new(struct netconfig, 1);
|
|
|
|
netconfig->ifindex = ifindex;
|
2019-06-26 00:20:54 +02:00
|
|
|
netconfig->ifaddr_list = l_queue_new();
|
2019-06-26 00:20:52 +02:00
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
netconfig_ipv4_dhcp_create(netconfig);
|
2019-06-26 00:20:57 +02:00
|
|
|
|
2019-06-26 00:20:52 +02:00
|
|
|
l_queue_push_tail(netconfig_list, netconfig);
|
|
|
|
|
2019-09-27 21:52:17 +02:00
|
|
|
return netconfig;
|
2019-06-26 00:20:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 21:52:17 +02:00
|
|
|
void netconfig_destroy(struct netconfig *netconfig)
|
2019-06-26 00:20:52 +02:00
|
|
|
{
|
|
|
|
if (!netconfig_list)
|
2019-09-27 21:52:17 +02:00
|
|
|
return;
|
2019-06-26 00:20:52 +02:00
|
|
|
|
|
|
|
l_debug();
|
|
|
|
|
2019-09-27 21:52:17 +02:00
|
|
|
l_queue_remove(netconfig_list, netconfig);
|
2019-06-26 00:20:52 +02:00
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
if (netconfig->rtm_protocol)
|
2019-08-16 20:25:37 +02:00
|
|
|
netconfig_ipv4_select_and_uninstall(netconfig);
|
|
|
|
|
2019-10-02 01:44:23 +02:00
|
|
|
if (netconfig->rtm_protocol || netconfig->rtm_v6_protocol)
|
2019-08-16 20:25:37 +02:00
|
|
|
resolve_remove(netconfig->ifindex);
|
|
|
|
|
2019-09-27 21:52:16 +02:00
|
|
|
netconfig_free(netconfig);
|
2019-06-26 00:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int netconfig_init(void)
|
|
|
|
{
|
|
|
|
bool enabled;
|
2019-06-26 00:20:54 +02:00
|
|
|
uint32_t r;
|
2019-06-26 00:20:52 +02:00
|
|
|
|
|
|
|
if (netconfig_list)
|
|
|
|
return -EALREADY;
|
|
|
|
|
|
|
|
if (!l_settings_get_bool(iwd_get_config(), "General",
|
2019-10-25 20:13:40 +02:00
|
|
|
"EnableNetworkConfiguration",
|
|
|
|
&enabled)) {
|
|
|
|
if (l_settings_get_bool(iwd_get_config(), "General",
|
|
|
|
"enable_network_config", &enabled))
|
|
|
|
l_warn("[General].enable_network_config is deprecated,"
|
|
|
|
" use [General].EnableNetworkConfiguration");
|
|
|
|
else
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
l_info("netconfig: Network configuration is disabled.");
|
2019-10-02 22:39:59 +02:00
|
|
|
return 0;
|
2019-06-26 00:20:52 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 00:20:54 +02:00
|
|
|
rtnl = l_netlink_new(NETLINK_ROUTE);
|
|
|
|
if (!rtnl) {
|
|
|
|
l_error("netconfig: Failed to open route netlink socket");
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getenv("IWD_RTNL_DEBUG"))
|
|
|
|
l_netlink_set_debug(rtnl, do_debug, "[NETCONFIG RTNL] ", NULL);
|
|
|
|
|
|
|
|
r = l_netlink_register(rtnl, RTNLGRP_IPV4_IFADDR,
|
|
|
|
netconfig_ifaddr_notify, NULL, NULL);
|
|
|
|
if (!r) {
|
|
|
|
l_error("netconfig: Failed to register for RTNL link address"
|
|
|
|
" notifications.");
|
2019-07-17 02:49:43 +02:00
|
|
|
goto error;
|
2019-06-26 00:20:54 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 00:20:55 +02:00
|
|
|
r = rtnl_ifaddr_get(rtnl, netconfig_ifaddr_cmd_cb, NULL, NULL);
|
|
|
|
if (!r) {
|
|
|
|
l_error("netconfig: Failed to get addresses from RTNL link.");
|
2019-07-17 02:49:43 +02:00
|
|
|
goto error;
|
2019-06-26 00:20:55 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:21 +02:00
|
|
|
r = l_netlink_register(rtnl, RTNLGRP_IPV6_IFADDR,
|
|
|
|
netconfig_ifaddr_ipv6_notify, NULL, NULL);
|
|
|
|
if (!r) {
|
|
|
|
l_error("netconfig: Failed to register for RTNL link IPv6 "
|
|
|
|
"address notifications.");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-10-02 01:44:22 +02:00
|
|
|
r = rtnl_ifaddr_ipv6_get(rtnl, netconfig_ifaddr_ipv6_cmd_cb, NULL,
|
|
|
|
NULL);
|
|
|
|
if (!r) {
|
|
|
|
l_error("netconfig: Failed to get IPv6 addresses from RTNL"
|
|
|
|
" link.");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:13:40 +02:00
|
|
|
if (!l_settings_get_uint(iwd_get_config(), "Network",
|
|
|
|
"RoutePriorityOffset",
|
2019-07-31 03:16:12 +02:00
|
|
|
&ROUTE_PRIORITY_OFFSET))
|
|
|
|
ROUTE_PRIORITY_OFFSET = 300;
|
|
|
|
|
2019-06-26 00:20:52 +02:00
|
|
|
netconfig_list = l_queue_new();
|
|
|
|
|
|
|
|
return 0;
|
2019-07-17 02:49:43 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
l_netlink_destroy(rtnl);
|
|
|
|
rtnl = NULL;
|
|
|
|
|
|
|
|
return r;
|
2019-06-26 00:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_exit(void)
|
|
|
|
{
|
|
|
|
if (!netconfig_list)
|
|
|
|
return;
|
|
|
|
|
2019-06-26 00:20:54 +02:00
|
|
|
l_netlink_destroy(rtnl);
|
|
|
|
rtnl = NULL;
|
|
|
|
|
2019-09-27 21:52:16 +02:00
|
|
|
l_queue_destroy(netconfig_list, netconfig_free);
|
2019-06-26 00:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IWD_MODULE(netconfig, netconfig_init, netconfig_exit)
|