1 /* 2 * ntp.h - NTP definitions for the masses 3 */ 4 #ifndef NTP_H 5 #define NTP_H 6 7 #include <stddef.h> 8 #include <math.h> 9 10 #include <ntp_fp.h> 11 #include <ntp_types.h> 12 #include <ntp_lists.h> 13 #include <ntp_stdlib.h> 14 #include <ntp_crypto.h> 15 #include <ntp_random.h> 16 #include <ntp_net.h> 17 18 #include <isc/boolean.h> 19 20 /* 21 * Calendar arithmetic - contributed by G. Healton 22 */ 23 #define YEAR_BREAK 500 /* years < this are tm_year values: 24 * Break < AnyFourDigitYear && Break > 25 * Anytm_yearYear */ 26 27 #define YEAR_PIVOT 98 /* 97/98: years < this are year 2000+ 28 * FYI: official UNIX pivot year is 29 * 68/69 */ 30 31 /* 32 * Number of Days since 1 BC Gregorian to 1 January of given year 33 */ 34 #define julian0(year) (((year) * 365 ) + ((year) > 0 ? (((year) + 3) \ 35 / 4 - ((year - 1) / 100) + ((year - 1) / \ 36 400)) : 0)) 37 38 /* 39 * Number of days since start of NTP time to 1 January of given year 40 */ 41 #define ntp0(year) (julian0(year) - julian0(1900)) 42 43 /* 44 * Number of days since start of UNIX time to 1 January of given year 45 */ 46 #define unix0(year) (julian0(year) - julian0(1970)) 47 48 /* 49 * LEAP YEAR test for full 4-digit years (e.g, 1999, 2010) 50 */ 51 #define isleap_4(y) ((y) % 4 == 0 && !((y) % 100 == 0 && !(y % \ 52 400 == 0))) 53 54 /* 55 * LEAP YEAR test for tm_year (struct tm) years (e.g, 99, 110) 56 */ 57 #define isleap_tm(y) ((y) % 4 == 0 && !((y) % 100 == 0 && !(((y) \ 58 + 1900) % 400 == 0))) 59 60 /* 61 * to convert simple two-digit years to tm_year style years: 62 * 63 * if (year < YEAR_PIVOT) 64 * year += 100; 65 * 66 * to convert either two-digit OR tm_year years to four-digit years: 67 * 68 * if (year < YEAR_PIVOT) 69 * year += 100; 70 * 71 * if (year < YEAR_BREAK) 72 * year += 1900; 73 */ 74 75 /* 76 * How to get signed characters. On machines where signed char works, 77 * use it. On machines where signed char doesn't work, char had better 78 * be signed. 79 */ 80 #ifdef NEED_S_CHAR_TYPEDEF 81 # if SIZEOF_SIGNED_CHAR 82 typedef signed char s_char; 83 # else 84 typedef char s_char; 85 # endif 86 /* XXX: Why is this sequent bit INSIDE this test? */ 87 # ifdef sequent 88 # undef SO_RCVBUF 89 # undef SO_SNDBUF 90 # endif 91 #endif 92 93 /* 94 * NTP protocol parameters. See section 3.2.6 of the specification. 95 */ 96 #define NTP_VERSION ((u_char)4) /* current version number */ 97 #define NTP_OLDVERSION ((u_char)1) /* oldest credible version */ 98 #define NTP_PORT 123 /* included for non-unix machines */ 99 100 /* 101 * Poll interval parameters 102 */ 103 #define NTP_UNREACH 10 /* poll unreach threshold */ 104 #define NTP_MINPOLL 3 /* log2 min poll interval (8 s) */ 105 #define NTP_MINDPOLL 6 /* log2 default min poll (64 s) */ 106 #define NTP_MAXDPOLL 10 /* log2 default max poll (~17 m) */ 107 #define NTP_MAXPOLL 17 /* log2 max poll interval (~36 h) */ 108 #define NTP_RETRY 3 /* max packet retries */ 109 #define NTP_MINPKT 2 /* guard time (s) */ 110 111 /* 112 * Clock filter algorithm tuning parameters 113 */ 114 #define MAXDISPERSE 16. /* max dispersion */ 115 #define NTP_SHIFT 8 /* clock filter stages */ 116 #define NTP_FWEIGHT .5 /* clock filter weight */ 117 118 /* 119 * Selection algorithm tuning parameters 120 */ 121 #define NTP_MINCLOCK 3 /* min survivors */ 122 #define NTP_MAXCLOCK 10 /* max candidates */ 123 #define MINDISPERSE .001 /* min distance */ 124 #define MAXDISTANCE 1.5 /* max root distance (select threshold) */ 125 #define CLOCK_SGATE 3. /* popcorn spike gate */ 126 #define HUFFPUFF 900 /* huff-n'-puff sample interval (s) */ 127 #define MAXHOP 2 /* anti-clockhop threshold */ 128 #define MAX_TTL 8 /* max ttl mapping vector size */ 129 #define BEACON 7200 /* manycast beacon interval */ 130 #define NTP_MAXEXTEN 2048 /* max extension field size */ 131 #define NTP_ORPHWAIT 300 /* orphan wait (s) */ 132 133 /* 134 * Miscellaneous stuff 135 */ 136 #define NTP_MAXKEY 65535 /* max authentication key number */ 137 138 /* 139 * Limits of things 140 */ 141 #define MAXFILENAME 256 /* max length of file name */ 142 #define MAXHOSTNAME 512 /* max length of host/node name */ 143 #define NTP_MAXSTRLEN 256 /* max string length */ 144 145 /* 146 * Operations for jitter calculations (these use doubles). 147 * 148 * Note that we carefully separate the jitter component from the 149 * dispersion component (frequency error plus precision). The frequency 150 * error component is computed as CLOCK_PHI times the difference between 151 * the epoch of the time measurement and the reference time. The 152 * precision component is computed as the square root of the mean of the 153 * squares of a zero-mean, uniform distribution of unit maximum 154 * amplitude. Whether this makes statistical sense may be arguable. 155 */ 156 #define SQUARE(x) ((x) * (x)) 157 #define SQRT(x) (sqrt(x)) 158 #define DIFF(x, y) (SQUARE((x) - (y))) 159 #define LOGTOD(a) ldexp(1., (int)(a)) /* log2 to double */ 160 #define UNIVAR(x) (SQUARE(.28867513 * LOGTOD(x))) /* std uniform distr */ 161 #define ULOGTOD(a) ldexp(1., (int)(a)) /* ulog2 to double */ 162 163 #define EVENT_TIMEOUT 0 /* one second, that is */ 164 165 166 /* 167 * The interface structure is used to hold the addresses and socket 168 * numbers of each of the local network addresses we are using. 169 * Because "interface" is a reserved word in C++ and has so many 170 * varied meanings, a change to "endpt" (via typedef) is under way. 171 * Eventually the struct tag will change from interface to endpt_tag. 172 * endpt is unrelated to the select algorithm's struct endpoint. 173 */ 174 typedef struct endpt_tag endpt; 175 struct endpt_tag { 176 endpt * elink; /* endpt list link */ 177 endpt * mclink; /* per-AF_* multicast list */ 178 void * ioreg_ctx; /* IO registration context */ 179 SOCKET fd; /* socket descriptor */ 180 SOCKET bfd; /* for receiving broadcasts */ 181 u_int32 ifnum; /* endpt instance count */ 182 sockaddr_u sin; /* unicast address */ 183 sockaddr_u mask; /* subnet mask */ 184 sockaddr_u bcast; /* broadcast address */ 185 char name[32]; /* name of interface */ 186 u_short family; /* AF_INET/AF_INET6 */ 187 u_short phase; /* phase in update cycle */ 188 u_int32 flags; /* INT_ flags */ 189 int last_ttl; /* last TTL specified */ 190 u_int32 addr_refid; /* IPv4 addr or IPv6 hash */ 191 # ifdef WORDS_BIGENDIAN 192 u_int32 old_refid; /* byte-swapped IPv6 refid */ 193 # endif 194 int num_mcast; /* mcast addrs enabled */ 195 u_long starttime; /* current_time at creation */ 196 volatile long received; /* number of incoming packets */ 197 long sent; /* number of outgoing packets */ 198 long notsent; /* number of send failures */ 199 u_int ifindex; /* for IPV6_MULTICAST_IF */ 200 isc_boolean_t ignore_packets; /* listen-read-drop this? */ 201 struct peer * peers; /* list of peers using endpt */ 202 u_int peercnt; /* count of same */ 203 }; 204 205 /* 206 * Flags for network endpoints (interfaces or really addresses) 207 */ 208 #define INT_UP 0x001 /* Interface is up */ 209 #define INT_PPP 0x002 /* Point-to-point interface */ 210 #define INT_LOOPBACK 0x004 /* ::1 or 127.0.0.1 */ 211 #define INT_BROADCAST 0x008 /* can broadcast out this interface */ 212 #define INT_MULTICAST 0x010 /* can multicast out this interface */ 213 #define INT_BCASTOPEN 0x020 /* broadcast receive socket is open */ 214 #define INT_MCASTOPEN 0x040 /* multicasting enabled */ 215 #define INT_WILDCARD 0x080 /* wildcard interface - usually skipped */ 216 #define INT_MCASTIF 0x100 /* bound directly to MCAST address */ 217 #define INT_PRIVACY 0x200 /* RFC 4941 IPv6 privacy address */ 218 #define INT_BCASTXMIT 0x400 /* socket setup to allow broadcasts */ 219 #define INT_LL_OF_GLOB 0x800 /* IPv6 link-local duplicate of global */ 220 221 /* 222 * Define flasher bits (tests 1 through 11 in packet procedure) 223 * These reveal the state at the last grumble from the peer and are 224 * most handy for diagnosing problems, even if not strictly a state 225 * variable in the spec. These are recorded in the peer structure. 226 * 227 * Packet errors 228 */ 229 #define TEST1 0X0001 /* duplicate packet */ 230 #define TEST2 0x0002 /* bogus packet */ 231 #define TEST3 0x0004 /* protocol unsynchronized */ 232 #define TEST4 0x0008 /* access denied */ 233 #define TEST5 0x0010 /* bad authentication */ 234 #define TEST6 0x0020 /* bad synch or stratum */ 235 #define TEST7 0x0040 /* bad header */ 236 #define TEST8 0x0080 /* bad autokey */ 237 #define TEST9 0x0100 /* bad crypto */ 238 #define PKT_TEST_MASK (TEST1 | TEST2 | TEST3 | TEST4 | TEST5 |\ 239 TEST6 | TEST7 | TEST8 | TEST9) 240 /* 241 * Peer errors 242 */ 243 #define TEST10 0x0200 /* peer bad synch or stratum */ 244 #define TEST11 0x0400 /* peer distance exceeded */ 245 #define TEST12 0x0800 /* peer synchronization loop */ 246 #define TEST13 0x1000 /* peer unreacable */ 247 #define PEER_TEST_MASK (TEST10 | TEST11 | TEST12 | TEST13) 248 249 /* 250 * Unused flags 251 */ 252 #define TEST14 0x2000 253 #define TEST15 0x4000 254 #define TEST16 0x8000 255 256 /* 257 * The peer structure. Holds state information relating to the guys 258 * we are peering with. Most of this stuff is from section 3.2 of the 259 * spec. 260 */ 261 struct peer { 262 struct peer *p_link; /* link pointer in free & peer lists */ 263 struct peer *adr_link; /* link pointer in address hash */ 264 struct peer *aid_link; /* link pointer in associd hash */ 265 struct peer *ilink; /* list of peers for interface */ 266 sockaddr_u srcadr; /* address of remote host */ 267 char * hostname; /* if non-NULL, remote name */ 268 struct addrinfo *addrs; /* hostname query result */ 269 struct addrinfo *ai; /* position within addrs */ 270 endpt * dstadr; /* local address */ 271 associd_t associd; /* association ID */ 272 u_char version; /* version number */ 273 u_char hmode; /* local association mode */ 274 u_char hpoll; /* local poll interval */ 275 u_char minpoll; /* min poll interval */ 276 u_char maxpoll; /* max poll interval */ 277 u_int flags; /* association flags */ 278 u_char cast_flags; /* additional flags */ 279 u_char last_event; /* last peer error code */ 280 u_char num_events; /* number of error events */ 281 u_int32 ttl; /* ttl/refclock mode */ 282 char *ident; /* group identifier name */ 283 284 /* 285 * Variables used by reference clock support 286 */ 287 #ifdef REFCLOCK 288 struct refclockproc *procptr; /* refclock structure pointer */ 289 u_char refclktype; /* reference clock type */ 290 u_char refclkunit; /* reference clock unit number */ 291 u_char sstclktype; /* clock type for system status word */ 292 #endif /* REFCLOCK */ 293 294 /* 295 * Variables set by received packet 296 */ 297 u_char leap; /* local leap indicator */ 298 u_char pmode; /* remote association mode */ 299 u_char stratum; /* remote stratum */ 300 u_char ppoll; /* remote poll interval */ 301 s_char precision; /* remote clock precision */ 302 double rootdelay; /* roundtrip delay to primary source */ 303 double rootdisp; /* dispersion to primary source */ 304 u_int32 refid; /* remote reference ID */ 305 l_fp reftime; /* update epoch */ 306 307 /* 308 * Variables used by authenticated client 309 */ 310 keyid_t keyid; /* current key ID */ 311 #ifdef AUTOKEY 312 #define clear_to_zero opcode 313 u_int32 opcode; /* last request opcode */ 314 associd_t assoc; /* peer association ID */ 315 u_int32 crypto; /* peer status word */ 316 EVP_PKEY *pkey; /* public key */ 317 const EVP_MD *digest; /* message digest algorithm */ 318 char *subject; /* certificate subject name */ 319 char *issuer; /* certificate issuer name */ 320 struct cert_info *xinfo; /* issuer certificate */ 321 keyid_t pkeyid; /* previous key ID */ 322 keyid_t hcookie; /* host cookie */ 323 keyid_t pcookie; /* peer cookie */ 324 const struct pkey_info *ident_pkey; /* identity key */ 325 BIGNUM *iffval; /* identity challenge (IFF, GQ, MV) */ 326 const BIGNUM *grpkey; /* identity challenge key (GQ) */ 327 struct value cookval; /* receive cookie values */ 328 struct value recval; /* receive autokey values */ 329 struct exten *cmmd; /* extension pointer */ 330 u_long refresh; /* next refresh epoch */ 331 332 /* 333 * Variables used by authenticated server 334 */ 335 keyid_t *keylist; /* session key ID list */ 336 int keynumber; /* current key number */ 337 struct value encrypt; /* send encrypt values */ 338 struct value sndval; /* send autokey values */ 339 #else /* !AUTOKEY follows */ 340 #define clear_to_zero status 341 #endif /* !AUTOKEY */ 342 343 /* 344 * Ephemeral state variables 345 */ 346 u_char status; /* peer status */ 347 u_char new_status; /* under-construction status */ 348 u_char reach; /* reachability register */ 349 u_char filter_nextpt; /* index into filter shift register */ 350 int flash; /* protocol error test tally bits */ 351 u_long epoch; /* reference epoch */ 352 int burst; /* packets remaining in burst */ 353 int retry; /* retry counter */ 354 int flip; /* interleave mode control */ 355 double filter_delay[NTP_SHIFT]; /* delay shift register */ 356 double filter_offset[NTP_SHIFT]; /* offset shift register */ 357 double filter_disp[NTP_SHIFT]; /* dispersion shift register */ 358 u_long filter_epoch[NTP_SHIFT]; /* epoch shift register */ 359 u_char filter_order[NTP_SHIFT]; /* filter sort index */ 360 l_fp rec; /* receive time stamp */ 361 l_fp xmt; /* transmit time stamp */ 362 l_fp dst; /* destination timestamp */ 363 l_fp aorg; /* origin timestamp */ 364 l_fp borg; /* alternate origin timestamp */ 365 l_fp bxmt; /* most recent broadcast transmit timestamp */ 366 l_fp nonce; /* Value of nonce we sent as the xmt stamp */ 367 double offset; /* peer clock offset */ 368 double delay; /* peer roundtrip delay */ 369 double jitter; /* peer jitter (squares) */ 370 double disp; /* peer dispersion */ 371 double xleave; /* interleave delay */ 372 double bias; /* programmed offset bias */ 373 374 /* 375 * Variables used to correct for packet length and asymmetry. 376 */ 377 double t21; /* outbound packet delay */ 378 int t21_bytes; /* outbound packet length */ 379 int t21_last; /* last outbound packet length */ 380 double r21; /* outbound data rate */ 381 double t34; /* inbound packet delay */ 382 int t34_bytes; /* inbound packet length */ 383 double r34; /* inbound data rate */ 384 385 /* 386 * End of clear-to-zero area 387 */ 388 u_long update; /* receive epoch */ 389 #define end_clear_to_zero update 390 int unreach; /* watchdog counter */ 391 int throttle; /* rate control */ 392 u_long outdate; /* send time last packet */ 393 u_long nextdate; /* send time next packet */ 394 395 /* 396 * Statistic counters 397 */ 398 u_long timereset; /* time stat counters were reset */ 399 u_long timelastrec; /* last packet received time, incl. trash */ 400 u_long timereceived; /* last (clean) packet received time */ 401 u_long timereachable; /* last reachable/unreachable time */ 402 403 u_long sent; /* packets sent */ 404 u_long received; /* packets received */ 405 u_long processed; /* packets processed */ 406 u_long badauth; /* bad authentication (TEST5) */ 407 u_long badNAK; /* invalid crypto-NAK */ 408 u_long bogusorg; /* bogus origin (TEST2, TEST3) */ 409 u_long oldpkt; /* old duplicate (TEST1) */ 410 u_long seldisptoolarge; /* bad header (TEST6, TEST7) */ 411 u_long selbroken; /* KoD received */ 412 }; 413 414 /* 415 * Values for peer.leap, sys_leap 416 */ 417 #define LEAP_NOWARNING 0x0 /* normal, no leap second warning */ 418 #define LEAP_ADDSECOND 0x1 /* last minute of day has 61 seconds */ 419 #define LEAP_DELSECOND 0x2 /* last minute of day has 59 seconds */ 420 #define LEAP_NOTINSYNC 0x3 /* overload, clock is free running */ 421 422 /* 423 * Values for peer mode and packet mode. Only the modes through 424 * MODE_BROADCAST and MODE_BCLIENT appear in the transition 425 * function. MODE_CONTROL and MODE_PRIVATE can appear in packets, 426 * but those never survive to the transition function. 427 */ 428 #define MODE_UNSPEC 0 /* unspecified (old version) */ 429 #define MODE_ACTIVE 1 /* symmetric active mode */ 430 #define MODE_PASSIVE 2 /* symmetric passive mode */ 431 #define MODE_CLIENT 3 /* client mode */ 432 #define MODE_SERVER 4 /* server mode */ 433 #define MODE_BROADCAST 5 /* broadcast mode */ 434 /* 435 * These can appear in packets 436 */ 437 #define MODE_CONTROL 6 /* control mode */ 438 #define MODE_PRIVATE 7 /* private mode */ 439 /* 440 * This is a made-up mode for broadcast client. 441 */ 442 #define MODE_BCLIENT 6 /* broadcast client mode */ 443 444 /* 445 * Values for peer.stratum, sys_stratum 446 */ 447 #define STRATUM_REFCLOCK ((u_char)0) /* default stratum */ 448 /* A stratum of 0 in the packet is mapped to 16 internally */ 449 #define STRATUM_PKT_UNSPEC ((u_char)0) /* unspecified in packet */ 450 #define STRATUM_UNSPEC ((u_char)16) /* unspecified */ 451 452 /* 453 * Values for peer.flags (u_int) 454 */ 455 #define FLAG_CONFIG 0x0001 /* association was configured */ 456 #define FLAG_PREEMPT 0x0002 /* preemptable association */ 457 #define FLAG_AUTHENTIC 0x0004 /* last message was authentic */ 458 #define FLAG_REFCLOCK 0x0008 /* this is actually a reference clock */ 459 #define FLAG_BC_VOL 0x0010 /* broadcast client volleying */ 460 #define FLAG_PREFER 0x0020 /* prefer peer */ 461 #define FLAG_BURST 0x0040 /* burst mode */ 462 #define FLAG_PPS 0x0080 /* steered by PPS */ 463 #define FLAG_IBURST 0x0100 /* initial burst mode */ 464 #define FLAG_NOSELECT 0x0200 /* never select */ 465 #define FLAG_TRUE 0x0400 /* force truechimer */ 466 #define FLAG_SKEY 0x0800 /* autokey authentication */ 467 #define FLAG_XLEAVE 0x1000 /* interleaved protocol */ 468 #define FLAG_XB 0x2000 /* interleaved broadcast */ 469 #define FLAG_XBOGUS 0x4000 /* interleaved bogus packet */ 470 #ifdef AUTOKEY 471 # define FLAG_ASSOC 0x8000 /* autokey request */ 472 #endif 473 #define FLAG_TSTAMP_PPS 0x10000 /* PPS source provides absolute timestamp */ 474 #define FLAG_LOOPNONCE 0x20000 /* Use a nonce for the loopback test */ 475 #define FLAG_DISABLED 0x40000 /* peer is being torn down */ 476 477 /* 478 * Definitions for the clear() routine. We use memset() to clear 479 * the parts of the peer structure which go to zero. These are 480 * used to calculate the start address and length of the area. 481 */ 482 #define CLEAR_TO_ZERO(p) ((char *)&((p)->clear_to_zero)) 483 #define END_CLEAR_TO_ZERO(p) ((char *)&((p)->end_clear_to_zero)) 484 #define LEN_CLEAR_TO_ZERO(p) (END_CLEAR_TO_ZERO(p) - CLEAR_TO_ZERO(p)) 485 #define CRYPTO_TO_ZERO(p) ((char *)&((p)->clear_to_zero)) 486 #define END_CRYPTO_TO_ZERO(p) ((char *)&((p)->end_clear_to_zero)) 487 #define LEN_CRYPTO_TO_ZERO (END_CRYPTO_TO_ZERO((struct peer *)0) \ 488 - CRYPTO_TO_ZERO((struct peer *)0)) 489 490 /* 491 * Reference clock types. Added as necessary. 492 */ 493 #define REFCLK_NONE 0 /* unknown or missing */ 494 #define REFCLK_LOCALCLOCK 1 /* external (e.g., lockclock) */ 495 #define REFCLK_GPS_TRAK 2 /* TRAK 8810 GPS Receiver */ 496 #define REFCLK_WWV_PST 3 /* PST/Traconex 1020 WWV/H */ 497 #define REFCLK_SPECTRACOM 4 /* Spectracom (generic) Receivers */ 498 #define REFCLK_TRUETIME 5 /* TrueTime (generic) Receivers */ 499 #define REFCLK_IRIG_AUDIO 6 /* IRIG-B/W audio decoder */ 500 #define REFCLK_CHU_AUDIO 7 /* CHU audio demodulator/decoder */ 501 #define REFCLK_PARSE 8 /* generic driver (usually DCF77,GPS,MSF) */ 502 #define REFCLK_GPS_MX4200 9 /* Magnavox MX4200 GPS */ 503 #define REFCLK_GPS_AS2201 10 /* Austron 2201A GPS */ 504 #define REFCLK_GPS_ARBITER 11 /* Arbiter 1088A/B/ GPS */ 505 #define REFCLK_IRIG_TPRO 12 /* KSI/Odetics TPRO-S IRIG */ 506 #define REFCLK_ATOM_LEITCH 13 /* Leitch CSD 5300 Master Clock */ 507 #define REFCLK_MSF_EES 14 /* EES M201 MSF Receiver */ 508 #define REFCLK_GPSTM_TRUE 15 /* OLD TrueTime GPS/TM-TMD Receiver */ 509 #define REFCLK_IRIG_BANCOMM 16 /* Bancomm GPS/IRIG Interface */ 510 #define REFCLK_GPS_DATUM 17 /* Datum Programmable Time System */ 511 #define REFCLK_ACTS 18 /* Generic Auto Computer Time Service */ 512 #define REFCLK_WWV_HEATH 19 /* Heath GC1000 WWV/WWVH Receiver */ 513 #define REFCLK_GPS_NMEA 20 /* NMEA based GPS clock */ 514 #define REFCLK_GPS_VME 21 /* TrueTime GPS-VME Interface */ 515 #define REFCLK_ATOM_PPS 22 /* 1-PPS Clock Discipline */ 516 #define REFCLK_PTB_ACTS 23 /* replaced by REFCLK_ACTS */ 517 #define REFCLK_USNO 24 /* replaced by REFCLK_ACTS */ 518 #define REFCLK_GPS_HP 26 /* HP 58503A Time/Frequency Receiver */ 519 #define REFCLK_ARCRON_MSF 27 /* ARCRON MSF radio clock. */ 520 #define REFCLK_SHM 28 /* clock attached thru shared memory */ 521 #define REFCLK_PALISADE 29 /* Trimble Navigation Palisade GPS */ 522 #define REFCLK_ONCORE 30 /* Motorola UT Oncore GPS */ 523 #define REFCLK_GPS_JUPITER 31 /* Rockwell Jupiter GPS receiver */ 524 #define REFCLK_CHRONOLOG 32 /* Chrono-log K WWVB receiver */ 525 #define REFCLK_DUMBCLOCK 33 /* Dumb localtime clock */ 526 #define REFCLK_ULINK 34 /* Ultralink M320 WWVB receiver */ 527 #define REFCLK_PCF 35 /* Conrad parallel port radio clock */ 528 #define REFCLK_WWV_AUDIO 36 /* WWV/H audio demodulator/decoder */ 529 #define REFCLK_FG 37 /* Forum Graphic GPS */ 530 #define REFCLK_HOPF_SERIAL 38 /* hopf DCF77/GPS serial receiver */ 531 #define REFCLK_HOPF_PCI 39 /* hopf DCF77/GPS PCI receiver */ 532 #define REFCLK_JJY 40 /* JJY receiver */ 533 #define REFCLK_TT560 41 /* TrueTime 560 IRIG-B decoder */ 534 #define REFCLK_ZYFER 42 /* Zyfer GPStarplus receiver */ 535 #define REFCLK_RIPENCC 43 /* RIPE NCC Trimble driver */ 536 #define REFCLK_NEOCLOCK4X 44 /* NeoClock4X DCF77 or TDF receiver */ 537 #define REFCLK_TSYNCPCI 45 /* Spectracom TSYNC PCI timing board */ 538 #define REFCLK_GPSDJSON 46 539 #define REFCLK_MAX 46 540 541 542 /* 543 * NTP packet format. The mac field is optional. It isn't really 544 * an l_fp either, but for now declaring it that way is convenient. 545 * See Appendix A in the specification. 546 * 547 * Note that all u_fp and l_fp values arrive in network byte order 548 * and must be converted (except the mac, which isn't, really). 549 */ 550 struct pkt { 551 u_char li_vn_mode; /* peer leap indicator */ 552 u_char stratum; /* peer stratum */ 553 u_char ppoll; /* peer poll interval */ 554 s_char precision; /* peer clock precision */ 555 u_fp rootdelay; /* roundtrip delay to primary source */ 556 u_fp rootdisp; /* dispersion to primary source*/ 557 u_int32 refid; /* reference id */ 558 l_fp reftime; /* last update time */ 559 l_fp org; /* originate time stamp */ 560 l_fp rec; /* receive time stamp */ 561 l_fp xmt; /* transmit time stamp */ 562 563 #define MIN_V4_PKT_LEN (12 * sizeof(u_int32)) /* min header length */ 564 #define LEN_PKT_NOMAC (12 * sizeof(u_int32)) /* min header length */ 565 #define MIN_MAC_LEN (1 * sizeof(u_int32)) /* crypto_NAK */ 566 #define MD5_LENGTH 16 567 #define SHAKE128_LENGTH 16 568 #define CMAC_LENGTH 16 569 #define SHA1_LENGTH 20 570 #define KEY_MAC_LEN sizeof(u_int32) /* key ID in MAC */ 571 #define MAX_MD5_LEN (KEY_MAC_LEN + MD5_LENGTH) 572 #define MAX_SHAKE128_LEN (KEY_MAC_LEN + SHAKE128_LENGTH) 573 #define MAX_SHA1_LEN (KEY_MAC_LEN + SHA1_LENGTH) 574 #define MAX_MAC_LEN (6 * sizeof(u_int32)) /* any MAC */ 575 #define MAX_MDG_LEN (MAX_MAC_LEN-KEY_MAC_LEN) /* max. digest len */ 576 577 /* 578 * The length of the packet less MAC must be a multiple of 64 579 * with an RSA modulus and Diffie-Hellman prime of 256 octets 580 * and maximum host name of 128 octets, the maximum autokey 581 * command is 152 octets and maximum autokey response is 460 582 * octets. A packet can contain no more than one command and one 583 * response, so the maximum total extension field length is 864 584 * octets. But, to handle humungus certificates, the bank must 585 * be broke. 586 * 587 * The different definitions of the 'exten' field are here for 588 * the benefit of applications that want to send a packet from 589 * an auto variable in the stack - not using the AUTOKEY version 590 * saves 2KB of stack space. The receive buffer should ALWAYS be 591 * big enough to hold a full extended packet if the extension 592 * fields have to be parsed or skipped. 593 */ 594 #ifdef AUTOKEY 595 u_int32 exten[(NTP_MAXEXTEN + MAX_MAC_LEN) / sizeof(u_int32)]; 596 #else /* !AUTOKEY follows */ 597 u_int32 exten[(MAX_MAC_LEN) / sizeof(u_int32)]; 598 #endif /* !AUTOKEY */ 599 }; 600 601 /* 602 * Stuff for extracting things from li_vn_mode 603 */ 604 #define PKT_MODE(li_vn_mode) ((u_char)((li_vn_mode) & 0x7)) 605 #define PKT_VERSION(li_vn_mode) ((u_char)(((li_vn_mode) >> 3) & 0x7)) 606 #define PKT_LEAP(li_vn_mode) ((u_char)(((li_vn_mode) >> 6) & 0x3)) 607 608 /* 609 * Stuff for putting things back into li_vn_mode in packets and vn_mode 610 * in ntp_monitor.c's mon_entry. 611 */ 612 #define VN_MODE(v, m) ((((v) & 7) << 3) | ((m) & 0x7)) 613 #define PKT_LI_VN_MODE(l, v, m) ((((l) & 3) << 6) | VN_MODE((v), (m))) 614 615 616 /* 617 * Dealing with stratum. 0 gets mapped to 16 incoming, and back to 0 618 * on output. 619 */ 620 #define PKT_TO_STRATUM(s) ((u_char)(((s) == (STRATUM_PKT_UNSPEC)) ?\ 621 (STRATUM_UNSPEC) : (s))) 622 623 #define STRATUM_TO_PKT(s) ((u_char)(((s) == (STRATUM_UNSPEC)) ?\ 624 (STRATUM_PKT_UNSPEC) : (s))) 625 626 627 /* 628 * A test to determine if the refid should be interpreted as text string. 629 * This is usually the case for a refclock, which has stratum 0 internally, 630 * which results in sys_stratum 1 if the refclock becomes system peer, or 631 * in case of a kiss-of-death (KoD) packet that has STRATUM_PKT_UNSPEC (==0) 632 * in the packet which is converted to STRATUM_UNSPEC when the packet 633 * is evaluated. 634 */ 635 #define REFID_ISTEXT(s) (((s) <= 1) || ((s) >= STRATUM_UNSPEC)) 636 637 638 /* 639 * Event codes. Used for reporting errors/events to the control module 640 */ 641 #define PEER_EVENT 0x080 /* this is a peer event */ 642 #define CRPT_EVENT 0x100 /* this is a crypto event */ 643 644 /* 645 * System event codes 646 */ 647 #define EVNT_UNSPEC 0 /* unspecified */ 648 #define EVNT_NSET 1 /* freq not set */ 649 #define EVNT_FSET 2 /* freq set */ 650 #define EVNT_SPIK 3 /* spike detect */ 651 #define EVNT_FREQ 4 /* freq mode */ 652 #define EVNT_SYNC 5 /* clock sync */ 653 #define EVNT_SYSRESTART 6 /* restart */ 654 #define EVNT_SYSFAULT 7 /* panic stop */ 655 #define EVNT_NOPEER 8 /* no sys peer */ 656 #define EVNT_ARMED 9 /* leap armed */ 657 #define EVNT_DISARMED 10 /* leap disarmed */ 658 #define EVNT_LEAP 11 /* leap event */ 659 #define EVNT_CLOCKRESET 12 /* clock step */ 660 #define EVNT_KERN 13 /* kernel event */ 661 #define EVNT_TAI 14 /* TAI */ 662 #define EVNT_LEAPVAL 15 /* stale leapsecond values */ 663 664 /* 665 * Peer event codes 666 */ 667 #define PEVNT_MOBIL (1 | PEER_EVENT) /* mobilize */ 668 #define PEVNT_DEMOBIL (2 | PEER_EVENT) /* demobilize */ 669 #define PEVNT_UNREACH (3 | PEER_EVENT) /* unreachable */ 670 #define PEVNT_REACH (4 | PEER_EVENT) /* reachable */ 671 #define PEVNT_RESTART (5 | PEER_EVENT) /* restart */ 672 #define PEVNT_REPLY (6 | PEER_EVENT) /* no reply */ 673 #define PEVNT_RATE (7 | PEER_EVENT) /* rate exceeded */ 674 #define PEVNT_DENY (8 | PEER_EVENT) /* access denied */ 675 #define PEVNT_ARMED (9 | PEER_EVENT) /* leap armed */ 676 #define PEVNT_NEWPEER (10 | PEER_EVENT) /* sys peer */ 677 #define PEVNT_CLOCK (11 | PEER_EVENT) /* clock event */ 678 #define PEVNT_AUTH (12 | PEER_EVENT) /* bad auth */ 679 #define PEVNT_POPCORN (13 | PEER_EVENT) /* popcorn */ 680 #define PEVNT_XLEAVE (14 | PEER_EVENT) /* interleave mode */ 681 #define PEVNT_XERR (15 | PEER_EVENT) /* interleave error */ 682 683 /* 684 * Clock event codes 685 */ 686 #define CEVNT_NOMINAL 0 /* unspecified */ 687 #define CEVNT_TIMEOUT 1 /* no reply */ 688 #define CEVNT_BADREPLY 2 /* bad format */ 689 #define CEVNT_FAULT 3 /* fault */ 690 #define CEVNT_PROP 4 /* bad signal */ 691 #define CEVNT_BADDATE 5 /* bad date */ 692 #define CEVNT_BADTIME 6 /* bad time */ 693 #define CEVNT_MAX CEVNT_BADTIME 694 695 /* 696 * Very misplaced value. Default port through which we send traps. 697 */ 698 #define TRAPPORT 18447 699 700 701 /* 702 * To speed lookups, peers are hashed by the low order bits of the 703 * remote IP address. These definitions relate to that. 704 */ 705 #define NTP_HASH_SIZE 128 706 #define NTP_HASH_MASK (NTP_HASH_SIZE-1) 707 #define NTP_HASH_ADDR(src) (sock_hash(src) & NTP_HASH_MASK) 708 709 /* 710 * min, min3 and max. Makes it easier to transliterate the spec without 711 * thinking about it. 712 */ 713 #define min(a,b) (((a) < (b)) ? (a) : (b)) 714 #define max(a,b) (((a) > (b)) ? (a) : (b)) 715 #define min3(a,b,c) min(min((a),(b)), (c)) 716 717 /* clamp a value within a range */ 718 #define CLAMP(val, minval, maxval) \ 719 max((minval), min((val), (maxval))) 720 721 722 /* 723 * Configuration items. These are for the protocol module (proto_config()) 724 */ 725 #define PROTO_BROADCLIENT 1 726 #define PROTO_PRECISION 2 /* (not used) */ 727 #define PROTO_AUTHENTICATE 3 728 #define PROTO_BROADDELAY 4 729 #define PROTO_AUTHDELAY 5 /* (not used) */ 730 #define PROTO_MULTICAST_ADD 6 731 #define PROTO_MULTICAST_DEL 7 732 #define PROTO_NTP 8 733 #define PROTO_KERNEL 9 734 #define PROTO_MONITOR 10 735 #define PROTO_FILEGEN 11 736 #define PROTO_PPS 12 737 #define PROTO_CAL 13 738 #define PROTO_MINCLOCK 14 739 #define PROTO_MAXCLOCK 15 740 #define PROTO_MINSANE 16 741 #define PROTO_FLOOR 17 742 #define PROTO_CEILING 18 743 #define PROTO_COHORT 19 744 #define PROTO_CALLDELAY 20 745 #define PROTO_MINDISP 21 746 #define PROTO_MAXDIST 22 747 /* available 23 */ 748 #define PROTO_MAXHOP 24 749 #define PROTO_BEACON 25 750 #define PROTO_ORPHAN 26 751 #define PROTO_ORPHWAIT 27 752 #define PROTO_MODE7 28 753 #define PROTO_UECRYPTO 29 754 #define PROTO_UECRYPTONAK 30 755 #define PROTO_UEDIGEST 31 756 #define PROTO_PCEDIGEST 32 757 #define PROTO_BCPOLLBSTEP 33 758 759 /* 760 * Configuration items for the loop filter 761 */ 762 #define LOOP_DRIFTINIT 1 /* iniitialize frequency */ 763 #define LOOP_KERN_CLEAR 2 /* set initial frequency offset */ 764 #define LOOP_MAX 3 /* set both step offsets */ 765 #define LOOP_MAX_BACK 4 /* set backward-step offset */ 766 #define LOOP_MAX_FWD 5 /* set forward-step offset */ 767 #define LOOP_PANIC 6 /* set panic offseet */ 768 #define LOOP_PHI 7 /* set dispersion rate */ 769 #define LOOP_MINSTEP 8 /* set step timeout */ 770 #define LOOP_MINPOLL 9 /* set min poll interval (log2 s) */ 771 #define LOOP_ALLAN 10 /* set minimum Allan intercept */ 772 #define LOOP_HUFFPUFF 11 /* set huff-n'-puff filter length */ 773 #define LOOP_FREQ 12 /* set initial frequency */ 774 #define LOOP_CODEC 13 /* set audio codec frequency */ 775 #define LOOP_LEAP 14 /* insert leap after second 23:59 */ 776 #define LOOP_TICK 15 /* sim. low precision clock */ 777 #define LOOP_NOFREQ 16 /* undo a previos LOOP_FREQ */ 778 779 /* 780 * Configuration items for the stats printer 781 */ 782 #define STATS_FREQ_FILE 1 /* configure drift file */ 783 #define STATS_STATSDIR 2 /* directory prefix for stats files */ 784 #define STATS_PID_FILE 3 /* configure ntpd PID file */ 785 #define STATS_LEAP_FILE 4 /* configure ntpd leapseconds file */ 786 787 #define MJD_1900 15020 /* MJD for 1 Jan 1900 */ 788 789 /* 790 * Default parameters. We use these in the absence of something better. 791 */ 792 #define INADDR_NTP 0xe0000101 /* NTP multicast address 224.0.1.1 */ 793 794 /* 795 * Structure used optionally for monitoring when this is turned on. 796 */ 797 typedef struct mon_data mon_entry; 798 struct mon_data { 799 mon_entry * hash_next; /* next structure in hash list */ 800 DECL_DLIST_LINK(mon_entry, mru);/* MRU list link pointers */ 801 endpt * lcladr; /* address on which this arrived */ 802 l_fp first; /* first time seen */ 803 l_fp last; /* last time seen */ 804 int leak; /* leaky bucket accumulator */ 805 int count; /* total packet count */ 806 u_short flags; /* restrict flags */ 807 u_char vn_mode; /* packet mode & version */ 808 u_char cast_flags; /* flags MDF_?CAST */ 809 sockaddr_u rmtadr; /* address of remote host */ 810 }; 811 812 /* 813 * Values for cast_flags in mon_entry and struct peer. mon_entry uses 814 * only the first three, MDF_UCAST, MDF_MCAST, and MDF_BCAST. 815 */ 816 #define MDF_UCAST 0x01 /* unicast client */ 817 #define MDF_MCAST 0x02 /* multicast server */ 818 #define MDF_BCAST 0x04 /* broadcast server */ 819 #define MDF_POOL 0x08 /* pool client solicitor */ 820 #define MDF_ACAST 0x10 /* manycast client solicitor */ 821 #define MDF_BCLNT 0x20 /* eph. broadcast/multicast client */ 822 #define MDF_PCLNT 0x40 /* preemptible pool client */ 823 /* 824 * In the context of struct peer in ntpd, three of the cast_flags bits 825 * represent configured associations which never receive packets, and 826 * whose reach is always 0: MDF_BCAST, MDF_MCAST, and MDF_ACAST. The 827 * last can be argued as responses are received, but those responses do 828 * not affect the MDF_ACAST association's reach register, rather they 829 * (may) result in mobilizing ephemeral MDF_ACLNT associations. 830 */ 831 #define MDF_TXONLY_MASK (MDF_BCAST | MDF_MCAST | MDF_ACAST | MDF_POOL) 832 /* 833 * manycastclient-like solicitor association cast_flags bits 834 */ 835 #define MDF_SOLICIT_MASK (MDF_ACAST | MDF_POOL) 836 /* 837 * Values used with mon_enabled to indicate reason for enabling monitoring 838 */ 839 #define MON_OFF 0x00 /* no monitoring */ 840 #define MON_ON 0x01 /* monitoring explicitly enabled */ 841 #define MON_RES 0x02 /* implicit monitoring for RES_LIMITED */ 842 /* 843 * Structure used for restrictlist entries 844 */ 845 typedef struct res_addr4_tag { 846 u_int32 addr; /* IPv4 addr (host order) */ 847 u_int32 mask; /* IPv4 mask (host order) */ 848 } res_addr4; 849 850 typedef struct res_addr6_tag { 851 struct in6_addr addr; /* IPv6 addr (net order) */ 852 struct in6_addr mask; /* IPv6 mask (net order) */ 853 } res_addr6; 854 855 typedef struct restrict_u_tag restrict_u; 856 struct restrict_u_tag { 857 restrict_u * link; /* link to next entry */ 858 u_int32 count; /* number of packets matched */ 859 u_int32 expire; /* valid until current_time */ 860 u_short rflags; /* restrict (accesslist) flags */ 861 u_int32 mflags; /* match flags */ 862 short ippeerlimit; /* limit of associations matching */ 863 union { /* variant starting here */ 864 res_addr4 v4; 865 res_addr6 v6; 866 } u; 867 }; 868 #define V4_SIZEOF_RESTRICT_U (offsetof(restrict_u, u) \ 869 + sizeof(res_addr4)) 870 #define V6_SIZEOF_RESTRICT_U (offsetof(restrict_u, u) \ 871 + sizeof(res_addr6)) 872 873 /* restrictions for (4) a given address */ 874 typedef struct r4addr_tag r4addr; 875 struct r4addr_tag { 876 u_short rflags; /* match flags */ 877 short ippeerlimit; /* IP peer limit */ 878 }; 879 880 /* 881 * Restrict (Access) flags (rflags) 882 */ 883 #define RES_IGNORE 0x0001 /* ignore packet */ 884 #define RES_DONTSERVE 0x0002 /* access denied */ 885 #define RES_DONTTRUST 0x0004 /* authentication required */ 886 #define RES_VERSION 0x0008 /* version mismatch */ 887 #define RES_NOPEER 0x0010 /* new association denied */ 888 #define RES_NOEPEER 0x0020 /* new ephemeral association denied */ 889 #define RES_LIMITED 0x0040 /* packet rate exceeded */ 890 #define RES_NOQUERY 0x0080 /* mode 6/7 packet denied */ 891 #define RES_NOMODIFY 0x0100 /* mode 6/7 modify denied */ 892 #define RES_NOTRAP 0x0200 /* mode 6/7 set trap denied */ 893 #define RES_LPTRAP 0x0400 /* mode 6/7 low priority trap */ 894 895 #define RES_KOD 0x0800 /* send kiss of death packet */ 896 #define RES_MSSNTP 0x1000 /* enable MS-SNTP authentication */ 897 #define RES_FLAKE 0x2000 /* flakeway - drop 10% */ 898 #define RES_NOMRULIST 0x4000 /* mode 6 mrulist denied */ 899 900 #define RES_SRVRSPFUZ 0x8000 /* Server response: fuzz */ 901 902 #define RES_UNUSED 0x0000 /* Unused flag bits (none left) */ 903 904 #define RES_ALLFLAGS (RES_IGNORE | RES_DONTSERVE | \ 905 RES_DONTTRUST | RES_VERSION | \ 906 RES_NOPEER | RES_NOEPEER | \ 907 RES_LIMITED | RES_NOQUERY | \ 908 RES_NOMODIFY | RES_NOTRAP | \ 909 RES_LPTRAP | RES_KOD | \ 910 RES_MSSNTP | RES_FLAKE | \ 911 RES_NOMRULIST | RES_SRVRSPFUZ ) 912 913 /* 914 * Match flags (mflags) 915 */ 916 #define RESM_INTERFACE 0x1000 /* this is an interface */ 917 #define RESM_NTPONLY 0x2000 /* match source port 123 */ 918 #define RESM_SOURCE 0x4000 /* from "restrict source" */ 919 920 /* 921 * Restriction configuration ops 922 */ 923 typedef enum 924 restrict_ops { 925 RESTRICT_FLAGS = 1, /* add rflags to restrict entry */ 926 RESTRICT_UNFLAG, /* remove rflags from restrict entry */ 927 RESTRICT_REMOVE, /* remove a restrict entry */ 928 RESTRICT_REMOVEIF, /* remove an interface restrict entry */ 929 } restrict_op; 930 931 /* 932 * Endpoint structure for the select algorithm 933 */ 934 struct endpoint { 935 double val; /* offset of endpoint */ 936 int type; /* interval entry/exit */ 937 }; 938 939 /* 940 * Association matching AM[] return codes 941 */ 942 #define AM_ERR -1 /* error */ 943 #define AM_NOMATCH 0 /* no match */ 944 #define AM_PROCPKT 1 /* server/symmetric packet */ 945 #define AM_BCST 2 /* broadcast packet */ 946 #define AM_FXMIT 3 /* client packet */ 947 #define AM_MANYCAST 4 /* manycast or pool */ 948 #define AM_NEWPASS 5 /* new passive */ 949 #define AM_NEWBCL 6 /* new broadcast */ 950 #define AM_POSSBCL 7 /* discard broadcast */ 951 952 /* NetInfo configuration locations */ 953 #ifdef HAVE_NETINFO 954 #define NETINFO_CONFIG_DIR "/config/ntp" 955 #endif 956 957 /* ntpq -c mrulist rows per request limit in ntpd */ 958 #define MRU_ROW_LIMIT 256 959 /* similar datagrams per response limit for ntpd */ 960 #define MRU_FRAGS_LIMIT 128 961 962 /* found on POSIX systems in sysexit.h */ 963 #ifndef EX_SOFTWARE 964 # define EX_SOFTWARE 70 /* internal software error */ 965 #endif 966 967 #define BYTESWAP32(u32) \ 968 (((u_int32)(u32) & 0xff000000) >> 24 | \ 969 ((u_int32)(u32) & 0xff0000) >> 8 | \ 970 ((u_int32)(u32) & 0xff00) << 8 | \ 971 ((u_int32)(u32) & 0xff) << 24) 972 #endif /* NTP_H */ 973