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