From 5cb584d6c1aca4a38fe22fb3d7a8cccbc359a28b Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Mon, 5 Feb 2024 14:20:19 +0100 Subject: [PATCH] conf: add ipv4 and ipv6 options to server/pool/peer directive Accept "ipv4" and "ipv6" options in the server/pool/peer directive to use only IPv4 or IPv6 addresses respectively. The configuration is different from the "server [-4|-6] hostname" syntax supported by ntpd to avoid breaking existing scripts which expect the hostname to always be the first argument of the directives. --- cmdparse.c | 5 +++++ cmdparse.h | 1 + conf.c | 6 ++++-- doc/chrony.conf.adoc | 6 ++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cmdparse.c b/cmdparse.c index ac5ace2..0a80fc0 100644 --- a/cmdparse.c +++ b/cmdparse.c @@ -46,6 +46,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src) uint32_t ef_type; int n, sel_option; + src->family = IPADDR_UNSPEC; src->port = SRC_DEFAULT_PORT; src->params.minpoll = SRC_DEFAULT_MINPOLL; src->params.maxpoll = SRC_DEFAULT_MAXPOLL; @@ -127,6 +128,10 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src) } else if (!strcasecmp(cmd, "filter")) { if (sscanf(line, "%d%n", &src->params.filter_length, &n) != 1) return 0; + } else if (!strcasecmp(cmd, "ipv4")) { + src->family = IPADDR_INET4; + } else if (!strcasecmp(cmd, "ipv6")) { + src->family = IPADDR_INET6; } else if (!strcasecmp(cmd, "maxdelay")) { if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1) return 0; diff --git a/cmdparse.h b/cmdparse.h index 7a87979..095a8e2 100644 --- a/cmdparse.h +++ b/cmdparse.h @@ -32,6 +32,7 @@ typedef struct { char *name; + int family; int port; SourceParameters params; } CPS_NTP_Source; diff --git a/conf.c b/conf.c index 9bae265..73fe774 100644 --- a/conf.c +++ b/conf.c @@ -1664,6 +1664,8 @@ compare_sources(const void *a, const void *b) return d; if ((d = (int)sa->pool - (int)sb->pool) != 0) return d; + if ((d = (int)sa->params.family - (int)sb->params.family) != 0) + return d; if ((d = (int)sa->params.port - (int)sb->params.port) != 0) return d; return memcmp(&sa->params.params, &sb->params.params, sizeof (sa->params.params)); @@ -1728,7 +1730,7 @@ reload_source_dirs(void) /* Add new sources */ if (pass == 1 && d > 0) { source = &new_sources[j]; - s = NSR_AddSourceByName(source->params.name, IPADDR_UNSPEC, source->params.port, + s = NSR_AddSourceByName(source->params.name, source->params.family, source->params.port, source->pool, source->type, &source->params.params, &new_ids[j]); @@ -1843,7 +1845,7 @@ CNF_AddSources(void) for (i = 0; i < ARR_GetSize(ntp_sources); i++) { source = (NTP_Source *)ARR_GetElement(ntp_sources, i); - s = NSR_AddSourceByName(source->params.name, IPADDR_UNSPEC, source->params.port, + s = NSR_AddSourceByName(source->params.name, source->params.family, source->params.port, source->pool, source->type, &source->params.params, NULL); if (s != NSR_Success && s != NSR_UnresolvedName) LOG(LOGS_ERR, "Could not add source %s", source->params.name); diff --git a/doc/chrony.conf.adoc b/doc/chrony.conf.adoc index cb3f95c..eeaa501 100644 --- a/doc/chrony.conf.adoc +++ b/doc/chrony.conf.adoc @@ -343,6 +343,12 @@ the PTP port. The corrections are applied only to NTP measurements with HW timestamps (enabled by the <> directive). This field should be enabled only for servers known to be running *chronyd* version 4.5 or later. +*ipv4*::: +*ipv6*::: +These options force *chronyd* to use only IPv4 or IPv6 addresses respectively +for this source. They do not override the *-4* or *-6* option on the *chronyd* +command line. + {blank}::: [[pool]]*pool* _name_ [_option_]...::