| [chrony-dev] [RFC PATCH 2/9] util: include port in hash of addresses |
[ Thread Index |
Date Index
| More chrony.tuxfamily.org/chrony-dev Archives
]
- To: chrony-dev@xxxxxxxxxxxxxxxxxxxx
- Subject: [chrony-dev] [RFC PATCH 2/9] util: include port in hash of addresses
- From: Wojtek Wasko <wwasko@xxxxxxxxxx>
- Date: Thu, 12 Nov 2026 17:22:31 +0100
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=B/5RO6lVUWlDUs7f7mPDJoNaSai+KuEx/kKsuav/sGE=; b=iHpSC+8Z6YrgHvM9jmA9Jdp7SIQFdUThysJf6jPoOLFGDO/Otsqab7isNNEHT6M+1Rs+p451+Pt5tviEePmRGsEH9m762dzTHBe1c0HU34GwieL5zK95v1Ogc9LVAzF7xkt8vYL2oqpSuetNDQzi0dkLvTxjaG3AP78PGzUI+UVZw3Shu+Nk33O71jpsc3w9/Qx6t0+cqBmTQIuiN6DKDjl3u3vZJeWrEJwxqfhv66zjRjdkLpU13G3M1MCfwxsrLePLeOljyg/NSReW+3/T2jdqmBt/4VHVzLCNMR9+5HGx2ds0jNaFhhEnudn6crP8JsU2bMl6rkXkO9lWMnqU2A==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=c7qewHqWFkW2YWomyLox7cr/0Iooly1VcXoLN4rX2srRLi0Pniyob7/K/awqNEXmuho8FoO18LZDkjjZYrkluUiIlCe0g87wT51SKY2blTtyHckPtpmnXS3FXQoWfDTT6D3jc91ryiUQf5d1BCv49pd4QXIflgxSUKFDNdsu5gQcAEvW2GQiaJ3/1YZ3gQIYAGT1JexH5a7IKMluU5PE/zgAIjGeKEvKGFvuoQNNUxHhZ2VUSAlngp6EmrOgbVMr9UG1yfh1iuYZA2jPTTCgYZBHzyZEUZoxX2KFbq2N6IiKLJ3XXuz32qKL1VrRTmfpIfbSOEWSNR6WdQjnY/1EYA==
- Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com;
- Cc: Wojtek Wasko <wwasko@xxxxxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=B/5RO6lVUWlDUs7f7mPDJoNaSai+KuEx/kKsuav/sGE=; b=XNpVzeJShmvWclP54T8OAgPd52gM8YrNsvdr9HTCNNGMDWw7sTVopI9OZ9BA2SAHgw5ijgytLVFIBw8yzcneNfKeKR/nMoi19zW9qnozamNSAdr2m0uzCAOF4uEfklSdH8oKxE9bvtheRI+9yyYr8xFJuPEz0VeuPN8idXxF45fssG154CSIGotx8pzaiyZTws8pVmN3UfvFc7WYFi8c8wR0qyopNBzB6oVwApU0Q8tqLZcF8EAGyIM+9e4ePW1ygjc1v1vPpgPm+uf3vmOk7QC8gRn74awVu3VJJU7TVBT05xOFsxkVG4jGIoFAMo5cmeI+xxFLoFfyEUvDdP9gQA==
UTI_IPToHash() hashes an IP address. It is used in the table of NTP
sources and the table of clients in clientlog.
A server cannot be identified by its IP address alone, e.g. with NTP and
NTP over PTP using different ports, and a hash of the address alone
encourages such identification.
Replace UTI_IPToHash() with UTI_IPSockAddrToHash(), which includes the
port in the hash. Use a zero port for clients, which are identified by
their address only, and for NTP sources for now.
---
clientlog.c | 7 ++++++-
ntp_sources.c | 5 ++++-
test/unit/ntp_sources.c | 11 ++++++-----
test/unit/util.c | 16 +++++++++++-----
util.c | 5 ++++-
util.h | 2 +-
6 files changed, 32 insertions(+), 14 deletions(-)
diff --git a/clientlog.c b/clientlog.c
index fbdb14b..12d06c0 100644
--- a/clientlog.c
+++ b/clientlog.c
@@ -238,13 +238,18 @@ get_record(IPAddr *ip)
uint32_t last_hit = 0, oldest_hit = 0;
Record *record, *oldest_record;
unsigned int first, i, j;
+ IPSockAddr addr;
if (!active || (ip->family != IPADDR_INET4 && ip->family != IPADDR_INET6))
return NULL;
+ /* Clients are identified by their address only */
+ addr.ip_addr = *ip;
+ addr.port = 0;
+
while (1) {
/* Get index of the first record in the slot */
- first = UTI_IPToHash(ip) % slots * SLOT_SIZE;
+ first = UTI_IPSockAddrToHash(&addr) % slots * SLOT_SIZE;
for (i = 0, oldest_record = NULL; i < SLOT_SIZE; i++) {
record = ARR_GetElement(records, first + i);
diff --git a/ntp_sources.c b/ntp_sources.c
index c120118..7545d4f 100644
--- a/ntp_sources.c
+++ b/ntp_sources.c
@@ -239,6 +239,7 @@ static int
find_slot(IPAddr *ip_addr, int *slot)
{
SourceRecord *record;
+ IPSockAddr addr;
uint32_t hash;
unsigned int i, size;
@@ -255,7 +256,9 @@ find_slot(IPAddr *ip_addr, int *slot)
return 0;
}
- hash = UTI_IPToHash(ip_addr);
+ addr.ip_addr = *ip_addr;
+ addr.port = 0;
+ hash = UTI_IPSockAddrToHash(&addr);
for (i = 0; i < size / 2; i++) {
/* Use quadratic probing */
diff --git a/test/unit/ntp_sources.c b/test/unit/ntp_sources.c
index 5038675..4094b21 100644
--- a/test/unit/ntp_sources.c
+++ b/test/unit/ntp_sources.c
@@ -160,7 +160,8 @@ test_unit(void)
while (1) {
do {
TST_GetRandomAddress(&addrs[j].ip_addr, IPADDR_UNSPEC, -1);
- } while (UTI_IPToHash(&addrs[j].ip_addr) % (1U << i) != hash % (1U << i));
+ addrs[j].port = 0;
+ } while (UTI_IPSockAddrToHash(&addrs[j]) % (1U << i) != hash % (1U << i));
for (k = 0; k < j; k++)
if (UTI_CompareIPs(&addrs[k].ip_addr, &addrs[j].ip_addr, NULL) == 0)
@@ -169,13 +170,13 @@ test_unit(void)
break;
}
- addrs[j].port = random() % 1024;
-
if (!j)
- hash = UTI_IPToHash(&addrs[j].ip_addr);
+ hash = UTI_IPSockAddrToHash(&addrs[j]);
DEBUG_LOG("adding source %s hash %"PRIu32, UTI_IPToString(&addrs[j].ip_addr),
- UTI_IPToHash(&addrs[j].ip_addr) % (1U << i));
+ UTI_IPSockAddrToHash(&addrs[j]) % (1U << i));
+
+ addrs[j].port = random() % 1024;
status = NSR_AddSource(&addrs[j], random() % 2 ? NTP_SERVER : NTP_PEER,
&source.params, NULL);
diff --git a/test/unit/util.c b/test/unit/util.c
index 7f8ad0d..b215221 100644
--- a/test/unit/util.c
+++ b/test/unit/util.c
@@ -384,18 +384,21 @@ test_unit(void)
s = UTI_RefidToString(0x41424344);
TEST_CHECK(strcmp(s, "ABCD") == 0);
+ ip_saddr.port = 123;
ip.family = IPADDR_UNSPEC;
s = UTI_IPToString(&ip);
TEST_CHECK(strcmp(s, "[UNSPEC]") == 0);
TEST_CHECK(UTI_IPToRefid(&ip) == 0);
- TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+ ip_saddr.ip_addr = ip;
+ TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
ip.family = IPADDR_INET4;
ip.addr.in4 = 0x7f010203;
s = UTI_IPToString(&ip);
TEST_CHECK(strcmp(s, "127.1.2.3") == 0);
TEST_CHECK(UTI_IPToRefid(&ip) == 0x7f010203);
- TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+ ip_saddr.ip_addr = ip;
+ TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
ip.family = IPADDR_INET6;
memset(&ip.addr.in6, 0, sizeof (ip.addr.in6));
@@ -408,20 +411,23 @@ test_unit(void)
TEST_CHECK(strcmp(s, "ab00:0000:0000:0000:0000:0000:0000:00cd") == 0);
#endif
TEST_CHECK(UTI_IPToRefid(&ip) == 0x5f9aa602);
- TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+ ip_saddr.ip_addr = ip;
+ TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
ip.family = IPADDR_ID;
ip.addr.id = 12345;
s = UTI_IPToString(&ip);
TEST_CHECK(strcmp(s, "ID#0000012345") == 0);
TEST_CHECK(UTI_IPToRefid(&ip) == 0);
- TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+ ip_saddr.ip_addr = ip;
+ TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
ip.family = IPADDR_UNSPEC + 10;
s = UTI_IPToString(&ip);
TEST_CHECK(strcmp(s, "[UNKNOWN]") == 0);
TEST_CHECK(UTI_IPToRefid(&ip) == 0);
- TEST_CHECK(UTI_IPToHash(&ip) == UTI_IPToHash(&ip));
+ ip_saddr.ip_addr = ip;
+ TEST_CHECK(UTI_IPSockAddrToHash(&ip_saddr) == UTI_IPSockAddrToHash(&ip_saddr));
TEST_CHECK(UTI_StringToIP("200.4.5.6", &ip));
TEST_CHECK(ip.family == IPADDR_INET4);
diff --git a/util.c b/util.c
index 38516ea..cff4c3a 100644
--- a/util.c
+++ b/util.c
@@ -435,9 +435,10 @@ UTI_IPToRefid(const IPAddr *ip)
/* ================================================== */
uint32_t
-UTI_IPToHash(const IPAddr *ip)
+UTI_IPSockAddrToHash(const IPSockAddr *sa)
{
static uint32_t seed = 0;
+ const IPAddr *ip = &sa->ip_addr;
const unsigned char *addr;
unsigned int i, len;
uint32_t hash;
@@ -467,6 +468,8 @@ UTI_IPToHash(const IPAddr *ip)
/* Calculate the FNV-1a 32-bit hash */
for (i = 0, hash = seed; i < len; i++)
hash = (hash ^ addr[i]) * 16777619;
+ hash = (hash ^ (sa->port & 0xff)) * 16777619;
+ hash = (hash ^ (sa->port >> 8)) * 16777619;
return hash;
}
diff --git a/util.h b/util.h
index 3bebe34..21cccc2 100644
--- a/util.h
+++ b/util.h
@@ -122,7 +122,7 @@ extern int UTI_IsStringIP(const char *string);
extern int UTI_StringToIdIP(const char *addr, IPAddr *ip);
extern int UTI_IsIPReal(const IPAddr *ip);
extern uint32_t UTI_IPToRefid(const IPAddr *ip);
-extern uint32_t UTI_IPToHash(const IPAddr *ip);
+extern uint32_t UTI_IPSockAddrToHash(const IPSockAddr *sa);
extern void UTI_IPHostToNetwork(const IPAddr *src, IPAddr *dest);
extern void UTI_IPNetworkToHost(const IPAddr *src, IPAddr *dest);
extern int UTI_CompareIPs(const IPAddr *a, const IPAddr *b, const IPAddr *mask);
--
2.43.0
--
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble? Email listmaster@xxxxxxxxxxxxxxxxxxxx.