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>
|
2020-11-05 16:38:32 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2019-06-26 00:20:52 +02:00
|
|
|
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
2021-03-12 04:46:09 +01:00
|
|
|
#include "ell/useful.h"
|
2019-06-26 00:20:52 +02:00
|
|
|
#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-07-03 01:14:50 +02:00
|
|
|
#include "src/resolve.h"
|
2021-08-27 05:02:03 +02:00
|
|
|
#include "src/util.h"
|
2021-08-27 05:02:04 +02:00
|
|
|
#include "src/ie.h"
|
2019-06-26 00:20:52 +02:00
|
|
|
#include "src/netconfig.h"
|
2021-11-03 23:15:00 +01:00
|
|
|
#include "src/sysfs.h"
|
2019-06-26 00:20:52 +02:00
|
|
|
|
|
|
|
struct netconfig {
|
|
|
|
uint32_t ifindex;
|
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;
|
2020-11-19 20:52:56 +01:00
|
|
|
struct l_rtnl_address *v4_address;
|
2021-10-21 10:50:31 +02:00
|
|
|
struct l_rtnl_address *v6_address;
|
2020-11-18 16:58:28 +01:00
|
|
|
char **dns4_overrides;
|
|
|
|
char **dns6_overrides;
|
2021-10-21 10:50:33 +02:00
|
|
|
char **dns4_list;
|
|
|
|
char **dns6_list;
|
2021-09-29 23:06:52 +02:00
|
|
|
char *mdns;
|
2021-08-27 05:02:04 +02:00
|
|
|
struct ie_fils_ip_addr_response_info *fils_override;
|
2021-10-21 10:50:32 +02:00
|
|
|
char *v4_gateway_str;
|
|
|
|
char *v6_gateway_str;
|
2021-10-21 10:50:34 +02:00
|
|
|
char *v4_domain;
|
|
|
|
char **v6_domains;
|
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;
|
2020-08-21 21:50:44 +02:00
|
|
|
|
|
|
|
struct resolve *resolve;
|
2019-06-26 00:20:52 +02:00
|
|
|
};
|
|
|
|
|
2019-06-26 00:20:54 +02:00
|
|
|
static struct l_netlink *rtnl;
|
2019-06-26 00:20:52 +02:00
|
|
|
|
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;
|
2020-11-13 22:24:17 +01:00
|
|
|
static bool ipv6_enabled;
|
2019-07-31 19:38:40 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:54:41 +02:00
|
|
|
static void netconfig_free_settings(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
l_rtnl_address_free(netconfig->v4_address);
|
|
|
|
netconfig->v4_address = NULL;
|
2021-10-21 10:50:31 +02:00
|
|
|
l_rtnl_address_free(netconfig->v6_address);
|
|
|
|
netconfig->v6_address = NULL;
|
2021-09-29 22:54:41 +02:00
|
|
|
|
|
|
|
l_strfreev(netconfig->dns4_overrides);
|
|
|
|
netconfig->dns4_overrides = NULL;
|
|
|
|
l_strfreev(netconfig->dns6_overrides);
|
|
|
|
netconfig->dns6_overrides = NULL;
|
2021-09-29 23:06:52 +02:00
|
|
|
|
|
|
|
l_free(netconfig->mdns);
|
|
|
|
netconfig->mdns = NULL;
|
2021-09-29 22:54:41 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
l_free(netconfig);
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:28:31 +01:00
|
|
|
static bool netconfig_use_fils_addr(struct netconfig *netconfig, int af)
|
|
|
|
{
|
|
|
|
if ((af == AF_INET ? netconfig->rtm_protocol :
|
|
|
|
netconfig->rtm_v6_protocol) != RTPROT_DHCP)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!netconfig->fils_override)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (af == AF_INET)
|
|
|
|
return !!netconfig->fils_override->ipv4_addr;
|
|
|
|
|
|
|
|
return !l_memeqzero(netconfig->fils_override->ipv6_addr, 16);
|
|
|
|
}
|
|
|
|
|
2021-09-08 23:44:40 +02:00
|
|
|
static void netconfig_set_neighbor_entry_cb(int error,
|
|
|
|
uint16_t type, const void *data,
|
|
|
|
uint32_t len, void *user_data)
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
l_error("l_rtnl_neighbor_set_hwaddr failed: %s (%i)",
|
|
|
|
strerror(-error), error);
|
|
|
|
}
|
2020-11-18 16:58:28 +01:00
|
|
|
|
2020-11-19 20:52:56 +01:00
|
|
|
static struct l_rtnl_address *netconfig_get_static4_address(
|
2021-08-27 05:02:03 +02:00
|
|
|
const struct l_settings *active_settings)
|
2019-07-31 03:16:05 +02:00
|
|
|
{
|
2020-11-19 20:52:56 +01:00
|
|
|
struct l_rtnl_address *ifaddr = NULL;
|
2021-08-27 05:02:03 +02:00
|
|
|
L_AUTO_FREE_VAR(char *, ip) = NULL;
|
|
|
|
L_AUTO_FREE_VAR(char *, netmask) = NULL;
|
2020-11-19 20:52:56 +01:00
|
|
|
struct in_addr in_addr;
|
2021-08-27 05:02:03 +02:00
|
|
|
L_AUTO_FREE_VAR(char *, broadcast) = NULL;
|
2020-11-19 20:52:56 +01:00
|
|
|
uint32_t prefix_len;
|
2019-07-31 03:16:05 +02:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
ip = l_settings_get_string(active_settings, "IPv4", "Address");
|
2020-11-19 20:52:56 +01:00
|
|
|
if (!ip)
|
|
|
|
return NULL;
|
2019-12-11 00:38:32 +01:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
netmask = l_settings_get_string(active_settings, "IPv4", "Netmask");
|
|
|
|
if (netmask) {
|
|
|
|
if (inet_pton(AF_INET, netmask, &in_addr) != 1) {
|
|
|
|
l_error("netconfig: Can't parse IPv4 Netmask");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-31 03:16:05 +02:00
|
|
|
|
2020-11-19 20:52:56 +01:00
|
|
|
prefix_len = __builtin_popcountl(in_addr.s_addr);
|
2019-07-31 03:16:05 +02:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
if (ntohl(in_addr.s_addr) !=
|
|
|
|
util_netmask_from_prefix(prefix_len)) {
|
|
|
|
l_error("netconfig: Invalid IPv4 Netmask");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
prefix_len = 24;
|
2019-12-11 00:38:32 +01:00
|
|
|
|
2020-11-19 20:52:56 +01:00
|
|
|
ifaddr = l_rtnl_address_new(ip, prefix_len);
|
|
|
|
if (!ifaddr) {
|
2021-08-27 05:02:03 +02:00
|
|
|
l_error("netconfig: Unable to parse IPv4.Address");
|
2020-11-19 20:52:56 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-31 03:16:05 +02:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
broadcast = l_settings_get_string(active_settings, "IPv4", "Broadcast");
|
|
|
|
if (broadcast && !l_rtnl_address_set_broadcast(ifaddr, broadcast)) {
|
|
|
|
l_error("netconfig: Unable to parse IPv4.Broadcast");
|
|
|
|
l_rtnl_address_free(ifaddr);
|
|
|
|
return NULL;
|
2019-07-31 03:16:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-19 20:52:56 +01:00
|
|
|
l_rtnl_address_set_noprefixroute(ifaddr, true);
|
|
|
|
return ifaddr;
|
2019-07-31 03:16:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-19 20:26:03 +01:00
|
|
|
static struct l_rtnl_address *netconfig_get_static6_address(
|
2021-08-27 05:02:03 +02:00
|
|
|
const struct l_settings *active_settings)
|
2019-10-02 01:44:23 +02:00
|
|
|
{
|
2020-11-19 20:26:03 +01:00
|
|
|
L_AUTO_FREE_VAR(char *, ip);
|
2019-10-02 01:44:23 +02:00
|
|
|
char *p;
|
2022-06-16 02:02:17 +02:00
|
|
|
char *endp;
|
2020-11-19 20:26:03 +01:00
|
|
|
struct l_rtnl_address *ret;
|
|
|
|
uint32_t prefix_len = 128;
|
2019-10-02 01:44:23 +02:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
ip = l_settings_get_string(active_settings, "IPv6", "Address");
|
2020-11-19 20:26:03 +01:00
|
|
|
if (!ip)
|
|
|
|
return NULL;
|
2019-10-02 01:44:23 +02:00
|
|
|
|
2020-11-19 20:26:03 +01:00
|
|
|
p = strrchr(ip, '/');
|
|
|
|
if (!p)
|
|
|
|
goto no_prefix_len;
|
2019-10-02 01:44:23 +02:00
|
|
|
|
2020-11-19 20:26:03 +01:00
|
|
|
*p = '\0';
|
|
|
|
if (*++p == '\0')
|
|
|
|
goto no_prefix_len;
|
2019-10-02 01:44:23 +02:00
|
|
|
|
2022-06-16 02:02:17 +02:00
|
|
|
errno = 0;
|
|
|
|
prefix_len = strtoul(p, &endp, 10);
|
|
|
|
if (unlikely(*endp != '\0' || errno ||
|
2020-11-19 20:26:03 +01:00
|
|
|
!prefix_len || prefix_len > 128)) {
|
2021-10-21 10:50:32 +02:00
|
|
|
l_error("netconfig: Invalid prefix '%s' provided in network"
|
2020-11-19 20:26:03 +01:00
|
|
|
" configuration file", p);
|
2019-10-02 01:44:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-19 20:26:03 +01:00
|
|
|
no_prefix_len:
|
|
|
|
ret = l_rtnl_address_new(ip, prefix_len);
|
|
|
|
if (!ret)
|
|
|
|
l_error("netconfig: Invalid IPv6 address %s is "
|
|
|
|
"provided in network configuration file.", ip);
|
|
|
|
|
|
|
|
return ret;
|
2019-10-02 01:44:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 00:49:18 +02:00
|
|
|
static void netconfig_gateway_to_arp(struct netconfig *netconfig)
|
|
|
|
{
|
2022-08-29 19:35:53 +02:00
|
|
|
struct l_dhcp_client *dhcp = NULL; /* TODO */
|
2021-10-14 00:49:18 +02:00
|
|
|
const struct l_dhcp_lease *lease;
|
|
|
|
_auto_(l_free) char *server_id = NULL;
|
|
|
|
_auto_(l_free) char *gw = NULL;
|
|
|
|
const uint8_t *server_mac;
|
|
|
|
struct in_addr in_gw;
|
|
|
|
|
|
|
|
/* Can only do this for DHCP in certain network setups */
|
|
|
|
if (netconfig->rtm_protocol != RTPROT_DHCP)
|
|
|
|
return;
|
|
|
|
|
2022-08-29 19:35:53 +02:00
|
|
|
lease = l_dhcp_client_get_lease(dhcp);
|
2021-10-14 00:49:18 +02:00
|
|
|
if (!lease)
|
|
|
|
return;
|
|
|
|
|
|
|
|
server_id = l_dhcp_lease_get_server_id(lease);
|
|
|
|
gw = l_dhcp_lease_get_gateway(lease);
|
|
|
|
server_mac = l_dhcp_lease_get_server_mac(lease);
|
|
|
|
|
2021-10-18 17:21:47 +02:00
|
|
|
if (!gw || strcmp(server_id, gw) || !server_mac)
|
2021-10-14 00:49:18 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
l_debug("Gateway MAC is known, setting into ARP cache");
|
|
|
|
in_gw.s_addr = l_dhcp_lease_get_gateway_u32(lease);
|
|
|
|
|
|
|
|
if (!l_rtnl_neighbor_set_hwaddr(rtnl, netconfig->ifindex, AF_INET,
|
|
|
|
&in_gw, server_mac, ETH_ALEN,
|
|
|
|
netconfig_set_neighbor_entry_cb, NULL,
|
|
|
|
NULL))
|
|
|
|
l_debug("l_rtnl_neighbor_set_hwaddr failed");
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:50:56 +02:00
|
|
|
static void netconfig_remove_v4_address(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
if (!netconfig->v4_address)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_rtnl_address_free(netconfig->v4_address);
|
|
|
|
netconfig->v4_address = NULL;
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:22:09 +01:00
|
|
|
static void netconfig_reset_v4(struct netconfig *netconfig)
|
|
|
|
{
|
|
|
|
if (netconfig->rtm_protocol) {
|
2021-05-19 20:50:56 +02:00
|
|
|
netconfig_remove_v4_address(netconfig);
|
2020-12-10 22:22:09 +01:00
|
|
|
|
2021-10-21 10:50:33 +02:00
|
|
|
l_strv_free(l_steal_ptr(netconfig->dns4_overrides));
|
|
|
|
l_strv_free(l_steal_ptr(netconfig->dns4_list));
|
2020-12-10 22:22:09 +01:00
|
|
|
|
|
|
|
netconfig->rtm_protocol = 0;
|
2021-05-19 20:50:56 +02:00
|
|
|
|
2021-10-21 10:50:32 +02:00
|
|
|
l_free(l_steal_ptr(netconfig->v4_gateway_str));
|
2021-10-21 10:50:34 +02:00
|
|
|
|
|
|
|
l_free(l_steal_ptr(netconfig->v4_domain));
|
2020-12-10 22:22:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
static int validate_dns_list(int family, char **dns_list)
|
|
|
|
{
|
|
|
|
unsigned int n_valid = 0;
|
|
|
|
struct in_addr in_addr;
|
|
|
|
struct in6_addr in6_addr;
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (p = dns_list; *p; p++) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (family == AF_INET)
|
|
|
|
r = inet_pton(AF_INET, *p, &in_addr);
|
|
|
|
else if (family == AF_INET6)
|
|
|
|
r = inet_pton(AF_INET6, *p, &in6_addr);
|
|
|
|
else
|
|
|
|
r = -EAFNOSUPPORT;
|
|
|
|
|
|
|
|
if (r > 0) {
|
|
|
|
n_valid += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_error("netconfig: Invalid DNS address '%s'.", *p);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n_valid;
|
|
|
|
}
|
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
bool netconfig_load_settings(struct netconfig *netconfig,
|
2021-09-27 18:21:40 +02:00
|
|
|
const struct l_settings *active_settings)
|
2019-06-26 00:20:57 +02:00
|
|
|
{
|
2021-10-21 10:50:35 +02:00
|
|
|
_auto_(l_free) char *mdns = NULL;
|
2021-06-18 19:40:46 +02:00
|
|
|
bool send_hostname;
|
2021-08-27 05:02:03 +02:00
|
|
|
bool v6_enabled;
|
|
|
|
char hostname[HOST_NAME_MAX + 1];
|
2021-10-21 10:50:35 +02:00
|
|
|
_auto_(l_strv_free) char **dns4_overrides = NULL;
|
|
|
|
_auto_(l_strv_free) char **dns6_overrides = NULL;
|
|
|
|
_auto_(l_rtnl_address_free) struct l_rtnl_address *v4_address = NULL;
|
|
|
|
_auto_(l_rtnl_address_free) struct l_rtnl_address *v6_address = NULL;
|
2021-02-11 21:54:17 +01:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
dns4_overrides = l_settings_get_string_list(active_settings,
|
2020-11-18 16:58:28 +01:00
|
|
|
"IPv4", "DNS", ' ');
|
2021-08-27 05:02:03 +02:00
|
|
|
if (dns4_overrides) {
|
|
|
|
int r = validate_dns_list(AF_INET, dns4_overrides);
|
2020-11-18 16:58:28 +01:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
if (unlikely(r <= 0)) {
|
|
|
|
l_strfreev(dns4_overrides);
|
|
|
|
dns4_overrides = NULL;
|
2020-11-18 16:58:28 +01:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
if (r < 0)
|
2021-10-21 10:50:35 +02:00
|
|
|
return false;
|
2020-11-18 16:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
l_error("netconfig: Empty IPv4.DNS entry, skipping...");
|
|
|
|
}
|
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
dns6_overrides = l_settings_get_string_list(active_settings,
|
2020-11-18 16:58:28 +01:00
|
|
|
"IPv6", "DNS", ' ');
|
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
if (dns6_overrides) {
|
|
|
|
int r = validate_dns_list(AF_INET6, dns6_overrides);
|
|
|
|
|
|
|
|
if (unlikely(r <= 0)) {
|
|
|
|
l_strfreev(dns6_overrides);
|
|
|
|
dns6_overrides = NULL;
|
2020-11-18 16:58:28 +01:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
if (r < 0)
|
2021-10-21 10:50:35 +02:00
|
|
|
return false;
|
2020-11-18 16:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
l_error("netconfig: Empty IPv6.DNS entry, skipping...");
|
|
|
|
}
|
|
|
|
|
2021-06-18 19:40:46 +02:00
|
|
|
if (!l_settings_get_bool(active_settings,
|
|
|
|
"IPv4", "SendHostname", &send_hostname))
|
|
|
|
send_hostname = false;
|
|
|
|
|
|
|
|
if (send_hostname) {
|
2021-08-27 05:02:03 +02:00
|
|
|
if (gethostname(hostname, sizeof(hostname)) != 0) {
|
2021-06-18 19:40:46 +02:00
|
|
|
l_warn("netconfig: Unable to get hostname. "
|
|
|
|
"Error %d: %s", errno, strerror(errno));
|
2021-08-27 05:02:03 +02:00
|
|
|
send_hostname = false;
|
2021-06-18 19:40:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
mdns = l_settings_get_string(active_settings,
|
|
|
|
"Network", "MulticastDNS");
|
2019-06-26 00:21:00 +02:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
if (l_settings_has_key(active_settings, "IPv4", "Address")) {
|
|
|
|
v4_address = netconfig_get_static4_address(active_settings);
|
|
|
|
|
|
|
|
if (unlikely(!v4_address)) {
|
|
|
|
l_error("netconfig: Can't parse IPv4 address");
|
2021-10-21 10:50:35 +02:00
|
|
|
return false;
|
2021-08-27 05:02:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!l_settings_get_bool(active_settings, "IPv6",
|
|
|
|
"Enabled", &v6_enabled))
|
|
|
|
v6_enabled = ipv6_enabled;
|
|
|
|
|
|
|
|
if (l_settings_has_key(active_settings, "IPv6", "Address")) {
|
|
|
|
v6_address = netconfig_get_static6_address(active_settings);
|
|
|
|
|
|
|
|
if (unlikely(!v6_address)) {
|
|
|
|
l_error("netconfig: Can't parse IPv6 address");
|
2021-10-21 10:50:35 +02:00
|
|
|
return false;
|
2021-08-27 05:02:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No more validation steps for now, commit new values */
|
2021-09-29 22:54:41 +02:00
|
|
|
netconfig->rtm_protocol = v4_address ? RTPROT_STATIC : RTPROT_DHCP;
|
2021-08-27 05:02:03 +02:00
|
|
|
|
|
|
|
if (!v6_enabled)
|
|
|
|
netconfig->rtm_v6_protocol = RTPROT_UNSPEC;
|
|
|
|
else if (v6_address)
|
|
|
|
netconfig->rtm_v6_protocol = RTPROT_STATIC;
|
|
|
|
else
|
|
|
|
netconfig->rtm_v6_protocol = RTPROT_DHCP;
|
|
|
|
|
2021-09-29 22:54:41 +02:00
|
|
|
netconfig_free_settings(netconfig);
|
|
|
|
|
|
|
|
if (netconfig->rtm_protocol == RTPROT_STATIC)
|
2021-10-21 10:50:35 +02:00
|
|
|
netconfig->v4_address = l_steal_ptr(v4_address);
|
2021-09-29 22:54:41 +02:00
|
|
|
|
2021-10-21 10:50:31 +02:00
|
|
|
if (netconfig->rtm_v6_protocol == RTPROT_STATIC)
|
2021-10-21 10:50:35 +02:00
|
|
|
netconfig->v6_address = l_steal_ptr(v6_address);
|
2021-10-21 10:50:31 +02:00
|
|
|
|
2021-08-27 05:02:03 +02:00
|
|
|
netconfig->active_settings = active_settings;
|
2021-10-21 10:50:35 +02:00
|
|
|
netconfig->dns4_overrides = l_steal_ptr(dns4_overrides);
|
|
|
|
netconfig->dns6_overrides = l_steal_ptr(dns6_overrides);
|
|
|
|
netconfig->mdns = l_steal_ptr(mdns);
|
2021-08-27 05:02:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool netconfig_configure(struct netconfig *netconfig,
|
|
|
|
netconfig_notify_func_t notify, void *user_data)
|
|
|
|
{
|
|
|
|
netconfig->notify = notify;
|
|
|
|
netconfig->user_data = user_data;
|
|
|
|
|
2022-08-29 19:35:53 +02:00
|
|
|
/* TODO */
|
2021-08-27 05:02:03 +02:00
|
|
|
|
2021-09-29 23:06:52 +02:00
|
|
|
resolve_set_mdns(netconfig->resolve, netconfig->mdns);
|
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-17 02:49:47 +02:00
|
|
|
|
2021-11-03 23:15:02 +01:00
|
|
|
bool netconfig_reconfigure(struct netconfig *netconfig, bool set_arp_gw)
|
2019-09-30 18:35:51 +02:00
|
|
|
{
|
2021-10-14 00:49:18 +02:00
|
|
|
/*
|
|
|
|
* Starting with kernel 4.20, ARP cache is flushed when the netdev
|
|
|
|
* detects NO CARRIER. This can result in unnecessarily long delays
|
|
|
|
* (about 1 second on some networks) due to ARP query response being
|
|
|
|
* lost or delayed. Try to force the gateway into the ARP cache
|
|
|
|
* to alleviate this
|
|
|
|
*/
|
2021-11-03 23:15:02 +01:00
|
|
|
if (set_arp_gw)
|
|
|
|
netconfig_gateway_to_arp(netconfig);
|
2021-10-14 00:49:18 +02:00
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
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)
|
|
|
|
{
|
2020-12-10 22:22:09 +01:00
|
|
|
netconfig_reset_v4(netconfig);
|
2019-09-30 18:35:51 +02:00
|
|
|
|
2020-11-19 20:52:56 +01:00
|
|
|
if (netconfig->rtm_v6_protocol) {
|
2021-10-21 10:50:31 +02:00
|
|
|
l_rtnl_address_free(netconfig->v6_address);
|
|
|
|
netconfig->v6_address = NULL;
|
|
|
|
|
2021-10-21 10:50:33 +02:00
|
|
|
l_strv_free(l_steal_ptr(netconfig->dns6_overrides));
|
|
|
|
l_strv_free(l_steal_ptr(netconfig->dns6_list));
|
2019-09-30 18:35:51 +02:00
|
|
|
|
2020-11-19 20:52:56 +01:00
|
|
|
netconfig->rtm_v6_protocol = 0;
|
2020-11-18 16:58:28 +01:00
|
|
|
|
2021-10-21 10:50:32 +02:00
|
|
|
l_free(l_steal_ptr(netconfig->v6_gateway_str));
|
2021-10-21 10:50:34 +02:00
|
|
|
|
|
|
|
l_strv_free(l_steal_ptr(netconfig->v6_domains));
|
2020-11-19 20:52:56 +01:00
|
|
|
}
|
2020-11-13 22:24:17 +01:00
|
|
|
|
2021-08-27 05:02:04 +02:00
|
|
|
l_free(l_steal_ptr(netconfig->fils_override));
|
|
|
|
|
2019-09-30 18:35:51 +02:00
|
|
|
return true;
|
2019-06-26 00:20:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 03:00:50 +02:00
|
|
|
char *netconfig_get_dhcp_server_ipv4(struct netconfig *netconfig)
|
|
|
|
{
|
2022-08-29 19:35:53 +02:00
|
|
|
struct l_dhcp_client *client = NULL; /* TODO */
|
2020-07-11 03:00:50 +02:00
|
|
|
const struct l_dhcp_lease *lease;
|
|
|
|
|
2022-08-29 19:35:53 +02:00
|
|
|
lease = l_dhcp_client_get_lease(client);
|
2020-07-11 03:00:50 +02:00
|
|
|
if (!lease)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return l_dhcp_lease_get_server_id(lease);
|
|
|
|
}
|
|
|
|
|
2021-08-27 05:02:04 +02:00
|
|
|
bool netconfig_get_fils_ip_req(struct netconfig *netconfig,
|
|
|
|
struct ie_fils_ip_addr_request_info *info)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Fill in the fields used for building the FILS IP Address Assigment
|
|
|
|
* IE during connection if we're configured to do automatic network
|
|
|
|
* configuration (usually DHCP). If we're configured with static
|
|
|
|
* values return false to mean the IE should not be sent.
|
|
|
|
*/
|
|
|
|
if (netconfig->rtm_protocol != RTPROT_DHCP &&
|
|
|
|
netconfig->rtm_v6_protocol != RTPROT_DHCP)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memset(info, 0, sizeof(*info));
|
|
|
|
info->ipv4 = (netconfig->rtm_protocol == RTPROT_DHCP);
|
|
|
|
info->ipv6 = (netconfig->rtm_v6_protocol == RTPROT_DHCP);
|
|
|
|
info->dns = (info->ipv4 && !netconfig->dns4_overrides) ||
|
|
|
|
(info->ipv6 && !netconfig->dns6_overrides);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void netconfig_handle_fils_ip_resp(struct netconfig *netconfig,
|
|
|
|
const struct ie_fils_ip_addr_response_info *info)
|
|
|
|
{
|
|
|
|
l_free(netconfig->fils_override);
|
|
|
|
netconfig->fils_override = l_memdup(info, sizeof(*info));
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:52:17 +02:00
|
|
|
struct netconfig *netconfig_new(uint32_t ifindex)
|
2019-06-26 00:20:52 +02:00
|
|
|
{
|
2020-11-05 16:38:32 +01:00
|
|
|
struct netdev *netdev = netdev_find(ifindex);
|
2019-06-26 00:20:52 +02:00
|
|
|
struct netconfig *netconfig;
|
2022-05-18 16:39:14 +02:00
|
|
|
const char *debug_level = NULL;
|
|
|
|
int dhcp_priority = L_LOG_INFO;
|
2022-08-29 19:35:53 +02:00
|
|
|
struct l_dhcp_client *dhcp = NULL; /* TODO */
|
|
|
|
struct l_dhcp6_client *dhcp6 = NULL; /* TODO */
|
2019-06-26 00:20:52 +02:00
|
|
|
|
|
|
|
l_debug("Starting netconfig for interface: %d", ifindex);
|
|
|
|
|
|
|
|
netconfig = l_new(struct netconfig, 1);
|
|
|
|
netconfig->ifindex = ifindex;
|
2020-08-21 21:50:44 +02:00
|
|
|
netconfig->resolve = resolve_new(ifindex);
|
2019-06-26 00:20:52 +02:00
|
|
|
|
2022-05-18 16:39:14 +02:00
|
|
|
debug_level = getenv("IWD_DHCP_DEBUG");
|
|
|
|
if (debug_level != NULL) {
|
|
|
|
if (!strcmp("debug", debug_level))
|
|
|
|
dhcp_priority = L_LOG_DEBUG;
|
|
|
|
else if (!strcmp("info", debug_level))
|
|
|
|
dhcp_priority = L_LOG_INFO;
|
|
|
|
else if (!strcmp("warn", debug_level))
|
|
|
|
dhcp_priority = L_LOG_WARNING;
|
|
|
|
else if (!strcmp("error", debug_level))
|
|
|
|
dhcp_priority = L_LOG_ERR;
|
|
|
|
else /* Default for backwards compatibility */
|
|
|
|
dhcp_priority = L_LOG_DEBUG;
|
|
|
|
}
|
|
|
|
|
2022-08-29 19:35:53 +02:00
|
|
|
l_dhcp_client_set_debug(dhcp, do_debug, "[DHCPv4] ", NULL,
|
|
|
|
dhcp_priority);
|
2020-11-05 16:38:32 +01:00
|
|
|
|
2022-08-29 19:35:53 +02:00
|
|
|
l_dhcp6_client_set_lla_randomized(dhcp6, true);
|
|
|
|
l_dhcp6_client_set_nodelay(dhcp6, true);
|
2020-11-05 16:38:32 +01:00
|
|
|
|
|
|
|
if (getenv("IWD_DHCP_DEBUG"))
|
2022-08-29 19:35:53 +02:00
|
|
|
l_dhcp6_client_set_debug(dhcp6, do_debug, "[DHCPv6] ", NULL);
|
2020-11-05 16:38:32 +01:00
|
|
|
|
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
|
|
|
{
|
2019-12-17 23:15:05 +01:00
|
|
|
l_debug("");
|
2019-06-26 00:20:52 +02:00
|
|
|
|
2020-11-19 20:52:56 +01:00
|
|
|
netconfig_reset(netconfig);
|
2020-08-21 21:50:44 +02:00
|
|
|
resolve_free(netconfig->resolve);
|
2019-09-27 21:52:16 +02:00
|
|
|
netconfig_free(netconfig);
|
2019-06-26 00:20:52 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 10:50:30 +02:00
|
|
|
bool netconfig_enabled(void)
|
|
|
|
{
|
|
|
|
bool enabled;
|
|
|
|
|
|
|
|
return l_settings_get_bool(iwd_get_config(), "General",
|
|
|
|
"EnableNetworkConfiguration",
|
|
|
|
&enabled) && enabled;
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:20:52 +02:00
|
|
|
static int netconfig_init(void)
|
|
|
|
{
|
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;
|
|
|
|
|
2020-11-13 22:24:17 +01:00
|
|
|
if (!l_settings_get_bool(iwd_get_config(), "Network",
|
|
|
|
"EnableIPv6",
|
|
|
|
&ipv6_enabled))
|
|
|
|
ipv6_enabled = false;
|
|
|
|
|
2019-06-26 00:20:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netconfig_exit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IWD_MODULE(netconfig, netconfig_init, netconfig_exit)
|