/contrib/ntp/include/ntp.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 907 lines · 550 code · 89 blank · 268 comment · 29 complexity · 25a5b3b25581c86f06de7adb4c421d0b MD5 · raw file

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