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