NTP Working Group David L. Mills Technical Report 06-6-1 University of Delaware June 2006 Network Time Protocol Version 4 Reference and Implementation Guide Abstract This document describes the Network Time Protocol Version 4 (NTPv4), which is widely used to synchronize the time for Internet hosts, routers and ancillary devices to Coordinated Universal Time (UTC) as disseminated by national standards laboratories. It describes the core architecture, protocol, state machine, data structures and algorithms. It explains the fundamental on-wire protocol used to exchange time values between peers, servers and clients. It summarizes the clock offset, roundtrip delay and various other statistics used by the mitigation algorithms to calculate the maximum error and nominal error inherent in computing these values. It describes several changes from Version 3 of NTP (NTPv3) originally described in RFC 1305, including the introduction of a modified protocol header to accommodate Internet Protocol Version 6 and a new header extension field to support the Autokey public key authentication scheme. This document is based on the reference implementation available at www.ntp.org. It is intended as a reference and implementation guide, not as a formal standard. The main body of the document describes the basic model data structures and algorithms necessary for an implementation which can interoperate properly with another implementation faithful to this model. This document describes a number of crafted mitigation algorithms which can improve the accuracy and stability of the timekeeping function, especially in NTP subnets with many servers and clients. It also describes the clock discipline function used to adjust the system clock in time and frequency to agree with the available sources of synchronization. As an implementation aid, a code skeleton for the reference implementation is presented in an appendix. It includes most of the data structures and algorithms of that program, but certain features, such as the control and monitoring protocol, Autokey public key authentication scheme, huff-’n-puff scheme and server discovery schemes are not included. These are discussed in companion documents on the Web and in print. Keywords: network time synchronization, computer time synchronization, time synchronization protocol Sponsored by: Naval Surface Weapons Center (Dahlgren) Contract N00178-04-1-9001. Table of Contents 1. Introduction 1 2. Modes of Operation 2 3. Definitions 4 4. Implementation Model 5 5. Data Types 6 6. Data Structures 9 6.1 Structure Conventions 9 6.2 Global Parameters 10 6.3 Packet Header Variables 11 7. On-Wire Protocol 14 8. Peer Process 16 8.1 Peer Process Variables 16 8.2 Peer Process Operations 18 8.3 Clock Filter Algorithm 22 9. System Process 24 9.1 System Process Variables 24 9.2 System Process Operations 25 9.2.1 Selection Algorithm 26 9.2.2 Clustering Algorithm 28 9.2.3 Combining Algorithm 29 9.2.4 Clock Discipline Algorithm 31 9.3 Clock Adjust Process 36 10. Poll Process 36 10.1 Poll Process Variables and Parameters 37 10.2 Poll Process Operations 37 11. References 39 Appendix A. NTPv4 Packet Formats 41 A.1 NTP Header Field Format 41 A.2 NTPv4 Extension Field Format 42 Appendix B. Code Skeleton 44 B.1 Global Definitions 44 B.1.1 Definitions, Constants and Parameters 44 B.1.2 Packet Data Structures 47 B.1.3 Association Data Structures 48 B.1.4 System Data Structures 49 B.1.5 Local Clock Data Structure 50 B.1.6 Function Prototypes 50 B.2 Main Program and Utility Routines 51 B.3 Kernel Input/Output Interface 55 B.4 Kernel System Clock Interface 55 B.5 Peer Process 57 B.5.1 receive() 58 B.5.2 packet() 62 B.5.3 clock_filter() 64 B.5.4 fast_xmit() 65 B.5.5 access() 66 B.6 System Process 66 B.6.1 clock_select() 67 B.6.2 root_dist() 70 B.6.3 accept() 70 B.6.4 clock_update() 71 B.6.5 clock_combine() 73 B.6.6 local_clock() 73 B.6.7 rstclock() 78 B.7 Clock Adjust Process 79 B.7.1 clock_adjust() 79 B.8 Poll Process 80 B.8.1 poll() 80 B.8.2 poll_update() 82 B.8.3 transmit() 83