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