154359Sroberto/*
254359Sroberto * ntp.h - NTP definitions for the masses
354359Sroberto */
454359Sroberto#ifndef NTP_H
554359Sroberto#define NTP_H
654359Sroberto
7290000Sglebius#include <stddef.h>
854359Sroberto#include <math.h>
9290000Sglebius
10290000Sglebius#include <ntp_fp.h>
11290000Sglebius#include <ntp_types.h>
12290000Sglebius#include <ntp_lists.h>
13290000Sglebius#include <ntp_stdlib.h>
14290000Sglebius#include <ntp_crypto.h>
15182007Sroberto#include <ntp_random.h>
16290000Sglebius#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 */
103290000Sglebius#define NTP_UNREACH	10	/* poll unreach threshold */
104290000Sglebius#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) */
108290000Sglebius#define	NTP_RETRY	3	/* max packet retries */
109290000Sglebius#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 */
123290000Sglebius#define MINDISPERSE	.001	/* min distance */
124290000Sglebius#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 */
130290000Sglebius#define NTP_MAXEXTEN	2048	/* max extension field size */
131290000Sglebius#define	NTP_ORPHWAIT	300	/* orphan wair (s) */
13282498Sroberto
13354359Sroberto/*
134132451Sroberto * Miscellaneous stuff
135132451Sroberto */
136182007Sroberto#define NTP_MAXKEY	65535	/* max authentication key number */
137290000Sglebius#define	KEY_TYPE_MD5	NID_md5	/* MD5 digest NID */
138132451Sroberto/*
139132451Sroberto * Limits of things
140132451Sroberto */
141290000Sglebius#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
152290000Sglebius * 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)))
159290000Sglebius#define LOGTOD(a)	ldexp(1., (int)(a)) /* log2 to double */
16054359Sroberto#define UNIVAR(x)	(SQUARE(.28867513 * LOGTOD(x))) /* std uniform distr */
161290000Sglebius#define ULOGTOD(a)	ldexp(1., (int)(a)) /* ulog2 to double */
16254359Sroberto
16382498Sroberto#define	EVENT_TIMEOUT	0	/* one second, that is */
16482498Sroberto
165290000Sglebius
16654359Sroberto/*
16754359Sroberto * The interface structure is used to hold the addresses and socket
168290000Sglebius * numbers of each of the local network addresses we are using.
169290000Sglebius * Because "interface" is a reserved word in C++ and has so many
170290000Sglebius * varied meanings, a change to "endpt" (via typedef) is under way.
171290000Sglebius * Eventually the struct tag will change from interface to endpt_tag.
172290000Sglebius * endpt is unrelated to the select algorithm's struct endpoint.
17354359Sroberto */
174290000Sglebiustypedef struct interface endpt;
17554359Srobertostruct interface {
176290000Sglebius	endpt *		elink;		/* endpt list link */
177290000Sglebius	endpt *		mclink;		/* per-AF_* multicast list */
178298770Sdelphij	void *		ioreg_ctx;	/* IO registration context */
179290000Sglebius	SOCKET		fd;		/* socket descriptor */
180290000Sglebius	SOCKET		bfd;		/* for receiving broadcasts */
181290000Sglebius	u_int32		ifnum;		/* endpt instance count */
182290000Sglebius	sockaddr_u	sin;		/* unicast address */
183290000Sglebius	sockaddr_u	mask;		/* subnet mask */
184290000Sglebius	sockaddr_u	bcast;		/* broadcast address */
185290000Sglebius	char		name[32];	/* name of interface */
186290000Sglebius	u_short		family;		/* AF_INET/AF_INET6 */
187290000Sglebius	u_short		phase;		/* phase in update cycle */
188290000Sglebius	u_int32		flags;		/* interface flags */
189290000Sglebius	int		last_ttl;	/* last TTL specified */
190290000Sglebius	u_int32		addr_refid;	/* IPv4 addr or IPv6 hash */
191290000Sglebius	int		num_mcast;	/* mcast addrs enabled */
192290000Sglebius	u_long		starttime;	/* current_time at creation */
193290000Sglebius	volatile long	received;	/* number of incoming packets */
194290000Sglebius	long		sent;		/* number of outgoing packets */
195290000Sglebius	long		notsent;	/* number of send failures */
196290000Sglebius	u_int		ifindex;	/* for IPV6_MULTICAST_IF */
197290000Sglebius	isc_boolean_t	ignore_packets; /* listen-read-drop this? */
198290000Sglebius	struct peer *	peers;		/* list of peers using endpt */
199290000Sglebius	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 */
210290000Sglebius#define	INT_BCASTOPEN	0x020	/* broadcast receive socket is open */
211182007Sroberto#define INT_MCASTOPEN	0x040	/* multicasting enabled */
212290000Sglebius#define INT_WILDCARD	0x080	/* wildcard interface - usually skipped */
213290000Sglebius#define INT_MCASTIF	0x100	/* bound directly to MCAST address */
214290000Sglebius#define INT_PRIVACY	0x200	/* RFC 4941 IPv6 privacy address */
215290000Sglebius#define INT_BCASTXMIT	0x400   /* socket setup to allow broadcasts */
216290000Sglebius
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 */
229290000Sglebius#define TEST5		0x0010	/* bad authentication */
230182007Sroberto#define TEST6		0x0020	/* bad synch or stratum */
231290000Sglebius#define TEST7		0x0040	/* bad header */
232290000Sglebius#define TEST8		0x0080  /* bad autokey */
233290000Sglebius#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 {
258290000Sglebius	struct peer *p_link;	/* link pointer in free & peer lists */
259290000Sglebius	struct peer *adr_link;	/* link pointer in address hash */
260290000Sglebius	struct peer *aid_link;	/* link pointer in associd hash */
261290000Sglebius	struct peer *ilink;	/* list of peers for interface */
262290000Sglebius	sockaddr_u srcadr;	/* address of remote host */
263290000Sglebius	char *	hostname;	/* if non-NULL, remote name */
264290000Sglebius	struct addrinfo *addrs;	/* hostname query result */
265290000Sglebius	struct addrinfo *ai;	/* position within addrs */
266290000Sglebius	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 */
277290000Sglebius	u_int32	ttl;		/* ttl/refclock mode */
278290000Sglebius	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 */
298290000Sglebius	double	rootdelay;	/* roundtrip delay to primary source */
299290000Sglebius	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 */
307290000Sglebius#ifdef AUTOKEY
308290000Sglebius#define clear_to_zero opcode
309290000Sglebius	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 */
316290000Sglebius	struct cert_info *xinfo; /* issuer certificate */
31782498Sroberto	keyid_t	pkeyid;		/* previous key ID */
318290000Sglebius	keyid_t	hcookie;	/* host cookie */
319132451Sroberto	keyid_t	pcookie;	/* peer cookie */
320290000Sglebius	const struct pkey_info *ident_pkey; /* identity key */
321290000Sglebius	BIGNUM	*iffval;	/* identity challenge (IFF, GQ, MV) */
322290000Sglebius	const BIGNUM *grpkey;	/* identity challenge key (GQ) */
323290000Sglebius	struct value cookval;	/* receive cookie values */
324132451Sroberto	struct value recval;	/* receive autokey values */
325132451Sroberto	struct exten *cmmd;	/* extension pointer */
326290000Sglebius	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 */
335290000Sglebius#else	/* !AUTOKEY follows */
33682498Sroberto#define clear_to_zero status
337290000Sglebius#endif	/* !AUTOKEY */
33882498Sroberto
33982498Sroberto	/*
34082498Sroberto	 * Ephemeral state variables
34182498Sroberto	 */
34282498Sroberto	u_char	status;		/* peer status */
343290000Sglebius	u_char	new_status;	/* under-construction status */
34482498Sroberto	u_char	reach;		/* reachability register */
345290000Sglebius	int	flash;		/* protocol error test tally bits */
34682498Sroberto	u_long	epoch;		/* reference epoch */
347290000Sglebius	int	burst;		/* packets remaining in burst */
348290000Sglebius	int	retry;		/* retry counter */
349290000Sglebius	int	flip;		/* interleave mode control */
350290000Sglebius	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 */
358290000Sglebius	l_fp	dst;		/* destination timestamp */
359290000Sglebius	l_fp	aorg;		/* origin timestamp */
360290000Sglebius	l_fp	borg;		/* alternate origin timestamp */
361294904Sdelphij	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 */
366290000Sglebius	double	xleave;		/* interleave delay */
367290000Sglebius	double	bias;		/* programmed offset bias */
36882498Sroberto
36982498Sroberto	/*
370290000Sglebius	 * Variables used to correct for packet length and asymmetry.
371290000Sglebius	 */
372290000Sglebius	double	t21;		/* outbound packet delay */
373290000Sglebius	int	t21_bytes;	/* outbound packet length */
374290000Sglebius	int	t21_last;	/* last outbound packet length */
375290000Sglebius	double	r21;		/* outbound data rate */
376290000Sglebius	double	t34;		/* inbound packet delay */
377290000Sglebius	int	t34_bytes;	/* inbound packet length */
378290000Sglebius	double	r34;		/* inbound data rate */
379290000Sglebius
380290000Sglebius	/*
38182498Sroberto	 * End of clear-to-zero area
38282498Sroberto	 */
38382498Sroberto	u_long	update;		/* receive epoch */
384290000Sglebius#define end_clear_to_zero update
385290000Sglebius	int	unreach;	/* watchdog counter */
386290000Sglebius	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 */
395294904Sdelphij	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 */
400290000Sglebius	u_long	processed;	/* packets processed */
401290000Sglebius	u_long	badauth;	/* bad authentication (TEST5) */
402298770Sdelphij	u_long	badNAK;		/* invalid crypto-NAK */
403290000Sglebius	u_long	bogusorg;	/* bogus origin (TEST2, TEST3) */
404290000Sglebius	u_long	oldpkt;		/* old duplicate (TEST1) */
405290000Sglebius	u_long	seldisptoolarge; /* bad header (TEST6, TEST7) */
406290000Sglebius	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/*
448290000Sglebius * Values for peer.flags (u_int)
44954359Sroberto */
45082498Sroberto#define	FLAG_CONFIG	0x0001	/* association was configured */
451290000Sglebius#define	FLAG_PREEMPT	0x0002	/* preemptable association */
45282498Sroberto#define	FLAG_AUTHENTIC	0x0004	/* last message was authentic */
453290000Sglebius#define	FLAG_REFCLOCK	0x0008	/* this is actually a reference clock */
454290000Sglebius#define	FLAG_BC_VOL	0x0010	/* broadcast client volleying */
455290000Sglebius#define	FLAG_PREFER	0x0020	/* prefer peer */
456290000Sglebius#define	FLAG_BURST	0x0040	/* burst mode */
457290000Sglebius#define	FLAG_PPS	0x0080	/* steered by PPS */
458290000Sglebius#define	FLAG_IBURST	0x0100	/* initial burst mode */
459290000Sglebius#define	FLAG_NOSELECT	0x0200	/* never select */
460290000Sglebius#define	FLAG_TRUE	0x0400	/* force truechimer */
461290000Sglebius#define	FLAG_SKEY	0x0800  /* autokey authentication */
462290000Sglebius#define	FLAG_XLEAVE	0x1000	/* interleaved protocol */
463290000Sglebius#define	FLAG_XB		0x2000	/* interleaved broadcast */
464290000Sglebius#define	FLAG_XBOGUS	0x4000	/* interleaved bogus packet */
465290000Sglebius#ifdef	OPENSSL
466290000Sglebius# define FLAG_ASSOC	0x8000	/* autokey request */
467290000Sglebius#endif /* OPENSSL */
468290000Sglebius#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))
477290000Sglebius#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 */
530290000Sglebius#define REFCLK_TSYNCPCI		45	/* Spectracom TSYNC PCI timing board */
531290000Sglebius#define REFCLK_GPSDJSON		46
532290000Sglebius#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 {
544290000Sglebius	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 */
548290000Sglebius	u_fp	rootdelay;	/* roundtrip delay to primary source */
549290000Sglebius	u_fp	rootdisp;	/* dispersion to primary source*/
550290000Sglebius	u_int32	refid;		/* reference id */
551290000Sglebius	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
556298770Sdelphij#define	MIN_V4_PKT_LEN	(12 * sizeof(u_int32)) /* min header length */
557290000Sglebius#define	LEN_PKT_NOMAC	(12 * sizeof(u_int32)) /* min header length */
558290000Sglebius#define MIN_MAC_LEN	(1 * sizeof(u_int32))	/* crypto_NAK */
559290000Sglebius#define MAX_MD5_LEN	(5 * sizeof(u_int32))	/* MD5 */
560290000Sglebius#define	MAX_MAC_LEN	(6 * sizeof(u_int32))	/* SHA */
56154359Sroberto
56254359Sroberto	/*
56354359Sroberto	 * The length of the packet less MAC must be a multiple of 64
564290000Sglebius	 * with an RSA modulus and Diffie-Hellman prime of 256 octets
56582498Sroberto	 * and maximum host name of 128 octets, the maximum autokey
56682498Sroberto	 * command is 152 octets and maximum autokey response is 460
56782498Sroberto	 * octets. A packet can contain no more than one command and one
568290000Sglebius	 * response, so the maximum total extension field length is 864
56982498Sroberto	 * octets. But, to handle humungus certificates, the bank must
57082498Sroberto	 * be broke.
571290000Sglebius	 *
572290000Sglebius	 * The different definitions of the 'exten' field are here for
573290000Sglebius	 * the benefit of applications that want to send a packet from
574290000Sglebius	 * an auto variable in the stack - not using the AUTOKEY version
575290000Sglebius	 * saves 2KB of stack space. The receive buffer should ALWAYS be
576290000Sglebius	 * big enough to hold a full extended packet if the extension
577290000Sglebius	 * fields have to be parsed or skipped.
57854359Sroberto	 */
579290000Sglebius#ifdef AUTOKEY
580290000Sglebius	u_int32	exten[(NTP_MAXEXTEN + MAX_MAC_LEN) / sizeof(u_int32)];
581290000Sglebius#else	/* !AUTOKEY follows */
582290000Sglebius	u_int32	exten[(MAX_MAC_LEN) / sizeof(u_int32)];
583290000Sglebius#endif	/* !AUTOKEY */
58454359Sroberto};
58554359Sroberto
58654359Sroberto/*
58754359Sroberto * Stuff for extracting things from li_vn_mode
58854359Sroberto */
58954359Sroberto#define	PKT_MODE(li_vn_mode)	((u_char)((li_vn_mode) & 0x7))
59054359Sroberto#define	PKT_VERSION(li_vn_mode)	((u_char)(((li_vn_mode) >> 3) & 0x7))
59154359Sroberto#define	PKT_LEAP(li_vn_mode)	((u_char)(((li_vn_mode) >> 6) & 0x3))
59254359Sroberto
59354359Sroberto/*
594290000Sglebius * Stuff for putting things back into li_vn_mode in packets and vn_mode
595290000Sglebius * in ntp_monitor.c's mon_entry.
59654359Sroberto */
597290000Sglebius#define VN_MODE(v, m)		((((v) & 7) << 3) | ((m) & 0x7))
598290000Sglebius#define	PKT_LI_VN_MODE(l, v, m) ((((l) & 3) << 6) | VN_MODE((v), (m)))
59954359Sroberto
60054359Sroberto
60154359Sroberto/*
60254359Sroberto * Dealing with stratum.  0 gets mapped to 16 incoming, and back to 0
60354359Sroberto * on output.
60454359Sroberto */
60554359Sroberto#define	PKT_TO_STRATUM(s)	((u_char)(((s) == (STRATUM_PKT_UNSPEC)) ?\
60654359Sroberto				(STRATUM_UNSPEC) : (s)))
60754359Sroberto
60854359Sroberto#define	STRATUM_TO_PKT(s)	((u_char)(((s) == (STRATUM_UNSPEC)) ?\
60954359Sroberto				(STRATUM_PKT_UNSPEC) : (s)))
61054359Sroberto
61182498Sroberto/*
61282498Sroberto * Event codes. Used for reporting errors/events to the control module
61382498Sroberto */
614132451Sroberto#define	PEER_EVENT	0x080	/* this is a peer event */
615132451Sroberto#define CRPT_EVENT	0x100	/* this is a crypto event */
61654359Sroberto
61754359Sroberto/*
61882498Sroberto * System event codes
61954359Sroberto */
62082498Sroberto#define	EVNT_UNSPEC	0	/* unspecified */
621290000Sglebius#define	EVNT_NSET	1	/* freq not set */
622290000Sglebius#define	EVNT_FSET	2	/* freq set */
623290000Sglebius#define	EVNT_SPIK	3	/* spike detect */
624290000Sglebius#define	EVNT_FREQ	4	/* freq mode */
625290000Sglebius#define	EVNT_SYNC	5	/* clock sync */
626290000Sglebius#define	EVNT_SYSRESTART	6	/* restart */
627290000Sglebius#define	EVNT_SYSFAULT	7	/* panic stop */
628290000Sglebius#define	EVNT_NOPEER	8	/* no sys peer */
629290000Sglebius#define	EVNT_ARMED	9	/* leap armed */
630290000Sglebius#define	EVNT_DISARMED	10	/* leap disarmed */
631290000Sglebius#define	EVNT_LEAP	11	/* leap event */
632290000Sglebius#define	EVNT_CLOCKRESET	12	/* clock step */
633290000Sglebius#define	EVNT_KERN	13	/* kernel event */
634290000Sglebius#define	EVNT_TAI	14	/* TAI */
635290000Sglebius#define	EVNT_LEAPVAL	15	/* stale leapsecond values */
63654359Sroberto
63782498Sroberto/*
63882498Sroberto * Peer event codes
63982498Sroberto */
640290000Sglebius#define	PEVNT_MOBIL	(1 | PEER_EVENT) /* mobilize */
641290000Sglebius#define	PEVNT_DEMOBIL	(2 | PEER_EVENT) /* demobilize */
642290000Sglebius#define	PEVNT_UNREACH	(3 | PEER_EVENT) /* unreachable */
643290000Sglebius#define	PEVNT_REACH	(4 | PEER_EVENT) /* reachable */
644290000Sglebius#define	PEVNT_RESTART	(5 | PEER_EVENT) /* restart */
645290000Sglebius#define	PEVNT_REPLY	(6 | PEER_EVENT) /* no reply */
646290000Sglebius#define	PEVNT_RATE	(7 | PEER_EVENT) /* rate exceeded */
647290000Sglebius#define	PEVNT_DENY	(8 | PEER_EVENT) /* access denied */
648290000Sglebius#define PEVNT_ARMED	(9 | PEER_EVENT) /* leap armed */
649290000Sglebius#define	PEVNT_NEWPEER	(10 | PEER_EVENT) /* sys peer */
650290000Sglebius#define	PEVNT_CLOCK	(11 | PEER_EVENT) /* clock event */
651290000Sglebius#define	PEVNT_AUTH	(12 | PEER_EVENT) /* bad auth */
652290000Sglebius#define	PEVNT_POPCORN	(13 | PEER_EVENT) /* popcorn */
653290000Sglebius#define	PEVNT_XLEAVE	(14 | PEER_EVENT) /* interleave mode */
654290000Sglebius#define	PEVNT_XERR	(15 | PEER_EVENT) /* interleave error */
65554359Sroberto
65654359Sroberto/*
65754359Sroberto * Clock event codes
65854359Sroberto */
65982498Sroberto#define	CEVNT_NOMINAL	0	/* unspecified */
660290000Sglebius#define	CEVNT_TIMEOUT	1	/* no reply */
661290000Sglebius#define	CEVNT_BADREPLY	2	/* bad format */
662290000Sglebius#define	CEVNT_FAULT	3	/* fault */
663290000Sglebius#define	CEVNT_PROP	4	/* bad signal */
664290000Sglebius#define	CEVNT_BADDATE	5	/* bad date */
665290000Sglebius#define	CEVNT_BADTIME	6	/* bad time */
66654359Sroberto#define CEVNT_MAX	CEVNT_BADTIME
66754359Sroberto
66854359Sroberto/*
66954359Sroberto * Very misplaced value.  Default port through which we send traps.
67054359Sroberto */
67154359Sroberto#define	TRAPPORT	18447
67254359Sroberto
67354359Sroberto
67454359Sroberto/*
67582498Sroberto * To speed lookups, peers are hashed by the low order bits of the
67682498Sroberto * remote IP address. These definitions relate to that.
67754359Sroberto */
678290000Sglebius#define	NTP_HASH_SIZE		128
679290000Sglebius#define	NTP_HASH_MASK		(NTP_HASH_SIZE-1)
680290000Sglebius#define	NTP_HASH_ADDR(src)	(sock_hash(src) & NTP_HASH_MASK)
68154359Sroberto
68254359Sroberto/*
68354359Sroberto * min, min3 and max.  Makes it easier to transliterate the spec without
68454359Sroberto * thinking about it.
68554359Sroberto */
68654359Sroberto#define	min(a,b)	(((a) < (b)) ? (a) : (b))
68754359Sroberto#define	max(a,b)	(((a) > (b)) ? (a) : (b))
68854359Sroberto#define	min3(a,b,c)	min(min((a),(b)), (c))
68954359Sroberto
69054359Sroberto
69154359Sroberto/*
69254359Sroberto * Configuration items.  These are for the protocol module (proto_config())
69354359Sroberto */
69454359Sroberto#define	PROTO_BROADCLIENT	1
69554359Sroberto#define	PROTO_PRECISION		2	/* (not used) */
69654359Sroberto#define	PROTO_AUTHENTICATE	3
69754359Sroberto#define	PROTO_BROADDELAY	4
69854359Sroberto#define	PROTO_AUTHDELAY		5	/* (not used) */
69954359Sroberto#define PROTO_MULTICAST_ADD	6
70054359Sroberto#define PROTO_MULTICAST_DEL	7
70154359Sroberto#define PROTO_NTP		8
70254359Sroberto#define PROTO_KERNEL		9
70354359Sroberto#define PROTO_MONITOR		10
70454359Sroberto#define PROTO_FILEGEN		11
70582498Sroberto#define	PROTO_PPS		12
70682498Sroberto#define PROTO_CAL		13
707132451Sroberto#define PROTO_MINCLOCK		14
708182007Sroberto#define	PROTO_MAXCLOCK		15
709182007Sroberto#define PROTO_MINSANE		16
710182007Sroberto#define PROTO_FLOOR		17
711182007Sroberto#define PROTO_CEILING		18
712182007Sroberto#define PROTO_COHORT		19
713182007Sroberto#define PROTO_CALLDELAY		20
714182007Sroberto#define PROTO_MINDISP		21
715182007Sroberto#define PROTO_MAXDIST		22
716290000Sglebius	/* available		23 */
717182007Sroberto#define	PROTO_MAXHOP		24
718182007Sroberto#define	PROTO_BEACON		25
719182007Sroberto#define	PROTO_ORPHAN		26
720290000Sglebius#define	PROTO_ORPHWAIT		27
721290000Sglebius#define	PROTO_MODE7		28
722294904Sdelphij#define	PROTO_UECRYPTO		29
723294904Sdelphij#define	PROTO_UECRYPTONAK	30
724294904Sdelphij#define	PROTO_UEDIGEST		31
725301301Sdelphij#define	PROTO_PCEDIGEST		32
726310419Sdelphij#define	PROTO_BCPOLLBSTEP	33
72754359Sroberto
72854359Sroberto/*
72954359Sroberto * Configuration items for the loop filter
73054359Sroberto */
731290000Sglebius#define	LOOP_DRIFTINIT		1	/* iniitialize frequency */
732290000Sglebius#define	LOOP_KERN_CLEAR		2	/* set initial frequency offset */
733290000Sglebius#define LOOP_MAX		3	/* set both step offsets */
734310419Sdelphij#define LOOP_MAX_BACK		4	/* set backward-step offset */
735290000Sglebius#define LOOP_MAX_FWD		5	/* set forward-step offset */
736290000Sglebius#define LOOP_PANIC		6	/* set panic offseet */
737290000Sglebius#define LOOP_PHI		7	/* set dispersion rate */
738290000Sglebius#define LOOP_MINSTEP		8	/* set step timeout */
739290000Sglebius#define LOOP_MINPOLL		9	/* set min poll interval (log2 s) */
740290000Sglebius#define LOOP_ALLAN		10	/* set minimum Allan intercept */
741290000Sglebius#define LOOP_HUFFPUFF		11	/* set huff-n'-puff filter length */
742290000Sglebius#define LOOP_FREQ		12	/* set initial frequency */
743290000Sglebius#define LOOP_CODEC		13	/* set audio codec frequency */
744290000Sglebius#define	LOOP_LEAP		14	/* insert leap after second 23:59 */
745290000Sglebius#define	LOOP_TICK		15	/* sim. low precision clock */
74654359Sroberto
74754359Sroberto/*
74854359Sroberto * Configuration items for the stats printer
74954359Sroberto */
75054359Sroberto#define	STATS_FREQ_FILE		1	/* configure drift file */
75154359Sroberto#define STATS_STATSDIR		2	/* directory prefix for stats files */
75254359Sroberto#define	STATS_PID_FILE		3	/* configure ntpd PID file */
753290000Sglebius#define	STATS_LEAP_FILE		4	/* configure ntpd leapseconds file */
75454359Sroberto
755132451Sroberto#define MJD_1900		15020	/* MJD for 1 Jan 1900 */
75654359Sroberto
75754359Sroberto/*
75854359Sroberto * Default parameters.  We use these in the absence of something better.
75954359Sroberto */
76054359Sroberto#define INADDR_NTP	0xe0000101	/* NTP multicast address 224.0.1.1 */
76182498Sroberto
76254359Sroberto/*
76354359Sroberto * Structure used optionally for monitoring when this is turned on.
76454359Sroberto */
765290000Sglebiustypedef struct mon_data	mon_entry;
76654359Srobertostruct mon_data {
767290000Sglebius	mon_entry *	hash_next;	/* next structure in hash list */
768290000Sglebius	DECL_DLIST_LINK(mon_entry, mru);/* MRU list link pointers */
769290000Sglebius	struct interface * lcladr;	/* address on which this arrived */
770290000Sglebius	l_fp		first;		/* first time seen */
771290000Sglebius	l_fp		last;		/* last time seen */
772290000Sglebius	int		leak;		/* leaky bucket accumulator */
773290000Sglebius	int		count;		/* total packet count */
774290000Sglebius	u_short		flags;		/* restrict flags */
775290000Sglebius	u_char		vn_mode;	/* packet mode & version */
776290000Sglebius	u_char		cast_flags;	/* flags MDF_?CAST */
777290000Sglebius	sockaddr_u	rmtadr;		/* address of remote host */
77854359Sroberto};
77954359Sroberto
78082498Sroberto/*
781290000Sglebius * Values for cast_flags in mon_entry and struct peer.  mon_entry uses
782290000Sglebius * only the first three, MDF_UCAST, MDF_MCAST, and MDF_BCAST.
78382498Sroberto */
784290000Sglebius#define	MDF_UCAST	0x01	/* unicast client */
785290000Sglebius#define	MDF_MCAST	0x02	/* multicast server */
786290000Sglebius#define	MDF_BCAST	0x04	/* broadcast server */
787290000Sglebius#define	MDF_POOL	0x08	/* pool client solicitor */
788290000Sglebius#define MDF_ACAST	0x10	/* manycast client solicitor */
789290000Sglebius#define	MDF_BCLNT	0x20	/* eph. broadcast/multicast client */
790290000Sglebius#define MDF_UCLNT	0x40	/* preemptible manycast or pool client */
79154359Sroberto/*
792290000Sglebius * In the context of struct peer in ntpd, three of the cast_flags bits
793290000Sglebius * represent configured associations which never receive packets, and
794290000Sglebius * whose reach is always 0: MDF_BCAST, MDF_MCAST, and MDF_ACAST.  The
795290000Sglebius * last can be argued as responses are received, but those responses do
796290000Sglebius * not affect the MDF_ACAST association's reach register, rather they
797290000Sglebius * (may) result in mobilizing ephemeral MDF_ACLNT associations.
798290000Sglebius */
799290000Sglebius#define MDF_TXONLY_MASK	(MDF_BCAST | MDF_MCAST | MDF_ACAST | MDF_POOL)
800290000Sglebius/*
801290000Sglebius * manycastclient-like solicitor association cast_flags bits
802290000Sglebius */
803290000Sglebius#define MDF_SOLICIT_MASK	(MDF_ACAST | MDF_POOL)
804290000Sglebius/*
80554359Sroberto * Values used with mon_enabled to indicate reason for enabling monitoring
80654359Sroberto */
807290000Sglebius#define MON_OFF		0x00		/* no monitoring */
808290000Sglebius#define MON_ON		0x01		/* monitoring explicitly enabled */
809290000Sglebius#define MON_RES		0x02		/* implicit monitoring for RES_LIMITED */
81054359Sroberto/*
81154359Sroberto * Structure used for restrictlist entries
81254359Sroberto */
813290000Sglebiustypedef struct res_addr4_tag {
814290000Sglebius	u_int32		addr;		/* IPv4 addr (host order) */
815290000Sglebius	u_int32		mask;		/* IPv4 mask (host order) */
816290000Sglebius} res_addr4;
81754359Sroberto
818290000Sglebiustypedef struct res_addr6_tag {
819290000Sglebius	struct in6_addr addr;		/* IPv6 addr (net order) */
820290000Sglebius	struct in6_addr mask;		/* IPv6 mask (net order) */
821290000Sglebius} res_addr6;
822290000Sglebius
823290000Sglebiustypedef struct restrict_u_tag	restrict_u;
824290000Sglebiusstruct restrict_u_tag {
825290000Sglebius	restrict_u *		link;	/* link to next entry */
826290000Sglebius	u_int32			count;	/* number of packets matched */
827290000Sglebius	u_short			flags;	/* accesslist flags */
828290000Sglebius	u_short			mflags;	/* match flags */
829290000Sglebius	u_long			expire;	/* valid until time */
830290000Sglebius	union {				/* variant starting here */
831290000Sglebius		res_addr4 v4;
832290000Sglebius		res_addr6 v6;
833290000Sglebius	} u;
834132451Sroberto};
835290000Sglebius#define	V4_SIZEOF_RESTRICT_U	(offsetof(restrict_u, u)	\
836290000Sglebius				 + sizeof(res_addr4))
837290000Sglebius#define	V6_SIZEOF_RESTRICT_U	(offsetof(restrict_u, u)	\
838290000Sglebius				 + sizeof(res_addr6))
839132451Sroberto
84054359Sroberto/*
84154359Sroberto * Access flags
84254359Sroberto */
843290000Sglebius#define	RES_IGNORE		0x0001	/* ignore packet */
844290000Sglebius#define	RES_DONTSERVE		0x0002	/* access denied */
845290000Sglebius#define	RES_DONTTRUST		0x0004	/* authentication required */
846290000Sglebius#define	RES_VERSION		0x0008	/* version mismatch */
847290000Sglebius#define	RES_NOPEER		0x0010	/* new association denied */
848290000Sglebius#define RES_LIMITED		0x0020	/* packet rate exceeded */
849132451Sroberto#define RES_FLAGS		(RES_IGNORE | RES_DONTSERVE |\
850132451Sroberto				    RES_DONTTRUST | RES_VERSION |\
851132451Sroberto				    RES_NOPEER | RES_LIMITED)
85254359Sroberto
853290000Sglebius#define	RES_NOQUERY		0x0040	/* mode 6/7 packet denied */
854290000Sglebius#define	RES_NOMODIFY		0x0080	/* mode 6/7 modify denied */
855290000Sglebius#define	RES_NOTRAP		0x0100	/* mode 6/7 set trap denied */
856290000Sglebius#define	RES_LPTRAP		0x0200	/* mode 6/7 low priority trap */
857132451Sroberto
858290000Sglebius#define	RES_KOD			0x0400	/* send kiss of death packet */
859290000Sglebius#define	RES_MSSNTP		0x0800	/* enable MS-SNTP authentication */
860290000Sglebius#define	RES_FLAKE		0x1000	/* flakeway - drop 10% */
861290000Sglebius#define	RES_NOMRULIST		0x2000	/* mode 6 mrulist denied */
862132451Sroberto
863290000Sglebius#define	RES_ALLFLAGS		(RES_FLAGS | RES_NOQUERY |	\
864290000Sglebius				 RES_NOMODIFY | RES_NOTRAP |	\
865290000Sglebius				 RES_LPTRAP | RES_KOD |		\
866290000Sglebius				 RES_MSSNTP | RES_FLAKE |	\
867290000Sglebius				 RES_NOMRULIST)
868132451Sroberto
86954359Sroberto/*
87054359Sroberto * Match flags
87154359Sroberto */
872290000Sglebius#define	RESM_INTERFACE		0x1000	/* this is an interface */
873290000Sglebius#define	RESM_NTPONLY		0x2000	/* match source port 123 */
874290000Sglebius#define RESM_SOURCE		0x4000	/* from "restrict source" */
87554359Sroberto
87654359Sroberto/*
87754359Sroberto * Restriction configuration ops
87854359Sroberto */
87954359Sroberto#define	RESTRICT_FLAGS		1	/* add flags to restrict entry */
88054359Sroberto#define	RESTRICT_UNFLAG		2	/* remove flags from restrict entry */
88154359Sroberto#define	RESTRICT_REMOVE		3	/* remove a restrict entry */
882290000Sglebius#define	RESTRICT_REMOVEIF	4	/* remove an interface restrict entry */
88354359Sroberto
88454359Sroberto/*
88554359Sroberto * Endpoint structure for the select algorithm
88654359Sroberto */
88754359Srobertostruct endpoint {
88854359Sroberto	double	val;			/* offset of endpoint */
88954359Sroberto	int	type;			/* interval entry/exit */
89054359Sroberto};
89154359Sroberto
89254359Sroberto/*
89354359Sroberto * Association matching AM[] return codes
89454359Sroberto */
895182007Sroberto#define AM_ERR		-1		/* error */
896182007Sroberto#define AM_NOMATCH	0		/* no match */
897290000Sglebius#define AM_PROCPKT	1		/* server/symmetric packet */
898290000Sglebius#define AM_BCST		2		/* broadcast packet */
899182007Sroberto#define AM_FXMIT	3		/* client packet */
900290000Sglebius#define AM_MANYCAST	4		/* manycast or pool */
901182007Sroberto#define AM_NEWPASS	5		/* new passive */
902182007Sroberto#define AM_NEWBCL	6		/* new broadcast */
903290000Sglebius#define AM_POSSBCL	7		/* discard broadcast */
90454359Sroberto
90554359Sroberto/* NetInfo configuration locations */
90654359Sroberto#ifdef HAVE_NETINFO
90754359Sroberto#define NETINFO_CONFIG_DIR "/config/ntp"
90854359Sroberto#endif
90954359Sroberto
910290000Sglebius/* ntpq -c mrulist rows per request limit in ntpd */
911290000Sglebius#define MRU_ROW_LIMIT	256
912290000Sglebius/* similar datagrams per response limit for ntpd */
913290000Sglebius#define MRU_FRAGS_LIMIT	128
91454359Sroberto#endif /* NTP_H */
915