1 /* 2 chronyd/chronyc - Programs for keeping computer clocks accurate. 3 4 ********************************************************************** 5 * Copyright (C) Richard P. Curnow 1997-2003 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of version 2 of the GNU General Public License as 9 * published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, write to the Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 * 20 ********************************************************************** 21 22 ======================================================================= 23 24 Header file containing common NTP bits and pieces 25 */ 26 27 #ifndef GOT_NTP_H 28 #define GOT_NTP_H 29 30 #include "sysincl.h" 31 32 #include "hash.h" 33 34 typedef struct { 35 uint32_t hi; 36 uint32_t lo; 37 } NTP_int64; 38 39 typedef uint32_t NTP_int32; 40 41 /* The UDP port number used by NTP */ 42 #define NTP_PORT 123 43 44 /* The NTP protocol version that we support */ 45 #define NTP_VERSION 4 46 47 /* Maximum stratum number (infinity) */ 48 #define NTP_MAX_STRATUM 16 49 50 /* Invalid stratum number */ 51 #define NTP_INVALID_STRATUM 0 52 53 /* The minimum and maximum supported length of MAC */ 54 #define NTP_MIN_MAC_LENGTH (4 + 16) 55 #define NTP_MAX_MAC_LENGTH (4 + MAX_HASH_LENGTH) 56 57 /* The minimum valid length of an extension field */ 58 #define NTP_MIN_EF_LENGTH 16 59 60 /* The maximum assumed length of all extension fields in an NTP packet, 61 including a MAC (RFC 5905 doesn't specify a limit on length or number of 62 extension fields in one packet) */ 63 #define NTP_MAX_EXTENSIONS_LENGTH (1024 + NTP_MAX_MAC_LENGTH) 64 65 /* The maximum length of MAC in NTPv4 packets which allows deterministic 66 parsing of extension fields (RFC 7822) */ 67 #define NTP_MAX_V4_MAC_LENGTH (4 + 20) 68 69 /* Type definition for leap bits */ 70 typedef enum { 71 LEAP_Normal = 0, 72 LEAP_InsertSecond = 1, 73 LEAP_DeleteSecond = 2, 74 LEAP_Unsynchronised = 3 75 } NTP_Leap; 76 77 typedef enum { 78 MODE_UNDEFINED = 0, 79 MODE_ACTIVE = 1, 80 MODE_PASSIVE = 2, 81 MODE_CLIENT = 3, 82 MODE_SERVER = 4, 83 MODE_BROADCAST = 5 84 } NTP_Mode; 85 86 typedef struct { 87 uint8_t lvm; 88 uint8_t stratum; 89 int8_t poll; 90 int8_t precision; 91 NTP_int32 root_delay; 92 NTP_int32 root_dispersion; 93 NTP_int32 reference_id; 94 NTP_int64 reference_ts; 95 NTP_int64 originate_ts; 96 NTP_int64 receive_ts; 97 NTP_int64 transmit_ts; 98 99 uint8_t extensions[NTP_MAX_EXTENSIONS_LENGTH]; 100 } NTP_Packet; 101 102 #define NTP_HEADER_LENGTH (int)offsetof(NTP_Packet, extensions) 103 104 /* Macros to work with the lvm field */ 105 #define NTP_LVM_TO_LEAP(lvm) (((lvm) >> 6) & 0x3) 106 #define NTP_LVM_TO_VERSION(lvm) (((lvm) >> 3) & 0x7) 107 #define NTP_LVM_TO_MODE(lvm) ((lvm) & 0x7) 108 #define NTP_LVM(leap, version, mode) \ 109 ((((leap) << 6) & 0xc0) | (((version) << 3) & 0x38) | ((mode) & 0x07)) 110 111 /* Special NTP reference IDs */ 112 #define NTP_REFID_UNSYNC 0x0UL 113 #define NTP_REFID_LOCAL 0x7F7F0101UL /* 127.127.1.1 */ 114 #define NTP_REFID_SMOOTH 0x7F7F01FFUL /* 127.127.1.255 */ 115 116 /* Non-authentication extension fields and corresponding internal flags */ 117 118 #define NTP_EF_EXP1 0xF323 119 120 #define NTP_EF_FLAG_EXP1 0x1 121 122 /* Pre-NTPv5 experimental extension field */ 123 typedef struct { 124 uint32_t magic; 125 NTP_int32 root_delay; 126 NTP_int32 root_dispersion; 127 NTP_int64 mono_receive_ts; 128 uint32_t mono_epoch; 129 } NTP_ExtFieldExp1; 130 131 #define NTP_EF_EXP1_MAGIC 0xF5BEDD9AU 132 133 /* Authentication extension fields */ 134 135 #define NTP_EF_NTS_UNIQUE_IDENTIFIER 0x0104 136 #define NTP_EF_NTS_COOKIE 0x0204 137 #define NTP_EF_NTS_COOKIE_PLACEHOLDER 0x0304 138 #define NTP_EF_NTS_AUTH_AND_EEF 0x0404 139 140 /* Enumeration for authentication modes of NTP packets */ 141 typedef enum { 142 NTP_AUTH_NONE = 0, /* No authentication */ 143 NTP_AUTH_SYMMETRIC, /* NTP MAC or CMAC using a symmetric key 144 (RFC 1305, RFC 5905, RFC 8573) */ 145 NTP_AUTH_MSSNTP, /* MS-SNTP authenticator field */ 146 NTP_AUTH_MSSNTP_EXT, /* MS-SNTP extended authenticator field */ 147 NTP_AUTH_NTS, /* Network Time Security (RFC 8915) */ 148 } NTP_AuthMode; 149 150 /* Structure describing an NTP packet */ 151 typedef struct { 152 int length; 153 int version; 154 NTP_Mode mode; 155 156 int ext_fields; 157 int ext_field_flags; 158 159 struct { 160 NTP_AuthMode mode; 161 struct { 162 int start; 163 int length; 164 uint32_t key_id; 165 } mac; 166 } auth; 167 } NTP_PacketInfo; 168 169 /* Structure used to save NTP measurements. time is the local time at which 170 the sample is to be considered to have been made and offset is the offset at 171 the time (positive indicates that the local clock is slow relative to the 172 source). root_delay/root_dispersion include peer_delay/peer_dispersion. */ 173 typedef struct { 174 struct timespec time; 175 double offset; 176 double peer_delay; 177 double peer_dispersion; 178 double root_delay; 179 double root_dispersion; 180 } NTP_Sample; 181 182 #endif /* GOT_NTP_H */ 183