From 018a1c42b050690ee57a21c35bee5cdaa0f110a2 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Fri, 16 Jun 2017 12:16:17 +0200 Subject: [PATCH] ntp: suggest clients to increase their polling interval When the poll value in a client request is smaller than the server's NTP rate limiting interval, set poll in the response to the rate limiting interval to suggest the client to increase its polling interval. This follows ntpd as a server. No current client implementation seems to be increasing its interval by the poll, but it may change in the future. --- clientlog.c | 13 +++++++++++++ clientlog.h | 1 + ntp_core.c | 9 +++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/clientlog.c b/clientlog.c index 63d932e..5288fa8 100644 --- a/clientlog.c +++ b/clientlog.c @@ -128,6 +128,9 @@ static int cmd_leak_rate; /* Flag indicating whether the last response was dropped */ #define FLAG_NTP_DROPPED 0x1 +/* NTP limit interval in log2 */ +static int ntp_limit_interval; + /* Flag indicating whether facility is turned on or not */ static int active; @@ -309,11 +312,13 @@ CLG_Initialise(void) ntp_tokens_per_packet = cmd_tokens_per_packet = 0; ntp_token_shift = cmd_token_shift = 0; ntp_leak_rate = cmd_leak_rate = 0; + ntp_limit_interval = MIN_LIMIT_INTERVAL; if (CNF_GetNTPRateLimit(&interval, &burst, &leak_rate)) { set_bucket_params(interval, burst, &max_ntp_tokens, &ntp_tokens_per_packet, &ntp_token_shift); ntp_leak_rate = CLAMP(MIN_LEAK_RATE, leak_rate, MAX_LEAK_RATE); + ntp_limit_interval = CLAMP(MIN_LIMIT_INTERVAL, interval, MAX_LIMIT_INTERVAL); } if (CNF_GetCommandRateLimit(&interval, &burst, &leak_rate)) { @@ -606,6 +611,14 @@ void CLG_GetNtpTimestamps(int index, NTP_int64 **rx_ts, NTP_int64 **tx_ts) /* ================================================== */ +int +CLG_GetNtpMinPoll(void) +{ + return ntp_limit_interval; +} + +/* ================================================== */ + int CLG_GetNumberOfIndices(void) { diff --git a/clientlog.h b/clientlog.h index 337b4d8..552c767 100644 --- a/clientlog.h +++ b/clientlog.h @@ -39,6 +39,7 @@ extern int CLG_LogCommandAccess(IPAddr *client, struct timespec *now); extern int CLG_LimitNTPResponseRate(int index); extern int CLG_LimitCommandResponseRate(int index); extern void CLG_GetNtpTimestamps(int index, NTP_int64 **rx_ts, NTP_int64 **tx_ts); +extern int CLG_GetNtpMinPoll(void); /* And some reporting functions, for use by chronyc. */ diff --git a/ntp_core.c b/ntp_core.c index becb797..fc63c68 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -1860,7 +1860,7 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a NTP_Mode pkt_mode, my_mode; NTP_int64 *local_ntp_rx, *local_ntp_tx; NTP_Local_Timestamp local_tx, *tx_ts; - int valid_auth, log_index, interleaved; + int valid_auth, log_index, interleaved, poll; AuthenticationMode auth_mode; uint32_t key_id; @@ -1947,8 +1947,13 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a } } + /* Suggest the client to increase its polling interval if it indicates + the interval is shorter than the rate limiting interval */ + poll = CLG_GetNtpMinPoll(); + poll = MAX(poll, message->poll); + /* Send a reply */ - transmit_packet(my_mode, interleaved, message->poll, NTP_LVM_TO_VERSION(message->lvm), + transmit_packet(my_mode, interleaved, poll, NTP_LVM_TO_VERSION(message->lvm), auth_mode, key_id, &message->receive_ts, &message->transmit_ts, rx_ts, tx_ts, local_ntp_rx, NULL, remote_addr, local_addr);