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