]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - contrib/ntp/ntpd/ntp_proto.c
Fix multiple vulnerabilities of ntp. [SA-17:03]
[FreeBSD/releng/10.3.git] / contrib / ntp / ntpd / ntp_proto.c
1 /*
2  * ntp_proto.c - NTP version 4 protocol machinery
3  *
4  * ATTENTION: Get approval from Dave Mills on all changes to this file!
5  *
6  */
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include "ntpd.h"
12 #include "ntp_stdlib.h"
13 #include "ntp_unixtime.h"
14 #include "ntp_control.h"
15 #include "ntp_string.h"
16 #include "ntp_leapsec.h"
17 #include "refidsmear.h"
18 #include "lib_strbuf.h"
19
20 #include <stdio.h>
21 #ifdef HAVE_LIBSCF_H
22 #include <libscf.h>
23 #endif
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 /* [Bug 3031] define automatic broadcastdelay cutoff preset */
29 #ifndef BDELAY_DEFAULT
30 # define BDELAY_DEFAULT (-0.050)
31 #endif
32
33 /*
34  * This macro defines the authentication state. If x is 1 authentication
35  * is required; othewise it is optional.
36  */
37 #define AUTH(x, y)      ((x) ? (y) == AUTH_OK \
38                              : (y) == AUTH_OK || (y) == AUTH_NONE)
39
40 #define AUTH_NONE       0       /* authentication not required */
41 #define AUTH_OK         1       /* authentication OK */
42 #define AUTH_ERROR      2       /* authentication error */
43 #define AUTH_CRYPTO     3       /* crypto_NAK */
44
45 /*
46  * Set up Kiss Code values
47  */
48
49 enum kiss_codes {
50         NOKISS,                         /* No Kiss Code */
51         RATEKISS,                       /* Rate limit Kiss Code */
52         DENYKISS,                       /* Deny Kiss */
53         RSTRKISS,                       /* Restricted Kiss */
54         XKISS,                          /* Experimental Kiss */
55         UNKNOWNKISS                     /* Unknown Kiss Code */
56 };
57
58 enum nak_error_codes {
59         NONAK,                          /* No NAK seen */
60         INVALIDNAK,                     /* NAK cannot be used */
61         VALIDNAK                        /* NAK is valid */
62 };
63
64 /*
65  * traffic shaping parameters
66  */
67 #define NTP_IBURST      6       /* packets in iburst */
68 #define RESP_DELAY      1       /* refclock burst delay (s) */
69
70 /*
71  * pool soliciting restriction duration (s)
72  */
73 #define POOL_SOLICIT_WINDOW     8
74
75 /*
76  * peer_select groups statistics for a peer used by clock_select() and
77  * clock_cluster().
78  */
79 typedef struct peer_select_tag {
80         struct peer *   peer;
81         double          synch;  /* sync distance */
82         double          error;  /* jitter */
83         double          seljit; /* selection jitter */
84 } peer_select;
85
86 /*
87  * System variables are declared here. Unless specified otherwise, all
88  * times are in seconds.
89  */
90 u_char  sys_leap;               /* system leap indicator, use set_sys_leap() to change this */
91 u_char  xmt_leap;               /* leap indicator sent in client requests, set up by set_sys_leap() */
92 u_char  sys_stratum;            /* system stratum */
93 s_char  sys_precision;          /* local clock precision (log2 s) */
94 double  sys_rootdelay;          /* roundtrip delay to primary source */
95 double  sys_rootdisp;           /* dispersion to primary source */
96 u_int32 sys_refid;              /* reference id (network byte order) */
97 l_fp    sys_reftime;            /* last update time */
98 struct  peer *sys_peer;         /* current peer */
99
100 #ifdef LEAP_SMEAR
101 struct leap_smear_info leap_smear;
102 #endif
103 int leap_sec_in_progress;
104
105 /*
106  * Rate controls. Leaky buckets are used to throttle the packet
107  * transmission rates in order to protect busy servers such as at NIST
108  * and USNO. There is a counter for each association and another for KoD
109  * packets. The association counter decrements each second, but not
110  * below zero. Each time a packet is sent the counter is incremented by
111  * a configurable value representing the average interval between
112  * packets. A packet is delayed as long as the counter is greater than
113  * zero. Note this does not affect the time value computations.
114  */
115 /*
116  * Nonspecified system state variables
117  */
118 int     sys_bclient;            /* broadcast client enable */
119 double  sys_bdelay;             /* broadcast client default delay */
120 int     sys_authenticate;       /* requre authentication for config */
121 l_fp    sys_authdelay;          /* authentication delay */
122 double  sys_offset;     /* current local clock offset */
123 double  sys_mindisp = MINDISPERSE; /* minimum distance (s) */
124 double  sys_maxdist = MAXDISTANCE; /* selection threshold */
125 double  sys_jitter;             /* system jitter */
126 u_long  sys_epoch;              /* last clock update time */
127 static  double sys_clockhop;    /* clockhop threshold */
128 static int leap_vote_ins;       /* leap consensus for insert */
129 static int leap_vote_del;       /* leap consensus for delete */
130 keyid_t sys_private;            /* private value for session seed */
131 int     sys_manycastserver;     /* respond to manycast client pkts */
132 int     ntp_mode7;              /* respond to ntpdc (mode7) */
133 int     peer_ntpdate;           /* active peers in ntpdate mode */
134 int     sys_survivors;          /* truest of the truechimers */
135 char    *sys_ident = NULL;      /* identity scheme */
136
137 /*
138  * TOS and multicast mapping stuff
139  */
140 int     sys_floor = 0;          /* cluster stratum floor */
141 u_char  sys_bcpollbstep = 0;    /* Broadcast Poll backstep gate */
142 int     sys_ceiling = STRATUM_UNSPEC - 1; /* cluster stratum ceiling */
143 int     sys_minsane = 1;        /* minimum candidates */
144 int     sys_minclock = NTP_MINCLOCK; /* minimum candidates */
145 int     sys_maxclock = NTP_MAXCLOCK; /* maximum candidates */
146 int     sys_cohort = 0;         /* cohort switch */
147 int     sys_orphan = STRATUM_UNSPEC + 1; /* orphan stratum */
148 int     sys_orphwait = NTP_ORPHWAIT; /* orphan wait */
149 int     sys_beacon = BEACON;    /* manycast beacon interval */
150 u_int   sys_ttlmax;             /* max ttl mapping vector index */
151 u_char  sys_ttl[MAX_TTL];       /* ttl mapping vector */
152
153 /*
154  * Statistics counters - first the good, then the bad
155  */
156 u_long  sys_stattime;           /* elapsed time */
157 u_long  sys_received;           /* packets received */
158 u_long  sys_processed;          /* packets for this host */
159 u_long  sys_newversion;         /* current version */
160 u_long  sys_oldversion;         /* old version */
161 u_long  sys_restricted;         /* access denied */
162 u_long  sys_badlength;          /* bad length or format */
163 u_long  sys_badauth;            /* bad authentication */
164 u_long  sys_declined;           /* declined */
165 u_long  sys_limitrejected;      /* rate exceeded */
166 u_long  sys_kodsent;            /* KoD sent */
167
168 /*
169  * Mechanism knobs: how soon do we peer_clear() or unpeer()?
170  *
171  * The default way is "on-receipt".  If this was a packet from a
172  * well-behaved source, on-receipt will offer the fastest recovery.
173  * If this was from a DoS attack, the default way makes it easier
174  * for a bad-guy to DoS us.  So look and see what bites you harder
175  * and choose according to your environment.
176  */
177 int peer_clear_digest_early     = 1;    /* bad digest (TEST5) and Autokey */
178 int unpeer_crypto_early         = 1;    /* bad crypto (TEST9) */
179 int unpeer_crypto_nak_early     = 1;    /* crypto_NAK (TEST5) */
180 int unpeer_digest_early         = 1;    /* bad digest (TEST5) */
181
182 int dynamic_interleave = DYNAMIC_INTERLEAVE;    /* Bug 2978 mitigation */
183
184 int kiss_code_check(u_char hisleap, u_char hisstratum, u_char hismode, u_int32 refid);
185 enum nak_error_codes valid_NAK(struct peer *peer, struct recvbuf *rbufp, u_char hismode);
186 static  double  root_distance   (struct peer *);
187 static  void    clock_combine   (peer_select *, int, int);
188 static  void    peer_xmit       (struct peer *);
189 static  void    fast_xmit       (struct recvbuf *, int, keyid_t, int);
190 static  void    pool_xmit       (struct peer *);
191 static  void    clock_update    (struct peer *);
192 static  void    measure_precision(void);
193 static  double  measure_tick_fuzz(void);
194 static  int     local_refid     (struct peer *);
195 static  int     peer_unfit      (struct peer *);
196 #ifdef AUTOKEY
197 static  int     group_test      (char *, char *);
198 #endif /* AUTOKEY */
199 #ifdef WORKER
200 void    pool_name_resolved      (int, int, void *, const char *,
201                                  const char *, const struct addrinfo *,
202                                  const struct addrinfo *);
203 #endif /* WORKER */
204
205 const char *    amtoa           (int am);
206
207
208 void
209 set_sys_leap(
210         u_char new_sys_leap
211         )
212 {
213         sys_leap = new_sys_leap;
214         xmt_leap = sys_leap;
215
216         /*
217          * Under certain conditions we send faked leap bits to clients, so
218          * eventually change xmt_leap below, but never change LEAP_NOTINSYNC.
219          */
220         if (xmt_leap != LEAP_NOTINSYNC) {
221                 if (leap_sec_in_progress) {
222                         /* always send "not sync" */
223                         xmt_leap = LEAP_NOTINSYNC;
224                 }
225 #ifdef LEAP_SMEAR
226                 else {
227                         /*
228                          * If leap smear is enabled in general we must
229                          * never send a leap second warning to clients,
230                          * so make sure we only send "in sync".
231                          */
232                         if (leap_smear.enabled)
233                                 xmt_leap = LEAP_NOWARNING;
234                 }
235 #endif  /* LEAP_SMEAR */
236         }
237 }
238
239
240 /*
241  * Kiss Code check
242  */
243 int
244 kiss_code_check(
245         u_char hisleap,
246         u_char hisstratum,
247         u_char hismode,
248         u_int32 refid
249         )
250 {
251
252         if (   hismode == MODE_SERVER
253             && hisleap == LEAP_NOTINSYNC
254             && hisstratum == STRATUM_UNSPEC) {
255                 if(memcmp(&refid,"RATE", 4) == 0) {
256                         return (RATEKISS);
257                 } else if(memcmp(&refid,"DENY", 4) == 0) {
258                         return (DENYKISS);
259                 } else if(memcmp(&refid,"RSTR", 4) == 0) {
260                         return (RSTRKISS);
261                 } else if(memcmp(&refid,"X", 1) == 0) {
262                         return (XKISS);
263                 } else {
264                         return (UNKNOWNKISS);
265                 }
266         } else {
267                 return (NOKISS);
268         }
269 }
270
271
272 /* 
273  * Check that NAK is valid
274  */
275 enum nak_error_codes
276 valid_NAK(
277           struct peer *peer,
278           struct recvbuf *rbufp,
279           u_char hismode
280           )
281 {
282         int             base_packet_length = MIN_V4_PKT_LEN;
283         int             remainder_size;
284         struct pkt *    rpkt;
285         int             keyid;
286         l_fp            p_org;  /* origin timestamp */
287         const l_fp *    myorg;  /* selected peer origin */
288
289         /*
290          * Check to see if there is something beyond the basic packet
291          */
292         if (rbufp->recv_length == base_packet_length) {
293                 return NONAK;
294         }
295
296         remainder_size = rbufp->recv_length - base_packet_length;
297         /*
298          * Is this a potential NAK?
299          */
300         if (remainder_size != 4) {
301                 return NONAK;
302         }
303
304         /*
305          * Only server responses can contain NAK's
306          */
307
308         if (hismode != MODE_SERVER &&
309             hismode != MODE_ACTIVE &&
310             hismode != MODE_PASSIVE
311             ) {
312                 return INVALIDNAK;
313         }
314
315         /* 
316          * Make sure that the extra field in the packet is all zeros
317          */
318         rpkt = &rbufp->recv_pkt;
319         keyid = ntohl(((u_int32 *)rpkt)[base_packet_length / 4]);
320         if (keyid != 0) {
321                 return INVALIDNAK;
322         }
323
324         /* 
325          * Only valid if peer uses a key
326          */
327         if (!peer || !peer->keyid || !(peer->flags & FLAG_SKEY)) {
328                 return INVALIDNAK;
329         }
330
331         /*
332          * The ORIGIN must match, or this cannot be a valid NAK, either.
333          */
334         NTOHL_FP(&rpkt->org, &p_org);
335         if (peer->flip > 0)
336                 myorg = &peer->borg;
337         else
338                 myorg = &peer->aorg;
339
340         if (L_ISZERO(&p_org) ||
341             L_ISZERO( myorg) ||
342             !L_ISEQU(&p_org, myorg)) {
343                 return INVALIDNAK;
344         }
345
346         /* If we ever passed all that checks, we should be safe. Well,
347          * as safe as we can ever be with an unauthenticated crypto-nak.
348          */
349         return VALIDNAK;
350 }
351
352
353 /*
354  * transmit - transmit procedure called by poll timeout
355  */
356 void
357 transmit(
358         struct peer *peer       /* peer structure pointer */
359         )
360 {
361         u_char  hpoll;
362
363         /*
364          * The polling state machine. There are two kinds of machines,
365          * those that never expect a reply (broadcast and manycast
366          * server modes) and those that do (all other modes). The dance
367          * is intricate...
368          */
369         hpoll = peer->hpoll;
370
371         /*
372          * In broadcast mode the poll interval is never changed from
373          * minpoll.
374          */
375         if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) {
376                 peer->outdate = current_time;
377                 if (sys_leap != LEAP_NOTINSYNC)
378                         peer_xmit(peer);
379                 poll_update(peer, hpoll);
380                 return;
381         }
382
383         /*
384          * In manycast mode we start with unity ttl. The ttl is
385          * increased by one for each poll until either sys_maxclock
386          * servers have been found or the maximum ttl is reached. When
387          * sys_maxclock servers are found we stop polling until one or
388          * more servers have timed out or until less than sys_minclock
389          * associations turn up. In this case additional better servers
390          * are dragged in and preempt the existing ones.  Once every
391          * sys_beacon seconds we are to transmit unconditionally, but
392          * this code is not quite right -- peer->unreach counts polls
393          * and is being compared with sys_beacon, so the beacons happen
394          * every sys_beacon polls.
395          */
396         if (peer->cast_flags & MDF_ACAST) {
397                 peer->outdate = current_time;
398                 if (peer->unreach > sys_beacon) {
399                         peer->unreach = 0;
400                         peer->ttl = 0;
401                         peer_xmit(peer);
402                 } else if (   sys_survivors < sys_minclock
403                            || peer_associations < sys_maxclock) {
404                         if (peer->ttl < sys_ttlmax)
405                                 peer->ttl++;
406                         peer_xmit(peer);
407                 }
408                 peer->unreach++;
409                 poll_update(peer, hpoll);
410                 return;
411         }
412
413         /*
414          * Pool associations transmit unicast solicitations when there
415          * are less than a hard limit of 2 * sys_maxclock associations,
416          * and either less than sys_minclock survivors or less than
417          * sys_maxclock associations.  The hard limit prevents unbounded
418          * growth in associations if the system clock or network quality
419          * result in survivor count dipping below sys_minclock often.
420          * This was observed testing with pool, where sys_maxclock == 12
421          * resulted in 60 associations without the hard limit.  A
422          * similar hard limit on manycastclient ephemeral associations
423          * may be appropriate.
424          */
425         if (peer->cast_flags & MDF_POOL) {
426                 peer->outdate = current_time;
427                 if (   (peer_associations <= 2 * sys_maxclock)
428                     && (   peer_associations < sys_maxclock
429                         || sys_survivors < sys_minclock))
430                         pool_xmit(peer);
431                 poll_update(peer, hpoll);
432                 return;
433         }
434
435         /*
436          * In unicast modes the dance is much more intricate. It is
437          * designed to back off whenever possible to minimize network
438          * traffic.
439          */
440         if (peer->burst == 0) {
441                 u_char oreach;
442
443                 /*
444                  * Update the reachability status. If not heard for
445                  * three consecutive polls, stuff infinity in the clock
446                  * filter.
447                  */
448                 oreach = peer->reach;
449                 peer->outdate = current_time;
450                 peer->unreach++;
451                 peer->reach <<= 1;
452                 if (!peer->reach) {
453
454                         /*
455                          * Here the peer is unreachable. If it was
456                          * previously reachable raise a trap. Send a
457                          * burst if enabled.
458                          */
459                         clock_filter(peer, 0., 0., MAXDISPERSE);
460                         if (oreach) {
461                                 peer_unfit(peer);
462                                 report_event(PEVNT_UNREACH, peer, NULL);
463                         }
464                         if (   (peer->flags & FLAG_IBURST)
465                             && peer->retry == 0)
466                                 peer->retry = NTP_RETRY;
467                 } else {
468
469                         /*
470                          * Here the peer is reachable. Send a burst if
471                          * enabled and the peer is fit.  Reset unreach
472                          * for persistent and ephemeral associations.
473                          * Unreach is also reset for survivors in
474                          * clock_select().
475                          */
476                         hpoll = sys_poll;
477                         if (!(peer->flags & FLAG_PREEMPT))
478                                 peer->unreach = 0;
479                         if (   (peer->flags & FLAG_BURST)
480                             && peer->retry == 0
481                             && !peer_unfit(peer))
482                                 peer->retry = NTP_RETRY;
483                 }
484
485                 /*
486                  * Watch for timeout.  If ephemeral, toss the rascal;
487                  * otherwise, bump the poll interval. Note the
488                  * poll_update() routine will clamp it to maxpoll.
489                  * If preemptible and we have more peers than maxclock,
490                  * and this peer has the minimum score of preemptibles,
491                  * demobilize.
492                  */
493                 if (peer->unreach >= NTP_UNREACH) {
494                         hpoll++;
495                         /* ephemeral: no FLAG_CONFIG nor FLAG_PREEMPT */
496                         if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
497                                 report_event(PEVNT_RESTART, peer, "timeout");
498                                 peer_clear(peer, "TIME");
499                                 unpeer(peer);
500                                 return;
501                         }
502                         if (   (peer->flags & FLAG_PREEMPT)
503                             && (peer_associations > sys_maxclock)
504                             && score_all(peer)) {
505                                 report_event(PEVNT_RESTART, peer, "timeout");
506                                 peer_clear(peer, "TIME");
507                                 unpeer(peer);
508                                 return;
509                         }
510                 }
511         } else {
512                 peer->burst--;
513                 if (peer->burst == 0) {
514
515                         /*
516                          * If ntpdate mode and the clock has not been
517                          * set and all peers have completed the burst,
518                          * we declare a successful failure.
519                          */
520                         if (mode_ntpdate) {
521                                 peer_ntpdate--;
522                                 if (peer_ntpdate == 0) {
523                                         msyslog(LOG_NOTICE,
524                                             "ntpd: no servers found");
525                                         if (!msyslog_term)
526                                                 printf(
527                                                     "ntpd: no servers found\n");
528                                         exit (0);
529                                 }
530                         }
531                 }
532         }
533         if (peer->retry > 0)
534                 peer->retry--;
535
536         /*
537          * Do not transmit if in broadcast client mode.
538          */
539         if (peer->hmode != MODE_BCLIENT)
540                 peer_xmit(peer);
541         poll_update(peer, hpoll);
542
543         return;
544 }
545
546
547 const char *
548 amtoa(
549         int am
550         )
551 {
552         char *bp;
553
554         switch(am) {
555             case AM_ERR:        return "AM_ERR";
556             case AM_NOMATCH:    return "AM_NOMATCH";
557             case AM_PROCPKT:    return "AM_PROCPKT";
558             case AM_BCST:       return "AM_BCST";
559             case AM_FXMIT:      return "AM_FXMIT";
560             case AM_MANYCAST:   return "AM_MANYCAST";
561             case AM_NEWPASS:    return "AM_NEWPASS";
562             case AM_NEWBCL:     return "AM_NEWBCL";
563             case AM_POSSBCL:    return "AM_POSSBCL";
564             default:
565                 LIB_GETBUF(bp);
566                 snprintf(bp, LIB_BUFLENGTH, "AM_#%d", am);
567                 return bp;
568         }
569 }
570
571
572 /*
573  * receive - receive procedure called for each packet received
574  */
575 void
576 receive(
577         struct recvbuf *rbufp
578         )
579 {
580         register struct peer *peer;     /* peer structure pointer */
581         register struct pkt *pkt;       /* receive packet pointer */
582         u_char  hisversion;             /* packet version */
583         u_char  hisleap;                /* packet leap indicator */
584         u_char  hismode;                /* packet mode */
585         u_char  hisstratum;             /* packet stratum */
586         u_short restrict_mask;          /* restrict bits */
587         const char *hm_str;             /* hismode string */
588         const char *am_str;             /* association match string */
589         int     kissCode = NOKISS;      /* Kiss Code */
590         int     has_mac;                /* length of MAC field */
591         int     authlen;                /* offset of MAC field */
592         int     is_authentic = AUTH_NONE;       /* cryptosum ok */
593         int     crypto_nak_test;        /* result of crypto-NAK check */
594         int     retcode = AM_NOMATCH;   /* match code */
595         keyid_t skeyid = 0;             /* key IDs */
596         u_int32 opcode = 0;             /* extension field opcode */
597         sockaddr_u *dstadr_sin;         /* active runway */
598         struct peer *peer2;             /* aux peer structure pointer */
599         endpt   *match_ep;              /* newpeer() local address */
600         l_fp    p_org;                  /* origin timestamp */
601         l_fp    p_rec;                  /* receive timestamp */
602         l_fp    p_xmt;                  /* transmit timestamp */
603 #ifdef AUTOKEY
604         char    hostname[NTP_MAXSTRLEN + 1];
605         char    *groupname = NULL;
606         struct autokey *ap;             /* autokey structure pointer */
607         int     rval;                   /* cookie snatcher */
608         keyid_t pkeyid = 0, tkeyid = 0; /* key IDs */
609 #endif  /* AUTOKEY */
610 #ifdef HAVE_NTP_SIGND
611         static unsigned char zero_key[16];
612 #endif /* HAVE_NTP_SIGND */
613
614         /*
615          * Monitor the packet and get restrictions. Note that the packet
616          * length for control and private mode packets must be checked
617          * by the service routines. Some restrictions have to be handled
618          * later in order to generate a kiss-o'-death packet.
619          */
620         /*
621          * Bogus port check is before anything, since it probably
622          * reveals a clogging attack.
623          */
624         sys_received++;
625         if (0 == SRCPORT(&rbufp->recv_srcadr)) {
626                 sys_badlength++;
627                 return;                         /* bogus port */
628         }
629         restrict_mask = restrictions(&rbufp->recv_srcadr);
630         pkt = &rbufp->recv_pkt;
631         DPRINTF(2, ("receive: at %ld %s<-%s flags %x restrict %03x org %#010x.%08x xmt %#010x.%08x\n",
632                     current_time, stoa(&rbufp->dstadr->sin),
633                     stoa(&rbufp->recv_srcadr), rbufp->dstadr->flags,
634                     restrict_mask, ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
635                     ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
636         hisversion = PKT_VERSION(pkt->li_vn_mode);
637         hisleap = PKT_LEAP(pkt->li_vn_mode);
638         hismode = (int)PKT_MODE(pkt->li_vn_mode);
639         hisstratum = PKT_TO_STRATUM(pkt->stratum);
640         INSIST(0 != hisstratum);
641
642         if (restrict_mask & RES_IGNORE) {
643                 sys_restricted++;
644                 return;                         /* ignore everything */
645         }
646         if (hismode == MODE_PRIVATE) {
647                 if (!ntp_mode7 || (restrict_mask & RES_NOQUERY)) {
648                         sys_restricted++;
649                         return;                 /* no query private */
650                 }
651                 process_private(rbufp, ((restrict_mask &
652                     RES_NOMODIFY) == 0));
653                 return;
654         }
655         if (hismode == MODE_CONTROL) {
656                 if (restrict_mask & RES_NOQUERY) {
657                         sys_restricted++;
658                         return;                 /* no query control */
659                 }
660                 process_control(rbufp, restrict_mask);
661                 return;
662         }
663         if (restrict_mask & RES_DONTSERVE) {
664                 sys_restricted++;
665                 return;                         /* no time serve */
666         }
667
668         /*
669          * This is for testing. If restricted drop ten percent of
670          * surviving packets.
671          */
672         if (restrict_mask & RES_FLAKE) {
673                 if ((double)ntp_random() / 0x7fffffff < .1) {
674                         sys_restricted++;
675                         return;                 /* no flakeway */
676                 }
677         }
678
679         /*
680          * Version check must be after the query packets, since they
681          * intentionally use an early version.
682          */
683         if (hisversion == NTP_VERSION) {
684                 sys_newversion++;               /* new version */
685         } else if (   !(restrict_mask & RES_VERSION)
686                    && hisversion >= NTP_OLDVERSION) {
687                 sys_oldversion++;               /* previous version */
688         } else {
689                 sys_badlength++;
690                 return;                         /* old version */
691         }
692
693         /*
694          * Figure out his mode and validate the packet. This has some
695          * legacy raunch that probably should be removed. In very early
696          * NTP versions mode 0 was equivalent to what later versions
697          * would interpret as client mode.
698          */
699         if (hismode == MODE_UNSPEC) {
700                 if (hisversion == NTP_OLDVERSION) {
701                         hismode = MODE_CLIENT;
702                 } else {
703                         sys_badlength++;
704                         return;                 /* invalid mode */
705                 }
706         }
707
708         /*
709          * Parse the extension field if present. We figure out whether
710          * an extension field is present by measuring the MAC size. If
711          * the number of words following the packet header is 0, no MAC
712          * is present and the packet is not authenticated. If 1, the
713          * packet is a crypto-NAK; if 3, the packet is authenticated
714          * with DES; if 5, the packet is authenticated with MD5; if 6,
715          * the packet is authenticated with SHA. If 2 or * 4, the packet
716          * is a runt and discarded forthwith. If greater than 6, an
717          * extension field is present, so we subtract the length of the
718          * field and go around again.
719          */
720
721         authlen = LEN_PKT_NOMAC;
722         has_mac = rbufp->recv_length - authlen;
723         while (has_mac > 0) {
724                 u_int32 len;
725 #ifdef AUTOKEY
726                 u_int32 hostlen;
727                 struct exten *ep;
728 #endif /*AUTOKEY */
729
730                 if (has_mac % 4 != 0 || has_mac < (int)MIN_MAC_LEN) {
731                         sys_badlength++;
732                         return;                 /* bad length */
733                 }
734                 if (has_mac <= (int)MAX_MAC_LEN) {
735                         skeyid = ntohl(((u_int32 *)pkt)[authlen / 4]);
736                         break;
737
738                 } else {
739                         opcode = ntohl(((u_int32 *)pkt)[authlen / 4]);
740                         len = opcode & 0xffff;
741                         if (   len % 4 != 0
742                             || len < 4
743                             || (int)len + authlen > rbufp->recv_length) {
744                                 sys_badlength++;
745                                 return;         /* bad length */
746                         }
747 #ifdef AUTOKEY
748                         /*
749                          * Extract calling group name for later.  If
750                          * sys_groupname is non-NULL, there must be
751                          * a group name provided to elicit a response.
752                          */
753                         if (   (opcode & 0x3fff0000) == CRYPTO_ASSOC
754                             && sys_groupname != NULL) {
755                                 ep = (struct exten *)&((u_int32 *)pkt)[authlen / 4];
756                                 hostlen = ntohl(ep->vallen);
757                                 if (   hostlen >= sizeof(hostname)
758                                     || hostlen > len -
759                                                 offsetof(struct exten, pkt)) {
760                                         sys_badlength++;
761                                         return;         /* bad length */
762                                 }
763                                 memcpy(hostname, &ep->pkt, hostlen);
764                                 hostname[hostlen] = '\0';
765                                 groupname = strchr(hostname, '@');
766                                 if (groupname == NULL) {
767                                         sys_declined++;
768                                         return;
769                                 }
770                                 groupname++;
771                         }
772 #endif /* AUTOKEY */
773                         authlen += len;
774                         has_mac -= len;
775                 }
776         }
777
778         /*
779          * If has_mac is < 0 we had a malformed packet.
780          */
781         if (has_mac < 0) {
782                 sys_badlength++;
783                 return;         /* bad length */
784         }
785
786         /*
787          * If authentication required, a MAC must be present.
788          */
789         if (restrict_mask & RES_DONTTRUST && has_mac == 0) {
790                 sys_restricted++;
791                 return;                         /* access denied */
792         }
793
794         /*
795          * Update the MRU list and finger the cloggers. It can be a
796          * little expensive, so turn it off for production use.
797          * RES_LIMITED and RES_KOD will be cleared in the returned
798          * restrict_mask unless one or both actions are warranted.
799          */
800         restrict_mask = ntp_monitor(rbufp, restrict_mask);
801         if (restrict_mask & RES_LIMITED) {
802                 sys_limitrejected++;
803                 if (   !(restrict_mask & RES_KOD)
804                     || MODE_BROADCAST == hismode
805                     || MODE_SERVER == hismode) {
806                         if (MODE_SERVER == hismode)
807                                 DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
808                                         stoa(&rbufp->recv_srcadr)));
809                         return;                 /* rate exceeded */
810                 }
811                 if (hismode == MODE_CLIENT)
812                         fast_xmit(rbufp, MODE_SERVER, skeyid,
813                             restrict_mask);
814                 else
815                         fast_xmit(rbufp, MODE_ACTIVE, skeyid,
816                             restrict_mask);
817                 return;                         /* rate exceeded */
818         }
819         restrict_mask &= ~RES_KOD;
820
821         /*
822          * We have tossed out as many buggy packets as possible early in
823          * the game to reduce the exposure to a clogging attack. Now we
824          * have to burn some cycles to find the association and
825          * authenticate the packet if required. Note that we burn only
826          * digest cycles, again to reduce exposure. There may be no
827          * matching association and that's okay.
828          *
829          * More on the autokey mambo. Normally the local interface is
830          * found when the association was mobilized with respect to a
831          * designated remote address. We assume packets arriving from
832          * the remote address arrive via this interface and the local
833          * address used to construct the autokey is the unicast address
834          * of the interface. However, if the sender is a broadcaster,
835          * the interface broadcast address is used instead.
836          * Notwithstanding this technobabble, if the sender is a
837          * multicaster, the broadcast address is null, so we use the
838          * unicast address anyway. Don't ask.
839          */
840         peer = findpeer(rbufp,  hismode, &retcode);
841         dstadr_sin = &rbufp->dstadr->sin;
842         NTOHL_FP(&pkt->org, &p_org);
843         NTOHL_FP(&pkt->rec, &p_rec);
844         NTOHL_FP(&pkt->xmt, &p_xmt);
845         hm_str = modetoa(hismode);
846         am_str = amtoa(retcode);
847
848         /*
849          * Authentication is conditioned by three switches:
850          *
851          * NOPEER  (RES_NOPEER) do not mobilize an association unless
852          *         authenticated
853          * NOTRUST (RES_DONTTRUST) do not allow access unless
854          *         authenticated (implies NOPEER)
855          * enable  (sys_authenticate) master NOPEER switch, by default
856          *         on
857          *
858          * The NOPEER and NOTRUST can be specified on a per-client basis
859          * using the restrict command. The enable switch if on implies
860          * NOPEER for all clients. There are four outcomes:
861          *
862          * NONE    The packet has no MAC.
863          * OK      the packet has a MAC and authentication succeeds
864          * ERROR   the packet has a MAC and authentication fails
865          * CRYPTO  crypto-NAK. The MAC has four octets only.
866          *
867          * Note: The AUTH(x, y) macro is used to filter outcomes. If x
868          * is zero, acceptable outcomes of y are NONE and OK. If x is
869          * one, the only acceptable outcome of y is OK.
870          */
871         crypto_nak_test = valid_NAK(peer, rbufp, hismode);
872
873         /*
874          * Drop any invalid crypto-NAKs
875          */
876         if (crypto_nak_test == INVALIDNAK) {
877                 report_event(PEVNT_AUTH, peer, "Invalid_NAK");
878                 if (0 != peer) {
879                         peer->badNAK++;
880                 }
881                 msyslog(LOG_ERR, "Invalid-NAK error at %ld %s<-%s", 
882                         current_time, stoa(dstadr_sin), stoa(&rbufp->recv_srcadr));
883                 return;
884         }
885
886         if (has_mac == 0) {
887                 restrict_mask &= ~RES_MSSNTP;
888                 is_authentic = AUTH_NONE; /* not required */
889                 DPRINTF(2, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x NOMAC\n",
890                             current_time, stoa(dstadr_sin),
891                             stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
892                             authlen,
893                             ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
894                             ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
895         } else if (crypto_nak_test == VALIDNAK) {
896                 restrict_mask &= ~RES_MSSNTP;
897                 is_authentic = AUTH_CRYPTO; /* crypto-NAK */
898                 DPRINTF(2, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x MAC4\n",
899                             current_time, stoa(dstadr_sin),
900                             stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
901                             skeyid, authlen + has_mac, is_authentic,
902                             ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
903                             ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
904
905 #ifdef HAVE_NTP_SIGND
906                 /*
907                  * If the signature is 20 bytes long, the last 16 of
908                  * which are zero, then this is a Microsoft client
909                  * wanting AD-style authentication of the server's
910                  * reply.
911                  *
912                  * This is described in Microsoft's WSPP docs, in MS-SNTP:
913                  * http://msdn.microsoft.com/en-us/library/cc212930.aspx
914                  */
915         } else if (   has_mac == MAX_MD5_LEN
916                    && (restrict_mask & RES_MSSNTP)
917                    && (retcode == AM_FXMIT || retcode == AM_NEWPASS)
918                    && (memcmp(zero_key, (char *)pkt + authlen + 4,
919                               MAX_MD5_LEN - 4) == 0)) {
920                 is_authentic = AUTH_NONE;
921 #endif /* HAVE_NTP_SIGND */
922
923         } else {
924                 restrict_mask &= ~RES_MSSNTP;
925 #ifdef AUTOKEY
926                 /*
927                  * For autokey modes, generate the session key
928                  * and install in the key cache. Use the socket
929                  * broadcast or unicast address as appropriate.
930                  */
931                 if (crypto_flags && skeyid > NTP_MAXKEY) {
932
933                         /*
934                          * More on the autokey dance (AKD). A cookie is
935                          * constructed from public and private values.
936                          * For broadcast packets, the cookie is public
937                          * (zero). For packets that match no
938                          * association, the cookie is hashed from the
939                          * addresses and private value. For server
940                          * packets, the cookie was previously obtained
941                          * from the server. For symmetric modes, the
942                          * cookie was previously constructed using an
943                          * agreement protocol; however, should PKI be
944                          * unavailable, we construct a fake agreement as
945                          * the EXOR of the peer and host cookies.
946                          *
947                          * hismode      ephemeral       persistent
948                          * =======================================
949                          * active       0               cookie#
950                          * passive      0%              cookie#
951                          * client       sys cookie      0%
952                          * server       0%              sys cookie
953                          * broadcast    0               0
954                          *
955                          * # if unsync, 0
956                          * % can't happen
957                          */
958                         if (has_mac < (int)MAX_MD5_LEN) {
959                                 sys_badauth++;
960                                 return;
961                         }
962                         if (hismode == MODE_BROADCAST) {
963
964                                 /*
965                                  * For broadcaster, use the interface
966                                  * broadcast address when available;
967                                  * otherwise, use the unicast address
968                                  * found when the association was
969                                  * mobilized. However, if this is from
970                                  * the wildcard interface, game over.
971                                  */
972                                 if (   crypto_flags
973                                     && rbufp->dstadr ==
974                                        ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr)) {
975                                         sys_restricted++;
976                                         return;      /* no wildcard */
977                                 }
978                                 pkeyid = 0;
979                                 if (!SOCK_UNSPEC(&rbufp->dstadr->bcast))
980                                         dstadr_sin =
981                                             &rbufp->dstadr->bcast;
982                         } else if (peer == NULL) {
983                                 pkeyid = session_key(
984                                     &rbufp->recv_srcadr, dstadr_sin, 0,
985                                     sys_private, 0);
986                         } else {
987                                 pkeyid = peer->pcookie;
988                         }
989
990                         /*
991                          * The session key includes both the public
992                          * values and cookie. In case of an extension
993                          * field, the cookie used for authentication
994                          * purposes is zero. Note the hash is saved for
995                          * use later in the autokey mambo.
996                          */
997                         if (authlen > (int)LEN_PKT_NOMAC && pkeyid != 0) {
998                                 session_key(&rbufp->recv_srcadr,
999                                     dstadr_sin, skeyid, 0, 2);
1000                                 tkeyid = session_key(
1001                                     &rbufp->recv_srcadr, dstadr_sin,
1002                                     skeyid, pkeyid, 0);
1003                         } else {
1004                                 tkeyid = session_key(
1005                                     &rbufp->recv_srcadr, dstadr_sin,
1006                                     skeyid, pkeyid, 2);
1007                         }
1008
1009                 }
1010 #endif  /* AUTOKEY */
1011
1012                 /*
1013                  * Compute the cryptosum. Note a clogging attack may
1014                  * succeed in bloating the key cache. If an autokey,
1015                  * purge it immediately, since we won't be needing it
1016                  * again. If the packet is authentic, it can mobilize an
1017                  * association. Note that there is no key zero.
1018                  */
1019                 if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1020                     has_mac))
1021                         is_authentic = AUTH_ERROR;
1022                 else
1023                         is_authentic = AUTH_OK;
1024 #ifdef AUTOKEY
1025                 if (crypto_flags && skeyid > NTP_MAXKEY)
1026                         authtrust(skeyid, 0);
1027 #endif  /* AUTOKEY */
1028                 DPRINTF(2, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x\n",
1029                             current_time, stoa(dstadr_sin),
1030                             stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1031                             skeyid, authlen + has_mac, is_authentic,
1032                             ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1033                             ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1034         }
1035
1036         /*
1037          * The association matching rules are implemented by a set of
1038          * routines and an association table. A packet matching an
1039          * association is processed by the peer process for that
1040          * association. If there are no errors, an ephemeral association
1041          * is mobilized: a broadcast packet mobilizes a broadcast client
1042          * aassociation; a manycast server packet mobilizes a manycast
1043          * client association; a symmetric active packet mobilizes a
1044          * symmetric passive association.
1045          */
1046         switch (retcode) {
1047
1048         /*
1049          * This is a client mode packet not matching any association. If
1050          * an ordinary client, simply toss a server mode packet back
1051          * over the fence. If a manycast client, we have to work a
1052          * little harder.
1053          */
1054         case AM_FXMIT:
1055
1056                 /*
1057                  * If authentication OK, send a server reply; otherwise,
1058                  * send a crypto-NAK.
1059                  */
1060                 if (!(rbufp->dstadr->flags & INT_MCASTOPEN)) {
1061                         if (AUTH(restrict_mask & RES_DONTTRUST,
1062                            is_authentic)) {
1063                                 fast_xmit(rbufp, MODE_SERVER, skeyid,
1064                                     restrict_mask);
1065                         } else if (is_authentic == AUTH_ERROR) {
1066                                 fast_xmit(rbufp, MODE_SERVER, 0,
1067                                     restrict_mask);
1068                                 sys_badauth++;
1069                         } else {
1070                                 sys_restricted++;
1071                         }
1072                         return;                 /* hooray */
1073                 }
1074
1075                 /*
1076                  * This must be manycast. Do not respond if not
1077                  * configured as a manycast server.
1078                  */
1079                 if (!sys_manycastserver) {
1080                         sys_restricted++;
1081                         return;                 /* not enabled */
1082                 }
1083
1084 #ifdef AUTOKEY
1085                 /*
1086                  * Do not respond if not the same group.
1087                  */
1088                 if (group_test(groupname, NULL)) {
1089                         sys_declined++;
1090                         return;
1091                 }
1092 #endif /* AUTOKEY */
1093
1094                 /*
1095                  * Do not respond if we are not synchronized or our
1096                  * stratum is greater than the manycaster or the
1097                  * manycaster has already synchronized to us.
1098                  */
1099                 if (   sys_leap == LEAP_NOTINSYNC
1100                     || sys_stratum >= hisstratum
1101                     || (!sys_cohort && sys_stratum == hisstratum + 1)
1102                     || rbufp->dstadr->addr_refid == pkt->refid) {
1103                         sys_declined++;
1104                         return;                 /* no help */
1105                 }
1106
1107                 /*
1108                  * Respond only if authentication succeeds. Don't do a
1109                  * crypto-NAK, as that would not be useful.
1110                  */
1111                 if (AUTH(restrict_mask & RES_DONTTRUST, is_authentic))
1112                         fast_xmit(rbufp, MODE_SERVER, skeyid,
1113                             restrict_mask);
1114                 return;                         /* hooray */
1115
1116         /*
1117          * This is a server mode packet returned in response to a client
1118          * mode packet sent to a multicast group address (for
1119          * manycastclient) or to a unicast address (for pool). The
1120          * origin timestamp is a good nonce to reliably associate the
1121          * reply with what was sent. If there is no match, that's
1122          * curious and could be an intruder attempting to clog, so we
1123          * just ignore it.
1124          *
1125          * If the packet is authentic and the manycastclient or pool
1126          * association is found, we mobilize a client association and
1127          * copy pertinent variables from the manycastclient or pool
1128          * association to the new client association. If not, just
1129          * ignore the packet.
1130          *
1131          * There is an implosion hazard at the manycast client, since
1132          * the manycast servers send the server packet immediately. If
1133          * the guy is already here, don't fire up a duplicate.
1134          */
1135         case AM_MANYCAST:
1136
1137 #ifdef AUTOKEY
1138                 /*
1139                  * Do not respond if not the same group.
1140                  */
1141                 if (group_test(groupname, NULL)) {
1142                         sys_declined++;
1143                         return;
1144                 }
1145 #endif /* AUTOKEY */
1146                 if ((peer2 = findmanycastpeer(rbufp)) == NULL) {
1147                         sys_restricted++;
1148                         return;                 /* not enabled */
1149                 }
1150                 if (!AUTH(  (!(peer2->cast_flags & MDF_POOL)
1151                              && sys_authenticate)
1152                           || (restrict_mask & (RES_NOPEER |
1153                               RES_DONTTRUST)), is_authentic)) {
1154                         sys_restricted++;
1155                         return;                 /* access denied */
1156                 }
1157
1158                 /*
1159                  * Do not respond if unsynchronized or stratum is below
1160                  * the floor or at or above the ceiling.
1161                  */
1162                 if (   hisleap == LEAP_NOTINSYNC
1163                     || hisstratum < sys_floor
1164                     || hisstratum >= sys_ceiling) {
1165                         sys_declined++;
1166                         return;                 /* no help */
1167                 }
1168                 peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1169                                MODE_CLIENT, hisversion, peer2->minpoll,
1170                                peer2->maxpoll, FLAG_PREEMPT |
1171                                (FLAG_IBURST & peer2->flags), MDF_UCAST |
1172                                MDF_UCLNT, 0, skeyid, sys_ident);
1173                 if (NULL == peer) {
1174                         sys_declined++;
1175                         return;                 /* ignore duplicate  */
1176                 }
1177
1178                 /*
1179                  * After each ephemeral pool association is spun,
1180                  * accelerate the next poll for the pool solicitor so
1181                  * the pool will fill promptly.
1182                  */
1183                 if (peer2->cast_flags & MDF_POOL)
1184                         peer2->nextdate = current_time + 1;
1185
1186                 /*
1187                  * Further processing of the solicitation response would
1188                  * simply detect its origin timestamp as bogus for the
1189                  * brand-new association (it matches the prototype
1190                  * association) and tinker with peer->nextdate delaying
1191                  * first sync.
1192                  */
1193                 return;         /* solicitation response handled */
1194
1195         /*
1196          * This is the first packet received from a broadcast server. If
1197          * the packet is authentic and we are enabled as broadcast
1198          * client, mobilize a broadcast client association. We don't
1199          * kiss any frogs here.
1200          */
1201         case AM_NEWBCL:
1202
1203 #ifdef AUTOKEY
1204                 /*
1205                  * Do not respond if not the same group.
1206                  */
1207                 if (group_test(groupname, sys_ident)) {
1208                         sys_declined++;
1209                         return;
1210                 }
1211 #endif /* AUTOKEY */
1212                 if (sys_bclient == 0) {
1213                         sys_restricted++;
1214                         return;                 /* not enabled */
1215                 }
1216                 if (!AUTH(sys_authenticate | (restrict_mask &
1217                     (RES_NOPEER | RES_DONTTRUST)), is_authentic)) {
1218                         sys_restricted++;
1219                         return;                 /* access denied */
1220                 }
1221
1222                 /*
1223                  * Do not respond if unsynchronized or stratum is below
1224                  * the floor or at or above the ceiling.
1225                  */
1226                 if (   hisleap == LEAP_NOTINSYNC
1227                     || hisstratum < sys_floor
1228                     || hisstratum >= sys_ceiling) {
1229                         sys_declined++;
1230                         return;                 /* no help */
1231                 }
1232
1233 #ifdef AUTOKEY
1234                 /*
1235                  * Do not respond if Autokey and the opcode is not a
1236                  * CRYPTO_ASSOC response with association ID.
1237                  */
1238                 if (   crypto_flags && skeyid > NTP_MAXKEY
1239                     && (opcode & 0xffff0000) != (CRYPTO_ASSOC | CRYPTO_RESP)) {
1240                         sys_declined++;
1241                         return;                 /* protocol error */
1242                 }
1243 #endif  /* AUTOKEY */
1244
1245                 /*
1246                  * Broadcasts received via a multicast address may
1247                  * arrive after a unicast volley has begun
1248                  * with the same remote address.  newpeer() will not
1249                  * find duplicate associations on other local endpoints
1250                  * if a non-NULL endpoint is supplied.  multicastclient
1251                  * ephemeral associations are unique across all local
1252                  * endpoints.
1253                  */
1254                 if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
1255                         match_ep = rbufp->dstadr;
1256                 else
1257                         match_ep = NULL;
1258
1259                 /*
1260                  * Determine whether to execute the initial volley.
1261                  */
1262                 if (sys_bdelay > 0.0) {
1263 #ifdef AUTOKEY
1264                         /*
1265                          * If a two-way exchange is not possible,
1266                          * neither is Autokey.
1267                          */
1268                         if (crypto_flags && skeyid > NTP_MAXKEY) {
1269                                 sys_restricted++;
1270                                 return;         /* no autokey */
1271                         }
1272 #endif  /* AUTOKEY */
1273
1274                         /*
1275                          * Do not execute the volley. Start out in
1276                          * broadcast client mode.
1277                          */
1278                         peer = newpeer(&rbufp->recv_srcadr, NULL,
1279                             match_ep, MODE_BCLIENT, hisversion,
1280                             pkt->ppoll, pkt->ppoll, FLAG_PREEMPT,
1281                             MDF_BCLNT, 0, skeyid, sys_ident);
1282                         if (NULL == peer) {
1283                                 sys_restricted++;
1284                                 return;         /* ignore duplicate */
1285
1286                         } else {
1287                                 peer->delay = sys_bdelay;
1288                                 peer->bxmt = p_xmt;
1289                         }
1290                         break;
1291                 }
1292
1293                 /*
1294                  * Execute the initial volley in order to calibrate the
1295                  * propagation delay and run the Autokey protocol.
1296                  *
1297                  * Note that the minpoll is taken from the broadcast
1298                  * packet, normally 6 (64 s) and that the poll interval
1299                  * is fixed at this value.
1300                  */
1301                 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1302                     MODE_CLIENT, hisversion, pkt->ppoll, pkt->ppoll,
1303                     FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
1304                     0, skeyid, sys_ident);
1305                 if (NULL == peer) {
1306                         sys_restricted++;
1307                         return;                 /* ignore duplicate */
1308                 }
1309                 peer->bxmt = p_xmt;
1310 #ifdef AUTOKEY
1311                 if (skeyid > NTP_MAXKEY)
1312                         crypto_recv(peer, rbufp);
1313 #endif  /* AUTOKEY */
1314
1315                 return;                         /* hooray */
1316
1317         /*
1318          * This is the first packet received from a symmetric active
1319          * peer. If the packet is authentic and the first he sent,
1320          * mobilize a passive association. If not, kiss the frog.
1321          */
1322         case AM_NEWPASS:
1323
1324 #ifdef AUTOKEY
1325                 /*
1326                  * Do not respond if not the same group.
1327                  */
1328                 if (group_test(groupname, sys_ident)) {
1329                         sys_declined++;
1330                         return;
1331                 }
1332 #endif /* AUTOKEY */
1333                 if (!AUTH(sys_authenticate | (restrict_mask &
1334                     (RES_NOPEER | RES_DONTTRUST)), is_authentic)) {
1335
1336                         /*
1337                          * If authenticated but cannot mobilize an
1338                          * association, send a symmetric passive
1339                          * response without mobilizing an association.
1340                          * This is for drat broken Windows clients. See
1341                          * Microsoft KB 875424 for preferred workaround.
1342                          */
1343                         if (AUTH(restrict_mask & RES_DONTTRUST,
1344                             is_authentic)) {
1345                                 fast_xmit(rbufp, MODE_PASSIVE, skeyid,
1346                                     restrict_mask);
1347                                 return;                 /* hooray */
1348                         }
1349                         if (is_authentic == AUTH_ERROR) {
1350                                 fast_xmit(rbufp, MODE_ACTIVE, 0,
1351                                     restrict_mask);
1352                                 sys_restricted++;
1353                                 return;
1354                         }
1355                         /* [Bug 2941]
1356                          * If we got here, the packet isn't part of an
1357                          * existing association, it isn't correctly
1358                          * authenticated, and it didn't meet either of
1359                          * the previous two special cases so we should
1360                          * just drop it on the floor.  For example,
1361                          * crypto-NAKs (is_authentic == AUTH_CRYPTO)
1362                          * will make it this far.  This is just
1363                          * debug-printed and not logged to avoid log
1364                          * flooding.
1365                          */
1366                         DPRINTF(2, ("receive: at %ld refusing to mobilize passive association"
1367                                     " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1368                                     current_time, stoa(&rbufp->recv_srcadr),
1369                                     hismode, hm_str, am_str, skeyid,
1370                                     (authlen + has_mac), is_authentic));
1371                         sys_declined++;
1372                         return;
1373                 }
1374
1375                 /*
1376                  * Do not respond if synchronized and if stratum is
1377                  * below the floor or at or above the ceiling. Note,
1378                  * this allows an unsynchronized peer to synchronize to
1379                  * us. It would be very strange if he did and then was
1380                  * nipped, but that could only happen if we were
1381                  * operating at the top end of the range.  It also means
1382                  * we will spin an ephemeral association in response to
1383                  * MODE_ACTIVE KoDs, which will time out eventually.
1384                  */
1385                 if (   hisleap != LEAP_NOTINSYNC
1386                     && (hisstratum < sys_floor || hisstratum >= sys_ceiling)) {
1387                         sys_declined++;
1388                         return;                 /* no help */
1389                 }
1390
1391                 /*
1392                  * The message is correctly authenticated and allowed.
1393                  * Mobilize a symmetric passive association.
1394                  */
1395                 if ((peer = newpeer(&rbufp->recv_srcadr, NULL,
1396                     rbufp->dstadr, MODE_PASSIVE, hisversion, pkt->ppoll,
1397                     NTP_MAXDPOLL, 0, MDF_UCAST, 0, skeyid,
1398                     sys_ident)) == NULL) {
1399                         sys_declined++;
1400                         return;                 /* ignore duplicate */
1401                 }
1402                 break;
1403
1404
1405         /*
1406          * Process regular packet. Nothing special.
1407          */
1408         case AM_PROCPKT:
1409
1410 #ifdef AUTOKEY
1411                 /*
1412                  * Do not respond if not the same group.
1413                  */
1414                 if (group_test(groupname, peer->ident)) {
1415                         sys_declined++;
1416                         return;
1417                 }
1418 #endif /* AUTOKEY */
1419
1420                 if (MODE_BROADCAST == hismode) {
1421                         int     bail = 0;
1422                         l_fp    tdiff;
1423                         u_long  deadband;
1424
1425                         DPRINTF(2, ("receive: PROCPKT/BROADCAST: prev pkt %ld seconds ago, ppoll: %d, %d secs\n",
1426                                     (current_time - peer->timelastrec),
1427                                     peer->ppoll, (1 << peer->ppoll)
1428                                     ));
1429                         /* Things we can check:
1430                          *
1431                          * Did the poll interval change?
1432                          * Is the poll interval in the packet in-range?
1433                          * Did this packet arrive too soon?
1434                          * Is the timestamp in this packet monotonic
1435                          *  with respect to the previous packet?
1436                          */
1437
1438                         /* This is noteworthy, not error-worthy */
1439                         if (pkt->ppoll != peer->ppoll) {
1440                                 msyslog(LOG_INFO, "receive: broadcast poll from %s changed from %ud to %ud",
1441                                         stoa(&rbufp->recv_srcadr),
1442                                         peer->ppoll, pkt->ppoll);
1443                         }
1444
1445                         /* This is error-worthy */
1446                         if (pkt->ppoll < peer->minpoll ||
1447                             pkt->ppoll > peer->maxpoll  ) {
1448                                 msyslog(LOG_INFO, "receive: broadcast poll of %ud from %s is out-of-range (%d to %d)!",
1449                                         pkt->ppoll, stoa(&rbufp->recv_srcadr),
1450                                         peer->minpoll, peer->maxpoll);
1451                                 ++bail;
1452                         }
1453
1454                         /* too early? worth an error, too!
1455                          *
1456                          * [Bug 3113] Ensure that at least one poll
1457                          * interval has elapsed since the last **clean**
1458                          * packet was received.  We limit the check to
1459                          * **clean** packets to prevent replayed packets
1460                          * and incorrectly authenticated packets, which
1461                          * we'll discard, from being used to create a
1462                          * denial of service condition.
1463                          */
1464                         deadband = (1u << pkt->ppoll);
1465                         if (FLAG_BC_VOL & peer->flags)
1466                                 deadband -= 3;  /* allow greater fuzz after volley */
1467                         if ((current_time - peer->timereceived) < deadband) {
1468                                 msyslog(LOG_INFO, "receive: broadcast packet from %s arrived after %lu, not %lu seconds!",
1469                                         stoa(&rbufp->recv_srcadr),
1470                                         (current_time - peer->timereceived),
1471                                         deadband);
1472                                 ++bail;
1473                         }
1474
1475                         /* Alert if time from the server is non-monotonic.
1476                          *
1477                          * [Bug 3114] is about Broadcast mode replay DoS.
1478                          *
1479                          * Broadcast mode *assumes* a trusted network.
1480                          * Even so, it's nice to be robust in the face
1481                          * of attacks.
1482                          *
1483                          * If we get an authenticated broadcast packet
1484                          * with an "earlier" timestamp, it means one of
1485                          * two things:
1486                          *
1487                          * - the broadcast server had a backward step.
1488                          *
1489                          * - somebody is trying a replay attack.
1490                          *
1491                          * deadband: By default, we assume the broadcast
1492                          * network is trustable, so we take our accepted
1493                          * broadcast packets as we receive them.  But
1494                          * some folks might want to take additional poll
1495                          * delays before believing a backward step. 
1496                          */
1497                         if (sys_bcpollbstep) {
1498                                 /* pkt->ppoll or peer->ppoll ? */
1499                                 deadband = (1u << pkt->ppoll)
1500                                            * sys_bcpollbstep + 2;
1501                         } else {
1502                                 deadband = 0;
1503                         }
1504
1505                         if (L_ISZERO(&peer->bxmt)) {
1506                                 tdiff.l_ui = tdiff.l_uf = 0;
1507                         } else {
1508                                 tdiff = p_xmt;
1509                                 L_SUB(&tdiff, &peer->bxmt);
1510                         }
1511                         if (tdiff.l_i < 0 &&
1512                             (current_time - peer->timereceived) < deadband)
1513                         {
1514                                 msyslog(LOG_INFO, "receive: broadcast packet from %s contains non-monotonic timestamp: %#010x.%08x -> %#010x.%08x",
1515                                         stoa(&rbufp->recv_srcadr),
1516                                         peer->bxmt.l_ui, peer->bxmt.l_uf,
1517                                         p_xmt.l_ui, p_xmt.l_uf
1518                                         );
1519                                 ++bail;
1520                         }
1521
1522                         if (bail) {
1523                                 peer->timelastrec = current_time;
1524                                 sys_declined++;
1525                                 return;
1526                         }
1527                 }
1528
1529                 break;
1530
1531         /*
1532          * A passive packet matches a passive association. This is
1533          * usually the result of reconfiguring a client on the fly. As
1534          * this association might be legitimate and this packet an
1535          * attempt to deny service, just ignore it.
1536          */
1537         case AM_ERR:
1538                 sys_declined++;
1539                 return;
1540
1541         /*
1542          * For everything else there is the bit bucket.
1543          */
1544         default:
1545                 sys_declined++;
1546                 return;
1547         }
1548
1549 #ifdef AUTOKEY
1550         /*
1551          * If the association is configured for Autokey, the packet must
1552          * have a public key ID; if not, the packet must have a
1553          * symmetric key ID.
1554          */
1555         if (   is_authentic != AUTH_CRYPTO
1556             && (   ((peer->flags & FLAG_SKEY) && skeyid <= NTP_MAXKEY)
1557                 || (!(peer->flags & FLAG_SKEY) && skeyid > NTP_MAXKEY))) {
1558                 sys_badauth++;
1559                 return;
1560         }
1561 #endif  /* AUTOKEY */
1562
1563         peer->received++;
1564         peer->flash &= ~PKT_TEST_MASK;
1565         if (peer->flags & FLAG_XBOGUS) {
1566                 peer->flags &= ~FLAG_XBOGUS;
1567                 peer->flash |= TEST3;
1568         }
1569
1570         /*
1571          * Next comes a rigorous schedule of timestamp checking. If the
1572          * transmit timestamp is zero, the server has not initialized in
1573          * interleaved modes or is horribly broken.
1574          *
1575          * A KoD packet we pay attention to cannot have a 0 transmit
1576          * timestamp.
1577          */
1578         if (L_ISZERO(&p_xmt)) {
1579                 peer->flash |= TEST3;                   /* unsynch */
1580                 if (STRATUM_UNSPEC == hisstratum) {     /* KoD packet */
1581                         peer->bogusorg++;               /* for TEST2 or TEST3 */
1582                         msyslog(LOG_INFO,
1583                                 "receive: Unexpected zero transmit timestamp in KoD from %s",
1584                                 ntoa(&peer->srcadr));
1585                         return;
1586                 }
1587
1588         /*
1589          * If the transmit timestamp duplicates our previous one, the
1590          * packet is a replay. This prevents the bad guys from replaying
1591          * the most recent packet, authenticated or not.
1592          */
1593         } else if (L_ISEQU(&peer->xmt, &p_xmt)) {
1594                 peer->flash |= TEST1;                   /* duplicate */
1595                 peer->oldpkt++;
1596                 return;
1597
1598         /*
1599          * If this is a broadcast mode packet, make sure hisstratum
1600          * is appropriate.  Don't do anything else here - we wait to
1601          * see if this is an interleave broadcast packet until after
1602          * we've validated the MAC that SHOULD be provided.
1603          *
1604          * hisstratum should never be 0.
1605          * If hisstratum is 15, then we'll advertise as UNSPEC but
1606          * at least we'll be able to sync with the broadcast server.
1607          */
1608         } else if (hismode == MODE_BROADCAST) {
1609                 if (   0 == hisstratum
1610                     || STRATUM_UNSPEC <= hisstratum) {
1611                         /* Is this a ++sys_declined or ??? */
1612                         msyslog(LOG_INFO,
1613                                 "receive: Unexpected stratum (%d) in broadcast from %s",
1614                                 hisstratum, ntoa(&peer->srcadr));
1615                         return;
1616                 }
1617
1618         /*
1619          * Basic KoD validation checking:
1620          *
1621          * KoD packets are a mixed-blessing.  Forged KoD packets
1622          * are DoS attacks.  There are rare situations where we might
1623          * get a valid KoD response, though.  Since KoD packets are
1624          * a special case that complicate the checks we do next, we
1625          * handle the basic KoD checks here.
1626          *
1627          * Note that we expect the incoming KoD packet to have its
1628          * (nonzero) org, rec, and xmt timestamps set to the xmt timestamp
1629          * that we have previously sent out.  Watch interleave mode.
1630          */
1631         } else if (STRATUM_UNSPEC == hisstratum) {
1632                 DEBUG_INSIST(!L_ISZERO(&p_xmt));
1633                 if (   L_ISZERO(&p_org)         /* We checked p_xmt above */
1634                     || L_ISZERO(&p_rec)) {
1635                         peer->bogusorg++;
1636                         msyslog(LOG_INFO,
1637                                 "receive: KoD packet from %s has a zero org or rec timestamp.  Ignoring.",
1638                                 ntoa(&peer->srcadr));
1639                         return;
1640                 }
1641
1642                 if (   !L_ISEQU(&p_xmt, &p_org)
1643                     || !L_ISEQU(&p_xmt, &p_rec)) {
1644                         peer->bogusorg++;
1645                         msyslog(LOG_INFO,
1646                                 "receive: KoD packet from %s has inconsistent xmt/org/rec timestamps.  Ignoring.",
1647                                 ntoa(&peer->srcadr));
1648                         return;
1649                 }
1650
1651                 /* Be conservative */
1652                 if (peer->flip == 0 && !L_ISEQU(&p_org, &peer->aorg)) {
1653                         peer->bogusorg++;
1654                         msyslog(LOG_INFO,
1655                                 "receive: flip 0 KoD origin timestamp %#010x.%08x from %s does not match %#010x.%08x - ignoring.",
1656                                 p_org.l_ui, p_org.l_uf,
1657                                 ntoa(&peer->srcadr),
1658                                 peer->aorg.l_ui, peer->aorg.l_uf);
1659                         return;
1660                 } else if (peer->flip == 1 && !L_ISEQU(&p_org, &peer->borg)) {
1661                         peer->bogusorg++;
1662                         msyslog(LOG_INFO,
1663                                 "receive: flip 1 KoD origin timestamp %#010x.%08x from %s does not match interleave %#010x.%08x - ignoring.",
1664                                 p_org.l_ui, p_org.l_uf,
1665                                 ntoa(&peer->srcadr),
1666                                 peer->borg.l_ui, peer->borg.l_uf);
1667                         return;
1668                 }
1669
1670         /*
1671          * Basic mode checks:
1672          *
1673          * If there is no origin timestamp, it's either an initial packet
1674          * or we've already received a response to our query.  Of course,
1675          * should 'aorg' be all-zero because this really was the original
1676          * transmit timestamp, we'll ignore this reply.  There is a window
1677          * of one nanosecond once every 136 years' time where this is
1678          * possible.  We currently ignore this situation.
1679          *
1680          * Otherwise, check for bogus packet in basic mode.
1681          * If it is bogus, switch to interleaved mode and resynchronize,
1682          * but only after confirming the packet is not bogus in
1683          * symmetric interleaved mode.
1684          *
1685          * This could also mean somebody is forging packets claiming to
1686          * be from us, attempting to cause our server to KoD us.
1687          */
1688         } else if (peer->flip == 0) {
1689                 INSIST(0 != hisstratum);
1690                 INSIST(STRATUM_UNSPEC != hisstratum);
1691
1692                 if (0) {
1693                 } else if (L_ISZERO(&p_org)) {
1694                         const char *action;
1695
1696 #ifdef BUG3361
1697                         msyslog(LOG_INFO,
1698                                 "receive: BUG 3361: Clearing peer->aorg ");
1699                         L_CLR(&peer->aorg);
1700 #endif
1701                         /**/
1702                         switch (hismode) {
1703                         /* We allow 0org for: */
1704                             case UCHAR_MAX:
1705                                 action = "Allow";
1706                                 break;
1707                         /* We disallow 0org for: */
1708                             case MODE_UNSPEC:
1709                             case MODE_ACTIVE:
1710                             case MODE_PASSIVE:
1711                             case MODE_CLIENT:
1712                             case MODE_SERVER:
1713                             case MODE_BROADCAST:
1714                                 action = "Drop";
1715                                 peer->bogusorg++;
1716                                 peer->flash |= TEST2;   /* bogus */
1717                                 break;
1718                             default:
1719                                 action = "";    /* for cranky compilers / MSVC */
1720                                 INSIST(!"receive(): impossible hismode");
1721                                 break;
1722                         }
1723                         /**/
1724                         msyslog(LOG_INFO,
1725                                 "receive: %s 0 origin timestamp from %s@%s xmt %#010x.%08x",
1726                                 action, hm_str, ntoa(&peer->srcadr),
1727                                 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
1728                 } else if (!L_ISEQU(&p_org, &peer->aorg)) {
1729                         /* are there cases here where we should bail? */
1730                         /* Should we set TEST2 if we decide to try xleave? */
1731                         peer->bogusorg++;
1732                         peer->flash |= TEST2;   /* bogus */
1733                         msyslog(LOG_INFO,
1734                                 "receive: Unexpected origin timestamp %#010x.%08x does not match aorg %#010x.%08x from %s@%s xmt %#010x.%08x",
1735                                 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1736                                 peer->aorg.l_ui, peer->aorg.l_uf,
1737                                 hm_str, ntoa(&peer->srcadr),
1738                                 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
1739                         if (  !L_ISZERO(&peer->dst)
1740                             && L_ISEQU(&p_org, &peer->dst)) {
1741                                 /* Might be the start of an interleave */
1742                                 if (dynamic_interleave) {
1743                                         peer->flip = 1;
1744                                         report_event(PEVNT_XLEAVE, peer, NULL);
1745                                 } else {
1746                                         msyslog(LOG_INFO,
1747                                                 "receive: Dynamic interleave from %s@%s denied",
1748                                                 hm_str, ntoa(&peer->srcadr));
1749                                 }
1750                         }
1751                 } else {
1752                         L_CLR(&peer->aorg);
1753                 }
1754
1755         /*
1756          * Check for valid nonzero timestamp fields.
1757          */
1758         } else if (   L_ISZERO(&p_org)
1759                    || L_ISZERO(&p_rec)
1760                    || L_ISZERO(&peer->dst)) {
1761                 peer->flash |= TEST3;           /* unsynch */
1762
1763         /*
1764          * Check for bogus packet in interleaved symmetric mode. This
1765          * can happen if a packet is lost, duplicated or crossed. If
1766          * found, flip and resynchronize.
1767          */
1768         } else if (   !L_ISZERO(&peer->dst)
1769                    && !L_ISEQU(&p_org, &peer->dst)) {
1770                 peer->bogusorg++;
1771                 peer->flags |= FLAG_XBOGUS;
1772                 peer->flash |= TEST2;           /* bogus */
1773                 return; /* Bogus packet, we are done */
1774         }
1775
1776         /**/
1777
1778         /*
1779          * If this is a crypto_NAK, the server cannot authenticate a
1780          * client packet. The server might have just changed keys. Clear
1781          * the association and restart the protocol.
1782          */
1783         if (crypto_nak_test == VALIDNAK) {
1784                 report_event(PEVNT_AUTH, peer, "crypto_NAK");
1785                 peer->flash |= TEST5;           /* bad auth */
1786                 peer->badauth++;
1787                 if (peer->flags & FLAG_PREEMPT) {
1788                         if (unpeer_crypto_nak_early) {
1789                                 unpeer(peer);
1790                         }
1791                         return;
1792                 }
1793 #ifdef AUTOKEY
1794                 if (peer->crypto) {
1795                         peer_clear(peer, "AUTH");
1796                 }
1797 #endif  /* AUTOKEY */
1798                 return;
1799
1800         /*
1801          * If the digest fails or it's missing for authenticated
1802          * associations, the client cannot authenticate a server
1803          * reply to a client packet previously sent. The loopback check
1804          * is designed to avoid a bait-and-switch attack, which was
1805          * possible in past versions. If symmetric modes, return a
1806          * crypto-NAK. The peer should restart the protocol.
1807          */
1808         } else if (!AUTH(peer->keyid || has_mac ||
1809                          (restrict_mask & RES_DONTTRUST), is_authentic)) {
1810
1811                 if (peer->flash & PKT_TEST_MASK) {
1812                         msyslog(LOG_INFO,
1813                                 "receive: Bad auth in packet with bad timestamps from %s denied - spoof?",
1814                                 ntoa(&peer->srcadr));
1815                         return;
1816                 }
1817
1818                 report_event(PEVNT_AUTH, peer, "digest");
1819                 peer->flash |= TEST5;           /* bad auth */
1820                 peer->badauth++;
1821                 if (   has_mac
1822                     && (   hismode == MODE_ACTIVE
1823                         || hismode == MODE_PASSIVE))
1824                         fast_xmit(rbufp, MODE_ACTIVE, 0, restrict_mask);
1825                 if (peer->flags & FLAG_PREEMPT) {
1826                         if (unpeer_digest_early) {
1827                                 unpeer(peer);
1828                         }
1829                 }
1830 #ifdef AUTOKEY
1831                 else if (peer_clear_digest_early && peer->crypto) {
1832                         peer_clear(peer, "AUTH");
1833                 }
1834 #endif  /* AUTOKEY */
1835                 return;
1836         }
1837
1838         /*
1839          * For broadcast packets:
1840          *
1841          * HMS: This next line never made much sense to me, even
1842          * when it was up higher:
1843          *   If an initial volley, bail out now and let the
1844          *   client do its stuff.
1845          *
1846          * If the packet has not failed authentication, then
1847          * - if the origin timestamp is nonzero this is an
1848          *   interleaved broadcast, so restart the protocol.
1849          * - else, this is not an interleaved broadcast packet.
1850          */
1851         if (hismode == MODE_BROADCAST) {
1852                 if (   is_authentic == AUTH_OK
1853                     || is_authentic == AUTH_NONE) {
1854                         if (!L_ISZERO(&p_org)) {
1855                                 if (!(peer->flags & FLAG_XB)) {
1856                                         msyslog(LOG_INFO,
1857                                                 "receive: Broadcast server at %s is in interleave mode",
1858                                                 ntoa(&peer->srcadr));
1859                                         peer->flags |= FLAG_XB;
1860                                         peer->aorg = p_xmt;
1861                                         peer->borg = rbufp->recv_time;
1862                                         report_event(PEVNT_XLEAVE, peer, NULL);
1863                                         return;
1864                                 }
1865                         } else if (peer->flags & FLAG_XB) {
1866                                 msyslog(LOG_INFO,
1867                                         "receive: Broadcast server at %s is no longer in interleave mode",
1868                                         ntoa(&peer->srcadr));
1869                                 peer->flags &= ~FLAG_XB;
1870                         }
1871                 } else {
1872                         msyslog(LOG_INFO,
1873                                 "receive: Bad broadcast auth (%d) from %s",
1874                                 is_authentic, ntoa(&peer->srcadr));
1875                 }
1876
1877                 /*
1878                  * Now that we know the packet is correctly authenticated,
1879                  * update peer->bxmt.
1880                  */
1881                 peer->bxmt = p_xmt;
1882         }
1883
1884
1885         /*
1886         ** Update the state variables.
1887         */
1888         if (peer->flip == 0) {
1889                 if (hismode != MODE_BROADCAST)
1890                         peer->rec = p_xmt;
1891                 peer->dst = rbufp->recv_time;
1892         }
1893         peer->xmt = p_xmt;
1894
1895         /*
1896          * Set the peer ppoll to the maximum of the packet ppoll and the
1897          * peer minpoll. If a kiss-o'-death, set the peer minpoll to
1898          * this maximum and advance the headway to give the sender some
1899          * headroom. Very intricate.
1900          */
1901
1902         /*
1903          * Check for any kiss codes. Note this is only used when a server
1904          * responds to a packet request
1905          */
1906
1907         kissCode = kiss_code_check(hisleap, hisstratum, hismode, pkt->refid);
1908
1909         /*
1910          * Check to see if this is a RATE Kiss Code
1911          * Currently this kiss code will accept whatever poll
1912          * rate that the server sends
1913          */
1914         peer->ppoll = max(peer->minpoll, pkt->ppoll);
1915         if (kissCode == RATEKISS) {
1916                 peer->selbroken++;      /* Increment the KoD count */
1917                 report_event(PEVNT_RATE, peer, NULL);
1918                 if (pkt->ppoll > peer->minpoll)
1919                         peer->minpoll = peer->ppoll;
1920                 peer->burst = peer->retry = 0;
1921                 peer->throttle = (NTP_SHIFT + 1) * (1 << peer->minpoll);
1922                 poll_update(peer, pkt->ppoll);
1923                 return;                         /* kiss-o'-death */
1924         }
1925         if (kissCode != NOKISS) {
1926                 peer->selbroken++;      /* Increment the KoD count */
1927                 return;         /* Drop any other kiss code packets */
1928         }
1929
1930
1931         /*
1932          * XXX
1933          */
1934
1935
1936         /*
1937          * If:
1938          *      - this is a *cast (uni-, broad-, or m-) server packet
1939          *      - and it's symmetric-key authenticated
1940          * then see if the sender's IP is trusted for this keyid.
1941          * If it is, great - nothing special to do here.
1942          * Otherwise, we should report and bail.
1943          *
1944          * Autokey-authenticated packets are accepted.
1945          */
1946
1947         switch (hismode) {
1948             case MODE_SERVER:           /* server mode */
1949             case MODE_BROADCAST:        /* broadcast mode */
1950             case MODE_ACTIVE:           /* symmetric active mode */
1951             case MODE_PASSIVE:          /* symmetric passive mode */
1952                 if (   is_authentic == AUTH_OK
1953                     && skeyid
1954                     && skeyid <= NTP_MAXKEY
1955                     && !authistrustedip(skeyid, &peer->srcadr)) {
1956                         report_event(PEVNT_AUTH, peer, "authIP");
1957                         peer->badauth++;
1958                         return;
1959                 }
1960                 break;
1961
1962             case MODE_CLIENT:           /* client mode */
1963 #if 0           /* At this point, MODE_CONTROL is overloaded by MODE_BCLIENT */
1964             case MODE_CONTROL:          /* control mode */
1965 #endif
1966             case MODE_PRIVATE:          /* private mode */
1967             case MODE_BCLIENT:          /* broadcast client mode */
1968                 break;
1969
1970             case MODE_UNSPEC:           /* unspecified (old version) */
1971             default:
1972                 msyslog(LOG_INFO,
1973                         "receive: Unexpected mode (%d) in packet from %s",
1974                         hismode, ntoa(&peer->srcadr));
1975                 break;
1976         }
1977
1978
1979         /*
1980          * That was hard and I am sweaty, but the packet is squeaky
1981          * clean. Get on with real work.
1982          */
1983         peer->timereceived = current_time;
1984         peer->timelastrec = current_time;
1985         if (is_authentic == AUTH_OK)
1986                 peer->flags |= FLAG_AUTHENTIC;
1987         else
1988                 peer->flags &= ~FLAG_AUTHENTIC;
1989
1990 #ifdef AUTOKEY
1991         /*
1992          * More autokey dance. The rules of the cha-cha are as follows:
1993          *
1994          * 1. If there is no key or the key is not auto, do nothing.
1995          *
1996          * 2. If this packet is in response to the one just previously
1997          *    sent or from a broadcast server, do the extension fields.
1998          *    Otherwise, assume bogosity and bail out.
1999          *
2000          * 3. If an extension field contains a verified signature, it is
2001          *    self-authenticated and we sit the dance.
2002          *
2003          * 4. If this is a server reply, check only to see that the
2004          *    transmitted key ID matches the received key ID.
2005          *
2006          * 5. Check to see that one or more hashes of the current key ID
2007          *    matches the previous key ID or ultimate original key ID
2008          *    obtained from the broadcaster or symmetric peer. If no
2009          *    match, sit the dance and call for new autokey values.
2010          *
2011          * In case of crypto error, fire the orchestra, stop dancing and
2012          * restart the protocol.
2013          */
2014         if (peer->flags & FLAG_SKEY) {
2015                 /*
2016                  * Decrement remaining autokey hashes. This isn't
2017                  * perfect if a packet is lost, but results in no harm.
2018                  */
2019                 ap = (struct autokey *)peer->recval.ptr;
2020                 if (ap != NULL) {
2021                         if (ap->seq > 0)
2022                                 ap->seq--;
2023                 }
2024                 peer->flash |= TEST8;
2025                 rval = crypto_recv(peer, rbufp);
2026                 if (rval == XEVNT_OK) {
2027                         peer->unreach = 0;
2028                 } else {
2029                         if (rval == XEVNT_ERR) {
2030                                 report_event(PEVNT_RESTART, peer,
2031                                     "crypto error");
2032                                 peer_clear(peer, "CRYP");
2033                                 peer->flash |= TEST9;   /* bad crypt */
2034                                 if (peer->flags & FLAG_PREEMPT) {
2035                                         if (unpeer_crypto_early) {
2036                                                 unpeer(peer);
2037                                         }
2038                                 }
2039                         }
2040                         return;
2041                 }
2042
2043                 /*
2044                  * If server mode, verify the receive key ID matches
2045                  * the transmit key ID.
2046                  */
2047                 if (hismode == MODE_SERVER) {
2048                         if (skeyid == peer->keyid)
2049                                 peer->flash &= ~TEST8;
2050
2051                 /*
2052                  * If an extension field is present, verify only that it
2053                  * has been correctly signed. We don't need a sequence
2054                  * check here, but the sequence continues.
2055                  */
2056                 } else if (!(peer->flash & TEST8)) {
2057                         peer->pkeyid = skeyid;
2058
2059                 /*
2060                  * Now the fun part. Here, skeyid is the current ID in
2061                  * the packet, pkeyid is the ID in the last packet and
2062                  * tkeyid is the hash of skeyid. If the autokey values
2063                  * have not been received, this is an automatic error.
2064                  * If so, check that the tkeyid matches pkeyid. If not,
2065                  * hash tkeyid and try again. If the number of hashes
2066                  * exceeds the number remaining in the sequence, declare
2067                  * a successful failure and refresh the autokey values.
2068                  */
2069                 } else if (ap != NULL) {
2070                         int i;
2071
2072                         for (i = 0; ; i++) {
2073                                 if (   tkeyid == peer->pkeyid
2074                                     || tkeyid == ap->key) {
2075                                         peer->flash &= ~TEST8;
2076                                         peer->pkeyid = skeyid;
2077                                         ap->seq -= i;
2078                                         break;
2079                                 }
2080                                 if (i > ap->seq) {
2081                                         peer->crypto &=
2082                                             ~CRYPTO_FLAG_AUTO;
2083                                         break;
2084                                 }
2085                                 tkeyid = session_key(
2086                                     &rbufp->recv_srcadr, dstadr_sin,
2087                                     tkeyid, pkeyid, 0);
2088                         }
2089                         if (peer->flash & TEST8)
2090                                 report_event(PEVNT_AUTH, peer, "keylist");
2091                 }
2092                 if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
2093                         peer->flash |= TEST8;   /* bad autokey */
2094
2095                 /*
2096                  * The maximum lifetime of the protocol is about one
2097                  * week before restarting the Autokey protocol to
2098                  * refresh certificates and leapseconds values.
2099                  */
2100                 if (current_time > peer->refresh) {
2101                         report_event(PEVNT_RESTART, peer,
2102                             "crypto refresh");
2103                         peer_clear(peer, "TIME");
2104                         return;
2105                 }
2106         }
2107 #endif  /* AUTOKEY */
2108
2109         /*
2110          * The dance is complete and the flash bits have been lit. Toss
2111          * the packet over the fence for processing, which may light up
2112          * more flashers.
2113          */
2114         process_packet(peer, pkt, rbufp->recv_length);
2115
2116         /*
2117          * In interleaved mode update the state variables. Also adjust the
2118          * transmit phase to avoid crossover.
2119          */
2120         if (peer->flip != 0) {
2121                 peer->rec = p_rec;
2122                 peer->dst = rbufp->recv_time;
2123                 if (peer->nextdate - current_time < (1U << min(peer->ppoll,
2124                     peer->hpoll)) / 2)
2125                         peer->nextdate++;
2126                 else
2127                         peer->nextdate--;
2128         }
2129 }
2130
2131
2132 /*
2133  * process_packet - Packet Procedure, a la Section 3.4.4 of RFC-1305
2134  *      Or almost, at least.  If we're in here we have a reasonable
2135  *      expectation that we will be having a long term
2136  *      relationship with this host.
2137  */
2138 void
2139 process_packet(
2140         register struct peer *peer,
2141         register struct pkt *pkt,
2142         u_int   len
2143         )
2144 {
2145         double  t34, t21;
2146         double  p_offset, p_del, p_disp;
2147         l_fp    p_rec, p_xmt, p_org, p_reftime, ci;
2148         u_char  pmode, pleap, pversion, pstratum;
2149         char    statstr[NTP_MAXSTRLEN];
2150 #ifdef ASSYM
2151         int     itemp;
2152         double  etemp, ftemp, td;
2153 #endif /* ASSYM */
2154
2155 #if 0
2156         sys_processed++;
2157         peer->processed++;
2158 #endif
2159         p_del = FPTOD(NTOHS_FP(pkt->rootdelay));
2160         p_offset = 0;
2161         p_disp = FPTOD(NTOHS_FP(pkt->rootdisp));
2162         NTOHL_FP(&pkt->reftime, &p_reftime);
2163         NTOHL_FP(&pkt->org, &p_org);
2164         NTOHL_FP(&pkt->rec, &p_rec);
2165         NTOHL_FP(&pkt->xmt, &p_xmt);
2166         pmode = PKT_MODE(pkt->li_vn_mode);
2167         pleap = PKT_LEAP(pkt->li_vn_mode);
2168         pversion = PKT_VERSION(pkt->li_vn_mode);
2169         pstratum = PKT_TO_STRATUM(pkt->stratum);
2170
2171         /**/
2172
2173         /**/
2174
2175         /*
2176          * Verify the server is synchronized; that is, the leap bits,
2177          * stratum and root distance are valid.
2178          */
2179         if (   pleap == LEAP_NOTINSYNC          /* test 6 */
2180             || pstratum < sys_floor || pstratum >= sys_ceiling)
2181                 peer->flash |= TEST6;           /* bad synch or strat */
2182         if (p_del / 2 + p_disp >= MAXDISPERSE)  /* test 7 */
2183                 peer->flash |= TEST7;           /* bad header */
2184
2185         /*
2186          * If any tests fail at this point, the packet is discarded.
2187          * Note that some flashers may have already been set in the
2188          * receive() routine.
2189          */
2190         if (peer->flash & PKT_TEST_MASK) {
2191                 peer->seldisptoolarge++;
2192                 DPRINTF(1, ("packet: flash header %04x\n",
2193                             peer->flash));
2194                 return;
2195         }
2196
2197         /**/
2198
2199 #if 1
2200         sys_processed++;
2201         peer->processed++;
2202 #endif
2203
2204         /*
2205          * Capture the header values in the client/peer association..
2206          */
2207         record_raw_stats(&peer->srcadr, peer->dstadr ?
2208             &peer->dstadr->sin : NULL,
2209             &p_org, &p_rec, &p_xmt, &peer->dst,
2210             pleap, pversion, pmode, pstratum, pkt->ppoll, pkt->precision,
2211             p_del, p_disp, pkt->refid);
2212         peer->leap = pleap;
2213         peer->stratum = min(pstratum, STRATUM_UNSPEC);
2214         peer->pmode = pmode;
2215         peer->precision = pkt->precision;
2216         peer->rootdelay = p_del;
2217         peer->rootdisp = p_disp;
2218         peer->refid = pkt->refid;               /* network byte order */
2219         peer->reftime = p_reftime;
2220
2221         /*
2222          * First, if either burst mode is armed, enable the burst.
2223          * Compute the headway for the next packet and delay if
2224          * necessary to avoid exceeding the threshold.
2225          */
2226         if (peer->retry > 0) {
2227                 peer->retry = 0;
2228                 if (peer->reach)
2229                         peer->burst = min(1 << (peer->hpoll -
2230                             peer->minpoll), NTP_SHIFT) - 1;
2231                 else
2232                         peer->burst = NTP_IBURST - 1;
2233                 if (peer->burst > 0)
2234                         peer->nextdate = current_time;
2235         }
2236         poll_update(peer, peer->hpoll);
2237
2238         /**/
2239
2240         /*
2241          * If the peer was previously unreachable, raise a trap. In any
2242          * case, mark it reachable.
2243          */
2244         if (!peer->reach) {
2245                 report_event(PEVNT_REACH, peer, NULL);
2246                 peer->timereachable = current_time;
2247         }
2248         peer->reach |= 1;
2249
2250         /*
2251          * For a client/server association, calculate the clock offset,
2252          * roundtrip delay and dispersion. The equations are reordered
2253          * from the spec for more efficient use of temporaries. For a
2254          * broadcast association, offset the last measurement by the
2255          * computed delay during the client/server volley. Note the
2256          * computation of dispersion includes the system precision plus
2257          * that due to the frequency error since the origin time.
2258          *
2259          * It is very important to respect the hazards of overflow. The
2260          * only permitted operation on raw timestamps is subtraction,
2261          * where the result is a signed quantity spanning from 68 years
2262          * in the past to 68 years in the future. To avoid loss of
2263          * precision, these calculations are done using 64-bit integer
2264          * arithmetic. However, the offset and delay calculations are
2265          * sums and differences of these first-order differences, which
2266          * if done using 64-bit integer arithmetic, would be valid over
2267          * only half that span. Since the typical first-order
2268          * differences are usually very small, they are converted to 64-
2269          * bit doubles and all remaining calculations done in floating-
2270          * double arithmetic. This preserves the accuracy while
2271          * retaining the 68-year span.
2272          *
2273          * There are three interleaving schemes, basic, interleaved
2274          * symmetric and interleaved broadcast. The timestamps are
2275          * idioscyncratically different. See the onwire briefing/white
2276          * paper at www.eecis.udel.edu/~mills for details.
2277          *
2278          * Interleaved symmetric mode
2279          * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
2280          * t4 = peer->dst
2281          */
2282         if (peer->flip != 0) {
2283                 ci = p_xmt;                             /* t3 - t4 */
2284                 L_SUB(&ci, &peer->dst);
2285                 LFPTOD(&ci, t34);
2286                 ci = p_rec;                             /* t2 - t1 */
2287                 if (peer->flip > 0)
2288                         L_SUB(&ci, &peer->borg);
2289                 else
2290                         L_SUB(&ci, &peer->aorg);
2291                 LFPTOD(&ci, t21);
2292                 p_del = t21 - t34;
2293                 p_offset = (t21 + t34) / 2.;
2294                 if (p_del < 0 || p_del > 1.) {
2295                         snprintf(statstr, sizeof(statstr),
2296                             "t21 %.6f t34 %.6f", t21, t34);
2297                         report_event(PEVNT_XERR, peer, statstr);
2298                         return;
2299                 }
2300
2301         /*
2302          * Broadcast modes
2303          */
2304         } else if (peer->pmode == MODE_BROADCAST) {
2305
2306                 /*
2307                  * Interleaved broadcast mode. Use interleaved timestamps.
2308                  * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
2309                  */
2310                 if (peer->flags & FLAG_XB) {
2311                         ci = p_org;                     /* delay */
2312                         L_SUB(&ci, &peer->aorg);
2313                         LFPTOD(&ci, t34);
2314                         ci = p_org;                     /* t2 - t1 */
2315                         L_SUB(&ci, &peer->borg);
2316                         LFPTOD(&ci, t21);
2317                         peer->aorg = p_xmt;
2318                         peer->borg = peer->dst;
2319                         if (t34 < 0 || t34 > 1.) {
2320                                 /* drop all if in the initial volley */
2321                                 if (FLAG_BC_VOL & peer->flags)
2322                                         goto bcc_init_volley_fail;
2323                                 snprintf(statstr, sizeof(statstr),
2324                                     "offset %.6f delay %.6f", t21, t34);
2325                                 report_event(PEVNT_XERR, peer, statstr);
2326                                 return;
2327                         }
2328                         p_offset = t21;
2329                         peer->xleave = t34;
2330
2331                 /*
2332                  * Basic broadcast - use direct timestamps.
2333                  * t3 = p_xmt, t4 = peer->dst
2334                  */
2335                 } else {
2336                         ci = p_xmt;             /* t3 - t4 */
2337                         L_SUB(&ci, &peer->dst);
2338                         LFPTOD(&ci, t34);
2339                         p_offset = t34;
2340                 }
2341
2342                 /*
2343                  * When calibration is complete and the clock is
2344                  * synchronized, the bias is calculated as the difference
2345                  * between the unicast timestamp and the broadcast
2346                  * timestamp. This works for both basic and interleaved
2347                  * modes.
2348                  * [Bug 3031] Don't keep this peer when the delay 
2349                  * calculation gives reason to suspect clock steps.
2350                  * This is assumed for delays > 50ms.
2351                  */
2352                 if (FLAG_BC_VOL & peer->flags) {
2353                         peer->flags &= ~FLAG_BC_VOL;
2354                         peer->delay = fabs(peer->offset - p_offset) * 2;
2355                         DPRINTF(2, ("broadcast volley: initial delay=%.6f\n",
2356                                 peer->delay));
2357                         if (peer->delay > fabs(sys_bdelay)) {
2358                 bcc_init_volley_fail:
2359                                 DPRINTF(2, ("%s", "broadcast volley: initial delay exceeds limit\n"));
2360                                 unpeer(peer);
2361                                 return;
2362                         }
2363                 }
2364                 peer->nextdate = current_time + (1u << peer->ppoll) - 2u;
2365                 p_del = peer->delay;
2366                 p_offset += p_del / 2;
2367
2368
2369         /*
2370          * Basic mode, otherwise known as the old fashioned way.
2371          *
2372          * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2373          */
2374         } else {
2375                 ci = p_xmt;                             /* t3 - t4 */
2376                 L_SUB(&ci, &peer->dst);
2377                 LFPTOD(&ci, t34);
2378                 ci = p_rec;                             /* t2 - t1 */
2379                 L_SUB(&ci, &p_org);
2380                 LFPTOD(&ci, t21);
2381                 p_del = fabs(t21 - t34);
2382                 p_offset = (t21 + t34) / 2.;
2383         }
2384         p_del = max(p_del, LOGTOD(sys_precision));
2385         p_disp = LOGTOD(sys_precision) + LOGTOD(peer->precision) +
2386             clock_phi * p_del;
2387
2388 #if ASSYM
2389         /*
2390          * This code calculates the outbound and inbound data rates by
2391          * measuring the differences between timestamps at different
2392          * packet lengths. This is helpful in cases of large asymmetric
2393          * delays commonly experienced on deep space communication
2394          * links.
2395          */
2396         if (peer->t21_last > 0 && peer->t34_bytes > 0) {
2397                 itemp = peer->t21_bytes - peer->t21_last;
2398                 if (itemp > 25) {
2399                         etemp = t21 - peer->t21;
2400                         if (fabs(etemp) > 1e-6) {
2401                                 ftemp = itemp / etemp;
2402                                 if (ftemp > 1000.)
2403                                         peer->r21 = ftemp;
2404                         }
2405                 }
2406                 itemp = len - peer->t34_bytes;
2407                 if (itemp > 25) {
2408                         etemp = -t34 - peer->t34;
2409                         if (fabs(etemp) > 1e-6) {
2410                                 ftemp = itemp / etemp;
2411                                 if (ftemp > 1000.)
2412                                         peer->r34 = ftemp;
2413                         }
2414                 }
2415         }
2416
2417         /*
2418          * The following section compensates for different data rates on
2419          * the outbound (d21) and inbound (t34) directions. To do this,
2420          * it finds t such that r21 * t - r34 * (d - t) = 0, where d is
2421          * the roundtrip delay. Then it calculates the correction as a
2422          * fraction of d.
2423          */
2424         peer->t21 = t21;
2425         peer->t21_last = peer->t21_bytes;
2426         peer->t34 = -t34;
2427         peer->t34_bytes = len;
2428         DPRINTF(2, ("packet: t21 %.9lf %d t34 %.9lf %d\n", peer->t21,
2429                     peer->t21_bytes, peer->t34, peer->t34_bytes));
2430         if (peer->r21 > 0 && peer->r34 > 0 && p_del > 0) {
2431                 if (peer->pmode != MODE_BROADCAST)
2432                         td = (peer->r34 / (peer->r21 + peer->r34) -
2433                             .5) * p_del;
2434                 else
2435                         td = 0;
2436
2437                 /*
2438                  * Unfortunately, in many cases the errors are
2439                  * unacceptable, so for the present the rates are not
2440                  * used. In future, we might find conditions where the
2441                  * calculations are useful, so this should be considered
2442                  * a work in progress.
2443                  */
2444                 t21 -= td;
2445                 t34 -= td;
2446                 DPRINTF(2, ("packet: del %.6lf r21 %.1lf r34 %.1lf %.6lf\n",
2447                             p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2448                             td));
2449         }
2450 #endif /* ASSYM */
2451
2452         /*
2453          * That was awesome. Now hand off to the clock filter.
2454          */
2455         clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2456
2457         /*
2458          * If we are in broadcast calibrate mode, return to broadcast
2459          * client mode when the client is fit and the autokey dance is
2460          * complete.
2461          */
2462         if (   (FLAG_BC_VOL & peer->flags)
2463             && MODE_CLIENT == peer->hmode
2464             && !(TEST11 & peer_unfit(peer))) {  /* distance exceeded */
2465 #ifdef AUTOKEY
2466                 if (peer->flags & FLAG_SKEY) {
2467                         if (!(~peer->crypto & CRYPTO_FLAG_ALL))
2468                                 peer->hmode = MODE_BCLIENT;
2469                 } else {
2470                         peer->hmode = MODE_BCLIENT;
2471                 }
2472 #else   /* !AUTOKEY follows */
2473                 peer->hmode = MODE_BCLIENT;
2474 #endif  /* !AUTOKEY */
2475         }
2476 }
2477
2478
2479 /*
2480  * clock_update - Called at system process update intervals.
2481  */
2482 static void
2483 clock_update(
2484         struct peer *peer       /* peer structure pointer */
2485         )
2486 {
2487         double  dtemp;
2488         l_fp    now;
2489 #ifdef HAVE_LIBSCF_H
2490         char    *fmri;
2491 #endif /* HAVE_LIBSCF_H */
2492
2493         /*
2494          * Update the system state variables. We do this very carefully,
2495          * as the poll interval might need to be clamped differently.
2496          */
2497         sys_peer = peer;
2498         sys_epoch = peer->epoch;
2499         if (sys_poll < peer->minpoll)
2500                 sys_poll = peer->minpoll;
2501         if (sys_poll > peer->maxpoll)
2502                 sys_poll = peer->maxpoll;
2503         poll_update(peer, sys_poll);
2504         sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
2505         if (   peer->stratum == STRATUM_REFCLOCK
2506             || peer->stratum == STRATUM_UNSPEC)
2507                 sys_refid = peer->refid;
2508         else
2509                 sys_refid = addr2refid(&peer->srcadr);
2510         /*
2511          * Root Dispersion (E) is defined (in RFC 5905) as:
2512          *
2513          * E = p.epsilon_r + p.epsilon + p.psi + PHI*(s.t - p.t) + |THETA|
2514          *
2515          * where:
2516          *  p.epsilon_r is the PollProc's root dispersion
2517          *  p.epsilon   is the PollProc's dispersion
2518          *  p.psi       is the PollProc's jitter
2519          *  THETA       is the combined offset
2520          *
2521          * NB: Think Hard about where these numbers come from and
2522          * what they mean.  When did peer->update happen?  Has anything
2523          * interesting happened since then?  What values are the most
2524          * defensible?  Why?
2525          *
2526          * DLM thinks this equation is probably the best of all worse choices.
2527          */
2528         dtemp   = peer->rootdisp
2529                 + peer->disp
2530                 + sys_jitter
2531                 + clock_phi * (current_time - peer->update)
2532                 + fabs(sys_offset);
2533
2534         if (dtemp > sys_mindisp)
2535                 sys_rootdisp = dtemp;
2536         else
2537                 sys_rootdisp = sys_mindisp;
2538         sys_rootdelay = peer->delay + peer->rootdelay;
2539         sys_reftime = peer->dst;
2540
2541         DPRINTF(1, ("clock_update: at %lu sample %lu associd %d\n",
2542                     current_time, peer->epoch, peer->associd));
2543
2544         /*
2545          * Comes now the moment of truth. Crank the clock discipline and
2546          * see what comes out.
2547          */
2548         switch (local_clock(peer, sys_offset)) {
2549
2550         /*
2551          * Clock exceeds panic threshold. Life as we know it ends.
2552          */
2553         case -1:
2554 #ifdef HAVE_LIBSCF_H
2555                 /*
2556                  * For Solaris enter the maintenance mode.
2557                  */
2558                 if ((fmri = getenv("SMF_FMRI")) != NULL) {
2559                         if (smf_maintain_instance(fmri, 0) < 0) {
2560                                 printf("smf_maintain_instance: %s\n",
2561                                     scf_strerror(scf_error()));
2562                                 exit(1);
2563                         }
2564                         /*
2565                          * Sleep until SMF kills us.
2566                          */
2567                         for (;;)
2568                                 pause();
2569                 }
2570 #endif /* HAVE_LIBSCF_H */
2571                 exit (-1);
2572                 /* not reached */
2573
2574         /*
2575          * Clock was stepped. Flush all time values of all peers.
2576          */
2577         case 2:
2578                 clear_all();
2579                 set_sys_leap(LEAP_NOTINSYNC);
2580                 sys_stratum = STRATUM_UNSPEC;
2581                 memcpy(&sys_refid, "STEP", 4);
2582                 sys_rootdelay = 0;
2583                 sys_rootdisp = 0;
2584                 L_CLR(&sys_reftime);
2585                 sys_jitter = LOGTOD(sys_precision);
2586                 leapsec_reset_frame();
2587                 break;
2588
2589         /*
2590          * Clock was slewed. Handle the leapsecond stuff.
2591          */
2592         case 1:
2593
2594                 /*
2595                  * If this is the first time the clock is set, reset the
2596                  * leap bits. If crypto, the timer will goose the setup
2597                  * process.
2598                  */
2599                 if (sys_leap == LEAP_NOTINSYNC) {
2600                         set_sys_leap(LEAP_NOWARNING);
2601 #ifdef AUTOKEY
2602                         if (crypto_flags)
2603                                 crypto_update();
2604 #endif  /* AUTOKEY */
2605                         /*
2606                          * If our parent process is waiting for the
2607                          * first clock sync, send them home satisfied.
2608                          */
2609 #ifdef HAVE_WORKING_FORK
2610                         if (waitsync_fd_to_close != -1) {
2611                                 close(waitsync_fd_to_close);
2612                                 waitsync_fd_to_close = -1;
2613                                 DPRINTF(1, ("notified parent --wait-sync is done\n"));
2614                         }
2615 #endif /* HAVE_WORKING_FORK */
2616
2617                 }
2618
2619                 /*
2620                  * If there is no leap second pending and the number of
2621                  * survivor leap bits is greater than half the number of
2622                  * survivors, try to schedule a leap for the end of the
2623                  * current month. (This only works if no leap second for
2624                  * that range is in the table, so doing this more than
2625                  * once is mostly harmless.)
2626                  */
2627                 if (leapsec == LSPROX_NOWARN) {
2628                         if (   leap_vote_ins > leap_vote_del
2629                             && leap_vote_ins > sys_survivors / 2) {
2630                                 get_systime(&now);
2631                                 leapsec_add_dyn(TRUE, now.l_ui, NULL);
2632                         }
2633                         if (   leap_vote_del > leap_vote_ins
2634                             && leap_vote_del > sys_survivors / 2) {
2635                                 get_systime(&now);
2636                                 leapsec_add_dyn(FALSE, now.l_ui, NULL);
2637                         }
2638                 }
2639                 break;
2640
2641         /*
2642          * Popcorn spike or step threshold exceeded. Pretend it never
2643          * happened.
2644          */
2645         default:
2646                 break;
2647         }
2648 }
2649
2650
2651 /*
2652  * poll_update - update peer poll interval
2653  */
2654 void
2655 poll_update(
2656         struct peer *peer,      /* peer structure pointer */
2657         u_char  mpoll
2658         )
2659 {
2660         u_long  next, utemp;
2661         u_char  hpoll;
2662
2663         /*
2664          * This routine figures out when the next poll should be sent.
2665          * That turns out to be wickedly complicated. One problem is
2666          * that sometimes the time for the next poll is in the past when
2667          * the poll interval is reduced. We watch out for races here
2668          * between the receive process and the poll process.
2669          *
2670          * Clamp the poll interval between minpoll and maxpoll.
2671          */
2672         hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
2673
2674 #ifdef AUTOKEY
2675         /*
2676          * If during the crypto protocol the poll interval has changed,
2677          * the lifetimes in the key list are probably bogus. Purge the
2678          * the key list and regenerate it later.
2679          */
2680         if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
2681                 key_expire(peer);
2682 #endif  /* AUTOKEY */
2683         peer->hpoll = hpoll;
2684
2685         /*
2686          * There are three variables important for poll scheduling, the
2687          * current time (current_time), next scheduled time (nextdate)
2688          * and the earliest time (utemp). The earliest time is 2 s
2689          * seconds, but could be more due to rate management. When
2690          * sending in a burst, use the earliest time. When not in a
2691          * burst but with a reply pending, send at the earliest time
2692          * unless the next scheduled time has not advanced. This can
2693          * only happen if multiple replies are pending in the same
2694          * response interval. Otherwise, send at the later of the next
2695          * scheduled time and the earliest time.
2696          *
2697          * Now we figure out if there is an override. If a burst is in
2698          * progress and we get called from the receive process, just
2699          * slink away. If called from the poll process, delay 1 s for a
2700          * reference clock, otherwise 2 s.
2701          */
2702         utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
2703             (1 << peer->minpoll), ntp_minpkt);
2704         if (peer->burst > 0) {
2705                 if (peer->nextdate > current_time)
2706                         return;
2707 #ifdef REFCLOCK
2708                 else if (peer->flags & FLAG_REFCLOCK)
2709                         peer->nextdate = current_time + RESP_DELAY;
2710 #endif /* REFCLOCK */
2711                 else
2712                         peer->nextdate = utemp;
2713
2714 #ifdef AUTOKEY
2715         /*
2716          * If a burst is not in progress and a crypto response message
2717          * is pending, delay 2 s, but only if this is a new interval.
2718          */
2719         } else if (peer->cmmd != NULL) {
2720                 if (peer->nextdate > current_time) {
2721                         if (peer->nextdate + ntp_minpkt != utemp)
2722                                 peer->nextdate = utemp;
2723                 } else {
2724                         peer->nextdate = utemp;
2725                 }
2726 #endif  /* AUTOKEY */
2727
2728         /*
2729          * The ordinary case. If a retry, use minpoll; if unreachable,
2730          * use host poll; otherwise, use the minimum of host and peer
2731          * polls; In other words, oversampling is okay but
2732          * understampling is evil. Use the maximum of this value and the
2733          * headway. If the average headway is greater than the headway
2734          * threshold, increase the headway by the minimum interval.
2735          */
2736         } else {
2737                 if (peer->retry > 0)
2738                         hpoll = peer->minpoll;
2739                 else if (!(peer->reach))
2740                         hpoll = peer->hpoll;
2741                 else
2742                         hpoll = min(peer->ppoll, peer->hpoll);
2743 #ifdef REFCLOCK
2744                 if (peer->flags & FLAG_REFCLOCK)
2745                         next = 1 << hpoll;
2746                 else
2747 #endif /* REFCLOCK */
2748                         next = ((0x1000UL | (ntp_random() & 0x0ff)) <<
2749                             hpoll) >> 12;
2750                 next += peer->outdate;
2751                 if (next > utemp)
2752                         peer->nextdate = next;
2753                 else
2754                         peer->nextdate = utemp;
2755                 if (peer->throttle > (1 << peer->minpoll))
2756                         peer->nextdate += ntp_minpkt;
2757         }
2758         DPRINTF(2, ("poll_update: at %lu %s poll %d burst %d retry %d head %d early %lu next %lu\n",
2759                     current_time, ntoa(&peer->srcadr), peer->hpoll,
2760                     peer->burst, peer->retry, peer->throttle,
2761                     utemp - current_time, peer->nextdate -
2762                     current_time));
2763 }
2764
2765
2766 /*
2767  * peer_clear - clear peer filter registers.  See Section 3.4.8 of the
2768  * spec.
2769  */
2770 void
2771 peer_clear(
2772         struct peer *peer,              /* peer structure */
2773         const char *ident               /* tally lights */
2774         )
2775 {
2776         u_char  u;
2777         l_fp    bxmt = peer->bxmt;      /* bcast clients retain this! */
2778
2779 #ifdef AUTOKEY
2780         /*
2781          * If cryptographic credentials have been acquired, toss them to
2782          * Valhalla. Note that autokeys are ephemeral, in that they are
2783          * tossed immediately upon use. Therefore, the keylist can be
2784          * purged anytime without needing to preserve random keys. Note
2785          * that, if the peer is purged, the cryptographic variables are
2786          * purged, too. This makes it much harder to sneak in some
2787          * unauthenticated data in the clock filter.
2788          */
2789         key_expire(peer);
2790         if (peer->iffval != NULL)
2791                 BN_free(peer->iffval);
2792         value_free(&peer->cookval);
2793         value_free(&peer->recval);
2794         value_free(&peer->encrypt);
2795         value_free(&peer->sndval);
2796         if (peer->cmmd != NULL)
2797                 free(peer->cmmd);
2798         if (peer->subject != NULL)
2799                 free(peer->subject);
2800         if (peer->issuer != NULL)
2801                 free(peer->issuer);
2802 #endif /* AUTOKEY */
2803
2804         /*
2805          * Clear all values, including the optional crypto values above.
2806          */
2807         memset(CLEAR_TO_ZERO(peer), 0, LEN_CLEAR_TO_ZERO(peer));
2808         peer->ppoll = peer->maxpoll;
2809         peer->hpoll = peer->minpoll;
2810         peer->disp = MAXDISPERSE;
2811         peer->flash = peer_unfit(peer);
2812         peer->jitter = LOGTOD(sys_precision);
2813
2814         /* Don't throw away our broadcast replay protection */
2815         if (peer->hmode == MODE_BCLIENT)
2816                 peer->bxmt = bxmt;
2817
2818         /*
2819          * If interleave mode, initialize the alternate origin switch.
2820          */
2821         if (peer->flags & FLAG_XLEAVE)
2822                 peer->flip = 1;
2823         for (u = 0; u < NTP_SHIFT; u++) {
2824                 peer->filter_order[u] = u;
2825                 peer->filter_disp[u] = MAXDISPERSE;
2826         }
2827 #ifdef REFCLOCK
2828         if (!(peer->flags & FLAG_REFCLOCK)) {
2829 #endif
2830                 peer->leap = LEAP_NOTINSYNC;
2831                 peer->stratum = STRATUM_UNSPEC;
2832                 memcpy(&peer->refid, ident, 4);
2833 #ifdef REFCLOCK
2834         }
2835 #endif
2836
2837         /*
2838          * During initialization use the association count to spread out
2839          * the polls at one-second intervals. Passive associations'
2840          * first poll is delayed by the "discard minimum" to avoid rate
2841          * limiting. Other post-startup new or cleared associations
2842          * randomize the first poll over the minimum poll interval to
2843          * avoid implosion.
2844          */
2845         peer->nextdate = peer->update = peer->outdate = current_time;
2846         if (initializing) {
2847                 peer->nextdate += peer_associations;
2848         } else if (MODE_PASSIVE == peer->hmode) {
2849                 peer->nextdate += ntp_minpkt;
2850         } else {
2851                 peer->nextdate += ntp_random() % peer->minpoll;
2852         }
2853 #ifdef AUTOKEY
2854         peer->refresh = current_time + (1 << NTP_REFRESH);
2855 #endif  /* AUTOKEY */
2856         DPRINTF(1, ("peer_clear: at %ld next %ld associd %d refid %s\n",
2857                     current_time, peer->nextdate, peer->associd,
2858                     ident));
2859 }
2860
2861
2862 /*
2863  * clock_filter - add incoming clock sample to filter register and run
2864  *                the filter procedure to find the best sample.
2865  */
2866 void
2867 clock_filter(
2868         struct peer *peer,              /* peer structure pointer */
2869         double  sample_offset,          /* clock offset */
2870         double  sample_delay,           /* roundtrip delay */
2871         double  sample_disp             /* dispersion */
2872         )
2873 {
2874         double  dst[NTP_SHIFT];         /* distance vector */
2875         int     ord[NTP_SHIFT];         /* index vector */
2876         int     i, j, k, m;
2877         double  dtemp, etemp;
2878         char    tbuf[80];
2879
2880         /*
2881          * A sample consists of the offset, delay, dispersion and epoch
2882          * of arrival. The offset and delay are determined by the on-
2883          * wire protocol. The dispersion grows from the last outbound
2884          * packet to the arrival of this one increased by the sum of the
2885          * peer precision and the system precision as required by the
2886          * error budget. First, shift the new arrival into the shift
2887          * register discarding the oldest one.
2888          */
2889         j = peer->filter_nextpt;
2890         peer->filter_offset[j] = sample_offset;
2891         peer->filter_delay[j] = sample_delay;
2892         peer->filter_disp[j] = sample_disp;
2893         peer->filter_epoch[j] = current_time;
2894         j = (j + 1) % NTP_SHIFT;
2895         peer->filter_nextpt = j;
2896
2897         /*
2898          * Update dispersions since the last update and at the same
2899          * time initialize the distance and index lists. Since samples
2900          * become increasingly uncorrelated beyond the Allan intercept,
2901          * only under exceptional cases will an older sample be used.
2902          * Therefore, the distance list uses a compound metric. If the
2903          * dispersion is greater than the maximum dispersion, clamp the
2904          * distance at that value. If the time since the last update is
2905          * less than the Allan intercept use the delay; otherwise, use
2906          * the sum of the delay and dispersion.
2907          */
2908         dtemp = clock_phi * (current_time - peer->update);
2909         peer->update = current_time;
2910         for (i = NTP_SHIFT - 1; i >= 0; i--) {
2911                 if (i != 0)
2912                         peer->filter_disp[j] += dtemp;
2913                 if (peer->filter_disp[j] >= MAXDISPERSE) {
2914                         peer->filter_disp[j] = MAXDISPERSE;
2915                         dst[i] = MAXDISPERSE;
2916                 } else if (peer->update - peer->filter_epoch[j] >
2917                     (u_long)ULOGTOD(allan_xpt)) {
2918                         dst[i] = peer->filter_delay[j] +
2919                             peer->filter_disp[j];
2920                 } else {
2921                         dst[i] = peer->filter_delay[j];
2922                 }
2923                 ord[i] = j;
2924                 j = (j + 1) % NTP_SHIFT;
2925         }
2926
2927         /*
2928          * If the clock has stabilized, sort the samples by distance.
2929          */
2930         if (freq_cnt == 0) {
2931                 for (i = 1; i < NTP_SHIFT; i++) {
2932                         for (j = 0; j < i; j++) {
2933                                 if (dst[j] > dst[i]) {
2934                                         k = ord[j];
2935                                         ord[j] = ord[i];
2936                                         ord[i] = k;
2937                                         etemp = dst[j];
2938                                         dst[j] = dst[i];
2939                                         dst[i] = etemp;
2940                                 }
2941                         }
2942                 }
2943         }
2944
2945         /*
2946          * Copy the index list to the association structure so ntpq
2947          * can see it later. Prune the distance list to leave only
2948          * samples less than the maximum dispersion, which disfavors
2949          * uncorrelated samples older than the Allan intercept. To
2950          * further improve the jitter estimate, of the remainder leave
2951          * only samples less than the maximum distance, but keep at
2952          * least two samples for jitter calculation.
2953          */
2954         m = 0;
2955         for (i = 0; i < NTP_SHIFT; i++) {
2956                 peer->filter_order[i] = (u_char) ord[i];
2957                 if (   dst[i] >= MAXDISPERSE
2958                     || (m >= 2 && dst[i] >= sys_maxdist))
2959                         continue;
2960                 m++;
2961         }
2962
2963         /*
2964          * Compute the dispersion and jitter. The dispersion is weighted
2965          * exponentially by NTP_FWEIGHT (0.5) so it is normalized close
2966          * to 1.0. The jitter is the RMS differences relative to the
2967          * lowest delay sample.
2968          */
2969         peer->disp = peer->jitter = 0;
2970         k = ord[0];
2971         for (i = NTP_SHIFT - 1; i >= 0; i--) {
2972                 j = ord[i];
2973                 peer->disp = NTP_FWEIGHT * (peer->disp +
2974                     peer->filter_disp[j]);
2975                 if (i < m)
2976                         peer->jitter += DIFF(peer->filter_offset[j],
2977                             peer->filter_offset[k]);
2978         }
2979
2980         /*
2981          * If no acceptable samples remain in the shift register,
2982          * quietly tiptoe home leaving only the dispersion. Otherwise,
2983          * save the offset, delay and jitter. Note the jitter must not
2984          * be less than the precision.
2985          */
2986         if (m == 0) {
2987                 clock_select();
2988                 return;
2989         }
2990         etemp = fabs(peer->offset - peer->filter_offset[k]);
2991         peer->offset = peer->filter_offset[k];
2992         peer->delay = peer->filter_delay[k];
2993         if (m > 1)
2994                 peer->jitter /= m - 1;
2995         peer->jitter = max(SQRT(peer->jitter), LOGTOD(sys_precision));
2996
2997         /*
2998          * If the the new sample and the current sample are both valid
2999          * and the difference between their offsets exceeds CLOCK_SGATE
3000          * (3) times the jitter and the interval between them is less
3001          * than twice the host poll interval, consider the new sample
3002          * a popcorn spike and ignore it.
3003          */
3004         if (   peer->disp < sys_maxdist
3005             && peer->filter_disp[k] < sys_maxdist
3006             && etemp > CLOCK_SGATE * peer->jitter
3007             && peer->filter_epoch[k] - peer->epoch
3008                < 2. * ULOGTOD(peer->hpoll)) {
3009                 snprintf(tbuf, sizeof(tbuf), "%.6f s", etemp);
3010                 report_event(PEVNT_POPCORN, peer, tbuf);
3011                 return;
3012         }
3013
3014         /*
3015          * A new minimum sample is useful only if it is later than the
3016          * last one used. In this design the maximum lifetime of any
3017          * sample is not greater than eight times the poll interval, so
3018          * the maximum interval between minimum samples is eight
3019          * packets.
3020          */
3021         if (peer->filter_epoch[k] <= peer->epoch) {
3022         DPRINTF(2, ("clock_filter: old sample %lu\n", current_time -
3023                     peer->filter_epoch[k]));
3024                 return;
3025         }
3026         peer->epoch = peer->filter_epoch[k];
3027
3028         /*
3029          * The mitigated sample statistics are saved for later
3030          * processing. If not synchronized or not in a burst, tickle the
3031          * clock select algorithm.
3032          */
3033         record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
3034             peer->offset, peer->delay, peer->disp, peer->jitter);
3035         DPRINTF(1, ("clock_filter: n %d off %.6f del %.6f dsp %.6f jit %.6f\n",
3036                     m, peer->offset, peer->delay, peer->disp,
3037                     peer->jitter));
3038         if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC)
3039                 clock_select();
3040 }
3041
3042
3043 /*
3044  * clock_select - find the pick-of-the-litter clock
3045  *
3046  * LOCKCLOCK: (1) If the local clock is the prefer peer, it will always
3047  * be enabled, even if declared falseticker, (2) only the prefer peer
3048  * can be selected as the system peer, (3) if the external source is
3049  * down, the system leap bits are set to 11 and the stratum set to
3050  * infinity.
3051  */
3052 void
3053 clock_select(void)
3054 {
3055         struct peer *peer;
3056         int     i, j, k, n;
3057         int     nlist, nl2;
3058         int     allow;
3059         int     speer;
3060         double  d, e, f, g;
3061         double  high, low;
3062         double  speermet;
3063         double  orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
3064         struct endpoint endp;
3065         struct peer *osys_peer;
3066         struct peer *sys_prefer = NULL; /* prefer peer */
3067         struct peer *typesystem = NULL;
3068         struct peer *typeorphan = NULL;
3069 #ifdef REFCLOCK
3070         struct peer *typeacts = NULL;
3071         struct peer *typelocal = NULL;
3072         struct peer *typepps = NULL;
3073 #endif /* REFCLOCK */
3074         static struct endpoint *endpoint = NULL;
3075         static int *indx = NULL;
3076         static peer_select *peers = NULL;
3077         static u_int endpoint_size = 0;
3078         static u_int peers_size = 0;
3079         static u_int indx_size = 0;
3080         size_t octets;
3081
3082         /*
3083          * Initialize and create endpoint, index and peer lists big
3084          * enough to handle all associations.
3085          */
3086         osys_peer = sys_peer;
3087         sys_survivors = 0;
3088 #ifdef LOCKCLOCK
3089         set_sys_leap(LEAP_NOTINSYNC);
3090         sys_stratum = STRATUM_UNSPEC;
3091         memcpy(&sys_refid, "DOWN", 4);
3092 #endif /* LOCKCLOCK */
3093
3094         /*
3095          * Allocate dynamic space depending on the number of
3096          * associations.
3097          */
3098         nlist = 1;
3099         for (peer = peer_list; peer != NULL; peer = peer->p_link)
3100                 nlist++;
3101         endpoint_size = ALIGNED_SIZE(nlist * 2 * sizeof(*endpoint));
3102         peers_size = ALIGNED_SIZE(nlist * sizeof(*peers));
3103         indx_size = ALIGNED_SIZE(nlist * 2 * sizeof(*indx));
3104         octets = endpoint_size + peers_size + indx_size;
3105         endpoint = erealloc(endpoint, octets);
3106         peers = INC_ALIGNED_PTR(endpoint, endpoint_size);
3107         indx = INC_ALIGNED_PTR(peers, peers_size);
3108
3109         /*
3110          * Initially, we populate the island with all the rifraff peers
3111          * that happen to be lying around. Those with seriously
3112          * defective clocks are immediately booted off the island. Then,
3113          * the falsetickers are culled and put to sea. The truechimers
3114          * remaining are subject to repeated rounds where the most
3115          * unpopular at each round is kicked off. When the population
3116          * has dwindled to sys_minclock, the survivors split a million
3117          * bucks and collectively crank the chimes.
3118          */
3119         nlist = nl2 = 0;        /* none yet */
3120         for (peer = peer_list; peer != NULL; peer = peer->p_link) {
3121                 peer->new_status = CTL_PST_SEL_REJECT;
3122
3123                 /*
3124                  * Leave the island immediately if the peer is
3125                  * unfit to synchronize.
3126                  */
3127                 if (peer_unfit(peer)) {
3128                         continue;
3129                 }
3130
3131                 /*
3132                  * If this peer is an orphan parent, elect the
3133                  * one with the lowest metric defined as the
3134                  * IPv4 address or the first 64 bits of the
3135                  * hashed IPv6 address.  To ensure convergence
3136                  * on the same selected orphan, consider as
3137                  * well that this system may have the lowest
3138                  * metric and be the orphan parent.  If this
3139                  * system wins, sys_peer will be NULL to trigger
3140                  * orphan mode in timer().
3141                  */
3142                 if (peer->stratum == sys_orphan) {
3143                         u_int32 localmet;
3144                         u_int32 peermet;
3145
3146                         if (peer->dstadr != NULL)
3147                                 localmet = ntohl(peer->dstadr->addr_refid);
3148                         else
3149                                 localmet = U_INT32_MAX;
3150                         peermet = ntohl(addr2refid(&peer->srcadr));
3151                         if (peermet < localmet && peermet < orphmet) {
3152                                 typeorphan = peer;
3153                                 orphmet = peermet;
3154                         }
3155                         continue;
3156                 }
3157
3158                 /*
3159                  * If this peer could have the orphan parent
3160                  * as a synchronization ancestor, exclude it
3161                  * from selection to avoid forming a
3162                  * synchronization loop within the orphan mesh,
3163                  * triggering stratum climb to infinity
3164                  * instability.  Peers at stratum higher than
3165                  * the orphan stratum could have the orphan
3166                  * parent in ancestry so are excluded.
3167                  * See http://bugs.ntp.org/2050
3168                  */
3169                 if (peer->stratum > sys_orphan) {
3170                         continue;
3171                 }
3172 #ifdef REFCLOCK
3173                 /*
3174                  * The following are special cases. We deal
3175                  * with them later.
3176                  */
3177                 if (!(peer->flags & FLAG_PREFER)) {
3178                         switch (peer->refclktype) {
3179                         case REFCLK_LOCALCLOCK:
3180                                 if (   current_time > orphwait
3181                                     && typelocal == NULL)
3182                                         typelocal = peer;
3183                                 continue;
3184
3185                         case REFCLK_ACTS:
3186                                 if (   current_time > orphwait
3187                                     && typeacts == NULL)
3188                                         typeacts = peer;
3189                                 continue;
3190                         }
3191                 }
3192 #endif /* REFCLOCK */
3193
3194                 /*
3195                  * If we get this far, the peer can stay on the
3196                  * island, but does not yet have the immunity
3197                  * idol.
3198                  */
3199                 peer->new_status = CTL_PST_SEL_SANE;
3200                 f = root_distance(peer);
3201                 peers[nlist].peer = peer;
3202                 peers[nlist].error = peer->jitter;
3203                 peers[nlist].synch = f;
3204                 nlist++;
3205
3206                 /*
3207                  * Insert each interval endpoint on the unsorted
3208                  * endpoint[] list.
3209                  */
3210                 e = peer->offset;
3211                 endpoint[nl2].type = -1;        /* lower end */
3212                 endpoint[nl2].val = e - f;
3213                 nl2++;
3214                 endpoint[nl2].type = 1;         /* upper end */
3215                 endpoint[nl2].val = e + f;
3216                 nl2++;
3217         }
3218         /*
3219          * Construct sorted indx[] of endpoint[] indexes ordered by
3220          * offset.
3221          */
3222         for (i = 0; i < nl2; i++)
3223                 indx[i] = i;
3224         for (i = 0; i < nl2; i++) {
3225                 endp = endpoint[indx[i]];
3226                 e = endp.val;
3227                 k = i;
3228                 for (j = i + 1; j < nl2; j++) {
3229                         endp = endpoint[indx[j]];
3230                         if (endp.val < e) {
3231                                 e = endp.val;
3232                                 k = j;
3233                         }
3234                 }
3235                 if (k != i) {
3236                         j = indx[k];
3237                         indx[k] = indx[i];
3238                         indx[i] = j;
3239                 }
3240         }
3241         for (i = 0; i < nl2; i++)
3242                 DPRINTF(3, ("select: endpoint %2d %.6f\n",
3243                         endpoint[indx[i]].type, endpoint[indx[i]].val));
3244
3245         /*
3246          * This is the actual algorithm that cleaves the truechimers
3247          * from the falsetickers. The original algorithm was described
3248          * in Keith Marzullo's dissertation, but has been modified for
3249          * better accuracy.
3250          *
3251          * Briefly put, we first assume there are no falsetickers, then
3252          * scan the candidate list first from the low end upwards and
3253          * then from the high end downwards. The scans stop when the
3254          * number of intersections equals the number of candidates less
3255          * the number of falsetickers. If this doesn't happen for a
3256          * given number of falsetickers, we bump the number of
3257          * falsetickers and try again. If the number of falsetickers
3258          * becomes equal to or greater than half the number of
3259          * candidates, the Albanians have won the Byzantine wars and
3260          * correct synchronization is not possible.
3261          *
3262          * Here, nlist is the number of candidates and allow is the
3263          * number of falsetickers. Upon exit, the truechimers are the
3264          * survivors with offsets not less than low and not greater than
3265          * high. There may be none of them.
3266          */
3267         low = 1e9;
3268         high = -1e9;
3269         for (allow = 0; 2 * allow < nlist; allow++) {
3270
3271                 /*
3272                  * Bound the interval (low, high) as the smallest
3273                  * interval containing points from the most sources.
3274                  */
3275                 n = 0;
3276                 for (i = 0; i < nl2; i++) {
3277                         low = endpoint[indx[i]].val;
3278                         n -= endpoint[indx[i]].type;
3279                         if (n >= nlist - allow)
3280                                 break;
3281                 }
3282                 n = 0;
3283                 for (j = nl2 - 1; j >= 0; j--) {
3284                         high = endpoint[indx[j]].val;
3285                         n += endpoint[indx[j]].type;
3286                         if (n >= nlist - allow)
3287                                 break;
3288                 }
3289
3290                 /*
3291                  * If an interval containing truechimers is found, stop.
3292                  * If not, increase the number of falsetickers and go
3293                  * around again.
3294                  */
3295                 if (high > low)
3296                         break;
3297         }
3298
3299         /*
3300          * Clustering algorithm. Whittle candidate list of falsetickers,
3301          * who leave the island immediately. The TRUE peer is always a
3302          * truechimer. We must leave at least one peer to collect the
3303          * million bucks.
3304          *
3305          * We assert the correct time is contained in the interval, but
3306          * the best offset estimate for the interval might not be
3307          * contained in the interval. For this purpose, a truechimer is
3308          * defined as the midpoint of an interval that overlaps the
3309          * intersection interval.
3310          */
3311         j = 0;
3312         for (i = 0; i < nlist; i++) {
3313                 double  h;
3314
3315                 peer = peers[i].peer;
3316                 h = peers[i].synch;
3317                 if ((   high <= low
3318                      || peer->offset + h < low
3319                      || peer->offset - h > high
3320                     ) && !(peer->flags & FLAG_TRUE))
3321                         continue;
3322
3323 #ifdef REFCLOCK
3324                 /*
3325                  * Eligible PPS peers must survive the intersection
3326                  * algorithm. Use the first one found, but don't
3327                  * include any of them in the cluster population.
3328                  */
3329                 if (peer->flags & FLAG_PPS) {
3330                         if (typepps == NULL)
3331                                 typepps = peer;
3332                         if (!(peer->flags & FLAG_TSTAMP_PPS))
3333                                 continue;
3334                 }
3335 #endif /* REFCLOCK */
3336
3337                 if (j != i)
3338                         peers[j] = peers[i];
3339                 j++;
3340         }
3341         nlist = j;
3342
3343         /*
3344          * If no survivors remain at this point, check if the modem
3345          * driver, local driver or orphan parent in that order. If so,
3346          * nominate the first one found as the only survivor.
3347          * Otherwise, give up and leave the island to the rats.
3348          */
3349         if (nlist == 0) {
3350                 peers[0].error = 0;
3351                 peers[0].synch = sys_mindisp;
3352 #ifdef REFCLOCK
3353                 if (typeacts != NULL) {
3354                         peers[0].peer = typeacts;
3355                         nlist = 1;
3356                 } else if (typelocal != NULL) {
3357                         peers[0].peer = typelocal;
3358                         nlist = 1;
3359                 } else
3360 #endif /* REFCLOCK */
3361                 if (typeorphan != NULL) {
3362                         peers[0].peer = typeorphan;
3363                         nlist = 1;
3364                 }
3365         }
3366
3367         /*
3368          * Mark the candidates at this point as truechimers.
3369          */
3370         for (i = 0; i < nlist; i++) {
3371                 peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
3372                 DPRINTF(2, ("select: survivor %s %f\n",
3373                         stoa(&peers[i].peer->srcadr), peers[i].synch));
3374         }
3375
3376         /*
3377          * Now, vote outliers off the island by select jitter weighted
3378          * by root distance. Continue voting as long as there are more
3379          * than sys_minclock survivors and the select jitter of the peer
3380          * with the worst metric is greater than the minimum peer
3381          * jitter. Stop if we are about to discard a TRUE or PREFER
3382          * peer, who of course have the immunity idol.
3383          */
3384         while (1) {
3385                 d = 1e9;
3386                 e = -1e9;
3387                 g = 0;
3388                 k = 0;
3389                 for (i = 0; i < nlist; i++) {
3390                         if (peers[i].error < d)
3391                                 d = peers[i].error;
3392                         peers[i].seljit = 0;
3393                         if (nlist > 1) {
3394                                 f = 0;
3395                                 for (j = 0; j < nlist; j++)
3396                                         f += DIFF(peers[j].peer->offset,
3397                                             peers[i].peer->offset);
3398                                 peers[i].seljit = SQRT(f / (nlist - 1));
3399                         }
3400                         if (peers[i].seljit * peers[i].synch > e) {
3401                                 g = peers[i].seljit;
3402                                 e = peers[i].seljit * peers[i].synch;
3403                                 k = i;
3404                         }
3405                 }
3406                 g = max(g, LOGTOD(sys_precision));
3407                 if (   nlist <= max(1, sys_minclock)
3408                     || g <= d
3409                     || ((FLAG_TRUE | FLAG_PREFER) & peers[k].peer->flags))
3410                         break;
3411
3412                 DPRINTF(3, ("select: drop %s seljit %.6f jit %.6f\n",
3413                         ntoa(&peers[k].peer->srcadr), g, d));
3414                 if (nlist > sys_maxclock)
3415                         peers[k].peer->new_status = CTL_PST_SEL_EXCESS;
3416                 for (j = k + 1; j < nlist; j++)
3417                         peers[j - 1] = peers[j];
3418                 nlist--;
3419         }
3420
3421         /*
3422          * What remains is a list usually not greater than sys_minclock
3423          * peers. Note that unsynchronized peers cannot survive this
3424          * far.  Count and mark these survivors.
3425          *
3426          * While at it, count the number of leap warning bits found.
3427          * This will be used later to vote the system leap warning bit.
3428          * If a leap warning bit is found on a reference clock, the vote
3429          * is always won.
3430          *
3431          * Choose the system peer using a hybrid metric composed of the
3432          * selection jitter scaled by the root distance augmented by
3433          * stratum scaled by sys_mindisp (.001 by default). The goal of
3434          * the small stratum factor is to avoid clockhop between a
3435          * reference clock and a network peer which has a refclock and
3436          * is using an older ntpd, which does not floor sys_rootdisp at
3437          * sys_mindisp.
3438          *
3439          * In contrast, ntpd 4.2.6 and earlier used stratum primarily
3440          * in selecting the system peer, using a weight of 1 second of
3441          * additional root distance per stratum.  This heavy bias is no
3442          * longer appropriate, as the scaled root distance provides a
3443          * more rational metric carrying the cumulative error budget.
3444          */
3445         e = 1e9;
3446         speer = 0;
3447         leap_vote_ins = 0;
3448         leap_vote_del = 0;
3449         for (i = 0; i < nlist; i++) {
3450                 peer = peers[i].peer;
3451                 peer->unreach = 0;
3452                 peer->new_status = CTL_PST_SEL_SYNCCAND;
3453                 sys_survivors++;
3454                 if (peer->leap == LEAP_ADDSECOND) {
3455                         if (peer->flags & FLAG_REFCLOCK)
3456                                 leap_vote_ins = nlist;
3457                         else if (leap_vote_ins < nlist)
3458                                 leap_vote_ins++;
3459                 }
3460                 if (peer->leap == LEAP_DELSECOND) {
3461                         if (peer->flags & FLAG_REFCLOCK)
3462                                 leap_vote_del = nlist;
3463                         else if (leap_vote_del < nlist)
3464                                 leap_vote_del++;
3465                 }
3466                 if (peer->flags & FLAG_PREFER)
3467                         sys_prefer = peer;
3468                 speermet = peers[i].seljit * peers[i].synch +
3469                     peer->stratum * sys_mindisp;
3470                 if (speermet < e) {
3471                         e = speermet;
3472                         speer = i;
3473                 }
3474         }
3475
3476         /*
3477          * Unless there are at least sys_misane survivors, leave the
3478          * building dark. Otherwise, do a clockhop dance. Ordinarily,
3479          * use the selected survivor speer. However, if the current
3480          * system peer is not speer, stay with the current system peer
3481          * as long as it doesn't get too old or too ugly.
3482          */
3483         if (nlist > 0 && nlist >= sys_minsane) {
3484                 double  x;
3485
3486                 typesystem = peers[speer].peer;
3487                 if (osys_peer == NULL || osys_peer == typesystem) {
3488                         sys_clockhop = 0;
3489                 } else if ((x = fabs(typesystem->offset -
3490                     osys_peer->offset)) < sys_mindisp) {
3491                         if (sys_clockhop == 0)
3492                                 sys_clockhop = sys_mindisp;
3493                         else
3494                                 sys_clockhop *= .5;
3495                         DPRINTF(1, ("select: clockhop %d %.6f %.6f\n",
3496                                 j, x, sys_clockhop));
3497                         if (fabs(x) < sys_clockhop)
3498                                 typesystem = osys_peer;
3499                         else
3500                                 sys_clockhop = 0;
3501                 } else {
3502                         sys_clockhop = 0;
3503                 }
3504         }
3505
3506         /*
3507          * Mitigation rules of the game. We have the pick of the
3508          * litter in typesystem if any survivors are left. If
3509          * there is a prefer peer, use its offset and jitter.
3510          * Otherwise, use the combined offset and jitter of all kitters.
3511          */
3512         if (typesystem != NULL) {
3513                 if (sys_prefer == NULL) {
3514                         typesystem->new_status = CTL_PST_SEL_SYSPEER;
3515                         clock_combine(peers, sys_survivors, speer);
3516                 } else {
3517                         typesystem = sys_prefer;
3518                         sys_clockhop = 0;
3519                         typesystem->new_status = CTL_PST_SEL_SYSPEER;
3520                         sys_offset = typesystem->offset;
3521                         sys_jitter = typesystem->jitter;
3522                 }
3523                 DPRINTF(1, ("select: combine offset %.9f jitter %.9f\n",
3524                         sys_offset, sys_jitter));
3525         }
3526 #ifdef REFCLOCK
3527         /*
3528          * If a PPS driver is lit and the combined offset is less than
3529          * 0.4 s, select the driver as the PPS peer and use its offset
3530          * and jitter. However, if this is the atom driver, use it only
3531          * if there is a prefer peer or there are no survivors and none
3532          * are required.
3533          */
3534         if (   typepps != NULL
3535             && fabs(sys_offset) < 0.4
3536             && (   typepps->refclktype != REFCLK_ATOM_PPS
3537                 || (   typepps->refclktype == REFCLK_ATOM_PPS
3538                     && (   sys_prefer != NULL
3539                         || (typesystem == NULL && sys_minsane == 0))))) {
3540                 typesystem = typepps;
3541                 sys_clockhop = 0;
3542                 typesystem->new_status = CTL_PST_SEL_PPS;
3543                 sys_offset = typesystem->offset;
3544                 sys_jitter = typesystem->jitter;
3545                 DPRINTF(1, ("select: pps offset %.9f jitter %.9f\n",
3546                         sys_offset, sys_jitter));
3547         }
3548 #endif /* REFCLOCK */
3549
3550         /*
3551          * If there are no survivors at this point, there is no
3552          * system peer. If so and this is an old update, keep the
3553          * current statistics, but do not update the clock.
3554          */
3555         if (typesystem == NULL) {
3556                 if (osys_peer != NULL) {
3557                         if (sys_orphwait > 0)
3558                                 orphwait = current_time + sys_orphwait;
3559                         report_event(EVNT_NOPEER, NULL, NULL);
3560                 }
3561                 sys_peer = NULL;
3562                 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3563                         peer->status = peer->new_status;
3564                 return;
3565         }
3566
3567         /*
3568          * Do not use old data, as this may mess up the clock discipline
3569          * stability.
3570          */
3571         if (typesystem->epoch <= sys_epoch)
3572                 return;
3573
3574         /*
3575          * We have found the alpha male. Wind the clock.
3576          */
3577         if (osys_peer != typesystem)
3578                 report_event(PEVNT_NEWPEER, typesystem, NULL);
3579         for (peer = peer_list; peer != NULL; peer = peer->p_link)
3580                 peer->status = peer->new_status;
3581         clock_update(typesystem);
3582 }
3583
3584
3585 static void
3586 clock_combine(
3587         peer_select *   peers,  /* survivor list */
3588         int             npeers, /* number of survivors */
3589         int             syspeer /* index of sys.peer */
3590         )
3591 {
3592         int     i;
3593         double  x, y, z, w;
3594
3595         y = z = w = 0;
3596         for (i = 0; i < npeers; i++) {
3597                 x = 1. / peers[i].synch;
3598                 y += x;
3599                 z += x * peers[i].peer->offset;
3600                 w += x * DIFF(peers[i].peer->offset,
3601                     peers[syspeer].peer->offset);
3602         }
3603         sys_offset = z / y;
3604         sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
3605 }
3606
3607
3608 /*
3609  * root_distance - compute synchronization distance from peer to root
3610  */
3611 static double
3612 root_distance(
3613         struct peer *peer       /* peer structure pointer */
3614         )
3615 {
3616         double  dtemp;
3617
3618         /*
3619          * Root Distance (LAMBDA) is defined as:
3620          * (delta + DELTA)/2 + epsilon + EPSILON + D
3621          *
3622          * where:
3623          *  delta   is the round-trip delay
3624          *  DELTA   is the root delay
3625          *  epsilon is the peer dispersion
3626          *          + (15 usec each second)
3627          *  EPSILON is the root dispersion
3628          *  D       is sys_jitter
3629          *
3630          * NB: Think hard about why we are using these values, and what
3631          * the alternatives are, and the various pros/cons.
3632          *
3633          * DLM thinks these are probably the best choices from any of the
3634          * other worse choices.
3635          */
3636         dtemp = (peer->delay + peer->rootdelay) / 2
3637                 + peer->disp
3638                   + clock_phi * (current_time - peer->update)
3639                 + peer->rootdisp
3640                 + peer->jitter;
3641         /*
3642          * Careful squeak here. The value returned must be greater than
3643          * the minimum root dispersion in order to avoid clockhop with
3644          * highly precise reference clocks. Note that the root distance
3645          * cannot exceed the sys_maxdist, as this is the cutoff by the
3646          * selection algorithm.
3647          */
3648         if (dtemp < sys_mindisp)
3649                 dtemp = sys_mindisp;
3650         return (dtemp);
3651 }
3652
3653
3654 /*
3655  * peer_xmit - send packet for persistent association.
3656  */
3657 static void
3658 peer_xmit(
3659         struct peer *peer       /* peer structure pointer */
3660         )
3661 {
3662         struct pkt xpkt;        /* transmit packet */
3663         size_t  sendlen, authlen;
3664         keyid_t xkeyid = 0;     /* transmit key ID */
3665         l_fp    xmt_tx, xmt_ty;
3666
3667         if (!peer->dstadr)      /* drop peers without interface */
3668                 return;
3669
3670         xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, peer->version,
3671             peer->hmode);
3672         xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
3673         xpkt.ppoll = peer->hpoll;
3674         xpkt.precision = sys_precision;
3675         xpkt.refid = sys_refid;
3676         xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
3677         xpkt.rootdisp =  HTONS_FP(DTOUFP(sys_rootdisp));
3678         HTONL_FP(&sys_reftime, &xpkt.reftime);
3679         HTONL_FP(&peer->rec, &xpkt.org);
3680         HTONL_FP(&peer->dst, &xpkt.rec);
3681
3682         /*
3683          * If the received packet contains a MAC, the transmitted packet
3684          * is authenticated and contains a MAC. If not, the transmitted
3685          * packet is not authenticated.
3686          *
3687          * It is most important when autokey is in use that the local
3688          * interface IP address be known before the first packet is
3689          * sent. Otherwise, it is not possible to compute a correct MAC
3690          * the recipient will accept. Thus, the I/O semantics have to do
3691          * a little more work. In particular, the wildcard interface
3692          * might not be usable.
3693          */
3694         sendlen = LEN_PKT_NOMAC;
3695         if (
3696 #ifdef AUTOKEY
3697             !(peer->flags & FLAG_SKEY) &&
3698 #endif  /* !AUTOKEY */
3699             peer->keyid == 0) {
3700
3701                 /*
3702                  * Transmit a-priori timestamps
3703                  */
3704                 get_systime(&xmt_tx);
3705                 if (peer->flip == 0) {  /* basic mode */
3706                         peer->aorg = xmt_tx;
3707                         HTONL_FP(&xmt_tx, &xpkt.xmt);
3708                 } else {                /* interleaved modes */
3709                         if (peer->hmode == MODE_BROADCAST) { /* bcst */
3710                                 HTONL_FP(&xmt_tx, &xpkt.xmt);
3711                                 if (peer->flip > 0)
3712                                         HTONL_FP(&peer->borg,
3713                                             &xpkt.org);
3714                                 else
3715                                         HTONL_FP(&peer->aorg,
3716                                             &xpkt.org);
3717                         } else {        /* symmetric */
3718                                 if (peer->flip > 0)
3719                                         HTONL_FP(&peer->borg,
3720                                             &xpkt.xmt);
3721                                 else
3722                                         HTONL_FP(&peer->aorg,
3723                                             &xpkt.xmt);
3724                         }
3725                 }
3726                 peer->t21_bytes = sendlen;
3727                 sendpkt(&peer->srcadr, peer->dstadr,
3728                         sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
3729                         &xpkt, sendlen);
3730                 peer->sent++;
3731                 peer->throttle += (1 << peer->minpoll) - 2;
3732
3733                 /*
3734                  * Capture a-posteriori timestamps
3735                  */
3736                 get_systime(&xmt_ty);
3737                 if (peer->flip != 0) {          /* interleaved modes */
3738                         if (peer->flip > 0)
3739                                 peer->aorg = xmt_ty;
3740                         else
3741                                 peer->borg = xmt_ty;
3742                         peer->flip = -peer->flip;
3743                 }
3744                 L_SUB(&xmt_ty, &xmt_tx);
3745                 LFPTOD(&xmt_ty, peer->xleave);
3746                 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d len %zu xmt %#010x.%08x\n",
3747                             current_time,
3748                             peer->dstadr ? stoa(&peer->dstadr->sin) : "-",
3749                             stoa(&peer->srcadr), peer->hmode, sendlen,
3750                             xmt_tx.l_ui, xmt_tx.l_uf));
3751                 return;
3752         }
3753
3754         /*
3755          * Authentication is enabled, so the transmitted packet must be
3756          * authenticated. If autokey is enabled, fuss with the various
3757          * modes; otherwise, symmetric key cryptography is used.
3758          */
3759 #ifdef AUTOKEY
3760         if (peer->flags & FLAG_SKEY) {
3761                 struct exten *exten;    /* extension field */
3762
3763                 /*
3764                  * The Public Key Dance (PKD): Cryptographic credentials
3765                  * are contained in extension fields, each including a
3766                  * 4-octet length/code word followed by a 4-octet
3767                  * association ID and optional additional data. Optional
3768                  * data includes a 4-octet data length field followed by
3769                  * the data itself. Request messages are sent from a
3770                  * configured association; response messages can be sent
3771                  * from a configured association or can take the fast
3772                  * path without ever matching an association. Response
3773                  * messages have the same code as the request, but have
3774                  * a response bit and possibly an error bit set. In this
3775                  * implementation, a message may contain no more than
3776                  * one command and one or more responses.
3777                  *
3778                  * Cryptographic session keys include both a public and
3779                  * a private componet. Request and response messages
3780                  * using extension fields are always sent with the
3781                  * private component set to zero. Packets without
3782                  * extension fields indlude the private component when
3783                  * the session key is generated.
3784                  */
3785                 while (1) {
3786
3787                         /*
3788                          * Allocate and initialize a keylist if not
3789                          * already done. Then, use the list in inverse
3790                          * order, discarding keys once used. Keep the
3791                          * latest key around until the next one, so
3792                          * clients can use client/server packets to
3793                          * compute propagation delay.
3794                          *
3795                          * Note that once a key is used from the list,
3796                          * it is retained in the key cache until the
3797                          * next key is used. This is to allow a client
3798                          * to retrieve the encrypted session key
3799                          * identifier to verify authenticity.
3800                          *
3801                          * If for some reason a key is no longer in the
3802                          * key cache, a birthday has happened or the key
3803                          * has expired, so the pseudo-random sequence is
3804                          * broken. In that case, purge the keylist and
3805                          * regenerate it.
3806                          */
3807                         if (peer->keynumber == 0)
3808                                 make_keylist(peer, peer->dstadr);
3809                         else
3810                                 peer->keynumber--;
3811                         xkeyid = peer->keylist[peer->keynumber];
3812                         if (authistrusted(xkeyid))
3813                                 break;
3814                         else
3815                                 key_expire(peer);
3816                 }
3817                 peer->keyid = xkeyid;
3818                 exten = NULL;
3819                 switch (peer->hmode) {
3820
3821                 /*
3822                  * In broadcast server mode the autokey values are
3823                  * required by the broadcast clients. Push them when a
3824                  * new keylist is generated; otherwise, push the
3825                  * association message so the client can request them at
3826                  * other times.
3827                  */
3828                 case MODE_BROADCAST:
3829                         if (peer->flags & FLAG_ASSOC)
3830                                 exten = crypto_args(peer, CRYPTO_AUTO |
3831                                     CRYPTO_RESP, peer->associd, NULL);
3832                         else
3833                                 exten = crypto_args(peer, CRYPTO_ASSOC |
3834                                     CRYPTO_RESP, peer->associd, NULL);
3835                         break;
3836
3837                 /*
3838                  * In symmetric modes the parameter, certificate,
3839                  * identity, cookie and autokey exchanges are
3840                  * required. The leapsecond exchange is optional. But, a
3841                  * peer will not believe the other peer until the other
3842                  * peer has synchronized, so the certificate exchange
3843                  * might loop until then. If a peer finds a broken
3844                  * autokey sequence, it uses the autokey exchange to
3845                  * retrieve the autokey values. In any case, if a new
3846                  * keylist is generated, the autokey values are pushed.
3847                  */
3848                 case MODE_ACTIVE:
3849                 case MODE_PASSIVE:
3850
3851                         /*
3852                          * Parameter, certificate and identity.
3853                          */
3854                         if (!peer->crypto)
3855                                 exten = crypto_args(peer, CRYPTO_ASSOC,
3856                                     peer->associd, hostval.ptr);
3857                         else if (!(peer->crypto & CRYPTO_FLAG_CERT))
3858                                 exten = crypto_args(peer, CRYPTO_CERT,
3859                                     peer->associd, peer->issuer);
3860                         else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
3861                                 exten = crypto_args(peer,
3862                                     crypto_ident(peer), peer->associd,
3863                                     NULL);
3864
3865                         /*
3866                          * Cookie and autokey. We request the cookie
3867                          * only when the this peer and the other peer
3868                          * are synchronized. But, this peer needs the
3869                          * autokey values when the cookie is zero. Any
3870                          * time we regenerate the key list, we offer the
3871                          * autokey values without being asked. If for
3872                          * some reason either peer finds a broken
3873                          * autokey sequence, the autokey exchange is
3874                          * used to retrieve the autokey values.
3875                          */
3876                         else if (   sys_leap != LEAP_NOTINSYNC
3877                                  && peer->leap != LEAP_NOTINSYNC
3878                                  && !(peer->crypto & CRYPTO_FLAG_COOK))
3879                                 exten = crypto_args(peer, CRYPTO_COOK,
3880                                     peer->associd, NULL);
3881                         else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
3882                                 exten = crypto_args(peer, CRYPTO_AUTO,
3883                                     peer->associd, NULL);
3884                         else if (   peer->flags & FLAG_ASSOC
3885                                  && peer->crypto & CRYPTO_FLAG_SIGN)
3886                                 exten = crypto_args(peer, CRYPTO_AUTO |
3887                                     CRYPTO_RESP, peer->assoc, NULL);
3888
3889                         /*
3890                          * Wait for clock sync, then sign the
3891                          * certificate and retrieve the leapsecond
3892                          * values.
3893                          */
3894                         else if (sys_leap == LEAP_NOTINSYNC)
3895                                 break;
3896
3897                         else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
3898                                 exten = crypto_args(peer, CRYPTO_SIGN,
3899                                     peer->associd, hostval.ptr);
3900                         else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
3901                                 exten = crypto_args(peer, CRYPTO_LEAP,
3902                                     peer->associd, NULL);
3903                         break;
3904
3905                 /*
3906                  * In client mode the parameter, certificate, identity,
3907                  * cookie and sign exchanges are required. The
3908                  * leapsecond exchange is optional. If broadcast client
3909                  * mode the same exchanges are required, except that the
3910                  * autokey exchange is substitutes for the cookie
3911                  * exchange, since the cookie is always zero. If the
3912                  * broadcast client finds a broken autokey sequence, it
3913                  * uses the autokey exchange to retrieve the autokey
3914                  * values.
3915                  */
3916                 case MODE_CLIENT:
3917
3918                         /*
3919                          * Parameter, certificate and identity.
3920                          */
3921                         if (!peer->crypto)
3922                                 exten = crypto_args(peer, CRYPTO_ASSOC,
3923                                     peer->associd, hostval.ptr);
3924                         else if (!(peer->crypto & CRYPTO_FLAG_CERT))
3925                                 exten = crypto_args(peer, CRYPTO_CERT,
3926                                     peer->associd, peer->issuer);
3927                         else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
3928                                 exten = crypto_args(peer,
3929                                     crypto_ident(peer), peer->associd,
3930                                     NULL);
3931
3932                         /*
3933                          * Cookie and autokey. These are requests, but
3934                          * we use the peer association ID with autokey
3935                          * rather than our own.
3936                          */
3937                         else if (!(peer->crypto & CRYPTO_FLAG_COOK))
3938                                 exten = crypto_args(peer, CRYPTO_COOK,
3939                                     peer->associd, NULL);
3940                         else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
3941                                 exten = crypto_args(peer, CRYPTO_AUTO,
3942                                     peer->assoc, NULL);
3943
3944                         /*
3945                          * Wait for clock sync, then sign the
3946                          * certificate and retrieve the leapsecond
3947                          * values.
3948                          */
3949                         else if (sys_leap == LEAP_NOTINSYNC)
3950                                 break;
3951
3952                         else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
3953                                 exten = crypto_args(peer, CRYPTO_SIGN,
3954                                     peer->associd, hostval.ptr);
3955                         else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
3956                                 exten = crypto_args(peer, CRYPTO_LEAP,
3957                                     peer->associd, NULL);
3958                         break;
3959                 }
3960
3961                 /*
3962                  * Add a queued extension field if present. This is
3963                  * always a request message, so the reply ID is already
3964                  * in the message. If an error occurs, the error bit is
3965                  * lit in the response.
3966                  */
3967                 if (peer->cmmd != NULL) {
3968                         u_int32 temp32;
3969
3970                         temp32 = CRYPTO_RESP;
3971                         peer->cmmd->opcode |= htonl(temp32);
3972                         sendlen += crypto_xmit(peer, &xpkt, NULL,
3973                             sendlen, peer->cmmd, 0);
3974                         free(peer->cmmd);
3975                         peer->cmmd = NULL;
3976                 }
3977
3978                 /*
3979                  * Add an extension field created above. All but the
3980                  * autokey response message are request messages.
3981                  */
3982                 if (exten != NULL) {
3983                         if (exten->opcode != 0)
3984                                 sendlen += crypto_xmit(peer, &xpkt,
3985                                     NULL, sendlen, exten, 0);
3986                         free(exten);
3987                 }
3988
3989                 /*
3990                  * Calculate the next session key. Since extension
3991                  * fields are present, the cookie value is zero.
3992                  */
3993                 if (sendlen > (int)LEN_PKT_NOMAC) {
3994                         session_key(&peer->dstadr->sin, &peer->srcadr,
3995                             xkeyid, 0, 2);
3996                 }
3997         }
3998 #endif  /* AUTOKEY */
3999
4000         /*
4001          * Transmit a-priori timestamps
4002          */
4003         get_systime(&xmt_tx);
4004         if (peer->flip == 0) {          /* basic mode */
4005                 peer->aorg = xmt_tx;
4006                 HTONL_FP(&xmt_tx, &xpkt.xmt);
4007         } else {                        /* interleaved modes */
4008                 if (peer->hmode == MODE_BROADCAST) { /* bcst */
4009                         HTONL_FP(&xmt_tx, &xpkt.xmt);
4010                         if (peer->flip > 0)
4011                                 HTONL_FP(&peer->borg, &xpkt.org);
4012                         else
4013                                 HTONL_FP(&peer->aorg, &xpkt.org);
4014                 } else {                /* symmetric */
4015                         if (peer->flip > 0)
4016                                 HTONL_FP(&peer->borg, &xpkt.xmt);
4017                         else
4018                                 HTONL_FP(&peer->aorg, &xpkt.xmt);
4019                 }
4020         }
4021         xkeyid = peer->keyid;
4022         authlen = authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4023         if (authlen == 0) {
4024                 report_event(PEVNT_AUTH, peer, "no key");
4025                 peer->flash |= TEST5;           /* auth error */
4026                 peer->badauth++;
4027                 return;
4028         }
4029         sendlen += authlen;
4030 #ifdef AUTOKEY
4031         if (xkeyid > NTP_MAXKEY)
4032                 authtrust(xkeyid, 0);
4033 #endif  /* AUTOKEY */
4034         if (sendlen > sizeof(xpkt)) {
4035                 msyslog(LOG_ERR, "peer_xmit: buffer overflow %zu", sendlen);
4036                 exit (-1);
4037         }
4038         peer->t21_bytes = sendlen;
4039         sendpkt(&peer->srcadr, peer->dstadr,
4040                 sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4041                 &xpkt, sendlen);
4042         peer->sent++;
4043         peer->throttle += (1 << peer->minpoll) - 2;
4044
4045         /*
4046          * Capture a-posteriori timestamps
4047          */
4048         get_systime(&xmt_ty);
4049         if (peer->flip != 0) {                  /* interleaved modes */
4050                 if (peer->flip > 0)
4051                         peer->aorg = xmt_ty;
4052                 else
4053                         peer->borg = xmt_ty;
4054                 peer->flip = -peer->flip;
4055         }
4056         L_SUB(&xmt_ty, &xmt_tx);
4057         LFPTOD(&xmt_ty, peer->xleave);
4058 #ifdef AUTOKEY
4059         DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu index %d\n",
4060                     current_time, latoa(peer->dstadr),
4061                     ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen,
4062                     peer->keynumber));
4063 #else   /* !AUTOKEY follows */
4064         DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu\n",
4065                     current_time, peer->dstadr ?
4066                     ntoa(&peer->dstadr->sin) : "-",
4067                     ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
4068 #endif  /* !AUTOKEY */
4069
4070         return;
4071 }
4072
4073
4074 #ifdef LEAP_SMEAR
4075
4076 static void
4077 leap_smear_add_offs(
4078         l_fp *t,
4079         l_fp *t_recv
4080         )
4081 {
4082
4083         L_ADD(t, &leap_smear.offset);
4084
4085         /*
4086         ** XXX: Should the smear be added to the root dispersion?
4087         */
4088
4089         return;
4090 }
4091
4092 #endif  /* LEAP_SMEAR */
4093
4094
4095 /*
4096  * fast_xmit - Send packet for nonpersistent association. Note that
4097  * neither the source or destination can be a broadcast address.
4098  */
4099 static void
4100 fast_xmit(
4101         struct recvbuf *rbufp,  /* receive packet pointer */
4102         int     xmode,          /* receive mode */
4103         keyid_t xkeyid,         /* transmit key ID */
4104         int     flags           /* restrict mask */
4105         )
4106 {
4107         struct pkt xpkt;        /* transmit packet structure */
4108         struct pkt *rpkt;       /* receive packet structure */
4109         l_fp    xmt_tx, xmt_ty;
4110         size_t  sendlen;
4111 #ifdef AUTOKEY
4112         u_int32 temp32;
4113 #endif
4114
4115         /*
4116          * Initialize transmit packet header fields from the receive
4117          * buffer provided. We leave the fields intact as received, but
4118          * set the peer poll at the maximum of the receive peer poll and
4119          * the system minimum poll (ntp_minpoll). This is for KoD rate
4120          * control and not strictly specification compliant, but doesn't
4121          * break anything.
4122          *
4123          * If the gazinta was from a multicast address, the gazoutta
4124          * must go out another way.
4125          */
4126         rpkt = &rbufp->recv_pkt;
4127         if (rbufp->dstadr->flags & INT_MCASTOPEN)
4128                 rbufp->dstadr = findinterface(&rbufp->recv_srcadr);
4129
4130         /*
4131          * If this is a kiss-o'-death (KoD) packet, show leap
4132          * unsynchronized, stratum zero, reference ID the four-character
4133          * kiss code and system root delay. Note we don't reveal the
4134          * local time, so these packets can't be used for
4135          * synchronization.
4136          */
4137         if (flags & RES_KOD) {
4138                 sys_kodsent++;
4139                 xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
4140                     PKT_VERSION(rpkt->li_vn_mode), xmode);
4141                 xpkt.stratum = STRATUM_PKT_UNSPEC;
4142                 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4143                 xpkt.precision = rpkt->precision;
4144                 memcpy(&xpkt.refid, "RATE", 4);
4145                 xpkt.rootdelay = rpkt->rootdelay;
4146                 xpkt.rootdisp = rpkt->rootdisp;
4147                 xpkt.reftime = rpkt->reftime;
4148                 xpkt.org = rpkt->xmt;
4149                 xpkt.rec = rpkt->xmt;
4150                 xpkt.xmt = rpkt->xmt;
4151
4152         /*
4153          * This is a normal packet. Use the system variables.
4154          */
4155         } else {
4156 #ifdef LEAP_SMEAR
4157                 /*
4158                  * Make copies of the variables which can be affected by smearing.
4159                  */
4160                 l_fp this_ref_time;
4161                 l_fp this_recv_time;
4162 #endif
4163
4164                 /*
4165                  * If we are inside the leap smear interval we add the current smear offset to
4166                  * the packet receive time, to the packet transmit time, and eventually to the
4167                  * reftime to make sure the reftime isn't later than the transmit/receive times.
4168                  */
4169                 xpkt.li_vn_mode = PKT_LI_VN_MODE(xmt_leap,
4170                     PKT_VERSION(rpkt->li_vn_mode), xmode);
4171
4172                 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4173                 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4174                 xpkt.precision = sys_precision;
4175                 xpkt.refid = sys_refid;
4176                 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4177                 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4178
4179 #ifdef LEAP_SMEAR
4180                 this_ref_time = sys_reftime;
4181                 if (leap_smear.in_progress) {
4182                         leap_smear_add_offs(&this_ref_time, NULL);
4183                         xpkt.refid = convertLFPToRefID(leap_smear.offset);
4184                         DPRINTF(2, ("fast_xmit: leap_smear.in_progress: refid %8x, smear %s\n",
4185                                 ntohl(xpkt.refid),
4186                                 lfptoa(&leap_smear.offset, 8)
4187                                 ));
4188                 }
4189                 HTONL_FP(&this_ref_time, &xpkt.reftime);
4190 #else
4191                 HTONL_FP(&sys_reftime, &xpkt.reftime);
4192 #endif
4193
4194                 xpkt.org = rpkt->xmt;
4195
4196 #ifdef LEAP_SMEAR
4197                 this_recv_time = rbufp->recv_time;
4198                 if (leap_smear.in_progress)
4199                         leap_smear_add_offs(&this_recv_time, NULL);
4200                 HTONL_FP(&this_recv_time, &xpkt.rec);
4201 #else
4202                 HTONL_FP(&rbufp->recv_time, &xpkt.rec);
4203 #endif
4204
4205                 get_systime(&xmt_tx);
4206 #ifdef LEAP_SMEAR
4207                 if (leap_smear.in_progress)
4208                         leap_smear_add_offs(&xmt_tx, &this_recv_time);
4209 #endif
4210                 HTONL_FP(&xmt_tx, &xpkt.xmt);
4211         }
4212
4213 #ifdef HAVE_NTP_SIGND
4214         if (flags & RES_MSSNTP) {
4215                 send_via_ntp_signd(rbufp, xmode, xkeyid, flags, &xpkt);
4216                 return;
4217         }
4218 #endif /* HAVE_NTP_SIGND */
4219
4220         /*
4221          * If the received packet contains a MAC, the transmitted packet
4222          * is authenticated and contains a MAC. If not, the transmitted
4223          * packet is not authenticated.
4224          */
4225         sendlen = LEN_PKT_NOMAC;
4226         if (rbufp->recv_length == sendlen) {
4227                 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt,
4228                     sendlen);
4229                 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d len %lu\n",
4230                             current_time, stoa(&rbufp->dstadr->sin),
4231                             stoa(&rbufp->recv_srcadr), xmode,
4232                             (u_long)sendlen));
4233                 return;
4234         }
4235
4236         /*
4237          * The received packet contains a MAC, so the transmitted packet
4238          * must be authenticated. For symmetric key cryptography, use
4239          * the predefined and trusted symmetric keys to generate the
4240          * cryptosum. For autokey cryptography, use the server private
4241          * value to generate the cookie, which is unique for every
4242          * source-destination-key ID combination.
4243          */
4244 #ifdef AUTOKEY
4245         if (xkeyid > NTP_MAXKEY) {
4246                 keyid_t cookie;
4247
4248                 /*
4249                  * The only way to get here is a reply to a legitimate
4250                  * client request message, so the mode must be
4251                  * MODE_SERVER. If an extension field is present, there
4252                  * can be only one and that must be a command. Do what
4253                  * needs, but with private value of zero so the poor
4254                  * jerk can decode it. If no extension field is present,
4255                  * use the cookie to generate the session key.
4256                  */
4257                 cookie = session_key(&rbufp->recv_srcadr,
4258                     &rbufp->dstadr->sin, 0, sys_private, 0);
4259                 if ((size_t)rbufp->recv_length > sendlen + MAX_MAC_LEN) {
4260                         session_key(&rbufp->dstadr->sin,
4261                             &rbufp->recv_srcadr, xkeyid, 0, 2);
4262                         temp32 = CRYPTO_RESP;
4263                         rpkt->exten[0] |= htonl(temp32);
4264                         sendlen += crypto_xmit(NULL, &xpkt, rbufp,
4265                             sendlen, (struct exten *)rpkt->exten,
4266                             cookie);
4267                 } else {
4268                         session_key(&rbufp->dstadr->sin,
4269                             &rbufp->recv_srcadr, xkeyid, cookie, 2);
4270                 }
4271         }
4272 #endif  /* AUTOKEY */
4273         get_systime(&xmt_tx);
4274         sendlen += authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4275 #ifdef AUTOKEY
4276         if (xkeyid > NTP_MAXKEY)
4277                 authtrust(xkeyid, 0);
4278 #endif  /* AUTOKEY */
4279         sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt, sendlen);
4280         get_systime(&xmt_ty);
4281         L_SUB(&xmt_ty, &xmt_tx);
4282         sys_authdelay = xmt_ty;
4283         DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d keyid %08x len %lu\n",
4284                     current_time, ntoa(&rbufp->dstadr->sin),
4285                     ntoa(&rbufp->recv_srcadr), xmode, xkeyid,
4286                     (u_long)sendlen));
4287 }
4288
4289
4290 /*
4291  * pool_xmit - resolve hostname or send unicast solicitation for pool.
4292  */
4293 static void
4294 pool_xmit(
4295         struct peer *pool       /* pool solicitor association */
4296         )
4297 {
4298 #ifdef WORKER
4299         struct pkt              xpkt;   /* transmit packet structure */
4300         struct addrinfo         hints;
4301         int                     rc;
4302         struct interface *      lcladr;
4303         sockaddr_u *            rmtadr;
4304         int                     restrict_mask;
4305         struct peer *           p;
4306         l_fp                    xmt_tx;
4307
4308         if (NULL == pool->ai) {
4309                 if (pool->addrs != NULL) {
4310                         /* free() is used with copy_addrinfo_list() */
4311                         free(pool->addrs);
4312                         pool->addrs = NULL;
4313                 }
4314                 ZERO(hints);
4315                 hints.ai_family = AF(&pool->srcadr);
4316                 hints.ai_socktype = SOCK_DGRAM;
4317                 hints.ai_protocol = IPPROTO_UDP;
4318                 /* ignore getaddrinfo_sometime() errors, we will retry */
4319                 rc = getaddrinfo_sometime(
4320                         pool->hostname,
4321                         "ntp",
4322                         &hints,
4323                         0,                      /* no retry */
4324                         &pool_name_resolved,
4325                         (void *)(intptr_t)pool->associd);
4326                 if (!rc)
4327                         DPRINTF(1, ("pool DNS lookup %s started\n",
4328                                 pool->hostname));
4329                 else
4330                         msyslog(LOG_ERR,
4331                                 "unable to start pool DNS %s: %m",
4332                                 pool->hostname);
4333                 return;
4334         }
4335
4336         do {
4337                 /* copy_addrinfo_list ai_addr points to a sockaddr_u */
4338                 rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
4339                 pool->ai = pool->ai->ai_next;
4340                 p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT, 0);
4341         } while (p != NULL && pool->ai != NULL);
4342         if (p != NULL)
4343                 return; /* out of addresses, re-query DNS next poll */
4344         restrict_mask = restrictions(rmtadr);
4345         if (RES_FLAGS & restrict_mask)
4346                 restrict_source(rmtadr, 0,
4347                                 current_time + POOL_SOLICIT_WINDOW + 1);
4348         lcladr = findinterface(rmtadr);
4349         memset(&xpkt, 0, sizeof(xpkt));
4350         xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, pool->version,
4351                                          MODE_CLIENT);
4352         xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4353         xpkt.ppoll = pool->hpoll;
4354         xpkt.precision = sys_precision;
4355         xpkt.refid = sys_refid;
4356         xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4357         xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4358         HTONL_FP(&sys_reftime, &xpkt.reftime);
4359         get_systime(&xmt_tx);
4360         pool->aorg = xmt_tx;
4361         HTONL_FP(&xmt_tx, &xpkt.xmt);
4362         sendpkt(rmtadr, lcladr,
4363                 sys_ttl[(pool->ttl >= sys_ttlmax) ? sys_ttlmax : pool->ttl],
4364                 &xpkt, LEN_PKT_NOMAC);
4365         pool->sent++;
4366         pool->throttle += (1 << pool->minpoll) - 2;
4367         DPRINTF(1, ("pool_xmit: at %ld %s->%s pool\n",
4368                     current_time, latoa(lcladr), stoa(rmtadr)));
4369         msyslog(LOG_INFO, "Soliciting pool server %s", stoa(rmtadr));
4370 #endif  /* WORKER */
4371 }
4372
4373
4374 #ifdef AUTOKEY
4375         /*
4376          * group_test - test if this is the same group
4377          *
4378          * host         assoc           return          action
4379          * none         none            0               mobilize *
4380          * none         group           0               mobilize *
4381          * group        none            0               mobilize *
4382          * group        group           1               mobilize
4383          * group        different       1               ignore
4384          * * ignore if notrust
4385          */
4386 int
4387 group_test(
4388         char    *grp,
4389         char    *ident
4390         )
4391 {
4392         if (grp == NULL)
4393                 return (0);
4394
4395         if (strcmp(grp, sys_groupname) == 0)
4396                 return (0);
4397
4398         if (ident == NULL)
4399                 return (1);
4400
4401         if (strcmp(grp, ident) == 0)
4402                 return (0);
4403
4404         return (1);
4405 }
4406 #endif /* AUTOKEY */
4407
4408
4409 #ifdef WORKER
4410 void
4411 pool_name_resolved(
4412         int                     rescode,
4413         int                     gai_errno,
4414         void *                  context,
4415         const char *            name,
4416         const char *            service,
4417         const struct addrinfo * hints,
4418         const struct addrinfo * res
4419         )
4420 {
4421         struct peer *   pool;   /* pool solicitor association */
4422         associd_t       assoc;
4423
4424         if (rescode) {
4425                 msyslog(LOG_ERR,
4426                         "error resolving pool %s: %s (%d)",
4427                         name, gai_strerror(rescode), rescode);
4428                 return;
4429         }
4430
4431         assoc = (associd_t)(intptr_t)context;
4432         pool = findpeerbyassoc(assoc);
4433         if (NULL == pool) {
4434                 msyslog(LOG_ERR,
4435                         "Could not find assoc %u for pool DNS %s",
4436                         assoc, name);
4437                 return;
4438         }
4439         DPRINTF(1, ("pool DNS %s completed\n", name));
4440         pool->addrs = copy_addrinfo_list(res);
4441         pool->ai = pool->addrs;
4442         pool_xmit(pool);
4443
4444 }
4445 #endif  /* WORKER */
4446
4447
4448 #ifdef AUTOKEY
4449 /*
4450  * key_expire - purge the key list
4451  */
4452 void
4453 key_expire(
4454         struct peer *peer       /* peer structure pointer */
4455         )
4456 {
4457         int i;
4458
4459         if (peer->keylist != NULL) {
4460                 for (i = 0; i <= peer->keynumber; i++)
4461                         authtrust(peer->keylist[i], 0);
4462                 free(peer->keylist);
4463                 peer->keylist = NULL;
4464         }
4465         value_free(&peer->sndval);
4466         peer->keynumber = 0;
4467         peer->flags &= ~FLAG_ASSOC;
4468         DPRINTF(1, ("key_expire: at %lu associd %d\n", current_time,
4469                     peer->associd));
4470 }
4471 #endif  /* AUTOKEY */
4472
4473
4474 /*
4475  * local_refid(peer) - check peer refid to avoid selecting peers
4476  *                     currently synced to this ntpd.
4477  */
4478 static int
4479 local_refid(
4480         struct peer *   p
4481         )
4482 {
4483         endpt * unicast_ep;
4484
4485         if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
4486                 unicast_ep = p->dstadr;
4487         else
4488                 unicast_ep = findinterface(&p->srcadr);
4489
4490         if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
4491                 return TRUE;
4492         else
4493                 return FALSE;
4494 }
4495
4496
4497 /*
4498  * Determine if the peer is unfit for synchronization
4499  *
4500  * A peer is unfit for synchronization if
4501  * > TEST10 bad leap or stratum below floor or at or above ceiling
4502  * > TEST11 root distance exceeded for remote peer
4503  * > TEST12 a direct or indirect synchronization loop would form
4504  * > TEST13 unreachable or noselect
4505  */
4506 int                             /* FALSE if fit, TRUE if unfit */
4507 peer_unfit(
4508         struct peer *peer       /* peer structure pointer */
4509         )
4510 {
4511         int     rval = 0;
4512
4513         /*
4514          * A stratum error occurs if (1) the server has never been
4515          * synchronized, (2) the server stratum is below the floor or
4516          * greater than or equal to the ceiling.
4517          */
4518         if (   peer->leap == LEAP_NOTINSYNC
4519             || peer->stratum < sys_floor
4520             || peer->stratum >= sys_ceiling) {
4521                 rval |= TEST10;         /* bad synch or stratum */
4522         }
4523
4524         /*
4525          * A distance error for a remote peer occurs if the root
4526          * distance is greater than or equal to the distance threshold
4527          * plus the increment due to one host poll interval.
4528          */
4529         if (   !(peer->flags & FLAG_REFCLOCK)
4530             && root_distance(peer) >= sys_maxdist
4531                                       + clock_phi * ULOGTOD(peer->hpoll)) {
4532                 rval |= TEST11;         /* distance exceeded */
4533         }
4534
4535         /*
4536          * A loop error occurs if the remote peer is synchronized to the
4537          * local peer or if the remote peer is synchronized to the same
4538          * server as the local peer but only if the remote peer is
4539          * neither a reference clock nor an orphan.
4540          */
4541         if (peer->stratum > 1 && local_refid(peer)) {
4542                 rval |= TEST12;         /* synchronization loop */
4543         }
4544
4545         /*
4546          * An unreachable error occurs if the server is unreachable or
4547          * the noselect bit is set.
4548          */
4549         if (!peer->reach || (peer->flags & FLAG_NOSELECT)) {
4550                 rval |= TEST13;         /* unreachable */
4551         }
4552
4553         peer->flash &= ~PEER_TEST_MASK;
4554         peer->flash |= rval;
4555         return (rval);
4556 }
4557
4558
4559 /*
4560  * Find the precision of this particular machine
4561  */
4562 #define MINSTEP         20e-9   /* minimum clock increment (s) */
4563 #define MAXSTEP         1       /* maximum clock increment (s) */
4564 #define MINCHANGES      12      /* minimum number of step samples */
4565 #define MAXLOOPS        ((int)(1. / MINSTEP))   /* avoid infinite loop */
4566
4567 /*
4568  * This routine measures the system precision defined as the minimum of
4569  * a sequence of differences between successive readings of the system
4570  * clock. However, if a difference is less than MINSTEP, the clock has
4571  * been read more than once during a clock tick and the difference is
4572  * ignored. We set MINSTEP greater than zero in case something happens
4573  * like a cache miss, and to tolerate underlying system clocks which
4574  * ensure each reading is strictly greater than prior readings while
4575  * using an underlying stepping (not interpolated) clock.
4576  *
4577  * sys_tick and sys_precision represent the time to read the clock for
4578  * systems with high-precision clocks, and the tick interval or step
4579  * size for lower-precision stepping clocks.
4580  *
4581  * This routine also measures the time to read the clock on stepping
4582  * system clocks by counting the number of readings between changes of
4583  * the underlying clock.  With either type of clock, the minimum time
4584  * to read the clock is saved as sys_fuzz, and used to ensure the
4585  * get_systime() readings always increase and are fuzzed below sys_fuzz.
4586  */
4587 void
4588 measure_precision(void)
4589 {
4590         /*
4591          * With sys_fuzz set to zero, get_systime() fuzzing of low bits
4592          * is effectively disabled.  trunc_os_clock is FALSE to disable
4593          * get_ostime() simulation of a low-precision system clock.
4594          */
4595         set_sys_fuzz(0.);
4596         trunc_os_clock = FALSE;
4597         measured_tick = measure_tick_fuzz();
4598         set_sys_tick_precision(measured_tick);
4599         msyslog(LOG_INFO, "proto: precision = %.3f usec (%d)",
4600                 sys_tick * 1e6, sys_precision);
4601         if (sys_fuzz < sys_tick) {
4602                 msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec",
4603                         sys_fuzz * 1e6);
4604         }
4605 }
4606
4607
4608 /*
4609  * measure_tick_fuzz()
4610  *
4611  * measures the minimum time to read the clock (stored in sys_fuzz)
4612  * and returns the tick, the larger of the minimum increment observed
4613  * between successive clock readings and the time to read the clock.
4614  */
4615 double
4616 measure_tick_fuzz(void)
4617 {
4618         l_fp    minstep;        /* MINSTEP as l_fp */
4619         l_fp    val;            /* current seconds fraction */
4620         l_fp    last;           /* last seconds fraction */
4621         l_fp    ldiff;          /* val - last */
4622         double  tick;           /* computed tick value */
4623         double  diff;
4624         long    repeats;
4625         long    max_repeats;
4626         int     changes;
4627         int     i;              /* log2 precision */
4628
4629         tick = MAXSTEP;
4630         max_repeats = 0;
4631         repeats = 0;
4632         changes = 0;
4633         DTOLFP(MINSTEP, &minstep);
4634         get_systime(&last);
4635         for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) {
4636                 get_systime(&val);
4637                 ldiff = val;
4638                 L_SUB(&ldiff, &last);
4639                 last = val;
4640                 if (L_ISGT(&ldiff, &minstep)) {
4641                         max_repeats = max(repeats, max_repeats);
4642                         repeats = 0;
4643                         changes++;
4644                         LFPTOD(&ldiff, diff);
4645                         tick = min(diff, tick);
4646                 } else {
4647                         repeats++;
4648                 }
4649         }
4650         if (changes < MINCHANGES) {
4651                 msyslog(LOG_ERR, "Fatal error: precision could not be measured (MINSTEP too large?)");
4652                 exit(1);
4653         }
4654
4655         if (0 == max_repeats) {
4656                 set_sys_fuzz(tick);
4657         } else {
4658                 set_sys_fuzz(tick / max_repeats);
4659         }
4660
4661         return tick;
4662 }
4663
4664
4665 void
4666 set_sys_tick_precision(
4667         double tick
4668         )
4669 {
4670         int i;
4671
4672         if (tick > 1.) {
4673                 msyslog(LOG_ERR,
4674                         "unsupported tick %.3f > 1s ignored", tick);
4675                 return;
4676         }
4677         if (tick < measured_tick) {
4678                 msyslog(LOG_ERR,
4679                         "proto: tick %.3f less than measured tick %.3f, ignored",
4680                         tick, measured_tick);
4681                 return;
4682         } else if (tick > measured_tick) {
4683                 trunc_os_clock = TRUE;
4684                 msyslog(LOG_NOTICE,
4685                         "proto: truncating system clock to multiples of %.9f",
4686                         tick);
4687         }
4688         sys_tick = tick;
4689
4690         /*
4691          * Find the nearest power of two.
4692          */
4693         for (i = 0; tick <= 1; i--)
4694                 tick *= 2;
4695         if (tick - 1 > 1 - tick / 2)
4696                 i++;
4697
4698         sys_precision = (s_char)i;
4699 }
4700
4701
4702 /*
4703  * init_proto - initialize the protocol module's data
4704  */
4705 void
4706 init_proto(void)
4707 {
4708         l_fp    dummy;
4709         int     i;
4710
4711         /*
4712          * Fill in the sys_* stuff.  Default is don't listen to
4713          * broadcasting, require authentication.
4714          */
4715         set_sys_leap(LEAP_NOTINSYNC);
4716         sys_stratum = STRATUM_UNSPEC;
4717         memcpy(&sys_refid, "INIT", 4);
4718         sys_peer = NULL;
4719         sys_rootdelay = 0;
4720         sys_rootdisp = 0;
4721         L_CLR(&sys_reftime);
4722         sys_jitter = 0;
4723         measure_precision();
4724         get_systime(&dummy);
4725         sys_survivors = 0;
4726         sys_manycastserver = 0;
4727         sys_bclient = 0;
4728         sys_bdelay = BDELAY_DEFAULT;    /*[Bug 3031] delay cutoff */
4729         sys_authenticate = 1;
4730         sys_stattime = current_time;
4731         orphwait = current_time + sys_orphwait;
4732         proto_clr_stats();
4733         for (i = 0; i < MAX_TTL; ++i)
4734                 sys_ttl[i] = (u_char)((i * 256) / MAX_TTL);
4735         sys_ttlmax = (MAX_TTL - 1);
4736         hardpps_enable = 0;
4737         stats_control = 1;
4738 }
4739
4740
4741 /*
4742  * proto_config - configure the protocol module
4743  */
4744 void
4745 proto_config(
4746         int     item,
4747         u_long  value,
4748         double  dvalue,
4749         sockaddr_u *svalue
4750         )
4751 {
4752         /*
4753          * Figure out what he wants to change, then do it
4754          */
4755         DPRINTF(2, ("proto_config: code %d value %lu dvalue %lf\n",
4756                     item, value, dvalue));
4757
4758         switch (item) {
4759
4760         /*
4761          * enable and disable commands - arguments are Boolean.
4762          */
4763         case PROTO_AUTHENTICATE: /* authentication (auth) */
4764                 sys_authenticate = value;
4765                 break;
4766
4767         case PROTO_BROADCLIENT: /* broadcast client (bclient) */
4768                 sys_bclient = (int)value;
4769                 if (sys_bclient == 0)
4770                         io_unsetbclient();
4771                 else
4772                         io_setbclient();
4773                 break;
4774
4775 #ifdef REFCLOCK
4776         case PROTO_CAL:         /* refclock calibrate (calibrate) */
4777                 cal_enable = value;
4778                 break;
4779 #endif /* REFCLOCK */
4780
4781         case PROTO_KERNEL:      /* kernel discipline (kernel) */
4782                 select_loop(value);
4783                 break;
4784
4785         case PROTO_MONITOR:     /* monitoring (monitor) */
4786                 if (value)
4787                         mon_start(MON_ON);
4788                 else {
4789                         mon_stop(MON_ON);
4790                         if (mon_enabled)
4791                                 msyslog(LOG_WARNING,
4792                                         "restrict: 'monitor' cannot be disabled while 'limited' is enabled");
4793                 }
4794                 break;
4795
4796         case PROTO_NTP:         /* NTP discipline (ntp) */
4797                 ntp_enable = value;
4798                 break;
4799
4800         case PROTO_MODE7:       /* mode7 management (ntpdc) */
4801                 ntp_mode7 = value;
4802                 break;
4803
4804         case PROTO_PPS:         /* PPS discipline (pps) */
4805                 hardpps_enable = value;
4806                 break;
4807
4808         case PROTO_FILEGEN:     /* statistics (stats) */
4809                 stats_control = value;
4810                 break;
4811
4812         /*
4813          * tos command - arguments are double, sometimes cast to int
4814          */
4815
4816         case PROTO_BCPOLLBSTEP: /* Broadcast Poll Backstep gate (bcpollbstep) */
4817                 sys_bcpollbstep = (u_char)dvalue;
4818                 break;
4819
4820         case PROTO_BEACON:      /* manycast beacon (beacon) */
4821                 sys_beacon = (int)dvalue;
4822                 break;
4823
4824         case PROTO_BROADDELAY:  /* default broadcast delay (bdelay) */
4825                 sys_bdelay = (dvalue ? dvalue : BDELAY_DEFAULT);
4826                 break;
4827
4828         case PROTO_CEILING:     /* stratum ceiling (ceiling) */
4829                 sys_ceiling = (int)dvalue;
4830                 break;
4831
4832         case PROTO_COHORT:      /* cohort switch (cohort) */
4833                 sys_cohort = (int)dvalue;
4834                 break;
4835
4836         case PROTO_FLOOR:       /* stratum floor (floor) */
4837                 sys_floor = (int)dvalue;
4838                 break;
4839
4840         case PROTO_MAXCLOCK:    /* maximum candidates (maxclock) */
4841                 sys_maxclock = (int)dvalue;
4842                 break;
4843
4844         case PROTO_MAXDIST:     /* select threshold (maxdist) */
4845                 sys_maxdist = dvalue;
4846                 break;
4847
4848         case PROTO_CALLDELAY:   /* modem call delay (mdelay) */
4849                 break;          /* NOT USED */
4850
4851         case PROTO_MINCLOCK:    /* minimum candidates (minclock) */
4852                 sys_minclock = (int)dvalue;
4853                 break;
4854
4855         case PROTO_MINDISP:     /* minimum distance (mindist) */
4856                 sys_mindisp = dvalue;
4857                 break;
4858
4859         case PROTO_MINSANE:     /* minimum survivors (minsane) */
4860                 sys_minsane = (int)dvalue;
4861                 break;
4862
4863         case PROTO_ORPHAN:      /* orphan stratum (orphan) */
4864                 sys_orphan = (int)dvalue;
4865                 break;
4866
4867         case PROTO_ORPHWAIT:    /* orphan wait (orphwait) */
4868                 orphwait -= sys_orphwait;
4869                 sys_orphwait = (int)dvalue;
4870                 orphwait += sys_orphwait;
4871                 break;
4872
4873         /*
4874          * Miscellaneous commands
4875          */
4876         case PROTO_MULTICAST_ADD: /* add group address */
4877                 if (svalue != NULL)
4878                         io_multicast_add(svalue);
4879                 sys_bclient = 1;
4880                 break;
4881
4882         case PROTO_MULTICAST_DEL: /* delete group address */
4883                 if (svalue != NULL)
4884                         io_multicast_del(svalue);
4885                 break;
4886
4887         /*
4888          * Peer_clear Early policy choices
4889          */
4890
4891         case PROTO_PCEDIGEST:   /* Digest */
4892                 peer_clear_digest_early = value;
4893                 break;
4894
4895         /*
4896          * Unpeer Early policy choices
4897          */
4898
4899         case PROTO_UECRYPTO:    /* Crypto */
4900                 unpeer_crypto_early = value;
4901                 break;
4902
4903         case PROTO_UECRYPTONAK: /* Crypto_NAK */
4904                 unpeer_crypto_nak_early = value;
4905                 break;
4906
4907         case PROTO_UEDIGEST:    /* Digest */
4908                 unpeer_digest_early = value;
4909                 break;
4910
4911         default:
4912                 msyslog(LOG_NOTICE,
4913                     "proto: unsupported option %d", item);
4914         }
4915 }
4916
4917
4918 /*
4919  * proto_clr_stats - clear protocol stat counters
4920  */
4921 void
4922 proto_clr_stats(void)
4923 {
4924         sys_stattime = current_time;
4925         sys_received = 0;
4926         sys_processed = 0;
4927         sys_newversion = 0;
4928         sys_oldversion = 0;
4929         sys_declined = 0;
4930         sys_restricted = 0;
4931         sys_badlength = 0;
4932         sys_badauth = 0;
4933         sys_limitrejected = 0;
4934         sys_kodsent = 0;
4935 }