154359Sroberto/*
254359Sroberto * ntp.h - NTP definitions for the masses
354359Sroberto */
454359Sroberto#ifndef NTP_H
554359Sroberto#define NTP_H
654359Sroberto
7285612Sdelphij#include <stddef.h>
854359Sroberto#include <math.h>
9285612Sdelphij
10285612Sdelphij#include <ntp_fp.h>
11285612Sdelphij#include <ntp_types.h>
12285612Sdelphij#include <ntp_lists.h>
13285612Sdelphij#include <ntp_stdlib.h>
14285612Sdelphij#include <ntp_crypto.h>
15182007Sroberto#include <ntp_random.h>
16285612Sdelphij#include <ntp_net.h>
1754359Sroberto
18182007Sroberto#include <isc/boolean.h>
19182007Sroberto
2082498Sroberto/*
2182498Sroberto * Calendar arithmetic - contributed by G. Healton
2282498Sroberto */
2382498Sroberto#define YEAR_BREAK 500		/* years < this are tm_year values:
2482498Sroberto				 * Break < AnyFourDigitYear && Break >
2582498Sroberto				 * Anytm_yearYear */
2654359Sroberto
2782498Sroberto#define YEAR_PIVOT 98		/* 97/98: years < this are year 2000+
2882498Sroberto				 * FYI: official UNIX pivot year is
2982498Sroberto				 * 68/69 */
3054359Sroberto
3182498Sroberto/*
3282498Sroberto * Number of Days since 1 BC Gregorian to 1 January of given year
3382498Sroberto */
3482498Sroberto#define julian0(year)	(((year) * 365 ) + ((year) > 0 ? (((year) + 3) \
3582498Sroberto			    / 4 - ((year - 1) / 100) + ((year - 1) / \
3682498Sroberto			    400)) : 0))
3754359Sroberto
3882498Sroberto/*
3982498Sroberto * Number of days since start of NTP time to 1 January of given year
4082498Sroberto */
4182498Sroberto#define ntp0(year)	(julian0(year) - julian0(1900))
4254359Sroberto
4382498Sroberto/*
4482498Sroberto * Number of days since start of UNIX time to 1 January of given year
4582498Sroberto */
4682498Sroberto#define unix0(year)	(julian0(year) - julian0(1970))
4754359Sroberto
4882498Sroberto/*
4982498Sroberto * LEAP YEAR test for full 4-digit years (e.g, 1999, 2010)
5082498Sroberto */
5182498Sroberto#define isleap_4(y)	((y) % 4 == 0 && !((y) % 100 == 0 && !(y % \
5282498Sroberto			    400 == 0)))
5354359Sroberto
5482498Sroberto/*
5582498Sroberto * LEAP YEAR test for tm_year (struct tm) years (e.g, 99, 110)
5682498Sroberto */
5782498Sroberto#define isleap_tm(y)	((y) % 4 == 0 && !((y) % 100 == 0 && !(((y) \
5882498Sroberto			    + 1900) % 400 == 0)))
5954359Sroberto
6082498Sroberto/*
6182498Sroberto * to convert simple two-digit years to tm_year style years:
6282498Sroberto *
6382498Sroberto *	if (year < YEAR_PIVOT)
6482498Sroberto *		year += 100;
6582498Sroberto *
6682498Sroberto * to convert either two-digit OR tm_year years to four-digit years:
6782498Sroberto *
6882498Sroberto *	if (year < YEAR_PIVOT)
6982498Sroberto *		year += 100;
7082498Sroberto *
7182498Sroberto *	if (year < YEAR_BREAK)
7282498Sroberto *		year += 1900;
7382498Sroberto */
7454359Sroberto
7554359Sroberto/*
7654359Sroberto * How to get signed characters.  On machines where signed char works,
7782498Sroberto * use it. On machines where signed char doesn't work, char had better
7854359Sroberto * be signed.
7954359Sroberto */
8054359Sroberto#ifdef NEED_S_CHAR_TYPEDEF
8154359Sroberto# if SIZEOF_SIGNED_CHAR
8254359Srobertotypedef signed char s_char;
8354359Sroberto# else
8454359Srobertotypedef char s_char;
8554359Sroberto# endif
8654359Sroberto  /* XXX: Why is this sequent bit INSIDE this test? */
8754359Sroberto# ifdef sequent
8854359Sroberto#  undef SO_RCVBUF
8954359Sroberto#  undef SO_SNDBUF
9054359Sroberto# endif
9154359Sroberto#endif
9254359Sroberto
9354359Sroberto/*
9454359Sroberto * NTP protocol parameters.  See section 3.2.6 of the specification.
9554359Sroberto */
9654359Sroberto#define	NTP_VERSION	((u_char)4) /* current version number */
9754359Sroberto#define	NTP_OLDVERSION	((u_char)1) /* oldest credible version */
98132451Sroberto#define	NTP_PORT	123	/* included for non-unix machines */
99132451Sroberto
100132451Sroberto/*
101132451Sroberto * Poll interval parameters
102132451Sroberto */
103285612Sdelphij#define NTP_UNREACH	10	/* poll unreach threshold */
104285612Sdelphij#define	NTP_MINPOLL	3	/* log2 min poll interval (8 s) */
105132451Sroberto#define NTP_MINDPOLL	6	/* log2 default min poll (64 s) */
106132451Sroberto#define NTP_MAXDPOLL	10	/* log2 default max poll (~17 m) */
107132451Sroberto#define	NTP_MAXPOLL	17	/* log2 max poll interval (~36 h) */
108285612Sdelphij#define	NTP_RETRY	3	/* max packet retries */
109285612Sdelphij#define	NTP_MINPKT	2	/* guard time (s) */
110132451Sroberto
111132451Sroberto/*
112132451Sroberto * Clock filter algorithm tuning parameters
113132451Sroberto */
114132451Sroberto#define MAXDISPERSE	16.	/* max dispersion */
115132451Sroberto#define	NTP_SHIFT	8	/* clock filter stages */
11654359Sroberto#define NTP_FWEIGHT	.5	/* clock filter weight */
117132451Sroberto
118132451Sroberto/*
119132451Sroberto * Selection algorithm tuning parameters
120132451Sroberto */
121182007Sroberto#define	NTP_MINCLOCK	3	/* min survivors */
122182007Sroberto#define	NTP_MAXCLOCK	10	/* max candidates */
123285612Sdelphij#define MINDISPERSE	.001	/* min distance */
124285612Sdelphij#define MAXDISTANCE	1.5	/* max root distance (select threshold) */
125132451Sroberto#define CLOCK_SGATE	3.	/* popcorn spike gate */
12682498Sroberto#define HUFFPUFF	900	/* huff-n'-puff sample interval (s) */
127182007Sroberto#define MAXHOP		2	/* anti-clockhop threshold */
128132451Sroberto#define MAX_TTL		8	/* max ttl mapping vector size */
129182007Sroberto#define	BEACON		7200	/* manycast beacon interval */
130285612Sdelphij#define NTP_MAXEXTEN	2048	/* max extension field size */
131285612Sdelphij#define	NTP_ORPHWAIT	300	/* orphan wair (s) */
13282498Sroberto
13354359Sroberto/*
134132451Sroberto * Miscellaneous stuff
135132451Sroberto */
136182007Sroberto#define NTP_MAXKEY	65535	/* max authentication key number */
137285612Sdelphij#define	KEY_TYPE_MD5	NID_md5	/* MD5 digest NID */
138132451Sroberto/*
139132451Sroberto * Limits of things
140132451Sroberto */
141285612Sdelphij#define	MAXFILENAME	256	/* max length of file name */
142132451Sroberto#define MAXHOSTNAME	512	/* max length of host/node name */
143182007Sroberto#define NTP_MAXSTRLEN	256	/* max string length */
144132451Sroberto
145132451Sroberto/*
14682498Sroberto * Operations for jitter calculations (these use doubles).
14782498Sroberto *
14882498Sroberto * Note that we carefully separate the jitter component from the
14982498Sroberto * dispersion component (frequency error plus precision). The frequency
15082498Sroberto * error component is computed as CLOCK_PHI times the difference between
15182498Sroberto * the epoch of the time measurement and the reference time. The
152285612Sdelphij * precision component is computed as the square root of the mean of the
15382498Sroberto * squares of a zero-mean, uniform distribution of unit maximum
15482498Sroberto * amplitude. Whether this makes statistical sense may be arguable.
15554359Sroberto */
15654359Sroberto#define SQUARE(x) ((x) * (x))
15754359Sroberto#define SQRT(x) (sqrt(x))
15854359Sroberto#define DIFF(x, y) (SQUARE((x) - (y)))
159285612Sdelphij#define LOGTOD(a)	ldexp(1., (int)(a)) /* log2 to double */
16054359Sroberto#define UNIVAR(x)	(SQUARE(.28867513 * LOGTOD(x))) /* std uniform distr */
161285612Sdelphij#define ULOGTOD(a)	ldexp(1., (int)(a)) /* ulog2 to double */
16254359Sroberto
16382498Sroberto#define	EVENT_TIMEOUT	0	/* one second, that is */
16482498Sroberto
165285612Sdelphij
16654359Sroberto/*
16754359Sroberto * The interface structure is used to hold the addresses and socket
168285612Sdelphij * numbers of each of the local network addresses we are using.
169285612Sdelphij * Because "interface" is a reserved word in C++ and has so many
170285612Sdelphij * varied meanings, a change to "endpt" (via typedef) is under way.
171285612Sdelphij * Eventually the struct tag will change from interface to endpt_tag.
172285612Sdelphij * endpt is unrelated to the select algorithm's struct endpoint.
17354359Sroberto */
174285612Sdelphijtypedef struct interface endpt;
17554359Srobertostruct interface {
176285612Sdelphij	endpt *		elink;		/* endpt list link */
177285612Sdelphij	endpt *		mclink;		/* per-AF_* multicast list */
178298770Sdelphij	void *		ioreg_ctx;	/* IO registration context */
179285612Sdelphij	SOCKET		fd;		/* socket descriptor */
180285612Sdelphij	SOCKET		bfd;		/* for receiving broadcasts */
181285612Sdelphij	u_int32		ifnum;		/* endpt instance count */
182285612Sdelphij	sockaddr_u	sin;		/* unicast address */
183285612Sdelphij	sockaddr_u	mask;		/* subnet mask */
184285612Sdelphij	sockaddr_u	bcast;		/* broadcast address */
185285612Sdelphij	char		name[32];	/* name of interface */
186285612Sdelphij	u_short		family;		/* AF_INET/AF_INET6 */
187285612Sdelphij	u_short		phase;		/* phase in update cycle */
188285612Sdelphij	u_int32		flags;		/* interface flags */
189285612Sdelphij	int		last_ttl;	/* last TTL specified */
190285612Sdelphij	u_int32		addr_refid;	/* IPv4 addr or IPv6 hash */
191285612Sdelphij	int		num_mcast;	/* mcast addrs enabled */
192285612Sdelphij	u_long		starttime;	/* current_time at creation */
193285612Sdelphij	volatile long	received;	/* number of incoming packets */
194285612Sdelphij	long		sent;		/* number of outgoing packets */
195285612Sdelphij	long		notsent;	/* number of send failures */
196285612Sdelphij	u_int		ifindex;	/* for IPV6_MULTICAST_IF */
197285612Sdelphij	isc_boolean_t	ignore_packets; /* listen-read-drop this? */
198285612Sdelphij	struct peer *	peers;		/* list of peers using endpt */
199285612Sdelphij	u_int		peercnt;	/* count of same */
20054359Sroberto};
20154359Sroberto
20254359Sroberto/*
20354359Sroberto * Flags for interfaces
20454359Sroberto */
205182007Sroberto#define INT_UP		0x001	/* Interface is up */
206182007Sroberto#define	INT_PPP		0x002	/* Point-to-point interface */
207182007Sroberto#define	INT_LOOPBACK	0x004	/* the loopback interface */
208182007Sroberto#define	INT_BROADCAST	0x008	/* can broadcast out this interface */
209182007Sroberto#define INT_MULTICAST	0x010	/* can multicast out this interface */
210285612Sdelphij#define	INT_BCASTOPEN	0x020	/* broadcast receive socket is open */
211182007Sroberto#define INT_MCASTOPEN	0x040	/* multicasting enabled */
212285612Sdelphij#define INT_WILDCARD	0x080	/* wildcard interface - usually skipped */
213285612Sdelphij#define INT_MCASTIF	0x100	/* bound directly to MCAST address */
214285612Sdelphij#define INT_PRIVACY	0x200	/* RFC 4941 IPv6 privacy address */
215285612Sdelphij#define INT_BCASTXMIT	0x400   /* socket setup to allow broadcasts */
216285612Sdelphij
21754359Sroberto/*
21882498Sroberto * Define flasher bits (tests 1 through 11 in packet procedure)
21954359Sroberto * These reveal the state at the last grumble from the peer and are
22054359Sroberto * most handy for diagnosing problems, even if not strictly a state
22154359Sroberto * variable in the spec. These are recorded in the peer structure.
222182007Sroberto *
223182007Sroberto * Packet errors
22454359Sroberto */
225182007Sroberto#define TEST1		0X0001	/* duplicate packet */
226182007Sroberto#define TEST2		0x0002	/* bogus packet */
22754359Sroberto#define TEST3		0x0004	/* protocol unsynchronized */
22882498Sroberto#define TEST4		0x0008	/* access denied */
229285612Sdelphij#define TEST5		0x0010	/* bad authentication */
230182007Sroberto#define TEST6		0x0020	/* bad synch or stratum */
231285612Sdelphij#define TEST7		0x0040	/* bad header */
232285612Sdelphij#define TEST8		0x0080  /* bad autokey */
233285612Sdelphij#define TEST9		0x0100	/* bad crypto */
234182007Sroberto#define	PKT_TEST_MASK	(TEST1 | TEST2 | TEST3 | TEST4 | TEST5 |\
235182007Sroberto			TEST6 | TEST7 | TEST8 | TEST9)
236182007Sroberto/*
237182007Sroberto * Peer errors
238182007Sroberto */
239182007Sroberto#define TEST10		0x0200	/* peer bad synch or stratum */
240182007Sroberto#define	TEST11		0x0400	/* peer distance exceeded */
241182007Sroberto#define TEST12		0x0800	/* peer synchronization loop */
242182007Sroberto#define TEST13		0x1000	/* peer unreacable */
243182007Sroberto#define	PEER_TEST_MASK	(TEST10 | TEST11 | TEST12 | TEST13)
24454359Sroberto
24554359Sroberto/*
246298770Sdelphij * Unused flags
247298770Sdelphij */
248298770Sdelphij#define TEST14		0x2000
249298770Sdelphij#define TEST15		0x4000
250298770Sdelphij#define TEST16		0x8000
251298770Sdelphij
252298770Sdelphij/*
25382498Sroberto * The peer structure. Holds state information relating to the guys
25482498Sroberto * we are peering with. Most of this stuff is from section 3.2 of the
25554359Sroberto * spec.
25654359Sroberto */
25754359Srobertostruct peer {
258285612Sdelphij	struct peer *p_link;	/* link pointer in free & peer lists */
259285612Sdelphij	struct peer *adr_link;	/* link pointer in address hash */
260285612Sdelphij	struct peer *aid_link;	/* link pointer in associd hash */
261285612Sdelphij	struct peer *ilink;	/* list of peers for interface */
262285612Sdelphij	sockaddr_u srcadr;	/* address of remote host */
263285612Sdelphij	char *	hostname;	/* if non-NULL, remote name */
264285612Sdelphij	struct addrinfo *addrs;	/* hostname query result */
265285612Sdelphij	struct addrinfo *ai;	/* position within addrs */
266285612Sdelphij	endpt *	dstadr;		/* local address */
26782498Sroberto	associd_t associd;	/* association ID */
26882498Sroberto	u_char	version;	/* version number */
26982498Sroberto	u_char	hmode;		/* local association mode */
27082498Sroberto	u_char	hpoll;		/* local poll interval */
27182498Sroberto	u_char	minpoll;	/* min poll interval */
27282498Sroberto	u_char	maxpoll;	/* max poll interval */
27382498Sroberto	u_int	flags;		/* association flags */
27482498Sroberto	u_char	cast_flags;	/* additional flags */
27582498Sroberto	u_char	last_event;	/* last peer error code */
27682498Sroberto	u_char	num_events;	/* number of error events */
277285612Sdelphij	u_int32	ttl;		/* ttl/refclock mode */
278285612Sdelphij	char	*ident;		/* group identifier name */
27954359Sroberto
28082498Sroberto	/*
28182498Sroberto	 * Variables used by reference clock support
28282498Sroberto	 */
283182007Sroberto#ifdef REFCLOCK
28482498Sroberto	struct refclockproc *procptr; /* refclock structure pointer */
28582498Sroberto	u_char	refclktype;	/* reference clock type */
28682498Sroberto	u_char	refclkunit;	/* reference clock unit number */
28782498Sroberto	u_char	sstclktype;	/* clock type for system status word */
288182007Sroberto#endif /* REFCLOCK */
28954359Sroberto
29082498Sroberto	/*
29182498Sroberto	 * Variables set by received packet
29282498Sroberto	 */
29382498Sroberto	u_char	leap;		/* local leap indicator */
29482498Sroberto	u_char	pmode;		/* remote association mode */
29582498Sroberto	u_char	stratum;	/* remote stratum */
296182007Sroberto	u_char	ppoll;		/* remote poll interval */
29782498Sroberto	s_char	precision;	/* remote clock precision */
298285612Sdelphij	double	rootdelay;	/* roundtrip delay to primary source */
299285612Sdelphij	double	rootdisp;	/* dispersion to primary source */
30082498Sroberto	u_int32	refid;		/* remote reference ID */
30182498Sroberto	l_fp	reftime;	/* update epoch */
30282498Sroberto
30382498Sroberto	/*
30482498Sroberto	 * Variables used by authenticated client
30582498Sroberto	 */
30682498Sroberto	keyid_t keyid;		/* current key ID */
307285612Sdelphij#ifdef AUTOKEY
308285612Sdelphij#define clear_to_zero opcode
309285612Sdelphij	u_int32	opcode;		/* last request opcode */
31082498Sroberto	associd_t assoc;	/* peer association ID */
31182498Sroberto	u_int32	crypto;		/* peer status word */
312132451Sroberto	EVP_PKEY *pkey;		/* public key */
313132451Sroberto	const EVP_MD *digest;	/* message digest algorithm */
314132451Sroberto	char	*subject;	/* certificate subject name */
315132451Sroberto	char	*issuer;	/* certificate issuer name */
316285612Sdelphij	struct cert_info *xinfo; /* issuer certificate */
31782498Sroberto	keyid_t	pkeyid;		/* previous key ID */
318285612Sdelphij	keyid_t	hcookie;	/* host cookie */
319132451Sroberto	keyid_t	pcookie;	/* peer cookie */
320285612Sdelphij	const struct pkey_info *ident_pkey; /* identity key */
321285612Sdelphij	BIGNUM	*iffval;	/* identity challenge (IFF, GQ, MV) */
322285612Sdelphij	const BIGNUM *grpkey;	/* identity challenge key (GQ) */
323285612Sdelphij	struct value cookval;	/* receive cookie values */
324132451Sroberto	struct value recval;	/* receive autokey values */
325132451Sroberto	struct exten *cmmd;	/* extension pointer */
326285612Sdelphij	u_long	refresh;	/* next refresh epoch */
327132451Sroberto
32882498Sroberto	/*
32982498Sroberto	 * Variables used by authenticated server
33082498Sroberto	 */
33182498Sroberto	keyid_t	*keylist;	/* session key ID list */
33282498Sroberto	int	keynumber;	/* current key number */
333132451Sroberto	struct value encrypt;	/* send encrypt values */
334132451Sroberto	struct value sndval;	/* send autokey values */
335285612Sdelphij#else	/* !AUTOKEY follows */
33682498Sroberto#define clear_to_zero status
337285612Sdelphij#endif	/* !AUTOKEY */
33882498Sroberto
33982498Sroberto	/*
34082498Sroberto	 * Ephemeral state variables
34182498Sroberto	 */
34282498Sroberto	u_char	status;		/* peer status */
343285612Sdelphij	u_char	new_status;	/* under-construction status */
34482498Sroberto	u_char	reach;		/* reachability register */
345285612Sdelphij	int	flash;		/* protocol error test tally bits */
34682498Sroberto	u_long	epoch;		/* reference epoch */
347285612Sdelphij	int	burst;		/* packets remaining in burst */
348285612Sdelphij	int	retry;		/* retry counter */
349285612Sdelphij	int	flip;		/* interleave mode control */
350285612Sdelphij	int	filter_nextpt;	/* index into filter shift register */
35182498Sroberto	double	filter_delay[NTP_SHIFT]; /* delay shift register */
35282498Sroberto	double	filter_offset[NTP_SHIFT]; /* offset shift register */
35382498Sroberto	double	filter_disp[NTP_SHIFT]; /* dispersion shift register */
35482498Sroberto	u_long	filter_epoch[NTP_SHIFT]; /* epoch shift register */
35582498Sroberto	u_char	filter_order[NTP_SHIFT]; /* filter sort index */
35682498Sroberto	l_fp	rec;		/* receive time stamp */
35782498Sroberto	l_fp	xmt;		/* transmit time stamp */
358285612Sdelphij	l_fp	dst;		/* destination timestamp */
359285612Sdelphij	l_fp	aorg;		/* origin timestamp */
360285612Sdelphij	l_fp	borg;		/* alternate origin timestamp */
361294569Sdelphij	l_fp	bxmt;		/* most recent broadcast transmit timestamp */
36282498Sroberto	double	offset;		/* peer clock offset */
36382498Sroberto	double	delay;		/* peer roundtrip delay */
36482498Sroberto	double	jitter;		/* peer jitter (squares) */
36582498Sroberto	double	disp;		/* peer dispersion */
366285612Sdelphij	double	xleave;		/* interleave delay */
367285612Sdelphij	double	bias;		/* programmed offset bias */
36882498Sroberto
36982498Sroberto	/*
370285612Sdelphij	 * Variables used to correct for packet length and asymmetry.
371285612Sdelphij	 */
372285612Sdelphij	double	t21;		/* outbound packet delay */
373285612Sdelphij	int	t21_bytes;	/* outbound packet length */
374285612Sdelphij	int	t21_last;	/* last outbound packet length */
375285612Sdelphij	double	r21;		/* outbound data rate */
376285612Sdelphij	double	t34;		/* inbound packet delay */
377285612Sdelphij	int	t34_bytes;	/* inbound packet length */
378285612Sdelphij	double	r34;		/* inbound data rate */
379285612Sdelphij
380285612Sdelphij	/*
38182498Sroberto	 * End of clear-to-zero area
38282498Sroberto	 */
38382498Sroberto	u_long	update;		/* receive epoch */
384285612Sdelphij#define end_clear_to_zero update
385285612Sdelphij	int	unreach;	/* watchdog counter */
386285612Sdelphij	int	throttle;	/* rate control */
38782498Sroberto	u_long	outdate;	/* send time last packet */
38882498Sroberto	u_long	nextdate;	/* send time next packet */
389182007Sroberto
39054359Sroberto	/*
39182498Sroberto	 * Statistic counters
39254359Sroberto	 */
39382498Sroberto	u_long	timereset;	/* time stat counters were reset */
394310419Sdelphij	u_long	timelastrec;	/* last packet received time, incl. trash */
395294569Sdelphij	u_long	timereceived;	/* last (clean) packet received time */
39682498Sroberto	u_long	timereachable;	/* last reachable/unreachable time */
39782498Sroberto
39882498Sroberto	u_long	sent;		/* packets sent */
39982498Sroberto	u_long	received;	/* packets received */
400285612Sdelphij	u_long	processed;	/* packets processed */
401285612Sdelphij	u_long	badauth;	/* bad authentication (TEST5) */
402298770Sdelphij	u_long	badNAK;		/* invalid crypto-NAK */
403285612Sdelphij	u_long	bogusorg;	/* bogus origin (TEST2, TEST3) */
404285612Sdelphij	u_long	oldpkt;		/* old duplicate (TEST1) */
405285612Sdelphij	u_long	seldisptoolarge; /* bad header (TEST6, TEST7) */
406285612Sdelphij	u_long	selbroken;	/* KoD received */
40754359Sroberto};
40854359Sroberto
40954359Sroberto/*
41054359Sroberto * Values for peer.leap, sys_leap
41154359Sroberto */
41254359Sroberto#define	LEAP_NOWARNING	0x0	/* normal, no leap second warning */
41354359Sroberto#define	LEAP_ADDSECOND	0x1	/* last minute of day has 61 seconds */
41454359Sroberto#define	LEAP_DELSECOND	0x2	/* last minute of day has 59 seconds */
41554359Sroberto#define	LEAP_NOTINSYNC	0x3	/* overload, clock is free running */
41654359Sroberto
41754359Sroberto/*
418182007Sroberto * Values for peer mode and packet mode. Only the modes through
419182007Sroberto * MODE_BROADCAST and MODE_BCLIENT appear in the transition
420182007Sroberto * function. MODE_CONTROL and MODE_PRIVATE can appear in packets,
421182007Sroberto * but those never survive to the transition function.
422310419Sdelphij */
423132451Sroberto#define	MODE_UNSPEC	0	/* unspecified (old version) */
424182007Sroberto#define	MODE_ACTIVE	1	/* symmetric active mode */
425182007Sroberto#define	MODE_PASSIVE	2	/* symmetric passive mode */
42654359Sroberto#define	MODE_CLIENT	3	/* client mode */
42754359Sroberto#define	MODE_SERVER	4	/* server mode */
42854359Sroberto#define	MODE_BROADCAST	5	/* broadcast mode */
429182007Sroberto/*
430182007Sroberto * These can appear in packets
431182007Sroberto */
432182007Sroberto#define	MODE_CONTROL	6	/* control mode */
433182007Sroberto#define	MODE_PRIVATE	7	/* private mode */
434182007Sroberto/*
435310419Sdelphij * This is a made-up mode for broadcast client.
436182007Sroberto */
437182007Sroberto#define	MODE_BCLIENT	6	/* broadcast client mode */
43854359Sroberto
43954359Sroberto/*
44054359Sroberto * Values for peer.stratum, sys_stratum
44154359Sroberto */
442132451Sroberto#define	STRATUM_REFCLOCK ((u_char)0) /* default stratum */
44354359Sroberto/* A stratum of 0 in the packet is mapped to 16 internally */
44454359Sroberto#define	STRATUM_PKT_UNSPEC ((u_char)0) /* unspecified in packet */
44582498Sroberto#define	STRATUM_UNSPEC	((u_char)16) /* unspecified */
44654359Sroberto
44754359Sroberto/*
448285612Sdelphij * Values for peer.flags (u_int)
44954359Sroberto */
45082498Sroberto#define	FLAG_CONFIG	0x0001	/* association was configured */
451285612Sdelphij#define	FLAG_PREEMPT	0x0002	/* preemptable association */
45282498Sroberto#define	FLAG_AUTHENTIC	0x0004	/* last message was authentic */
453285612Sdelphij#define	FLAG_REFCLOCK	0x0008	/* this is actually a reference clock */
454285612Sdelphij#define	FLAG_BC_VOL	0x0010	/* broadcast client volleying */
455285612Sdelphij#define	FLAG_PREFER	0x0020	/* prefer peer */
456285612Sdelphij#define	FLAG_BURST	0x0040	/* burst mode */
457285612Sdelphij#define	FLAG_PPS	0x0080	/* steered by PPS */
458285612Sdelphij#define	FLAG_IBURST	0x0100	/* initial burst mode */
459285612Sdelphij#define	FLAG_NOSELECT	0x0200	/* never select */
460285612Sdelphij#define	FLAG_TRUE	0x0400	/* force truechimer */
461285612Sdelphij#define	FLAG_SKEY	0x0800  /* autokey authentication */
462285612Sdelphij#define	FLAG_XLEAVE	0x1000	/* interleaved protocol */
463285612Sdelphij#define	FLAG_XB		0x2000	/* interleaved broadcast */
464285612Sdelphij#define	FLAG_XBOGUS	0x4000	/* interleaved bogus packet */
465285612Sdelphij#ifdef	OPENSSL
466285612Sdelphij# define FLAG_ASSOC	0x8000	/* autokey request */
467285612Sdelphij#endif /* OPENSSL */
468285612Sdelphij#define FLAG_TSTAMP_PPS	0x10000	/* PPS source provides absolute timestamp */
46954359Sroberto
47054359Sroberto/*
47154359Sroberto * Definitions for the clear() routine.  We use memset() to clear
47254359Sroberto * the parts of the peer structure which go to zero.  These are
47354359Sroberto * used to calculate the start address and length of the area.
47454359Sroberto */
47554359Sroberto#define	CLEAR_TO_ZERO(p)	((char *)&((p)->clear_to_zero))
47654359Sroberto#define	END_CLEAR_TO_ZERO(p)	((char *)&((p)->end_clear_to_zero))
477285612Sdelphij#define	LEN_CLEAR_TO_ZERO(p)	(END_CLEAR_TO_ZERO(p) - CLEAR_TO_ZERO(p))
478132451Sroberto#define CRYPTO_TO_ZERO(p)	((char *)&((p)->clear_to_zero))
479132451Sroberto#define END_CRYPTO_TO_ZERO(p)	((char *)&((p)->end_clear_to_zero))
480132451Sroberto#define LEN_CRYPTO_TO_ZERO	(END_CRYPTO_TO_ZERO((struct peer *)0) \
48182498Sroberto				    - CRYPTO_TO_ZERO((struct peer *)0))
48282498Sroberto
48354359Sroberto/*
48454359Sroberto * Reference clock types.  Added as necessary.
48554359Sroberto */
48654359Sroberto#define	REFCLK_NONE		0	/* unknown or missing */
48754359Sroberto#define	REFCLK_LOCALCLOCK	1	/* external (e.g., lockclock) */
48854359Sroberto#define	REFCLK_GPS_TRAK		2	/* TRAK 8810 GPS Receiver */
48954359Sroberto#define	REFCLK_WWV_PST		3	/* PST/Traconex 1020 WWV/H */
49056746Sroberto#define	REFCLK_SPECTRACOM	4	/* Spectracom (generic) Receivers */
49154359Sroberto#define	REFCLK_TRUETIME		5	/* TrueTime (generic) Receivers */
492132451Sroberto#define REFCLK_IRIG_AUDIO	6	/* IRIG-B/W audio decoder */
49356746Sroberto#define	REFCLK_CHU_AUDIO	7	/* CHU audio demodulator/decoder */
49454359Sroberto#define REFCLK_PARSE		8	/* generic driver (usually DCF77,GPS,MSF) */
49554359Sroberto#define	REFCLK_GPS_MX4200	9	/* Magnavox MX4200 GPS */
49654359Sroberto#define REFCLK_GPS_AS2201	10	/* Austron 2201A GPS */
49754359Sroberto#define	REFCLK_GPS_ARBITER	11	/* Arbiter 1088A/B/ GPS */
49854359Sroberto#define REFCLK_IRIG_TPRO	12	/* KSI/Odetics TPRO-S IRIG */
49954359Sroberto#define REFCLK_ATOM_LEITCH	13	/* Leitch CSD 5300 Master Clock */
50054359Sroberto#define REFCLK_MSF_EES		14	/* EES M201 MSF Receiver */
50154359Sroberto#define	REFCLK_GPSTM_TRUE	15	/* OLD TrueTime GPS/TM-TMD Receiver */
50254359Sroberto#define REFCLK_IRIG_BANCOMM	16	/* Bancomm GPS/IRIG Interface */
50354359Sroberto#define REFCLK_GPS_DATUM	17	/* Datum Programmable Time System */
504182007Sroberto#define REFCLK_ACTS		18	/* Generic Auto Computer Time Service */
50554359Sroberto#define REFCLK_WWV_HEATH	19	/* Heath GC1000 WWV/WWVH Receiver */
50654359Sroberto#define REFCLK_GPS_NMEA		20	/* NMEA based GPS clock */
50754359Sroberto#define REFCLK_GPS_VME		21	/* TrueTime GPS-VME Interface */
50854359Sroberto#define REFCLK_ATOM_PPS		22	/* 1-PPS Clock Discipline */
509182007Sroberto#define REFCLK_PTB_ACTS		23	/* replaced by REFCLK_ACTS */
510182007Sroberto#define REFCLK_USNO		24	/* replaced by REFCLK_ACTS */
51154359Sroberto#define REFCLK_GPS_HP		26	/* HP 58503A Time/Frequency Receiver */
512132451Sroberto#define REFCLK_ARCRON_MSF	27	/* ARCRON MSF radio clock. */
51354359Sroberto#define REFCLK_SHM		28	/* clock attached thru shared memory */
51454359Sroberto#define REFCLK_PALISADE		29	/* Trimble Navigation Palisade GPS */
51554359Sroberto#define REFCLK_ONCORE		30	/* Motorola UT Oncore GPS */
51654359Sroberto#define REFCLK_GPS_JUPITER	31	/* Rockwell Jupiter GPS receiver */
517132451Sroberto#define REFCLK_CHRONOLOG	32	/* Chrono-log K WWVB receiver */
518132451Sroberto#define REFCLK_DUMBCLOCK	33	/* Dumb localtime clock */
519132451Sroberto#define REFCLK_ULINK		34	/* Ultralink M320 WWVB receiver */
52056746Sroberto#define REFCLK_PCF		35	/* Conrad parallel port radio clock */
52156746Sroberto#define REFCLK_WWV_AUDIO	36	/* WWV/H audio demodulator/decoder */
52256746Sroberto#define REFCLK_FG		37	/* Forum Graphic GPS */
523132451Sroberto#define REFCLK_HOPF_SERIAL	38	/* hopf DCF77/GPS serial receiver  */
52482498Sroberto#define REFCLK_HOPF_PCI		39	/* hopf DCF77/GPS PCI receiver  */
525106163Sroberto#define REFCLK_JJY		40	/* JJY receiver  */
526106424Sroberto#define	REFCLK_TT560		41	/* TrueTime 560 IRIG-B decoder */
527106424Sroberto#define REFCLK_ZYFER		42	/* Zyfer GPStarplus receiver  */
528106424Sroberto#define REFCLK_RIPENCC		43	/* RIPE NCC Trimble driver */
529132451Sroberto#define REFCLK_NEOCLOCK4X	44	/* NeoClock4X DCF77 or TDF receiver */
530285612Sdelphij#define REFCLK_TSYNCPCI		45	/* Spectracom TSYNC PCI timing board */
531285612Sdelphij#define REFCLK_GPSDJSON		46
532285612Sdelphij#define REFCLK_MAX		46
53354359Sroberto
534132451Sroberto
53554359Sroberto/*
53654359Sroberto * NTP packet format.  The mac field is optional.  It isn't really
53754359Sroberto * an l_fp either, but for now declaring it that way is convenient.
53854359Sroberto * See Appendix A in the specification.
53954359Sroberto *
54054359Sroberto * Note that all u_fp and l_fp values arrive in network byte order
54154359Sroberto * and must be converted (except the mac, which isn't, really).
54254359Sroberto */
54354359Srobertostruct pkt {
544285612Sdelphij	u_char	li_vn_mode;	/* peer leap indicator */
54582498Sroberto	u_char	stratum;	/* peer stratum */
54682498Sroberto	u_char	ppoll;		/* peer poll interval */
54782498Sroberto	s_char	precision;	/* peer clock precision */
548285612Sdelphij	u_fp	rootdelay;	/* roundtrip delay to primary source */
549285612Sdelphij	u_fp	rootdisp;	/* dispersion to primary source*/
550285612Sdelphij	u_int32	refid;		/* reference id */
551285612Sdelphij	l_fp	reftime;	/* last update time */
55282498Sroberto	l_fp	org;		/* originate time stamp */
55382498Sroberto	l_fp	rec;		/* receive time stamp */
55482498Sroberto	l_fp	xmt;		/* transmit time stamp */
55554359Sroberto
556330567Sgordon#define	MIN_V4_PKT_LEN	(12 * sizeof(u_int32))	/* min header length */
557330567Sgordon#define	LEN_PKT_NOMAC	(12 * sizeof(u_int32))	/* min header length */
558330567Sgordon#define	MIN_MAC_LEN	(1 * sizeof(u_int32))	/* crypto_NAK */
559330567Sgordon#define	MAX_MD5_LEN	(5 * sizeof(u_int32))	/* MD5 */
560285612Sdelphij#define	MAX_MAC_LEN	(6 * sizeof(u_int32))	/* SHA */
561330567Sgordon#define	KEY_MAC_LEN	sizeof(u_int32)		/* key ID in MAC */
562330567Sgordon#define	MAX_MDG_LEN	(MAX_MAC_LEN-KEY_MAC_LEN) /* max. digest len */
56354359Sroberto
56454359Sroberto	/*
56554359Sroberto	 * The length of the packet less MAC must be a multiple of 64
566285612Sdelphij	 * with an RSA modulus and Diffie-Hellman prime of 256 octets
56782498Sroberto	 * and maximum host name of 128 octets, the maximum autokey
56882498Sroberto	 * command is 152 octets and maximum autokey response is 460
56982498Sroberto	 * octets. A packet can contain no more than one command and one
570285612Sdelphij	 * response, so the maximum total extension field length is 864
57182498Sroberto	 * octets. But, to handle humungus certificates, the bank must
57282498Sroberto	 * be broke.
573285612Sdelphij	 *
574285612Sdelphij	 * The different definitions of the 'exten' field are here for
575285612Sdelphij	 * the benefit of applications that want to send a packet from
576285612Sdelphij	 * an auto variable in the stack - not using the AUTOKEY version
577285612Sdelphij	 * saves 2KB of stack space. The receive buffer should ALWAYS be
578285612Sdelphij	 * big enough to hold a full extended packet if the extension
579285612Sdelphij	 * fields have to be parsed or skipped.
58054359Sroberto	 */
581285612Sdelphij#ifdef AUTOKEY
582285612Sdelphij	u_int32	exten[(NTP_MAXEXTEN + MAX_MAC_LEN) / sizeof(u_int32)];
583285612Sdelphij#else	/* !AUTOKEY follows */
584285612Sdelphij	u_int32	exten[(MAX_MAC_LEN) / sizeof(u_int32)];
585285612Sdelphij#endif	/* !AUTOKEY */
58654359Sroberto};
58754359Sroberto
58854359Sroberto/*
58954359Sroberto * Stuff for extracting things from li_vn_mode
59054359Sroberto */
59154359Sroberto#define	PKT_MODE(li_vn_mode)	((u_char)((li_vn_mode) & 0x7))
59254359Sroberto#define	PKT_VERSION(li_vn_mode)	((u_char)(((li_vn_mode) >> 3) & 0x7))
59354359Sroberto#define	PKT_LEAP(li_vn_mode)	((u_char)(((li_vn_mode) >> 6) & 0x3))
59454359Sroberto
59554359Sroberto/*
596285612Sdelphij * Stuff for putting things back into li_vn_mode in packets and vn_mode
597285612Sdelphij * in ntp_monitor.c's mon_entry.
59854359Sroberto */
599285612Sdelphij#define VN_MODE(v, m)		((((v) & 7) << 3) | ((m) & 0x7))
600285612Sdelphij#define	PKT_LI_VN_MODE(l, v, m) ((((l) & 3) << 6) | VN_MODE((v), (m)))
60154359Sroberto
60254359Sroberto
60354359Sroberto/*
60454359Sroberto * Dealing with stratum.  0 gets mapped to 16 incoming, and back to 0
60554359Sroberto * on output.
60654359Sroberto */
60754359Sroberto#define	PKT_TO_STRATUM(s)	((u_char)(((s) == (STRATUM_PKT_UNSPEC)) ?\
60854359Sroberto				(STRATUM_UNSPEC) : (s)))
60954359Sroberto
61054359Sroberto#define	STRATUM_TO_PKT(s)	((u_char)(((s) == (STRATUM_UNSPEC)) ?\
61154359Sroberto				(STRATUM_PKT_UNSPEC) : (s)))
61254359Sroberto
61382498Sroberto/*
61482498Sroberto * Event codes. Used for reporting errors/events to the control module
61582498Sroberto */
616132451Sroberto#define	PEER_EVENT	0x080	/* this is a peer event */
617132451Sroberto#define CRPT_EVENT	0x100	/* this is a crypto event */
61854359Sroberto
61954359Sroberto/*
62082498Sroberto * System event codes
62154359Sroberto */
62282498Sroberto#define	EVNT_UNSPEC	0	/* unspecified */
623285612Sdelphij#define	EVNT_NSET	1	/* freq not set */
624285612Sdelphij#define	EVNT_FSET	2	/* freq set */
625285612Sdelphij#define	EVNT_SPIK	3	/* spike detect */
626285612Sdelphij#define	EVNT_FREQ	4	/* freq mode */
627285612Sdelphij#define	EVNT_SYNC	5	/* clock sync */
628285612Sdelphij#define	EVNT_SYSRESTART	6	/* restart */
629285612Sdelphij#define	EVNT_SYSFAULT	7	/* panic stop */
630285612Sdelphij#define	EVNT_NOPEER	8	/* no sys peer */
631285612Sdelphij#define	EVNT_ARMED	9	/* leap armed */
632285612Sdelphij#define	EVNT_DISARMED	10	/* leap disarmed */
633285612Sdelphij#define	EVNT_LEAP	11	/* leap event */
634285612Sdelphij#define	EVNT_CLOCKRESET	12	/* clock step */
635285612Sdelphij#define	EVNT_KERN	13	/* kernel event */
636285612Sdelphij#define	EVNT_TAI	14	/* TAI */
637285612Sdelphij#define	EVNT_LEAPVAL	15	/* stale leapsecond values */
63854359Sroberto
63982498Sroberto/*
64082498Sroberto * Peer event codes
64182498Sroberto */
642285612Sdelphij#define	PEVNT_MOBIL	(1 | PEER_EVENT) /* mobilize */
643285612Sdelphij#define	PEVNT_DEMOBIL	(2 | PEER_EVENT) /* demobilize */
644285612Sdelphij#define	PEVNT_UNREACH	(3 | PEER_EVENT) /* unreachable */
645285612Sdelphij#define	PEVNT_REACH	(4 | PEER_EVENT) /* reachable */
646285612Sdelphij#define	PEVNT_RESTART	(5 | PEER_EVENT) /* restart */
647285612Sdelphij#define	PEVNT_REPLY	(6 | PEER_EVENT) /* no reply */
648285612Sdelphij#define	PEVNT_RATE	(7 | PEER_EVENT) /* rate exceeded */
649285612Sdelphij#define	PEVNT_DENY	(8 | PEER_EVENT) /* access denied */
650285612Sdelphij#define PEVNT_ARMED	(9 | PEER_EVENT) /* leap armed */
651285612Sdelphij#define	PEVNT_NEWPEER	(10 | PEER_EVENT) /* sys peer */
652285612Sdelphij#define	PEVNT_CLOCK	(11 | PEER_EVENT) /* clock event */
653285612Sdelphij#define	PEVNT_AUTH	(12 | PEER_EVENT) /* bad auth */
654285612Sdelphij#define	PEVNT_POPCORN	(13 | PEER_EVENT) /* popcorn */
655285612Sdelphij#define	PEVNT_XLEAVE	(14 | PEER_EVENT) /* interleave mode */
656285612Sdelphij#define	PEVNT_XERR	(15 | PEER_EVENT) /* interleave error */
65754359Sroberto
65854359Sroberto/*
65954359Sroberto * Clock event codes
66054359Sroberto */
66182498Sroberto#define	CEVNT_NOMINAL	0	/* unspecified */
662285612Sdelphij#define	CEVNT_TIMEOUT	1	/* no reply */
663285612Sdelphij#define	CEVNT_BADREPLY	2	/* bad format */
664285612Sdelphij#define	CEVNT_FAULT	3	/* fault */
665285612Sdelphij#define	CEVNT_PROP	4	/* bad signal */
666285612Sdelphij#define	CEVNT_BADDATE	5	/* bad date */
667285612Sdelphij#define	CEVNT_BADTIME	6	/* bad time */
66854359Sroberto#define CEVNT_MAX	CEVNT_BADTIME
66954359Sroberto
67054359Sroberto/*
67154359Sroberto * Very misplaced value.  Default port through which we send traps.
67254359Sroberto */
67354359Sroberto#define	TRAPPORT	18447
67454359Sroberto
67554359Sroberto
67654359Sroberto/*
67782498Sroberto * To speed lookups, peers are hashed by the low order bits of the
67882498Sroberto * remote IP address. These definitions relate to that.
67954359Sroberto */
680285612Sdelphij#define	NTP_HASH_SIZE		128
681285612Sdelphij#define	NTP_HASH_MASK		(NTP_HASH_SIZE-1)
682285612Sdelphij#define	NTP_HASH_ADDR(src)	(sock_hash(src) & NTP_HASH_MASK)
68354359Sroberto
68454359Sroberto/*
68554359Sroberto * min, min3 and max.  Makes it easier to transliterate the spec without
68654359Sroberto * thinking about it.
68754359Sroberto */
68854359Sroberto#define	min(a,b)	(((a) < (b)) ? (a) : (b))
68954359Sroberto#define	max(a,b)	(((a) > (b)) ? (a) : (b))
69054359Sroberto#define	min3(a,b,c)	min(min((a),(b)), (c))
69154359Sroberto
69254359Sroberto
69354359Sroberto/*
69454359Sroberto * Configuration items.  These are for the protocol module (proto_config())
69554359Sroberto */
69654359Sroberto#define	PROTO_BROADCLIENT	1
69754359Sroberto#define	PROTO_PRECISION		2	/* (not used) */
69854359Sroberto#define	PROTO_AUTHENTICATE	3
69954359Sroberto#define	PROTO_BROADDELAY	4
70054359Sroberto#define	PROTO_AUTHDELAY		5	/* (not used) */
70154359Sroberto#define PROTO_MULTICAST_ADD	6
70254359Sroberto#define PROTO_MULTICAST_DEL	7
70354359Sroberto#define PROTO_NTP		8
70454359Sroberto#define PROTO_KERNEL		9
70554359Sroberto#define PROTO_MONITOR		10
70654359Sroberto#define PROTO_FILEGEN		11
70782498Sroberto#define	PROTO_PPS		12
70882498Sroberto#define PROTO_CAL		13
709132451Sroberto#define PROTO_MINCLOCK		14
710182007Sroberto#define	PROTO_MAXCLOCK		15
711182007Sroberto#define PROTO_MINSANE		16
712182007Sroberto#define PROTO_FLOOR		17
713182007Sroberto#define PROTO_CEILING		18
714182007Sroberto#define PROTO_COHORT		19
715182007Sroberto#define PROTO_CALLDELAY		20
716182007Sroberto#define PROTO_MINDISP		21
717182007Sroberto#define PROTO_MAXDIST		22
718285612Sdelphij	/* available		23 */
719182007Sroberto#define	PROTO_MAXHOP		24
720182007Sroberto#define	PROTO_BEACON		25
721182007Sroberto#define	PROTO_ORPHAN		26
722285612Sdelphij#define	PROTO_ORPHWAIT		27
723285612Sdelphij#define	PROTO_MODE7		28
724294569Sdelphij#define	PROTO_UECRYPTO		29
725294569Sdelphij#define	PROTO_UECRYPTONAK	30
726294569Sdelphij#define	PROTO_UEDIGEST		31
727301301Sdelphij#define	PROTO_PCEDIGEST		32
728310419Sdelphij#define	PROTO_BCPOLLBSTEP	33
72954359Sroberto
73054359Sroberto/*
73154359Sroberto * Configuration items for the loop filter
73254359Sroberto */
733285612Sdelphij#define	LOOP_DRIFTINIT		1	/* iniitialize frequency */
734285612Sdelphij#define	LOOP_KERN_CLEAR		2	/* set initial frequency offset */
735285612Sdelphij#define LOOP_MAX		3	/* set both step offsets */
736310419Sdelphij#define LOOP_MAX_BACK		4	/* set backward-step offset */
737285612Sdelphij#define LOOP_MAX_FWD		5	/* set forward-step offset */
738285612Sdelphij#define LOOP_PANIC		6	/* set panic offseet */
739285612Sdelphij#define LOOP_PHI		7	/* set dispersion rate */
740285612Sdelphij#define LOOP_MINSTEP		8	/* set step timeout */
741285612Sdelphij#define LOOP_MINPOLL		9	/* set min poll interval (log2 s) */
742285612Sdelphij#define LOOP_ALLAN		10	/* set minimum Allan intercept */
743285612Sdelphij#define LOOP_HUFFPUFF		11	/* set huff-n'-puff filter length */
744285612Sdelphij#define LOOP_FREQ		12	/* set initial frequency */
745285612Sdelphij#define LOOP_CODEC		13	/* set audio codec frequency */
746285612Sdelphij#define	LOOP_LEAP		14	/* insert leap after second 23:59 */
747285612Sdelphij#define	LOOP_TICK		15	/* sim. low precision clock */
74854359Sroberto
74954359Sroberto/*
75054359Sroberto * Configuration items for the stats printer
75154359Sroberto */
75254359Sroberto#define	STATS_FREQ_FILE		1	/* configure drift file */
75354359Sroberto#define STATS_STATSDIR		2	/* directory prefix for stats files */
75454359Sroberto#define	STATS_PID_FILE		3	/* configure ntpd PID file */
755285612Sdelphij#define	STATS_LEAP_FILE		4	/* configure ntpd leapseconds file */
75654359Sroberto
757132451Sroberto#define MJD_1900		15020	/* MJD for 1 Jan 1900 */
75854359Sroberto
75954359Sroberto/*
76054359Sroberto * Default parameters.  We use these in the absence of something better.
76154359Sroberto */
76254359Sroberto#define INADDR_NTP	0xe0000101	/* NTP multicast address 224.0.1.1 */
76382498Sroberto
76454359Sroberto/*
76554359Sroberto * Structure used optionally for monitoring when this is turned on.
76654359Sroberto */
767285612Sdelphijtypedef struct mon_data	mon_entry;
76854359Srobertostruct mon_data {
769285612Sdelphij	mon_entry *	hash_next;	/* next structure in hash list */
770285612Sdelphij	DECL_DLIST_LINK(mon_entry, mru);/* MRU list link pointers */
771285612Sdelphij	struct interface * lcladr;	/* address on which this arrived */
772285612Sdelphij	l_fp		first;		/* first time seen */
773285612Sdelphij	l_fp		last;		/* last time seen */
774285612Sdelphij	int		leak;		/* leaky bucket accumulator */
775285612Sdelphij	int		count;		/* total packet count */
776285612Sdelphij	u_short		flags;		/* restrict flags */
777285612Sdelphij	u_char		vn_mode;	/* packet mode & version */
778285612Sdelphij	u_char		cast_flags;	/* flags MDF_?CAST */
779285612Sdelphij	sockaddr_u	rmtadr;		/* address of remote host */
78054359Sroberto};
78154359Sroberto
78282498Sroberto/*
783285612Sdelphij * Values for cast_flags in mon_entry and struct peer.  mon_entry uses
784285612Sdelphij * only the first three, MDF_UCAST, MDF_MCAST, and MDF_BCAST.
78582498Sroberto */
786285612Sdelphij#define	MDF_UCAST	0x01	/* unicast client */
787285612Sdelphij#define	MDF_MCAST	0x02	/* multicast server */
788285612Sdelphij#define	MDF_BCAST	0x04	/* broadcast server */
789285612Sdelphij#define	MDF_POOL	0x08	/* pool client solicitor */
790285612Sdelphij#define MDF_ACAST	0x10	/* manycast client solicitor */
791285612Sdelphij#define	MDF_BCLNT	0x20	/* eph. broadcast/multicast client */
792285612Sdelphij#define MDF_UCLNT	0x40	/* preemptible manycast or pool client */
79354359Sroberto/*
794285612Sdelphij * In the context of struct peer in ntpd, three of the cast_flags bits
795285612Sdelphij * represent configured associations which never receive packets, and
796285612Sdelphij * whose reach is always 0: MDF_BCAST, MDF_MCAST, and MDF_ACAST.  The
797285612Sdelphij * last can be argued as responses are received, but those responses do
798285612Sdelphij * not affect the MDF_ACAST association's reach register, rather they
799285612Sdelphij * (may) result in mobilizing ephemeral MDF_ACLNT associations.
800285612Sdelphij */
801285612Sdelphij#define MDF_TXONLY_MASK	(MDF_BCAST | MDF_MCAST | MDF_ACAST | MDF_POOL)
802285612Sdelphij/*
803285612Sdelphij * manycastclient-like solicitor association cast_flags bits
804285612Sdelphij */
805285612Sdelphij#define MDF_SOLICIT_MASK	(MDF_ACAST | MDF_POOL)
806285612Sdelphij/*
80754359Sroberto * Values used with mon_enabled to indicate reason for enabling monitoring
80854359Sroberto */
809285612Sdelphij#define MON_OFF		0x00		/* no monitoring */
810285612Sdelphij#define MON_ON		0x01		/* monitoring explicitly enabled */
811285612Sdelphij#define MON_RES		0x02		/* implicit monitoring for RES_LIMITED */
81254359Sroberto/*
81354359Sroberto * Structure used for restrictlist entries
81454359Sroberto */
815285612Sdelphijtypedef struct res_addr4_tag {
816285612Sdelphij	u_int32		addr;		/* IPv4 addr (host order) */
817285612Sdelphij	u_int32		mask;		/* IPv4 mask (host order) */
818285612Sdelphij} res_addr4;
81954359Sroberto
820285612Sdelphijtypedef struct res_addr6_tag {
821285612Sdelphij	struct in6_addr addr;		/* IPv6 addr (net order) */
822285612Sdelphij	struct in6_addr mask;		/* IPv6 mask (net order) */
823285612Sdelphij} res_addr6;
824285612Sdelphij
825285612Sdelphijtypedef struct restrict_u_tag	restrict_u;
826285612Sdelphijstruct restrict_u_tag {
827330567Sgordon	restrict_u *	link;		/* link to next entry */
828330567Sgordon	u_int32		count;		/* number of packets matched */
829330567Sgordon	u_short		rflags;		/* restrict (accesslist) flags */
830330567Sgordon	u_short		mflags;		/* match flags */
831330567Sgordon	short		ippeerlimit;	/* IP peer limit */
832330567Sgordon	u_long		expire;		/* valid until time */
833285612Sdelphij	union {				/* variant starting here */
834285612Sdelphij		res_addr4 v4;
835285612Sdelphij		res_addr6 v6;
836285612Sdelphij	} u;
837132451Sroberto};
838285612Sdelphij#define	V4_SIZEOF_RESTRICT_U	(offsetof(restrict_u, u)	\
839285612Sdelphij				 + sizeof(res_addr4))
840285612Sdelphij#define	V6_SIZEOF_RESTRICT_U	(offsetof(restrict_u, u)	\
841285612Sdelphij				 + sizeof(res_addr6))
842132451Sroberto
843330567Sgordontypedef struct r4addr_tag	r4addr;
844330567Sgordonstruct r4addr_tag {
845330567Sgordon	u_short		rflags;		/* match flags */
846330567Sgordon	short		ippeerlimit;	/* IP peer limit */
847330567Sgordon};
848330567Sgordon
849330567Sgordonchar *build_iflags(u_int32 flags);
850330567Sgordonchar *build_mflags(u_short mflags);
851330567Sgordonchar *build_rflags(u_short rflags);
852330567Sgordon
85354359Sroberto/*
854330567Sgordon * Restrict (Access) flags (rflags)
85554359Sroberto */
856285612Sdelphij#define	RES_IGNORE		0x0001	/* ignore packet */
857285612Sdelphij#define	RES_DONTSERVE		0x0002	/* access denied */
858285612Sdelphij#define	RES_DONTTRUST		0x0004	/* authentication required */
859285612Sdelphij#define	RES_VERSION		0x0008	/* version mismatch */
860285612Sdelphij#define	RES_NOPEER		0x0010	/* new association denied */
861330567Sgordon#define	RES_NOEPEER		0x0020	/* new ephemeral association denied */
862330567Sgordon#define RES_LIMITED		0x0040	/* packet rate exceeded */
863132451Sroberto#define RES_FLAGS		(RES_IGNORE | RES_DONTSERVE |\
864132451Sroberto				    RES_DONTTRUST | RES_VERSION |\
865330567Sgordon				    RES_NOPEER | RES_NOEPEER | RES_LIMITED)
86654359Sroberto
867330567Sgordon#define	RES_NOQUERY		0x0080	/* mode 6/7 packet denied */
868330567Sgordon#define	RES_NOMODIFY		0x0100	/* mode 6/7 modify denied */
869330567Sgordon#define	RES_NOTRAP		0x0200	/* mode 6/7 set trap denied */
870330567Sgordon#define	RES_LPTRAP		0x0400	/* mode 6/7 low priority trap */
871132451Sroberto
872330567Sgordon#define	RES_KOD			0x0800	/* send kiss of death packet */
873330567Sgordon#define	RES_MSSNTP		0x1000	/* enable MS-SNTP authentication */
874330567Sgordon#define	RES_FLAKE		0x2000	/* flakeway - drop 10% */
875330567Sgordon#define	RES_NOMRULIST		0x4000	/* mode 6 mrulist denied */
876330567Sgordon#define RES_UNUSED		0x8000	/* Unused flag bits */
877132451Sroberto
878285612Sdelphij#define	RES_ALLFLAGS		(RES_FLAGS | RES_NOQUERY |	\
879285612Sdelphij				 RES_NOMODIFY | RES_NOTRAP |	\
880285612Sdelphij				 RES_LPTRAP | RES_KOD |		\
881285612Sdelphij				 RES_MSSNTP | RES_FLAKE |	\
882285612Sdelphij				 RES_NOMRULIST)
883132451Sroberto
88454359Sroberto/*
885330567Sgordon * Match flags (mflags)
88654359Sroberto */
887285612Sdelphij#define	RESM_INTERFACE		0x1000	/* this is an interface */
888285612Sdelphij#define	RESM_NTPONLY		0x2000	/* match source port 123 */
889285612Sdelphij#define RESM_SOURCE		0x4000	/* from "restrict source" */
89054359Sroberto
89154359Sroberto/*
89254359Sroberto * Restriction configuration ops
89354359Sroberto */
894330567Sgordontypedef enum
895330567Sgordonrestrict_ops {
896330567Sgordon	RESTRICT_FLAGS = 1,	/* add rflags to restrict entry */
897330567Sgordon	RESTRICT_UNFLAG,	/* remove rflags from restrict entry */
898330567Sgordon	RESTRICT_REMOVE,	/* remove a restrict entry */
899330567Sgordon	RESTRICT_REMOVEIF,	/* remove an interface restrict entry */
900330567Sgordon} restrict_op;
90154359Sroberto
90254359Sroberto/*
90354359Sroberto * Endpoint structure for the select algorithm
90454359Sroberto */
90554359Srobertostruct endpoint {
90654359Sroberto	double	val;			/* offset of endpoint */
90754359Sroberto	int	type;			/* interval entry/exit */
90854359Sroberto};
90954359Sroberto
91054359Sroberto/*
91154359Sroberto * Association matching AM[] return codes
91254359Sroberto */
913182007Sroberto#define AM_ERR		-1		/* error */
914182007Sroberto#define AM_NOMATCH	0		/* no match */
915285612Sdelphij#define AM_PROCPKT	1		/* server/symmetric packet */
916285612Sdelphij#define AM_BCST		2		/* broadcast packet */
917182007Sroberto#define AM_FXMIT	3		/* client packet */
918285612Sdelphij#define AM_MANYCAST	4		/* manycast or pool */
919182007Sroberto#define AM_NEWPASS	5		/* new passive */
920182007Sroberto#define AM_NEWBCL	6		/* new broadcast */
921285612Sdelphij#define AM_POSSBCL	7		/* discard broadcast */
92254359Sroberto
92354359Sroberto/* NetInfo configuration locations */
92454359Sroberto#ifdef HAVE_NETINFO
92554359Sroberto#define NETINFO_CONFIG_DIR "/config/ntp"
92654359Sroberto#endif
92754359Sroberto
928285612Sdelphij/* ntpq -c mrulist rows per request limit in ntpd */
929285612Sdelphij#define MRU_ROW_LIMIT	256
930285612Sdelphij/* similar datagrams per response limit for ntpd */
931285612Sdelphij#define MRU_FRAGS_LIMIT	128
93254359Sroberto#endif /* NTP_H */
933