xref: /freebsd/contrib/ntp/ntpd/ntp_monitor.c (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1c0b746e5SOllivier Robert /*
29c2daa00SOllivier Robert  * ntp_monitor - monitor ntpd statistics
3c0b746e5SOllivier Robert  */
4c0b746e5SOllivier Robert #ifdef HAVE_CONFIG_H
5c0b746e5SOllivier Robert # include <config.h>
6c0b746e5SOllivier Robert #endif
7c0b746e5SOllivier Robert 
8c0b746e5SOllivier Robert #include "ntpd.h"
9c0b746e5SOllivier Robert #include "ntp_io.h"
10c0b746e5SOllivier Robert #include "ntp_if.h"
112b15cb3dSCy Schubert #include "ntp_lists.h"
12c0b746e5SOllivier Robert #include "ntp_stdlib.h"
13ea906c41SOllivier Robert #include <ntp_random.h>
14c0b746e5SOllivier Robert 
15224ba2bdSOllivier Robert #include <stdio.h>
16224ba2bdSOllivier Robert #include <signal.h>
17224ba2bdSOllivier Robert #ifdef HAVE_SYS_IOCTL_H
18224ba2bdSOllivier Robert # include <sys/ioctl.h>
19224ba2bdSOllivier Robert #endif
20224ba2bdSOllivier Robert 
21c0b746e5SOllivier Robert /*
222b15cb3dSCy Schubert  * Record statistics based on source address, mode and version. The
232b15cb3dSCy Schubert  * receive procedure calls us with the incoming rbufp before it does
242b15cb3dSCy Schubert  * anything else. While at it, implement rate controls for inbound
252b15cb3dSCy Schubert  * traffic.
26c0b746e5SOllivier Robert  *
272b15cb3dSCy Schubert  * Each entry is doubly linked into two lists, a hash table and a most-
282b15cb3dSCy Schubert  * recently-used (MRU) list. When a packet arrives it is looked up in
299c2daa00SOllivier Robert  * the hash table. If found, the statistics are updated and the entry
309c2daa00SOllivier Robert  * relinked at the head of the MRU list. If not found, a new entry is
319c2daa00SOllivier Robert  * allocated, initialized and linked into both the hash table and at the
329c2daa00SOllivier Robert  * head of the MRU list.
33c0b746e5SOllivier Robert  *
349c2daa00SOllivier Robert  * Memory is usually allocated by grabbing a big chunk of new memory and
359c2daa00SOllivier Robert  * cutting it up into littler pieces. The exception to this when we hit
369c2daa00SOllivier Robert  * the memory limit. Then we free memory by grabbing entries off the
379c2daa00SOllivier Robert  * tail for the MRU list, unlinking from the hash table, and
38c0b746e5SOllivier Robert  * reinitializing.
39c0b746e5SOllivier Robert  *
402b15cb3dSCy Schubert  * INC_MONLIST is the default allocation granularity in entries.
412b15cb3dSCy Schubert  * INIT_MONLIST is the default initial allocation in entries.
42c0b746e5SOllivier Robert  */
432b15cb3dSCy Schubert #ifdef MONMEMINC		/* old name */
442b15cb3dSCy Schubert # define	INC_MONLIST	MONMEMINC
452b15cb3dSCy Schubert #elif !defined(INC_MONLIST)
462b15cb3dSCy Schubert # define	INC_MONLIST	(4 * 1024 / sizeof(mon_entry))
47c0b746e5SOllivier Robert #endif
482b15cb3dSCy Schubert #ifndef INIT_MONLIST
492b15cb3dSCy Schubert # define	INIT_MONLIST	(4 * 1024 / sizeof(mon_entry))
502b15cb3dSCy Schubert #endif
512b15cb3dSCy Schubert #ifndef MRU_MAXDEPTH_DEF
522b15cb3dSCy Schubert # define MRU_MAXDEPTH_DEF	(1024 * 1024 / sizeof(mon_entry))
53c0b746e5SOllivier Robert #endif
54c0b746e5SOllivier Robert 
55c0b746e5SOllivier Robert /*
56c0b746e5SOllivier Robert  * Hashing stuff
57c0b746e5SOllivier Robert  */
582b15cb3dSCy Schubert u_char	mon_hash_bits;
59c0b746e5SOllivier Robert 
60c0b746e5SOllivier Robert /*
612b15cb3dSCy Schubert  * Pointers to the hash table and the MRU list.  Memory for the hash
622b15cb3dSCy Schubert  * table is allocated only if monitoring is enabled.
63c0b746e5SOllivier Robert  */
642b15cb3dSCy Schubert mon_entry **	mon_hash;	/* MRU hash table */
652b15cb3dSCy Schubert mon_entry	mon_mru_list;	/* mru listhead */
669c2daa00SOllivier Robert 
67c0b746e5SOllivier Robert /*
682b15cb3dSCy Schubert  * List of free structures structures, and counters of in-use and total
69c0b746e5SOllivier Robert  * structures. The free structures are linked with the hash_next field.
70c0b746e5SOllivier Robert  */
712b15cb3dSCy Schubert static  mon_entry *mon_free;		/* free list or null if none */
722b15cb3dSCy Schubert 	u_int mru_alloc;		/* mru list + free list count */
732b15cb3dSCy Schubert 	u_int mru_entries;		/* mru list count */
742b15cb3dSCy Schubert 	u_int mru_peakentries;		/* highest mru_entries seen */
752b15cb3dSCy Schubert 	u_int mru_initalloc = INIT_MONLIST;/* entries to preallocate */
762b15cb3dSCy Schubert 	u_int mru_incalloc = INC_MONLIST;/* allocation batch factor */
772b15cb3dSCy Schubert static	u_int mon_mem_increments;	/* times called malloc() */
782b15cb3dSCy Schubert 
792b15cb3dSCy Schubert /*
802b15cb3dSCy Schubert  * Parameters of the RES_LIMITED restriction option. We define headway
812b15cb3dSCy Schubert  * as the idle time between packets. A packet is discarded if the
822b15cb3dSCy Schubert  * headway is less than the minimum, as well as if the average headway
832b15cb3dSCy Schubert  * is less than eight times the increment.
842b15cb3dSCy Schubert  */
85a466cc55SCy Schubert int	ntp_minpkt = NTP_MINPKT;	/* minimum seconds between */
86a466cc55SCy Schubert 					/* requests from a client */
87a466cc55SCy Schubert u_char	ntp_minpoll = NTP_MINPOLL;	/* minimum average log2 seconds */
88a466cc55SCy Schubert 					/* between client requests */
89c0b746e5SOllivier Robert 
90c0b746e5SOllivier Robert /*
91c0b746e5SOllivier Robert  * Initialization state.  We may be monitoring, we may not.  If
92c0b746e5SOllivier Robert  * we aren't, we may not even have allocated any memory yet.
93c0b746e5SOllivier Robert  */
942b15cb3dSCy Schubert 	u_int	mon_enabled;		/* enable switch */
952b15cb3dSCy Schubert 	u_int	mru_mindepth = 600;	/* preempt above this */
962b15cb3dSCy Schubert 	int	mru_maxage = 64;	/* for entries older than */
972b15cb3dSCy Schubert 	u_int	mru_maxdepth = 		/* MRU count hard limit */
982b15cb3dSCy Schubert 			MRU_MAXDEPTH_DEF;
992b15cb3dSCy Schubert 	int	mon_age = 3000;		/* preemption limit */
1002b15cb3dSCy Schubert 
1012b15cb3dSCy Schubert static	void		mon_getmoremem(void);
1022b15cb3dSCy Schubert static	void		remove_from_hash(mon_entry *);
1032b15cb3dSCy Schubert static	inline void	mon_free_entry(mon_entry *);
1042b15cb3dSCy Schubert static	inline void	mon_reclaim_entry(mon_entry *);
1052b15cb3dSCy Schubert 
106c0b746e5SOllivier Robert 
107c0b746e5SOllivier Robert /*
108c0b746e5SOllivier Robert  * init_mon - initialize monitoring global data
109c0b746e5SOllivier Robert  */
110c0b746e5SOllivier Robert void
111c0b746e5SOllivier Robert init_mon(void)
112c0b746e5SOllivier Robert {
113c0b746e5SOllivier Robert 	/*
114c0b746e5SOllivier Robert 	 * Don't do much of anything here.  We don't allocate memory
1152b15cb3dSCy Schubert 	 * until mon_start().
116c0b746e5SOllivier Robert 	 */
117c0b746e5SOllivier Robert 	mon_enabled = MON_OFF;
1182b15cb3dSCy Schubert 	INIT_DLIST(mon_mru_list, mru);
1192b15cb3dSCy Schubert }
120c0b746e5SOllivier Robert 
1212b15cb3dSCy Schubert 
1222b15cb3dSCy Schubert /*
1232b15cb3dSCy Schubert  * remove_from_hash - removes an entry from the address hash table and
1242b15cb3dSCy Schubert  *		      decrements mru_entries.
1252b15cb3dSCy Schubert  */
1262b15cb3dSCy Schubert static void
1272b15cb3dSCy Schubert remove_from_hash(
1282b15cb3dSCy Schubert 	mon_entry *mon
1292b15cb3dSCy Schubert 	)
1302b15cb3dSCy Schubert {
1312b15cb3dSCy Schubert 	u_int hash;
1322b15cb3dSCy Schubert 	mon_entry *punlinked;
1332b15cb3dSCy Schubert 
1342b15cb3dSCy Schubert 	mru_entries--;
1352b15cb3dSCy Schubert 	hash = MON_HASH(&mon->rmtadr);
1362b15cb3dSCy Schubert 	UNLINK_SLIST(punlinked, mon_hash[hash], mon, hash_next,
1372b15cb3dSCy Schubert 		     mon_entry);
1389034852cSGleb Smirnoff 	ENSURE(punlinked == mon);
1392b15cb3dSCy Schubert }
1402b15cb3dSCy Schubert 
1412b15cb3dSCy Schubert 
1422b15cb3dSCy Schubert static inline void
1432b15cb3dSCy Schubert mon_free_entry(
1442b15cb3dSCy Schubert 	mon_entry *m
1452b15cb3dSCy Schubert 	)
1462b15cb3dSCy Schubert {
1472b15cb3dSCy Schubert 	ZERO(*m);
1482b15cb3dSCy Schubert 	LINK_SLIST(mon_free, m, hash_next);
1492b15cb3dSCy Schubert }
1502b15cb3dSCy Schubert 
1512b15cb3dSCy Schubert 
1522b15cb3dSCy Schubert /*
1532b15cb3dSCy Schubert  * mon_reclaim_entry - Remove an entry from the MRU list and from the
1542b15cb3dSCy Schubert  *		       hash array, then zero-initialize it.  Indirectly
1552b15cb3dSCy Schubert  *		       decrements mru_entries.
1562b15cb3dSCy Schubert 
1572b15cb3dSCy Schubert  * The entry is prepared to be reused.  Before return, in
1582b15cb3dSCy Schubert  * remove_from_hash(), mru_entries is decremented.  It is the caller's
1592b15cb3dSCy Schubert  * responsibility to increment it again.
1602b15cb3dSCy Schubert  */
1612b15cb3dSCy Schubert static inline void
1622b15cb3dSCy Schubert mon_reclaim_entry(
1632b15cb3dSCy Schubert 	mon_entry *m
1642b15cb3dSCy Schubert 	)
1652b15cb3dSCy Schubert {
1662b15cb3dSCy Schubert 	DEBUG_INSIST(NULL != m);
1672b15cb3dSCy Schubert 
1682b15cb3dSCy Schubert 	UNLINK_DLIST(m, mru);
1692b15cb3dSCy Schubert 	remove_from_hash(m);
1702b15cb3dSCy Schubert 	ZERO(*m);
1712b15cb3dSCy Schubert }
1722b15cb3dSCy Schubert 
1732b15cb3dSCy Schubert 
1742b15cb3dSCy Schubert /*
1752b15cb3dSCy Schubert  * mon_getmoremem - get more memory and put it on the free list
1762b15cb3dSCy Schubert  */
1772b15cb3dSCy Schubert static void
1782b15cb3dSCy Schubert mon_getmoremem(void)
1792b15cb3dSCy Schubert {
1802b15cb3dSCy Schubert 	mon_entry *chunk;
1812b15cb3dSCy Schubert 	u_int entries;
1822b15cb3dSCy Schubert 
1832b15cb3dSCy Schubert 	entries = (0 == mon_mem_increments)
1842b15cb3dSCy Schubert 		      ? mru_initalloc
1852b15cb3dSCy Schubert 		      : mru_incalloc;
1862b15cb3dSCy Schubert 
1872b15cb3dSCy Schubert 	if (entries) {
188276da39aSCy Schubert 		chunk = eallocarray(entries, sizeof(*chunk));
1892b15cb3dSCy Schubert 		mru_alloc += entries;
1902b15cb3dSCy Schubert 		for (chunk += entries; entries; entries--)
1912b15cb3dSCy Schubert 			mon_free_entry(--chunk);
1922b15cb3dSCy Schubert 
1932b15cb3dSCy Schubert 		mon_mem_increments++;
1942b15cb3dSCy Schubert 	}
195c0b746e5SOllivier Robert }
196c0b746e5SOllivier Robert 
197c0b746e5SOllivier Robert 
198c0b746e5SOllivier Robert /*
199c0b746e5SOllivier Robert  * mon_start - start up the monitoring software
200c0b746e5SOllivier Robert  */
201c0b746e5SOllivier Robert void
202c0b746e5SOllivier Robert mon_start(
203c0b746e5SOllivier Robert 	int mode
204c0b746e5SOllivier Robert 	)
205c0b746e5SOllivier Robert {
2062b15cb3dSCy Schubert 	size_t octets;
2072b15cb3dSCy Schubert 	u_int min_hash_slots;
208c0b746e5SOllivier Robert 
2092b15cb3dSCy Schubert 	if (MON_OFF == mode)		/* MON_OFF is 0 */
2102b15cb3dSCy Schubert 		return;
2112b15cb3dSCy Schubert 	if (mon_enabled) {
212c0b746e5SOllivier Robert 		mon_enabled |= mode;
213c0b746e5SOllivier Robert 		return;
214c0b746e5SOllivier Robert 	}
2152b15cb3dSCy Schubert 	if (0 == mon_mem_increments)
216c0b746e5SOllivier Robert 		mon_getmoremem();
2172b15cb3dSCy Schubert 	/*
2182b15cb3dSCy Schubert 	 * Select the MRU hash table size to limit the average count
2192b15cb3dSCy Schubert 	 * per bucket at capacity (mru_maxdepth) to 8, if possible
2202b15cb3dSCy Schubert 	 * given our hash is limited to 16 bits.
2212b15cb3dSCy Schubert 	 */
2222b15cb3dSCy Schubert 	min_hash_slots = (mru_maxdepth / 8) + 1;
2232b15cb3dSCy Schubert 	mon_hash_bits = 0;
2242b15cb3dSCy Schubert 	while (min_hash_slots >>= 1)
2252b15cb3dSCy Schubert 		mon_hash_bits++;
2262b15cb3dSCy Schubert 	mon_hash_bits = max(4, mon_hash_bits);
2272b15cb3dSCy Schubert 	mon_hash_bits = min(16, mon_hash_bits);
2282b15cb3dSCy Schubert 	octets = sizeof(*mon_hash) * MON_HASH_SIZE;
2292b15cb3dSCy Schubert 	mon_hash = erealloc_zero(mon_hash, octets, 0);
230c0b746e5SOllivier Robert 
231c0b746e5SOllivier Robert 	mon_enabled = mode;
232c0b746e5SOllivier Robert }
233c0b746e5SOllivier Robert 
234c0b746e5SOllivier Robert 
235c0b746e5SOllivier Robert /*
236c0b746e5SOllivier Robert  * mon_stop - stop the monitoring software
237c0b746e5SOllivier Robert  */
238c0b746e5SOllivier Robert void
239c0b746e5SOllivier Robert mon_stop(
240c0b746e5SOllivier Robert 	int mode
241c0b746e5SOllivier Robert 	)
242c0b746e5SOllivier Robert {
2432b15cb3dSCy Schubert 	mon_entry *mon;
244c0b746e5SOllivier Robert 
2452b15cb3dSCy Schubert 	if (MON_OFF == mon_enabled)
246c0b746e5SOllivier Robert 		return;
247c0b746e5SOllivier Robert 	if ((mon_enabled & mode) == 0 || mode == MON_OFF)
248c0b746e5SOllivier Robert 		return;
249c0b746e5SOllivier Robert 
250c0b746e5SOllivier Robert 	mon_enabled &= ~mode;
251c0b746e5SOllivier Robert 	if (mon_enabled != MON_OFF)
252c0b746e5SOllivier Robert 		return;
253c0b746e5SOllivier Robert 
254c0b746e5SOllivier Robert 	/*
2552b15cb3dSCy Schubert 	 * Move everything on the MRU list to the free list quickly,
2562b15cb3dSCy Schubert 	 * without bothering to remove each from either the MRU list or
2572b15cb3dSCy Schubert 	 * the hash table.
258c0b746e5SOllivier Robert 	 */
2592b15cb3dSCy Schubert 	ITER_DLIST_BEGIN(mon_mru_list, mon, mru, mon_entry)
2602b15cb3dSCy Schubert 		mon_free_entry(mon);
2612b15cb3dSCy Schubert 	ITER_DLIST_END()
2622b15cb3dSCy Schubert 
2632b15cb3dSCy Schubert 	/* empty the MRU list and hash table. */
2642b15cb3dSCy Schubert 	mru_entries = 0;
2652b15cb3dSCy Schubert 	INIT_DLIST(mon_mru_list, mru);
2662b15cb3dSCy Schubert 	zero_mem(mon_hash, sizeof(*mon_hash) * MON_HASH_SIZE);
267c0b746e5SOllivier Robert }
268c0b746e5SOllivier Robert 
269c0b746e5SOllivier Robert 
2702b15cb3dSCy Schubert /*
2712b15cb3dSCy Schubert  * mon_clearinterface -- remove mru entries referring to a local address
2722b15cb3dSCy Schubert  *			 which is going away.
2732b15cb3dSCy Schubert  */
274ea906c41SOllivier Robert void
2752b15cb3dSCy Schubert mon_clearinterface(
2762b15cb3dSCy Schubert 	endpt *lcladr
2772b15cb3dSCy Schubert 	)
278ea906c41SOllivier Robert {
2792b15cb3dSCy Schubert 	mon_entry *mon;
280ea906c41SOllivier Robert 
2812b15cb3dSCy Schubert 	/* iterate mon over mon_mru_list */
2822b15cb3dSCy Schubert 	ITER_DLIST_BEGIN(mon_mru_list, mon, mru, mon_entry)
2832b15cb3dSCy Schubert 		if (mon->lcladr == lcladr) {
2842b15cb3dSCy Schubert 			/* remove from mru list */
2852b15cb3dSCy Schubert 			UNLINK_DLIST(mon, mru);
2862b15cb3dSCy Schubert 			/* remove from hash list, adjust mru_entries */
2872b15cb3dSCy Schubert 			remove_from_hash(mon);
2882b15cb3dSCy Schubert 			/* put on free list */
2892b15cb3dSCy Schubert 			mon_free_entry(mon);
290ea906c41SOllivier Robert 		}
2912b15cb3dSCy Schubert 	ITER_DLIST_END()
292ea906c41SOllivier Robert }
2932b15cb3dSCy Schubert 
294c0b746e5SOllivier Robert 
295c0b746e5SOllivier Robert /*
296c0b746e5SOllivier Robert  * ntp_monitor - record stats about this packet
297ea906c41SOllivier Robert  *
2982b15cb3dSCy Schubert  * Returns supplied restriction flags, with RES_LIMITED and RES_KOD
2992b15cb3dSCy Schubert  * cleared unless the packet should not be responded to normally
3002b15cb3dSCy Schubert  * (RES_LIMITED) and possibly should trigger a KoD response (RES_KOD).
3012b15cb3dSCy Schubert  * The returned flags are saved in the MRU entry, so that it reflects
3022b15cb3dSCy Schubert  * whether the last packet from that source triggered rate limiting,
3032b15cb3dSCy Schubert  * and if so, possible KoD response.  This implies you can not tell
3042b15cb3dSCy Schubert  * whether a given address is eligible for rate limiting/KoD from the
3052b15cb3dSCy Schubert  * monlist restrict bits, only whether or not the last packet triggered
3062b15cb3dSCy Schubert  * such responses.  ntpdc -c reslist lets you see whether RES_LIMITED
3072b15cb3dSCy Schubert  * or RES_KOD is lit for a particular address before ntp_monitor()'s
3082b15cb3dSCy Schubert  * typical dousing.
309c0b746e5SOllivier Robert  */
3102b15cb3dSCy Schubert u_short
311c0b746e5SOllivier Robert ntp_monitor(
3122b15cb3dSCy Schubert 	struct recvbuf *rbufp,
3132b15cb3dSCy Schubert 	u_short	flags
314c0b746e5SOllivier Robert 	)
315c0b746e5SOllivier Robert {
3162b15cb3dSCy Schubert 	l_fp		interval_fp;
3172b15cb3dSCy Schubert 	struct pkt *	pkt;
3182b15cb3dSCy Schubert 	mon_entry *	mon;
3192b15cb3dSCy Schubert 	mon_entry *	oldest;
3202b15cb3dSCy Schubert 	int		oldest_age;
3212b15cb3dSCy Schubert 	u_int		hash;
3222b15cb3dSCy Schubert 	u_short		restrict_mask;
3232b15cb3dSCy Schubert 	u_char		mode;
3242b15cb3dSCy Schubert 	u_char		version;
3252b15cb3dSCy Schubert 	int		interval;
3262b15cb3dSCy Schubert 	int		head;		/* headway increment */
3272b15cb3dSCy Schubert 	int		leak;		/* new headway */
3282b15cb3dSCy Schubert 	int		limit;		/* average threshold */
329c0b746e5SOllivier Robert 
3309034852cSGleb Smirnoff 	REQUIRE(rbufp != NULL);
3319034852cSGleb Smirnoff 
332*f5f40dd6SCy Schubert 	if (mon_enabled == MON_OFF) {
3332b15cb3dSCy Schubert 		return ~(RES_LIMITED | RES_KOD) & flags;
334*f5f40dd6SCy Schubert 	}
335c0b746e5SOllivier Robert 	pkt = &rbufp->recv_pkt;
3362b15cb3dSCy Schubert 	hash = MON_HASH(&rbufp->recv_srcadr);
337c0b746e5SOllivier Robert 	mode = PKT_MODE(pkt->li_vn_mode);
3382b15cb3dSCy Schubert 	version = PKT_VERSION(pkt->li_vn_mode);
3392b15cb3dSCy Schubert 	mon = mon_hash[hash];
340c0b746e5SOllivier Robert 
341c0b746e5SOllivier Robert 	/*
3422b15cb3dSCy Schubert 	 * We keep track of all traffic for a given IP in one entry,
3432b15cb3dSCy Schubert 	 * otherwise cron'ed ntpdate or similar evades RES_LIMITED.
3449c2daa00SOllivier Robert 	 */
3452b15cb3dSCy Schubert 
346*f5f40dd6SCy Schubert 	for (; mon != NULL; mon = mon->hash_next) {
347*f5f40dd6SCy Schubert 		if (SOCK_EQ(&mon->rmtadr, &rbufp->recv_srcadr)) {
3482b15cb3dSCy Schubert 			break;
349*f5f40dd6SCy Schubert 		}
350*f5f40dd6SCy Schubert 	}
3512b15cb3dSCy Schubert 	if (mon != NULL) {
3522b15cb3dSCy Schubert 		interval_fp = rbufp->recv_time;
3532b15cb3dSCy Schubert 		L_SUB(&interval_fp, &mon->last);
3542b15cb3dSCy Schubert 		/* add one-half second to round up */
3552b15cb3dSCy Schubert 		L_ADDUF(&interval_fp, 0x80000000);
3562b15cb3dSCy Schubert 		interval = interval_fp.l_i;
3572b15cb3dSCy Schubert 		mon->last = rbufp->recv_time;
3582b15cb3dSCy Schubert 		NSRCPORT(&mon->rmtadr) = NSRCPORT(&rbufp->recv_srcadr);
3592b15cb3dSCy Schubert 		mon->count++;
3602b15cb3dSCy Schubert 		restrict_mask = flags;
3612b15cb3dSCy Schubert 		mon->vn_mode = VN_MODE(version, mode);
3622b15cb3dSCy Schubert 
3632b15cb3dSCy Schubert 		/* Shuffle to the head of the MRU list. */
3642b15cb3dSCy Schubert 		UNLINK_DLIST(mon, mru);
3652b15cb3dSCy Schubert 		LINK_DLIST(mon_mru_list, mon, mru);
3669c2daa00SOllivier Robert 
3679c2daa00SOllivier Robert 		/*
3682b15cb3dSCy Schubert 		 * At this point the most recent arrival is first in the
3692b15cb3dSCy Schubert 		 * MRU list.  Decrease the counter by the headway, but
3702b15cb3dSCy Schubert 		 * not less than zero.
371c0b746e5SOllivier Robert 		 */
3722b15cb3dSCy Schubert 		mon->leak -= interval;
3732b15cb3dSCy Schubert 		mon->leak = max(0, mon->leak);
3742b15cb3dSCy Schubert 		head = 1 << ntp_minpoll;
3752b15cb3dSCy Schubert 		leak = mon->leak + head;
3762b15cb3dSCy Schubert 		limit = NTP_SHIFT * head;
3772b15cb3dSCy Schubert 
3782b15cb3dSCy Schubert 		DPRINTF(2, ("MRU: interval %d headway %d limit %d\n",
3792b15cb3dSCy Schubert 			    interval, leak, limit));
3802b15cb3dSCy Schubert 
3812b15cb3dSCy Schubert 		/*
3822b15cb3dSCy Schubert 		 * If the minimum and average thresholds are not
3832b15cb3dSCy Schubert 		 * exceeded, douse the RES_LIMITED and RES_KOD bits and
3842b15cb3dSCy Schubert 		 * increase the counter by the headway increment.  Note
3852b15cb3dSCy Schubert 		 * that we give a 1-s grace for the minimum threshold
3862b15cb3dSCy Schubert 		 * and a 2-s grace for the headway increment.  If one or
3872b15cb3dSCy Schubert 		 * both thresholds are exceeded and the old counter is
3882b15cb3dSCy Schubert 		 * less than the average threshold, set the counter to
3892b15cb3dSCy Schubert 		 * the average threshold plus the increment and leave
3902b15cb3dSCy Schubert 		 * the RES_LIMITED and RES_KOD bits lit. Otherwise,
3912b15cb3dSCy Schubert 		 * leave the counter alone and douse the RES_KOD bit.
392*f5f40dd6SCy Schubert 		 * This rate-limits the KoDs to no more often than the
393*f5f40dd6SCy Schubert 		 * average headway.
3942b15cb3dSCy Schubert 		 */
3952b15cb3dSCy Schubert 		if (interval + 1 >= ntp_minpkt && leak < limit) {
3962b15cb3dSCy Schubert 			mon->leak = leak - 2;
3972b15cb3dSCy Schubert 			restrict_mask &= ~(RES_LIMITED | RES_KOD);
398*f5f40dd6SCy Schubert 		} else if (mon->leak < limit) {
3992b15cb3dSCy Schubert 			mon->leak = limit + head;
400*f5f40dd6SCy Schubert 		} else {
4012b15cb3dSCy Schubert 			restrict_mask &= ~RES_KOD;
402*f5f40dd6SCy Schubert 		}
4032b15cb3dSCy Schubert 		mon->flags = restrict_mask;
4042b15cb3dSCy Schubert 
4052b15cb3dSCy Schubert 		return mon->flags;
406c0b746e5SOllivier Robert 	}
407c0b746e5SOllivier Robert 
408c0b746e5SOllivier Robert 	/*
409c0b746e5SOllivier Robert 	 * If we got here, this is the first we've heard of this
410c0b746e5SOllivier Robert 	 * guy.  Get him some memory, either from the free list
411c0b746e5SOllivier Robert 	 * or from the tail of the MRU list.
4122b15cb3dSCy Schubert 	 *
4132b15cb3dSCy Schubert 	 * The following ntp.conf "mru" knobs come into play determining
4142b15cb3dSCy Schubert 	 * the depth (or count) of the MRU list:
4152b15cb3dSCy Schubert 	 * - mru_mindepth ("mru mindepth") is a floor beneath which
4162b15cb3dSCy Schubert 	 *   entries are kept without regard to their age.  The
4172b15cb3dSCy Schubert 	 *   default is 600 which matches the longtime implementation
4182b15cb3dSCy Schubert 	 *   limit on the total number of entries.
4192b15cb3dSCy Schubert 	 * - mru_maxage ("mru maxage") is a ceiling on the age in
4202b15cb3dSCy Schubert 	 *   seconds of entries.  Entries older than this are
4212b15cb3dSCy Schubert 	 *   reclaimed once mon_mindepth is exceeded.  64s default.
4222b15cb3dSCy Schubert 	 *   Note that entries older than this can easily survive
4232b15cb3dSCy Schubert 	 *   as they are reclaimed only as needed.
4242b15cb3dSCy Schubert 	 * - mru_maxdepth ("mru maxdepth") is a hard limit on the
4252b15cb3dSCy Schubert 	 *   number of entries.
4262b15cb3dSCy Schubert 	 * - "mru maxmem" sets mru_maxdepth to the number of entries
4272b15cb3dSCy Schubert 	 *   which fit in the given number of kilobytes.  The default is
4282b15cb3dSCy Schubert 	 *   1024, or 1 megabyte.
4292b15cb3dSCy Schubert 	 * - mru_initalloc ("mru initalloc" sets the count of the
4302b15cb3dSCy Schubert 	 *   initial allocation of MRU entries.
4312b15cb3dSCy Schubert 	 * - "mru initmem" sets mru_initalloc in units of kilobytes.
4322b15cb3dSCy Schubert 	 *   The default is 4.
4332b15cb3dSCy Schubert 	 * - mru_incalloc ("mru incalloc" sets the number of entries to
4342b15cb3dSCy Schubert 	 *   allocate on-demand each time the free list is empty.
4352b15cb3dSCy Schubert 	 * - "mru incmem" sets mru_incalloc in units of kilobytes.
4362b15cb3dSCy Schubert 	 *   The default is 4.
4372b15cb3dSCy Schubert 	 * Whichever of "mru maxmem" or "mru maxdepth" occurs last in
4382b15cb3dSCy Schubert 	 * ntp.conf controls.  Similarly for "mru initalloc" and "mru
4392b15cb3dSCy Schubert 	 * initmem", and for "mru incalloc" and "mru incmem".
440c0b746e5SOllivier Robert 	 */
4412b15cb3dSCy Schubert 	if (mru_entries < mru_mindepth) {
4422b15cb3dSCy Schubert 		if (NULL == mon_free)
4439c2daa00SOllivier Robert 			mon_getmoremem();
4442b15cb3dSCy Schubert 		UNLINK_HEAD_SLIST(mon, mon_free, hash_next);
4452b15cb3dSCy Schubert 	} else {
4462b15cb3dSCy Schubert 		oldest = TAIL_DLIST(mon_mru_list, mru);
4472b15cb3dSCy Schubert 		oldest_age = 0;		/* silence uninit warning */
4482b15cb3dSCy Schubert 		if (oldest != NULL) {
4492b15cb3dSCy Schubert 			interval_fp = rbufp->recv_time;
4502b15cb3dSCy Schubert 			L_SUB(&interval_fp, &oldest->last);
4512b15cb3dSCy Schubert 			/* add one-half second to round up */
4522b15cb3dSCy Schubert 			L_ADDUF(&interval_fp, 0x80000000);
4532b15cb3dSCy Schubert 			oldest_age = interval_fp.l_i;
4542b15cb3dSCy Schubert 		}
4552b15cb3dSCy Schubert 		/* note -1 is legal for mru_maxage (disables) */
4562b15cb3dSCy Schubert 		if (oldest != NULL && mru_maxage < oldest_age) {
4572b15cb3dSCy Schubert 			mon_reclaim_entry(oldest);
4582b15cb3dSCy Schubert 			mon = oldest;
4592b15cb3dSCy Schubert 		} else if (mon_free != NULL || mru_alloc <
4602b15cb3dSCy Schubert 			   mru_maxdepth) {
4612b15cb3dSCy Schubert 			if (NULL == mon_free)
4622b15cb3dSCy Schubert 				mon_getmoremem();
4632b15cb3dSCy Schubert 			UNLINK_HEAD_SLIST(mon, mon_free, hash_next);
4642b15cb3dSCy Schubert 		/* Preempt from the MRU list if old enough. */
465a466cc55SCy Schubert 		} else if (ntp_uurandom() >
4662b15cb3dSCy Schubert 			   (double)oldest_age / mon_age) {
4672b15cb3dSCy Schubert 			return ~(RES_LIMITED | RES_KOD) & flags;
4682b15cb3dSCy Schubert 		} else {
4692b15cb3dSCy Schubert 			mon_reclaim_entry(oldest);
4702b15cb3dSCy Schubert 			mon = oldest;
4712b15cb3dSCy Schubert 		}
472c0b746e5SOllivier Robert 	}
473c0b746e5SOllivier Robert 
4749034852cSGleb Smirnoff 	INSIST(mon != NULL);
4759034852cSGleb Smirnoff 
476c0b746e5SOllivier Robert 	/*
477c0b746e5SOllivier Robert 	 * Got one, initialize it
478c0b746e5SOllivier Robert 	 */
4792b15cb3dSCy Schubert 	mru_entries++;
4802b15cb3dSCy Schubert 	mru_peakentries = max(mru_peakentries, mru_entries);
4812b15cb3dSCy Schubert 	mon->last = rbufp->recv_time;
4822b15cb3dSCy Schubert 	mon->first = mon->last;
4832b15cb3dSCy Schubert 	mon->count = 1;
4842b15cb3dSCy Schubert 	mon->flags = ~(RES_LIMITED | RES_KOD) & flags;
4852b15cb3dSCy Schubert 	mon->leak = 0;
4862b15cb3dSCy Schubert 	memcpy(&mon->rmtadr, &rbufp->recv_srcadr, sizeof(mon->rmtadr));
4872b15cb3dSCy Schubert 	mon->vn_mode = VN_MODE(version, mode);
4882b15cb3dSCy Schubert 	mon->lcladr = rbufp->dstadr;
4892b15cb3dSCy Schubert 	mon->cast_flags = (u_char)(((rbufp->dstadr->flags &
4902b15cb3dSCy Schubert 	    INT_MCASTOPEN) && rbufp->fd == mon->lcladr->fd) ? MDF_MCAST
4912b15cb3dSCy Schubert 	    : rbufp->fd == mon->lcladr->bfd ? MDF_BCAST : MDF_UCAST);
492c0b746e5SOllivier Robert 
493c0b746e5SOllivier Robert 	/*
4949c2daa00SOllivier Robert 	 * Drop him into front of the hash table. Also put him on top of
4959c2daa00SOllivier Robert 	 * the MRU list.
496c0b746e5SOllivier Robert 	 */
4972b15cb3dSCy Schubert 	LINK_SLIST(mon_hash[hash], mon, hash_next);
4982b15cb3dSCy Schubert 	LINK_DLIST(mon_mru_list, mon, mru);
4992b15cb3dSCy Schubert 
5002b15cb3dSCy Schubert 	return mon->flags;
501c0b746e5SOllivier Robert }
502c0b746e5SOllivier Robert 
503c0b746e5SOllivier Robert 
504