fix: NTP Y2036 rollover - #40
Open
mattdibi wants to merge 1 commit into
Open
Conversation
* fix: NTP Y2036 rollover * style: fix pylint errors * feat: add method assumptions * test: test all range of possible values * style: simplify chained comparison * style: correct newline * refactor: improve readability and add docs * style: timestamp -> ntp_timestamp * fix: system_to_ntp_time * refactor: remove unused variables * test: add assertion to highligth timestamp ambiguity * docs: add note about ambiguity * test: added boundary dates * docs: some docs rewording * docs: fix wrong doc string * docs: remove wrong comment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes the NTP Y2036 rollover issue, interpreting NTP timestamp correctly after wrapping in 2036. The original fix was implemented in Eurotech’s fork here: eurotech#6, and we’d like to contribute it upstream.
The Problem
Contrary to what was said in #18 (comment) the
ntpliblibrary does support NTPv4 protocol correctly. As per RFC-5905 Section 7.3:The 128 bit date format described in RFC-5905 Section 6 is never transmitted over the line.
In other words: up to NTP protocol version 4, the era number is not transmitted in the NTP network packets. So, assuming the NTP server sends the appropriate second counts before and after the rollover, it is is the task of the NTP client to determine and apply the correct era number to determine a correct date after 2036-02-07.
Sources:
Notable datapoints and glossary
As described in RFC-5905 Section 6, NTP timestamps sent over the line are 64 bits long:
For the problem at hand we only care about the 32-bit integral part of the timestamps (i.e. the seconds).
The 136 years span (i.e. 2^32 seconds) is called NTP era. The NTP era spanning from 1/1/1900 (i.e. NTP prime epoch) to 7/2/2036 (i.e. the rollover date) is NTP era 0. This means that:
Reference: https://www.eecis.udel.edu/~mills/y2k.html
The Solution
This PR patches the client (i.e. the
ntpliblibrary) so that it interprets correctly the timestamp sent by NTP server after the 07/02/2036 date.The solution implemented in this PR simply assumes that, if we receive a timestamp from an NTP server which pre-dates the NTP era halfway point, we must have received a rolled-over timestamp and thus we must be in NTP era 1.
In other words: if we receive a timestamp representing a date before 21/01/1968, we arbitrarily decide to add 136 years (i.e. NTP era span) to the timestamp, thus obtaining dates past the Y2036 rollover date.
In other other words:
NTP Date = NTP Timestamp + NTP Era.This solution is similar to what was implemented in
ntpd. See:Limitations
ntpliblibrary containing this patch will interpret correctly NTP timestamps between the dates:NTP timestamps outside this range will be affected by the rollover effect and interpreted incorrectly.
This behaviour is consistent with the behaviour displayed by other tools (e.g. Wireshark, ntpd).
Testing
Setup
Raspberry Pi providing NTP server via Chrony
Configuration
/etc/chrony/chrony.conf:Script
Results
This PR should address the following: