1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * University Copyright- Copyright (c) 1982, 1986, 1988 29 * The Regents of the University of California 30 * All Rights Reserved 31 * 32 * University Acknowledgment- Portions of this document are derived from 33 * software developed by the University of California, Berkeley, and its 34 * contributors. 35 */ 36 37 #ifndef _NTP_H 38 #define _NTP_H 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 /* $Source: /usr/users/louie/ntp/RCS/ntp.h,v $ */ 45 /* $Revision: 3.4.1.5 $ $Date: 89/04/10 15:55:42 $ */ 46 47 /* 48 * $Log: ntp.h,v $ 49 * Revision 3.4.1.5 89/04/10 15:55:42 louie 50 * Provide default value for number of bits/byte if not defined. Compute the 51 * Window shift mask inside of conditional code on XTAL so we get the correct 52 * value if configured without a crystal controled clock (!!) 53 * 54 * Revision 3.4.1.4 89/03/31 16:34:50 louie 55 * Add bit in flags which allow a peer to be synced to. Changed a char to a bit 56 * field so that it is always signed. 57 * 58 * Revision 3.4.1.3 89/03/29 12:26:18 louie 59 * Removed some unused #defines. Replaced MAXSTRATUM with NTP_INFIN per new 60 * spec. The variable 'mode' in the peer structure has been renamed 'hmode' 61 * per the new spec. 62 * 63 * Revision 3.4.1.2 89/03/22 18:28:18 louie 64 * patch3: Use new RCS headers. 65 * 66 * Revision 3.4.1.1 89/03/20 00:02:53 louie 67 * 1 68 * 69 * Revision 3.4 89/03/17 18:37:00 louie 70 * Latest test release. 71 * 72 * Revision 3.3.1.1 89/03/17 18:23:49 louie 73 * Change CLOCK_FACTOR to be a power of 2. 74 * 75 * Revision 3.3 89/03/15 14:19:36 louie 76 * New baseline for next release. 77 * 78 * Revision 3.2.1.2 89/03/15 13:46:52 louie 79 * The version number for that particular flavor of ntpd <--> ntpdc interaction 80 * is now defined by NTPDC_VERSION. The packet format for the ntpdc program 81 * has changed slightly to improve robustness when dealing with multiple packets 82 * of status data. 83 * 84 * Revision 3.2.1.1 89/03/09 17:11:24 louie 85 * patch1: Updated constants, which were previously in incorrect units. 86 * 87 * Revision 3.2 89/03/07 18:21:45 louie 88 * New version of UNIX NTP daemon and software based on the 6 March 1989 89 * draft of the new NTP protocol specification. This version doesn't 90 * implement authentication, and accepts and send only NTP Version 1 91 * packets. 92 * 93 * Revision 3.1.1.1 89/02/15 08:54:42 louie 94 * *** empty log message *** 95 * 96 * 97 * Revision 3.1 89/01/30 14:43:07 louie 98 * Second UNIX NTP test release. 99 * 100 * Revision 3.0 88/12/12 16:01:07 louie 101 * Test release of new UNIX NTP software. This version should conform to the 102 * revised NTP protocol specification. 103 * 104 */ 105 106 #ifndef FD_SET 107 #define NFDBITS 32 108 #define FD_SETSIZE 32 109 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) 110 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) 111 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) 112 #define FD_ZERO(p) bzero((char *)(p), sizeof (*(p))) 113 #endif 114 115 #ifndef NBBY 116 #define NBBY 8 /* number of bits per byte */ 117 #endif 118 119 #define MAXNETIF 10 120 121 struct intf { 122 int fd; 123 char *name; 124 struct sockaddr_in sin; 125 struct sockaddr_in bcast; 126 struct sockaddr_in mask; 127 int uses; 128 int if_flags; 129 }; 130 extern struct intf addrs[]; 131 extern int nintf; 132 133 /* 134 * Definitions for the masses 135 */ 136 #define JAN_1970 2208988800U /* 1970 - 1900 in seconds */ 137 138 /* 139 * Daemon specific (ntpd.c) 140 */ 141 #define SHIFT_MASK 0xff /* number of intervals to wait */ 142 143 #ifndef WAYTOOBIG 144 #define WAYTOOBIG 1000.0 /* Too many seconds to correct, something is */ 145 /* really wrong */ 146 #endif 147 148 #ifndef XTAL 149 #define XTAL 1 /* crystal controlled clock by default */ 150 #endif 151 152 #ifndef NTPINITFILE 153 #define NTPINITFILE "/etc/ntp.conf" 154 #endif 155 156 struct list { 157 struct ntp_peer *head; 158 struct ntp_peer *tail; 159 int members; 160 }; 161 162 #define STRMCMP(a, cond, b) \ 163 (((a) == UNSPECIFIED ? NTP_INFIN+1 : a) cond \ 164 ((b) == UNSPECIFIED ? NTP_INFIN+1 : (b))) 165 166 167 /* 168 * Definitions outlined in the NTP spec 169 */ 170 #define NTP_VERSION 1 171 #define NTP_PORT 123 /* for ref only (see /etc/services) */ 172 #define NTP_INFIN 15 173 #define NTP_MAXAGE 86400 174 #define NTP_MAXSKW 0.01 /* seconds */ 175 #define NTP_MINDIST 0.02 /* seconds */ 176 #define NTP_MINPOLL 6 /* (64) seconds between messages */ 177 #define NTP_MAXPOLL 10 /* (1024) secs to poll */ 178 #define NTP_WINDOW 8 /* size of shift register */ 179 #define NTP_MAXWGT 8 /* maximum allowable dispersion */ 180 #define NTP_MAXLIST 5 /* max size of selection list */ 181 #define NTP_MAXSTRA 2 /* max number of strata in selection list */ 182 #define X_NTP_CANDIDATES 64 /* number of peers to consider when doing */ 183 /* clock selection */ 184 #define NTP_SELECT 0.75 /* weight used to compute dispersion */ 185 186 #define PEER_MAXDISP 64.0 /* Maximum dispersion */ 187 #define PEER_THRESHOLD 0.5 /* dispersion threshold */ 188 #define PEER_FILTER 0.5 /* filter weight */ 189 190 #if XTAL == 0 191 #define PEER_SHIFT 4 192 #define NTP_WINDOW_SHIFT_MASK 0x0f 193 #else 194 #define PEER_SHIFT 8 195 #define NTP_WINDOW_SHIFT_MASK 0xff 196 #endif 197 198 199 /* 200 * 5.1 Uniform Phase Adjustments 201 * Clock parameters 202 */ 203 #define CLOCK_UPDATE 8 /* update interval (1<<CLOCK_UPDATE secs) */ 204 #if XTAL 205 #define CLOCK_ADJ 2 /* adjustment interval (1<<CLOCK_ADJ secs) */ 206 #define CLOCK_PHASE 8 /* phase shift */ 207 #define CLOCK_MAX 0.128 /* maximum aperture (milliseconds) */ 208 #else 209 #define CLOCK_ADJ 0 210 #define CLOCK_PHASE 6 /* phase shift */ 211 #define CLOCK_MAX 0.512 /* maximum aperture (milliseconds) */ 212 #endif 213 #define CLOCK_FREQ 10 /* frequency shift */ 214 #define CLOCK_TRACK 8 215 #define CLOCK_COMP 4 216 #define CLOCK_FACTOR 18 217 218 /* 219 * Structure definitions for NTP fixed point values 220 * 221 * 0 1 2 3 222 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 223 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 224 * | Integer Part | 225 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 226 * | Fraction Part | 227 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 228 * 229 * 230 * 0 1 2 3 231 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 232 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 233 * | Integer Part | Fraction Part | 234 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 235 */ 236 struct l_fixedpt { 237 ulong_t int_part; 238 ulong_t fraction; 239 }; 240 241 struct s_fixedpt { 242 ushort_t int_part; 243 ushort_t fraction; 244 }; 245 246 /* 247 * ================= Table 3.3. Packet Variables ================= 248 * 0 1 2 3 249 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 250 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 251 * |LI | VN | Mode| Stratum | Poll | Precision | 252 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 253 * | Synchronizing Distance | 254 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 255 * | Synchronizing Dispersion | 256 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 257 * | Reference Clock Identifier | 258 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 259 * | | 260 * | Reference Timestamp (64 bits) | 261 * | | 262 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 263 * | | 264 * | Originate Timestamp (64 bits) | 265 * | | 266 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 267 * | | 268 * | Receive Timestamp (64 bits) | 269 * | | 270 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 271 * | | 272 * | Transmit Timestamp (64 bits) | 273 * | | 274 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 275 * | Encryption Keyid (32 bits, when A bit set) | 276 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 277 * | | 278 * | Message Authentication Code/MAC (when A bit set) | 279 * | | 280 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 281 */ 282 283 #define MAC_OCTETS_DES 8 284 #define MAC_OCTETS_MD5 16 285 #define MAC_OCTETS_MIN MAC_OCTETS_DES 286 #define MAC_OCTETS_MAX MAC_OCTETS_MD5 287 #define AUTH_OCTETS_V3 (MAC_OCTETS_MAX + sizeof (uint32_t)) 288 289 struct ntpdata { 290 uchar_t li_vn_mode; /* contains leap indicator, version and mode */ 291 uchar_t stratum; /* Stratum level */ 292 uchar_t ppoll; /* poll value */ 293 int precision:8; 294 struct s_fixedpt distance; 295 struct s_fixedpt dispersion; 296 ulong_t refid; 297 struct l_fixedpt reftime; 298 struct l_fixedpt org; 299 struct l_fixedpt rec; 300 struct l_fixedpt xmt; 301 uint32_t keyid; 302 uchar_t mac[MAC_OCTETS_MAX]; 303 }; 304 305 #define LEN_PKT_NOMAC (sizeof (struct ntpdata) - AUTH_OCTETS_V3) 306 307 /* 308 * Leap Second Codes (high order two bits) 309 */ 310 #define NO_WARNING 0x00 /* no warning */ 311 #define PLUS_SEC 0x40 /* add a second (61 seconds) */ 312 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */ 313 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */ 314 315 /* 316 * Clock Status Bits that Encode Version 317 */ 318 #define NTPVERSION_1 0x08 319 #define VERSIONMASK 0x38 320 #define LEAPMASK 0xc0 321 #define NTPMODEMASK 0x07 322 323 /* 324 * Code values 325 */ 326 #define MODE_UNSPEC 0 /* unspecified */ 327 #define MODE_SYM_ACT 1 /* symmetric active */ 328 #define MODE_SYM_PAS 2 /* symmetric passive */ 329 #define MODE_CLIENT 3 /* client */ 330 #define MODE_SERVER 4 /* server */ 331 #define MODE_BROADCAST 5 /* broadcast */ 332 #define MODE_CONTROL 6 /* control */ 333 #define MODE_PRIVATE 7 /* private */ 334 335 /* 336 * Stratum Definitions 337 */ 338 #define UNSPECIFIED 0 339 #define PRIM_REF 1 /* radio clock */ 340 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */ 341 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */ 342 343 344 /* ================= table 3.2 Peer Variables ================= */ 345 struct ntp_peer { 346 struct ntp_peer *next, *prev; 347 struct sockaddr_in src; /* both peer.srcadr and peer.srcport */ 348 int flags; /* local flags */ 349 #define PEER_FL_CONFIG 1 350 #define PEER_FL_AUTHENABLE 2 351 #define PEER_FL_SYNC 0x1000 /* peer can bet sync'd to */ 352 #define PEER_FL_BCAST 0x2000 /* broadcast peer */ 353 #define PEER_FL_SELECTED 0x8000 /* actually used by query routine */ 354 355 int sock; /* index into sockets to derive */ 356 /* peer.dstadr and peer.dstport */ 357 uchar_t leap; /* receive */ 358 uchar_t hmode; /* receive */ 359 uchar_t stratum; /* receive */ 360 uchar_t ppoll; /* receive */ 361 uchar_t hpoll; /* poll update */ 362 short precision; /* receive */ 363 struct s_fixedpt distance; /* receive */ 364 struct s_fixedpt dispersion; /* receive */ 365 ulong_t refid; /* receive */ 366 struct l_fixedpt reftime; /* receive */ 367 struct l_fixedpt org; /* receive, clear */ 368 struct l_fixedpt rec; /* receive, clear */ 369 struct l_fixedpt xmt; /* transmit, clear */ 370 ulong_t reach; /* receive, transmit, clear */ 371 ulong_t valid; /* packet, transmit, clear */ 372 ulong_t timer; /* receive, transmit, poll update */ 373 long stopwatch; /* <<local>> for timing */ 374 /* 375 * first order offsets 376 */ 377 struct filter { 378 short samples; /* <<local>> */ 379 double offset[PEER_SHIFT]; 380 double delay[PEER_SHIFT]; 381 } filter; /* filter, clear */ 382 383 double estdelay; /* filter */ 384 double estoffset; /* filter */ 385 double estdisp; /* filter */ 386 387 ulong_t pkt_sent; /* <<local>> */ 388 ulong_t pkt_rcvd; /* <<local>> */ 389 ulong_t pkt_dropped; /* <<local>> */ 390 }; 391 392 /* ================= table 3.1: System Variables ================= */ 393 394 struct sysdata { /* procedure */ 395 uchar_t leap; /* clock update */ 396 uchar_t stratum; /* clock update */ 397 short precision; /* system */ 398 struct s_fixedpt distance; /* clock update */ 399 struct s_fixedpt dispersion; /* clock update */ 400 ulong_t refid; /* clock update */ 401 struct l_fixedpt reftime; /* clock update */ 402 int hold; /* clock update */ 403 struct ntp_peer *peer; /* selection */ 404 int maxpeers; /* <<local>> */ 405 uchar_t filler; /* put here for %&*%$$ SUNs */ 406 }; 407 408 #define NTPDC_VERSION 2 409 410 /* 411 * These structures are used to pass information to the ntpdc (control) 412 * program. They are unique to this implementation and not part of the 413 * NTP specification. 414 */ 415 struct clockinfo { 416 ulong_t net_address; 417 ulong_t my_address; 418 ushort_t port; 419 ushort_t flags; 420 ulong_t pkt_sent; 421 ulong_t pkt_rcvd; 422 ulong_t pkt_dropped; 423 ulong_t timer; 424 uchar_t leap; 425 uchar_t stratum; 426 uchar_t ppoll; 427 int precision:8; 428 429 uchar_t hpoll; 430 uchar_t filler1; 431 ushort_t reach; 432 433 long estdisp; /* scaled by 1000 */ 434 long estdelay; /* in milliseconds */ 435 long estoffset; /* in milliseconds */ 436 ulong_t refid; 437 struct l_fixedpt reftime; 438 struct info_filter { 439 short index; 440 short filler; 441 long offset[PEER_SHIFT]; /* in milliseconds */ 442 long delay[PEER_SHIFT]; /* in milliseconds */ 443 } info_filter; 444 }; 445 446 struct ntpinfo { 447 uchar_t version; 448 uchar_t type; /* request type (stratum in ntp packets) */ 449 uchar_t count; /* number of entries in this packet */ 450 uchar_t seq; /* sequence number of this packet */ 451 452 uchar_t npkts; /* total number of packets */ 453 uchar_t peers; 454 uchar_t fill3; 455 uchar_t fill4; 456 }; 457 458 /* 459 * From usr/src/cmd/xntpd/include/ntp_control.h: 460 * Definition of a mode 6 packet. 461 */ 462 struct ntp_control { 463 uchar_t li_vn_mode; /* leap, version, mode */ 464 uchar_t r_m_e_op; /* response, more, error, opcode */ 465 ushort_t sequence; /* sequence number of request */ 466 ushort_t status; /* status word for association */ 467 ushort_t associd; /* association ID */ 468 ushort_t offset; /* offset of this batch of data */ 469 ushort_t count; /* count of data in this packet */ 470 uchar_t data[1]; /* data + auth */ 471 }; 472 473 #define NTPC_DATA_MAXLEN (480 + AUTH_OCTETS_V3) 474 475 /* 476 * Decoding for the r_m_e_op field 477 */ 478 #define CTL_RESPONSE 0x80 479 #define CTL_ERROR 0x40 480 #define CTL_MORE 0x20 481 #define CTL_OP_MASK 0x1f 482 483 /* 484 * Opcodes 485 */ 486 #define CTL_OP_UNSPEC 0 487 #define CTL_OP_READSTAT 1 488 #define CTL_OP_READVAR 2 489 #define CTL_OP_WRITEVAR 3 490 #define CTL_OP_READCLOCK 4 491 #define CTL_OP_WRITECLOCK 5 492 #define CTL_OP_SETTRAP 6 493 #define CTL_OP_ASYNCMSG 7 494 #define CTL_OP_UNSETTRAP 31 495 496 /* 497 * From usr/src/cmd/xntpd/include/ntp_request.h: 498 * A mode 7 packet is used exchanging data between an NTP server 499 * and a client for purposes other than time synchronization, e.g. 500 * monitoring, statistics gathering and configuration. A mode 7 501 * packet has the following format: 502 */ 503 504 struct ntp_private { 505 uchar_t rm_vn_mode; /* response, more, version, mode */ 506 uchar_t auth_seq; /* key, sequence number */ 507 uchar_t implementation; /* implementation number */ 508 uchar_t request; /* request number */ 509 ushort_t err_nitems; /* error code/number of data items */ 510 ushort_t mbz_itemsize; /* item size */ 511 char data[1]; /* data area */ 512 }; 513 514 #define RESP_BIT 0x80 515 #define MORE_BIT 0x40 516 #define INFO_VERSION(rm_vn_mode) ((uchar_t)(((rm_vn_mode)>>3) & 0x7)) 517 #define INFO_MODE(rm_vn_mode) ((rm_vn_mode) & 0x7) 518 519 #define AUTH_BIT 0x80 520 #define INFO_SEQ(auth_seq) ((auth_seq) & 0x7f) 521 522 #define INFO_ERR(err_nitems) ((ushort_t)((ntohs(err_nitems) >> 12) & 0xf)) 523 #define INFO_NITEMS(err_nitems) ((ushort_t)(ntohs(err_nitems) & 0xfff)) 524 525 #define INFO_ITEMSIZE(mbz_itemsize) (ntohs(mbz_itemsize) & 0xfff) 526 527 #ifdef __cplusplus 528 } 529 #endif 530 531 #endif /* _NTP_H */ 532