network: introduce link_get_use_ntp()

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2024-04-16 10:13:14 +09:00
parent 8763e48342
commit d12fb2bc7b
10 changed files with 62 additions and 71 deletions

View File

@@ -18,6 +18,7 @@
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-ntp.h"
#include "networkd-queue.h"
#include "networkd-route-util.h"
#include "parse-util.h"
@@ -365,7 +366,7 @@ static int link_push_uplink_to_dhcp_server(
addresses[n_addresses++] = ia.in;
}
use_dhcp_lease_data = link->network->dhcp_use_ntp;
use_dhcp_lease_data = link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4);
break;
}

View File

@@ -20,6 +20,7 @@
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-ntp.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-setlink.h"
@@ -749,7 +750,7 @@ static int dhcp4_request_routes_to_ntp(Link *link) {
assert(link->dhcp_lease);
assert(link->network);
if (!link->network->dhcp_use_ntp ||
if (!link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4) ||
!link->network->dhcp_routes_to_ntp)
return 0;
@@ -1546,7 +1547,7 @@ static int dhcp4_configure(Link *link) {
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for domain search list: %m");
}
if (link->network->dhcp_use_ntp) {
if (link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for NTP server: %m");

View File

@@ -14,6 +14,7 @@
#include "networkd-dhcp6.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-ntp.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-state-file.h"
@@ -651,7 +652,7 @@ static int dhcp6_configure(Link *link) {
return log_link_debug_errno(link, r, "DHCPv6 CLIENT: Failed to request captive portal: %m");
}
if (link->network->dhcp6_use_ntp) {
if (link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NTP_SERVER);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv6 CLIENT: Failed to request NTP servers: %m");

View File

@@ -17,6 +17,7 @@
#include "networkd-neighbor.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-ntp.h"
#include "networkd-route-util.h"
#include "networkd-route.h"
#include "networkd-routing-policy-rule.h"
@@ -564,7 +565,7 @@ static int ntp_append_json(Link *link, JsonVariant **v) {
}
if (!link->ntp) {
if (link->dhcp_lease && link->network->dhcp_use_ntp) {
if (link->dhcp_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
const struct in_addr *ntp;
union in_addr_union s;
int n_ntp;
@@ -585,7 +586,7 @@ static int ntp_append_json(Link *link, JsonVariant **v) {
}
}
if (link->dhcp6_lease && link->network->dhcp6_use_ntp) {
if (link->dhcp6_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
const struct in6_addr *ntp_addr;
union in_addr_union s;
char **ntp_fqdn;

View File

@@ -225,7 +225,7 @@ DHCPv4.RequestAddress, config_parse_in_addr_non_null,
DHCPv4.ClientIdentifier, config_parse_dhcp_client_identifier, 0, offsetof(Network, dhcp_client_identifier)
DHCPv4.UseDNS, config_parse_tristate, 0, offsetof(Network, dhcp_use_dns)
DHCPv4.RoutesToDNS, config_parse_bool, 0, offsetof(Network, dhcp_routes_to_dns)
DHCPv4.UseNTP, config_parse_dhcp_use_ntp, AF_INET, 0
DHCPv4.UseNTP, config_parse_tristate, 0, offsetof(Network, dhcp_use_ntp)
DHCPv4.RoutesToNTP, config_parse_bool, 0, offsetof(Network, dhcp_routes_to_ntp)
DHCPv4.UseSIP, config_parse_bool, 0, offsetof(Network, dhcp_use_sip)
DHCPv4.UseCaptivePortal, config_parse_bool, 0, offsetof(Network, dhcp_use_captive_portal)
@@ -275,7 +275,7 @@ DHCPv6.UseDelegatedPrefix, config_parse_bool,
DHCPv6.UseDNS, config_parse_tristate, 0, offsetof(Network, dhcp6_use_dns)
DHCPv6.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp6_use_hostname)
DHCPv6.UseDomains, config_parse_use_domains, 0, offsetof(Network, dhcp6_use_domains)
DHCPv6.UseNTP, config_parse_dhcp_use_ntp, AF_INET6, 0
DHCPv6.UseNTP, config_parse_tristate, 0, offsetof(Network, dhcp6_use_ntp)
DHCPv6.UseCaptivePortal, config_parse_bool, 0, offsetof(Network, dhcp6_use_captive_portal)
DHCPv6.MUDURL, config_parse_mud_url, 0, offsetof(Network, dhcp6_mudurl)
DHCPv6.SendHostname, config_parse_dhcp_send_hostname, AF_INET6, 0
@@ -590,7 +590,7 @@ IPv6PrefixDelegation.DNSLifetimeSec, config_parse_sec,
DHCPv4.BlackList, config_parse_in_addr_prefixes, AF_INET, offsetof(Network, dhcp_deny_listed_ip)
DHCP.ClientIdentifier, config_parse_dhcp_client_identifier, 0, offsetof(Network, dhcp_client_identifier)
DHCP.UseDNS, config_parse_tristate, 0, offsetof(Network, compat_dhcp_use_dns)
DHCP.UseNTP, config_parse_dhcp_use_ntp, AF_UNSPEC, 0
DHCP.UseNTP, config_parse_tristate, 0, offsetof(Network, compat_dhcp_use_ntp)
DHCP.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_use_mtu)
DHCP.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_use_hostname)
DHCP.UseDomains, config_parse_use_domains, 0, offsetof(Network, compat_dhcp_use_domains)

View File

@@ -379,10 +379,11 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.compat_dhcp_use_domains = _USE_DOMAINS_INVALID,
.compat_dhcp_use_dns = -1,
.compat_dhcp_use_ntp = -1,
.dhcp_duid.type = _DUID_TYPE_INVALID,
.dhcp_critical = -1,
.dhcp_use_ntp = true,
.dhcp_use_ntp = -1,
.dhcp_routes_to_ntp = true,
.dhcp_use_sip = true,
.dhcp_use_captive_portal = true,
@@ -407,7 +408,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.dhcp6_use_dns = -1,
.dhcp6_use_domains = _USE_DOMAINS_INVALID,
.dhcp6_use_hostname = true,
.dhcp6_use_ntp = true,
.dhcp6_use_ntp = -1,
.dhcp6_use_captive_portal = true,
.dhcp6_use_rapid_commit = true,
.dhcp6_send_hostname = true,

View File

@@ -116,6 +116,7 @@ struct Network {
/* For backward compatibility, only applied to DHCPv4 and DHCPv6. */
UseDomains compat_dhcp_use_domains;
int compat_dhcp_use_dns;
int compat_dhcp_use_ntp;
/* DHCP Client Support */
AddressFamily dhcp;
@@ -150,8 +151,7 @@ struct Network {
int dhcp_use_rapid_commit;
int dhcp_use_dns;
bool dhcp_routes_to_dns;
bool dhcp_use_ntp;
bool dhcp_use_ntp_set;
int dhcp_use_ntp;
bool dhcp_routes_to_ntp;
bool dhcp_use_sip;
bool dhcp_use_captive_portal;
@@ -182,8 +182,7 @@ struct Network {
bool dhcp6_send_hostname_set;
int dhcp6_use_dns;
bool dhcp6_use_hostname;
bool dhcp6_use_ntp;
bool dhcp6_use_ntp_set;
int dhcp6_use_ntp;
bool dhcp6_use_captive_portal;
bool dhcp6_use_rapid_commit;
UseDomains dhcp6_use_domains;

View File

@@ -9,6 +9,39 @@
/* Let's assume that anything above this number is a user misconfiguration. */
#define MAX_NTP_SERVERS 128U
bool link_get_use_ntp(Link *link, NetworkConfigSource proto) {
int n, c;
assert(link);
if (!link->network)
return false;
switch (proto) {
case NETWORK_CONFIG_SOURCE_DHCP4:
n = link->network->dhcp_use_ntp;
c = link->network->compat_dhcp_use_ntp;
break;
case NETWORK_CONFIG_SOURCE_DHCP6:
n = link->network->dhcp6_use_ntp;
c = link->network->compat_dhcp_use_ntp;
break;
default:
assert_not_reached();
}
/* If per-network and per-protocol setting is specified, use it. */
if (n >= 0)
return n;
/* If compat setting is specified, use it. */
if (c >= 0)
return c;
/* Otherwise, defaults to yes. */
return true;
}
int config_parse_ntp(
const char *unit,
const char *filename,
@@ -66,54 +99,3 @@ int config_parse_ntp(
return log_oom();
}
}
int config_parse_dhcp_use_ntp(
const char* unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
int r;
assert(filename);
assert(lvalue);
assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
assert(rvalue);
assert(data);
r = parse_boolean(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse UseNTP=%s, ignoring assignment: %m", rvalue);
return 0;
}
switch (ltype) {
case AF_INET:
network->dhcp_use_ntp = r;
network->dhcp_use_ntp_set = true;
break;
case AF_INET6:
network->dhcp6_use_ntp = r;
network->dhcp6_use_ntp_set = true;
break;
case AF_UNSPEC:
/* For backward compatibility. */
if (!network->dhcp_use_ntp_set)
network->dhcp_use_ntp = r;
if (!network->dhcp6_use_ntp_set)
network->dhcp6_use_ntp = r;
break;
default:
assert_not_reached();
}
return 0;
}

View File

@@ -2,6 +2,10 @@
#pragma once
#include "conf-parser.h"
#include "networkd-util.h"
typedef struct Link Link;
bool link_get_use_ntp(Link *link, NetworkConfigSource proto);
CONFIG_PARSER_PROTOTYPE(config_parse_ntp);
CONFIG_PARSER_PROTOTYPE(config_parse_dhcp_use_ntp);

View File

@@ -15,6 +15,7 @@
#include "networkd-manager-bus.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-ntp.h"
#include "networkd-state-file.h"
#include "ordered-set.h"
#include "set.h"
@@ -150,7 +151,7 @@ static int link_put_ntp(Link *link, OrderedSet **s) {
if (r < 0)
return r;
if (link->dhcp_lease && link->network->dhcp_use_ntp) {
if (link->dhcp_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
const struct in_addr *addresses;
r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
@@ -161,7 +162,7 @@ static int link_put_ntp(Link *link, OrderedSet **s) {
}
}
if (link->dhcp6_lease && link->network->dhcp6_use_ntp) {
if (link->dhcp6_lease && link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP6)) {
const struct in6_addr *addresses;
char **fqdn;
@@ -693,10 +694,10 @@ static int link_save(Link *link) {
serialize_addresses(f, "NTP", NULL,
link->network->ntp,
link->dhcp_lease,
link->network->dhcp_use_ntp,
link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4),
SD_DHCP_LEASE_NTP,
link->dhcp6_lease,
link->network->dhcp6_use_ntp,
link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP6),
sd_dhcp6_lease_get_ntp_addrs,
sd_dhcp6_lease_get_ntp_fqdn);