1ef64b99roberto/*
2ef64b99roberto * ntp.h - NTP definitions for the masses
3ef64b99roberto */
4ef64b99roberto#ifndef NTP_H
5ef64b99roberto#define NTP_H
6ef64b99roberto
7d54cfbdroberto#include <stddef.h>
8ef64b99roberto#include <math.h>
9d54cfbdroberto
10d54cfbdroberto#include <ntp_fp.h>
11d54cfbdroberto#include <ntp_types.h>
12047f369cy#include <ntp_lists.h>
13d54cfbdroberto#include <ntp_stdlib.h>
14d54cfbdroberto#include <ntp_crypto.h>
157a6072eroberto#include <ntp_random.h>
16d54cfbdroberto#include <ntp_net.h>
177a6072eroberto
187a6072eroberto#include <isc/boolean.h>
19ef64b99roberto
2040b8e41roberto/*
2140b8e41roberto * Calendar arithmetic - contributed by G. Healton
2240b8e41roberto */
2340b8e41roberto#define YEAR_BREAK 500		/* years < this are tm_year values:
2440b8e41roberto				 * Break < AnyFourDigitYear && Break >
2540b8e41roberto				 * Anytm_yearYear */
2640b8e41roberto
2740b8e41roberto#define YEAR_PIVOT 98		/* 97/98: years < this are year 2000+
2840b8e41roberto				 * FYI: official UNIX pivot year is
2940b8e41roberto				 * 68/69 */
3040b8e41roberto
3140b8e41roberto/*
3240b8e41roberto * Number of Days since 1 BC Gregorian to 1 January of given year
3340b8e41roberto */
3440b8e41roberto#define julian0(year)	(((year) * 365 ) + ((year) > 0 ? (((year) + 3) \
3540b8e41roberto			    / 4 - ((year - 1) / 100) + ((year - 1) / \
3640b8e41roberto			    400)) : 0))
3740b8e41roberto
3840b8e41roberto/*
3940b8e41roberto * Number of days since start of NTP time to 1 January of given year
4040b8e41roberto */
4140b8e41roberto#define ntp0(year)	(julian0(year) - julian0(1900))
4240b8e41roberto
4340b8e41roberto/*
4440b8e41roberto * Number of days since start of UNIX time to 1 January of given year
4540b8e41roberto */
4640b8e41roberto#define unix0(year)	(julian0(year) - julian0(1970))
4740b8e41roberto
4840b8e41roberto/*
4940b8e41roberto * LEAP YEAR test for full 4-digit years (e.g, 1999, 2010)
5040b8e41roberto */
5140b8e41roberto#define isleap_4(y)	((y) % 4 == 0 && !((y) % 100 == 0 && !(y % \
5240b8e41roberto			    400 == 0)))
5340b8e41roberto
5440b8e41roberto/*
5540b8e41roberto * LEAP YEAR test for tm_year (struct tm) years (e.g, 99, 110)
5640b8e41roberto */
5740b8e41roberto#define isleap_tm(y)	((y) % 4 == 0 && !((y) % 100 == 0 && !(((y) \
5840b8e41roberto			    + 1900) % 400 == 0)))
5940b8e41roberto
6040b8e41roberto/*
6140b8e41roberto * to convert simple two-digit years to tm_year style years:
6240b8e41roberto *
6340b8e41roberto *	if (year < YEAR_PIVOT)
6440b8e41roberto *		year += 100;
6540b8e41roberto *
6640b8e41roberto * to convert either two-digit OR tm_year years to four-digit years:
6740b8e41roberto *
6840b8e41roberto *	if (year < YEAR_PIVOT)
6940b8e41roberto *		year += 100;
7040b8e41roberto *
7140b8e41roberto *	if (year < YEAR_BREAK)
7240b8e41roberto *		year += 1900;
7340b8e41roberto */
74ef64b99roberto
75ef64b99roberto/*
76ef64b99roberto * How to get signed characters.  On machines where signed char works,
7740b8e41roberto * use it. On machines where signed char doesn't work, char had better
78ef64b99roberto * be signed.
79ef64b99roberto */
80ef64b99roberto#ifdef NEED_S_CHAR_TYPEDEF
81ef64b99roberto# if SIZEOF_SIGNED_CHAR
82ef64b99robertotypedef signed char s_char;
83ef64b99roberto# else
84ef64b99robertotypedef char s_char;
85ef64b99roberto# endif
86ef64b99roberto  /* XXX: Why is this sequent bit INSIDE this test? */
87ef64b99roberto# ifdef sequent
88ef64b99roberto#  undef SO_RCVBUF
89ef64b99roberto#  undef SO_SNDBUF
90ef64b99roberto# endif
91ef64b99roberto#endif
92ef64b99roberto
93ef64b99roberto/*
94ef64b99roberto * NTP protocol parameters.  See section 3.2.6 of the specification.
95ef64b99roberto */
96ef64b99roberto#define	NTP_VERSION	((u_char)4) /* current version number */
97ef64b99roberto#define	NTP_OLDVERSION	((u_char)1) /* oldest credible version */
98118e757roberto#define	NTP_PORT	123	/* included for non-unix machines */
99118e757roberto
100118e757roberto/*
101118e757roberto * Poll interval parameters
102118e757roberto */
103d54cfbdroberto#define NTP_UNREACH	10	/* poll unreach threshold */
104d54cfbdroberto#define	NTP_MINPOLL	3	/* log2 min poll interval (8 s) */
105118e757roberto#define NTP_MINDPOLL	6	/* log2 default min poll (64 s) */
106118e757roberto#define NTP_MAXDPOLL	10	/* log2 default max poll (~17 m) */
107118e757roberto#define	NTP_MAXPOLL	17	/* log2 max poll interval (~36 h) */
108d54cfbdroberto#define	NTP_RETRY	3	/* max packet retries */
109047f369cy#define	NTP_MINPKT	2	/* guard time (s) */
110118e757roberto
111118e757roberto/*
112118e757roberto * Clock filter algorithm tuning parameters
113118e757roberto */
114118e757roberto#define MAXDISPERSE	16.	/* max dispersion */
115118e757roberto#define	NTP_SHIFT	8	/* clock filter stages */
116ef64b99roberto#define NTP_FWEIGHT	.5	/* clock filter weight */
117118e757roberto
118118e757roberto/*
119118e757roberto * Selection algorithm tuning parameters
120118e757roberto */
1217a6072eroberto#define	NTP_MINCLOCK	3	/* min survivors */
1227a6072eroberto#define	NTP_MAXCLOCK	10	/* max candidates */
123d54cfbdroberto#define MINDISPERSE	.001	/* min distance */
124d54cfbdroberto#define MAXDISTANCE	1.5	/* max root distance (select threshold) */
125118e757roberto#define CLOCK_SGATE	3.	/* popcorn spike gate */
12640b8e41roberto#define HUFFPUFF	900	/* huff-n'-puff sample interval (s) */
1277a6072eroberto#define MAXHOP		2	/* anti-clockhop threshold */
128118e757roberto#define MAX_TTL		8	/* max ttl mapping vector size */
1297a6072eroberto#define	BEACON		7200	/* manycast beacon interval */
130d54cfbdroberto#define NTP_MAXEXTEN	2048	/* max extension field size */
13170964a8cy#define	NTP_ORPHWAIT	300	/* orphan wait (s) */
132118e757roberto
133118e757roberto/*
134118e757roberto * Miscellaneous stuff
135118e757roberto */
1367a6072eroberto#define NTP_MAXKEY	65535	/* max authentication key number */
137d54cfbdroberto#define	KEY_TYPE_MD5	NID_md5	/* MD5 digest NID */
138118e757roberto/*
139118e757roberto * Limits of things
140118e757roberto */
141047f369cy#define	MAXFILENAME	256	/* max length of file name */
142118e757roberto#define MAXHOSTNAME	512	/* max length of host/node name */
1437a6072eroberto#define NTP_MAXSTRLEN	256	/* max string length */
14440b8e41roberto
145ef64b99roberto/*
14640b8e41roberto * Operations for jitter calculations (these use doubles).
14740b8e41roberto *
14840b8e41roberto * Note that we carefully separate the jitter component from the
14940b8e41roberto * dispersion component (frequency error plus precision). The frequency
15040b8e41roberto * error component is computed as CLOCK_PHI times the difference between
15140b8e41roberto * the epoch of the time measurement and the reference time. The
152d54cfbdroberto * precision component is computed as the square root of the mean of the
15340b8e41roberto * squares of a zero-mean, uniform distribution of unit maximum
15440b8e41roberto * amplitude. Whether this makes statistical sense may be arguable.
155ef64b99roberto */
156ef64b99roberto#define SQUARE(x) ((x) * (x))
157ef64b99roberto#define SQRT(x) (sqrt(x))
158ef64b99roberto#define DIFF(x, y) (SQUARE((x) - (y)))
159047f369cy#define LOGTOD(a)	ldexp(1., (int)(a)) /* log2 to double */
160ef64b99roberto#define UNIVAR(x)	(SQUARE(.28867513 * LOGTOD(x))) /* std uniform distr */
161047f369cy#define ULOGTOD(a)	ldexp(1., (int)(a)) /* ulog2 to double */
162ef64b99roberto
16340b8e41roberto#define	EVENT_TIMEOUT	0	/* one second, that is */
16440b8e41roberto
165d54cfbdroberto
166ef64b99roberto/*
167ef64b99roberto * The interface structure is used to hold the addresses and socket
168d54cfbdroberto * numbers of each of the local network addresses we are using.
169d54cfbdroberto * Because "interface" is a reserved word in C++ and has so many
170d54cfbdroberto * varied meanings, a change to "endpt" (via typedef) is under way.
171d54cfbdroberto * Eventually the struct tag will change from interface to endpt_tag.
172d54cfbdroberto * endpt is unrelated to the select algorithm's struct endpoint.
173ef64b99roberto */
174d54cfbdrobertotypedef struct interface endpt;
175ef64b99robertostruct interface {
176d54cfbdroberto	endpt *		elink;		/* endpt list link */
177d54cfbdroberto	endpt *		mclink;		/* per-AF_* multicast list */
1787598092delphij	void *		ioreg_ctx;	/* IO registration context */
179d54cfbdroberto	SOCKET		fd;		/* socket descriptor */
180d54cfbdroberto	SOCKET		bfd;		/* for receiving broadcasts */
181d54cfbdroberto	u_int32		ifnum;		/* endpt instance count */
182d54cfbdroberto	sockaddr_u	sin;		/* unicast address */
183d54cfbdroberto	sockaddr_u	mask;		/* subnet mask */
184d54cfbdroberto	sockaddr_u	bcast;		/* broadcast address */
185d54cfbdroberto	char		name[32];	/* name of interface */
186d54cfbdroberto	u_short		family;		/* AF_INET/AF_INET6 */
187d54cfbdroberto	u_short		phase;		/* phase in update cycle */
188d54cfbdroberto	u_int32		flags;		/* interface flags */
189d54cfbdroberto	int		last_ttl;	/* last TTL specified */
190d54cfbdroberto	u_int32		addr_refid;	/* IPv4 addr or IPv6 hash */
191d54cfbdroberto	int		num_mcast;	/* mcast addrs enabled */
192d54cfbdroberto	u_long		starttime;	/* current_time at creation */
193d54cfbdroberto	volatile long	received;	/* number of incoming packets */
194d54cfbdroberto	long		sent;		/* number of outgoing packets */
195d54cfbdroberto	long		notsent;	/* number of send failures */
196d54cfbdroberto	u_int		ifindex;	/* for IPV6_MULTICAST_IF */
197d54cfbdroberto	isc_boolean_t	ignore_packets; /* listen-read-drop this? */
198d54cfbdroberto	struct peer *	peers;		/* list of peers using endpt */
199d54cfbdroberto	u_int		peercnt;	/* count of same */
200ef64b99roberto};
201ef64b99roberto
202ef64b99roberto/*
203ef64b99roberto * Flags for interfaces
204ef64b99roberto */
2057a6072eroberto#define INT_UP		0x001	/* Interface is up */
2067a6072eroberto#define	INT_PPP		0x002	/* Point-to-point interface */
2077a6072eroberto#define	INT_LOOPBACK	0x004	/* the loopback interface */
2087a6072eroberto#define	INT_BROADCAST	0x008	/* can broadcast out this interface */
2097a6072eroberto#define INT_MULTICAST	0x010	/* can multicast out this interface */
210047f369cy#define	INT_BCASTOPEN	0x020	/* broadcast receive socket is open */
2117a6072eroberto#define INT_MCASTOPEN	0x040	/* multicasting enabled */
212d54cfbdroberto#define INT_WILDCARD	0x080	/* wildcard interface - usually skipped */
213d54cfbdroberto#define INT_MCASTIF	0x100	/* bound directly to MCAST address */
214d54cfbdroberto#define INT_PRIVACY	0x200	/* RFC 4941 IPv6 privacy address */
215047f369cy#define INT_BCASTXMIT	0x400   /* socket setup to allow broadcasts */
216d54cfbdroberto
217ef64b99roberto/*
21840b8e41roberto * Define flasher bits (tests 1 through 11 in packet procedure)
219ef64b99roberto * These reveal the state at the last grumble from the peer and are
220ef64b99roberto * most handy for diagnosing problems, even if not strictly a state
221ef64b99roberto * variable in the spec. These are recorded in the peer structure.
2227a6072eroberto *
2237a6072eroberto * Packet errors
224ef64b99roberto */
2257a6072eroberto#define TEST1		0X0001	/* duplicate packet */
2267a6072eroberto#define TEST2		0x0002	/* bogus packet */
227ef64b99roberto#define TEST3		0x0004	/* protocol unsynchronized */
22840b8e41roberto#define TEST4		0x0008	/* access denied */
229d54cfbdroberto#define TEST5		0x0010	/* bad authentication */
2307a6072eroberto#define TEST6		0x0020	/* bad synch or stratum */
231d54cfbdroberto#define TEST7		0x0040	/* bad header */
232d54cfbdroberto#define TEST8		0x0080  /* bad autokey */
233d54cfbdroberto#define TEST9		0x0100	/* bad crypto */
2347a6072eroberto#define	PKT_TEST_MASK	(TEST1 | TEST2 | TEST3 | TEST4 | TEST5 |\
2357a6072eroberto			TEST6 | TEST7 | TEST8 | TEST9)
2367a6072eroberto/*
2377a6072eroberto * Peer errors
2387a6072eroberto */
2397a6072eroberto#define TEST10		0x0200	/* peer bad synch or stratum */
2407a6072eroberto#define	TEST11		0x0400	/* peer distance exceeded */
2417a6072eroberto#define TEST12		0x0800	/* peer synchronization loop */
2427a6072eroberto#define TEST13		0x1000	/* peer unreacable */
2437a6072eroberto#define	PEER_TEST_MASK	(TEST10 | TEST11 | TEST12 | TEST13)
2447a6072eroberto
2457a6072eroberto/*
2467598092delphij * Unused flags
2477598092delphij */
2487598092delphij#define TEST14		0x2000
2497598092delphij#define TEST15		0x4000
2507598092delphij#define TEST16		0x8000
2517598092delphij
2527598092delphij/*
25340b8e41roberto * The peer structure. Holds state information relating to the guys
25440b8e41roberto * we are peering with. Most of this stuff is from section 3.2 of the
255ef64b99roberto * spec.
256ef64b99roberto */
257ef64b99robertostruct peer {
258047f369cy	struct peer *p_link;	/* link pointer in free & peer lists */
259047f369cy	struct peer *adr_link;	/* link pointer in address hash */
260047f369cy	struct peer *aid_link;	/* link pointer in associd hash */
261d54cfbdroberto	struct peer *ilink;	/* list of peers for interface */
262d54cfbdroberto	sockaddr_u srcadr;	/* address of remote host */
263047f369cy	char *	hostname;	/* if non-NULL, remote name */
264047f369cy	struct addrinfo *addrs;	/* hostname query result */
265047f369cy	struct addrinfo *ai;	/* position within addrs */
266d54cfbdroberto	endpt *	dstadr;		/* local address */
26740b8e41roberto	associd_t associd;	/* association ID */
26840b8e41roberto	u_char	version;	/* version number */
26940b8e41roberto	u_char	hmode;		/* local association mode */
27040b8e41roberto	u_char	hpoll;		/* local poll interval */
27140b8e41roberto	u_char	minpoll;	/* min poll interval */
27240b8e41roberto	u_char	maxpoll;	/* max poll interval */
27340b8e41roberto	u_int	flags;		/* association flags */
27440b8e41roberto	u_char	cast_flags;	/* additional flags */
27540b8e41roberto	u_char	last_event;	/* last peer error code */
27640b8e41roberto	u_char	num_events;	/* number of error events */
277047f369cy	u_int32	ttl;		/* ttl/refclock mode */
278047f369cy	char	*ident;		/* group identifier name */
27940b8e41roberto
28040b8e41roberto	/*
28140b8e41roberto	 * Variables used by reference clock support
28240b8e41roberto	 */
2837a6072eroberto#ifdef REFCLOCK
28440b8e41roberto	struct refclockproc *procptr; /* refclock structure pointer */
28540b8e41roberto	u_char	refclktype;	/* reference clock type */
28640b8e41roberto	u_char	refclkunit;	/* reference clock unit number */
28740b8e41roberto	u_char	sstclktype;	/* clock type for system status word */
2887a6072eroberto#endif /* REFCLOCK */
28940b8e41roberto
29040b8e41roberto	/*
29140b8e41roberto	 * Variables set by received packet
29240b8e41roberto	 */
29340b8e41roberto	u_char	leap;		/* local leap indicator */
29440b8e41roberto	u_char	pmode;		/* remote association mode */
29540b8e41roberto	u_char	stratum;	/* remote stratum */
29640b8e41roberto	u_char	ppoll;		/* remote poll interval */
2977a6072eroberto	s_char	precision;	/* remote clock precision */
298d54cfbdroberto	double	rootdelay;	/* roundtrip delay to primary source */
299d54cfbdroberto	double	rootdisp;	/* dispersion to primary source */
30040b8e41roberto	u_int32	refid;		/* remote reference ID */
30140b8e41roberto	l_fp	reftime;	/* update epoch */
30240b8e41roberto
30340b8e41roberto	/*
30440b8e41roberto	 * Variables used by authenticated client
30540b8e41roberto	 */
30640b8e41roberto	keyid_t keyid;		/* current key ID */
307047f369cy#ifdef AUTOKEY
308d54cfbdroberto#define clear_to_zero opcode
309d54cfbdroberto	u_int32	opcode;		/* last request opcode */
31040b8e41roberto	associd_t assoc;	/* peer association ID */
31140b8e41roberto	u_int32	crypto;		/* peer status word */
312118e757roberto	EVP_PKEY *pkey;		/* public key */
313118e757roberto	const EVP_MD *digest;	/* message digest algorithm */
314118e757roberto	char	*subject;	/* certificate subject name */
315118e757roberto	char	*issuer;	/* certificate issuer name */
316d54cfbdroberto	struct cert_info *xinfo; /* issuer certificate */
31740b8e41roberto	keyid_t	pkeyid;		/* previous key ID */
318d54cfbdroberto	keyid_t	hcookie;	/* host cookie */
319118e757roberto	keyid_t	pcookie;	/* peer cookie */
320d54cfbdroberto	const struct pkey_info *ident_pkey; /* identity key */
321d54cfbdroberto	BIGNUM	*iffval;	/* identity challenge (IFF, GQ, MV) */
322d54cfbdroberto	const BIGNUM *grpkey;	/* identity challenge key (GQ) */
323d54cfbdroberto	struct value cookval;	/* receive cookie values */
324118e757roberto	struct value recval;	/* receive autokey values */
325118e757roberto	struct exten *cmmd;	/* extension pointer */
326d54cfbdroberto	u_long	refresh;	/* next refresh epoch */
327118e757roberto
32840b8e41roberto	/*
32940b8e41roberto	 * Variables used by authenticated server
33040b8e41roberto	 */
33140b8e41roberto	keyid_t	*keylist;	/* session key ID list */
33240b8e41roberto	int	keynumber;	/* current key number */
333118e757roberto	struct value encrypt;	/* send encrypt values */
334118e757roberto	struct value sndval;	/* send autokey values */
335047f369cy#else	/* !AUTOKEY follows */
33640b8e41roberto#define clear_to_zero status
337047f369cy#endif	/* !AUTOKEY */
33840b8e41roberto
33940b8e41roberto	/*
34040b8e41roberto	 * Ephemeral state variables
34140b8e41roberto	 */
34240b8e41roberto	u_char	status;		/* peer status */
343d54cfbdroberto	u_char	new_status;	/* under-construction status */
34440b8e41roberto	u_char	reach;		/* reachability register */
345d54cfbdroberto	int	flash;		/* protocol error test tally bits */
34640b8e41roberto	u_long	epoch;		/* reference epoch */
347d54cfbdroberto	int	burst;		/* packets remaining in burst */
348d54cfbdroberto	int	retry;		/* retry counter */
349d54cfbdroberto	int	flip;		/* interleave mode control */
350d54cfbdroberto	int	filter_nextpt;	/* index into filter shift register */
35140b8e41roberto	double	filter_delay[NTP_SHIFT]; /* delay shift register */
35240b8e41roberto	double	filter_offset[NTP_SHIFT]; /* offset shift register */
35340b8e41roberto	double	filter_disp[NTP_SHIFT]; /* dispersion shift register */
35440b8e41roberto	u_long	filter_epoch[NTP_SHIFT]; /* epoch shift register */
35540b8e41roberto	u_char	filter_order[NTP_SHIFT]; /* filter sort index */
35640b8e41roberto	l_fp	rec;		/* receive time stamp */
35740b8e41roberto	l_fp	xmt;		/* transmit time stamp */
358d54cfbdroberto	l_fp	dst;		/* destination timestamp */
359d54cfbdroberto	l_fp	aorg;		/* origin timestamp */
360d54cfbdroberto	l_fp	borg;		/* alternate origin timestamp */
361a835b8fdelphij	l_fp	bxmt;		/* most recent broadcast transmit timestamp */
362837c91fcy	l_fp	nonce;		/* Value of nonce we sent as the xmt stamp */
36340b8e41roberto	double	offset;		/* peer clock offset */
36440b8e41roberto	double	delay;		/* peer roundtrip delay */
36540b8e41roberto	double	jitter;		/* peer jitter (squares) */
36640b8e41roberto	double	disp;		/* peer dispersion */
367d54cfbdroberto	double	xleave;		/* interleave delay */
368047f369cy	double	bias;		/* programmed offset bias */
369d54cfbdroberto
370d54cfbdroberto	/*
371d54cfbdroberto	 * Variables used to correct for packet length and asymmetry.
372d54cfbdroberto	 */
373d54cfbdroberto	double	t21;		/* outbound packet delay */
374d54cfbdroberto	int	t21_bytes;	/* outbound packet length */
375d54cfbdroberto	int	t21_last;	/* last outbound packet length */
376d54cfbdroberto	double	r21;		/* outbound data rate */
377d54cfbdroberto	double	t34;		/* inbound packet delay */
378d54cfbdroberto	int	t34_bytes;	/* inbound packet length */
379d54cfbdroberto	double	r34;		/* inbound data rate */
38040b8e41roberto
38140b8e41roberto	/*
38240b8e41roberto	 * End of clear-to-zero area
38340b8e41roberto	 */
38440b8e41roberto	u_long	update;		/* receive epoch */
385d54cfbdroberto#define end_clear_to_zero update
386d54cfbdroberto	int	unreach;	/* watchdog counter */
387d54cfbdroberto	int	throttle;	/* rate control */
38840b8e41roberto	u_long	outdate;	/* send time last packet */
38940b8e41roberto	u_long	nextdate;	/* send time next packet */
3907a6072eroberto
391ef64b99roberto	/*
39240b8e41roberto	 * Statistic counters
393ef64b99roberto	 */
39440b8e41roberto	u_long	timereset;	/* time stat counters were reset */
3953565d4bdelphij	u_long	timelastrec;	/* last packet received time, incl. trash */
396a835b8fdelphij	u_long	timereceived;	/* last (clean) packet received time */
39740b8e41roberto	u_long	timereachable;	/* last reachable/unreachable time */
39840b8e41roberto
39940b8e41roberto	u_long	sent;		/* packets sent */
40040b8e41roberto	u_long	received;	/* packets received */
401d54cfbdroberto	u_long	processed;	/* packets processed */
402d54cfbdroberto	u_long	badauth;	/* bad authentication (TEST5) */
4037598092delphij	u_long	badNAK;		/* invalid crypto-NAK */
404d54cfbdroberto	u_long	bogusorg;	/* bogus origin (TEST2, TEST3) */
405d54cfbdroberto	u_long	oldpkt;		/* old duplicate (TEST1) */
406d54cfbdroberto	u_long	seldisptoolarge; /* bad header (TEST6, TEST7) */
407d54cfbdroberto	u_long	selbroken;	/* KoD received */
408ef64b99roberto};
409ef64b99roberto
410ef64b99roberto/*
411ef64b99roberto * Values for peer.leap, sys_leap
412ef64b99roberto */
413ef64b99roberto#define	LEAP_NOWARNING	0x0	/* normal, no leap second warning */
414ef64b99roberto#define	LEAP_ADDSECOND	0x1	/* last minute of day has 61 seconds */
415ef64b99roberto#define	LEAP_DELSECOND	0x2	/* last minute of day has 59 seconds */
416ef64b99roberto#define	LEAP_NOTINSYNC	0x3	/* overload, clock is free running */
417ef64b99roberto
418ef64b99roberto/*
4197a6072eroberto * Values for peer mode and packet mode. Only the modes through
4207a6072eroberto * MODE_BROADCAST and MODE_BCLIENT appear in the transition
4217a6072eroberto * function. MODE_CONTROL and MODE_PRIVATE can appear in packets,
4227a6072eroberto * but those never survive to the transition function.
4233565d4bdelphij */
424118e757roberto#define	MODE_UNSPEC	0	/* unspecified (old version) */
4257a6072eroberto#define	MODE_ACTIVE	1	/* symmetric active mode */
4267a6072eroberto#define	MODE_PASSIVE	2	/* symmetric passive mode */
427ef64b99roberto#define	MODE_CLIENT	3	/* client mode */
428ef64b99roberto#define	MODE_SERVER	4	/* server mode */
429ef64b99roberto#define	MODE_BROADCAST	5	/* broadcast mode */
4307a6072eroberto/*
4317a6072eroberto * These can appear in packets
4327a6072eroberto */
4337a6072eroberto#define	MODE_CONTROL	6	/* control mode */
4347a6072eroberto#define	MODE_PRIVATE	7	/* private mode */
4357a6072eroberto/*
4363565d4bdelphij * This is a made-up mode for broadcast client.
4377a6072eroberto */
4387a6072eroberto#define	MODE_BCLIENT	6	/* broadcast client mode */
439ef64b99roberto
440ef64b99roberto/*
441ef64b99roberto * Values for peer.stratum, sys_stratum
442ef64b99roberto */
443118e757roberto#define	STRATUM_REFCLOCK ((u_char)0) /* default stratum */
444ef64b99roberto/* A stratum of 0 in the packet is mapped to 16 internally */
445ef64b99roberto#define	STRATUM_PKT_UNSPEC ((u_char)0) /* unspecified in packet */
44640b8e41roberto#define	STRATUM_UNSPEC	((u_char)16) /* unspecified */
447ef64b99roberto
448ef64b99roberto/*
449f63afe2cy * Values for peer.flags (u_int)
450ef64b99roberto */
45140b8e41roberto#define	FLAG_CONFIG	0x0001	/* association was configured */
452d54cfbdroberto#define	FLAG_PREEMPT	0x0002	/* preemptable association */
45340b8e41roberto#define	FLAG_AUTHENTIC	0x0004	/* last message was authentic */
454d54cfbdroberto#define	FLAG_REFCLOCK	0x0008	/* this is actually a reference clock */
455d54cfbdroberto#define	FLAG_BC_VOL	0x0010	/* broadcast client volleying */
456d54cfbdroberto#define	FLAG_PREFER	0x0020	/* prefer peer */
457d54cfbdroberto#define	FLAG_BURST	0x0040	/* burst mode */
458d54cfbdroberto#define	FLAG_PPS	0x0080	/* steered by PPS */
459d54cfbdroberto#define	FLAG_IBURST	0x0100	/* initial burst mode */
460d54cfbdroberto#define	FLAG_NOSELECT	0x0200	/* never select */
461d54cfbdroberto#define	FLAG_TRUE	0x0400	/* force truechimer */
462d54cfbdroberto#define	FLAG_SKEY	0x0800  /* autokey authentication */
463d54cfbdroberto#define	FLAG_XLEAVE	0x1000	/* interleaved protocol */
464d54cfbdroberto#define	FLAG_XB		0x2000	/* interleaved broadcast */
465d54cfbdroberto#define	FLAG_XBOGUS	0x4000	/* interleaved bogus packet */
466d54cfbdroberto#ifdef	OPENSSL
467f63afe2cy# define FLAG_ASSOC	0x8000	/* autokey request */
468d54cfbdroberto#endif /* OPENSSL */
469f63afe2cy#define FLAG_TSTAMP_PPS	0x10000	/* PPS source provides absolute timestamp */
470837c91fcy#define FLAG_LOOPNONCE	0x20000	/* Use a nonce for the loopback test */
471ef64b99roberto
472ef64b99roberto/*
473ef64b99roberto * Definitions for the clear() routine.  We use memset() to clear
474ef64b99roberto * the parts of the peer structure which go to zero.  These are
475ef64b99roberto * used to calculate the start address and length of the area.
476ef64b99roberto */
477ef64b99roberto#define	CLEAR_TO_ZERO(p)	((char *)&((p)->clear_to_zero))
478ef64b99roberto#define	END_CLEAR_TO_ZERO(p)	((char *)&((p)->end_clear_to_zero))
479047f369cy#define	LEN_CLEAR_TO_ZERO(p)	(END_CLEAR_TO_ZERO(p) - CLEAR_TO_ZERO(p))
480118e757roberto#define CRYPTO_TO_ZERO(p)	((char *)&((p)->clear_to_zero))
481118e757roberto#define END_CRYPTO_TO_ZERO(p)	((char *)&((p)->end_clear_to_zero))
482118e757roberto#define LEN_CRYPTO_TO_ZERO	(END_CRYPTO_TO_ZERO((struct peer *)0) \
48340b8e41roberto				    - CRYPTO_TO_ZERO((struct peer *)0))
48440b8e41roberto
485ef64b99roberto/*
486ef64b99roberto * Reference clock types.  Added as necessary.
487ef64b99roberto */
488ef64b99roberto#define	REFCLK_NONE		0	/* unknown or missing */
489ef64b99roberto#define	REFCLK_LOCALCLOCK	1	/* external (e.g., lockclock) */
490ef64b99roberto#define	REFCLK_GPS_TRAK		2	/* TRAK 8810 GPS Receiver */
491ef64b99roberto#define	REFCLK_WWV_PST		3	/* PST/Traconex 1020 WWV/H */
492b5b40f9roberto#define	REFCLK_SPECTRACOM	4	/* Spectracom (generic) Receivers */
493ef64b99roberto#define	REFCLK_TRUETIME		5	/* TrueTime (generic) Receivers */
494118e757roberto#define REFCLK_IRIG_AUDIO	6	/* IRIG-B/W audio decoder */
495b5b40f9roberto#define	REFCLK_CHU_AUDIO	7	/* CHU audio demodulator/decoder */
496ef64b99roberto#define REFCLK_PARSE		8	/* generic driver (usually DCF77,GPS,MSF) */
497ef64b99roberto#define	REFCLK_GPS_MX4200	9	/* Magnavox MX4200 GPS */
498ef64b99roberto#define REFCLK_GPS_AS2201	10	/* Austron 2201A GPS */
499ef64b99roberto#define	REFCLK_GPS_ARBITER	11	/* Arbiter 1088A/B/ GPS */
500ef64b99roberto#define REFCLK_IRIG_TPRO	12	/* KSI/Odetics TPRO-S IRIG */
501ef64b99roberto#define REFCLK_ATOM_LEITCH	13	/* Leitch CSD 5300 Master Clock */
502ef64b99roberto#define REFCLK_MSF_EES		14	/* EES M201 MSF Receiver */
503ef64b99roberto#define	REFCLK_GPSTM_TRUE	15	/* OLD TrueTime GPS/TM-TMD Receiver */
504ef64b99roberto#define REFCLK_IRIG_BANCOMM	16	/* Bancomm GPS/IRIG Interface */
505ef64b99roberto#define REFCLK_GPS_DATUM	17	/* Datum Programmable Time System */
5067a6072eroberto#define REFCLK_ACTS		18	/* Generic Auto Computer Time Service */
507ef64b99roberto#define REFCLK_WWV_HEATH	19	/* Heath GC1000 WWV/WWVH Receiver */
508ef64b99roberto#define REFCLK_GPS_NMEA		20	/* NMEA based GPS clock */
509ef64b99roberto#define REFCLK_GPS_VME		21	/* TrueTime GPS-VME Interface */
510ef64b99roberto#define REFCLK_ATOM_PPS		22	/* 1-PPS Clock Discipline */
5117a6072eroberto#define REFCLK_PTB_ACTS		23	/* replaced by REFCLK_ACTS */
5127a6072eroberto#define REFCLK_USNO		24	/* replaced by REFCLK_ACTS */
513ef64b99roberto#define REFCLK_GPS_HP		26	/* HP 58503A Time/Frequency Receiver */
514118e757roberto#define REFCLK_ARCRON_MSF	27	/* ARCRON MSF radio clock. */
515ef64b99roberto#define REFCLK_SHM		28	/* clock attached thru shared memory */
516ef64b99roberto#define REFCLK_PALISADE		29	/* Trimble Navigation Palisade GPS */
517ef64b99roberto#define REFCLK_ONCORE		30	/* Motorola UT Oncore GPS */
518ef64b99roberto#define REFCLK_GPS_JUPITER	31	/* Rockwell Jupiter GPS receiver */
519118e757roberto#define REFCLK_CHRONOLOG	32	/* Chrono-log K WWVB receiver */
520118e757roberto#define REFCLK_DUMBCLOCK	33	/* Dumb localtime clock */
521118e757roberto#define REFCLK_ULINK		34	/* Ultralink M320 WWVB receiver */
522b5b40f9roberto#define REFCLK_PCF		35	/* Conrad parallel port radio clock */
523b5b40f9roberto#define REFCLK_WWV_AUDIO	36	/* WWV/H audio demodulator/decoder */
524b5b40f9roberto#define REFCLK_FG		37	/* Forum Graphic GPS */
525118e757roberto#define REFCLK_HOPF_SERIAL	38	/* hopf DCF77/GPS serial receiver  */
52640b8e41roberto#define REFCLK_HOPF_PCI		39	/* hopf DCF77/GPS PCI receiver  */
527f771469roberto#define REFCLK_JJY		40	/* JJY receiver  */
528a85d9aeroberto#define	REFCLK_TT560		41	/* TrueTime 560 IRIG-B decoder */
529a85d9aeroberto#define REFCLK_ZYFER		42	/* Zyfer GPStarplus receiver  */
530a85d9aeroberto#define REFCLK_RIPENCC		43	/* RIPE NCC Trimble driver */
531118e757roberto#define REFCLK_NEOCLOCK4X	44	/* NeoClock4X DCF77 or TDF receiver */
532047f369cy#define REFCLK_TSYNCPCI		45	/* Spectracom TSYNC PCI timing board */
533047f369cy#define REFCLK_GPSDJSON		46
534047f369cy#define REFCLK_MAX		46
535118e757roberto
536118e757roberto
537ef64b99roberto/*
538ef64b99roberto * NTP packet format.  The mac field is optional.  It isn't really
539ef64b99roberto * an l_fp either, but for now declaring it that way is convenient.
540ef64b99roberto * See Appendix A in the specification.
541ef64b99roberto *
542ef64b99roberto * Note that all u_fp and l_fp values arrive in network byte order
543ef64b99roberto * and must be converted (except the mac, which isn't, really).
544ef64b99roberto */
545ef64b99robertostruct pkt {
546d54cfbdroberto	u_char	li_vn_mode;	/* peer leap indicator */
54740b8e41roberto	u_char	stratum;	/* peer stratum */
54840b8e41roberto	u_char	ppoll;		/* peer poll interval */
54940b8e41roberto	s_char	precision;	/* peer clock precision */
550d54cfbdroberto	u_fp	rootdelay;	/* roundtrip delay to primary source */
551d54cfbdroberto	u_fp	rootdisp;	/* dispersion to primary source*/
552d54cfbdroberto	u_int32	refid;		/* reference id */
553d54cfbdroberto	l_fp	reftime;	/* last update time */
55440b8e41roberto	l_fp	org;		/* originate time stamp */
55540b8e41roberto	l_fp	rec;		/* receive time stamp */
55640b8e41roberto	l_fp	xmt;		/* transmit time stamp */
55740b8e41roberto
55830f4731delphij#define	MIN_V4_PKT_LEN	(12 * sizeof(u_int32))	/* min header length */
55930f4731delphij#define	LEN_PKT_NOMAC	(12 * sizeof(u_int32))	/* min header length */
56030f4731delphij#define	MIN_MAC_LEN	(1 * sizeof(u_int32))	/* crypto_NAK */
56130f4731delphij#define	MAX_MD5_LEN	(5 * sizeof(u_int32))	/* MD5 */
562d54cfbdroberto#define	MAX_MAC_LEN	(6 * sizeof(u_int32))	/* SHA */
56330f4731delphij#define	KEY_MAC_LEN	sizeof(u_int32)		/* key ID in MAC */
56430f4731delphij#define	MAX_MDG_LEN	(MAX_MAC_LEN-KEY_MAC_LEN) /* max. digest len */
565ef64b99roberto
566ef64b99roberto	/*
567ef64b99roberto	 * The length of the packet less MAC must be a multiple of 64
568d54cfbdroberto	 * with an RSA modulus and Diffie-Hellman prime of 256 octets
56940b8e41roberto	 * and maximum host name of 128 octets, the maximum autokey
57040b8e41roberto	 * command is 152 octets and maximum autokey response is 460
57140b8e41roberto	 * octets. A packet can contain no more than one command and one
572d54cfbdroberto	 * response, so the maximum total extension field length is 864
57340b8e41roberto	 * octets. But, to handle humungus certificates, the bank must
57440b8e41roberto	 * be broke.
575047f369cy	 *
576047f369cy	 * The different definitions of the 'exten' field are here for
577047f369cy	 * the benefit of applications that want to send a packet from
578047f369cy	 * an auto variable in the stack - not using the AUTOKEY version
579047f369cy	 * saves 2KB of stack space. The receive buffer should ALWAYS be
580047f369cy	 * big enough to hold a full extended packet if the extension
581047f369cy	 * fields have to be parsed or skipped.
582ef64b99roberto	 */
583047f369cy#ifdef AUTOKEY
584047f369cy	u_int32	exten[(NTP_MAXEXTEN + MAX_MAC_LEN) / sizeof(u_int32)];
585047f369cy#else	/* !AUTOKEY follows */
586047f369cy	u_int32	exten[(MAX_MAC_LEN) / sizeof(u_int32)];
587047f369cy#endif	/* !AUTOKEY */
588ef64b99roberto};
589ef64b99roberto
590ef64b99roberto/*
591ef64b99roberto * Stuff for extracting things from li_vn_mode
592ef64b99roberto */
593ef64b99roberto#define	PKT_MODE(li_vn_mode)	((u_char)((li_vn_mode) & 0x7))
594ef64b99roberto#define	PKT_VERSION(li_vn_mode)	((u_char)(((li_vn_mode) >> 3) & 0x7))
595ef64b99roberto#define	PKT_LEAP(li_vn_mode)	((u_char)(((li_vn_mode) >> 6) & 0x3))
596ef64b99roberto
597ef64b99roberto/*
598047f369cy * Stuff for putting things back into li_vn_mode in packets and vn_mode
599047f369cy * in ntp_monitor.c's mon_entry.
600ef64b99roberto */
601047f369cy#define VN_MODE(v, m)		((((v) & 7) << 3) | ((m) & 0x7))
602047f369cy#define	PKT_LI_VN_MODE(l, v, m) ((((l) & 3) << 6) | VN_MODE((v), (m)))
603ef64b99roberto
604ef64b99roberto
605ef64b99roberto/*
606ef64b99roberto * Dealing with stratum.  0 gets mapped to 16 incoming, and back to 0
607ef64b99roberto * on output.
608ef64b99roberto */
609ef64b99roberto#define	PKT_TO_STRATUM(s)	((u_char)(((s) == (STRATUM_PKT_UNSPEC)) ?\
610ef64b99roberto				(STRATUM_UNSPEC) : (s)))
611ef64b99roberto
612ef64b99roberto#define	STRATUM_TO_PKT(s)	((u_char)(((s) == (STRATUM_UNSPEC)) ?\
613ef64b99roberto				(STRATUM_PKT_UNSPEC) : (s)))
614ef64b99roberto
61575715b3delphij
61675715b3delphij/*
61775715b3delphij * A test to determine if the refid should be interpreted as text string.
61875715b3delphij * This is usually the case for a refclock, which has stratum 0 internally,
61975715b3delphij * which results in sys_stratum 1 if the refclock becomes system peer, or
62075715b3delphij * in case of a kiss-of-death (KoD) packet that has STRATUM_PKT_UNSPEC (==0)
62175715b3delphij * in the packet which is converted to STRATUM_UNSPEC when the packet
62275715b3delphij * is evaluated.
62375715b3delphij */
62475715b3delphij#define REFID_ISTEXT(s) (((s) <= 1) || ((s) >= STRATUM_UNSPEC))
62575715b3delphij
62675715b3delphij
627ef64b99roberto/*
62840b8e41roberto * Event codes. Used for reporting errors/events to the control module
629ef64b99roberto */
630118e757roberto#define	PEER_EVENT	0x080	/* this is a peer event */
631118e757roberto#define CRPT_EVENT	0x100	/* this is a crypto event */
632ef64b99roberto
63340b8e41roberto/*
63440b8e41roberto * System event codes
63540b8e41roberto */
63640b8e41roberto#define	EVNT_UNSPEC	0	/* unspecified */
637d54cfbdroberto#define	EVNT_NSET	1	/* freq not set */
638d54cfbdroberto#define	EVNT_FSET	2	/* freq set */
639d54cfbdroberto#define	EVNT_SPIK	3	/* spike detect */
640d54cfbdroberto#define	EVNT_FREQ	4	/* freq mode */
641d54cfbdroberto#define	EVNT_SYNC	5	/* clock sync */
642d54cfbdroberto#define	EVNT_SYSRESTART	6	/* restart */
643d54cfbdroberto#define	EVNT_SYSFAULT	7	/* panic stop */
644d54cfbdroberto#define	EVNT_NOPEER	8	/* no sys peer */
645d54cfbdroberto#define	EVNT_ARMED	9	/* leap armed */
646d54cfbdroberto#define	EVNT_DISARMED	10	/* leap disarmed */
647d54cfbdroberto#define	EVNT_LEAP	11	/* leap event */
648d54cfbdroberto#define	EVNT_CLOCKRESET	12	/* clock step */
649d54cfbdroberto#define	EVNT_KERN	13	/* kernel event */
650d54cfbdroberto#define	EVNT_TAI	14	/* TAI */
651d54cfbdroberto#define	EVNT_LEAPVAL	15	/* stale leapsecond values */
652ef64b99roberto
65340b8e41roberto/*
65440b8e41roberto * Peer event codes
65540b8e41roberto */
656d54cfbdroberto#define	PEVNT_MOBIL	(1 | PEER_EVENT) /* mobilize */
657d54cfbdroberto#define	PEVNT_DEMOBIL	(2 | PEER_EVENT) /* demobilize */
658d54cfbdroberto#define	PEVNT_UNREACH	(3 | PEER_EVENT) /* unreachable */
659d54cfbdroberto#define	PEVNT_REACH	(4 | PEER_EVENT) /* reachable */
660d54cfbdroberto#define	PEVNT_RESTART	(5 | PEER_EVENT) /* restart */
661d54cfbdroberto#define	PEVNT_REPLY	(6 | PEER_EVENT) /* no reply */
662d54cfbdroberto#define	PEVNT_RATE	(7 | PEER_EVENT) /* rate exceeded */
663d54cfbdroberto#define	PEVNT_DENY	(8 | PEER_EVENT) /* access denied */
664d54cfbdroberto#define PEVNT_ARMED	(9 | PEER_EVENT) /* leap armed */
665d54cfbdroberto#define	PEVNT_NEWPEER	(10 | PEER_EVENT) /* sys peer */
666d54cfbdroberto#define	PEVNT_CLOCK	(11 | PEER_EVENT) /* clock event */
667d54cfbdroberto#define	PEVNT_AUTH	(12 | PEER_EVENT) /* bad auth */
668d54cfbdroberto#define	PEVNT_POPCORN	(13 | PEER_EVENT) /* popcorn */
669d54cfbdroberto#define	PEVNT_XLEAVE	(14 | PEER_EVENT) /* interleave mode */
670d54cfbdroberto#define	PEVNT_XERR	(15 | PEER_EVENT) /* interleave error */
671ef64b99roberto
672ef64b99roberto/*
673ef64b99roberto * Clock event codes
674ef64b99roberto */
67540b8e41roberto#define	CEVNT_NOMINAL	0	/* unspecified */
676d54cfbdroberto#define	CEVNT_TIMEOUT	1	/* no reply */
677d54cfbdroberto#define	CEVNT_BADREPLY	2	/* bad format */
678d54cfbdroberto#define	CEVNT_FAULT	3	/* fault */
679d54cfbdroberto#define	CEVNT_PROP	4	/* bad signal */
680d54cfbdroberto#define	CEVNT_BADDATE	5	/* bad date */
681d54cfbdroberto#define	CEVNT_BADTIME	6	/* bad time */
682ef64b99roberto#define CEVNT_MAX	CEVNT_BADTIME
683ef64b99roberto
684ef64b99roberto/*
685ef64b99roberto * Very misplaced value.  Default port through which we send traps.
686ef64b99roberto */
687ef64b99roberto#define	TRAPPORT	18447
688ef64b99roberto
689ef64b99roberto
690ef64b99roberto/*
69140b8e41roberto * To speed lookups, peers are hashed by the low order bits of the
69240b8e41roberto * remote IP address. These definitions relate to that.
693ef64b99roberto */
694047f369cy#define	NTP_HASH_SIZE		128
695047f369cy#define	NTP_HASH_MASK		(NTP_HASH_SIZE-1)
696047f369cy#define	NTP_HASH_ADDR(src)	(sock_hash(src) & NTP_HASH_MASK)
697ef64b99roberto
698ef64b99roberto/*
699ef64b99roberto * min, min3 and max.  Makes it easier to transliterate the spec without
700ef64b99roberto * thinking about it.
701ef64b99roberto */
702ef64b99roberto#define	min(a,b)	(((a) < (b)) ? (a) : (b))
703ef64b99roberto#define	max(a,b)	(((a) > (b)) ? (a) : (b))
704ef64b99roberto#define	min3(a,b,c)	min(min((a),(b)), (c))
705ef64b99roberto
706ef64b99roberto
707ef64b99roberto/*
708ef64b99roberto * Configuration items.  These are for the protocol module (proto_config())
709ef64b99roberto */
710ef64b99roberto#define	PROTO_BROADCLIENT	1
711ef64b99roberto#define	PROTO_PRECISION		2	/* (not used) */
712ef64b99roberto#define	PROTO_AUTHENTICATE	3
713ef64b99roberto#define	PROTO_BROADDELAY	4
714ef64b99roberto#define	PROTO_AUTHDELAY		5	/* (not used) */
715ef64b99roberto#define PROTO_MULTICAST_ADD	6
716ef64b99roberto#define PROTO_MULTICAST_DEL	7
717ef64b99roberto#define PROTO_NTP		8
718ef64b99roberto#define PROTO_KERNEL		9
719ef64b99roberto#define PROTO_MONITOR		10
720ef64b99roberto#define PROTO_FILEGEN		11
72140b8e41roberto#define	PROTO_PPS		12
72240b8e41roberto#define PROTO_CAL		13
723118e757roberto#define PROTO_MINCLOCK		14
7247a6072eroberto#define	PROTO_MAXCLOCK		15
7257a6072eroberto#define PROTO_MINSANE		16
7267a6072eroberto#define PROTO_FLOOR		17
7277a6072eroberto#define PROTO_CEILING		18
7287a6072eroberto#define PROTO_COHORT		19
7297a6072eroberto#define PROTO_CALLDELAY		20
7307a6072eroberto#define PROTO_MINDISP		21
7317a6072eroberto#define PROTO_MAXDIST		22
732047f369cy	/* available		23 */
7337a6072eroberto#define	PROTO_MAXHOP		24
7347a6072eroberto#define	PROTO_BEACON		25
7357a6072eroberto#define	PROTO_ORPHAN		26
736047f369cy#define	PROTO_ORPHWAIT		27
737047f369cy#define	PROTO_MODE7		28
738a835b8fdelphij#define	PROTO_UECRYPTO		29
739a835b8fdelphij#define	PROTO_UECRYPTONAK	30
740a835b8fdelphij#define	PROTO_UEDIGEST		31
7415a3cfd0delphij#define	PROTO_PCEDIGEST		32
7423565d4bdelphij#define	PROTO_BCPOLLBSTEP	33
743ef64b99roberto
744ef64b99roberto/*
745ef64b99roberto * Configuration items for the loop filter
746ef64b99roberto */
747047f369cy#define	LOOP_DRIFTINIT		1	/* iniitialize frequency */
748047f369cy#define	LOOP_KERN_CLEAR		2	/* set initial frequency offset */
749f63afe2cy#define LOOP_MAX		3	/* set both step offsets */
7503565d4bdelphij#define LOOP_MAX_BACK		4	/* set backward-step offset */
751f63afe2cy#define LOOP_MAX_FWD		5	/* set forward-step offset */
752f63afe2cy#define LOOP_PANIC		6	/* set panic offseet */
753f63afe2cy#define LOOP_PHI		7	/* set dispersion rate */
754f63afe2cy#define LOOP_MINSTEP		8	/* set step timeout */
755f63afe2cy#define LOOP_MINPOLL		9	/* set min poll interval (log2 s) */
756f63afe2cy#define LOOP_ALLAN		10	/* set minimum Allan intercept */
757f63afe2cy#define LOOP_HUFFPUFF		11	/* set huff-n'-puff filter length */
758f63afe2cy#define LOOP_FREQ		12	/* set initial frequency */
759f63afe2cy#define LOOP_CODEC		13	/* set audio codec frequency */
760f63afe2cy#define	LOOP_LEAP		14	/* insert leap after second 23:59 */
761f63afe2cy#define	LOOP_TICK		15	/* sim. low precision clock */
762ef64b99roberto
763ef64b99roberto/*
764ef64b99roberto * Configuration items for the stats printer
765ef64b99roberto */
766ef64b99roberto#define	STATS_FREQ_FILE		1	/* configure drift file */
767ef64b99roberto#define STATS_STATSDIR		2	/* directory prefix for stats files */
768ef64b99roberto#define	STATS_PID_FILE		3	/* configure ntpd PID file */
769d54cfbdroberto#define	STATS_LEAP_FILE		4	/* configure ntpd leapseconds file */
770ef64b99roberto
771118e757roberto#define MJD_1900		15020	/* MJD for 1 Jan 1900 */
772ef64b99roberto
773ef64b99roberto/*
774ef64b99roberto * Default parameters.  We use these in the absence of something better.
775ef64b99roberto */
776ef64b99roberto#define INADDR_NTP	0xe0000101	/* NTP multicast address 224.0.1.1 */
77740b8e41roberto
778ef64b99roberto/*
779ef64b99roberto * Structure used optionally for monitoring when this is turned on.
780ef64b99roberto */
781047f369cytypedef struct mon_data	mon_entry;
782ef64b99robertostruct mon_data {
783047f369cy	mon_entry *	hash_next;	/* next structure in hash list */
784047f369cy	DECL_DLIST_LINK(mon_entry, mru);/* MRU list link pointers */
785047f369cy	struct interface * lcladr;	/* address on which this arrived */
786047f369cy	l_fp		first;		/* first time seen */
787047f369cy	l_fp		last;		/* last time seen */
788047f369cy	int		leak;		/* leaky bucket accumulator */
789047f369cy	int		count;		/* total packet count */
790047f369cy	u_short		flags;		/* restrict flags */
791047f369cy	u_char		vn_mode;	/* packet mode & version */
792047f369cy	u_char		cast_flags;	/* flags MDF_?CAST */
793047f369cy	sockaddr_u	rmtadr;		/* address of remote host */
794ef64b99roberto};
795ef64b99roberto
79640b8e41roberto/*
797047f369cy * Values for cast_flags in mon_entry and struct peer.  mon_entry uses
798047f369cy * only the first three, MDF_UCAST, MDF_MCAST, and MDF_BCAST.
79940b8e41roberto */
800047f369cy#define	MDF_UCAST	0x01	/* unicast client */
801047f369cy#define	MDF_MCAST	0x02	/* multicast server */
802047f369cy#define	MDF_BCAST	0x04	/* broadcast server */
803047f369cy#define	MDF_POOL	0x08	/* pool client solicitor */
804047f369cy#define MDF_ACAST	0x10	/* manycast client solicitor */
805047f369cy#define	MDF_BCLNT	0x20	/* eph. broadcast/multicast client */
806047f369cy#define MDF_UCLNT	0x40	/* preemptible manycast or pool client */
807047f369cy/*
808047f369cy * In the context of struct peer in ntpd, three of the cast_flags bits
809047f369cy * represent configured associations which never receive packets, and
810047f369cy * whose reach is always 0: MDF_BCAST, MDF_MCAST, and MDF_ACAST.  The
811047f369cy * last can be argued as responses are received, but those responses do
812047f369cy * not affect the MDF_ACAST association's reach register, rather they
813047f369cy * (may) result in mobilizing ephemeral MDF_ACLNT associations.
814047f369cy */
815047f369cy#define MDF_TXONLY_MASK	(MDF_BCAST | MDF_MCAST | MDF_ACAST | MDF_POOL)
816047f369cy/*
817047f369cy * manycastclient-like solicitor association cast_flags bits
818047f369cy */
819047f369cy#define MDF_SOLICIT_MASK	(MDF_ACAST | MDF_POOL)
820ef64b99roberto/*
821ef64b99roberto * Values used with mon_enabled to indicate reason for enabling monitoring
822ef64b99roberto */
823047f369cy#define MON_OFF		0x00		/* no monitoring */
824047f369cy#define MON_ON		0x01		/* monitoring explicitly enabled */
825047f369cy#define MON_RES		0x02		/* implicit monitoring for RES_LIMITED */
826ef64b99roberto/*
827ef64b99roberto * Structure used for restrictlist entries
828ef64b99roberto */
829d54cfbdrobertotypedef struct res_addr4_tag {
830d54cfbdroberto	u_int32		addr;		/* IPv4 addr (host order) */
831d54cfbdroberto	u_int32		mask;		/* IPv4 mask (host order) */
832d54cfbdroberto} res_addr4;
833d54cfbdroberto
834d54cfbdrobertotypedef struct res_addr6_tag {
835d54cfbdroberto	struct in6_addr addr;		/* IPv6 addr (net order) */
836d54cfbdroberto	struct in6_addr mask;		/* IPv6 mask (net order) */
837d54cfbdroberto} res_addr6;
838d54cfbdroberto
839d54cfbdrobertotypedef struct restrict_u_tag	restrict_u;
840d54cfbdrobertostruct restrict_u_tag {
84130f4731delphij	restrict_u *	link;		/* link to next entry */
84230f4731delphij	u_int32		count;		/* number of packets matched */
84330f4731delphij	u_short		rflags;		/* restrict (accesslist) flags */
84430f4731delphij	u_short		mflags;		/* match flags */
84530f4731delphij	short		ippeerlimit;	/* IP peer limit */
846837c91fcy	int		srvfuzrftpoll;	/* server response: fuzz reftime */
84730f4731delphij	u_long		expire;		/* valid until time */
848d54cfbdroberto	union {				/* variant starting here */
849d54cfbdroberto		res_addr4 v4;
850d54cfbdroberto		res_addr6 v6;
851d54cfbdroberto	} u;
852ef64b99roberto};
853d54cfbdroberto#define	V4_SIZEOF_RESTRICT_U	(offsetof(restrict_u, u)	\
854d54cfbdroberto				 + sizeof(res_addr4))
855d54cfbdroberto#define	V6_SIZEOF_RESTRICT_U	(offsetof(restrict_u, u)	\
856d54cfbdroberto				 + sizeof(res_addr6))
857ef64b99roberto
85830f4731delphijtypedef struct r4addr_tag	r4addr;
85930f4731delphijstruct r4addr_tag {
86030f4731delphij	u_short		rflags;		/* match flags */
86130f4731delphij	short		ippeerlimit;	/* IP peer limit */
86230f4731delphij};
86330f4731delphij
86430f4731delphijchar *build_iflags(u_int32 flags);
86530f4731delphijchar *build_mflags(u_short mflags);
86630f4731delphijchar *build_rflags(u_short rflags);
86730f4731delphij
868ef64b99roberto/*
86930f4731delphij * Restrict (Access) flags (rflags)
870ef64b99roberto */
871d54cfbdroberto#define	RES_IGNORE		0x0001	/* ignore packet */
872d54cfbdroberto#define	RES_DONTSERVE		0x0002	/* access denied */
873d54cfbdroberto#define	RES_DONTTRUST		0x0004	/* authentication required */
874d54cfbdroberto#define	RES_VERSION		0x0008	/* version mismatch */
875d54cfbdroberto#define	RES_NOPEER		0x0010	/* new association denied */
87630f4731delphij#define	RES_NOEPEER		0x0020	/* new ephemeral association denied */
87730f4731delphij#define RES_LIMITED		0x0040	/* packet rate exceeded */
878118e757roberto#define RES_FLAGS		(RES_IGNORE | RES_DONTSERVE |\
879118e757roberto				    RES_DONTTRUST | RES_VERSION |\
88030f4731delphij				    RES_NOPEER | RES_NOEPEER | RES_LIMITED)
881ef64b99roberto
88230f4731delphij#define	RES_NOQUERY		0x0080	/* mode 6/7 packet denied */
88330f4731delphij#define	RES_NOMODIFY		0x0100	/* mode 6/7 modify denied */
88430f4731delphij#define	RES_NOTRAP		0x0200	/* mode 6/7 set trap denied */
88530f4731delphij#define	RES_LPTRAP		0x0400	/* mode 6/7 low priority trap */
886118e757roberto
88730f4731delphij#define	RES_KOD			0x0800	/* send kiss of death packet */
88830f4731delphij#define	RES_MSSNTP		0x1000	/* enable MS-SNTP authentication */
88930f4731delphij#define	RES_FLAKE		0x2000	/* flakeway - drop 10% */
89030f4731delphij#define	RES_NOMRULIST		0x4000	/* mode 6 mrulist denied */
891837c91fcy
892837c91fcy#define	RES_SRVRSPFUZ		0x8000	/* Server response: fuzz */
893837c91fcy
894837c91fcy#define RES_UNUSED		0x0000	/* Unused flag bits (none left) */
895118e757roberto
896047f369cy#define	RES_ALLFLAGS		(RES_FLAGS | RES_NOQUERY |	\
897047f369cy				 RES_NOMODIFY | RES_NOTRAP |	\
898047f369cy				 RES_LPTRAP | RES_KOD |		\
899047f369cy				 RES_MSSNTP | RES_FLAKE |	\
900837c91fcy				 RES_NOMRULIST | RES_SRVRSPFUZ )
901ef64b99roberto
902ef64b99roberto/*
90330f4731delphij * Match flags (mflags)
904ef64b99roberto */
905d54cfbdroberto#define	RESM_INTERFACE		0x1000	/* this is an interface */
906047f369cy#define	RESM_NTPONLY		0x2000	/* match source port 123 */
907047f369cy#define RESM_SOURCE		0x4000	/* from "restrict source" */
908ef64b99roberto
909ef64b99roberto/*
910ef64b99roberto * Restriction configuration ops
911ef64b99roberto */
91230f4731delphijtypedef enum
91330f4731delphijrestrict_ops {
91430f4731delphij	RESTRICT_FLAGS = 1,	/* add rflags to restrict entry */
91530f4731delphij	RESTRICT_UNFLAG,	/* remove rflags from restrict entry */
91630f4731delphij	RESTRICT_REMOVE,	/* remove a restrict entry */
91730f4731delphij	RESTRICT_REMOVEIF,	/* remove an interface restrict entry */
91830f4731delphij} restrict_op;
919ef64b99roberto
920ef64b99roberto/*
921ef64b99roberto * Endpoint structure for the select algorithm
922ef64b99roberto */
923ef64b99robertostruct endpoint {
924ef64b99roberto	double	val;			/* offset of endpoint */
925ef64b99roberto	int	type;			/* interval entry/exit */
926ef64b99roberto};
927ef64b99roberto
928ef64b99roberto/*
929ef64b99roberto * Association matching AM[] return codes
930ef64b99roberto */
9317a6072eroberto#define AM_ERR		-1		/* error */
9327a6072eroberto#define AM_NOMATCH	0		/* no match */
933f63afe2cy#define AM_PROCPKT	1		/* server/symmetric packet */
934f63afe2cy#define AM_BCST		2		/* broadcast packet */
9357a6072eroberto#define AM_FXMIT	3		/* client packet */
936047f369cy#define AM_MANYCAST	4		/* manycast or pool */
9377a6072eroberto#define AM_NEWPASS	5		/* new passive */
9387a6072eroberto#define AM_NEWBCL	6		/* new broadcast */
939f63afe2cy#define AM_POSSBCL	7		/* discard broadcast */
940ef64b99roberto
941ef64b99roberto/* NetInfo configuration locations */
942ef64b99roberto#ifdef HAVE_NETINFO
943ef64b99roberto#define NETINFO_CONFIG_DIR "/config/ntp"
944ef64b99roberto#endif
945ef64b99roberto
946047f369cy/* ntpq -c mrulist rows per request limit in ntpd */
947047f369cy#define MRU_ROW_LIMIT	256
948047f369cy/* similar datagrams per response limit for ntpd */
949047f369cy#define MRU_FRAGS_LIMIT	128
950ef64b99roberto#endif /* NTP_H */
951